package cz.cuni.mff.d3s.spl.core.data.segment;

import static cz.cuni.mff.d3s.spl.test.TestUtils.assertStatisticsEqual;

import org.junit.Before;
import org.junit.Test;

import cz.cuni.mff.d3s.spl.core.data.PrecomputedStatistics;
import cz.cuni.mff.d3s.spl.core.data.SampleBasedDataSource;

public class SlidingTimeSlotDataSourceTest {

	private final static double EPSILON = 0.0001;
	
	private SlidingTimeSlotDataSource prefilledSource;
	private SlidingTimeSlotDataSource emptySource;
	
	@Before
	public void prepareSamples() {
		prefilledSource = new SlidingTimeSlotDataSource(0, 10);
		
		for (int i = 0; i < 100; i++) {
			prefilledSource.newSample(i, i);
		}
		
		emptySource = new SlidingTimeSlotDataSource(0, 10);
	}
	
	@Test
	public void emptySourceProducesEmptyStatistics() {
		assertStatisticsEqual(PrecomputedStatistics.empty, emptySource.get(), EPSILON);
	}
	
	@Test(expected=IllegalArgumentException.class)
	public void invalidIntervalThrowsException() {
		new SlidingTimeSlotDataSource(15, 10);
	}
	
	@Test
	public void testStatisticsComputedCorrectly() {
		/* 0 + 1 + .. + 9 */
		assertStatisticsEqual(PrecomputedStatistics.create(4.5, 10), prefilledSource.get(), EPSILON);
	}
	
	@Test
	public void statisticsRecomputedAfterShift() {
		prefilledSource.shift(5);
		/* 5 + 6 + .. + 9 */
		assertStatisticsEqual(PrecomputedStatistics.create(7.0, 5), prefilledSource.get(), EPSILON);
	}
	
	@Test
	public void shiftCapturesInDifferentInterval() {
		prefilledSource.shift(5);
		prefilledSource.newSample(1, 12);
		prefilledSource.newSample(1, 9999);
		/* 5 + 6 + .. + 9 + 15 */
		assertStatisticsEqual(PrecomputedStatistics.create(6.0, 6), prefilledSource.get(), EPSILON);
	}
}

@@ -466,22 +466,17 @@
 aSource 
-emptyS
+s
 ource;%0A%09
@@ -657,22 +657,17 @@
 %09%7D%0A%09%09%0A%09%09
-emptyS
+s
 ource = 
@@ -821,22 +821,17 @@
 .empty, 
-emptyS
+s
 ource.ge
@@ -1463,16 +1463,51 @@
 ift(5);%0A
+%09%09prefilledSource.newSample(1, 0);%0A
 %09%09prefil
@@ -1596,24 +1596,24 @@
 + 9 + 15 */%0A
-
 %09%09assertStat
@@ -1698,10 +1698,750 @@
 ON);%0A%09%7D%0A
+%09%0A%09@Test%0A%09public void testMultipleShifts() %7B%0A%09%09source.newSample(1, 3);%0A%09%09source.newSample(2, 6);%0A%09%09source.newSample(3, 9);%0A%09%09source.newSample(4, 12);%0A%09%09assertStatisticsEqual(PrecomputedStatistics.create(2.0, 3), source.get(), EPSILON);%0A%09%09%0A%09%09/* Will be at %3C20, 30). */%0A%09%09source.shift(20);%0A%09%09assertStatisticsEqual(PrecomputedStatistics.empty, source.get(), EPSILON);%0A%09%09%0A%09%09/* Insert some data. */%0A%09%09source.newSample(100, 15);%0A%09%09source.newSample(105, 22);%0A%09%09source.newSample(110, 27);%0A%09%09source.newSample(120, 42);%0A%09%09assertStatisticsEqual(PrecomputedStatistics.create(107.5, 2), source.get(), EPSILON);%0A%09%09%0A%09%09/* Will be at %3C35, 45). */%0A%09%09source.shift(15);%0A%09%09assertStatisticsEqual(PrecomputedStatistics.create(42.0, 1), source.get(), EPSILON);%0A%09%7D%0A
 %7D%0A

Add another test for sliding time slot