Skip to content

Commit 86b7848

Browse files
Merge pull request #1 from prashantdubeypng
fix(tests): replace List.of() with Arrays.asList() for Java 8 compatibility
2 parents a8bc9cc + 25c253f commit 86b7848

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package com.thealgorithms.datastructures.graphs;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.List;
10+
import java.util.Map;
11+
import java.util.Arrays;
12+
import org.junit.jupiter.api.Test;
13+
14+
/**
15+
* Tests for {@link BreadthFirstSearch}.
16+
*/
17+
class BreadthFirstSearchTest {
18+
19+
@Test
20+
void testBfsWithIntegerGraphSimple() {
21+
// Create a simple graph: 0 -> 1 -> 2
22+
// | |
23+
// v v
24+
// 3 4
25+
int[][] edges = { { 0, 1 }, { 0, 3 }, { 1, 2 }, { 1, 4 } };
26+
List<List<Integer>> graph = BreadthFirstSearch.createGraph(5, edges, false);
27+
28+
List<Integer> result = BreadthFirstSearch.bfs(graph, 5, 0);
29+
30+
assertEquals(5, result.size());
31+
assertEquals(0, result.get(0)); // Source is first
32+
// BFS visits by level: 0 -> [1, 3] -> [2, 4]
33+
assertTrue(result.indexOf(1) < result.indexOf(2)); // 1 before 2
34+
assertTrue(result.indexOf(1) < result.indexOf(4)); // 1 before 4
35+
}
36+
37+
@Test
38+
void testBfsWithUndirectedGraph() {
39+
// Create an undirected graph
40+
int[][] edges = { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 } };
41+
List<List<Integer>> graph = BreadthFirstSearch.createGraph(5, edges, true);
42+
43+
List<Integer> result = BreadthFirstSearch.bfs(graph, 5, 2);
44+
45+
assertEquals(5, result.size());
46+
assertEquals(2, result.get(0)); // Source is first
47+
}
48+
49+
@Test
50+
void testBfsWithDisconnectedGraph() {
51+
// Graph with disconnected components: 0 -> 1, 2 -> 3 (separate components)
52+
int[][] edges = { { 0, 1 }, { 2, 3 } };
53+
List<List<Integer>> graph = BreadthFirstSearch.createGraph(4, edges, false);
54+
55+
List<Integer> result = BreadthFirstSearch.bfs(graph, 4, 0);
56+
57+
// Should only visit reachable vertices from 0
58+
assertEquals(2, result.size());
59+
assertTrue(result.contains(0));
60+
assertTrue(result.contains(1));
61+
}
62+
63+
@Test
64+
void testBfsWithSingleVertex() {
65+
int[][] edges = {};
66+
List<List<Integer>> graph = BreadthFirstSearch.createGraph(1, edges, false);
67+
68+
List<Integer> result = BreadthFirstSearch.bfs(graph, 1, 0);
69+
70+
assertEquals(1, result.size());
71+
assertEquals(0, result.get(0));
72+
}
73+
74+
@Test
75+
void testBfsWithCyclicGraph() {
76+
// Graph with a cycle: 0 -> 1 -> 2 -> 0
77+
int[][] edges = { { 0, 1 }, { 1, 2 }, { 2, 0 } };
78+
List<List<Integer>> graph = BreadthFirstSearch.createGraph(3, edges, false);
79+
80+
List<Integer> result = BreadthFirstSearch.bfs(graph, 3, 0);
81+
82+
assertEquals(3, result.size());
83+
assertEquals(0, result.get(0)); // Source is first
84+
}
85+
86+
@Test
87+
void testBfsWithMapBasedGraph() {
88+
Map<String, List<String>> graph = new HashMap<>();
89+
graph.put("A", Arrays.asList("B", "C"));
90+
graph.put("B", Arrays.asList("D"));
91+
graph.put("C", Arrays.asList("E"));
92+
graph.put("D", new ArrayList<>());
93+
graph.put("E", new ArrayList<>());
94+
95+
List<String> result = BreadthFirstSearch.bfs(graph, "A");
96+
97+
assertEquals(5, result.size());
98+
assertEquals("A", result.get(0)); // Source is first
99+
// B and C should come before D and E
100+
assertTrue(result.indexOf("B") < result.indexOf("D"));
101+
assertTrue(result.indexOf("C") < result.indexOf("E"));
102+
}
103+
104+
@Test
105+
void testBfsWithStringGraphCreation() {
106+
String[][] edges = { { "DELHI", "MUMBAI" }, { "DELHI", "PUNE" }, { "MUMBAI", "GOA" } };
107+
Map<String, List<String>> graph = BreadthFirstSearch.createStringGraph(edges, true);
108+
109+
List<String> result = BreadthFirstSearch.bfs(graph, "DELHI");
110+
111+
assertEquals(4, result.size());
112+
assertEquals("DELHI", result.get(0));
113+
}
114+
115+
@Test
116+
void testBfsThrowsExceptionForNullSource() {
117+
Map<String, List<String>> graph = new HashMap<>();
118+
graph.put("A", Arrays.asList("B"));
119+
120+
assertThrows(IllegalArgumentException.class, () -> BreadthFirstSearch.bfs(graph, null));
121+
}
122+
123+
@Test
124+
void testBfsThrowsExceptionForSourceNotInGraph() {
125+
Map<String, List<String>> graph = new HashMap<>();
126+
graph.put("A", Arrays.asList("B"));
127+
128+
assertThrows(IllegalArgumentException.class, () -> BreadthFirstSearch.bfs(graph, "X"));
129+
}
130+
131+
@Test
132+
void testBfsThrowsExceptionForInvalidSourceIndex() {
133+
List<List<Integer>> graph = BreadthFirstSearch.createGraph(3, new int[][] {}, false);
134+
135+
assertThrows(IllegalArgumentException.class, () -> BreadthFirstSearch.bfs(graph, 3, -1));
136+
assertThrows(IllegalArgumentException.class, () -> BreadthFirstSearch.bfs(graph, 3, 5));
137+
}
138+
139+
@Test
140+
void testBfsLevelOrderProperty() {
141+
// Test that BFS visits vertices level by level
142+
// 0
143+
// /|\
144+
// 1 2 3
145+
// | |
146+
// 4 5
147+
int[][] edges = { { 0, 1 }, { 0, 2 }, { 0, 3 }, { 1, 4 }, { 3, 5 } };
148+
List<List<Integer>> graph = BreadthFirstSearch.createGraph(6, edges, false);
149+
150+
List<Integer> result = BreadthFirstSearch.bfs(graph, 6, 0);
151+
152+
// Level 0: 0
153+
// Level 1: 1, 2, 3
154+
// Level 2: 4, 5
155+
assertEquals(0, result.get(0));
156+
// All level-1 vertices come before level-2 vertices
157+
int level1End = Math.max(result.indexOf(1), Math.max(result.indexOf(2), result.indexOf(3)));
158+
int level2Start = Math.min(result.indexOf(4), result.indexOf(5));
159+
assertTrue(level1End < level2Start);
160+
}
161+
162+
@Test
163+
void testBfsWithEmptyNeighborList() {
164+
Map<String, List<String>> graph = new HashMap<>();
165+
graph.put("A", new ArrayList<>());
166+
167+
List<String> result = BreadthFirstSearch.bfs(graph, "A");
168+
169+
assertEquals(1, result.size());
170+
assertEquals("A", result.get(0));
171+
}
172+
}

0 commit comments

Comments
 (0)