Update paths and tests.

This commit is contained in:
Holt59 2018-03-20 21:31:01 +01:00
parent b4e666d08d
commit 46436f81f3
2 changed files with 77 additions and 20 deletions

View File

@ -167,7 +167,16 @@ public class Path {
* @return true if this path is empty, false otherwise. * @return true if this path is empty, false otherwise.
*/ */
public boolean isEmpty() { public boolean isEmpty() {
return arcs.isEmpty(); return this.origin == null;
}
/**
* Get the number of <b>nodes</b> in this path.
*
* @return Number of nodes in this path.
*/
public int size() {
return 1 + this.arcs.size();
} }
/** /**

View File

@ -25,7 +25,8 @@ public class PathTest {
private static Arc a2b, a2c, a2e, b2c, c2d_1, c2d_2, c2d_3, c2a, d2a, d2e, e2d; private static Arc a2b, a2c, a2e, b2c, c2d_1, c2d_2, c2d_3, c2a, d2a, d2e, e2d;
// Some paths... // Some paths...
private static Path emptyPath, shortPath, longPath, loopPath, longLoopPath, invalidPath; private static Path emptyPath, singleNodePath, shortPath, longPath, loopPath, longLoopPath,
invalidPath;
@BeforeClass @BeforeClass
public static void initAll() throws IOException { 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_2 = Node.linkNodes(nodes[2], nodes[3], 10, speed10, null);
c2d_3 = Node.linkNodes(nodes[2], nodes[3], 15, speed20, null); c2d_3 = Node.linkNodes(nodes[2], nodes[3], 15, speed20, null);
d2a = Node.linkNodes(nodes[3], nodes[0], 15, speed10, 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); e2d = Node.linkNodes(nodes[4], nodes[0], 10, speed10, null);
graph = new Graph("ID", "", Arrays.asList(nodes), null); graph = new Graph("ID", "", Arrays.asList(nodes), null);
emptyPath = new Path(graph, new ArrayList<Arc>()); emptyPath = new Path(graph, new ArrayList<Arc>());
shortPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1 })); singleNodePath = new Path(graph, nodes[1]);
longPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2e })); shortPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1 }));
loopPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2a })); 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, longLoopPath = new Path(graph,
Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2a, a2c, c2d_3, d2a, a2b, b2c })); 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 })); invalidPath = new Path(graph, Arrays.asList(new Arc[] { a2b, c2d_1, d2e }));
} }
@Test @Test
public void testConstructor() { public void testConstructor() {
assertEquals(graph, emptyPath.getGraph()); assertEquals(graph, emptyPath.getGraph());
assertEquals(graph, singleNodePath.getGraph());
assertEquals(graph, shortPath.getGraph()); assertEquals(graph, shortPath.getGraph());
assertEquals(graph, longPath.getGraph()); assertEquals(graph, longPath.getGraph());
assertEquals(graph, loopPath.getGraph()); assertEquals(graph, loopPath.getGraph());
@ -83,6 +86,7 @@ public class PathTest {
public void testIsEmpty() { public void testIsEmpty() {
assertTrue(emptyPath.isEmpty()); assertTrue(emptyPath.isEmpty());
assertFalse(singleNodePath.isEmpty());
assertFalse(shortPath.isEmpty()); assertFalse(shortPath.isEmpty());
assertFalse(longPath.isEmpty()); assertFalse(longPath.isEmpty());
assertFalse(loopPath.isEmpty()); assertFalse(loopPath.isEmpty());
@ -93,6 +97,7 @@ public class PathTest {
@Test @Test
public void testIsValid() { public void testIsValid() {
assertTrue(emptyPath.isValid()); assertTrue(emptyPath.isValid());
assertTrue(singleNodePath.isValid());
assertTrue(shortPath.isValid()); assertTrue(shortPath.isValid());
assertTrue(longPath.isValid()); assertTrue(longPath.isValid());
assertTrue(loopPath.isValid()); assertTrue(loopPath.isValid());
@ -104,17 +109,38 @@ public class PathTest {
@Test @Test
public void testGetLength() { public void testGetLength() {
assertEquals(emptyPath.getLength(), 0, 1e-6); assertEquals(emptyPath.getLength(), 0, 1e-6);
assertEquals(singleNodePath.getLength(), 0, 1e-6);
assertEquals(shortPath.getLength(), 40, 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(loopPath.getLength(), 55, 1e-6);
assertEquals(longLoopPath.getLength(), 120, 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 @Test
public void testGetMinimumTravelTime() { public void testGetMinimumTravelTime() {
assertEquals(emptyPath.getMinimumTravelTime(), 0, 1e-4); assertEquals(emptyPath.getMinimumTravelTime(), 0, 1e-4);
assertEquals(singleNodePath.getLength(), 0, 1e-4);
assertEquals(shortPath.getMinimumTravelTime(), 4, 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(loopPath.getMinimumTravelTime(), 5.5, 1e-4);
assertEquals(longLoopPath.getMinimumTravelTime(), 11.25, 1e-4); assertEquals(longLoopPath.getMinimumTravelTime(), 11.25, 1e-4);
} }
@ -126,8 +152,8 @@ public class PathTest {
// Simple construction // Simple construction
path = Path.createFastestPathFromNodes(graph, path = Path.createFastestPathFromNodes(graph,
Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2] })); Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2] }));
expected = new Arc[]{ a2b, b2c }; expected = new Arc[] { a2b, b2c };
assertEquals(expected.length, path.getArcs().size()); assertEquals(expected.length, path.getArcs().size());
for (int i = 0; i < expected.length; ++i) { for (int i = 0; i < expected.length; ++i) {
assertEquals(expected[i], path.getArcs().get(i)); assertEquals(expected[i], path.getArcs().get(i));
@ -135,12 +161,23 @@ public class PathTest {
// Not so simple construction // Not so simple construction
path = Path.createFastestPathFromNodes(graph, path = Path.createFastestPathFromNodes(graph,
Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2], nodes[3] })); Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2], nodes[3] }));
expected = new Arc[]{ a2b, b2c, c2d_3 }; expected = new Arc[] { a2b, b2c, c2d_3 };
assertEquals(expected.length, path.getArcs().size()); assertEquals(expected.length, path.getArcs().size());
for (int i = 0; i < expected.length; ++i) { for (int i = 0; i < expected.length; ++i) {
assertEquals(expected[i], path.getArcs().get(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 @Test
@ -150,8 +187,8 @@ public class PathTest {
// Simple construction // Simple construction
path = Path.createShortestPathFromNodes(graph, path = Path.createShortestPathFromNodes(graph,
Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2] })); Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2] }));
expected = new Arc[]{ a2b, b2c }; expected = new Arc[] { a2b, b2c };
assertEquals(expected.length, path.getArcs().size()); assertEquals(expected.length, path.getArcs().size());
for (int i = 0; i < expected.length; ++i) { for (int i = 0; i < expected.length; ++i) {
assertEquals(expected[i], path.getArcs().get(i)); assertEquals(expected[i], path.getArcs().get(i));
@ -159,22 +196,33 @@ public class PathTest {
// Not so simple construction // Not so simple construction
path = Path.createShortestPathFromNodes(graph, path = Path.createShortestPathFromNodes(graph,
Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2], nodes[3] })); Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2], nodes[3] }));
expected = new Arc[]{ a2b, b2c, c2d_2 }; expected = new Arc[] { a2b, b2c, c2d_2 };
assertEquals(expected.length, path.getArcs().size()); assertEquals(expected.length, path.getArcs().size());
for (int i = 0; i < expected.length; ++i) { for (int i = 0; i < expected.length; ++i) {
assertEquals(expected[i], path.getArcs().get(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) @Test(expected = IllegalArgumentException.class)
public void testCreateFastestPathFromNodesException() { 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) @Test(expected = IllegalArgumentException.class)
public void testCreateShortestPathFromNodesException() { 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] }));
} }
} }