-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeterminism_test.go
More file actions
52 lines (48 loc) · 1.82 KB
/
Copy pathdeterminism_test.go
File metadata and controls
52 lines (48 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package streamhash
import (
"bytes"
"context"
"math/rand/v2"
"os"
"path/filepath"
"testing"
)
// TestBuildDeterminismAndGlobalSeed verifies that builds are reproducible and
// that WithGlobalSeed actually controls the output for BOTH algorithms. PTRHash
// was previously non-deterministic — its Phase-2 eviction RNG drew from the
// process-global generator and ignored WithGlobalSeed — so the same inputs could
// produce different indexes run to run. This guards that fix:
// - same seed -> byte-identical output (reproducible)
// - other seed -> different output (the seed is actually used)
func TestBuildDeterminismAndGlobalSeed(t *testing.T) {
rng := rand.New(rand.NewPCG(99, 1234))
keys := generateRandomKeys(rng, 5000, 16)
buildWithSeed := func(t *testing.T, algo Algorithm, seed uint64) []byte {
t.Helper()
// quickBuild (test_helpers_test.go) copies + block-sorts the keys and
// builds a sorted index; we just pass the algorithm/seed/workers options
// and read the resulting bytes.
p := filepath.Join(t.TempDir(), "idx")
if err := quickBuild(context.Background(), p, keys, WithAlgorithm(algo), WithGlobalSeed(seed), WithWorkers(4)); err != nil {
t.Fatalf("quickBuild: %v", err)
}
data, err := os.ReadFile(p)
if err != nil {
t.Fatalf("ReadFile: %v", err)
}
return data
}
for _, algo := range []Algorithm{AlgoBijection, AlgoPTRHash} {
t.Run(algo.String(), func(t *testing.T) {
a1 := buildWithSeed(t, algo, 0xA5A5A5A5)
a2 := buildWithSeed(t, algo, 0xA5A5A5A5)
if !bytes.Equal(a1, a2) {
t.Fatalf("%s: identical inputs + same WithGlobalSeed produced different output (not reproducible)", algo)
}
other := buildWithSeed(t, algo, 0x12345678)
if bytes.Equal(a1, other) {
t.Fatalf("%s: a different WithGlobalSeed produced identical output (seed is ignored)", algo)
}
})
}
}