Skip to content

Commit d2f1284

Browse files
WilliamChang80github-actionssiriak
authored
Add tests on DepthFirstSearch (#299)
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Andrii Siriak <[email protected]>
1 parent b2e79af commit d2f1284

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

graph/depthfirstsearch_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package graph
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestDfsWhenPathIsFound(t *testing.T) {
9+
nodes := []int{
10+
1, 2, 3, 4, 5, 6,
11+
}
12+
13+
//Adjacency Matrix for connected nodes
14+
edges := [][]bool{
15+
{false, true, true, false, false, false},
16+
{true, false, false, true, false, false},
17+
{true, false, false, true, false, false},
18+
{false, true, true, false, true, false},
19+
{false, false, false, true, false, true},
20+
{false, false, false, false, true, false},
21+
}
22+
23+
start := 1
24+
end := 6
25+
26+
actual, actualIsFound := DepthFirstSearch(start, end, nodes, edges)
27+
expected := []int{1, 3, 4, 5, 6}
28+
expectedIsFound := true
29+
t.Run("Test Dfs", func(t *testing.T) {
30+
if !reflect.DeepEqual(expected, actual) || !reflect.DeepEqual(actualIsFound, expectedIsFound) {
31+
t.Errorf("got route: %v, want route: %v", actual, expected)
32+
t.Errorf("got isFound: %v, want isFound: %v", actualIsFound, expectedIsFound)
33+
}
34+
})
35+
}
36+
37+
func TestDfsWhenPathIsNotFound(t *testing.T) {
38+
nodes := []int{
39+
1, 2, 3, 4, 5, 6,
40+
}
41+
42+
//Adjacency Matrix for connected nodes
43+
edges := [][]bool{
44+
{false, true, true, false, false, false},
45+
{true, false, false, true, false, false},
46+
{true, false, false, true, false, false},
47+
{false, true, true, false, true, false},
48+
{false, false, false, true, false, true},
49+
{false, false, false, false, true, false},
50+
}
51+
52+
start := 1
53+
end := 7
54+
55+
actual, actualIsFound := DepthFirstSearch(start, end, nodes, edges)
56+
var expected []int
57+
expectedIsFound := false
58+
t.Run("Test Dfs", func(t *testing.T) {
59+
if !reflect.DeepEqual(expected, actual) || !reflect.DeepEqual(actualIsFound, expectedIsFound) {
60+
t.Errorf("got route: %v, want route: %v", actual, expected)
61+
t.Errorf("got isFound: %v, want isFound: %v", actualIsFound, expectedIsFound)
62+
}
63+
})
64+
}

0 commit comments

Comments
 (0)