-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis_test.go
224 lines (212 loc) · 3.78 KB
/
analysis_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package dataflow
import (
"reflect"
"sort"
"testing"
)
func Test_convertStages(t *testing.T) {
tests := []struct {
name string
stages map[string]Stage
want map[string][]string
}{
{
"empty graph",
map[string]Stage{},
map[string][]string{},
},
{
"simple tree",
map[string]Stage{
"b": {requires: []string{"a"}},
"c": {requires: []string{"a"}},
"d": {requires: []string{"b"}},
"e": {requires: []string{"b"}},
"f": {requires: []string{"c"}},
},
map[string][]string{
"a": {"b", "c"},
"b": {"d", "e"},
"c": {"f"},
},
},
{
"diamond",
map[string]Stage{
"b": {requires: []string{"a"}},
"c": {requires: []string{"a"}},
"d": {requires: []string{"b", "c"}},
},
map[string][]string{
"a": {"b", "c"},
"b": {"d"},
"c": {"d"},
},
},
{
"graph with a cycle",
map[string]Stage{
"a": {requires: []string{"d"}},
"b": {requires: []string{"a"}},
"c": {requires: []string{"a"}},
"d": {requires: []string{"b"}},
"e": {requires: []string{"b"}},
"f": {requires: []string{"d"}},
},
map[string][]string{
"a": {"b", "c"},
"b": {"d", "e"},
"d": {"a", "f"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := convertStages(tt.stages); !equalGraphs(got, tt.want) {
t.Errorf("convertStages => %v, want %v", got, tt.want)
}
})
}
}
func Test_containsLoop(t *testing.T) {
tests := []struct {
name string
graph map[string][]string
want bool
}{
{
"empty graph",
map[string][]string{},
false,
},
{
"simple tree",
map[string][]string{
"a": {"b", "c"},
"b": {"d", "e"},
"c": {"f"},
},
false,
},
{
"graph with a single cycle",
map[string][]string{
"a": {"b", "c"},
"b": {"d", "e"},
"d": {"a", "f"},
},
true,
},
{
"graph with two cycles",
map[string][]string{
"a": {"b", "c"},
"b": {"d", "e"},
"d": {"f", "a"},
"f": {"b", "h"},
},
true,
},
{
"loop-graph",
map[string][]string{
"a": {"b"},
"b": {"a"},
},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := containsLoop(tt.graph); got != tt.want {
t.Errorf("containsLoop => %v, want %v", got, tt.want)
}
})
}
}
func Test_consistencyCheck(t *testing.T) {
tests := []struct {
name string
graph map[string][]string
expectErr bool
}{
{
"detached input",
map[string][]string{
Input: {},
"f1": {"f2", "f3"},
"f2": {sink},
"f3": {sink},
},
true,
},
{
"no final stage",
map[string][]string{
Input: {"f2"},
"f2": {"f3"},
sink: {},
},
true,
},
{
"dangling execution results",
map[string][]string{
Input: {"f1", "f2"},
"f1": {"f3"},
"f2": {"f4", "f5"}, // f4 missing output
"f3": {sink},
"f5": {sink},
},
true,
},
{
"unreachable stage",
map[string][]string{
Input: {"f1", "f2"},
"f1": {"f3"},
"f2": {"f4"},
"f3": {sink},
"f4": {sink},
"f5": {sink}, // unreachable
},
true,
},
{
"normal execution",
map[string][]string{
Input: {"f1", "f2"},
"f1": {"f3"},
"f2": {"f4", "f5"},
"f3": {sink},
"f4": {sink},
"f5": {sink},
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := consistencyCheck(tt.graph); (err != nil) != tt.expectErr {
t.Errorf("consistencyCheck error => %v, expected %v", err, tt.expectErr)
}
})
}
}
func equalGraphs(m1, m2 map[string][]string) bool {
if len(m1) != len(m2) {
return false
}
for k1, v1 := range m1 {
v2, exist := m2[k1]
if !exist {
return false
}
sort.Strings(v1)
sort.Strings(v2)
if !reflect.DeepEqual(v1, v2) {
return false
}
}
return true
}