diff options
Diffstat (limited to 'src/jcgp/backend/tests/NodeTests.java')
-rw-r--r-- | src/jcgp/backend/tests/NodeTests.java | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/src/jcgp/backend/tests/NodeTests.java b/src/jcgp/backend/tests/NodeTests.java new file mode 100644 index 0000000..7121e81 --- /dev/null +++ b/src/jcgp/backend/tests/NodeTests.java @@ -0,0 +1,196 @@ +package jcgp.backend.tests; + +import static org.junit.Assert.assertTrue; +import jcgp.JCGP.Resources; +import jcgp.backend.function.Arithmetic; +import jcgp.backend.function.Function; +import jcgp.backend.population.Chromosome; +import jcgp.backend.population.Connection; +import jcgp.backend.population.Node; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * + * Tests which cover the behaviour specified for a node. + * + * - A node should contain read-only row and column values which are set upon construction. + * - It should contain a fully accessible Function object. + * - It should contain a set of connections which can be initialised and randomly + * modified. It should be able to return an addressed connection. + * - It should be able to compute a value using its function with its connections as arguments. + * + * WARNING: changing parameters may cause the tests to incorrectly fail! + * + * @author Eduardo Pedroni + * + */ +public class NodeTests { + + private Node node; + private static Chromosome chromosome; + private static Resources resources; + // these numbers will be used by the node under test + private final int arg1 = 2; + private final int arg2 = 5; + + @BeforeClass + public static void setUpBeforeClass() { + + resources = new Resources(); + + chromosome = new Chromosome(resources); + } + + @Before + public void setUp() throws Exception { + node = new Node(chromosome, 0, 0, resources.getInt("arity")); + // make node with anonymous addition function and hard-coded value connections + node.initialise(new Arithmetic.Addition(), + new Connection[]{new Connection() { + + @Override + public Object getValue() { + // hardcode a value + return arg1; + } + + @Override + public String getDescription() { + // blank + return null; + } + + }, + new Connection() { + + @Override + public Object getValue() { + // hardcode a value + return arg2; + } + + @Override + public String getDescription() { + // blank + return null; + } + + }}); + } + + @Test + public void rowAndColumnTest() { + assertTrue("Incorrect row.", node.getRow() == 0); + assertTrue("Incorrect column.", node.getColumn() == 0); + } + + @Test + public void functionTest() { + // make a new function and assign to node + Function f = new Function() { + + @Override + public Object run(Connection... connections) { + // blank + return 0; + } + + @Override + public int getArity() { + // blank + return 0; + } + + @Override + public String getName() { + // blank + return null; + } + }; + + node.setFunction(f); + + // 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.", ((Integer) node.getValue()) == 0); + } + + @Test + public void evaluationTest() { + // check that addition is working + 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 Arithmetic.Subtraction()); + + assertTrue("Node did not return expected value (difference of arguments).", ((Integer) node.getValue()) == arg1 - arg2); + + } + + @Test + public void connectionsTest() { + // make new blank connections, check that they are returned correctly when addressed + Connection conn0, conn1, conn2; + conn0 = new Connection() { + + @Override + public Object getValue() { + // blank + return 0; + } + + @Override + public String getDescription() { + // blank + return null; + } + + }; + conn1 = new Connection() { + + @Override + public Object getValue() { + // blank + return 0; + } + + @Override + public String getDescription() { + // blank + return null; + } + + }; + 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 Object getValue() { + // blank + return 0; + } + + @Override + public String getDescription() { + // blank + return null; + } + }; + node.setConnection(resources.getRandomInt(resources.getInt("arity")), conn2); + + assertTrue("Connection was not found in node.", node.getConnection(0) == conn2 || node.getConnection(1) == conn2); + + } + + +} |