No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PathTest.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package org.insa.graph;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertTrue;
  5. import java.io.IOException;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import org.insa.graph.RoadInformation.RoadType;
  9. import org.junit.BeforeClass;
  10. import org.junit.Test;
  11. public class PathTest {
  12. // Small graph use for tests
  13. private static Graph graph;
  14. // List of nodes
  15. private static Node[] nodes;
  16. // List of arcs in the graph, a2b is the arc from node A (0) to B (1).
  17. @SuppressWarnings("unused")
  18. private static Arc a2b, a2c, a2e, b2c, c2d_1, c2d_2, c2d_3, c2a, d2a, d2e, e2d;
  19. // Some paths...
  20. private static Path emptyPath, singleNodePath, shortPath, longPath, loopPath, longLoopPath,
  21. invalidPath;
  22. @BeforeClass
  23. public static void initAll() throws IOException {
  24. // 10 and 20 meters per seconds
  25. RoadInformation speed10 = new RoadInformation(RoadType.MOTORWAY, null, true, 36, ""),
  26. speed20 = new RoadInformation(RoadType.MOTORWAY, null, true, 72, "");
  27. // Create nodes
  28. nodes = new Node[5];
  29. for (int i = 0; i < nodes.length; ++i) {
  30. nodes[i] = new Node(i, null);
  31. }
  32. // Add arcs...
  33. a2b = Node.linkNodes(nodes[0], nodes[1], 10, speed10, null);
  34. a2c = Node.linkNodes(nodes[0], nodes[2], 15, speed10, null);
  35. a2e = Node.linkNodes(nodes[0], nodes[4], 15, speed20, null);
  36. b2c = Node.linkNodes(nodes[1], nodes[2], 10, speed10, null);
  37. c2d_1 = Node.linkNodes(nodes[2], nodes[3], 20, speed10, null);
  38. c2d_2 = Node.linkNodes(nodes[2], nodes[3], 10, speed10, null);
  39. c2d_3 = Node.linkNodes(nodes[2], nodes[3], 15, speed20, null);
  40. d2a = Node.linkNodes(nodes[3], nodes[0], 15, speed10, null);
  41. d2e = Node.linkNodes(nodes[3], nodes[4], 22.8f, speed20, null);
  42. e2d = Node.linkNodes(nodes[4], nodes[0], 10, speed10, null);
  43. graph = new Graph("ID", "", Arrays.asList(nodes), null);
  44. emptyPath = new Path(graph, new ArrayList<Arc>());
  45. singleNodePath = new Path(graph, nodes[1]);
  46. shortPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1 }));
  47. longPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2e }));
  48. loopPath = new Path(graph, Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2a }));
  49. longLoopPath = new Path(graph,
  50. Arrays.asList(new Arc[] { a2b, b2c, c2d_1, d2a, a2c, c2d_3, d2a, a2b, b2c }));
  51. invalidPath = new Path(graph, Arrays.asList(new Arc[] { a2b, c2d_1, d2e }));
  52. }
  53. @Test
  54. public void testConstructor() {
  55. assertEquals(graph, emptyPath.getGraph());
  56. assertEquals(graph, singleNodePath.getGraph());
  57. assertEquals(graph, shortPath.getGraph());
  58. assertEquals(graph, longPath.getGraph());
  59. assertEquals(graph, loopPath.getGraph());
  60. assertEquals(graph, longLoopPath.getGraph());
  61. assertEquals(graph, invalidPath.getGraph());
  62. }
  63. @Test(expected = UnsupportedOperationException.class)
  64. public void testImmutability() {
  65. emptyPath.getArcs().add(a2b);
  66. }
  67. @Test
  68. public void testIsEmpty() {
  69. assertTrue(emptyPath.isEmpty());
  70. assertFalse(singleNodePath.isEmpty());
  71. assertFalse(shortPath.isEmpty());
  72. assertFalse(longPath.isEmpty());
  73. assertFalse(loopPath.isEmpty());
  74. assertFalse(longLoopPath.isEmpty());
  75. assertFalse(invalidPath.isEmpty());
  76. }
  77. @Test
  78. public void testSize() {
  79. assertEquals(emptyPath.size(), 0);
  80. assertEquals(singleNodePath.size(), 1);
  81. assertEquals(shortPath.size(), 4);
  82. assertEquals(longPath.size(), 5);
  83. assertEquals(loopPath.size(), 5);
  84. assertEquals(longLoopPath.size(), 10);
  85. }
  86. @Test
  87. public void testIsValid() {
  88. assertTrue(emptyPath.isValid());
  89. assertTrue(singleNodePath.isValid());
  90. assertTrue(shortPath.isValid());
  91. assertTrue(longPath.isValid());
  92. assertTrue(loopPath.isValid());
  93. assertTrue(longLoopPath.isValid());
  94. assertFalse(invalidPath.isValid());
  95. }
  96. @Test
  97. public void testGetLength() {
  98. assertEquals(emptyPath.getLength(), 0, 1e-6);
  99. assertEquals(singleNodePath.getLength(), 0, 1e-6);
  100. assertEquals(shortPath.getLength(), 40, 1e-6);
  101. assertEquals(longPath.getLength(), 62.8, 1e-6);
  102. assertEquals(loopPath.getLength(), 55, 1e-6);
  103. assertEquals(longLoopPath.getLength(), 120, 1e-6);
  104. }
  105. @Test
  106. public void testGetTravelTime() {
  107. // Note: 18 km/h = 5m/s
  108. assertEquals(emptyPath.getTravelTime(18), 0, 1e-6);
  109. assertEquals(singleNodePath.getTravelTime(18), 0, 1e-6);
  110. assertEquals(shortPath.getTravelTime(18), 8, 1e-6);
  111. assertEquals(longPath.getTravelTime(18), 12.56, 1e-6);
  112. assertEquals(loopPath.getTravelTime(18), 11, 1e-6);
  113. assertEquals(longLoopPath.getTravelTime(18), 24, 1e-6);
  114. // Note: 28.8 km/h = 8m/s
  115. assertEquals(emptyPath.getTravelTime(28.8), 0, 1e-6);
  116. assertEquals(singleNodePath.getTravelTime(28.8), 0, 1e-6);
  117. assertEquals(shortPath.getTravelTime(28.8), 5, 1e-6);
  118. assertEquals(longPath.getTravelTime(28.8), 7.85, 1e-6);
  119. assertEquals(loopPath.getTravelTime(28.8), 6.875, 1e-6);
  120. assertEquals(longLoopPath.getTravelTime(28.8), 15, 1e-6);
  121. }
  122. @Test
  123. public void testGetMinimumTravelTime() {
  124. assertEquals(emptyPath.getMinimumTravelTime(), 0, 1e-4);
  125. assertEquals(singleNodePath.getLength(), 0, 1e-4);
  126. assertEquals(shortPath.getMinimumTravelTime(), 4, 1e-4);
  127. assertEquals(longPath.getMinimumTravelTime(), 5.14, 1e-4);
  128. assertEquals(loopPath.getMinimumTravelTime(), 5.5, 1e-4);
  129. assertEquals(longLoopPath.getMinimumTravelTime(), 11.25, 1e-4);
  130. }
  131. @Test
  132. public void testCreateFastestPathFromNodes() {
  133. Path path;
  134. Arc[] expected;
  135. // Simple construction
  136. path = Path.createFastestPathFromNodes(graph,
  137. Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2] }));
  138. expected = new Arc[] { a2b, b2c };
  139. assertEquals(expected.length, path.getArcs().size());
  140. for (int i = 0; i < expected.length; ++i) {
  141. assertEquals(expected[i], path.getArcs().get(i));
  142. }
  143. // Not so simple construction
  144. path = Path.createFastestPathFromNodes(graph,
  145. Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2], nodes[3] }));
  146. expected = new Arc[] { a2b, b2c, c2d_3 };
  147. assertEquals(expected.length, path.getArcs().size());
  148. for (int i = 0; i < expected.length; ++i) {
  149. assertEquals(expected[i], path.getArcs().get(i));
  150. }
  151. // Trap construction!
  152. path = Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1] }));
  153. assertEquals(path.getOrigin(), nodes[1]);
  154. assertEquals(path.getArcs().size(), 0);
  155. // Trap construction - The return!
  156. path = Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[0]));
  157. assertEquals(path.getOrigin(), null);
  158. assertEquals(path.getArcs().size(), 0);
  159. assertTrue(path.isEmpty());
  160. }
  161. @Test
  162. public void testCreateShortestPathFromNodes() {
  163. Path path;
  164. Arc[] expected;
  165. // Simple construction
  166. path = Path.createShortestPathFromNodes(graph,
  167. Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2] }));
  168. expected = new Arc[] { a2b, b2c };
  169. assertEquals(expected.length, path.getArcs().size());
  170. for (int i = 0; i < expected.length; ++i) {
  171. assertEquals(expected[i], path.getArcs().get(i));
  172. }
  173. // Not so simple construction
  174. path = Path.createShortestPathFromNodes(graph,
  175. Arrays.asList(new Node[] { nodes[0], nodes[1], nodes[2], nodes[3] }));
  176. expected = new Arc[] { a2b, b2c, c2d_2 };
  177. assertEquals(expected.length, path.getArcs().size());
  178. for (int i = 0; i < expected.length; ++i) {
  179. assertEquals(expected[i], path.getArcs().get(i));
  180. }
  181. // Trap construction!
  182. path = Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1] }));
  183. assertEquals(path.getOrigin(), nodes[1]);
  184. assertEquals(path.getArcs().size(), 0);
  185. // Trap construction - The return!
  186. path = Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[0]));
  187. assertEquals(path.getOrigin(), null);
  188. assertEquals(path.getArcs().size(), 0);
  189. assertTrue(path.isEmpty());
  190. }
  191. @Test(expected = IllegalArgumentException.class)
  192. public void testCreateFastestPathFromNodesException() {
  193. Path.createFastestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1], nodes[0] }));
  194. }
  195. @Test(expected = IllegalArgumentException.class)
  196. public void testCreateShortestPathFromNodesException() {
  197. Path.createShortestPathFromNodes(graph, Arrays.asList(new Node[] { nodes[1], nodes[0] }));
  198. }
  199. }