From 224a67dc9fefca23333e7c2adeafa9a18960eec8 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 5 Nov 2022 16:01:39 +0100 Subject: [PATCH] Add tests for exercise 07 --- .../java/ch/nuth/zhaw/exbox/ADS7_3_test.java | 76 +++++++++++++++++++ .../java/ch/nuth/zhaw/exbox/ADS7_4_test.java | 73 ++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 app/src/test/java/ch/nuth/zhaw/exbox/ADS7_3_test.java create mode 100644 app/src/test/java/ch/nuth/zhaw/exbox/ADS7_4_test.java diff --git a/app/src/test/java/ch/nuth/zhaw/exbox/ADS7_3_test.java b/app/src/test/java/ch/nuth/zhaw/exbox/ADS7_3_test.java new file mode 100644 index 0000000..2eaa5f0 --- /dev/null +++ b/app/src/test/java/ch/nuth/zhaw/exbox/ADS7_3_test.java @@ -0,0 +1,76 @@ +package ch.nuth.zhaw.exbox; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @(#)TreeTest.java + * + * + * @author K Rege + * @version 1.00 2018/3/17 + * @version 1.01 2021/8/1 + */ +public class ADS7_3_test { + RouteServer routeServer; + Graph graph; + String fileToTest = "RouteServer.java"; + + @BeforeEach + void init() throws Exception { + String swiss = "Winterthur Zürich 25\n" + + "Zürich Bern 126\n" + + "Zürich Genf 277\n" + + "Zürich Luzern 54\n" + + "Zürich Chur 121\n" + + "Zürich Berikon 16\n" + + "Bern Genf 155\n" + + "Genf Lugano 363\n" + + "Lugano Luzern 206\n" + + "Lugano Chur 152\n" + + "Chur Luzern 146\n" + + "Luzern Bern 97\n" + + "Bern Berikon 102\n" + + "Luzern Berikon 41\n"; + routeServer = new RouteServer(); + graph = routeServer.createGraph(swiss); + } + + private void testDest(DijkstraNode start, String destName, double dist) { + for (Edge road: start.getEdges()) { + DijkstraNode destTown = (DijkstraNode) road.getDest(); + if (destName.equals(destTown.getName())) { + assertEquals(dist, road.getWeight(), 1E-10, start.getName() + " to " + destName); + return; + } + } + fail(destName + " not connected to " + start.getName()); + } + + @Test + public void testCreateGrasphEdges() { + DijkstraNode town = graph.findNode("Luzern"); + assertNotNull(town, "Luzern"); + testDest(town, "Bern", 97); + testDest(town, "Berikon", 41); + testDest(town, "Chur", 146); + testDest(town, "Lugano", 206); + testDest(town, "Zürich", 54); + testDest(town, "Berikon", 41); + } + + @Test + public void testCreateGrasphNodes() { + DijkstraNode town = graph.findNode("Luzern"); + assertNotNull(town, "Luzern"); + town = graph.findNode("Winterthur"); + assertNotNull(town, "Winterthur"); + town = graph.findNode("Paris"); + assertNull(town, "Paris"); + } +} diff --git a/app/src/test/java/ch/nuth/zhaw/exbox/ADS7_4_test.java b/app/src/test/java/ch/nuth/zhaw/exbox/ADS7_4_test.java new file mode 100644 index 0000000..bd6b4ba --- /dev/null +++ b/app/src/test/java/ch/nuth/zhaw/exbox/ADS7_4_test.java @@ -0,0 +1,73 @@ +package ch.nuth.zhaw.exbox; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @(#)TreeTest.java + * + * + * @author K Rege + * @version 1.00 2018/3/17 + * @version 1.01 2021/8/1 + */ +public class ADS7_4_test { + RouteServer routeServer; + Graph graph; + String fileToTest = "RouteServer.java"; + + @BeforeEach + private void init() throws Exception { + String swiss = "Winterthur Zürich 25\n" + + "Zürich Bern 126\n" + + "Zürich Genf 277\n" + + "Zürich Luzern 54\n" + + "Zürich Chur 121\n" + + "Zürich Berikon 16\n" + + "Bern Genf 155\n" + + "Genf Lugano 363\n" + + "Lugano Luzern 206\n" + + "Lugano Chur 152\n" + + "Chur Luzern 146\n" + + "Luzern Bern 97\n" + + "Bern Berikon 102\n" + + "Luzern Berikon 41\n"; + routeServer = new RouteServer(); + graph = routeServer.createGraph(swiss); + } + + private void testDest(List route, String startName, String destName, double dist) { + for (DijkstraNode town : route) { + if (destName.equals(town.getName())) { + assertEquals(dist, town.getDist(), 1E-10, startName + " to " + destName); + return; + } + } + fail(startName + " not connected to " + destName); + } + + @Test + public void testWinterthurLugano() { + routeServer.dijkstraRoute(graph, "Winterthur", "Lugano"); + List route = routeServer.getRoute(graph, "Lugano"); + testDest(route, "Winterthur", "Winterthur", 0); + testDest(route, "Winterthur", "Zürich", 25); + testDest(route, "Winterthur", "Luzern", 79); + testDest(route, "Winterthur", "Lugano", 285); + } + + @Test + public void testLuganoWinterthur() { + routeServer.dijkstraRoute(graph, "Lugano", "Winterthur"); + List route = routeServer.getRoute(graph, "Winterthur"); + testDest(route, "Lugano", "Winterthur", 285); + testDest(route, "Lugano", "Zürich", 260); + testDest(route, "Lugano", "Luzern", 206); + testDest(route, "Lugano", "Lugano", 0); + } +}