diff options
Diffstat (limited to 'src/jcgp/tests/PopulationTests.java')
-rw-r--r-- | src/jcgp/tests/PopulationTests.java | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/src/jcgp/tests/PopulationTests.java b/src/jcgp/tests/PopulationTests.java new file mode 100644 index 0000000..16a8d89 --- /dev/null +++ b/src/jcgp/tests/PopulationTests.java @@ -0,0 +1,94 @@ +package jcgp.tests; + +import static org.junit.Assert.*; + +import java.util.Random; + +import jcgp.Parameters; +import jcgp.Utilities; +import jcgp.function.Addition; +import jcgp.function.FunctionSet; +import jcgp.function.Subtraction; +import jcgp.population.Chromosome; +import jcgp.population.Population; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * + * Tests which cover the behaviour specified for a population. + * + * - A population is a collection of chromosomes. It should be Iterable + * so the chromosomes can be accessed. Chromosomes should never be removed + * via the iterator. + * - When constructed with no arguments, it should generate populationSize + * random chromosomes. + * - If an array of chromosomes is passed as an argument to the constructor, + * it should use those chromosomes as the population. + * + * + * @author Eduardo Pedroni + * + */ +public class PopulationTests { + + private Population population; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + // initialise function set + FunctionSet functionSet = new FunctionSet(new Addition(), new Subtraction()); + + // initialise utilities + Utilities.setResources(new Random(1234), functionSet); + + // initialise parameters + Parameters.setColumns(20); + Parameters.setRows(20); + Parameters.setInputs(2); + Parameters.setOutputs(4); + Parameters.setLevelsBack(1); + Parameters.setMutationRate(10); + Parameters.setTotalGenerations(100); + Parameters.setTotalRuns(5); + Parameters.setPopulationSize(5); + Parameters.setMaxArity(functionSet.getMaxArity()); + } + + @Before + public void setUp() throws Exception { + population = new Population(); + } + + @Test + public void iterableTest() { + // check that Population is really Iterable + assertTrue("Population must implement Iterable.", population instanceof Iterable); + + // iterate through, check that different chromosomes are returned and no chromosome can be removed + Chromosome comparison = null; + int iterationCount = 0; + for (Chromosome chromosome : population) { + iterationCount++; + assertTrue("Same chromosome returned twice.", comparison != chromosome); + comparison = chromosome; + population.iterator().remove(); + assertTrue("Chromosome removed.", comparison == chromosome); + } + // check that all chromosomes were iterated through + assertTrue("Not enough itearations occurred.", iterationCount == Parameters.getPopulationSize()); + } + + @Test + public void contentsTest() { + // check that the constructor really generates populationSize chromosomes when none is given + int populationCount = 0; + for (Chromosome chromosome : population) { + populationCount++; + } + assertTrue("Incorrect number of chromosomes generated.", populationCount == Parameters.getPopulationSize()); + } + +} |