Debian Codegraph @

build/build_test.go

raw symbols SCIP
1 package build
2
3 import (
4 "fmt"
5 "github.com/yourbasic/graph"
6 "reflect"
7 "testing"
8 )
9
10 func diff(res, exp interface{}) (message string, diff bool) {
11 if !reflect.DeepEqual(res, exp) {
12 message = fmt.Sprintf("%v; want %v", res, exp)
13 diff = true
14 }
15 return
16 }
17
18 // Check internal consistency.
19 func Consistent(mess string, t *testing.T, g *Virtual) {
20 if g == nil {
21 return
22 }
23 n := g.order
24 // Check that degree and visit are consistent with edge and cost.
25 for v := 0; v < n; v++ {
26 deg := 0
27 visited := make([]bool, n)
28 g.visit(v, 0, func(w int, c int64) (skip bool) {
29 visited[w] = true
30 deg++
31 if !g.edge(v, w) {
32 t.Errorf("%s: visit(%d): (%d,%d); edge(%d,%d): false\n", mess, v, w, c, v, w)
33 }
34 if cost := g.cost(v, w); c != cost {
35 t.Errorf("%s: visit(%d): (%d,%d); cost(%d,%d): %v\n", mess, v, w, c, v, w, cost)
36 }
37 return
38 })
39 d := g.degree(v)
40 if deg != d {
41 t.Errorf("%s: visit(%d) found %v neighbors; degree(%d): %v\n", mess, v, deg, v, d)
42 }
43 for w, ok := range visited {
44 if ok {
45 continue
46 }
47 if g.edge(v, w) {
48 t.Errorf("%s: visit(%d,%d): --; edge(%d,%d): true\n", mess, v, w, v, w)
49 }
50 }
51 CheckStart(mess, t, g, v, visited)
52 CheckAbort(mess, t, g, v)
53 }
54 return
55 }
56
57 // Check visit from all different starting points.
58 func CheckStart(mess string, t *testing.T, g *Virtual, v int, visited []bool) {
59 n := g.order
60 check := make([]bool, len(visited)) // Check that we get the same neighbors.
61 for a := 0; a <= n+1; a++ {
62 prev := -1
63 for v := a; v < n; v++ {
64 check[v] = visited[v]
65 }
66 g.visit(v, a, func(w int, _ int64) (skip bool) {
67 if w < 0 {
68 t.Errorf("%s: visit(%d, %d): returned negative w=%d", mess, v, a, w)
69 }
70 if w >= n {
71 t.Errorf("%s: visit(%d, %d): returned w=%d bigger than n=%d", mess, v, a, w, n)
72 }
73 if w < a {
74 t.Errorf("%s: visit(%d, %d): returned small w=%d", mess, v, a, w)
75 } else if 0 <= w && w < n && !check[w] {
76 t.Errorf("%s, visit(%d, %d) didn't return %d when a=0.\n", mess, v, a, w)
77 }
78 if v == w {
79 t.Errorf("%s: visit(%d, %d): self-loop detected\n", mess, v, a)
80 }
81 if prev > w {
82 t.Errorf("%s, visit(%d, %d) returned %d before %d.\n", mess, v, a, prev, w)
83 }
84 check[w] = false
85 prev = w
86 return
87 })
88 for w := a; w < n; w++ {
89 if check[w] {
90 t.Errorf("%s, visit(%d, %d) did return %d when a=0.\n", mess, v, a, w)
91 }
92 }
93 }
94 }
95
96 // Check visit for failed abort and wrong return value.
97 func CheckAbort(mess string, t *testing.T, g *Virtual, v int) {
98 for n := 1; n <= g.degree(v); n++ {
99 count := 0
100 aborted := g.visit(v, 0, func(_ int, _ int64) bool {
101 count++
102 return count == n // break after n iterations
103 })
104 if count != n {
105 t.Errorf("%s: visit(%d) didnt't abort after %d iteration(s).\n", mess, v, n)
106 }
107 if !aborted {
108 t.Errorf("%s: visit(%d) didnt't return true after %d iteration(s).\n", mess, v, n)
109 }
110 aborted = g.visit(v, 0, func(_ int, _ int64) bool {
111 return false
112 })
113 if aborted {
114 t.Errorf("%s: visit(%d) returned true after completed iteration.\n", mess, v)
115 }
116
117 }
118 }
119
120 func TestNull(t *testing.T) {
121 g := null
122 if mess, diff := diff(g.String(), "0 []"); diff {
123 t.Errorf("null %s", mess)
124 }
125 if mess, diff := diff(g.Order(), 0); diff {
126 t.Errorf("null %s", mess)
127 }
128 Consistent("null", t, g)
129 }
130
131 func TestSingleton(t *testing.T) {
132 if mess, diff := diff(singleton().String(), "1 []"); diff {
133 t.Errorf("singleton %s", mess)
134 }
135 Consistent("singleton", t, singleton())
136
137 if mess, diff := diff(singleton().String(), "1 []"); diff {
138 t.Errorf("singleton %s", mess)
139 }
140 Consistent("singleton", t, singleton())
141 }
142
143 func TestEdge(t *testing.T) {
144 e := edge()
145 if mess, diff := diff(e.String(), "2 [{0 1}]"); diff {
146 t.Errorf("edge %s", mess)
147 }
148 Consistent("edge", t, e)
149 }
150
151 func TestLine(t *testing.T) {
152 if mess, diff := diff(line(-1), (*Virtual)(nil)); diff {
153 t.Errorf("line %s", mess)
154 }
155 if mess, diff := diff(line(0).String(), "0 []"); diff {
156 t.Errorf("line %s", mess)
157 }
158 if mess, diff := diff(line(1).String(), "1 []"); diff {
159 t.Errorf("line %s", mess)
160 }
161 if mess, diff := diff(line(2).String(), "2 [{0 1}]"); diff {
162 t.Errorf("line %s", mess)
163 }
164 if mess, diff := diff(line(3).String(), "3 [{0 1} {1 2}]"); diff {
165 t.Errorf("line %s", mess)
166 }
167 for n := 0; n < 5; n++ {
168 Consistent("line", t, line(n))
169 }
170 }
171
172 func TestGeneric(t *testing.T) {
173 g := generic0(-1, alwaysEdge)
174 if mess, diff := diff(g, (*Virtual)(nil)); diff {
175 t.Errorf("generic0 %s", mess)
176 }
177 Consistent("generic0", t, g)
178
179 g = generic0(0, alwaysEdge)
180 if mess, diff := diff(g.String(), "0 []"); diff {
181 t.Errorf("generic0 %s", mess)
182 }
183 Consistent("generic0", t, g)
184
185 g = generic0(1, alwaysEdge)
186 if mess, diff := diff(g.String(), "1 []"); diff {
187 t.Errorf("generic0 %s", mess)
188 }
189 Consistent("generic0", t, g)
190
191 g = generic(-1, zero, alwaysEdge)
192 if mess, diff := diff(g, (*Virtual)(nil)); diff {
193 t.Errorf("generic %s", mess)
194 }
195 Consistent("generic", t, g)
196
197 g = generic(0, zero, alwaysEdge)
198 if mess, diff := diff(g.String(), "0 []"); diff {
199 t.Errorf("generic %s", mess)
200 }
201 Consistent("generic", t, g)
202
203 g = generic(1, zero, alwaysEdge)
204 if mess, diff := diff(g.String(), "1 []"); diff {
205 t.Errorf("generic %s", mess)
206 }
207 Consistent("generic", t, g)
208
209 g = generic(2, zero, alwaysEdge)
210 if mess, diff := diff(g.String(), "2 [{0 1}]"); diff {
211 t.Errorf("generic %s", mess)
212 }
213 Consistent("generic", t, g)
214
215 g = Generic(-1, nil)
216 if mess, diff := diff(g, (*Virtual)(nil)); diff {
217 t.Errorf("Generic %s", mess)
218 }
219 Consistent("Generic", t, g)
220
221 g = Generic(0, nil)
222 if mess, diff := diff(g.String(), "0 []"); diff {
223 t.Errorf("Generic %s", mess)
224 }
225 Consistent("Generic", t, g)
226
227 g = Generic(1, nil)
228 if mess, diff := diff(g.String(), "1 []"); diff {
229 t.Errorf("Generic %s", mess)
230 }
231 Consistent("Generic", t, g)
232
233 g = Generic(2, nil)
234 if mess, diff := diff(g.String(), "2 [{0 1}]"); diff {
235 t.Errorf("Generic %s", mess)
236 }
237 Consistent("Generic", t, g)
238
239 g = Generic(0, func(v, w int) bool { return v != 0 })
240 if mess, diff := diff(g.String(), "0 []"); diff {
241 t.Errorf("Generic %s", mess)
242 }
243 Consistent("Generic", t, g)
244
245 g = Generic(1, func(v, w int) bool { return v != 0 })
246 if mess, diff := diff(g.String(), "1 []"); diff {
247 t.Errorf("Generic %s", mess)
248 }
249 Consistent("Generic", t, g)
250
251 g = Generic(2, func(v, w int) bool { return v != 0 })
252 if mess, diff := diff(g.String(), "2 [(1 0)]"); diff {
253 t.Errorf("Generic %s", mess)
254 }
255 Consistent("Generic", t, g)
256
257 g3 := Generic(3, func(v, w int) bool { return v != 0 })
258 if mess, diff := diff(g3.String(), "3 [(1 0) {1 2} (2 0)]"); diff {
259 t.Errorf("Generic %s", mess)
260 }
261 Consistent("Generic", t, g3)
262 }
263
264 func TestSpecific(t *testing.T) {
265 if mess, diff := diff(graph.Equal(Kn(4), Specific(Kn(4))), true); diff {
266 t.Errorf("Specific %s", mess)
267 }
268 Consistent("Specific", t, Specific(Kn(4)))
269
270 cost := func(v, w int) int64 { return int64(10*v + w) }
271 res := Specific(Grid(2, 2).AddCostFunc(cost))
272 exp := Grid(2, 2).AddCostFunc(cost)
273 if mess, diff := diff(graph.Equal(res, exp), true); diff {
274 t.Errorf("Specific %s", mess)
275 }
276 Consistent("Specific", t, Specific(Kn(4)))
277
278 if mess, diff := diff(res.cost(0, 0), int64(0)); diff {
279 t.Errorf("Specific cost %s", mess)
280 }
281 if mess, diff := diff(res.cost(0, 1), int64(1)); diff {
282 t.Errorf("Specific cost %s", mess)
283 }
284 if mess, diff := diff(res.cost(1, 0), int64(10)); diff {
285 t.Errorf("Specific cost %s", mess)
286 }
287 }
288
289 func TestEmpty(t *testing.T) {
290 if mess, diff := diff(Empty(-1), (*Virtual)(nil)); diff {
291 t.Errorf("Empty %s", mess)
292 }
293 if mess, diff := diff(Empty(0).String(), "0 []"); diff {
294 t.Errorf("Empty %s", mess)
295 }
296 if mess, diff := diff(Empty(1).String(), "1 []"); diff {
297 t.Errorf("Empty %s", mess)
298 }
299 if mess, diff := diff(Empty(2).String(), "2 []"); diff {
300 t.Errorf("Empty %s", mess)
301 }
302 for n := 0; n < 10; n++ {
303 Consistent("Empty", t, Empty(n))
304 }
305 }
306
307 func TestKn(t *testing.T) {
308 if mess, diff := diff(Kn(-1), (*Virtual)(nil)); diff {
309 t.Errorf("Kn %s", mess)
310 }
311 if mess, diff := diff(Kn(0).String(), "0 []"); diff {
312 t.Errorf("Kn %s", mess)
313 }
314 if mess, diff := diff(Kn(1).String(), "1 []"); diff {
315 t.Errorf("Kn %s", mess)
316 }
317 if mess, diff := diff(Kn(2).String(), "2 [{0 1}]"); diff {
318 t.Errorf("Kn %s", mess)
319 }
320 for n := 0; n < 5; n++ {
321 Consistent("Kn", t, Kn(n))
322 }
323 }
324
325 func TestComplement(t *testing.T) {
326 res := Kn(0).AddCost(1).Complement()
327 exp := "0 []"
328 if mess, diff := diff(res.String(), exp); diff {
329 t.Errorf("Complement %s", mess)
330 }
331 Consistent("Complement", t, res)
332
333 res = Kn(4).AddCost(1).Complement()
334 exp = "4 []"
335 if mess, diff := diff(res.String(), exp); diff {
336 t.Errorf("Complement %s", mess)
337 }
338 Consistent("Complement", t, res)
339
340 res = Empty(1).Complement()
341 exp = "1 []"
342 if mess, diff := diff(res.String(), exp); diff {
343 t.Errorf("Complement %s", mess)
344 }
345 Consistent("Complement", t, res)
346
347 res = Cycle(2).AddCost(1).Complement()
348 exp = "2 []"
349 if mess, diff := diff(res.String(), exp); diff {
350 t.Errorf("Complement %s", mess)
351 }
352 Consistent("Complement", t, res)
353
354 res = Cycle(4).AddCost(1).Complement()
355 exp = "4 [{0 2} {1 3}]"
356 if mess, diff := diff(res.String(), exp); diff {
357 t.Errorf("Complement %s", mess)
358 }
359 Consistent("Complement", t, res)
360
361 res = Grid(2, 3).Complement()
362 exp = "6 [{0 2} {0 4} {0 5} {1 3} {1 5} {2 3} {2 4} {3 5}]"
363 if mess, diff := diff(res.String(), exp); diff {
364 t.Errorf("Complement %s", mess)
365 }
366 Consistent("Complement", t, res)
367 }
368
369 func TestKeep(t *testing.T) {
370 g := Cycle(3).AddCost(1).Keep(func(v, w int) bool {
371 return v+1 != w
372 })
373 if mess, diff := diff(g.String(), "3 [{0 2}:1 (1 0):1 (2 1):1]"); diff {
374 t.Errorf("Keep %s", mess)
375 }
376 Consistent("Keep", t, g)
377
378 g = Cycle(3).AddCost(1).Keep(nil)
379 if mess, diff := diff(g.String(), "3 [{0 1}:1 {0 2}:1 {1 2}:1]"); diff {
380 t.Errorf("Keep %s", mess)
381 }
382 Consistent("Keep", t, g)
383 }
384
385 func TestAddCost(t *testing.T) {
386 res := Grid(1, 2).AddCost(5)
387 exp := "2 [{0 1}:5]"
388 if mess, diff := diff(res.String(), exp); diff {
389 t.Errorf("AddCost %s", mess)
390 }
391 Consistent("AccCost", t, res)
392
393 res = Empty(0).AddCost(0)
394 exp = "0 []"
395 if mess, diff := diff(res.String(), exp); diff {
396 t.Errorf("AddCost %s", mess)
397 }
398 Consistent("AccCost", t, res)
399 }
400
401 func TestAddCostFunc(t *testing.T) {
402 res := Grid(1, 2).AddCostFunc(Cost(5))
403 exp := "2 [{0 1}:5]"
404 if mess, diff := diff(res.String(), exp); diff {
405 t.Errorf("AddCostFunc %s", mess)
406 }
407 Consistent("AccCostFunc", t, res)
408
409 if mess, diff := diff(res.Cost(0, 0), int64(0)); diff {
410 t.Errorf("Cost %s", mess)
411 }
412 if mess, diff := diff(res.Cost(0, 1), int64(5)); diff {
413 t.Errorf("Cost %s", mess)
414 }
415 if mess, diff := diff(res.Cost(-1, 0), int64(0)); diff {
416 t.Errorf("Cost %s", mess)
417 }
418
419 res = Grid(1, 2).AddCostFunc(nil)
420 exp = "2 [{0 1}]"
421 if mess, diff := diff(res.String(), exp); diff {
422 t.Errorf("AddCostFunc %s", mess)
423 }
424 Consistent("AccCostFunc", t, res)
425
426 res = Empty(0).AddCostFunc(Cost(5))
427 exp = "0 []"
428 if mess, diff := diff(res.String(), exp); diff {
429 t.Errorf("AddCostFunc %s", mess)
430 }
431 Consistent("AddCostFunc", t, res)
432 }
433
434 func TestVisit(t *testing.T) {
435 g := Grid(1, 2).AddCost(5)
436 res := g.Visit(0, func(w int, c int64) (skip bool) {
437 return w == 1 && c == 5
438 })
439 if mess, diff := diff(res, true); diff {
440 t.Errorf("Visit %s", mess)
441 }
442
443 res = g.VisitFrom(0, 0, func(w int, c int64) (skip bool) {
444 return w == 1 && c == 5
445 })
446 if mess, diff := diff(res, true); diff {
447 t.Errorf("Visit %s", mess)
448 }
449
450 res = g.VisitFrom(0, -1, func(w int, c int64) (skip bool) {
451 return w == 1 && c == 5
452 })
453 if mess, diff := diff(res, true); diff {
454 t.Errorf("Visit %s", mess)
455 }
456
457 res = g.VisitFrom(0, 2, func(w int, c int64) (skip bool) {
458 return w == 1 && c == 5
459 })
460 if mess, diff := diff(res, false); diff {
461 t.Errorf("Visit %s", mess)
462 }
463 }