aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/tests/OutputTests.java
blob: 06295ae2a1c0b616a6ebdc9d647af64d9c39e5c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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);
	}
}