Debian Codegraph @

go/callgraph/vta/internal/trie/trie_test.go

raw symbols SCIP
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build go1.13
6
7 package trie
8
9 import (
10 "reflect"
11 "strconv"
12 "testing"
13 )
14
15 func TestScope(t *testing.T) {
16 def := Scope{}
17 s0, s1 := newScope(), newScope()
18 if s0 == def || s1 == def {
19 t.Error("newScope() should never be == to the default scope")
20 }
21 if s0 == s1 {
22 t.Errorf("newScope() %q and %q should not be ==", s0, s1)
23 }
24 if s0.id == 0 {
25 t.Error("s0.id is 0")
26 }
27 if s1.id == 0 {
28 t.Error("s1.id is 0")
29 }
30 got := s0.String()
31 if _, err := strconv.Atoi(got); err != nil {
32 t.Errorf("scope{%s}.String() is not an int: got %s with error %s", s0, got, err)
33 }
34 }
35
36 func TestCollision(t *testing.T) {
37 var x any = 1
38 var y any = 2
39
40 if v := TakeLhs(x, y); v != x {
41 t.Errorf("TakeLhs(%s, %s) got %s. want %s", x, y, v, x)
42 }
43 if v := TakeRhs(x, y); v != y {
44 t.Errorf("TakeRhs(%s, %s) got %s. want %s", x, y, v, y)
45 }
46 }
47
48 func TestDefault(t *testing.T) {
49 def := Map{}
50
51 if def.Size() != 0 {
52 t.Errorf("default node has non-0 size %d", def.Size())
53 }
54 if want, got := (Scope{}), def.Scope(); got != want {
55 t.Errorf("default is in a non default scope (%s) from b (%s)", got, want)
56 }
57 if v, ok := def.Lookup(123); !(v == nil && !ok) {
58 t.Errorf("Scope{}.Lookup() = (%s, %v) not (nil, false)", v, ok)
59 }
60 if !def.Range(func(k uint64, v any) bool {
61 t.Errorf("Scope{}.Range() called it callback on %d:%s", k, v)
62 return true
63 }) {
64 t.Error("Scope{}.Range() always iterates through all elements")
65 }
66
67 if got, want := def.String(), "{}"; got != want {
68 t.Errorf("Scope{}.String() got %s. want %s", got, want)
69 }
70
71 b := NewBuilder()
72 if def == b.Empty() {
73 t.Error("Scope{} == to an empty node from a builder")
74 }
75 if b.Clone(def) != b.Empty() {
76 t.Error("b.Clone(Scope{}) should equal b.Empty()")
77 }
78 if !def.DeepEqual(b.Empty()) {
79 t.Error("Scope{}.DeepEqual(b.Empty()) should hold")
80 }
81 }
82
83 func TestBuilders(t *testing.T) {
84 b0, b1 := NewBuilder(), NewBuilder()
85 if b0.Scope() == b1.Scope() {
86 t.Errorf("builders have the same scope %s", b0.Scope())
87 }
88
89 if b0.Empty() == b1.Empty() {
90 t.Errorf("empty nodes from different scopes are disequal")
91 }
92 if !b0.Empty().DeepEqual(b1.Empty()) {
93 t.Errorf("empty nodes from different scopes are not DeepEqual")
94 }
95
96 clone := b1.Clone(b0.Empty())
97 if clone != b1.Empty() {
98 t.Errorf("Clone() empty nodes %v != %v", clone, b1.Empty())
99 }
100 }
101
102 func TestEmpty(t *testing.T) {
103 b := NewBuilder()
104 e := b.Empty()
105 if e.Size() != 0 {
106 t.Errorf("empty nodes has non-0 size %d", e.Size())
107 }
108 if e.Scope() != b.Scope() {
109 t.Errorf("b.Empty() is in a different scope (%s) from b (%s)", e.Scope(), b.Scope())
110 }
111 if v, ok := e.Lookup(123); !(v == nil && !ok) {
112 t.Errorf("empty.Lookup() = (%s, %v) not (nil, false)", v, ok)
113 }
114 if l := e.n.find(123); l != nil {
115 t.Errorf("empty.find(123) got %v. want nil", l)
116 }
117 e.Range(func(k uint64, v any) bool {
118 t.Errorf("empty.Range() called it callback on %d:%s", k, v)
119 return true
120 })
121
122 want := "{}"
123 if got := e.String(); got != want {
124 t.Errorf("empty.String(123) got %s. want %s", got, want)
125 }
126 }
127
128 func TestCreate(t *testing.T) {
129 // The node orders are printed in lexicographic little-endian.
130 b := NewBuilder()
131 for _, c := range []struct {
132 m map[uint64]any
133 want string
134 }{
135 {
136 map[uint64]any{},
137 "{}",
138 },
139 {
140 map[uint64]any{1: "a"},
141 "{1: a}",
142 },
143 {
144 map[uint64]any{2: "b", 1: "a"},
145 "{1: a, 2: b}",
146 },
147 {
148 map[uint64]any{1: "x", 4: "y", 5: "z"},
149 "{1: x, 4: y, 5: z}",
150 },
151 } {
152 m := b.Create(c.m)
153 if got := m.String(); got != c.want {
154 t.Errorf("Create(%v) got %q. want %q ", c.m, got, c.want)
155 }
156 }
157 }
158
159 func TestElems(t *testing.T) {
160 b := NewBuilder()
161 for _, orig := range []map[uint64]any{
162 {},
163 {1: "a"},
164 {1: "a", 2: "b"},
165 {1: "x", 4: "y", 5: "z"},
166 {1: "x", 4: "y", 5: "z", 123: "abc"},
167 } {
168 m := b.Create(orig)
169 if elems := Elems(m); !reflect.DeepEqual(orig, elems) {
170 t.Errorf("Elems(%v) got %q. want %q ", m, elems, orig)
171 }
172 }
173 }
174
175 func TestRange(t *testing.T) {
176 b := NewBuilder()
177 m := b.Create(map[uint64]any{1: "x", 3: "y", 5: "z", 6: "stop", 8: "a"})
178
179 calls := 0
180 cb := func(k uint64, v any) bool {
181 t.Logf("visiting (%d, %v)", k, v)
182 calls++
183 return k%2 != 0 // stop after the first even number.
184 }
185 // The nodes are visited in increasing order.
186 all := m.Range(cb)
187 if all {
188 t.Error("expected to stop early")
189 }
190 want := 4
191 if calls != want {
192 t.Errorf("# of callbacks (%d) was expected to equal %d (1 + # of evens)",
193 calls, want)
194 }
195 }
196
197 func TestDeepEqual(t *testing.T) {
198 for _, m := range []map[uint64]any{
199 {},
200 {1: "x"},
201 {1: "x", 2: "y"},
202 } {
203 l := NewBuilder().Create(m)
204 r := NewBuilder().Create(m)
205 if !l.DeepEqual(r) {
206 t.Errorf("Expect %v to be DeepEqual() to %v", l, r)
207 }
208 }
209 }
210
211 func TestNotDeepEqual(t *testing.T) {
212 for _, c := range []struct {
213 left map[uint64]any
214 right map[uint64]any
215 }{
216 {
217 map[uint64]any{1: "x"},
218 map[uint64]any{},
219 },
220 {
221 map[uint64]any{},
222 map[uint64]any{1: "y"},
223 },
224 {
225 map[uint64]any{1: "x"},
226 map[uint64]any{1: "y"},
227 },
228 {
229 map[uint64]any{1: "x"},
230 map[uint64]any{1: "x", 2: "Y"},
231 },
232 {
233 map[uint64]any{1: "x", 2: "Y"},
234 map[uint64]any{1: "x"},
235 },
236 {
237 map[uint64]any{1: "x", 2: "y"},
238 map[uint64]any{1: "x", 2: "Y"},
239 },
240 } {
241 l := NewBuilder().Create(c.left)
242 r := NewBuilder().Create(c.right)
243 if l.DeepEqual(r) {
244 t.Errorf("Expect %v to be !DeepEqual() to %v", l, r)
245 }
246 }
247 }
248
249 func TestMerge(t *testing.T) {
250 b := NewBuilder()
251 for _, c := range []struct {
252 left map[uint64]any
253 right map[uint64]any
254 want string
255 }{
256 {
257 map[uint64]any{},
258 map[uint64]any{},
259 "{}",
260 },
261 {
262 map[uint64]any{},
263 map[uint64]any{1: "a"},
264 "{1: a}",
265 },
266 {
267 map[uint64]any{1: "a"},
268 map[uint64]any{},
269 "{1: a}",
270 },
271 {
272 map[uint64]any{1: "a", 2: "b"},
273 map[uint64]any{},
274 "{1: a, 2: b}",
275 },
276 {
277 map[uint64]any{1: "x"},
278 map[uint64]any{1: "y"},
279 "{1: x}", // default collision is left
280 },
281 {
282 map[uint64]any{1: "x"},
283 map[uint64]any{2: "y"},
284 "{1: x, 2: y}",
285 },
286 {
287 map[uint64]any{4: "y", 5: "z"},
288 map[uint64]any{1: "x"},
289 "{1: x, 4: y, 5: z}",
290 },
291 {
292 map[uint64]any{1: "x", 5: "z"},
293 map[uint64]any{4: "y"},
294 "{1: x, 4: y, 5: z}",
295 },
296 {
297 map[uint64]any{1: "x", 4: "y"},
298 map[uint64]any{5: "z"},
299 "{1: x, 4: y, 5: z}",
300 },
301 {
302 map[uint64]any{1: "a", 4: "c"},
303 map[uint64]any{2: "b", 5: "d"},
304 "{1: a, 2: b, 4: c, 5: d}",
305 },
306 {
307 map[uint64]any{1: "a", 4: "c"},
308 map[uint64]any{2: "b", 5 + 8: "d"},
309 "{1: a, 2: b, 4: c, 13: d}",
310 },
311 {
312 map[uint64]any{2: "b", 5 + 8: "d"},
313 map[uint64]any{1: "a", 4: "c"},
314 "{1: a, 2: b, 4: c, 13: d}",
315 },
316 {
317 map[uint64]any{1: "a", 4: "c"},
318 map[uint64]any{2: "b", 5 + 8: "d"},
319 "{1: a, 2: b, 4: c, 13: d}",
320 },
321 {
322 map[uint64]any{2: "b", 5 + 8: "d"},
323 map[uint64]any{1: "a", 4: "c"},
324 "{1: a, 2: b, 4: c, 13: d}",
325 },
326 {
327 map[uint64]any{2: "b", 5 + 8: "d"},
328 map[uint64]any{2: "", 3: "a"},
329 "{2: b, 3: a, 13: d}",
330 },
331
332 {
333 // crafted for `!prefixesOverlap(p, m, q, n)`
334 left: map[uint64]any{1: "a", 2 + 1: "b"},
335 right: map[uint64]any{4 + 1: "c", 4 + 2: "d"},
336 // p: 5, m: 2 q: 1, n: 2
337 want: "{1: a, 3: b, 5: c, 6: d}",
338 },
339 {
340 // crafted for `ord(m, n) && !zeroBit(q, m)`
341 left: map[uint64]any{8 + 2 + 1: "a", 16 + 4: "b"},
342 right: map[uint64]any{16 + 8 + 2 + 1: "c", 16 + 8 + 4 + 2 + 1: "d"},
343 // left: p: 15, m: 16
344 // right: q: 27, n: 4
345 want: "{11: a, 20: b, 27: c, 31: d}",
346 },
347 {
348 // crafted for `ord(n, m) && !zeroBit(p, n)`
349 // p: 6, m: 1 q: 5, n: 2
350 left: map[uint64]any{4 + 2: "b", 4 + 2 + 1: "c"},
351 right: map[uint64]any{4: "a", 4 + 2 + 1: "dropped"},
352 want: "{4: a, 6: b, 7: c}",
353 },
354 } {
355 l, r := b.Create(c.left), b.Create(c.right)
356 m := b.Merge(l, r)
357 if got := m.String(); got != c.want {
358 t.Errorf("Merge(%s, %s) got %q. want %q ", l, r, got, c.want)
359 }
360 }
361 }
362
363 func TestIntersect(t *testing.T) {
364 // Most of the test cases go after specific branches of intersect.
365 b := NewBuilder()
366 for _, c := range []struct {
367 left map[uint64]any
368 right map[uint64]any
369 want string
370 }{
371 {
372 left: map[uint64]any{10: "a", 39: "b"},
373 right: map[uint64]any{10: "A", 39: "B", 75: "C"},
374 want: "{10: a, 39: b}",
375 },
376 {
377 left: map[uint64]any{10: "a", 39: "b"},
378 right: map[uint64]any{},
379 want: "{}",
380 },
381 {
382 left: map[uint64]any{},
383 right: map[uint64]any{10: "A", 39: "B", 75: "C"},
384 want: "{}",
385 },
386 { // m == n && p == q && left.(*empty) case
387 left: map[uint64]any{4: 1, 6: 3, 10: 8, 15: "on left"},
388 right: map[uint64]any{0: 8, 7: 6, 11: 0, 15: "on right"},
389 want: "{15: on left}",
390 },
391 { // m == n && p == q && right.(*empty) case
392 left: map[uint64]any{0: "on left", 1: 2, 2: 3, 3: 1, 7: 3},
393 right: map[uint64]any{0: "on right", 5: 1, 6: 8},
394 want: "{0: on left}",
395 },
396 { // m == n && p == q && both left and right are not empty
397 left: map[uint64]any{1: "a", 2: "b", 3: "c"},
398 right: map[uint64]any{0: "A", 1: "B", 2: "C"},
399 want: "{1: a, 2: b}",
400 },
401 { // m == n && p == q && both left and right are not empty
402 left: map[uint64]any{1: "a", 2: "b", 3: "c"},
403 right: map[uint64]any{0: "A", 1: "B", 2: "C"},
404 want: "{1: a, 2: b}",
405 },
406 { // !prefixesOverlap(p, m, q, n)
407 // p = 1, m = 2, q = 5, n = 2
408 left: map[uint64]any{0b001: 1, 0b011: 3},
409 right: map[uint64]any{0b100: 4, 0b111: 7},
410 want: "{}",
411 },
412 { // ord(m, n) && zeroBit(q, m)
413 // p = 3, m = 4, q = 0, n = 1
414 left: map[uint64]any{0b010: 2, 0b101: 5},
415 right: map[uint64]any{0b000: 0, 0b001: 1},
416 want: "{}",
417 },
418
419 { // ord(m, n) && !zeroBit(q, m)
420 // p = 29, m = 2, q = 30, n = 1
421 left: map[uint64]any{
422 0b11101: "29",
423 0b11110: "30",
424 },
425 right: map[uint64]any{
426 0b11110: "30 on right",
427 0b11111: "31",
428 },
429 want: "{30: 30}",
430 },
431 { // ord(n, m) && zeroBit(p, n)
432 // p = 5, m = 2, q = 3, n = 4
433 left: map[uint64]any{0b000: 0, 0b001: 1},
434 right: map[uint64]any{0b010: 2, 0b101: 5},
435 want: "{}",
436 },
437 { // default case
438 // p = 5, m = 2, q = 3, n = 4
439 left: map[uint64]any{0b100: 1, 0b110: 3},
440 right: map[uint64]any{0b000: 8, 0b111: 6},
441 want: "{}",
442 },
443 } {
444 l, r := b.Create(c.left), b.Create(c.right)
445 m := b.Intersect(l, r)
446 if got := m.String(); got != c.want {
447 t.Errorf("Intersect(%s, %s) got %q. want %q ", l, r, got, c.want)
448 }
449 }
450 }
451
452 func TestIntersectWith(t *testing.T) {
453 b := NewBuilder()
454 l := b.Create(map[uint64]any{10: 2.0, 39: 32.0})
455 r := b.Create(map[uint64]any{10: 6.0, 39: 10.0, 75: 1.0})
456
457 prodIfDifferent := func(x any, y any) any {
458 if x, ok := x.(float64); ok {
459 if y, ok := y.(float64); ok {
460 if x == y {
461 return x
462 }
463 return x * y
464 }
465 }
466 return x
467 }
468
469 m := b.IntersectWith(prodIfDifferent, l, r)
470
471 want := "{10: %!s(float64=12), 39: %!s(float64=320)}"
472 if got := m.String(); got != want {
473 t.Errorf("IntersectWith(min, %s, %s) got %q. want %q ", l, r, got, want)
474 }
475 }
476
477 func TestRemove(t *testing.T) {
478 // Most of the test cases go after specific branches of intersect.
479 b := NewBuilder()
480 for _, c := range []struct {
481 m map[uint64]any
482 key uint64
483 want string
484 }{
485 {map[uint64]any{}, 10, "{}"},
486 {map[uint64]any{10: "a"}, 10, "{}"},
487 {map[uint64]any{39: "b"}, 10, "{39: b}"},
488 // Branch cases:
489 // !matchPrefix(kp, br.prefix, br.branching)
490 {map[uint64]any{10: "a", 39: "b"}, 128, "{10: a, 39: b}"},
491 // case: left == br.left && right == br.right
492 {map[uint64]any{10: "a", 39: "b"}, 16, "{10: a, 39: b}"},
493 // left updated and is empty.
494 {map[uint64]any{10: "a", 39: "b"}, 10, "{39: b}"},
495 // right updated and is empty.
496 {map[uint64]any{10: "a", 39: "b"}, 39, "{10: a}"},
497 // final b.mkBranch(...) case.
498 {map[uint64]any{10: "a", 39: "b", 128: "c"}, 39, "{10: a, 128: c}"},
499 } {
500 pre := b.Create(c.m)
501 post := b.Remove(pre, c.key)
502 if got := post.String(); got != c.want {
503 t.Errorf("Remove(%s, %d) got %q. want %q ", pre, c.key, got, c.want)
504 }
505 }
506 }
507
508 func TestRescope(t *testing.T) {
509 b := NewBuilder()
510 l := b.Create(map[uint64]any{10: "a", 39: "b"})
511 r := b.Create(map[uint64]any{10: "A", 39: "B", 75: "C"})
512
513 b.Rescope()
514
515 m := b.Intersect(l, r)
516 if got, want := m.String(), "{10: a, 39: b}"; got != want {
517 t.Errorf("Intersect(%s, %s) got %q. want %q", l, r, got, want)
518 }
519 if m.Scope() == l.Scope() {
520 t.Errorf("m.Scope() = %v should not equal l.Scope() = %v", m.Scope(), l.Scope())
521 }
522 if m.Scope() == r.Scope() {
523 t.Errorf("m.Scope() = %v should not equal r.Scope() = %v", m.Scope(), r.Scope())
524 }
525 }
526
527 func TestSharing(t *testing.T) {
528 b := NewBuilder()
529 l := b.Create(map[uint64]any{0: "a", 1: "b"})
530 r := b.Create(map[uint64]any{1: "B", 2: "C"})
531
532 rleftold := r.n.(*branch).left
533
534 m := b.Merge(l, r)
535 if mleft := m.n.(*branch).left; mleft != l.n {
536 t.Errorf("unexpected value for left branch of %v. want %v got %v", m, l, mleft)
537 }
538
539 if rleftnow := r.n.(*branch).left; rleftnow != rleftold {
540 t.Errorf("r.n.(*branch).left was modified by the Merge operation. was %v now %v", rleftold, rleftnow)
541 }
542 }