package algorithms;

import computecore.RequestPoints;
import computecore.Way;
import graphrep.GraphRep;
import graphrep.GraphRepReader;
import graphrep.GraphRepTextReader;
import org.junit.Test;

import java.io.ByteArrayInputStream;

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

/**
 * @author Christoph Haag, Sascha Meusel, Niklas Schnelle, Peter Vollmer
 */
public class ConstrainedSPTest {
    @Test
    public void testCompute() throws Exception {
        GraphRepReader graphRepTextReader = new GraphRepTextReader();

      String testFile = new String("8\n18\n10000000 10000000 20 0\n20000000 20000000 5 0\n20000000 10000000 10 0\n10000000 20000000 100 0\n30000000 10000000 40 0\n30000000 20000000 45 0\n20000000 30000000 30 0\n10000000 30000000 50 0\n0 1 0 4 -1 -1\n0 2 0 2 -1 -1\n0 3 0 3 -1 -1\n1 0 0 4 -1 -1\n1 6 0 2 -1 -1\n2 0 0 2 -1 -1\n2 4 0 3 -1 -1\n3 0 0 3 -1 -1\n3 7 0 4 -1 -1\n4 2 0 2 -1 -1\n4 5 0 4 -1 -1\n5 4 0 4 -1 -1\n5 7 0 5 -1 -1\n6 1 0 2 -1 -1\n6 7 0 3 -1 -1\n7 6 0 3 -1 -1\n7 5 0 5 -1 -1\n7 3 0 4 -1 -1");
        byte[] testFileBytes = testFile.getBytes();
        ByteArrayInputStream testFileByteArrayStream = new ByteArrayInputStream(testFileBytes);
        GraphRep graphRep = graphRepTextReader.createGraphRep(testFileByteArrayStream);
        GraphAlgorithmFactory fac = new ConstrainedSPFactory(graphRep);
        ConstrainedSP constrainedSP = (ConstrainedSP)fac.createAlgorithm();

        RequestPoints points = new RequestPoints();
        points.addPoint(10000000,10000000);
        points.addPoint(10000000,30000000);

        //shortest path is the best fittest

        Way resultWay = new Way();
        int[] result = constrainedSP.cSP(points,resultWay,90);
        assertEquals(80,result[0]);
        assertEquals(7,result[1]);
        assertEquals(10000000,resultWay.getPointLat(0));
        assertEquals(10000000,resultWay.getPointLon(0));
        assertEquals(10000000,resultWay.getPointLat(1));
        assertEquals(20000000,resultWay.getPointLon(1));
        assertEquals(10000000,resultWay.getPointLat(2));
        assertEquals(30000000,resultWay.getPointLon(2));

        //middle way

        resultWay = new Way();
        result = constrainedSP.cSP(points,resultWay,60);
        assertEquals(45,result[0]);
        assertEquals(9,result[1]);
        assertEquals(10000000,resultWay.getPointLat(0));
        assertEquals(10000000,resultWay.getPointLon(0));
        assertEquals(20000000,resultWay.getPointLat(1));
        assertEquals(20000000,resultWay.getPointLon(1));
        assertEquals(20000000,resultWay.getPointLat(2));
        assertEquals(30000000,resultWay.getPointLon(2));
        assertEquals(10000000,resultWay.getPointLat(3));
        assertEquals(30000000,resultWay.getPointLon(3));

        //longest possible way

        resultWay = new Way();
        result = constrainedSP.cSP(points,resultWay,44);
        assertEquals(40,result[0]);
        assertEquals(14,result[1]);
        assertEquals(10000000,resultWay.getPointLat(0));
        assertEquals(10000000,resultWay.getPointLon(0));
        assertEquals(20000000,resultWay.getPointLat(1));
        assertEquals(10000000,resultWay.getPointLon(1));
        assertEquals(30000000,resultWay.getPointLat(2));
        assertEquals(10000000,resultWay.getPointLon(2));
        assertEquals(30000000,resultWay.getPointLat(3));
        assertEquals(20000000,resultWay.getPointLon(3));
        assertEquals(10000000,resultWay.getPointLat(4));
        assertEquals(30000000,resultWay.getPointLon(4));

        //no possible way with a constraint
        resultWay = new Way();
        try {
            result = constrainedSP.cSP(points,resultWay,30);
            fail("Should have raised an IllegalArgumentException");
        } catch (ComputeException expected) {
        }



    }
}
                            
@@ -3589,16 +3589,79 @@
 nstraint
+. There should be the way with the least difference of altitude
 %0A       
@@ -3688,26 +3688,8 @@
 ();%0A
-        try %7B%0A    
     
@@ -3753,125 +3753,641 @@
     
-    fail(%22Should have raised an IllegalArgumentException%22);%0A        %7D catch (ComputeException expected) %7B%0A        %7D%0A%0A
+assertEquals(40,result%5B0%5D);%0A        assertEquals(14,result%5B1%5D);%0A        assertEquals(10000000,resultWay.getPointLat(0));%0A        assertEquals(10000000,resultWay.getPointLon(0));%0A        assertEquals(20000000,resultWay.getPointLat(1));%0A        assertEquals(10000000,resultWay.getPointLon(1));%0A        assertEquals(30000000,resultWay.getPointLat(2));%0A        assertEquals(10000000,resultWay.getPointLon(2));%0A        assertEquals(30000000,resultWay.getPointLat(3));%0A        assertEquals(20000000,resultWay.getPointLon(3));%0A        assertEquals(10000000,resultWay.getPointLat(4));%0A        assertEquals(30000000,resultWay.getPointLon(4));
 %0A%0A  

insert least way if no way with constraint exists