Debian Codegraph @

gopls/internal/util/lru/lru_fuzz_test.go

raw symbols SCIP
1 // Copyright 2023 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 lru_test
6
7 import (
8 "testing"
9
10 "golang.org/x/tools/gopls/internal/util/lru"
11 )
12
13 // Simple fuzzing test for consistency.
14 func FuzzCache(f *testing.F) {
15 type op struct {
16 set bool
17 key, value byte
18 }
19 f.Fuzz(func(t *testing.T, data []byte) {
20 var ops []op
21 for len(data) >= 3 {
22 ops = append(ops, op{data[0]%2 == 0, data[1], data[2]})
23 data = data[3:]
24 }
25 cache := lru.New[byte, byte](100)
26 var reference [256]byte
27 for _, op := range ops {
28 if op.set {
29 reference[op.key] = op.value
30 cache.Set(op.key, op.value, 1)
31 } else {
32 if v, ok := cache.Get(op.key); ok && v != reference[op.key] {
33 t.Fatalf("cache.Get(%d) = %d, want %d", op.key, v, reference[op.key])
34 }
35 }
36 }
37 })
38 }