| 1 |
// This is just a temporary fork of https://github.com/josharian/vitter (ISC License, https://github.com/josharian/vitter/blob/main/LICENSE) |
| 2 |
// |
| 3 |
// This file will be removed once https://github.com/josharian/vitter/issues/1 is resolved. |
| 4 |
|
| 5 |
package collections |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"math/rand/v2" |
| 10 |
"reflect" |
| 11 |
"testing" |
| 12 |
"time" |
| 13 |
) |
| 14 |
|
| 15 |
var goldenTests = []struct { |
| 16 |
seed int64 |
| 17 |
k, max int |
| 18 |
want []int |
| 19 |
}{ |
| 20 |
{2, 10, 100, []int{6, 20, 34, 45, 58, 59, 64, 69, 70, 72}}, |
| 21 |
{3, 10, 100, []int{8, 11, 22, 26, 30, 40, 74, 76, 93, 95}}, |
| 22 |
{4, 5, 1000, []int{183, 283, 443, 501, 531}}, |
| 23 |
{5, 15, 100000, []int{12984, 17778, 20370, 23830, 27120, 33258, 45718, 50064, 57096, 58580, 80960, 84396, 84594, 95561, 97687}}, |
| 24 |
} |
| 25 |
|
| 26 |
func TestGolden(t *testing.T) { |
| 27 |
for _, test := range goldenTests { |
| 28 |
prng := rand.New(rand.NewPCG(uint64(test.seed), 0)) |
| 29 |
var got []int |
| 30 |
testD(prng, t, test.k, test.max, func(n int) { |
| 31 |
got = append(got, n) |
| 32 |
}) |
| 33 |
if !reflect.DeepEqual(got, test.want) { |
| 34 |
t.Errorf("golden(%d, %d, %d) = %#v want %#v", test.seed, test.k, test.max, got, test.want) |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func TestInspectCounts(t *testing.T) { |
| 40 |
prng := rand.New(rand.NewPCG(uint64(time.Now().UnixNano()), uint64(time.Now().UnixNano()))) |
| 41 |
const max = 100 |
| 42 |
const k = 10 |
| 43 |
const iters = 10000 |
| 44 |
counts := make([]int, max) |
| 45 |
for range iters { |
| 46 |
testD(prng, t, k, max, func(n int) { |
| 47 |
counts[n]++ |
| 48 |
}) |
| 49 |
} |
| 50 |
for i := range counts { |
| 51 |
counts[i] -= (iters * k / max) |
| 52 |
} |
| 53 |
t.Log(counts) |
| 54 |
} |
| 55 |
|
| 56 |
func testD(prng *rand.Rand, tb testing.TB, want, max int, choose func(n int)) { |
| 57 |
prev := -1 |
| 58 |
got := want |
| 59 |
_d(prng, want, max, func(x int) { |
| 60 |
if x <= prev { |
| 61 |
tb.Fatalf("backwards: %d then %d", prev, x) |
| 62 |
} |
| 63 |
if x < 0 || x >= max { |
| 64 |
tb.Fatalf("bad selection: %d", x) |
| 65 |
} |
| 66 |
prev = x |
| 67 |
got-- |
| 68 |
if got < 0 { |
| 69 |
tb.Fatal("choose called too many times") |
| 70 |
} |
| 71 |
choose(x) |
| 72 |
}) |
| 73 |
if got != 0 { |
| 74 |
tb.Fatal("choose not called enough times") |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestWantIsMax(t *testing.T) { |
| 79 |
// Ensure that when want == max, we get all indices. |
| 80 |
prng := rand.New(rand.NewPCG(uint64(time.Now().UnixNano()), uint64(time.Now().UnixNano()))) |
| 81 |
const n = 10000 |
| 82 |
testD(prng, t, n, n, func(n int) {}) |
| 83 |
} |
| 84 |
|
| 85 |
func BenchmarkD(b *testing.B) { |
| 86 |
prng := rand.New(rand.NewPCG(uint64(time.Now().UnixNano()), uint64(time.Now().UnixNano()))) |
| 87 |
// TODO: count rng calls? |
| 88 |
for _, want := range []int{1, 100, 10000} { |
| 89 |
b.Run(fmt.Sprintf("want=%d", want), func(b *testing.B) { |
| 90 |
for b.Loop() { |
| 91 |
_d(prng, want, 1000000, func(int) {}) |
| 92 |
} |
| 93 |
}) |
| 94 |
} |
| 95 |
} |