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,12 +50,13 @@ 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>());
singleNodePath = new Path(graph, nodes[1]);
shortPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_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 })); 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 })); loopPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2a }));
@ -67,6 +69,7 @@ public class PathTest {
@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);
} }
@ -141,6 +167,17 @@ public class PathTest {
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
@ -165,6 +202,17 @@ public class PathTest {
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)