Skip to content

Commit 423cc3a

Browse files
Update BridgesTest.java
1 parent 3fb9a74 commit 423cc3a

1 file changed

Lines changed: 106 additions & 5 deletions

File tree

‎src/test/java/com/thealgorithms/graph/BridgesTest.java‎

Lines changed: 106 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ void testEmptyGraph() {
4848
assertEquals(0, result.size());
4949
}
5050

51+
@Test
52+
void testZeroVertices() {
53+
List<int[]> result = Bridges.findBridges(0, new int[0][]);
54+
55+
assertEquals(0, result.size());
56+
}
57+
5158
@Test
5259
void testParallelEdgesAreNotBridges() {
5360
int[][] edges = {{0, 1}, {0, 1}};
@@ -66,27 +73,121 @@ void testSelfLoopIsNotBridge() {
6673
assertEquals(0, result.size());
6774
}
6875

76+
@Test
77+
void testSingleEdgeIsBridge() {
78+
int[][] edges = {{0, 1}};
79+
80+
List<int[]> result = Bridges.findBridges(2, edges);
81+
82+
assertEquals(1, result.size());
83+
assertArrayEquals(new int[] {0, 1}, result.get(0));
84+
}
85+
86+
@Test
87+
void testCycleWithTail() {
88+
int[][] edges = {{0, 1}, {1, 2}, {2, 0}, {2, 3}};
89+
90+
List<int[]> result = Bridges.findBridges(4, edges);
91+
92+
assertEquals(1, result.size());
93+
assertArrayEquals(new int[] {2, 3}, result.get(0));
94+
}
95+
6996
@Test
7097
void testNegativeVertexCountThrowsException() {
71-
assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(-1, new int[0][]));
98+
assertThrows(
99+
IllegalArgumentException.class,
100+
() -> Bridges.findBridges(-1, new int[0][])
101+
);
72102
}
73103

74104
@Test
75105
void testNullEdgesThrowsException() {
76-
assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, null));
106+
assertThrows(
107+
IllegalArgumentException.class,
108+
() -> Bridges.findBridges(2, null)
109+
);
77110
}
78111

79112
@Test
80113
void testMalformedEdgeThrowsException() {
81114
int[][] edges = {{0}};
82115

83-
assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, edges));
116+
assertThrows(
117+
IllegalArgumentException.class,
118+
() -> Bridges.findBridges(2, edges)
119+
);
120+
}
121+
122+
@Test
123+
void testEdgeWithTooManyVerticesThrowsException() {
124+
int[][] edges = {{0, 1, 2}};
125+
126+
assertThrows(
127+
IllegalArgumentException.class,
128+
() -> Bridges.findBridges(3, edges)
129+
);
130+
}
131+
132+
@Test
133+
void testNullEdgeThrowsException() {
134+
int[][] edges = {null};
135+
136+
assertThrows(
137+
IllegalArgumentException.class,
138+
() -> Bridges.findBridges(2, edges)
139+
);
140+
}
141+
142+
@Test
143+
void testNegativeFirstVertexThrowsException() {
144+
int[][] edges = {{-1, 1}};
145+
146+
assertThrows(
147+
IllegalArgumentException.class,
148+
() -> Bridges.findBridges(2, edges)
149+
);
150+
}
151+
152+
@Test
153+
void testNegativeSecondVertexThrowsException() {
154+
int[][] edges = {{0, -1}};
155+
156+
assertThrows(
157+
IllegalArgumentException.class,
158+
() -> Bridges.findBridges(2, edges)
159+
);
84160
}
85161

86162
@Test
87-
void testInvalidVertexThrowsException() {
163+
void testFirstVertexOutOfRangeThrowsException() {
164+
int[][] edges = {{2, 1}};
165+
166+
assertThrows(
167+
IllegalArgumentException.class,
168+
() -> Bridges.findBridges(2, edges)
169+
);
170+
}
171+
172+
@Test
173+
void testSecondVertexOutOfRangeThrowsException() {
88174
int[][] edges = {{0, 2}};
89175

90-
assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, edges));
176+
assertThrows(
177+
IllegalArgumentException.class,
178+
() -> Bridges.findBridges(2, edges)
179+
);
180+
}
181+
182+
@Test
183+
void testBridgesAreSortedByBothEndpoints() {
184+
int[][] edges = {{2, 3}, {0, 2}, {0, 1}};
185+
186+
List<int[]> result = Bridges.findBridges(4, edges);
187+
188+
assertEquals(3, result.size());
189+
assertArrayEquals(new int[] {0, 1}, result.get(0));
190+
assertArrayEquals(new int[] {0, 2}, result.get(1));
191+
assertArrayEquals(new int[] {2, 3}, result.get(2));
91192
}
92193
}

0 commit comments

Comments
 (0)