Browse Source

Update paths and tests.

Holt59 3 years ago
parent
commit
46436f81f3
2 changed files with 77 additions and 20 deletions
  1. 10
    1
      src/main/org/insa/graph/Path.java
  2. 67
    19
      src/test/org/insa/graph/PathTest.java

+ 10
- 1
src/main/org/insa/graph/Path.java View File

@@ -167,7 +167,16 @@ public class Path {
167 167
      * @return true if this path is empty, false otherwise.
168 168
      */
169 169
     public boolean isEmpty() {
170
-        return arcs.isEmpty();
170
+        return this.origin == null;
171
+    }
172
+
173
+    /**
174
+     * Get the number of <b>nodes</b> in this path.
175
+     * 
176
+     * @return Number of nodes in this path.
177
+     */
178
+    public int size() {
179
+        return 1 + this.arcs.size();
171 180
     }
172 181
 
173 182
     /**

+ 67
- 19
src/test/org/insa/graph/PathTest.java View File

@@ -25,7 +25,8 @@ public class PathTest {
25 25
     private static Arc a2b, a2c, a2e, b2c, c2d_1, c2d_2, c2d_3, c2a, d2a, d2e, e2d;
26 26
 
27 27
     // Some paths...
28
-    private static Path emptyPath, shortPath, longPath, loopPath, longLoopPath, invalidPath;
28
+    private static Path emptyPath, singleNodePath, shortPath, longPath, loopPath, longLoopPath,
29
+            invalidPath;
29 30
 
30 31
     @BeforeClass
31 32
     public static void initAll() throws IOException {
@@ -49,24 +50,26 @@ public class PathTest {
49 50
         c2d_2 = Node.linkNodes(nodes[2], nodes[3], 10, speed10, null);
50 51
         c2d_3 = Node.linkNodes(nodes[2], nodes[3], 15, speed20, null);
51 52
         d2a = Node.linkNodes(nodes[3], nodes[0], 15, speed10, null);
52
-        d2e = Node.linkNodes(nodes[3], nodes[4], 20, speed20, null);
53
+        d2e = Node.linkNodes(nodes[3], nodes[4], 22.8f, speed20, null);
53 54
         e2d = Node.linkNodes(nodes[4], nodes[0], 10, speed10, null);
54 55
 
55 56
         graph = new Graph("ID", "", Arrays.asList(nodes), null);
56 57
 
57 58
         emptyPath = new Path(graph, new ArrayList<Arc>());
58
-        shortPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1 }));
59
-        longPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2e }));
60
-        loopPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2a }));
59
+        singleNodePath = new Path(graph, nodes[1]);
60
+        shortPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1 }));
61
+        longPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2e }));
62
+        loopPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2a }));
61 63
         longLoopPath = new Path(graph,
62
-                Arrays.asList(new Arc[]{ a2b, b2c, c2d_1, d2a, a2c, c2d_3, d2a, a2b, b2c }));
63
-        invalidPath = new Path(graph, Arrays.asList(new Arc[]{ a2b, c2d_1, d2e }));
64
+                Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2a, a2c, c2d_3, d2a, a2b, b2c }));
65
+        invalidPath = new Path(graph, Arrays.asList(new Arc[] { a2b, c2d_1, d2e }));
64 66
 
65 67
     }
66 68
 
67 69
     @Test
68 70
     public void testConstructor() {
69 71
         assertEquals(graph, emptyPath.getGraph());
72
+        assertEquals(graph, singleNodePath.getGraph());
70 73
         assertEquals(graph, shortPath.getGraph());
71 74
         assertEquals(graph, longPath.getGraph());
72 75
         assertEquals(graph, loopPath.getGraph());
@@ -83,6 +86,7 @@ public class PathTest {
83 86
     public void testIsEmpty() {
84 87
         assertTrue(emptyPath.isEmpty());
85 88
 
89
+        assertFalse(singleNodePath.isEmpty());
86 90
         assertFalse(shortPath.isEmpty());
87 91
         assertFalse(longPath.isEmpty());
88 92
         assertFalse(loopPath.isEmpty());
@@ -93,6 +97,7 @@ public class PathTest {
93 97
     @Test
94 98
     public void testIsValid() {
95 99
         assertTrue(emptyPath.isValid());
100
+        assertTrue(singleNodePath.isValid());
96 101
         assertTrue(shortPath.isValid());
97 102
         assertTrue(longPath.isValid());
98 103
         assertTrue(loopPath.isValid());
@@ -104,17 +109,38 @@ public class PathTest {
104 109
     @Test
105 110
     public void testGetLength() {
106 111
         assertEquals(emptyPath.getLength(), 0, 1e-6);
112
+        assertEquals(singleNodePath.getLength(), 0, 1e-6);
107 113
         assertEquals(shortPath.getLength(), 40, 1e-6);
108
-        assertEquals(longPath.getLength(), 60, 1e-6);
114
+        assertEquals(longPath.getLength(), 62.8, 1e-6);
109 115
         assertEquals(loopPath.getLength(), 55, 1e-6);
110 116
         assertEquals(longLoopPath.getLength(), 120, 1e-6);
111 117
     }
112 118
 
113 119
     @Test
120
+    public void testGetTravelTime() {
121
+        // Note: 18 km/h = 5m/s
122
+        assertEquals(emptyPath.getTravelTime(18), 0, 1e-6);
123
+        assertEquals(singleNodePath.getTravelTime(18), 0, 1e-6);
124
+        assertEquals(shortPath.getTravelTime(18), 8, 1e-6);
125
+        assertEquals(longPath.getTravelTime(18), 12.56, 1e-6);
126
+        assertEquals(loopPath.getTravelTime(18), 11, 1e-6);
127
+        assertEquals(longLoopPath.getTravelTime(18), 24, 1e-6);
128
+
129
+        // Note: 28.8 km/h = 8m/s
130
+        assertEquals(emptyPath.getTravelTime(28.8), 0, 1e-6);
131
+        assertEquals(singleNodePath.getTravelTime(28.8), 0, 1e-6);
132
+        assertEquals(shortPath.getTravelTime(28.8), 5, 1e-6);
133
+        assertEquals(longPath.getTravelTime(28.8), 7.85, 1e-6);
134
+        assertEquals(loopPath.getTravelTime(28.8), 6.875, 1e-6);
135
+        assertEquals(longLoopPath.getTravelTime(28.8), 15, 1e-6);
136
+    }
137
+
138
+    @Test
114 139
     public void testGetMinimumTravelTime() {
115 140
         assertEquals(emptyPath.getMinimumTravelTime(), 0, 1e-4);
141
+        assertEquals(singleNodePath.getLength(), 0, 1e-4);
116 142
         assertEquals(shortPath.getMinimumTravelTime(), 4, 1e-4);
117
-        assertEquals(longPath.getMinimumTravelTime(), 5, 1e-4);
143
+        assertEquals(longPath.getMinimumTravelTime(), 5.14, 1e-4);
118 144
         assertEquals(loopPath.getMinimumTravelTime(), 5.5, 1e-4);
119 145
         assertEquals(longLoopPath.getMinimumTravelTime(), 11.25, 1e-4);
120 146
     }
@@ -126,8 +152,8 @@ public class PathTest {
126 152
 
127 153
         // Simple construction
128 154
         path = Path.createFastestPathFromNodes(graph,
129
-                Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2] }));
130
-        expected = new Arc[]{ a2b, b2c };
155
+                Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2] }));
156
+        expected = new Arc[] { a2b, b2c };
131 157
         assertEquals(expected.length, path.getArcs().size());
132 158
         for (int i = 0; i < expected.length; ++i) {
133 159
             assertEquals(expected[i], path.getArcs().get(i));
@@ -135,12 +161,23 @@ public class PathTest {
135 161
 
136 162
         // Not so simple construction
137 163
         path = Path.createFastestPathFromNodes(graph,
138
-                Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2], nodes[3] }));
139
-        expected = new Arc[]{ a2b, b2c, c2d_3 };
164
+                Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2], nodes[3] }));
165
+        expected = new Arc[] { a2b, b2c, c2d_3 };
140 166
         assertEquals(expected.length, path.getArcs().size());
141 167
         for (int i = 0; i < expected.length; ++i) {
142 168
             assertEquals(expected[i], path.getArcs().get(i));
143 169
         }
170
+
171
+        // Trap construction!
172
+        path = Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1] }));
173
+        assertEquals(path.getOrigin(), nodes[1]);
174
+        assertEquals(path.getArcs().size(), 0);
175
+
176
+        // Trap construction - The return!
177
+        path = Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[0]));
178
+        assertEquals(path.getOrigin(), null);
179
+        assertEquals(path.getArcs().size(), 0);
180
+        assertTrue(path.isEmpty());
144 181
     }
145 182
 
146 183
     @Test
@@ -150,8 +187,8 @@ public class PathTest {
150 187
 
151 188
         // Simple construction
152 189
         path = Path.createShortestPathFromNodes(graph,
153
-                Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2] }));
154
-        expected = new Arc[]{ a2b, b2c };
190
+                Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2] }));
191
+        expected = new Arc[] { a2b, b2c };
155 192
         assertEquals(expected.length, path.getArcs().size());
156 193
         for (int i = 0; i < expected.length; ++i) {
157 194
             assertEquals(expected[i], path.getArcs().get(i));
@@ -159,22 +196,33 @@ public class PathTest {
159 196
 
160 197
         // Not so simple construction
161 198
         path = Path.createShortestPathFromNodes(graph,
162
-                Arrays.asList(new Node[]{ nodes[0], nodes[1], nodes[2], nodes[3] }));
163
-        expected = new Arc[]{ a2b, b2c, c2d_2 };
199
+                Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2], nodes[3] }));
200
+        expected = new Arc[] { a2b, b2c, c2d_2 };
164 201
         assertEquals(expected.length, path.getArcs().size());
165 202
         for (int i = 0; i < expected.length; ++i) {
166 203
             assertEquals(expected[i], path.getArcs().get(i));
167 204
         }
205
+
206
+        // Trap construction!
207
+        path = Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1] }));
208
+        assertEquals(path.getOrigin(), nodes[1]);
209
+        assertEquals(path.getArcs().size(), 0);
210
+
211
+        // Trap construction - The return!
212
+        path = Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[0]));
213
+        assertEquals(path.getOrigin(), null);
214
+        assertEquals(path.getArcs().size(), 0);
215
+        assertTrue(path.isEmpty());
168 216
     }
169 217
 
170 218
     @Test(expected = IllegalArgumentException.class)
171 219
     public void testCreateFastestPathFromNodesException() {
172
-        Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[]{ nodes[1], nodes[0] }));
220
+        Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1], nodes[0] }));
173 221
     }
174 222
 
175 223
     @Test(expected = IllegalArgumentException.class)
176 224
     public void testCreateShortestPathFromNodesException() {
177
-        Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[]{ nodes[1], nodes[0] }));
225
+        Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1], nodes[0] }));
178 226
     }
179 227
 
180 228
 }

Loading…
Cancel
Save