aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/tests/NodeTests.java
diff options
context:
space:
mode:
authorEduardo Pedroni <ep625@york.ac.uk>2014-02-12 11:57:18 +0000
committerEduardo Pedroni <ep625@york.ac.uk>2014-02-12 11:57:18 +0000
commit727801ab8002481fd2d213b45c3b43225b0f73bb (patch)
tree88f30ff057cb433f4a83a2e2acd4b5391c8935e7 /src/jcgp/tests/NodeTests.java
parentccdecd80ffe482fbe994515e98eeae68fb4ca401 (diff)
Added a few more tests, be back after the meeting.
Diffstat (limited to 'src/jcgp/tests/NodeTests.java')
-rw-r--r--src/jcgp/tests/NodeTests.java212
1 files changed, 208 insertions, 4 deletions
diff --git a/src/jcgp/tests/NodeTests.java b/src/jcgp/tests/NodeTests.java
index 921879f..ecc87ff 100644
--- a/src/jcgp/tests/NodeTests.java
+++ b/src/jcgp/tests/NodeTests.java
@@ -1,8 +1,21 @@
package jcgp.tests;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Random;
+
+import jcgp.Parameters;
+import jcgp.Utilities;
+import jcgp.function.Function;
+import jcgp.function.InsufficientArgumentsException;
+import jcgp.population.Chromosome;
+import jcgp.population.Connection;
+import jcgp.population.InsufficientConnectionsException;
+import jcgp.population.Node;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
/**
@@ -12,7 +25,8 @@ import org.junit.Test;
* - 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.
+ * 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!
*
@@ -21,13 +35,203 @@ import org.junit.Test;
*/
public class NodeTests {
+ private Node node;
+ private static Chromosome chromosome;
+ // 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() {
+ // initialise utilities
+ Utilities.setResources(new Random(1234), null);
+
+ // initialise parameters
+ Parameters.setColumns(0);
+ Parameters.setRows(0);
+ Parameters.setInputs(0);
+ Parameters.setOutputs(0);
+ Parameters.setLevelsBack(0);
+ Parameters.setMutationRate(10);
+ Parameters.setTotalGenerations(100);
+ Parameters.setTotalRuns(5);
+ Parameters.setMaxArity(2);
+
+ chromosome = new Chromosome();
+ }
+
@Before
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;
+
+ @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 int getArity() {
+ // addition with arity 2
+ return arity;
+ }
+
+ }, new Connection[]{new Connection() {
+
+ @Override
+ public int getValue() {
+ // hardcode a value
+ return arg1;
+ }
+
+ @Override
+ public void getActive(ArrayList<Connection> activeNodes) {
+ // irrelevant for this test
+ }
+
+ },
+ new Connection() {
+
+ @Override
+ public int getValue() {
+ // hardcode a value
+ return arg2;
+ }
+
+ @Override
+ public void getActive(ArrayList<Connection> activeNodes) {
+ // irrelevant for this test
+ }
+
+ }});
+ }
+
+ @Test
+ public void rowAndColumnTest() {
+ assertTrue("Incorrect row.", node.getRow() == 0);
+ assertTrue("Incorrect column.", node.getColumn() == 0);
}
@Test
- public void test() {
+ public void functionTest() {
+ // make a new function and assign to node
+ Function f = new Function() {
+
+ @Override
+ public int run(Connection... connections)
+ throws InsufficientConnectionsException {
+ // blank
+ return 0;
+ }
+
+ @Override
+ public int getArity() {
+ // blank
+ return 0;
+ }
+ };
+
+ 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.", 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);
+
+ // put in a different function, check the output has changed appropriately
+ node.setFunction(new Function() {
+
+ 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);
+
+ }
+
+ @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 int getValue() {
+ // blank
+ return 0;
+ }
+
+ @Override
+ public void getActive(ArrayList<Connection> activeNodes) {
+ // blank
+
+ }
+ };
+ conn1 = new Connection() {
+
+ @Override
+ public int getValue() {
+ // blank
+ return 0;
+ }
+
+ @Override
+ public void getActive(ArrayList<Connection> activeNodes) {
+ // blank
+
+ }
+ };
+ 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() {
+ // blank
+ return 0;
+ }
+
+ @Override
+ public void getActive(ArrayList<Connection> activeNodes) {
+ // blank
+
+ }
+ };
+ node.setConnection(conn2);
+
+ assertTrue("Connection was not found in node.", node.getConnection(0) == conn2 || node.getConnection(1) == conn2);
}