@Test public void notEqualsNull() { final Object that = factory().newInstance(); Assert.assertFalse(that == null); }
@Test public void notEqualsStringType() { final Object that = factory().newInstance(); Assert.assertFalse(that.equals("__some_string__")); }
@Test public void notEqualsClassType() { final Object that = factory().newInstance(); Assert.assertFalse(that.equals(Class.class)); }
@Test public void cloneMethod() throws Exception { final Object that = factory().newInstance(); if (that instanceof Cloneable) { final Method clone = that.getClass().getMethod("clone"); final Object other = clone.invoke(that); Assert.assertEquals(other, that); Assert.assertNotSame(other, that); } }
@Test public void notEqualsAlleleNull() { final Object that = factory().newInstance().getAllele(); Assert.assertFalse(that.equals(null)); }
@Test public void objectSerialize() throws Exception { final Object object = factory().newInstance(); if (object instanceof Serializable) { for (int i = 0; i < 10; ++i) { final Serializable serializable = (Serializable)factory().newInstance(); Serialize.object.test(serializable); } } }
@Test public void alleleNotNull() { for (int i = 0; i < 1000; ++i) { Assert.assertNotNull(factory().newInstance().getAllele()); } }
@Test public void isValid() { final T a = factory().newInstance(); if (a instanceof Verifiable) { Assert.assertTrue(((Verifiable)a).isValid()); } }
@Test(expectedExceptions = NullPointerException.class) public void newInstanceFromNullArray() { final Chromosome<G> c = factory().newInstance(); c.newInstance(null); }
@Test public void newInstanceFromArray() { for (int i = 0; i < 100; ++i) { final Chromosome<G> c1 = factory().newInstance(); final ISeq<G> genes = c1.toSeq(); final Chromosome<G> c2 = c1.newInstance(genes); Assert.assertEquals(c2, c1); Assert.assertEquals(c1, c2); } }
protected MSeq<T> newEqualObjects(final int length) { return Stream .generate(() -> with(new Random(589), r -> factory().newInstance())) .limit(length) .collect(toMSeq()); }
@Test public void minMax() { @SuppressWarnings("unchecked") final NumericChromosome<N, G> c1 = (NumericChromosome<N, G>) factory().newInstance(); @SuppressWarnings("unchecked") final NumericChromosome<N, G> c2 = (NumericChromosome<N, G>) factory().newInstance(); assertMinMax(c1, c2); assertValid(c1); assertValid(c2); }
@Test public void length() { final Chromosome<G> c = factory().newInstance(); final ISeq<G> a = c.toSeq(); Assert.assertEquals(c.length(), a.length()); }
@Test(dataProvider = "intScalarData") public void ofIntScalar(final IntRange domain) { final Codec<Integer, IntegerGene> codec = Codecs.ofScalar(domain); final Genotype<IntegerGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), 1); Assert.assertEquals(gt.getGene().getMin().intValue(), domain.getMin()); Assert.assertEquals(gt.getGene().getMax().intValue(), domain.getMax()); final Function<Genotype<IntegerGene>, Integer> f = codec.decoder(); Assert.assertEquals(f.apply(gt).intValue(), gt.getGene().intValue()); }
@Test(dataProvider = "longScalarData") public void ofLongScalar(final LongRange domain) { final Codec<Long, LongGene> codec = Codecs.ofScalar(domain); final Genotype<LongGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), 1); Assert.assertEquals(gt.getGene().getMin().longValue(), domain.getMin()); Assert.assertEquals(gt.getGene().getMax().longValue(), domain.getMax()); final Function<Genotype<LongGene>, Long> f = codec.decoder(); Assert.assertEquals(f.apply(gt).longValue(), gt.getGene().longValue()); }
@Test public void primitiveTypeAccess() { @SuppressWarnings("unchecked") final NumericChromosome<N, G> c = (NumericChromosome<N, G>) factory().newInstance(); Assert.assertEquals(c.byteValue(), c.byteValue(0)); Assert.assertEquals(c.shortValue(), c.shortValue(0)); Assert.assertEquals(c.intValue(), c.intValue(0)); Assert.assertEquals(c.floatValue(), c.floatValue(0)); Assert.assertEquals(c.doubleValue(), c.doubleValue(0)); }
@Test(dataProvider = "doubleScalarData") public void ofDoubleScalar(final DoubleRange domain) { final Codec<Double, DoubleGene> codec = Codecs.ofScalar(domain); final Genotype<DoubleGene> gt = codec.encoding().newInstance(); Assert.assertEquals(gt.length(), 1); Assert.assertEquals(gt.getChromosome().length(), 1); Assert.assertEquals(gt.getGene().getMin(), domain.getMin()); Assert.assertEquals(gt.getGene().getMax(), domain.getMax()); final Function<Genotype<DoubleGene>, Double> f = codec.decoder(); Assert.assertEquals(f.apply(gt), gt.getGene().doubleValue()); }
private Phenotype<DoubleGene, Vec<double[]>> phenotype() { return Phenotype.of( PROBLEM.codec().encoding().newInstance(), 1L, gt -> PROBLEM.fitness().apply(PROBLEM.codec().decode(gt)) ); }
@Test public void getGene() { final Chromosome<G> c = factory().newInstance(); final ISeq<G> genes = c.toSeq(); Assert.assertEquals(c.getGene(), genes.get(0)); for (int i = 0; i < genes.length(); ++i) { Assert.assertSame(c.getGene(i), genes.get(i)); } }
@Test(expectedExceptions = IllegalArgumentException.class) public void selectNegativeCountArgument() { final Factory<Genotype<DoubleGene>> gtf = Genotype.of(DoubleChromosome.of(0.0, 1.0)); final MSeq<Phenotype<DoubleGene, Double>> population = MSeq.ofLength(2); for (int i = 0, n = 2; i < n; ++i) { population.set(i, Phenotype.of(gtf.newInstance(), 12, TestUtils.FF)); } selector().select(population.toISeq(), -1, Optimize.MAXIMUM); }