-
Notifications
You must be signed in to change notification settings - Fork 1
/
detect_cycle_if_linked_test.go
201 lines (163 loc) · 4.66 KB
/
detect_cycle_if_linked_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
package incr
import (
"testing"
"github.com/wcharczuk/go-incr/testutil"
)
func Test_DetectCycleIfLinked(t *testing.T) {
g := New()
n0 := MapN[any, any](g, identMany)
n01 := MapN[any, any](g, identMany)
n02 := MapN[any, any](g, identMany)
n03 := MapN[any, any](g, identMany)
n1 := MapN[any, any](g, identMany)
n11 := MapN[any, any](g, identMany)
n12 := MapN[any, any](g, identMany)
n13 := MapN[any, any](g, identMany)
var err error
err = n01.AddInput(n0)
testutil.NoError(t, err)
err = n02.AddInput(n01)
testutil.NoError(t, err)
err = n03.AddInput(n02)
testutil.NoError(t, err)
err = n1.AddInput(n02)
testutil.NoError(t, err)
err = n11.AddInput(n1)
testutil.NoError(t, err)
err = n12.AddInput(n11)
testutil.NoError(t, err)
err = DetectCycleIfLinked(n13, n12)
testutil.NoError(t, err)
err = DetectCycleIfLinked(n1, n12)
testutil.Error(t, err)
}
func Test_DetectCycleIfLinked_nils(t *testing.T) {
g := New()
n0 := newMockBareNode(g)
n1 := newMockBareNode(g)
n1.n.parentsFn = func() []INode {
return []INode{nil}
}
var err error
err = DetectCycleIfLinked(nil, n0)
testutil.NoError(t, err)
err = DetectCycleIfLinked(n0, nil)
testutil.NoError(t, err)
err = DetectCycleIfLinked(n0, n1)
testutil.NoError(t, err)
}
func detectCycleNode(label string) MapNIncr[any, any] {
g := New()
n := MapN[any, any](g, identMany)
n.Node().SetLabel(label)
return n
}
func Test_DetectCycleIfLinked_complex(t *testing.T) {
n0 := detectCycleNode("n0")
n1 := detectCycleNode("n1")
n2 := detectCycleNode("n2")
var err error
err = n1.AddInput(n0)
testutil.NoError(t, err)
err = n2.AddInput(n1)
testutil.NoError(t, err)
n01 := detectCycleNode("n01")
n02 := detectCycleNode("n02")
err = n01.AddInput(n2)
testutil.NoError(t, err)
err = n02.AddInput(n01)
testutil.NoError(t, err)
n11 := detectCycleNode("n11")
n12 := detectCycleNode("n12")
n13 := detectCycleNode("n13")
err = n11.AddInput(n01)
testutil.NoError(t, err)
err = n12.AddInput(n11)
testutil.NoError(t, err)
err = n13.AddInput(n12)
testutil.NoError(t, err)
n21 := detectCycleNode("n21")
n22 := detectCycleNode("n22")
n23 := detectCycleNode("n23")
n24 := detectCycleNode("n24")
err = n21.AddInput(n11)
testutil.NoError(t, err)
err = n22.AddInput(n21)
testutil.NoError(t, err)
err = n23.AddInput(n22)
testutil.NoError(t, err)
err = n24.AddInput(n23)
testutil.NoError(t, err)
err = DetectCycleIfLinked(n2, n02)
testutil.NotNil(t, err)
err = DetectCycleIfLinked(n2, n13)
testutil.NotNil(t, err)
err = DetectCycleIfLinked(n2, n24)
testutil.NotNil(t, err)
err = DetectCycleIfLinked(n02, n13)
testutil.Nil(t, err, "this should _not_ cause a cycle")
err = DetectCycleIfLinked(n01, n13)
testutil.NotNil(t, err)
}
func Test_DetectCycleIfLinked_complex2(t *testing.T) {
n0 := detectCycleNode("n0")
n10 := detectCycleNode("n10")
n11 := detectCycleNode("n11")
n12 := detectCycleNode("n12")
n21 := detectCycleNode("n21")
n22 := detectCycleNode("n22")
var err error
err = n10.AddInput(n0)
testutil.NoError(t, err)
err = n11.AddInput(n10)
testutil.NoError(t, err)
err = n12.AddInput(n11)
testutil.NoError(t, err)
err = n21.AddInput(n11)
testutil.NoError(t, err)
err = n22.AddInput(n21)
testutil.NoError(t, err)
err = DetectCycleIfLinked(n10, n22)
testutil.Error(t, err)
}
func Test_DetectCycleIfLinked_2(t *testing.T) {
/* these are some trivial cases to make sure bases are covered */
g := New()
n0 := MapN[any, any](g, identMany)
n1 := MapN[any, any](g, identMany)
n2 := MapN[any, any](g, identMany)
err := DetectCycleIfLinked(n0, n0)
testutil.Error(t, err)
err = n1.AddInput(n0)
testutil.NoError(t, err)
err = DetectCycleIfLinked(n2, n1)
testutil.NoError(t, err)
err = n2.AddInput(n1)
testutil.NoError(t, err)
err = DetectCycleIfLinked(n0, n2)
testutil.NotNil(t, err)
}
func Test_DetectCycleIfLinked_regression(t *testing.T) {
g := New()
table := Var(g, "table")
columnDownload := Map(g, table, ident)
lastDownload := Map(g, columnDownload, ident)
targetUpload := Map(g, lastDownload, ident)
columnUpload := Map(g, table, ident)
lastUpload := Map(g, columnUpload, ident)
uploadRemaining := MapN(g, identMany, lastUpload)
err := DetectCycleIfLinked(uploadRemaining, targetUpload)
testutil.Nil(t, err, "this should _not_ cause a cycle!")
}
func Test_DetectCycleIfLinked_regression2(t *testing.T) {
g := New()
table := Var(g, "table")
columnDownload := MapN(g, identMany[string])
lastDownload := Map(g, columnDownload, ident)
_ = Map(g, lastDownload, ident)
columnUpload := Map(g, table, ident)
lastUpload := Map(g, columnUpload, ident)
_ = MapN(g, identMany, lastUpload)
err := DetectCycleIfLinked(columnDownload, table)
testutil.Nil(t, err, "this should _not_ cause a cycle!")
}