From afa484021ba94d12e98da682a9ff69c3837d5dbb Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Fri, 14 Feb 2014 18:13:21 +0000 Subject: Generic data type functionality implemented. All tests were refactored to reflect this, and some chromosome tests were rewritten with more rigorous assertions. --- src/jcgp/tests/NodeTests.java | 99 ++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 68 deletions(-) (limited to 'src/jcgp/tests/NodeTests.java') diff --git a/src/jcgp/tests/NodeTests.java b/src/jcgp/tests/NodeTests.java index 5b378d2..9ea6769 100644 --- a/src/jcgp/tests/NodeTests.java +++ b/src/jcgp/tests/NodeTests.java @@ -6,11 +6,11 @@ import java.util.Random; import jcgp.Parameters; import jcgp.Utilities; +import jcgp.function.Addition; import jcgp.function.Function; -import jcgp.function.InsufficientArgumentsException; +import jcgp.function.Subtraction; import jcgp.population.Chromosome; import jcgp.population.Connection; -import jcgp.population.InsufficientConnectionsException; import jcgp.population.Node; import org.junit.Before; @@ -63,44 +63,25 @@ public class NodeTests { public void setUp() throws Exception { node = new Node(chromosome, 0, 0); // make node with anonymous addition function and hard-coded value connections - node.initialise(new Function() { - private int arity = 2; + node.initialise(new Addition(), + new Connection[]{new Connection() { - @Override - public int run(Connection... connections) { - - // add together the first n inputs if they exist, else throw exception - if (connections.length >= arity) { - return connections[0].getValue() + connections[1].getValue(); - } else { - throw new InsufficientArgumentsException(); - } - } + @Override + public Object getValue() { + // hardcode a value + return arg1; + } - @Override - public int getArity() { - // addition with arity 2 - return arity; - } + }, + new Connection() { - }, new Connection[]{new Connection() { + @Override + public Object getValue() { + // hardcode a value + return arg2; + } - @Override - public int getValue() { - // hardcode a value - return arg1; - } - - }, - new Connection() { - - @Override - public int getValue() { - // hardcode a value - return arg2; - } - - }}); + }}); } @Test @@ -115,8 +96,7 @@ public class NodeTests { Function f = new Function() { @Override - public int run(Connection... connections) - throws InsufficientConnectionsException { + public Object run(Connection... connections) { // blank return 0; } @@ -133,36 +113,19 @@ public class NodeTests { // check that the function returned by the node is f assertTrue("Incorrect function returned.", node.getFunction() == f); // check that it outputs 0 as it should - assertTrue("Incorrect function output.", node.getValue() == 0); + assertTrue("Incorrect function output.", ((Integer) node.getValue()) == 0); } @Test public void evaluationTest() { // check that addition is working - assertTrue("Node did not return expected value (sum of arguments).", node.getValue() == arg1 + arg2); + assertTrue("Node did not return expected value (sum of arguments). Output was: " + ((int) node.getValue()), + ((int) node.getValue()) == arg1 + arg2); // put in a different function, check the output has changed appropriately - node.setFunction(new Function() { + node.setFunction(new Subtraction()); - private int arity = 2; - - @Override - public int run(Connection... connections) throws InsufficientConnectionsException { - // add together the first n inputs if they exist, else throw exception - if (connections.length >= arity) { - return connections[0].getValue() - connections[1].getValue(); - } else { - throw new InsufficientArgumentsException(); - } - } - - @Override - public int getArity() { - return arity; - } - }); - - assertTrue("Node did not return expected value (difference of arguments).", node.getValue() == arg1 - arg2); + assertTrue("Node did not return expected value (difference of arguments).", ((Integer) node.getValue()) == arg1 - arg2); } @@ -173,7 +136,7 @@ public class NodeTests { conn0 = new Connection() { @Override - public int getValue() { + public Object getValue() { // blank return 0; } @@ -182,31 +145,31 @@ public class NodeTests { conn1 = new Connection() { @Override - public int getValue() { + public Object getValue() { // blank return 0; } }; node.initialise(null, conn0, conn1); - + assertTrue("Connection 0 is incorrect.", node.getConnection(0) == conn0); assertTrue("Connection 1 is incorrect.", node.getConnection(1) == conn1); - + // make yet another connection, set it randomly, check that it is one of the node's connections conn2 = new Connection() { @Override - public int getValue() { + public Object getValue() { // blank return 0; } }; node.setConnection(conn2); - + assertTrue("Connection was not found in node.", node.getConnection(0) == conn2 || node.getConnection(1) == conn2); - + } - + } -- cgit v1.2.3