Debian Codegraph @

go/callgraph/vta/internal/trie/op_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 package trie_test
6
7 import (
8 "fmt"
9 "math/rand"
10 "reflect"
11 "testing"
12 "time"
13
14 "golang.org/x/tools/go/callgraph/vta/internal/trie"
15 "maps"
16 )
17
18 // This file tests trie.Map by cross checking operations on a collection of
19 // trie.Map's against a collection of map[uint64]interface{}. This includes
20 // both limited fuzz testing for correctness and benchmarking.
21
22 // mapCollection is effectively a []map[uint64]interface{}.
23 // These support operations being applied to the i'th maps.
24 type mapCollection interface {
25 Elements() []map[uint64]any
26
27 DeepEqual(l, r int) bool
28 Lookup(id int, k uint64) (any, bool)
29
30 Insert(id int, k uint64, v any)
31 Update(id int, k uint64, v any)
32 Remove(id int, k uint64)
33 Intersect(l int, r int)
34 Merge(l int, r int)
35 Clear(id int)
36
37 Average(l int, r int)
38 Assign(l int, r int)
39 }
40
41 // opCode of an operation.
42 type opCode int
43
44 const (
45 deepEqualsOp opCode = iota
46 lookupOp
47 insert
48 update
49 remove
50 merge
51 intersect
52 clear
53 takeAverage
54 assign
55 )
56
57 func (op opCode) String() string {
58 switch op {
59 case deepEqualsOp:
60 return "DE"
61 case lookupOp:
62 return "LO"
63 case insert:
64 return "IN"
65 case update:
66 return "UP"
67 case remove:
68 return "RE"
69 case merge:
70 return "ME"
71 case intersect:
72 return "IT"
73 case clear:
74 return "CL"
75 case takeAverage:
76 return "AV"
77 case assign:
78 return "AS"
79 default:
80 return "??"
81 }
82 }
83
84 // A mapCollection backed by MutMaps.
85 type trieCollection struct {
86 b *trie.Builder
87 tries []trie.MutMap
88 }
89
90 func (c *trieCollection) Elements() []map[uint64]any {
91 var maps []map[uint64]any
92 for _, m := range c.tries {
93 maps = append(maps, trie.Elems(m.M))
94 }
95 return maps
96 }
97 func (c *trieCollection) Eq(id int, m map[uint64]any) bool {
98 elems := trie.Elems(c.tries[id].M)
99 return !reflect.DeepEqual(elems, m)
100 }
101
102 func (c *trieCollection) Lookup(id int, k uint64) (any, bool) {
103 return c.tries[id].M.Lookup(k)
104 }
105 func (c *trieCollection) DeepEqual(l, r int) bool {
106 return c.tries[l].M.DeepEqual(c.tries[r].M)
107 }
108
109 func (c *trieCollection) Add() {
110 c.tries = append(c.tries, c.b.MutEmpty())
111 }
112
113 func (c *trieCollection) Insert(id int, k uint64, v any) {
114 c.tries[id].Insert(k, v)
115 }
116
117 func (c *trieCollection) Update(id int, k uint64, v any) {
118 c.tries[id].Update(k, v)
119 }
120
121 func (c *trieCollection) Remove(id int, k uint64) {
122 c.tries[id].Remove(k)
123 }
124
125 func (c *trieCollection) Intersect(l int, r int) {
126 c.tries[l].Intersect(c.tries[r].M)
127 }
128
129 func (c *trieCollection) Merge(l int, r int) {
130 c.tries[l].Merge(c.tries[r].M)
131 }
132
133 func (c *trieCollection) Average(l int, r int) {
134 c.tries[l].MergeWith(average, c.tries[r].M)
135 }
136
137 func (c *trieCollection) Clear(id int) {
138 c.tries[id] = c.b.MutEmpty()
139 }
140 func (c *trieCollection) Assign(l, r int) {
141 c.tries[l] = c.tries[r]
142 }
143
144 func average(x any, y any) any {
145 if x, ok := x.(float32); ok {
146 if y, ok := y.(float32); ok {
147 return (x + y) / 2.0
148 }
149 }
150 return x
151 }
152
153 type builtinCollection []map[uint64]any
154
155 func (c builtinCollection) Elements() []map[uint64]any {
156 return c
157 }
158
159 func (c builtinCollection) Lookup(id int, k uint64) (any, bool) {
160 v, ok := c[id][k]
161 return v, ok
162 }
163 func (c builtinCollection) DeepEqual(l, r int) bool {
164 return reflect.DeepEqual(c[l], c[r])
165 }
166
167 func (c builtinCollection) Insert(id int, k uint64, v any) {
168 if _, ok := c[id][k]; !ok {
169 c[id][k] = v
170 }
171 }
172
173 func (c builtinCollection) Update(id int, k uint64, v any) {
174 c[id][k] = v
175 }
176
177 func (c builtinCollection) Remove(id int, k uint64) {
178 delete(c[id], k)
179 }
180
181 func (c builtinCollection) Intersect(l int, r int) {
182 result := map[uint64]any{}
183 for k, v := range c[l] {
184 if _, ok := c[r][k]; ok {
185 result[k] = v
186 }
187 }
188 c[l] = result
189 }
190
191 func (c builtinCollection) Merge(l int, r int) {
192 result := map[uint64]any{}
193 maps.Copy(result, c[r])
194 maps.Copy(result, c[l])
195 c[l] = result
196 }
197
198 func (c builtinCollection) Average(l int, r int) {
199 avg := map[uint64]any{}
200 for k, lv := range c[l] {
201 if rv, ok := c[r][k]; ok {
202 avg[k] = average(lv, rv)
203 } else {
204 avg[k] = lv // add elements just in l
205 }
206 }
207 for k, rv := range c[r] {
208 if _, ok := c[l][k]; !ok {
209 avg[k] = rv // add elements just in r
210 }
211 }
212 c[l] = avg
213 }
214
215 func (c builtinCollection) Assign(l, r int) {
216 m := map[uint64]any{}
217 maps.Copy(m, c[r])
218 c[l] = m
219 }
220
221 func (c builtinCollection) Clear(id int) {
222 c[id] = map[uint64]any{}
223 }
224
225 func newTriesCollection(size int) *trieCollection {
226 tc := &trieCollection{
227 b: trie.NewBuilder(),
228 tries: make([]trie.MutMap, size),
229 }
230 for i := range size {
231 tc.tries[i] = tc.b.MutEmpty()
232 }
233 return tc
234 }
235
236 func newMapsCollection(size int) *builtinCollection {
237 maps := make(builtinCollection, size)
238 for i := range size {
239 maps[i] = map[uint64]any{}
240 }
241 return &maps
242 }
243
244 // operation on a map collection.
245 type operation struct {
246 code opCode
247 l, r int
248 k uint64
249 v float32
250 }
251
252 // Apply the operation to maps.
253 func (op operation) Apply(maps mapCollection) any {
254 type lookupresult struct {
255 v any
256 ok bool
257 }
258 switch op.code {
259 case deepEqualsOp:
260 return maps.DeepEqual(op.l, op.r)
261 case lookupOp:
262 v, ok := maps.Lookup(op.l, op.k)
263 return lookupresult{v, ok}
264 case insert:
265 maps.Insert(op.l, op.k, op.v)
266 case update:
267 maps.Update(op.l, op.k, op.v)
268 case remove:
269 maps.Remove(op.l, op.k)
270 case merge:
271 maps.Merge(op.l, op.r)
272 case intersect:
273 maps.Intersect(op.l, op.r)
274 case clear:
275 maps.Clear(op.l)
276 case takeAverage:
277 maps.Average(op.l, op.r)
278 case assign:
279 maps.Assign(op.l, op.r)
280 }
281 return nil
282 }
283
284 // Returns a collection of op codes with dist[op] copies of op.
285 func distribution(dist map[opCode]int) []opCode {
286 var codes []opCode
287 for op, n := range dist {
288 for range n {
289 codes = append(codes, op)
290 }
291 }
292 return codes
293 }
294
295 // options for generating a random operation.
296 type options struct {
297 maps int
298 maxKey uint64
299 maxVal int
300 codes []opCode
301 }
302
303 // returns a random operation using r as a source of randomness.
304 func randOperator(r *rand.Rand, opts options) operation {
305 id := func() int { return r.Intn(opts.maps) }
306 key := func() uint64 { return r.Uint64() % opts.maxKey }
307 val := func() float32 { return float32(r.Intn(opts.maxVal)) }
308 switch code := opts.codes[r.Intn(len(opts.codes))]; code {
309 case lookupOp, remove:
310 return operation{code: code, l: id(), k: key()}
311 case insert, update:
312 return operation{code: code, l: id(), k: key(), v: val()}
313 case deepEqualsOp, merge, intersect, takeAverage, assign:
314 return operation{code: code, l: id(), r: id()}
315 case clear:
316 return operation{code: code, l: id()}
317 default:
318 panic("Invalid op code")
319 }
320 }
321
322 func randOperators(r *rand.Rand, numops int, opts options) []operation {
323 ops := make([]operation, numops)
324 for i := range numops {
325 ops[i] = randOperator(r, opts)
326 }
327 return ops
328 }
329
330 // TestOperations applies a series of random operations to collection of
331 // trie.MutMaps and map[uint64]interface{}. It tests for the maps being equal.
332 func TestOperations(t *testing.T) {
333 seed := time.Now().UnixNano()
334 s := rand.NewSource(seed)
335 r := rand.New(s)
336 t.Log("seed: ", seed)
337
338 size := 10
339 N := 100000
340 ops := randOperators(r, N, options{
341 maps: size,
342 maxKey: 128,
343 maxVal: 100,
344 codes: distribution(map[opCode]int{
345 deepEqualsOp: 1,
346 lookupOp: 10,
347 insert: 10,
348 update: 10,
349 remove: 10,
350 merge: 10,
351 intersect: 10,
352 clear: 2,
353 takeAverage: 5,
354 assign: 5,
355 }),
356 })
357
358 var tries mapCollection = newTriesCollection(size)
359 var maps mapCollection = newMapsCollection(size)
360 check := func() error {
361 if got, want := tries.Elements(), maps.Elements(); !reflect.DeepEqual(got, want) {
362 return fmt.Errorf("elements of tries and maps and tries differed. got %v want %v", got, want)
363 }
364 return nil
365 }
366
367 for i, op := range ops {
368 got, want := op.Apply(tries), op.Apply(maps)
369 if got != want {
370 t.Errorf("op[%d]: (%v).Apply(%v) != (%v).Apply(%v). got %v want %v",
371 i, op, tries, op, maps, got, want)
372 }
373 }
374 if err := check(); err != nil {
375 t.Errorf("%d operators failed with %s", size, err)
376 t.Log("Rerunning with more checking")
377 tries, maps = newTriesCollection(size), newMapsCollection(size)
378 for i, op := range ops {
379 op.Apply(tries)
380 op.Apply(maps)
381 if err := check(); err != nil {
382 t.Fatalf("Failed first on op[%d]=%v: %v", i, op, err)
383 }
384 }
385 }
386 }
387
388 func run(b *testing.B, opts options, seed int64, mk func(int) mapCollection) {
389 r := rand.New(rand.NewSource(seed))
390 ops := randOperators(r, b.N, opts)
391 maps := mk(opts.maps)
392 for _, op := range ops {
393 op.Apply(maps)
394 }
395 }
396
397 var standard options = options{
398 maps: 10,
399 maxKey: 128,
400 maxVal: 100,
401 codes: distribution(map[opCode]int{
402 deepEqualsOp: 1,
403 lookupOp: 20,
404 insert: 20,
405 update: 20,
406 remove: 20,
407 merge: 10,
408 intersect: 10,
409 clear: 1,
410 takeAverage: 5,
411 assign: 20,
412 }),
413 }
414
415 func BenchmarkTrieStandard(b *testing.B) {
416 run(b, standard, 123, func(size int) mapCollection {
417 return newTriesCollection(size)
418 })
419 }
420
421 func BenchmarkMapsStandard(b *testing.B) {
422 run(b, standard, 123, func(size int) mapCollection {
423 return newMapsCollection(size)
424 })
425 }
426
427 var smallWide options = options{
428 maps: 100,
429 maxKey: 100,
430 maxVal: 8,
431 codes: distribution(map[opCode]int{
432 deepEqualsOp: 0,
433 lookupOp: 0,
434 insert: 30,
435 update: 20,
436 remove: 0,
437 merge: 10,
438 intersect: 0,
439 clear: 1,
440 takeAverage: 0,
441 assign: 30,
442 }),
443 }
444
445 func BenchmarkTrieSmallWide(b *testing.B) {
446 run(b, smallWide, 456, func(size int) mapCollection {
447 return newTriesCollection(size)
448 })
449 }
450
451 func BenchmarkMapsSmallWide(b *testing.B) {
452 run(b, smallWide, 456, func(size int) mapCollection {
453 return newMapsCollection(size)
454 })
455 }