Skip to content

Commit 715fd1b

Browse files
authored
Implement unit tests for BidirectionalBFS
Added unit tests for BidirectionalBFS to check path existence.
1 parent 3a0f3a3 commit 715fd1b

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed
Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
package com.thealgorithms.graphs;
2+
13
import java.util.Map;
24
import java.util.List;
35
import java.util.HashMap;
46
import java.util.Arrays;
57

8+
import org.junit.Test;
9+
import static org.junit.Assert.*;
10+
611
/**
7-
* Simple test for BidirectionalBFS.
12+
* Unit tests for BidirectionalBFS.
813
*/
914
public class BidirectionalBFSTest
1015
{
11-
public static void main(String[] args)
16+
@Test
17+
public void testPathExists()
1218
{
1319
Map<Integer, List<Integer>> graph = new HashMap<>();
1420
graph.put(0, Arrays.asList(1, 2));
@@ -18,12 +24,19 @@ public static void main(String[] args)
1824
graph.put(4, Arrays.asList(2, 5));
1925
graph.put(5, Arrays.asList(3, 4));
2026

21-
// Test 1
22-
boolean result1 = BidirectionalBFS.bidirectionalBFS(graph, 0, 5);
23-
System.out.println("Path 0->5 exists: " + result1); // true
27+
assertTrue(BidirectionalBFS.bidirectionalBFS(graph, 0, 5));
28+
}
2429

25-
// Test 2
26-
boolean result2 = BidirectionalBFS.bidirectionalBFS(graph, 0, 6);
27-
System.out.println("Path 0->6 exists: " + result2); // false
30+
@Test
31+
public void testPathDoesNotExist()
32+
{
33+
Map<Integer, List<Integer>> graph = new HashMap<>();
34+
graph.put(0, Arrays.asList(1));
35+
graph.put(1, Arrays.asList(0));
36+
// node 2 is isolated
37+
graph.put(2, Arrays.asList());
38+
39+
assertFalse(BidirectionalBFS.bidirectionalBFS(graph, 0, 2));
2840
}
2941
}
42+

0 commit comments

Comments
 (0)