24
src/test/java/eva2/problems/F23ProblemTest.java
Normal file
24
src/test/java/eva2/problems/F23ProblemTest.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package eva2.problems;
|
||||
|
||||
import eva2.tools.math.Mathematics;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class F23ProblemTest extends TestCase {
|
||||
|
||||
/**
|
||||
* The Generalized Schaffer function has its minimum
|
||||
* at x_{i} = 0
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public void testEvaluate() throws Exception {
|
||||
F23Problem problem = new F23Problem();
|
||||
|
||||
for (int i = 1; i < 100; i++) {
|
||||
double[] x = Mathematics.zeroes(i);
|
||||
double[] res = problem.evaluate(x);
|
||||
assertEquals(1, res.length);
|
||||
assertEquals(0.0, res[0]);
|
||||
}
|
||||
}
|
||||
}
|
24
src/test/java/eva2/problems/F3ProblemTest.java
Normal file
24
src/test/java/eva2/problems/F3ProblemTest.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package eva2.problems;
|
||||
|
||||
import eva2.tools.math.Mathematics;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class F3ProblemTest extends TestCase {
|
||||
|
||||
/**
|
||||
* The Step function has its minimum
|
||||
* at x_{i} = 0
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public void testEvaluate() throws Exception {
|
||||
F3Problem problem = new F3Problem();
|
||||
|
||||
for (int i = 1; i < 100; i++) {
|
||||
double[] x = Mathematics.zeroes(i);
|
||||
double[] res = problem.evaluate(x);
|
||||
assertEquals(1, res.length);
|
||||
assertEquals(0.0, res[0]);
|
||||
}
|
||||
}
|
||||
}
|
111
src/test/java/eva2/tools/SerializerTest.java
Normal file
111
src/test/java/eva2/tools/SerializerTest.java
Normal file
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* To change this template, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package eva2.tools;
|
||||
|
||||
import java.io.*;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author becker
|
||||
*/
|
||||
public class SerializerTest {
|
||||
|
||||
private ExampleDataStruct dataStructObject;
|
||||
|
||||
public SerializerTest() {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
// Create a eva2.problems.simple object graph
|
||||
dataStructObject = new ExampleDataStruct();
|
||||
dataStructObject.message = "hello world";
|
||||
dataStructObject.data = new int[]{1, 2, 3, 4};
|
||||
dataStructObject.other = new ExampleDataStruct();
|
||||
dataStructObject.other.message = "nested structure";
|
||||
dataStructObject.other.data = new int[]{9, 8, 7};
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of deepClone method, of class Serializer.
|
||||
*/
|
||||
@Test
|
||||
public void testDeepClone() {
|
||||
ExampleDataStruct copy = (ExampleDataStruct) Serializer.deepClone(dataStructObject);
|
||||
assertNotSame("Objects are the same", copy, dataStructObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of storeString method, of class Serializer.
|
||||
*/
|
||||
@Test
|
||||
public void testStoreString() {
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
String data = "This is a test string to write.";
|
||||
Serializer.storeString(outStream, data);
|
||||
String writtenData = new String(outStream.toByteArray());
|
||||
assertEquals("Wrong data in stream", data, writtenData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of loadString method, of class Serializer.
|
||||
*/
|
||||
@Test
|
||||
public void testLoadString() {
|
||||
String data = "This is a test string to read.";
|
||||
InputStream inputStream = new ByteArrayInputStream(data.getBytes());
|
||||
String readData = Serializer.loadString(inputStream);
|
||||
assertEquals("Wrong data in stream", data, readData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of storeObject method, of class Serializer.
|
||||
*/
|
||||
@Test
|
||||
public void testStoreObject() {
|
||||
OutputStream outStream = null;
|
||||
Serializable s = null;
|
||||
Serializer.storeObject(outStream, s);
|
||||
// TODO review the generated test code and remove the default call to fail.
|
||||
//fail("The test case is a prototype.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of loadObject method, of class Serializer.
|
||||
*/
|
||||
@Test
|
||||
public void testLoadObject_InputStream() {
|
||||
InputStream inputStream = null;
|
||||
Object expResult = null;
|
||||
Object result = Serializer.loadObject(inputStream);
|
||||
assertEquals(expResult, result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a eva2.problems.simple serializable data structure that we use below for testing the methods above
|
||||
*
|
||||
*/
|
||||
class ExampleDataStruct implements Serializable {
|
||||
|
||||
String message;
|
||||
int[] data;
|
||||
ExampleDataStruct other;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String msg = message;
|
||||
for (int i = 0; i < data.length; i++) {
|
||||
msg += " " + data[i];
|
||||
}
|
||||
if (other != null) {
|
||||
msg += "\n\t" + other.toString();
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
89
src/test/java/eva2/tools/StringToolsTest.java
Normal file
89
src/test/java/eva2/tools/StringToolsTest.java
Normal file
@@ -0,0 +1,89 @@
|
||||
package eva2.tools;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author becker
|
||||
*/
|
||||
public class StringToolsTest {
|
||||
|
||||
public StringToolsTest() {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of humaniseCamelCase method, of class StringTools.
|
||||
*/
|
||||
@Test
|
||||
public void testHumaniseCamelCase() {
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put("camelCase", "Camel Case");
|
||||
map.put("Camel Case", "Camel Case");
|
||||
map.put("thisIsAwesome", "This Is Awesome");
|
||||
map.put("THQIsNice", "THQ Is Nice");
|
||||
map.put("iLikeABC", "I Like ABC");
|
||||
map.put("foo2Bar", "Foo2 Bar");
|
||||
map.put("phi1", "Phi1");
|
||||
|
||||
String key, value;
|
||||
for (Object o : map.entrySet()) {
|
||||
Map.Entry pairs = (Map.Entry) o;
|
||||
key = (String) pairs.getKey();
|
||||
value = (String) pairs.getValue();
|
||||
String result = StringTools.humaniseCamelCase(key);
|
||||
assertEquals(value, result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of humaniseCamelCase method, of class StringTools.
|
||||
*/
|
||||
@Test
|
||||
public void testConvertToUnderscore() {
|
||||
HashMap<String, String> map = new HashMap<>();
|
||||
map.put("camelCase", "camel-case");
|
||||
map.put("CamelCase", "camel-case");
|
||||
map.put("thisIsAwesome", "this-is-awesome");
|
||||
map.put("THQIsNice", "thq-is-nice");
|
||||
map.put("iLikeABC", "i-like-abc");
|
||||
|
||||
String key, value;
|
||||
for (Object o : map.entrySet()) {
|
||||
Map.Entry pairs = (Map.Entry) o;
|
||||
key = (String) pairs.getKey();
|
||||
value = (String) pairs.getValue();
|
||||
String result = StringTools.convertToUnderscore(key);
|
||||
assertEquals(value, result);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test of upcaseFirst method, of class StringTools.
|
||||
*/
|
||||
@Test
|
||||
public void testUpcaseFirst() {
|
||||
assertEquals("Camel", StringTools.upcaseFirst("camel"));
|
||||
assertEquals("UpWeGo", StringTools.upcaseFirst("upWeGo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCutClassName() {
|
||||
assertEquals("StringTools", StringTools.cutClassName("eva2.tools.StringTools"));
|
||||
assertEquals("RandomClass", StringTools.cutClassName("eva2.optimization.operator.RandomClass"));
|
||||
}
|
||||
}
|
123
src/test/java/eva2/tools/math/MathematicsTest.java
Normal file
123
src/test/java/eva2/tools/math/MathematicsTest.java
Normal file
@@ -0,0 +1,123 @@
|
||||
package eva2.tools.math;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class MathematicsTest {
|
||||
|
||||
@Test
|
||||
public void testEuclideanDist() throws Exception {
|
||||
double[] values1 = {6.0, 51.0, 3.0};
|
||||
double[] values2 = {1.9, 99.0, 2.9};
|
||||
|
||||
assertEquals(48.174889, Mathematics.euclideanDist(values1, values2), 0.00001);
|
||||
assertEquals(48.174889, Mathematics.euclideanDist(values2, values1), 0.00001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMax() throws Exception {
|
||||
double[] vals = {1,2,3,4,5,6,4,3,2,2,12};
|
||||
assertEquals(12.0, Mathematics.max(vals), 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMean() throws Exception {
|
||||
double[] vals = {2.0,3.05,4.9,7.8,12.7};
|
||||
assertEquals(6.09, Mathematics.mean(vals), 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZeroes() throws Exception {
|
||||
double[] vals = Mathematics.zeroes(10);
|
||||
for (final double val : vals) {
|
||||
assertEquals(0.0, val, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContains() throws Exception {
|
||||
assertTrue(Mathematics.contains(new int[]{1,2,3,4,5}, 4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMakeVector() throws Exception {
|
||||
double[] vals = Mathematics.makeVector(42.23, 10);
|
||||
assertEquals(10, vals.length);
|
||||
for (final double val : vals) {
|
||||
assertEquals(42.23, val, 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMeanVect() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMedian() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMedian2() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVariance() throws Exception {
|
||||
double[] values = {9.8,9.2,12.3,15.7,3.14};
|
||||
|
||||
assertEquals(21.37892, Mathematics.variance(values), 0.000001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStdDev() throws Exception {
|
||||
double[] values = {12.9,13.5,19.8,12.3,10.7};
|
||||
|
||||
// Mean
|
||||
assertEquals(13.84, Mathematics.mean(values), 0.000001);
|
||||
// Variance of sample
|
||||
assertEquals(12.188, Mathematics.variance(values), 0.000001);
|
||||
// Sample std.dev.
|
||||
assertEquals(3.491131, Mathematics.stdDev(values), 0.000001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMin() throws Exception {
|
||||
double[] values = {1.9,2.8,3.7,4.6,5.5};
|
||||
|
||||
assertEquals(1.9, Mathematics.min(values), 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProjectToRange() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScaleRange() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSum() throws Exception {
|
||||
double[] values = {1.9,2.8,3.7,4.6,5.5};
|
||||
|
||||
assertEquals(18.5, Mathematics.sum(values), 0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScale() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTTestEqSizeEqVar() {
|
||||
double[] values1 = {6,6,2,7,8,8,2,3,5,7,10,5,4,7,5,7,4,5,2,5,3,4,4,4,4};
|
||||
double[] values2 = {6,11,8,5,11,8,10,7,4,3,7,6,10,10,6,5,10,11,13,8,5,11,7,8,5};
|
||||
|
||||
assertEquals(-4.05593, Mathematics.tTestEqSizeEqVar(values1, values2), 0.00001);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user