import java.util.Arrays; import java.util.List; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; public class SimpleTest { private static FloydRoutePlanner rp; private static final double TOL = 1e-8; private static final Double INF = Double.POSITIVE_INFINITY; // definieer constante om oneindig gewicht (= geen boog) voor te stellen @BeforeClass public static void init() { rp = new FloydRoutePlanner(); } @Test public void test1() { double[][] adjacentie = new double[][]{ {0.0, 10.7, 8.1}, {10.7, 0.0, 1.2}, {8.1, 1.2, 0.0} }; double[][] adjacentieK = kopieerAdjacentie(adjacentie); RoutePlanner.KortstePaden antw = rp.berekenKortstePaden(adjacentieK); Assert.assertTrue("Je mag de input niet aanpassen.",Arrays.deepEquals(adjacentie, adjacentieK)); check(antw, 0, 0, 0.0, Arrays.asList(0,0)); check(antw, 0, 1, 9.3, Arrays.asList(0,2,1)); check(antw, 0, 2, 8.1, Arrays.asList(0,2)); check(antw, 1, 0, 9.3, Arrays.asList(1,2,0)); check(antw, 1, 1, 0.0, Arrays.asList(1,1)); check(antw, 1, 2, 1.2, Arrays.asList(1,2)); check(antw, 2, 0, 8.1, Arrays.asList(2,0)); check(antw, 2, 1, 1.2, Arrays.asList(2,1)); check(antw, 2, 2, 0.0, Arrays.asList(2,2)); } @Test public void test2() { double[][] adjacentie = new double[][]{ {INF, 8.1, 1.7}, {INF, 0.0, 5.2}, {7.0, INF, 0.0} }; double[][] adjacentieK = kopieerAdjacentie(adjacentie); RoutePlanner.KortstePaden antw = rp.berekenKortstePaden(adjacentieK); Assert.assertTrue("Je mag de input niet aanpassen.",Arrays.deepEquals(adjacentie, adjacentieK)); check(antw, 0, 0, 8.7, Arrays.asList(0,2,0)); check(antw, 0, 1, 8.1, Arrays.asList(0,1)); check(antw, 0, 2, 1.7, Arrays.asList(0,2)); check(antw, 1, 0, 12.2, Arrays.asList(1,2,0)); check(antw, 1, 1, 0.0, Arrays.asList(1,1)); check(antw, 1, 2, 5.2, Arrays.asList(1,2)); check(antw, 2, 0, 7.0, Arrays.asList(2,0)); check(antw, 2, 1, 15.1, Arrays.asList(2,0,1)); check(antw, 2, 2, 0.0, Arrays.asList(2,2)); } @Test public void test3() { double[][] adjacentie = new double[][]{ {INF, INF, 9.0, INF}, {1.0, 0.0, INF, INF}, {1.0, INF, 0.0, 1.2}, {8.0, 2.0, INF, INF} }; double[][] adjacentieK = kopieerAdjacentie(adjacentie); RoutePlanner.KortstePaden antw = rp.berekenKortstePaden(adjacentieK); Assert.assertTrue("Je mag de input niet aanpassen.",Arrays.deepEquals(adjacentie, adjacentieK)); check(antw, 0, 0, 10.0, Arrays.asList(0,2,0)); check(antw, 0, 1, 12.2, Arrays.asList(0,2,3,1)); check(antw, 0, 2, 9.0, Arrays.asList(0,2)); check(antw, 0, 3, 10.2, Arrays.asList(0,2,3)); check(antw, 1, 0, 1.0, Arrays.asList(1,0)); check(antw, 1, 1, 0.0, Arrays.asList(1,1)); check(antw, 1, 2, 10.0, Arrays.asList(1,0,2)); check(antw, 1, 3, 11.2, Arrays.asList(1,0,2,3)); check(antw, 2, 0, 1.0, Arrays.asList(2,0)); check(antw, 2, 1, 3.2, Arrays.asList(2,3,1)); check(antw, 2, 2, 0.0, Arrays.asList(2,2)); check(antw, 2, 3, 1.2, Arrays.asList(2,3)); check(antw, 3, 0, 3.0, Arrays.asList(3,1,0)); check(antw, 3, 1, 2.0, Arrays.asList(3,1)); check(antw, 3, 2, 12.0, Arrays.asList(3,1,0,2)); check(antw, 3, 3, 13.2, Arrays.asList(3,1,0,2,3)); } public void check(RoutePlanner.KortstePaden antw, int top1, int top2, double oplLengte, List oplPad){ Assert.assertEquals("De lengte van het kortste pad van top " + top1 + " naar top " + top2 + " klopt niet.", oplLengte, antw.getKortstePadLengte(top1, top2), TOL); Assert.assertEquals("Kortste pad van top " + top1 + " naar top " + top2 + " klopt niet.", oplPad, antw.getKortstePad(top1, top2)); } public double[][] kopieerAdjacentie(double[][] adjacentie){ double[][] adjacentieK = new double[adjacentie.length][]; for (int i = 0; i < adjacentieK.length; i++) { adjacentieK[i] = Arrays.copyOf(adjacentie[i], adjacentie[i].length); } return adjacentieK; } }