diff --git a/src/main/org/insa/graph/Path.java b/src/main/org/insa/graph/Path.java index d660eff..22e7a1f 100644 --- a/src/main/org/insa/graph/Path.java +++ b/src/main/org/insa/graph/Path.java @@ -167,7 +167,16 @@ public class Path { * @return true if this path is empty, false otherwise. */ public boolean isEmpty() { - return arcs.isEmpty(); + return this.origin == null; + } + + /** + * Get the number of nodes in this path. + * + * @return Number of nodes in this path. + */ + public int size() { + return 1 + this.arcs.size(); } /** diff --git a/src/test/org/insa/graph/PathTest.java b/src/test/org/insa/graph/PathTest.java index ee4d327..52ab860 100644 --- a/src/test/org/insa/graph/PathTest.java +++ b/src/test/org/insa/graph/PathTest.java @@ -25,7 +25,8 @@ public class PathTest { private static Arc a2b, a2c, a2e, b2c, c2d_1, c2d_2, c2d_3, c2a, d2a, d2e, e2d; // Some paths... - private static Path emptyPath, shortPath, longPath, loopPath, longLoopPath, invalidPath; + private static Path emptyPath, singleNodePath, shortPath, longPath, loopPath, longLoopPath, + invalidPath; @BeforeClass public static void initAll() throws IOException { @@ -49,24 +50,26 @@ public class PathTest { c2d_2 = Node.linkNodes(nodes[2], nodes[3], 10, speed10, null); c2d_3 = Node.linkNodes(nodes[2], nodes[3], 15, speed20, null); d2a = Node.linkNodes(nodes[3], nodes[0], 15, speed10, null); - d2e = Node.linkNodes(nodes[3], nodes[4], 20, speed20, null); + d2e = Node.linkNodes(nodes[3], nodes[4], 22.8f, speed20, null); e2d = Node.linkNodes(nodes[4], nodes[0], 10, speed10, null); graph = new Graph("ID", "", Arrays.asList(nodes), null); emptyPath = new Path(graph, new ArrayList()); - shortPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1 })); - longPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2e })); - loopPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2a })); + singleNodePath = new Path(graph, nodes[1]); + shortPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1 })); + longPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2e })); + loopPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2a })); longLoopPath = new Path(graph, - Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2a, a2c, c2d_3, d2a, a2b, b2c })); - invalidPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, c2d_1, d2e })); + Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2a, a2c, c2d_3, d2a, a2b, b2c })); + invalidPath = new Path(graph, Arrays.asList(new Arc[] { a2b, c2d_1, d2e })); } @Test public void testConstructor() { assertEquals(graph, emptyPath.getGraph()); + assertEquals(graph, singleNodePath.getGraph()); assertEquals(graph, shortPath.getGraph()); assertEquals(graph, longPath.getGraph()); assertEquals(graph, loopPath.getGraph()); @@ -83,6 +86,7 @@ public class PathTest { public void testIsEmpty() { assertTrue(emptyPath.isEmpty()); + assertFalse(singleNodePath.isEmpty()); assertFalse(shortPath.isEmpty()); assertFalse(longPath.isEmpty()); assertFalse(loopPath.isEmpty()); @@ -93,6 +97,7 @@ public class PathTest { @Test public void testIsValid() { assertTrue(emptyPath.isValid()); + assertTrue(singleNodePath.isValid()); assertTrue(shortPath.isValid()); assertTrue(longPath.isValid()); assertTrue(loopPath.isValid()); @@ -104,17 +109,38 @@ public class PathTest { @Test public void testGetLength() { assertEquals(emptyPath.getLength(), 0, 1e-6); + assertEquals(singleNodePath.getLength(), 0, 1e-6); assertEquals(shortPath.getLength(), 40, 1e-6); - assertEquals(longPath.getLength(), 60, 1e-6); + assertEquals(longPath.getLength(), 62.8, 1e-6); assertEquals(loopPath.getLength(), 55, 1e-6); assertEquals(longLoopPath.getLength(), 120, 1e-6); } + @Test + public void testGetTravelTime() { + // Note: 18 km/h = 5m/s + assertEquals(emptyPath.getTravelTime(18), 0, 1e-6); + assertEquals(singleNodePath.getTravelTime(18), 0, 1e-6); + assertEquals(shortPath.getTravelTime(18), 8, 1e-6); + assertEquals(longPath.getTravelTime(18), 12.56, 1e-6); + assertEquals(loopPath.getTravelTime(18), 11, 1e-6); + assertEquals(longLoopPath.getTravelTime(18), 24, 1e-6); + + // Note: 28.8 km/h = 8m/s + assertEquals(emptyPath.getTravelTime(28.8), 0, 1e-6); + assertEquals(singleNodePath.getTravelTime(28.8), 0, 1e-6); + assertEquals(shortPath.getTravelTime(28.8), 5, 1e-6); + assertEquals(longPath.getTravelTime(28.8), 7.85, 1e-6); + assertEquals(loopPath.getTravelTime(28.8), 6.875, 1e-6); + assertEquals(longLoopPath.getTravelTime(28.8), 15, 1e-6); + } + @Test public void testGetMinimumTravelTime() { assertEquals(emptyPath.getMinimumTravelTime(), 0, 1e-4); + assertEquals(singleNodePath.getLength(), 0, 1e-4); assertEquals(shortPath.getMinimumTravelTime(), 4, 1e-4); - assertEquals(longPath.getMinimumTravelTime(), 5, 1e-4); + assertEquals(longPath.getMinimumTravelTime(), 5.14, 1e-4); assertEquals(loopPath.getMinimumTravelTime(), 5.5, 1e-4); assertEquals(longLoopPath.getMinimumTravelTime(), 11.25, 1e-4); } @@ -126,8 +152,8 @@ public class PathTest { // Simple construction path = Path.createFastestPathFromNodes(graph, - Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2] })); - expected = new Arc[]{ a2b, b2c }; + Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2] })); + expected = new Arc[] { a2b, b2c }; assertEquals(expected.length, path.getArcs().size()); for (int i = 0; i < expected.length; ++i) { assertEquals(expected[i], path.getArcs().get(i)); @@ -135,12 +161,23 @@ public class PathTest { // Not so simple construction path = Path.createFastestPathFromNodes(graph, - Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2], nodes[3] })); - expected = new Arc[]{ a2b, b2c, c2d_3 }; + Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2], nodes[3] })); + expected = new Arc[] { a2b, b2c, c2d_3 }; assertEquals(expected.length, path.getArcs().size()); for (int i = 0; i < expected.length; ++i) { assertEquals(expected[i], path.getArcs().get(i)); } + + // Trap construction! + path = Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1] })); + assertEquals(path.getOrigin(), nodes[1]); + assertEquals(path.getArcs().size(), 0); + + // Trap construction - The return! + path = Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[0])); + assertEquals(path.getOrigin(), null); + assertEquals(path.getArcs().size(), 0); + assertTrue(path.isEmpty()); } @Test @@ -150,8 +187,8 @@ public class PathTest { // Simple construction path = Path.createShortestPathFromNodes(graph, - Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2] })); - expected = new Arc[]{ a2b, b2c }; + Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2] })); + expected = new Arc[] { a2b, b2c }; assertEquals(expected.length, path.getArcs().size()); for (int i = 0; i < expected.length; ++i) { assertEquals(expected[i], path.getArcs().get(i)); @@ -159,22 +196,33 @@ public class PathTest { // Not so simple construction path = Path.createShortestPathFromNodes(graph, - Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2], nodes[3] })); - expected = new Arc[]{ a2b, b2c, c2d_2 }; + Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2], nodes[3] })); + expected = new Arc[] { a2b, b2c, c2d_2 }; assertEquals(expected.length, path.getArcs().size()); for (int i = 0; i < expected.length; ++i) { assertEquals(expected[i], path.getArcs().get(i)); } + + // Trap construction! + path = Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1] })); + assertEquals(path.getOrigin(), nodes[1]); + assertEquals(path.getArcs().size(), 0); + + // Trap construction - The return! + path = Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[0])); + assertEquals(path.getOrigin(), null); + assertEquals(path.getArcs().size(), 0); + assertTrue(path.isEmpty()); } @Test(expected = IllegalArgumentException.class) public void testCreateFastestPathFromNodesException() { - Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[]{ nodes[1], nodes[0] })); + Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1], nodes[0] })); } @Test(expected = IllegalArgumentException.class) public void testCreateShortestPathFromNodesException() { - Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[]{ nodes[1], nodes[0] })); + Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1], nodes[0] })); } }