/*
 * 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.util;

import java.util.Random;

import org.testng.Assert;
import org.testng.annotations.Test;

import org.jenetics.internal.math.DoubleAdder;
import org.jenetics.internal.math.base;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 */
public class baseTest {

	@Test
	public void summarize() {
		final double[] values = new double[150000];
		for (int i = 0; i < values.length; ++i) {
			values[i] = 1.0/values.length;
		}

		Assert.assertEquals(DoubleAdder.sum(values), 1.0);
	}

	@Test
	public void subset() {
		final Random random = new Random();

		for (int i = 1; i < 100; ++i) {
			int[] sub = new int[i];
			base.subset(1000, sub, random);

			Assert.assertTrue(isSortedd(sub));
		}
	}

	private static boolean isSortedd(int[] array) {
		boolean sorted = true;
		for (int i = 0; i < array.length - 1 && sorted; ++i) {
			sorted = array[i] < array[i + 1];
		}
		return sorted;
	}

}

@@ -773,23 +773,135 @@
 ort 
-java.util.Rando
+static org.jenetics.stat.StatisticsAssert.assertUniformDistribution;%0A%0Aimport java.util.Random;%0Aimport java.util.stream.IntStrea
 m;%0A%0A
@@ -1051,16 +1051,53 @@
 .base;%0A%0A
+import org.jenetics.stat.Histogram;%0A%0A
 /**%0A * @
@@ -1619,17 +1619,25 @@
 isSorted
-d
+AndUnique
 (sub));%0A
@@ -1680,10 +1680,24 @@
 rted
-d(
+AndUnique(final 
 int%5B
@@ -1851,10 +1851,750 @@
 ed;%0A%09%7D%0A%0A
+%09@Test(invocationCount = 20, successPercentage = 95)%0A%09public void subSetDistribution() %7B%0A%09%09final int%5B%5D sub = new int%5B3%5D;%0A%09%09final int n = 100_000;%0A%0A%09%09final Random random = new Random();%0A%09%09final Histogram%3CInteger%3E histogram = Histogram.ofInteger(0, n, 13);%0A%0A%09%09IntStream.range(0, 10_000)%0A%09%09%09.flatMap(i -%3E IntStream.of(base.subset(n, sub, random)))%0A%09%09%09.forEach(histogram::accept);%0A%0A%09%09assertUniformDistribution(histogram);%0A%09%7D%0A%0A%09// https://en.wikipedia.org/wiki/Reservoir_sampling%0A%09static void subset(final int n, final int sub%5B%5D, final Random random) %7B%0A%09%09for (int i = 0; i %3C sub.length; ++i) %7B%0A%09%09%09sub%5Bi%5D = i;%0A%09%09%7D%0A%0A%09%09for (int i = sub.length; i %3C n; ++i) %7B%0A%09%09%09final int j = random.nextInt(i);%0A%09%09%09if (j %3C sub.length) %7B%0A%09%09%09%09sub%5Bj%5D = i;%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A
 %7D%0A

Add distribution test for 'base.subset' method.