diff options
Diffstat (limited to 'src/jcgp/tests/OutputTests.java')
-rw-r--r-- | src/jcgp/tests/OutputTests.java | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/jcgp/tests/OutputTests.java b/src/jcgp/tests/OutputTests.java new file mode 100644 index 0000000..06295ae --- /dev/null +++ b/src/jcgp/tests/OutputTests.java @@ -0,0 +1,108 @@ +package jcgp.tests; + +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Random; + +import jcgp.Parameters; +import jcgp.Utilities; +import jcgp.population.Chromosome; +import jcgp.population.Connection; +import jcgp.population.Output; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * + * Tests which cover the behaviour specified for an output. + * + * - An output contains a single source Connection, which can be set and got. + * - It should return the value of its source connection. + * - It must be addressable by an index set upon construction only. + * + * + * @author Eduardo Pedroni + * + */ +public class OutputTests { + + private Output output; + private static Chromosome chromosome; + // these are the test values + private final int outputValue = 10; + private final int outputIndex = 2; + + @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 { + output = new Output(chromosome, outputIndex); + } + + @Test + public void evaluationsTest() { + // set source connection, check that the appropriate value is returned + output.setConnection(new Connection() { + + @Override + public int getValue() { + // test value + return outputValue; + } + + @Override + public void getActive(ArrayList<Connection> activeNodes) { + // blank + } + }); + + assertTrue("Incorrect evaluation.", output.calculate() == outputValue); + } + + @Test + public void connectionTest() { + // set a new connection, check that it is correctly returned + Connection newConn = new Connection() { + + @Override + public int getValue() { + // blank + return 0; + } + + @Override + public void getActive(ArrayList<Connection> activeNodes) { + // blank + } + }; + output.setConnection(newConn); + + assertTrue("Incorrect connection returned.", output.getSource() == newConn); + } + + @Test + public void indexTest() { + // check that the index returned is the one passed to the constructor + assertTrue("Incorrect index returned.", output.getIndex() == outputIndex); + } +} |