@Test public void ofSubSet() { final Codec<ISeq<String>, EnumGene<String>> codec = Codecs.ofSubSet( ISeq.of("1", "2", "3", "4", "5"), 3 ); for (int i = 0; i < 100; ++i) { final Genotype<EnumGene<String>> gt = codec.encoding().newInstance(); final Chromosome<EnumGene<String>> ch = gt.getChromosome(); Assert.assertEquals(ch.length(), 3); Assert.assertTrue(ch.isValid()); final ISeq<String> permutation = codec.decoder().apply(gt); Assert.assertEquals(permutation.length(), 3); } }
@Test public void ofAnyVector() { final int length = 23; final Codec<ISeq<Integer>, AnyGene<Integer>> codec = Codecs.ofVector( () -> RandomRegistry.getRandom().nextInt(1000), (Predicate<Integer>) i -> i < 100, length ); for (int i = 0; i < 100; ++i) { final Chromosome<AnyGene<Integer>> ch = codec.encoding() .newInstance().getChromosome(); Assert.assertEquals(ch.length(), length); for (AnyGene<Integer> gene : ch) { Assert.assertEquals(gene.isValid(), gene.getAllele() < 100); if (!gene.isValid()) { Assert.assertFalse(ch.isValid()); } Assert.assertTrue(gene.getAllele() < 1000); Assert.assertTrue(gene.getAllele() >= 0); } } }