/*
 * Copyright 2015 Charles University in Prague
 * Copyright 2015 Vojtech Horky
 * 
 * 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.
 */
package cz.cuni.mff.d3s.spl.interpretation;

import static org.junit.Assert.*;

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

import cz.cuni.mff.d3s.spl.BenchmarkRun;
import cz.cuni.mff.d3s.spl.ComparisonResult;
import cz.cuni.mff.d3s.spl.DataSnapshot;
import cz.cuni.mff.d3s.spl.Interpretation;
import cz.cuni.mff.d3s.spl.data.DataSnapshotBuilder;
import cz.cuni.mff.d3s.spl.data.ImmutableBenchmarkRun;

public class DistributionLearningInterpretationTest  {
	private static final double EPSILON = 0.1;
	
	private static final long[] RUN_1_1 = new long[] {10, 10, 12, 10 };
	private static final long[] RUN_1_2 = new long[] {10, 12, 9, 10 };
	private static final long[] RUN_2_1 = new long[] {10, 10, 11, 10 };
	private static final long[] RUN_2_2 = new long[] {10, 10, 10, 9 };
	
	private Interpretation interpretation;
	
	private DataSnapshot SNAPSHOT_1;
	private DataSnapshot SNAPSHOT_2;
	
	@Before
	public void prepareSnapshots() {
		SNAPSHOT_1 = makeSnapshot(null, RUN_1_1, RUN_1_2);
		SNAPSHOT_1 = makeSnapshot(SNAPSHOT_1, RUN_1_1, RUN_1_2);
		SNAPSHOT_2 = makeSnapshot(null, RUN_2_1, RUN_2_2);
	}
	
	@Before
	public void prepareInterpretation() {
		interpretation = new DistributionLearningInterpretation();
	}
	
	private DataSnapshot makeSnapshot(DataSnapshot previousEpoch, long[]... runs) {		
		DataSnapshotBuilder builder = new DataSnapshotBuilder();
		for (long[] samples : runs) {
			BenchmarkRun run = new ImmutableBenchmarkRun(samples);
			builder.addRun(run);
		}
		
		builder.setPreviousEpoch(previousEpoch);
		
		return builder.create();
	}
		
	
	@Test
	public void smokeTestForTwoSnapshots() {
		ComparisonResult result = interpretation.compare(SNAPSHOT_1, SNAPSHOT_2);
		
		assertEquals(0.375, result.getStatistic(), 0.000001);
		assertEquals(ComparisonResult.Relation.GREATER_THAN, result.get(0.2));
		assertEquals(-0.65, result.getCriticalValue(0.1), EPSILON);
		assertEquals(-0.84, result.getCriticalValue(0.05), EPSILON);
	}
	
	@Test
	public void smokeTestForOneSnapshot() {
		@SuppressWarnings("unused")
		ComparisonResult result = interpretation.compare(SNAPSHOT_1, 10.);
		
		// FIXME - compute
	}
}

@@ -1046,16 +1046,225 @@
 rkRun;%0A%0A
+/** Test for the distribution-learning interpretation.%0A *%0A * Note that we test only the basic properties and not precise values because%0A * the actual values are random and bound to change between runs.%0A *%0A */%0A
 public c
@@ -1351,16 +1351,21 @@
 LON = 0.
+00000
 1;%0A%09%0A%09pr
@@ -1635,16 +1635,162 @@
 0, 9 %7D;%0A
+%09private static final long%5B%5D RUN_3_1 = new long%5B%5D %7B110, 110, 111, 110 %7D;%0A%09private static final long%5B%5D RUN_3_2 = new long%5B%5D %7B110, 110, 110, 109 %7D;%0A
 %09%0A%09priva
@@ -1893,16 +1893,50 @@
 SHOT_2;%0A
+%09private DataSnapshot SNAPSHOT_3;%0A
 %09%0A%09@Befo
@@ -2137,16 +2137,69 @@
 N_2_2);%0A
+%09%09SNAPSHOT_3 = makeSnapshot(null, RUN_3_1, RUN_3_2);%0A
 %09%7D%0A%09%0A%09@B
@@ -2826,24 +2826,23 @@
 stic(), 
-0.000001
+EPSILON
 );%0A%09%09ass
@@ -2909,16 +2909,151 @@
 (0.2));%0A
+%09%7D%0A%09%0A%09@Test%0A%09public void smokeTestForHugeDifference() %7B%0A%09%09ComparisonResult result = interpretation.compare(SNAPSHOT_1, SNAPSHOT_3);%0A%09%09%0A
 %09%09assert
@@ -3064,11 +3064,13 @@
 ls(-
-0
+99
 .6
+2
 5, r
@@ -3082,25 +3082,18 @@
 .get
-CriticalValue(0.1
+Statistic(
 ), E
@@ -3120,53 +3120,61 @@
 als(
--0.84, result.getCriticalValue(0.05), EPSILON
+ComparisonResult.Relation.LESS_THAN, result.get(0.01)
 );%0A%09

Extend test for distribution learning interpretation