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