-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from FedorSmirnovUdacity/master
Permutation and IntegerMapGenotype tests and changes
- Loading branch information
Showing
4 changed files
with
456 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
264 changes: 264 additions & 0 deletions
264
opt4j-core/src/test/java/org/opt4j/core/genotype/IntegerMapGenotypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,264 @@ | ||
package org.opt4j.core.genotype; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
import org.junit.Test; | ||
|
||
public class IntegerMapGenotypeTest { | ||
|
||
class MockObject { | ||
private final double field1; | ||
private final int field2; | ||
|
||
public MockObject(double field1, int field2) { | ||
this.field1 = field1; | ||
this.field2 = field2; | ||
} | ||
|
||
public double getField1() { | ||
return field1; | ||
} | ||
|
||
public int getField2() { | ||
return field2; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "mock"; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
final int prime = 31; | ||
int result = 1; | ||
result = prime * result + getOuterType().hashCode(); | ||
long temp; | ||
temp = Double.doubleToLongBits(field1); | ||
result = prime * result + (int) (temp ^ (temp >>> 32)); | ||
result = prime * result + field2; | ||
return result; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) | ||
return true; | ||
if (obj == null) | ||
return false; | ||
if (getClass() != obj.getClass()) | ||
return false; | ||
MockObject other = (MockObject) obj; | ||
if (!getOuterType().equals(other.getOuterType())) | ||
return false; | ||
if (Double.doubleToLongBits(field1) != Double.doubleToLongBits(other.field1)) | ||
return false; | ||
if (field2 != other.field2) | ||
return false; | ||
return true; | ||
} | ||
|
||
private IntegerMapGenotypeTest getOuterType() { | ||
return IntegerMapGenotypeTest.this; | ||
} | ||
} | ||
|
||
@Test(expected = UnsupportedOperationException.class) | ||
public void testForbiddenInit() { | ||
Random rand = new Random(); | ||
int[] lowerBounds = { 1, 2, 3 }; | ||
int[] upperBounds = { 2, 3, 4 }; | ||
Bounds<Integer> bounds = new IntegerBounds(lowerBounds, upperBounds); | ||
MockObject mockup1 = new MockObject(1.0, 1); | ||
MockObject mockup2 = new MockObject(2.0, 2); | ||
MockObject mockup3 = new MockObject(3.0, 3); | ||
List<MockObject> inputList = new ArrayList<IntegerMapGenotypeTest.MockObject>(); | ||
inputList.add(mockup1); | ||
inputList.add(mockup2); | ||
inputList.add(mockup3); | ||
IntegerMapGenotype<MockObject> integerMapGenotype = new IntegerMapGenotype<IntegerMapGenotypeTest.MockObject>( | ||
inputList, bounds); | ||
integerMapGenotype.init(rand, 3); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testNonUniqueKeys() { | ||
int[] lowerBounds = { 1, 2, 3 }; | ||
int[] upperBounds = { 2, 3, 4 }; | ||
Bounds<Integer> bounds = new IntegerBounds(lowerBounds, upperBounds); | ||
MockObject mockup1 = new MockObject(1.0, 1); | ||
MockObject mockup2 = new MockObject(2.0, 2); | ||
MockObject mockup3 = new MockObject(3.0, 3); | ||
List<MockObject> inputList = new ArrayList<IntegerMapGenotypeTest.MockObject>(); | ||
inputList.add(mockup1); | ||
inputList.add(mockup2); | ||
inputList.add(mockup3); | ||
inputList.add(mockup1); | ||
new IntegerMapGenotype<IntegerMapGenotypeTest.MockObject>(inputList, bounds); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testGetIndexInvalidKey() { | ||
int[] lowerBounds = { 1, 2, 3 }; | ||
int[] upperBounds = { 2, 3, 4 }; | ||
Bounds<Integer> bounds = new IntegerBounds(lowerBounds, upperBounds); | ||
MockObject mockup1 = new MockObject(1.0, 1); | ||
MockObject mockup2 = new MockObject(2.0, 2); | ||
MockObject mockup3 = new MockObject(3.0, 3); | ||
MockObject mockup4 = new MockObject(4.0, 4); | ||
List<MockObject> inputList = new ArrayList<IntegerMapGenotypeTest.MockObject>(); | ||
inputList.add(mockup1); | ||
inputList.add(mockup2); | ||
inputList.add(mockup3); | ||
IntegerMapGenotype<MockObject> integerMapGenotype = new IntegerMapGenotype<IntegerMapGenotypeTest.MockObject>( | ||
inputList, bounds); | ||
integerMapGenotype.getIndexOf(mockup4); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testSetInvalidKey() { | ||
int[] lowerBounds = { 1, 2, 3 }; | ||
int[] upperBounds = { 2, 3, 4 }; | ||
Bounds<Integer> bounds = new IntegerBounds(lowerBounds, upperBounds); | ||
MockObject mockup1 = new MockObject(1.0, 1); | ||
MockObject mockup2 = new MockObject(2.0, 2); | ||
MockObject mockup3 = new MockObject(3.0, 3); | ||
MockObject mockup4 = new MockObject(4.0, 4); | ||
List<MockObject> inputList = new ArrayList<IntegerMapGenotypeTest.MockObject>(); | ||
inputList.add(mockup1); | ||
inputList.add(mockup2); | ||
inputList.add(mockup3); | ||
IntegerMapGenotype<MockObject> integerMapGenotype = new IntegerMapGenotype<IntegerMapGenotypeTest.MockObject>( | ||
inputList, bounds); | ||
integerMapGenotype.setValue(mockup4, 1); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testSetOutOfLowerBoundsValue() { | ||
int[] lowerBounds = { 1, 2, 3 }; | ||
int[] upperBounds = { 2, 3, 4 }; | ||
Bounds<Integer> bounds = new IntegerBounds(lowerBounds, upperBounds); | ||
MockObject mockup1 = new MockObject(1.0, 1); | ||
MockObject mockup2 = new MockObject(2.0, 2); | ||
MockObject mockup3 = new MockObject(3.0, 3); | ||
List<MockObject> inputList = new ArrayList<IntegerMapGenotypeTest.MockObject>(); | ||
inputList.add(mockup1); | ||
inputList.add(mockup2); | ||
inputList.add(mockup3); | ||
IntegerMapGenotype<MockObject> integerMapGenotype = new IntegerMapGenotype<IntegerMapGenotypeTest.MockObject>( | ||
inputList, bounds); | ||
integerMapGenotype.setValue(mockup3, -1); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testSetOutOfUpperBoundsValue() { | ||
int[] lowerBounds = { 1, 2, 3 }; | ||
int[] upperBounds = { 2, 3, 4 }; | ||
Bounds<Integer> bounds = new IntegerBounds(lowerBounds, upperBounds); | ||
MockObject mockup1 = new MockObject(1.0, 1); | ||
MockObject mockup2 = new MockObject(2.0, 2); | ||
MockObject mockup3 = new MockObject(3.0, 3); | ||
List<MockObject> inputList = new ArrayList<IntegerMapGenotypeTest.MockObject>(); | ||
inputList.add(mockup1); | ||
inputList.add(mockup2); | ||
inputList.add(mockup3); | ||
IntegerMapGenotype<MockObject> integerMapGenotype = new IntegerMapGenotype<IntegerMapGenotypeTest.MockObject>( | ||
inputList, bounds); | ||
integerMapGenotype.setValue(mockup3, 5); | ||
} | ||
|
||
@Test | ||
public void testToString() { | ||
Random rand = new Random(); | ||
int[] lowerBounds = { 1, 2, 3 }; | ||
int[] upperBounds = { 1, 2, 3 }; | ||
Bounds<Integer> bounds = new IntegerBounds(lowerBounds, upperBounds); | ||
MockObject mockup1 = new MockObject(1.0, 1); | ||
MockObject mockup2 = new MockObject(2.0, 2); | ||
MockObject mockup3 = new MockObject(3.0, 3); | ||
List<MockObject> inputList = new ArrayList<IntegerMapGenotypeTest.MockObject>(); | ||
inputList.add(mockup1); | ||
inputList.add(mockup2); | ||
inputList.add(mockup3); | ||
IntegerMapGenotype<MockObject> integerMapGenotype = new IntegerMapGenotype<IntegerMapGenotypeTest.MockObject>( | ||
inputList, bounds); | ||
integerMapGenotype.init(rand); | ||
assertEquals("[mock=1.0;mock=2.0;mock=3.0;]", integerMapGenotype.toString()); | ||
} | ||
|
||
@Test | ||
public void testFixedBoundConstructor() { | ||
MockObject mockup1 = new MockObject(1.0, 1); | ||
MockObject mockup2 = new MockObject(2.0, 2); | ||
MockObject mockup3 = new MockObject(3.0, 3); | ||
List<MockObject> inputList = new ArrayList<IntegerMapGenotypeTest.MockObject>(); | ||
inputList.add(mockup1); | ||
inputList.add(mockup2); | ||
inputList.add(mockup3); | ||
IntegerMapGenotype<MockObject> integerMapGenotype = new IntegerMapGenotype<IntegerMapGenotypeTest.MockObject>( | ||
inputList, 1, 3); | ||
for (int i = 1; i < 4; i++) { | ||
assertEquals(1, integerMapGenotype.getLowerBound(i)); | ||
assertEquals(3, integerMapGenotype.getUpperBound(i)); | ||
} | ||
} | ||
|
||
@Test | ||
public void testBoundConstructor() { | ||
Random rand = new Random(); | ||
int[] lowerBounds = { 1, 2, 3 }; | ||
int[] upperBounds = { 2, 3, 4 }; | ||
Bounds<Integer> bounds = new IntegerBounds(lowerBounds, upperBounds); | ||
MockObject mockup1 = new MockObject(1.0, 1); | ||
MockObject mockup2 = new MockObject(2.0, 2); | ||
MockObject mockup3 = new MockObject(3.0, 3); | ||
MockObject mockup4 = new MockObject(4.0, 4); | ||
List<MockObject> inputList = new ArrayList<IntegerMapGenotypeTest.MockObject>(); | ||
inputList.add(mockup1); | ||
inputList.add(mockup2); | ||
inputList.add(mockup3); | ||
IntegerMapGenotype<MockObject> integerMapGenotype = new IntegerMapGenotype<IntegerMapGenotypeTest.MockObject>( | ||
inputList, bounds); | ||
assertTrue(integerMapGenotype.isEmpty()); | ||
assertEquals(integerMapGenotype.getLowerBound(0), 1); | ||
assertEquals(integerMapGenotype.getLowerBound(1), 2); | ||
assertEquals(integerMapGenotype.getLowerBound(2), 3); | ||
assertEquals(integerMapGenotype.getUpperBound(0), 2); | ||
assertEquals(integerMapGenotype.getUpperBound(1), 3); | ||
assertEquals(integerMapGenotype.getUpperBound(2), 4); | ||
integerMapGenotype.init(rand); | ||
assertEquals(3, integerMapGenotype.size()); | ||
assertTrue(integerMapGenotype.containsKey(mockup1)); | ||
assertTrue(integerMapGenotype.containsKey(mockup2)); | ||
assertTrue(integerMapGenotype.containsKey(mockup3)); | ||
assertFalse(integerMapGenotype.containsKey(mockup4)); | ||
assertEquals(0, integerMapGenotype.getIndexOf(mockup1)); | ||
assertEquals(1, integerMapGenotype.getIndexOf(mockup2)); | ||
assertEquals(2, integerMapGenotype.getIndexOf(mockup3)); | ||
Collection<MockObject> keys = integerMapGenotype.getKeys(); | ||
assertTrue(keys.contains(mockup1)); | ||
assertTrue(keys.contains(mockup2)); | ||
assertTrue(keys.contains(mockup3)); | ||
assertFalse(keys.contains(mockup4)); | ||
assertTrue(integerMapGenotype.getValue(mockup1) >= 1); | ||
assertTrue(integerMapGenotype.getValue(mockup1) <= 2); | ||
assertTrue(integerMapGenotype.getValue(mockup2) >= 2); | ||
assertTrue(integerMapGenotype.getValue(mockup2) <= 3); | ||
assertTrue(integerMapGenotype.getValue(mockup3) >= 3); | ||
assertTrue(integerMapGenotype.getValue(mockup3) <= 4); | ||
integerMapGenotype.setValue(mockup3, 4); | ||
assertEquals(new Long(4), new Long(integerMapGenotype.getValue(mockup3))); | ||
IntegerMapGenotype<MockObject> other = integerMapGenotype.newInstance(); | ||
assertNotEquals(integerMapGenotype, other); | ||
assertTrue(other.isEmpty()); | ||
other.setValue(mockup2, 2); | ||
assertEquals(new Long(2), new Long(other.getValue(mockup2))); | ||
assertEquals(new Long(1), new Long(other.getValue(mockup1))); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
opt4j-core/src/test/java/org/opt4j/core/genotype/PermutationGenotypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package org.opt4j.core.genotype; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.HashSet; | ||
import java.util.Random; | ||
import java.util.Set; | ||
|
||
import org.junit.Test; | ||
|
||
public class PermutationGenotypeTest { | ||
|
||
@Test | ||
public void testEmptyConstructor() { | ||
Random rand = new Random(); | ||
PermutationGenotype<Integer> permutationGenotype = new PermutationGenotype<Integer>(); | ||
permutationGenotype.add(1); | ||
permutationGenotype.add(2); | ||
permutationGenotype.add(3); | ||
|
||
assertEquals(3, permutationGenotype.size()); | ||
permutationGenotype.init(rand); | ||
assertEquals(3, permutationGenotype.size()); | ||
assertTrue(permutationGenotype.contains(1)); | ||
assertTrue(permutationGenotype.contains(2)); | ||
assertTrue(permutationGenotype.contains(3)); | ||
|
||
PermutationGenotype<Integer> other = permutationGenotype.newInstance(); | ||
assertNotEquals(permutationGenotype, other); | ||
assertTrue(other.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testCollectionConstructor() { | ||
Random rand = new Random(); | ||
Set<Integer> inputCollection = new HashSet<Integer>(); | ||
inputCollection.add(1); | ||
inputCollection.add(2); | ||
inputCollection.add(3); | ||
PermutationGenotype<Integer> permutationGenotype = new PermutationGenotype<Integer>(inputCollection); | ||
|
||
assertEquals(3, permutationGenotype.size()); | ||
permutationGenotype.init(rand); | ||
assertEquals(3, permutationGenotype.size()); | ||
assertTrue(permutationGenotype.contains(1)); | ||
assertTrue(permutationGenotype.contains(2)); | ||
assertTrue(permutationGenotype.contains(3)); | ||
|
||
PermutationGenotype<Integer> other = permutationGenotype.newInstance(); | ||
assertNotEquals(permutationGenotype, other); | ||
assertTrue(other.isEmpty()); | ||
} | ||
|
||
} |
Oops, something went wrong.