aboutsummaryrefslogtreecommitdiffstats
path: root/src/jcgp/backend/tests/OutputTests.java
blob: 95b7fc7d80928a66aa086590603d52d8b3650587 (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
package jcgp.backend.tests;

import static org.junit.Assert.assertTrue;
import jcgp.backend.function.DoubleArithmetic;
import jcgp.backend.population.Chromosome;
import jcgp.backend.population.Connection;
import jcgp.backend.population.Output;
import jcgp.backend.resources.ModifiableResources;

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;
	private static ModifiableResources resources;
	// these are the test values
	private final int outputValue = 10;
	private final int outputIndex = 2;

	@BeforeClass
	public static void setUpBeforeClass() {
		resources = new ModifiableResources();
		resources.setFunctionSet(new DoubleArithmetic());
		chromosome = new Chromosome(resources);
	}

	@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(0, new Connection() {

			@Override
			public Object getValue() {
				// test value
				return outputValue;
			}
		});

		assertTrue("Incorrect evaluation.", ((Integer) output.calculate()) == outputValue);
	}

	@Test
	public void connectionTest() {
		// set a new connection, check that it is correctly returned
		Connection newConn = new Connection() {

			@Override
			public Object getValue() {
				// blank
				return 0;
			}
		};
		output.setConnection(0, 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);
	}
}