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

import java.util.Random;

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

import org.jenetics.engine.FitnessConvergenceLimit.Buffer;
import org.jenetics.stat.DoubleMomentStatistics;
import org.jenetics.stat.DoubleMoments;

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

	@Test
	public void stream() {
		final long seed = 0xdeadbeef;
		final int capacity = 10;

		final Random random = new Random(seed);
		final Buffer buffer = new Buffer(capacity);

		for (int i = 0; i < buffer.capacity(); ++i) {
			final double value = random.nextDouble()*1000;
			buffer.accept(value);

			buffer.intStream(1000).forEach(j -> System.out.print("" + j + ", "));
			System.out.println();
		}

	}

	@Test
	public void buffer() {
		final long seed = 0xdeadbeef;
		final int capacity = 10;

		final Random random = new Random(seed);
		final Buffer buffer = new Buffer(capacity);

		DoubleMomentStatistics statistics = new DoubleMomentStatistics();
		for (int i = 0; i < buffer.capacity(); ++i) {
			final double value = random.nextDouble()*1000;
			buffer.accept(value);
			statistics.accept(value);

			final DoubleMoments moments = DoubleMoments.of(statistics);
			Assert.assertEquals(moments, buffer.doubleMoments(1000));
		}

		final Random sr = new Random(seed);
		for (int i = 0; i < buffer.capacity(); ++i) {
			statistics = statistics(new Random(seed), i + 1, buffer.capacity() - 1);

			final double value = random.nextDouble()*1000;
			buffer.accept(value);
			statistics.accept(value);

			final DoubleMoments moments = DoubleMoments.of(statistics);
			Assert.assertEquals(moments, buffer.doubleMoments(1000));
		}
	}

	public static DoubleMomentStatistics statistics(
		final Random random,
		final int skip,
		final int size
	) {
		final DoubleMomentStatistics statistics = new DoubleMomentStatistics();
		for (int i = 0; i < skip; ++i) random.nextDouble();
		for (int i = 0; i < size; ++i) statistics.accept(random.nextDouble()*1000);

		return statistics;
	}

}

@@ -1136,16 +1136,497 @@
 Test %7B%0A%0A
+%09@Test%0A%09public void bufferLength() %7B%0A%09%09final long seed = 0xdeadbeef;%0A%09%09final int capacity = 10;%0A%0A%09%09final Random random = new Random(seed);%0A%09%09final Buffer buffer = new Buffer(capacity);%0A%0A%09%09for (int i = 0; i %3C buffer.capacity(); ++i) %7B%0A%09%09%09buffer.accept(random.nextDouble());%0A%09%09%09Assert.assertEquals(buffer.length(), i + 1);%0A%09%09%7D%0A%0A%09%09for (int i = 0; i %3C buffer.capacity(); ++i) %7B%0A%09%09%09buffer.accept(random.nextDouble());%0A%09%09%09Assert.assertEquals(buffer.length(), buffer.capacity());%0A%09%09%7D%0A%09%7D%0A%0A
 %09@Test%0A%09
@@ -1880,37 +1880,32 @@
 dom.nextDouble()
-*1000
 ;%0A%09%09%09buffer.acce
@@ -1915,18 +1915,45 @@
 value);%0A
-%0A
 %09
+%09%7D%0A%0A%09%09random.setSeed(seed);%0A
 %09%09buffer
@@ -1957,22 +1957,15 @@
 fer.
-intS
+s
 tream(
-1000
 ).fo
@@ -1974,76 +1974,212 @@
 ach(
-j
+d
  -%3E 
-System.out.print(%22%22 + j + %22, %22));%0A%09%09%09System.out.println();%0A%09%09%7D%0A
+Assert.assertEquals(d, random.nextDouble()));%0A%0A%09%09random.setSeed(seed);%0A%09%09for (int i = 0; i %3C 5; ++i) random.nextDouble();%0A%09%09buffer.stream(5).forEach(d -%3E Assert.assertEquals(d, random.nextDouble()));
 %0A%09%7D%0A
@@ -2205,16 +2205,29 @@
 d buffer
+DoubleMoments
 () %7B%0A%09%09f

Add Buffer unit tests.