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 source;
	
	@Before
	public void prepareSamples() {
		source = new SlidingTimeSlotDataSource(0, 10);
		
		for (int i = 0; i < 100; i++) {
			source.newSample(i, i);
		}
	}
	
	@Test
	public void emptySourceProducesEmptyStatistics() {
		SampleBasedDataSource empty = new SlidingTimeSlotDataSource(0, 10);
		assertStatisticsEqual(PrecomputedStatistics.empty, empty.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), source.get(), EPSILON);
	}
	
	@Test
	public void statisticsRecomputedAfterShift() {
		source.shift(5);
		/* 5 + 6 + .. + 9 */
		assertStatisticsEqual(PrecomputedStatistics.create(7.0, 5), source.get(), EPSILON);
	}
	
	@Test
	public void shiftCapturesInDifferentInterval() {
		source.shift(5);
		source.newSample(1, 12);
		source.newSample(1, 9999);
		/* 5 + 6 + .. + 9 + 15 */
		assertStatisticsEqual(PrecomputedStatistics.create(6.0, 6), source.get(), EPSILON);
	}
}

@@ -414,17 +414,74 @@
 aSource 
-s
+prefilledSource;%0A%09private SlidingTimeSlotDataSource emptyS
 ource;%0A%09
@@ -520,25 +520,34 @@
 mples() %7B%0A%09%09
-s
+prefilledS
 ource = new 
@@ -620,17 +620,26 @@
 +) %7B%0A%09%09%09
-s
+prefilledS
 ource.ne
@@ -657,16 +657,73 @@
 i);%0A%09%09%7D%0A
+%09%09%0A%09%09emptySource = new SlidingTimeSlotDataSource(0, 10);%0A
 %09%7D%0A%09%0A%09@T
@@ -782,78 +782,8 @@
 ) %7B%0A
-%09%09SampleBasedDataSource empty = new SlidingTimeSlotDataSource(0, 10);%0A
 %09%09as
@@ -836,16 +836,22 @@
 y, empty
+Source
 .get(), 
@@ -1149,17 +1149,26 @@
 5, 10), 
-s
+prefilledS
 ource.ge
@@ -1240,25 +1240,34 @@
 Shift() %7B%0A%09%09
-s
+prefilledS
 ource.shift(
@@ -1355,17 +1355,26 @@
 .0, 5), 
-s
+prefilledS
 ource.ge
@@ -1452,17 +1452,26 @@
 l() %7B%0A%09%09
-s
+prefilledS
 ource.sh
@@ -1476,25 +1476,34 @@
 shift(5);%0A%09%09
-s
+prefilledS
 ource.newSam
@@ -1516,17 +1516,26 @@
  12);%0A%09%09
-s
+prefilledS
 ource.ne
@@ -1642,17 +1642,26 @@
 .0, 6), 
-s
+prefilledS
 ource.ge

Test refactoring