package test.lowlevel;

import java.util.Properties;

import org.junit.Test;
import static org.junit.Assert.assertEquals;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ibis.constellation.Constellation;
import ibis.constellation.ConstellationConfiguration;
import ibis.constellation.ConstellationFactory;
import ibis.constellation.StealStrategy;
import ibis.constellation.context.UnitActivityContext;
import ibis.constellation.context.UnitExecutorContext;
import ibis.constellation.util.SingleEventCollector;

public class DivideAndConquerCleanTest {

	private static final Logger logger = LoggerFactory.getLogger(DivideAndConquerClean.class);

	private void runTest(int branch, int depth, int nodes, int executors) throws Exception { 

		long start = System.nanoTime();

		Properties p = new Properties();
	    p.put("ibis.constellation.distributed", "false");

		ConstellationConfiguration config = new ConstellationConfiguration(new UnitExecutorContext("DC"), 
				StealStrategy.SMALLEST, StealStrategy.BIGGEST, StealStrategy.BIGGEST);

		Constellation c = ConstellationFactory.createConstellation(p, config, executors);
		c.activate();

		long count = 0;

		for (int i = 0; i <= depth; i++) {
			count += Math.pow(branch, i);
		}

		if (c.isMaster()) {

			logger.info("Running D&C with branch factor " + branch + " and depth " + depth + " (expected jobs: " + count + ")");

			SingleEventCollector a = new SingleEventCollector(new UnitActivityContext("DC"));

			c.submit(a);
			c.submit(new DivideAndConquerClean(a.identifier(), branch, depth));

			long result = (Long) a.waitForEvent().getData();

			long end = System.nanoTime();

			double msPerJob = Math.round(((end - start) / 10000.0) * executors * nodes / Math.pow(branch, depth)) / 100.0;

			String correct = (result == count) ? " (CORRECT)" : " (WRONG!)";
						
			logger.info("D&C(" + branch + ", " + depth + ") = " + result + correct + " total time = "
					+ Math.round((end - start) / 1000000.0) / 1000.0 + " sec; leaf job time = " + msPerJob + " msec/job");
			
			assertEquals(result, count);
		}

		c.done();
	}
	
	@Test
	public void fibOnOne() throws Exception {
		runTest(2, 5, 1, 1);
	}
}

@@ -1662,17 +1662,17 @@
 %09double 
-m
+n
 sPerJob 
@@ -1705,19 +1705,18 @@
 ) / 
-10000.0
+count
 ) * 
+(
 exec
@@ -1724,51 +1724,16 @@
 tors
- * 
+*
 nodes
- / Math.pow(branch, depth)) / 100.0
+))
 ;%0A%0A%09
@@ -1982,17 +1982,17 @@
 e = %22 + 
-m
+n
 sPerJob 
@@ -1995,17 +1995,17 @@
 Job + %22 
-m
+n
 sec/job%22
@@ -2132,9 +2132,10 @@
 (2, 
-5
+21
 , 1,
@@ -2142,10 +2142,173 @@
  1);%0A%09%7D%0A
+%09%0A%09@Test%0A%09public void fibOnTwo() throws Exception %7B%0A%09%09runTest(2, 21, 1, 2);%0A%09%7D%0A%09%0A%09@Test%0A%09public void fibOnFour() throws Exception %7B%0A%09%09runTest(2, 21, 1, 4);%0A%09%7D%0A%09%0A%09%0A
 %7D%0A

Use more jobs in DivideAndConquerTest