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

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

import org.apache.commons.math3.stat.StatUtils;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import org.jenetics.internal.util.Named;

import org.jenetics.Optimize;
import org.jenetics.util.RandomRegistry;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version <em>$Date: 2014-09-19 $</em>
 */
public class MinMaxTest {

	private static final Named<Comparator<Integer>> NORMAL = Named.of(
		"NORMAL",
		(Comparator<Integer>)Optimize.MAXIMUM::compare
	);

	private static final Named<Comparator<Integer>> REVERSE = Named.of(
		"REVERSE",
		(Comparator<Integer>)Optimize.MINIMUM::compare
	);

	@Test(dataProvider = "minTestValues")
	public void min(
		final Named<Comparator<Integer>> comparator,
		final Integer a,
		final Integer b,
		final Integer min
	) {
		Assert.assertEquals(MinMax.min(comparator.value, a, b), min);
	}

	@DataProvider(name = "minTestValues")
	public Object[][] minTestValues() {
		return new Object[][] {
			{NORMAL, null, null, null},
			{NORMAL, null, 1, 1},
			{NORMAL, 1, null, 1},
			{NORMAL, 1, 2, 1},
			{NORMAL, 2, 1, 1},
			{NORMAL, 1, 1, 1},

			{REVERSE, null, null, null},
			{REVERSE, null, 2, 2},
			{REVERSE, 2, null, 2},
			{REVERSE, 1, 2, 2},
			{REVERSE, 2, 1, 2},
			{REVERSE, 2, 2, 2}
		};
	}

	@Test(dataProvider = "maxTestValues")
	public void max(
		final Named<Comparator<Integer>> comparator,
		final Integer a,
		final Integer b,
		final Integer max
	) {
		Assert.assertEquals(MinMax.max(comparator.value, a, b), max);
	}

	@DataProvider(name = "maxTestValues")
	public Object[][] maxTestValues() {
		return new Object[][] {
			{NORMAL, null, null, null},
			{NORMAL, null, 1, 1},
			{NORMAL, 1, null, 1},
			{NORMAL, 1, 2, 2},
			{NORMAL, 2, 1, 2},
			{NORMAL, 1, 1, 1},

			{REVERSE, null, null, null},
			{REVERSE, null, 2, 2},
			{REVERSE, 2, null, 2},
			{REVERSE, 1, 2, 1},
			{REVERSE, 2, 1, 1},
			{REVERSE, 2, 2, 2}
		};
	}

	@Test
	public void acceptNormalMinMax() {
		final Random random = RandomRegistry.getRandom();
		final double[] numbers = random.doubles().limit(1000).toArray();

		final MinMax<Double> minMax = MinMax.of();
		Arrays.stream(numbers)
			.mapToObj(Double::new)
			.forEach(minMax);

		Assert.assertEquals(minMax.getMin(), StatUtils.min(numbers));
		Assert.assertEquals(minMax.getMax(), StatUtils.max(numbers));
	}

	@Test
	public void acceptReverseMinMax() {
		final Random random = RandomRegistry.getRandom();
		final double[] numbers = random.doubles().limit(1000).toArray();

		final MinMax<Double> minMax = MinMax.of((a, b) -> b.compareTo(a));
		Arrays.stream(numbers)
			.mapToObj(Double::new)
			.forEach(minMax);

		Assert.assertEquals(minMax.getMin(), StatUtils.max(numbers));
		Assert.assertEquals(minMax.getMax(), StatUtils.min(numbers));
	}

}

@@ -1232,10 +1232,10 @@
 -09-
+2
 1
-9
  $%3C/
@@ -3682,10 +3682,845 @@
 ));%0A%09%7D%0A%0A
+%09@Test%0A%09public void toMinMaxNormal() %7B%0A%09%09final Random random = RandomRegistry.getRandom();%0A%09%09final double%5B%5D numbers = random.doubles().limit(1000).toArray();%0A%0A%09%09final MinMax%3CDouble%3E minMax = Arrays.stream(numbers)%0A%09%09%09.mapToObj(Double::new)%0A%09%09%09.collect(MinMax.toMinMax());%0A%0A%09%09Assert.assertEquals(minMax.getMin(), StatUtils.min(numbers));%0A%09%09Assert.assertEquals(minMax.getMax(), StatUtils.max(numbers));%0A%09%7D%0A%0A%09@Test%0A%09public void toMinMaxReverse() %7B%0A%09%09final Random random = RandomRegistry.getRandom();%0A%09%09final double%5B%5D numbers = random.doubles().limit(1000).toArray();%0A%0A%09%09final MinMax%3CDouble%3E minMax = Arrays.stream(numbers)%0A%09%09%09.mapToObj(Double::new)%0A%09%09%09.collect(MinMax.toMinMax((a, b) -%3E b.compareTo(a)));%0A%0A%09%09Assert.assertEquals(minMax.getMin(), StatUtils.max(numbers));%0A%09%09Assert.assertEquals(minMax.getMax(), StatUtils.min(numbers));%0A%09%7D%0A%0A
 %7D%0A

Add tests for 'MinMax' class.