diff options
Diffstat (limited to 'src/jcgp/backend/tests')
| -rw-r--r-- | src/jcgp/backend/tests/ChromosomeTests.java | 10 | ||||
| -rw-r--r-- | src/jcgp/backend/tests/NodeTests.java | 13 | ||||
| -rw-r--r-- | src/jcgp/backend/tests/TestFunctionSet.java | 54 | 
3 files changed, 65 insertions, 12 deletions
| diff --git a/src/jcgp/backend/tests/ChromosomeTests.java b/src/jcgp/backend/tests/ChromosomeTests.java index bc0c57d..c326805 100644 --- a/src/jcgp/backend/tests/ChromosomeTests.java +++ b/src/jcgp/backend/tests/ChromosomeTests.java @@ -2,7 +2,6 @@ package jcgp.backend.tests;  import static org.junit.Assert.assertTrue;  import static org.junit.Assert.fail; -import jcgp.backend.function.SymbolicRegressionFunctions;  import jcgp.backend.population.Chromosome;  import jcgp.backend.population.Connection;  import jcgp.backend.population.Input; @@ -46,7 +45,7 @@ public class ChromosomeTests {  	@BeforeClass  	public static void setUpBeforeClass() {  		resources = new ModifiableResources(); -		resources.setFunctionSet(new SymbolicRegressionFunctions()); +		resources.setFunctionSet(new TestFunctionSet());  	}  	@Before @@ -225,9 +224,12 @@ public class ChromosomeTests {  		chromosome.setInputs(5, 8, 4); +		Integer output0 = (Integer) chromosome.getOutput(0).calculate(); +		Integer output1 = (Integer) chromosome.getOutput(1).calculate(); +		  		// with this configuration, the outputs should be 13 and 25. -		assertTrue("Incorrect output returned.", (Integer) chromosome.getOutput(0).calculate() == 13); -		assertTrue("Incorrect output returned.", (Integer) chromosome.getOutput(1).calculate() == 25); +		assertTrue("Incorrect output returned: " + output0, output0 == 13.0); +		assertTrue("Incorrect output returned: " + output1, output1 == 25.0);  	}  	/** diff --git a/src/jcgp/backend/tests/NodeTests.java b/src/jcgp/backend/tests/NodeTests.java index c63b41e..ee940a0 100644 --- a/src/jcgp/backend/tests/NodeTests.java +++ b/src/jcgp/backend/tests/NodeTests.java @@ -1,8 +1,6 @@  package jcgp.backend.tests;  import static org.junit.Assert.assertTrue; -import jcgp.backend.exceptions.InvalidArgumentsException; -import jcgp.backend.function.SymbolicRegressionFunctions;  import jcgp.backend.function.Function;  import jcgp.backend.population.Chromosome;  import jcgp.backend.population.Connection; @@ -41,15 +39,15 @@ public class NodeTests {  	public static void setUpBeforeClass() {  		resources = new ModifiableResources(); -		resources.setFunctionSet(new SymbolicRegressionFunctions()); +		resources.setFunctionSet(new TestFunctionSet());  		chromosome = new Chromosome(resources);  	}  	@Before  	public void setUp() throws Exception {  		node = new Node(chromosome, 0, 0, resources.arity()); -		// make node with anonymous addition function and hard-coded value connections -		node.initialise(new SymbolicRegressionFunctions.Addition(), +		// make node with addition function and hard-coded value connections +		node.initialise(resources.getFunction(0),  				new Connection[]{new Connection() {  					@Override @@ -107,7 +105,7 @@ public class NodeTests {  				((int) node.getValue()) == arg1 + arg2);  		// put in a different function, check the output has changed appropriately -		node.setFunction(new SymbolicRegressionFunctions.Subtraction()); +		node.setFunction(resources.getFunction(1));  		assertTrue("Node did not return expected value (difference of arguments).", ((Integer) node.getValue()) == arg1 - arg2); @@ -136,8 +134,7 @@ public class NodeTests {  		Function function = new Function() {  			@Override -			public Object run(Object... connections) -					throws InvalidArgumentsException { +			public Object run(Object... connections) {  				// blank  				return null;  			} diff --git a/src/jcgp/backend/tests/TestFunctionSet.java b/src/jcgp/backend/tests/TestFunctionSet.java new file mode 100644 index 0000000..1910d2a --- /dev/null +++ b/src/jcgp/backend/tests/TestFunctionSet.java @@ -0,0 +1,54 @@ +package jcgp.backend.tests; + +import jcgp.backend.function.Function; +import jcgp.backend.function.FunctionSet; + +public class TestFunctionSet extends FunctionSet { +	 +	public TestFunctionSet() { +		 +		functionList = new Function[] { +			new Function() { +				@Override +				public Object run(Object... args) { +					return (Integer) args[0] + (Integer) args[1]; +				} +				@Override +				public int getArity() { +					return 2; +				} +			}, +			new Function() { +				@Override +				public Object run(Object... args) { +					return (Integer) args[0] - (Integer) args[1]; +				} +				@Override +				public int getArity() { +					return 2; +				} +			}, +			new Function() { +				@Override +				public Object run(Object... args) { +					return (Integer) args[0] * (Integer) args[1]; +				} +				@Override +				public int getArity() { +					return 2; +				} +			}, +			new Function() { +				@Override +				public Object run(Object... args) { +					return (Integer) args[0] / (Integer) args[1]; +				} +				@Override +				public int getArity() { +					return 2; +				} +			} +		}; +		enableAll(); +	}	 +}
\ No newline at end of file | 
