|
| 1 | +//go:build go1.18 |
| 2 | +// +build go1.18 |
| 3 | + |
| 4 | +package weightedrand |
| 5 | + |
| 6 | +import ( |
| 7 | + "encoding/binary" |
| 8 | + "errors" |
| 9 | + "fmt" |
| 10 | + "reflect" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +// Fuzz testing does not support slices as a corpus type in go1.18, thus we |
| 15 | +// write a bunch of boilerplate here to allow us to encode []uint64 as []byte |
| 16 | +// for kicks. |
| 17 | + |
| 18 | +func bEncodeSlice(xs []uint64) []byte { |
| 19 | + bs := make([]byte, len(xs)*8) |
| 20 | + for i, x := range xs { |
| 21 | + n := i * 8 |
| 22 | + binary.LittleEndian.PutUint64(bs[n:], x) |
| 23 | + } |
| 24 | + return bs |
| 25 | +} |
| 26 | + |
| 27 | +func bDecodeSlice(bs []byte) []uint64 { |
| 28 | + n := len(bs) / 8 |
| 29 | + xs := make([]uint64, 0, n) |
| 30 | + for i := 0; i < n; i++ { |
| 31 | + x := binary.LittleEndian.Uint64(bs[8*i:]) |
| 32 | + xs = append(xs, x) |
| 33 | + } |
| 34 | + return xs |
| 35 | +} |
| 36 | + |
| 37 | +// test our own encoder to make sure we didn't introduce errors. |
| 38 | +func Test_bEncodeSlice(t *testing.T) { |
| 39 | + var testcases = [][]uint64{ |
| 40 | + {}, |
| 41 | + {1}, |
| 42 | + {42}, |
| 43 | + {912346}, |
| 44 | + {1, 2}, |
| 45 | + {1, 1, 1}, |
| 46 | + {1, 2, 3}, |
| 47 | + {1, 1000000}, |
| 48 | + {1, 2, 3, 4, 5, 6, 7, 8, 9}, |
| 49 | + } |
| 50 | + for _, tc := range testcases { |
| 51 | + t.Run(fmt.Sprintf("%v", tc), func(t *testing.T) { |
| 52 | + before := tc |
| 53 | + encoded := bEncodeSlice(before) |
| 54 | + if want, got := len(before)*8, len(encoded); want != got { |
| 55 | + t.Errorf("encoded length not as expected: want %d got %d", want, got) |
| 56 | + } |
| 57 | + decoded := bDecodeSlice(encoded) |
| 58 | + if !reflect.DeepEqual(before, decoded) { |
| 59 | + t.Errorf("want %v got %v", before, decoded) |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func FuzzNewChooser(f *testing.F) { |
| 66 | + var fuzzcases = [][]uint64{ |
| 67 | + {}, |
| 68 | + {0}, |
| 69 | + {1}, |
| 70 | + {1, 1}, |
| 71 | + {1, 2, 3}, |
| 72 | + {0, 1, 2}, |
| 73 | + } |
| 74 | + for _, tc := range fuzzcases { |
| 75 | + f.Add(bEncodeSlice(tc)) |
| 76 | + } |
| 77 | + |
| 78 | + f.Fuzz(func(t *testing.T, encodedWeights []byte) { |
| 79 | + weights := bDecodeSlice(encodedWeights) |
| 80 | + const sentinel = 1 |
| 81 | + |
| 82 | + cs := make([]Choice[int, uint64], 0, len(weights)) |
| 83 | + for _, w := range weights { |
| 84 | + cs = append(cs, Choice[int, uint64]{Item: sentinel, Weight: w}) |
| 85 | + } |
| 86 | + |
| 87 | + // fuzz for error or panic on NewChooser |
| 88 | + c, err := NewChooser(cs...) |
| 89 | + if err != nil && !errors.Is(err, errNoValidChoices) && !errors.Is(err, errWeightOverflow) { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + |
| 93 | + if err == nil { |
| 94 | + result := c.Pick() // fuzz for panic on Panic |
| 95 | + if result != sentinel { // fuzz for returned value unexpected (just use same non-zero sentinel value for all choices) |
| 96 | + t.Fatalf("expected %v got %v", sentinel, result) |
| 97 | + } |
| 98 | + } |
| 99 | + }) |
| 100 | +} |
0 commit comments