-
Notifications
You must be signed in to change notification settings - Fork 0
/
dag_test.go
91 lines (80 loc) · 1.84 KB
/
dag_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package dag
import (
"testing"
)
func TestDAG(t *testing.T) {
requires := map[int][]int{
0: nil,
1: {0},
2: {0, 1},
3: {0},
4: {2, 3},
}
dag, err := NewDAG(requires)
assertNil(t, err)
assertNotNil(t, dag)
nodesList := dag.TopoSort()
assertEqual(t, len(nodesList), 4)
assertEqual(t, []int{0}, nodesList[0])
// assert.ElementsMatch(t, []Node{1, 3}, nodesList[1])
assertEqual(t, []int{2}, nodesList[2])
assertEqual(t, []int{4}, nodesList[3])
problem := []int{3}
nodesList2 := dag.Solve((problem))
assertEqual(t, len(nodesList2), 2)
assertEqual(t, []int{0}, nodesList2[0])
assertEqual(t, []int{3}, nodesList2[1])
problem2 := []int{2}
nodesList3 := dag.Solve((problem2))
assertEqual(t, len(nodesList3), 3)
assertEqual(t, []int{0}, nodesList3[0])
assertEqual(t, []int{1}, nodesList3[1])
assertEqual(t, []int{2}, nodesList3[2])
}
func TestDAGWithInvalid(t *testing.T) {
requires := map[int][]int{
0: nil,
1: {0, 2},
2: {0, 1},
3: {0},
4: {2, 3},
}
_, err := NewDAG(requires)
assertNotNil(t, err)
}
func TestDAGWithInvalid2(t *testing.T) {
requires := map[int][]int{
0: nil,
1: {0},
2: {0, 1, 5},
3: {0},
4: {2, 3},
}
_, err := NewDAG(requires)
assertNotNil(t, err)
}
func TestNodes(t *testing.T) {
requires := map[int][]int{
0: nil,
1: {0},
2: {0, 1},
3: {0},
4: {2, 3},
}
dag, err := NewDAG(requires)
assertNil(t, err)
// []Node{0, 1, 3, 2, 4} or []Node{0, 3, 1, 2, 4}
assertEqual(t, int(0), dag.nodes[0])
//assert.Contains(t, []Node{1, 3}, dag.nodes[1])
//assert.Contains(t, []Node{1, 3}, dag.nodes[2])
assertEqual(t, int(2), dag.nodes[3])
assertEqual(t, int(4), dag.nodes[4])
assertEqual(t, dag.nodes, dag.Nodes())
}
func TestDAG3(t *testing.T) {
dag, err := NewDAG[int](nil)
assertNil(t, err)
assertEqual(t, len(dag.Nodes()), 0)
soloution := dag.Solve(nil)
assertEqual(t, len(soloution), 0)
}