diff --git a/.travis.yml b/.travis.yml index cf75128a..6a10166e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,16 @@ language: java install: true dist: trusty + +before_install: + - sudo apt-get update + - sudo apt-get install jq + - wget -O ~/codacy-coverage-reporter-assembly-latest.jar $(curl https://api.github.com/repos/codacy/codacy-coverage-reporter/releases/latest | jq -r .assets[0].browser_download_url) + after_success: -- "./gradlew coveralls" +- ./gradlew coveralls +- gradlew jacocoRootReport +- java -jar ~/codacy-coverage-reporter-assembly-latest.jar report -l Java -r build/reports/jacoco/jacocoRootReport/jacocoRootReport.xml before_deploy: - "./gradlew assemble website" @@ -19,10 +27,7 @@ deploy: tags: true - provider: pages skip-cleanup: true - api_key: - secure: dEolMpa639YJvocR44Qltm3SJ3AyFMNL9W8/ODwI1xI5lmZv4uMVkiiy6YWMjsphvq1+ATNv11fXcHRCCaQyP4Tddz43TLzUhvaOKBfPagnT0N/9mUkUX3XbzaIWT+9kqO9XdcqkLJbppF4Xj/p3ICivwENvqbarQjPzAAUpVax/LLxhf5rMErdktJu1nmd57YJdBElZyq5j4lYzUvXoLNyTuEc7DOKZwPAUGk8qvqcofVbDdoR4obk4y/LsuApf967y6+Re/4I7kcNVNYF4UfA/sGSd5O4yRp5VjpP2UAsYy6D965fCafCVIEb7xobgoIluwp7jbCRSA9PrK/jfvfD0BLKQgVpTSoBItOZMhggrSfdDmE0xn/y9GLcGIscjvaYxmSn0/5TTwhQgFQza9YXSozRZlCzbSjZLth8+CbkM09zler01+pi5B0h/QskYTIaIdbWGC8Ux1TrneCN4N1qEJQarfMtbtYh3bq9tNVuDb83r0bqygpioTWd6kdWjV03ECEG4+TufZYtBduBQPi+BbAcN8mlGjXCRulGdMOAtYtOttuvU/vgSWFWoUCIwrqwt/aGN8tjX5l+MwVWWS7SGLjF1J3cUgYde1axEZPKJHzlghtBjH1+UQ9bwpPdNb1Bhy5AI1Iz45nxjvkyhXrOdpYfvUuHH/cM18tdFUr8= - # was the following instead of api_key: - # github-token: # Set in travis-ci.org dashboard, marked secure + github-token: $GITHUB_TOKEN # Set in travis-ci.org dashboard, marked secure keep-history: true local-dir: ./build/website on: diff --git a/opt4j-core/src/main/java/org/opt4j/core/genotype/SelectMapGenotype.java b/opt4j-core/src/main/java/org/opt4j/core/genotype/SelectMapGenotype.java index b654b5cb..aa4040d8 100644 --- a/opt4j-core/src/main/java/org/opt4j/core/genotype/SelectMapGenotype.java +++ b/opt4j-core/src/main/java/org/opt4j/core/genotype/SelectMapGenotype.java @@ -19,15 +19,17 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. *******************************************************************************/ - + package org.opt4j.core.genotype; import java.lang.reflect.Constructor; import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Random; import org.opt4j.core.Genotype; @@ -44,8 +46,8 @@ * Example usage:
* *
- * SelectMapGenotype<Ball, Color> genotype = new SelectMapGenotype<Ball, Color>(Arrays.asList(ball1, ball2, ball3, ball4,
- * 		ball5), Arrays.asList(Color.BLUE, Color.GREEN, Color.RED));
+ * SelectMapGenotype<Ball, Color> genotype = new SelectMapGenotype<Ball, Color>(
+ * 		Arrays.asList(ball1, ball2, ball3, ball4, ball5), Arrays.asList(Color.BLUE, Color.GREEN, Color.RED));
  * genotype.init(new Random());
  * 
* @@ -74,6 +76,11 @@ protected static class SelectBounds implements Bounds { public SelectBounds(List list, Map> map) { this.list = list; this.map = map; + for (Entry> entry : map.entrySet()) { + if (entry.getValue().isEmpty()) { + throw new IllegalArgumentException("Empty value map provided for key " + entry.getKey()); + } + } } @Override @@ -97,11 +104,20 @@ public Integer getUpperBound(int index) { */ public SelectMapGenotype(List keys, Map> values) { super(new SelectBounds(keys, values)); + if (!keys.containsAll(values.keySet()) || !values.keySet().containsAll(keys)) { + throw new IllegalArgumentException("The provided list does not match the provided map"); + } + if (new HashSet(keys).size() < keys.size()) { + throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_NON_UNIQUE_KEYS); + } this.keys = keys; this.values = values; } private static Map> toMap(List keys, List values) { + if (new HashSet(keys).size() < keys.size()) { + throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_NON_UNIQUE_KEYS); + } Map> map = new HashMap>(); for (K key : keys) { map.put(key, values); @@ -142,7 +158,7 @@ public void init(Random random) { */ @Override public void init(Random random, int n) { - throw new UnsupportedOperationException("Use method init(Random) instead"); + throw new UnsupportedOperationException(MapGenotype.ERROR_MESSAGE_UNSUPPORTED_INIT); } /* @@ -162,6 +178,9 @@ public boolean containsKey(K key) { */ @Override public int getIndexOf(K key) { + if (!containsKey(key)) { + throw new IllegalArgumentException(MapGenotype.ERROR_MESSAGE_INVALID_KEY); + } return keys.indexOf(key); } @@ -174,11 +193,7 @@ public int getIndexOf(K key) { public V getValue(K key) { int i = getIndexOf(key); int v = get(i); - assert v <= getUpperBound(i); - assert v >= getLowerBound(i); List valueList = values.get(key); - - assert valueList.size() > v : "index " + v + " unavailable for list of key " + key + ": " + valueList; return valueList.get(v); } @@ -190,14 +205,16 @@ public V getValue(K key) { */ @Override public void setValue(K key, V value) { - int i = keys.indexOf(key); + int i = getIndexOf(key); while (size() <= i) { add(bounds.getLowerBound(i)); } - List valueList = values.get(key); + if (!valueList.contains(value)) { + throw new IllegalArgumentException( + "The list provided for key " + key + " does not contain the value " + value); + } int v = valueList.indexOf(value); - set(i, v); } diff --git a/opt4j-core/src/test/java/org/opt4j/core/genotype/SelectMapGenotypeTest.java b/opt4j-core/src/test/java/org/opt4j/core/genotype/SelectMapGenotypeTest.java new file mode 100644 index 00000000..d0bb87ff --- /dev/null +++ b/opt4j-core/src/test/java/org/opt4j/core/genotype/SelectMapGenotypeTest.java @@ -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 list; + protected Map> map; + protected List first; + protected List second; + + public Inputs() { + this.list = new ArrayList(); + list.add(1); + list.add(2); + this.map = new HashMap>(); + this.first = new ArrayList(); + first.add(3); + this.second = new ArrayList(); + 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 genotype = new SelectMapGenotype(inputs.list, inputs.map); + genotype.init(new Random()); + genotype.setValue(2, 5); + } + + @Test + public void testSetValue(){ + Inputs inputs = new Inputs(); + SelectMapGenotype genotype = new SelectMapGenotype(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 genotype = new SelectMapGenotype(inputs.list, inputs.map); + Collection 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 geno = new SelectMapGenotype(inputs.list, inputs.map); + geno.getIndexOf(4); + } + + @Test(expected = IllegalArgumentException.class) + public void testListMapMismatch2() { + Inputs inputs = new Inputs(); + inputs.map.put(3, new ArrayList()); + new SelectMapGenotype(inputs.list, inputs.map); + } + + @Test(expected = IllegalArgumentException.class) + public void testListMapMismatch1() { + Inputs inputs = new Inputs(); + inputs.list.add(3); + new SelectMapGenotype(inputs.list, inputs.map); + } + + @Test(expected = IllegalArgumentException.class) + public void testNonUniqueKeys2() { + Inputs inputs = new Inputs(); + inputs.list.add(1); + new SelectMapGenotype(inputs.list, inputs.map.get(1)); + } + + @Test(expected = IllegalArgumentException.class) + public void testNonUniqueKeys1() { + Inputs inputs = new Inputs(); + inputs.list.add(1); + new SelectMapGenotype(inputs.list, inputs.map); + } + + @Test(expected = UnsupportedOperationException.class) + public void testListConstructor() { + Inputs inputs = new Inputs(); + SelectMapGenotype genotype = new SelectMapGenotype(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()); + new SelectBounds(inputs.list, inputs.map); + } + + @Test + public void testSelectBounds() { + Inputs inputs = new Inputs(); + SelectBounds bounds = new SelectBounds(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)); + } + +}