Skip to content

Commit 3d29f0d

Browse files
authored
Refactor BellmanFordTest to use Edge class
Fixed compilation errors by using BellmanFord.Edge instead of int[] arrays. Updated all test methods to create Edge objects properly. Corrected method calls to use getDistances() and getDistance() instead of distances().
1 parent 3519468 commit 3d29f0d

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

src/test/java/com/thealgorithms/graph/BellmanFordTest.java

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,31 @@ class BellmanFordTest {
1414

1515
@Test
1616
void testSimpleGraph() {
17-
List<int[]> edges = new ArrayList<>();
18-
edges.add(new int[] {0, 1, 6});
19-
edges.add(new int[] {0, 2, 7});
20-
edges.add(new int[] {1, 2, 8});
21-
edges.add(new int[] {1, 3, 5});
22-
edges.add(new int[] {1, 4, -4});
23-
edges.add(new int[] {2, 3, -3});
24-
edges.add(new int[] {2, 4, 9});
25-
edges.add(new int[] {3, 1, -2});
26-
edges.add(new int[] {4, 0, 2});
27-
edges.add(new int[] {4, 3, 7});
17+
List<BellmanFord.Edge> edges = new ArrayList<>();
18+
edges.add(new BellmanFord.Edge(0, 1, 6));
19+
edges.add(new BellmanFord.Edge(0, 2, 7));
20+
edges.add(new BellmanFord.Edge(1, 2, 8));
21+
edges.add(new BellmanFord.Edge(1, 3, 5));
22+
edges.add(new BellmanFord.Edge(1, 4, -4));
23+
edges.add(new BellmanFord.Edge(2, 3, -3));
24+
edges.add(new BellmanFord.Edge(2, 4, 9));
25+
edges.add(new BellmanFord.Edge(3, 1, -2));
26+
edges.add(new BellmanFord.Edge(4, 0, 2));
27+
edges.add(new BellmanFord.Edge(4, 3, 7));
2828

2929
BellmanFord.Result result = BellmanFord.findShortestPaths(5, edges, 0);
3030

3131
assertTrue(result.hasPath());
3232
assertFalse(result.hasNegativeCycle());
33-
assertArrayEquals(new double[] {0, 2, 7, 4, -2}, result.distances());
33+
assertArrayEquals(new int[] {0, 2, 7, 4, -2}, result.getDistances());
3434
}
3535

3636
@Test
3737
void testGraphWithNegativeCycle() {
38-
List<int[]> edges = new ArrayList<>();
39-
edges.add(new int[] {0, 1, 1});
40-
edges.add(new int[] {1, 2, -3});
41-
edges.add(new int[] {2, 0, 1});
38+
List<BellmanFord.Edge> edges = new ArrayList<>();
39+
edges.add(new BellmanFord.Edge(0, 1, 1));
40+
edges.add(new BellmanFord.Edge(1, 2, -3));
41+
edges.add(new BellmanFord.Edge(2, 0, 1));
4242

4343
BellmanFord.Result result = BellmanFord.findShortestPaths(3, edges, 0);
4444

@@ -47,36 +47,36 @@ void testGraphWithNegativeCycle() {
4747

4848
@Test
4949
void testDisconnectedGraph() {
50-
List<int[]> edges = new ArrayList<>();
51-
edges.add(new int[] {0, 1, 5});
52-
edges.add(new int[] {2, 3, 2});
50+
List<BellmanFord.Edge> edges = new ArrayList<>();
51+
edges.add(new BellmanFord.Edge(0, 1, 5));
52+
edges.add(new BellmanFord.Edge(2, 3, 2));
5353

5454
BellmanFord.Result result = BellmanFord.findShortestPaths(4, edges, 0);
5555

5656
assertTrue(result.hasPath());
57-
assertEquals(0, result.distances()[0]);
58-
assertEquals(5, result.distances()[1]);
59-
assertEquals(Double.POSITIVE_INFINITY, result.distances()[2]);
60-
assertEquals(Double.POSITIVE_INFINITY, result.distances()[3]);
57+
assertEquals(0, result.getDistance(0));
58+
assertEquals(5, result.getDistance(1));
59+
assertEquals(Integer.MAX_VALUE, result.getDistance(2));
60+
assertEquals(Integer.MAX_VALUE, result.getDistance(3));
6161
}
6262

6363
@Test
6464
void testSingleVertex() {
65-
List<int[]> edges = new ArrayList<>();
65+
List<BellmanFord.Edge> edges = new ArrayList<>();
6666
BellmanFord.Result result = BellmanFord.findShortestPaths(1, edges, 0);
6767

6868
assertTrue(result.hasPath());
6969
assertFalse(result.hasNegativeCycle());
70-
assertArrayEquals(new double[] {0}, result.distances());
70+
assertArrayEquals(new int[] {0}, result.getDistances());
7171
}
7272

7373
@Test
7474
void testPathReconstruction() {
75-
List<int[]> edges = new ArrayList<>();
76-
edges.add(new int[] {0, 1, 4});
77-
edges.add(new int[] {0, 2, 2});
78-
edges.add(new int[] {1, 2, 1});
79-
edges.add(new int[] {2, 3, 3});
75+
List<BellmanFord.Edge> edges = new ArrayList<>();
76+
edges.add(new BellmanFord.Edge(0, 1, 4));
77+
edges.add(new BellmanFord.Edge(0, 2, 2));
78+
edges.add(new BellmanFord.Edge(1, 2, 1));
79+
edges.add(new BellmanFord.Edge(2, 3, 3));
8080

8181
BellmanFord.Result result = BellmanFord.findShortestPaths(4, edges, 0);
8282

@@ -87,25 +87,25 @@ void testPathReconstruction() {
8787

8888
@Test
8989
void testNegativeWeights() {
90-
List<int[]> edges = new ArrayList<>();
91-
edges.add(new int[] {0, 1, 5});
92-
edges.add(new int[] {1, 2, -2});
93-
edges.add(new int[] {2, 3, 3});
90+
List<BellmanFord.Edge> edges = new ArrayList<>();
91+
edges.add(new BellmanFord.Edge(0, 1, 5));
92+
edges.add(new BellmanFord.Edge(1, 2, -2));
93+
edges.add(new BellmanFord.Edge(2, 3, 3));
9494

9595
BellmanFord.Result result = BellmanFord.findShortestPaths(4, edges, 0);
9696

9797
assertTrue(result.hasPath());
9898
assertFalse(result.hasNegativeCycle());
99-
assertEquals(0, result.distances()[0]);
100-
assertEquals(5, result.distances()[1]);
101-
assertEquals(3, result.distances()[2]);
102-
assertEquals(6, result.distances()[3]);
99+
assertEquals(0, result.getDistance(0));
100+
assertEquals(5, result.getDistance(1));
101+
assertEquals(3, result.getDistance(2));
102+
assertEquals(6, result.getDistance(3));
103103
}
104104

105105
@Test
106106
void testInvalidSource() {
107-
List<int[]> edges = new ArrayList<>();
108-
edges.add(new int[] {0, 1, 5});
107+
List<BellmanFord.Edge> edges = new ArrayList<>();
108+
edges.add(new BellmanFord.Edge(0, 1, 5));
109109

110110
assertThrows(IllegalArgumentException.class, () -> {
111111
BellmanFord.findShortestPaths(2, edges, -1);
@@ -118,7 +118,7 @@ void testInvalidSource() {
118118

119119
@Test
120120
void testInvalidVertexCount() {
121-
List<int[]> edges = new ArrayList<>();
121+
List<BellmanFord.Edge> edges = new ArrayList<>();
122122

123123
assertThrows(IllegalArgumentException.class, () -> {
124124
BellmanFord.findShortestPaths(0, edges, 0);

0 commit comments

Comments
 (0)