/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics;

import static org.jenetics.stat.StatisticsAssert.assertDistribution;
import static org.jenetics.util.RandomRegistry.using;

import java.util.Arrays;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import org.jenetics.internal.util.Named;

import org.jenetics.stat.Histogram;
import org.jenetics.util.Factory;
import org.jenetics.util.LCG64ShiftRandom;
import org.jenetics.util.TestData;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 */
public class TruncationSelectorTest
	extends SelectorTester<TruncationSelector<DoubleGene, Double>>
{

	@Override
	protected Factory<TruncationSelector<DoubleGene, Double>> factory() {
		return TruncationSelector::new;
	}

	@Test(dataProvider = "expectedDistribution", groups = {"statistics"})
	public void selectDistribution(final Named<double[]> expected, final Optimize opt) {
		retry(3, () -> {
			final int loops = 50;
			final int npopulation = POPULATION_COUNT;

			final ThreadLocal<LCG64ShiftRandom> random = new LCG64ShiftRandom.ThreadLocal();
			using(random, r -> {
				final Histogram<Double> distribution = SelectorTester.distribution(
					new TruncationSelector<>(),
					opt,
					npopulation,
					loops
				);

				assertDistribution(distribution, expected.value, 0.001, 10);
			});
		});
	}

	@DataProvider(name = "expectedDistribution")
	public Object[][] expectedDistribution() {
		final String resource =
			"/org/jenetics/selector/distribution/TruncationSelector";

		return Arrays.stream(Optimize.values())
			.map(opt -> {
				final TestData data = TestData.of(resource, opt.toString());
				final double[] expected = data.stream()
					.map(line -> line[0])
					.mapToDouble(Double::parseDouble)
					.toArray();

				return new Object[]{Named.of("distribution", expected), opt};
			}).toArray(Object[][]::new);
	}

	public static void main(final String[] args) {
		writeDistributionData(Optimize.MAXIMUM);
		writeDistributionData(Optimize.MINIMUM);
	}

	private static void writeDistributionData(final Optimize opt) {
		final ThreadLocal<LCG64ShiftRandom> random = new LCG64ShiftRandom.ThreadLocal();
		using(random, r -> {
			final int npopulation = POPULATION_COUNT;
			//final int loops = 2_500_000;
			final int loops = 100_000;

			printDistributions(
				System.out,
				Arrays.asList(""),
				value -> new TruncationSelector<DoubleGene, Double>(),
				opt,
				npopulation,
				loops
			);
		});
	}

}

@@ -903,24 +903,50 @@
 il.Arrays;%0A%0A
+import org.testng.Assert;%0A
 import org.t
@@ -1518,16 +1518,796 @@
 ew;%0A%09%7D%0A%0A
+%09@Test%0A%09public void worstIndividual() %7B%0A%09%09final int size = 20;%0A%09%09final Population%3CDoubleGene, Integer%3E population = new Population%3C%3E(size);%0A%09%09for (int i = 0; i %3C size; ++i) %7B%0A%09%09%09final DoubleGene gene = DoubleGene.of(i, 0, size + 10);%0A%09%09%09final DoubleChromosome ch = DoubleChromosome.of(gene);%0A%09%09%09final Genotype%3CDoubleGene%3E gt = Genotype.of(ch);%0A%09%09%09final Phenotype%3CDoubleGene, Integer%3E pt = Phenotype.of(%0A%09%09%09%09gt, 1, g -%3E g.getGene().intValue()%0A%09%09%09);%0A%0A%09%09%09population.add(pt);%0A%09%09%7D%0A%0A%09%09final TruncationSelector%3CDoubleGene, Integer%3E selector =%0A%09%09%09new TruncationSelector%3C%3E(5);%0A%09%09final Population%3CDoubleGene, Integer%3E selected =%0A%09%09%09selector.select(population, 10, Optimize.MINIMUM);%0A%0A%09%09for (Phenotype%3CDoubleGene, Integer%3E pt : selected) %7B%0A%09%09%09Assert.assertTrue(pt.getFitness() %3C 5);%0A%09%09%7D%0A%09%7D%0A%0A
 %09@Test(d

Add TruncationSelector unit-test.