-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathutils_test.go
132 lines (115 loc) · 2.14 KB
/
utils_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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package di
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func TestGraph(t *testing.T) {
tests := []struct {
descr string
vertices []int
edges [][]int
expected []int
expectedErr bool
}{
{
descr: "test dag 1",
vertices: []int{},
edges: [][]int{
{1, 2},
{1, 3},
{4, 2},
{4, 3},
{2, 5},
},
expected: []int{4, 1, 3, 2, 5},
expectedErr: false,
},
{
descr: "test dag 2",
vertices: []int{9999},
edges: [][]int{
{1, 2},
{1, 3},
{4, 2},
{4, 3},
{2, 5},
{1, 2}, // redeclared
},
expected: []int{4, 1, 3, 2, 5, 9999},
expectedErr: false,
},
{
descr: "test dag 3",
edges: [][]int{
{5, 11},
{7, 11},
{7, 8},
{3, 8},
{3, 10},
{11, 2},
{11, 9},
{11, 10},
{8, 9},
},
expected: []int{3, 7, 8, 5, 11, 10, 9, 2},
expectedErr: false,
},
{
descr: "test dag cycle",
vertices: []int{},
edges: [][]int{
{1, 2},
{1, 3},
{4, 2},
{4, 3},
{2, 5},
{5, 1},
},
expected: []int{1, 2, 3, 4, 5},
expectedErr: true,
},
}
for _, test := range tests {
g := newGraph()
for _, v := range test.vertices {
g.AddVertex(v)
}
for _, e := range test.edges {
g.AddEdge(e[0], e[1])
}
l, err := g.TopologicalOrdering()
if test.expectedErr {
require.NotNil(t, err, test.descr)
} else {
require.Nil(t, err, test.descr)
}
require.Equal(t, test.expected, l, test.descr)
}
}
func TestMultiErrBuilder(t *testing.T) {
builder := &multiErrBuilder{}
err := builder.Build()
require.Nil(t, err)
builder.Add(errors.New("a"))
err = builder.Build()
require.NotNil(t, err)
require.Equal(t, "a", err.Error())
builder.Add(errors.New("b"))
err = builder.Build()
require.NotNil(t, err)
require.Equal(t, "a AND b", err.Error())
builder.Add(errors.New("c"))
err = builder.Build()
require.NotNil(t, err)
require.Equal(t, "a AND b AND c", err.Error())
}
func TestFillUtil(t *testing.T) {
var err error
var i int
err = fill(100, &i)
require.Nil(t, err)
require.Equal(t, 100, i)
err = fill(100, i)
require.NotNil(t, err)
}