Skip to content

Commit 3519468

Browse files
authored
test: Add comprehensive tests for BellmanFord algorithm
This test class verifies the functionality of the Bellman-Ford algorithm with various scenarios including simple graphs, negative cycles, disconnected graphs, and invalid inputs.
1 parent 9d94f93 commit 3519468

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.thealgorithms.graph;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
/**
11+
* Test class for Bellman-Ford algorithm implementation
12+
*/
13+
class BellmanFordTest {
14+
15+
@Test
16+
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});
28+
29+
BellmanFord.Result result = BellmanFord.findShortestPaths(5, edges, 0);
30+
31+
assertTrue(result.hasPath());
32+
assertFalse(result.hasNegativeCycle());
33+
assertArrayEquals(new double[] {0, 2, 7, 4, -2}, result.distances());
34+
}
35+
36+
@Test
37+
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});
42+
43+
BellmanFord.Result result = BellmanFord.findShortestPaths(3, edges, 0);
44+
45+
assertTrue(result.hasNegativeCycle());
46+
}
47+
48+
@Test
49+
void testDisconnectedGraph() {
50+
List<int[]> edges = new ArrayList<>();
51+
edges.add(new int[] {0, 1, 5});
52+
edges.add(new int[] {2, 3, 2});
53+
54+
BellmanFord.Result result = BellmanFord.findShortestPaths(4, edges, 0);
55+
56+
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]);
61+
}
62+
63+
@Test
64+
void testSingleVertex() {
65+
List<int[]> edges = new ArrayList<>();
66+
BellmanFord.Result result = BellmanFord.findShortestPaths(1, edges, 0);
67+
68+
assertTrue(result.hasPath());
69+
assertFalse(result.hasNegativeCycle());
70+
assertArrayEquals(new double[] {0}, result.distances());
71+
}
72+
73+
@Test
74+
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});
80+
81+
BellmanFord.Result result = BellmanFord.findShortestPaths(4, edges, 0);
82+
83+
List<Integer> path = result.getPath(3);
84+
assertNotNull(path);
85+
assertEquals(Arrays.asList(0, 2, 3), path);
86+
}
87+
88+
@Test
89+
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});
94+
95+
BellmanFord.Result result = BellmanFord.findShortestPaths(4, edges, 0);
96+
97+
assertTrue(result.hasPath());
98+
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]);
103+
}
104+
105+
@Test
106+
void testInvalidSource() {
107+
List<int[]> edges = new ArrayList<>();
108+
edges.add(new int[] {0, 1, 5});
109+
110+
assertThrows(IllegalArgumentException.class, () -> {
111+
BellmanFord.findShortestPaths(2, edges, -1);
112+
});
113+
114+
assertThrows(IllegalArgumentException.class, () -> {
115+
BellmanFord.findShortestPaths(2, edges, 2);
116+
});
117+
}
118+
119+
@Test
120+
void testInvalidVertexCount() {
121+
List<int[]> edges = new ArrayList<>();
122+
123+
assertThrows(IllegalArgumentException.class, () -> {
124+
BellmanFord.findShortestPaths(0, edges, 0);
125+
});
126+
127+
assertThrows(IllegalArgumentException.class, () -> {
128+
BellmanFord.findShortestPaths(-1, edges, 0);
129+
});
130+
}
131+
132+
@Test
133+
void testNullEdges() {
134+
assertThrows(IllegalArgumentException.class, () -> {
135+
BellmanFord.findShortestPaths(3, null, 0);
136+
});
137+
}
138+
}

0 commit comments

Comments
 (0)