Browse Source

Remove access to underlying containers inside Graph and Node.

Holt59 3 years ago
parent
commit
bdb378c079

+ 3
- 3
src/main/org/insa/algo/shortestpath/BellmanFordAlgorithm.java View File

@@ -24,7 +24,7 @@ public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
24 24
         ShortestPathData data = getInputData();
25 25
         Graph graph = data.getGraph();
26 26
 
27
-        final int nbNodes = graph.getNodes().size();
27
+        final int nbNodes = graph.size();
28 28
 
29 29
         // Initialize array of distances.
30 30
         double[] distances = new double[nbNodes];
@@ -41,8 +41,8 @@ public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
41 41
         boolean found = false;
42 42
         for (int i = 0; !found && i < nbNodes; ++i) {
43 43
             found = true;
44
-            for (Node node: graph.getNodes()) {
45
-                for (Arc arc: node.getSuccessors()) {
44
+            for (Node node: graph) {
45
+                for (Arc arc: node) {
46 46
 
47 47
                     // Small test to check allowed roads...
48 48
                     if (!data.isAllowed(arc)) {

+ 39
- 29
src/main/org/insa/algo/utils/BinaryHeap.java View File

@@ -1,9 +1,9 @@
1 1
 //
2 2
 // ******************PUBLIC OPERATIONS*********************
3
-// void insert( x )       --> Insert x
3
+// void insert( x ) --> Insert x
4 4
 // Comparable deleteMin( )--> Return and remove smallest item
5
-// Comparable findMin( )  --> Return smallest item
6
-// boolean isEmpty( )     --> Return true if empty; else false
5
+// Comparable findMin( ) --> Return smallest item
6
+// boolean isEmpty( ) --> Return true if empty; else false
7 7
 // ******************ERRORS********************************
8 8
 // Throws RuntimeException for findMin and deleteMin when empty
9 9
 
@@ -61,20 +61,6 @@ public class BinaryHeap<E extends Comparable<E>> {
61 61
     }
62 62
 
63 63
     /**
64
-     * @return true if the heap is empty, false otherwise.
65
-     */
66
-    public boolean isEmpty() {
67
-        return this.currentSize == 0;
68
-    }
69
-
70
-    /**
71
-     * @return Current size (number of elements) of this heap.
72
-     */
73
-    public int size() {
74
-        return this.currentSize;
75
-    }
76
-
77
-    /**
78 64
      * @return Index of the parent of the given index.
79 65
      */
80 66
     private int index_parent(int index) {
@@ -89,17 +75,6 @@ public class BinaryHeap<E extends Comparable<E>> {
89 75
     }
90 76
 
91 77
     /**
92
-     * Insert the given element into the heap.
93
-     * 
94
-     * @param x Item to insert.
95
-     */
96
-    public void insert(E x) {
97
-        int index = this.currentSize++;
98
-        this.arraySet(index, x);
99
-        this.percolateUp(index);
100
-    }
101
-
102
-    /**
103 78
      * Internal method to percolate up in the heap.
104 79
      * 
105 80
      * @param index Index at which the percolate begins.
@@ -152,6 +127,41 @@ public class BinaryHeap<E extends Comparable<E>> {
152 127
     }
153 128
 
154 129
     /**
130
+     * @return true if the heap is empty, false otherwise.
131
+     */
132
+    public boolean isEmpty() {
133
+        return this.currentSize == 0;
134
+    }
135
+
136
+    /**
137
+     * @return Current size (number of elements) of this heap.
138
+     */
139
+    public int size() {
140
+        return this.currentSize;
141
+    }
142
+
143
+    /**
144
+     * Insert the given element into the heap.
145
+     * 
146
+     * @param x Item to insert.
147
+     */
148
+    public void add(E x) {
149
+        int index = this.currentSize++;
150
+        this.arraySet(index, x);
151
+        this.percolateUp(index);
152
+    }
153
+
154
+    /**
155
+     * Tell the binary heap that the given element has been modified and should be
156
+     * re-positioned inside the heap.
157
+     * 
158
+     * @param x Item to update.
159
+     */
160
+    public void update(E x) {
161
+        // TODO:
162
+    }
163
+
164
+    /**
155 165
      * Find the smallest item in the heap.
156 166
      * 
157 167
      * @return The smallest item in the heap.
@@ -160,7 +170,7 @@ public class BinaryHeap<E extends Comparable<E>> {
160 170
      */
161 171
     public E findMin() throws RuntimeException {
162 172
         if (isEmpty())
163
-            throw new RuntimeException("Empty binary heap");
173
+            throw new RuntimeException("Empty binary heap.");
164 174
         return this.array.get(0);
165 175
     }
166 176
 

+ 8
- 9
src/main/org/insa/algo/weakconnectivity/WeaklyConnectedComponentsAlgorithm.java View File

@@ -4,7 +4,6 @@ import java.util.ArrayList;
4 4
 import java.util.Arrays;
5 5
 import java.util.HashSet;
6 6
 import java.util.LinkedList;
7
-import java.util.List;
8 7
 import java.util.Queue;
9 8
 
10 9
 import org.insa.algo.AbstractAlgorithm;
@@ -72,14 +71,14 @@ public class WeaklyConnectedComponentsAlgorithm
72 71
      *         graph.
73 72
      */
74 73
     protected ArrayList<HashSet<Integer>> createUndirectedGraph() {
75
-        int nNodes = getInputData().getGraph().getNodes().size();
74
+        int nNodes = getInputData().getGraph().size();
76 75
         ArrayList<HashSet<Integer>> res = new ArrayList<HashSet<Integer>>(nNodes);
77 76
         for (int i = 0; i < nNodes; ++i) {
78 77
             res.add(new HashSet<Integer>());
79 78
         }
80 79
 
81
-        for (Node node: getInputData().getGraph().getNodes()) {
82
-            for (Arc arc: node.getSuccessors()) {
80
+        for (Node node: getInputData().getGraph()) {
81
+            for (Arc arc: node) {
83 82
                 res.get(node.getId()).add(arc.getDestination().getId());
84 83
                 if (arc.getRoadInformation().isOneWay()) {
85 84
                     res.get(arc.getDestination().getId()).add(node.getId());
@@ -100,27 +99,27 @@ public class WeaklyConnectedComponentsAlgorithm
100 99
      * @return
101 100
      */
102 101
     protected ArrayList<Node> bfs(ArrayList<HashSet<Integer>> ugraph, boolean[] marked, int cur) {
103
-        List<Node> nodes = getInputData().getGraph().getNodes();
102
+        Graph graph = getInputData().getGraph();
104 103
         ArrayList<Node> component = new ArrayList<Node>();
105 104
 
106 105
         // Using a queue because we are doing a BFS
107 106
         Queue<Integer> queue = new LinkedList<Integer>();
108 107
 
109 108
         // Notify observers about the current component.
110
-        notifyStartComponent(nodes.get(cur));
109
+        notifyStartComponent(graph.get(cur));
111 110
 
112 111
         // Add original node and loop until the queue is empty.
113 112
         queue.add(cur);
114 113
         marked[cur] = true;
115 114
         while (!queue.isEmpty()) {
116
-            Node node = nodes.get(queue.remove());
115
+            Node node = graph.get(queue.remove());
117 116
             component.add(node);
118 117
 
119 118
             // Notify observers
120 119
             notifyNewNodeInComponent(node);
121 120
 
122 121
             for (Integer destId: ugraph.get(node.getId())) {
123
-                Node dest = nodes.get(destId);
122
+                Node dest = graph.get(destId);
124 123
                 if (!marked[dest.getId()]) {
125 124
                     queue.add(destId);
126 125
                     marked[destId] = true;
@@ -138,7 +137,7 @@ public class WeaklyConnectedComponentsAlgorithm
138 137
 
139 138
         Graph graph = getInputData().getGraph();
140 139
         ArrayList<HashSet<Integer>> ugraph = createUndirectedGraph();
141
-        boolean[] marked = new boolean[graph.getNodes().size()];
140
+        boolean[] marked = new boolean[graph.size()];
142 141
         Arrays.fill(marked, false);
143 142
 
144 143
         ArrayList<ArrayList<Node>> components = new ArrayList<ArrayList<Node>>();

+ 24
- 6
src/main/org/insa/graph/Graph.java View File

@@ -1,7 +1,7 @@
1 1
 package org.insa.graph;
2 2
 
3 3
 import java.util.ArrayList;
4
-import java.util.Collections;
4
+import java.util.Iterator;
5 5
 import java.util.List;
6 6
 
7 7
 /**
@@ -11,7 +11,7 @@ import java.util.List;
11 11
  * holds a list of nodes and each node holds a list of its successors.
12 12
  *
13 13
  */
14
-public class Graph {
14
+public class Graph implements Iterable<Node> {
15 15
 
16 16
     // Map identifier.
17 17
     private final String mapId;
@@ -48,10 +48,28 @@ public class Graph {
48 48
     }
49 49
 
50 50
     /**
51
-     * @return Immutable view of the list of nodes of this graph.
51
+     * Fetch the node with the given ID.
52
+     * 
53
+     * Complexity: O(1).
54
+     * 
55
+     * @param id ID of the node to fetch.
56
+     * 
57
+     * @return Node with the given ID.
52 58
      */
53
-    public List<Node> getNodes() {
54
-        return Collections.unmodifiableList(nodes);
59
+    public Node get(int id) {
60
+        return this.nodes.get(id);
61
+    }
62
+
63
+    /**
64
+     * @return Number of nodes in this graph.
65
+     */
66
+    public int size() {
67
+        return this.nodes.size();
68
+    }
69
+
70
+    @Override
71
+    public Iterator<Node> iterator() {
72
+        return this.nodes.iterator();
55 73
     }
56 74
 
57 75
     /**
@@ -78,7 +96,7 @@ public class Graph {
78 96
         }
79 97
         for (Node node: nodes) {
80 98
             Node orig = trNodes.get(node.getId());
81
-            for (Arc arc: node.getSuccessors()) {
99
+            for (Arc arc: node) {
82 100
                 if (arc.getRoadInformation().isOneWay()) {
83 101
                     Node dest = trNodes.get(arc.getDestination().getId());
84 102
                     dest.addSuccessor(new ArcBackward(new ArcForward(orig, dest, arc.getLength(),

+ 17
- 5
src/main/org/insa/graph/Node.java View File

@@ -2,7 +2,7 @@ package org.insa.graph;
2 2
 
3 3
 import java.util.ArrayList;
4 4
 import java.util.Collections;
5
-import java.util.List;
5
+import java.util.Iterator;
6 6
 
7 7
 /**
8 8
  * Class representing a Node in a {@link Graph}.
@@ -13,7 +13,7 @@ import java.util.List;
13 13
  * Nodes are comparable based on their ID.
14 14
  *
15 15
  */
16
-public class Node implements Comparable<Node> {
16
+public class Node implements Comparable<Node>, Iterable<Arc> {
17 17
 
18 18
     /**
19 19
      * Link the two given nodes with one or two arcs (depending on roadInformation),
@@ -94,10 +94,22 @@ public class Node implements Comparable<Node> {
94 94
     }
95 95
 
96 96
     /**
97
-     * @return Immutable view of the list of successors of this node.
97
+     * @return Number of successors of this node.
98 98
      */
99
-    public List<Arc> getSuccessors() {
100
-        return Collections.unmodifiableList(successors);
99
+    public int getNumberOfSuccessors() {
100
+        return this.successors.size();
101
+    }
102
+
103
+    /**
104
+     * @return true if this node has at least one successor.
105
+     */
106
+    public boolean hasSuccessors() {
107
+        return !this.successors.isEmpty();
108
+    }
109
+
110
+    @Override
111
+    public Iterator<Arc> iterator() {
112
+        return this.successors.iterator();
101 113
     }
102 114
 
103 115
     /**

+ 1
- 1
src/main/org/insa/graph/io/BinaryPathReader.java View File

@@ -70,7 +70,7 @@ public class BinaryPathReader extends BinaryReader implements PathReader {
70 70
      * @throws IndexOutOfBoundsException if the node is not in the graph.
71 71
      */
72 72
     protected Node readNode(Graph graph) throws IOException {
73
-        return graph.getNodes().get(dis.readInt());
73
+        return graph.get(dis.readInt());
74 74
     }
75 75
 
76 76
 }

+ 2
- 2
src/main/org/insa/graphics/MainWindow.java View File

@@ -602,8 +602,8 @@ public class MainWindow extends JFrame {
602 602
                     // name that are right-to-left (e.g. arabic names).
603 603
                     info += " - " + graph.getMapName() + "\u200e";
604 604
                 }
605
-                info += ", " + graph.getNodes().size() + " nodes, "
606
-                        + graph.getGraphInformation().getArcCount() + " arcs.";
605
+                info += ", " + graph.size() + " nodes, " + graph.getGraphInformation().getArcCount()
606
+                        + " arcs.";
607 607
                 graphInfoPanel.setText(info);
608 608
 
609 609
                 drawGraph();

+ 2
- 2
src/main/org/insa/graphics/NodesInputPanel.java View File

@@ -65,7 +65,7 @@ public class NodesInputPanel extends JPanel
65 65
         public Node findClosestNode(Point point) {
66 66
             Node minNode = null;
67 67
             double minDis = Double.POSITIVE_INFINITY;
68
-            for (Node node: graph.getNodes()) {
68
+            for (Node node: graph) {
69 69
                 double dlon = point.getLongitude() - node.getPoint().getLongitude();
70 70
                 double dlat = point.getLatitude() - node.getPoint().getLatitude();
71 71
                 double dis = dlon * dlon + dlat * dlat; // No need to square
@@ -308,7 +308,7 @@ public class NodesInputPanel extends JPanel
308 308
      */
309 309
     protected Node getNodeForInput(JTextField textfield) {
310 310
         try {
311
-            Node node = graph.getNodes().get(Integer.valueOf(textfield.getText().trim()));
311
+            Node node = graph.get(Integer.valueOf(textfield.getText().trim()));
312 312
             return node;
313 313
         }
314 314
         catch (IllegalArgumentException | IndexOutOfBoundsException ex) {

+ 3
- 3
src/main/org/insa/graphics/drawing/components/BasicDrawing.java View File

@@ -614,7 +614,7 @@ public class BasicDrawing extends JPanel implements Drawing {
614 614
 
615 615
     @Override
616 616
     public void drawGraph(Graph graph, GraphPalette palette) {
617
-        int repaintModulo = Math.max(1, graph.getNodes().size() / 100);
617
+        int repaintModulo = Math.max(1, graph.size() / 100);
618 618
 
619 619
         // Initialize the buffered image
620 620
 
@@ -625,8 +625,8 @@ public class BasicDrawing extends JPanel implements Drawing {
625 625
         this.removeMouseMotionListener(zoomAndPanListener);
626 626
         this.removeMouseWheelListener(zoomAndPanListener);
627 627
 
628
-        for (Node node: graph.getNodes()) {
629
-            for (Arc arc: node.getSuccessors()) {
628
+        for (Node node: graph) {
629
+            for (Arc arc: node) {
630 630
                 // Draw arcs only if there are one-way arcs or if origin is lower than
631 631
                 // destination, avoid drawing two-ways arc twice.
632 632
                 if (arc.getRoadInformation().isOneWay()

+ 67
- 21
src/test/org/insa/algo/utils/BinaryHeapTest.java View File

@@ -11,29 +11,68 @@ import org.junit.Test;
11 11
 
12 12
 public class BinaryHeapTest {
13 13
 
14
-    private int[] data1 = IntStream.range(0, 20).toArray();
15
-    private int[] data2 = { 8, 1, 6, 3, 4, 5, 9 };
14
+    class MutableInteger implements Comparable<MutableInteger> {
16 15
 
17
-    private BinaryHeap<Integer> heap1, heap2;
16
+        // Actual value
17
+        private int value;
18
+
19
+        public MutableInteger(int value) {
20
+            this.value = value;
21
+        }
22
+
23
+        /**
24
+         * @return The integer value stored inside this MutableInteger.
25
+         */
26
+        public int get() {
27
+            return this.value;
28
+        }
29
+
30
+        /**
31
+         * Update the integer value stored inside this MutableInteger.
32
+         * 
33
+         * @param value New value to set.
34
+         */
35
+        public void set(int value) {
36
+            this.value = value;
37
+        }
38
+
39
+        @Override
40
+        public int compareTo(MutableInteger other) {
41
+            return Integer.compare(this.value, other.value);
42
+        }
43
+
44
+    };
45
+
46
+    // Raw data arrays.
47
+    private MutableInteger[] data1 = IntStream.range(0, 20).mapToObj(MutableInteger::new)
48
+            .toArray(MutableInteger[]::new);
49
+    private MutableInteger[] data2 = Arrays.stream(new int[] { 8, 1, 6, 3, 4, 5, 9 })
50
+            .mapToObj(MutableInteger::new).toArray(MutableInteger[]::new);
51
+
52
+    // Actual heap.
53
+    private BinaryHeap<MutableInteger> heap1, heap2;
18 54
 
19 55
     @Before
20 56
     public void init() {
21 57
         // Create the range heap
22
-        this.heap1 = new BinaryHeap<Integer>();
23
-        this.heap2 = new BinaryHeap<Integer>();
58
+        this.heap1 = new BinaryHeap<>();
59
+        this.heap2 = new BinaryHeap<>();
60
+
61
+        for (MutableInteger v: data1) {
62
+            this.heap1.add(v);
63
+        }
24 64
 
25
-        for (int v: data1)
26
-            this.heap1.insert(v);
27
-        for (int v: data2)
28
-            this.heap2.insert(v);
65
+        for (MutableInteger v: data2) {
66
+            this.heap2.add(v);
67
+        }
29 68
     }
30 69
 
31 70
     @Test
32 71
     public void testInsert() {
33
-        BinaryHeap<Integer> heap = new BinaryHeap<Integer>();
72
+        BinaryHeap<MutableInteger> heap = new BinaryHeap<>();
34 73
         int size = 0;
35
-        for (int x: data1) {
36
-            heap.insert(x);
74
+        for (MutableInteger x: data1) {
75
+            heap.add(x);
37 76
             size += 1;
38 77
             assertEquals(heap.size(), size);
39 78
         }
@@ -41,8 +80,8 @@ public class BinaryHeapTest {
41 80
 
42 81
         heap = new BinaryHeap<>();
43 82
         size = 0;
44
-        for (int x: data2) {
45
-            heap.insert(x);
83
+        for (MutableInteger x: data2) {
84
+            heap.add(x);
46 85
             size += 1;
47 86
             assertEquals(heap.size(), size);
48 87
         }
@@ -52,11 +91,10 @@ public class BinaryHeapTest {
52 91
     @Test
53 92
     public void testDeleteMin() {
54 93
         // range 1 (sorted)
55
-        int[] range1 = data1;
56
-        int size = range1.length;
94
+        int size = data1.length;
57 95
         assertEquals(heap1.size(), size);
58
-        for (int x: range1) {
59
-            assertEquals(heap1.deleteMin().intValue(), x);
96
+        for (MutableInteger x: data1) {
97
+            assertEquals(heap1.deleteMin(), x);
60 98
             size -= 1;
61 99
             assertEquals(heap1.size(), size);
62 100
         }
@@ -64,12 +102,12 @@ public class BinaryHeapTest {
64 102
         assertTrue(heap1.isEmpty());
65 103
 
66 104
         // range 2 (was not sorted)
67
-        int[] range2 = Arrays.copyOf(data2, data2.length);
105
+        MutableInteger[] range2 = Arrays.copyOf(data2, data2.length);
68 106
         Arrays.sort(range2);
69 107
         size = range2.length;
70 108
         assertEquals(heap2.size(), size);
71
-        for (int x: range2) {
72
-            assertEquals(heap2.deleteMin().intValue(), x);
109
+        for (MutableInteger x: range2) {
110
+            assertEquals(heap2.deleteMin().get(), x.get());
73 111
             size -= 1;
74 112
             assertEquals(heap2.size(), size);
75 113
         }
@@ -77,4 +115,12 @@ public class BinaryHeapTest {
77 115
         assertTrue(heap2.isEmpty());
78 116
     }
79 117
 
118
+    @Test
119
+    public void testUpdate() {
120
+        MutableInteger newMin = data2[data2.length - 1];
121
+        newMin.set(0);
122
+        heap2.update(newMin);
123
+        assertEquals(heap2.findMin(), newMin);
124
+    }
125
+
80 126
 }

+ 23
- 43
src/test/org/insa/graph/GraphTest.java View File

@@ -62,7 +62,7 @@ public class GraphTest {
62 62
      */
63 63
     private List<Arc> getArcsBetween(Node a, Node b) {
64 64
         List<Arc> arcs = new ArrayList<>();
65
-        for (Arc arc: a.getSuccessors()) {
65
+        for (Arc arc: a) {
66 66
             if (arc.getDestination().equals(b)) {
67 67
                 arcs.add(arc);
68 68
             }
@@ -76,53 +76,33 @@ public class GraphTest {
76 76
 
77 77
         // Basic asserts...
78 78
         assertEquals("R/" + graph.getMapId(), transpose.getMapId());
79
-        assertEquals(graph.getNodes().size(), transpose.getNodes().size());
79
+        assertEquals(graph.size(), transpose.size());
80 80
 
81 81
         final int expNbSucc[] = { 4, 2, 2, 4, 2 };
82 82
         for (int i = 0; i < expNbSucc.length; ++i) {
83
-            assertEquals(expNbSucc[i], transpose.getNodes().get(i).getSuccessors().size());
83
+            assertEquals(expNbSucc[i], transpose.get(i).getNumberOfSuccessors());
84 84
         }
85 85
 
86
-        assertEquals(
87
-                getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(1)).size(), 1);
88
-        assertEquals(
89
-                getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(2)).size(), 1);
90
-        assertEquals(
91
-                getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(3)).size(), 1);
92
-        assertEquals(
93
-                getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(4)).size(), 1);
94
-        assertEquals(
95
-                getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(0)).size(), 1);
96
-        assertEquals(
97
-                getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(2)).size(), 1);
98
-        assertEquals(
99
-                getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(3)).size(), 0);
100
-        assertEquals(
101
-                getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(4)).size(), 0);
102
-        assertEquals(
103
-                getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(0)).size(), 1);
104
-        assertEquals(
105
-                getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(1)).size(), 1);
106
-        assertEquals(
107
-                getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(3)).size(), 0);
108
-        assertEquals(
109
-                getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(4)).size(), 0);
110
-        assertEquals(
111
-                getArcsBetween(transpose.getNodes().get(3), transpose.getNodes().get(0)).size(), 1);
112
-        assertEquals(
113
-                getArcsBetween(transpose.getNodes().get(3), transpose.getNodes().get(1)).size(), 0);
114
-        assertEquals(
115
-                getArcsBetween(transpose.getNodes().get(3), transpose.getNodes().get(2)).size(), 3);
116
-        assertEquals(
117
-                getArcsBetween(transpose.getNodes().get(3), transpose.getNodes().get(4)).size(), 0);
118
-        assertEquals(
119
-                getArcsBetween(transpose.getNodes().get(4), transpose.getNodes().get(0)).size(), 1);
120
-        assertEquals(
121
-                getArcsBetween(transpose.getNodes().get(4), transpose.getNodes().get(1)).size(), 0);
122
-        assertEquals(
123
-                getArcsBetween(transpose.getNodes().get(4), transpose.getNodes().get(2)).size(), 0);
124
-        assertEquals(
125
-                getArcsBetween(transpose.getNodes().get(4), transpose.getNodes().get(3)).size(), 1);
86
+        assertEquals(getArcsBetween(transpose.get(0), transpose.get(1)).size(), 1);
87
+        assertEquals(getArcsBetween(transpose.get(0), transpose.get(2)).size(), 1);
88
+        assertEquals(getArcsBetween(transpose.get(0), transpose.get(3)).size(), 1);
89
+        assertEquals(getArcsBetween(transpose.get(0), transpose.get(4)).size(), 1);
90
+        assertEquals(getArcsBetween(transpose.get(1), transpose.get(0)).size(), 1);
91
+        assertEquals(getArcsBetween(transpose.get(1), transpose.get(2)).size(), 1);
92
+        assertEquals(getArcsBetween(transpose.get(1), transpose.get(3)).size(), 0);
93
+        assertEquals(getArcsBetween(transpose.get(1), transpose.get(4)).size(), 0);
94
+        assertEquals(getArcsBetween(transpose.get(2), transpose.get(0)).size(), 1);
95
+        assertEquals(getArcsBetween(transpose.get(2), transpose.get(1)).size(), 1);
96
+        assertEquals(getArcsBetween(transpose.get(2), transpose.get(3)).size(), 0);
97
+        assertEquals(getArcsBetween(transpose.get(2), transpose.get(4)).size(), 0);
98
+        assertEquals(getArcsBetween(transpose.get(3), transpose.get(0)).size(), 1);
99
+        assertEquals(getArcsBetween(transpose.get(3), transpose.get(1)).size(), 0);
100
+        assertEquals(getArcsBetween(transpose.get(3), transpose.get(2)).size(), 3);
101
+        assertEquals(getArcsBetween(transpose.get(3), transpose.get(4)).size(), 0);
102
+        assertEquals(getArcsBetween(transpose.get(4), transpose.get(0)).size(), 1);
103
+        assertEquals(getArcsBetween(transpose.get(4), transpose.get(1)).size(), 0);
104
+        assertEquals(getArcsBetween(transpose.get(4), transpose.get(2)).size(), 0);
105
+        assertEquals(getArcsBetween(transpose.get(4), transpose.get(3)).size(), 1);
126 106
 
127 107
     }
128 108
 }

+ 17
- 5
src/test/org/insa/graph/NodeTest.java View File

@@ -18,7 +18,7 @@ public class NodeTest {
18 18
     public void initAll() throws IOException {
19 19
 
20 20
         // Create nodes
21
-        nodes = new Node[5];
21
+        nodes = new Node[6];
22 22
         for (int i = 0; i < nodes.length; ++i) {
23 23
             nodes[i] = new Node(i, null);
24 24
         }
@@ -54,7 +54,7 @@ public class NodeTest {
54 54
      * @return The first arc between from a to b, or null.
55 55
      */
56 56
     private Arc getFirstArcBetween(Node a, Node b) {
57
-        for (Arc arc: a.getSuccessors()) {
57
+        for (Arc arc: a) {
58 58
             if (arc.getDestination().equals(b)) {
59 59
                 return arc;
60 60
             }
@@ -63,13 +63,25 @@ public class NodeTest {
63 63
     }
64 64
 
65 65
     @Test
66
-    public void testLinkNodes() {
67
-        final int[] expNbSucc = { 4, 2, 5, 2, 1 };
66
+    public void testGetNumberOfSuccessors() {
67
+        final int[] expNbSucc = { 4, 2, 5, 2, 1, 0 };
68
+        assertEquals(nodes.length, expNbSucc.length);
69
+        for (int i = 0; i < expNbSucc.length; ++i) {
70
+            assertEquals(nodes[i].getNumberOfSuccessors(), expNbSucc[i]);
71
+        }
72
+    }
73
+
74
+    @Test
75
+    public void testHasSuccessors() {
76
+        final int[] expNbSucc = { 4, 2, 5, 2, 1, 0 };
68 77
         assertEquals(nodes.length, expNbSucc.length);
69 78
         for (int i = 0; i < expNbSucc.length; ++i) {
70
-            assertEquals(nodes[i].getSuccessors().size(), expNbSucc[i]);
79
+            assertEquals(nodes[i].hasSuccessors(), expNbSucc[i] != 0);
71 80
         }
81
+    }
72 82
 
83
+    @Test
84
+    public void testLinkNodes() {
73 85
         assertEquals(getFirstArcBetween(nodes[0], nodes[1]).getRoadInformation(),
74 86
                 getFirstArcBetween(nodes[1], nodes[0]).getRoadInformation());
75 87
     }

Loading…
Cancel
Save