1 import junit
.framework
.*;
6 * @author "Anton Johansson" <anton.johansson@gmail.com>
8 public class GraphTest
extends TestCase
{
9 public GraphTest(String name
) {
13 public void testEmpty() {
14 Graph g
= new Graph(11, 24);
15 assertTrue(g
.isEmpty());
16 g
.insertNode("lund", 0, 0);
17 assertFalse(g
.isEmpty());
18 g
.insertNode("malmö", 3, 4);
19 g
.deleteNode(g
.getNode("lund"));
20 assertFalse(g
.isEmpty());
21 g
.deleteNode(g
.getNode("malmö"));
22 assertTrue(g
.isEmpty());
26 public void testEdges() {
27 Graph g
= new Graph(11, 24);
28 g
.insertNode("lund", 1, 2);
29 g
.insertNode("malmö", 1, 2);
30 g
.insertNode("eslöv", 1, 2);
31 g
.insertNode("umeå", 1, 2);
32 g
.insertNode("stockholm", 2, 4);
33 g
.insertEdge("lund", "malmö", new Double(100));
34 g
.insertEdge("lund", "eslöv", new Double(200));
35 g
.insertEdge("eslöv", "umeå", new Double(300));
36 g
.insertEdge("lund", "stockholm", new Double(400));
37 assertEquals("malmö", g
.getNode("malmö").getName());
38 assertEquals("umeå", g
.getNode("umeå").getName());
39 assertFalse("malmö" != g
.getNode("malmö").getName());
40 assertEquals(new Double(100.0), g
.getEdge("lund", "malmö"));
41 assertEquals(new Double(200.0), g
.getEdge("lund", "eslöv"));
42 assertEquals(new Double(300.0), g
.getEdge("eslöv", "umeå"));
43 //assertEquals(300.0, g.getEdge("umeå", "eslöv"));
44 assertEquals(new Double(400.0), g
.getEdge("lund", "stockholm"));
47 public void testSetGetDistance() {
48 Graph g
= new Graph(11, 24);
49 GraphNode lund
= g
.insertNode("lund", 0, 0);
50 GraphNode malmo
= g
.insertNode("malmö", 0, 2);
51 GraphNode eslov
= g
.insertNode("eslöv", 5, 0);
52 GraphNode umea
= g
.insertNode("umeå", 4, 7);
53 GraphNode kiruna
= g
.insertNode("kiruna", 9, 15);
55 assertEquals(0, lund
.getX());
56 assertEquals(0, lund
.getY());
58 assertEquals(0, malmo
.getX());
59 assertEquals(2, malmo
.getY());
61 lund
.setDistanceToGoal(malmo
);
62 assertEquals(2.0, lund
.getDistanceToGoal());
64 lund
.setDistanceToGoal(eslov
);
65 assertEquals(5.0, lund
.getDistanceToGoal());
67 lund
.setDistanceToGoal(umea
);
68 // (sqrt (+ (expt 4 2) (expt 7 2)))
69 assertEquals(8.06225774829855, lund
.getDistanceToGoal(), 0.0000000001);
71 umea
.setDistanceToGoal(kiruna
);
72 // (sqrt (+ (expt (- 4 9) 2) (expt (- 7 15) 2)))
73 assertEquals(9.433981132056603, umea
.getDistanceToGoal(), 0.0000000001);