DoubleMapGenotype extends DoubleGenotype implements MapGenotype<K, Double> { @Override public void setValue(K key, Double value) { if (!containsKey(key)) { throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_INVALID_KEY); } int i = keys.indexOf(key); if (value < bounds.getLowerBound(i) || value > bounds.getUpperBound(i)) { throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_OUT_OF_BOUNDS); } while (size() <= i) { add(bounds.getLowerBound(i)); } set(i, value); } DoubleMapGenotype(List<K> keys, Bounds<Double> bounds); void init(Random random); @Override void init(Random random, int n); @Override boolean containsKey(K key); @Override Double getValue(K key); @Override void setValue(K key, Double value); @Override @SuppressWarnings({ "unchecked", "rawtypes" }) G newInstance(); @Override String toString(); @Override int getIndexOf(K key); @Override Collection<K> getKeys();  }
@Test public void testSetValue() { List<MockObject> keys = new ArrayList<>(); MockObject m1 = new MockObject(1); MockObject m2 = new MockObject(2); keys.add(m1); keys.add(m2); DoubleBounds bounds = new DoubleBounds(lower2, upper); DoubleMapGenotype<MockObject> doubleMapGeno = new DoubleMapGenotype<>(keys, bounds); doubleMapGeno.setValue(m2, 0.4); assertEquals(0.4, doubleMapGeno.getValue(m2), 0.0); doubleMapGeno.init(new Random()); doubleMapGeno.setValue(m1, 0.5); assertEquals(0.5, doubleMapGeno.getValue(m1), 0.0); }
@Test(expected = IllegalArgumentException.class) public void testSetValueOutOfBounds2() { List<MockObject> keys = new ArrayList<>(); MockObject m1 = new MockObject(1); MockObject m2 = new MockObject(2); keys.add(m1); keys.add(m2); DoubleMapGenotype<MockObject> doubleMapGeno = new DoubleMapGenotype<>(keys, new MockBounds()); doubleMapGeno.setValue(m1, -2.0); }
@Test(expected = IllegalArgumentException.class) public void testSetValueOutOfBounds1() { List<MockObject> keys = new ArrayList<>(); MockObject m1 = new MockObject(1); MockObject m2 = new MockObject(2); keys.add(m1); keys.add(m2); DoubleMapGenotype<MockObject> doubleMapGeno = new DoubleMapGenotype<>(keys, new MockBounds()); doubleMapGeno.setValue(m1, 2.0); }
@Test(expected = IllegalArgumentException.class) public void testSetValueInvalidKey() { List<MockObject> keys = new ArrayList<>(); MockObject m1 = new MockObject(1); MockObject m2 = new MockObject(2); MockObject m3 = new MockObject(3); keys.add(m1); keys.add(m2); DoubleMapGenotype<MockObject> doubleMapGeno = new DoubleMapGenotype<>(keys, new MockBounds()); doubleMapGeno.setValue(m3, 1.0); }