-
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.
- Loading branch information
Showing
3 changed files
with
175 additions
and
16 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
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
137 changes: 137 additions & 0 deletions
137
opt4j-core/src/test/java/org/opt4j/core/genotype/SelectMapGenotypeTest.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,137 @@ | ||
package org.opt4j.core.genotype; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
|
||
import org.junit.Test; | ||
import org.opt4j.core.genotype.SelectMapGenotype.SelectBounds; | ||
|
||
public class SelectMapGenotypeTest { | ||
|
||
public class Inputs { | ||
protected List<Integer> list; | ||
protected Map<Integer, List<Integer>> map; | ||
protected List<Integer> first; | ||
protected List<Integer> second; | ||
|
||
public Inputs() { | ||
this.list = new ArrayList<Integer>(); | ||
list.add(1); | ||
list.add(2); | ||
this.map = new HashMap<Integer, List<Integer>>(); | ||
this.first = new ArrayList<Integer>(); | ||
first.add(3); | ||
this.second = new ArrayList<Integer>(); | ||
second.add(3); | ||
second.add(4); | ||
map.put(1, first); | ||
map.put(2, second); | ||
} | ||
} | ||
|
||
@Test(expected=IllegalArgumentException.class) | ||
public void testSetWrongValue(){ | ||
Inputs inputs = new Inputs(); | ||
SelectMapGenotype<Integer, Integer> genotype = new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map); | ||
genotype.init(new Random()); | ||
genotype.setValue(2, 5); | ||
} | ||
|
||
@Test | ||
public void testSetValue(){ | ||
Inputs inputs = new Inputs(); | ||
SelectMapGenotype<Integer, Integer> genotype = new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map); | ||
genotype.init(new Random()); | ||
genotype.setValue(2, 3); | ||
assertEquals(3, (long) genotype.getValue(2)); | ||
genotype.setValue(2, 4); | ||
assertEquals(4, (long) genotype.getValue(2)); | ||
} | ||
|
||
@Test | ||
public void testMapConstructor() { | ||
Inputs inputs = new Inputs(); | ||
inputs.map.get(2).remove(1); | ||
SelectMapGenotype<Integer, Integer> genotype = new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map); | ||
Collection<Integer> keys = genotype.getKeys(); | ||
assertTrue(keys.contains(1)); | ||
assertTrue(keys.contains(2)); | ||
Random rand = new Random(); | ||
genotype.init(rand); | ||
assertEquals("[1=3;2=3;]", genotype.toString()); | ||
assertTrue(genotype.containsKey(2)); | ||
assertFalse(genotype.containsKey(3)); | ||
assertNotEquals(genotype, genotype.newInstance()); | ||
assertEquals(0, genotype.getIndexOf(1)); | ||
assertEquals(3, (long) genotype.getValue(1)); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testInvalidKey() { | ||
Inputs inputs = new Inputs(); | ||
SelectMapGenotype<Integer, Integer> geno = new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map); | ||
geno.getIndexOf(4); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testListMapMismatch2() { | ||
Inputs inputs = new Inputs(); | ||
inputs.map.put(3, new ArrayList<Integer>()); | ||
new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testListMapMismatch1() { | ||
Inputs inputs = new Inputs(); | ||
inputs.list.add(3); | ||
new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testNonUniqueKeys2() { | ||
Inputs inputs = new Inputs(); | ||
inputs.list.add(1); | ||
new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map.get(1)); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testNonUniqueKeys1() { | ||
Inputs inputs = new Inputs(); | ||
inputs.list.add(1); | ||
new SelectMapGenotype<Integer, Integer>(inputs.list, inputs.map); | ||
} | ||
|
||
@Test(expected = UnsupportedOperationException.class) | ||
public void testListConstructor() { | ||
Inputs inputs = new Inputs(); | ||
SelectMapGenotype<Integer, Integer> genotype = new SelectMapGenotype<Integer, Integer>(inputs.list, | ||
inputs.map.get(1)); | ||
Random rand = new Random(); | ||
genotype.init(rand, 2); | ||
} | ||
|
||
@Test(expected = IllegalArgumentException.class) | ||
public void testEmptyMap() { | ||
Inputs inputs = new Inputs(); | ||
inputs.list.add(3); | ||
inputs.map.put(3, new ArrayList<Integer>()); | ||
new SelectBounds<Integer, Integer>(inputs.list, inputs.map); | ||
} | ||
|
||
@Test | ||
public void testSelectBounds() { | ||
Inputs inputs = new Inputs(); | ||
SelectBounds<Integer, Integer> bounds = new SelectBounds<Integer, Integer>(inputs.list, inputs.map); | ||
assertEquals(0, (long) bounds.getLowerBound(0)); | ||
assertEquals(0, (long) bounds.getUpperBound(0)); | ||
assertEquals(0, (long) bounds.getLowerBound(1)); | ||
assertEquals(1, (long) bounds.getUpperBound(1)); | ||
} | ||
|
||
} |