@@ -7,7 +7,7 @@ package lcs
7
7
import (
8
8
"fmt"
9
9
"log"
10
- "math/rand"
10
+ "math/rand/v2 "
11
11
"os"
12
12
"strings"
13
13
"testing"
@@ -106,15 +106,16 @@ func TestRegressionOld003(t *testing.T) {
106
106
}
107
107
108
108
func TestRandOld (t * testing.T ) {
109
- rand .Seed (1 )
110
- for i := 0 ; i < 1000 ; i ++ {
109
+ rng := rng (t )
110
+
111
+ for i := range 1000 {
111
112
// TODO(adonovan): use ASCII and bytesSeqs here? The use of
112
113
// non-ASCII isn't relevant to the property exercised by the test.
113
- a := []rune (randstr ("abω" , 16 ))
114
- b := []rune (randstr ("abωc" , 16 ))
114
+ a := []rune (randstr (rng , "abω" , 16 ))
115
+ b := []rune (randstr (rng , "abωc" , 16 ))
115
116
seq := runesSeqs {a , b }
116
117
117
- const lim = 24 // large enough to get true lcs
118
+ const lim = 0 // make sure we get the lcs (24 was too small)
118
119
_ , forw := compute (seq , forward , lim )
119
120
_ , back := compute (seq , backward , lim )
120
121
_ , two := compute (seq , twosided , lim )
@@ -158,7 +159,7 @@ func TestDiffAPI(t *testing.T) {
158
159
}
159
160
160
161
func BenchmarkTwoOld (b * testing.B ) {
161
- tests := genBench ("abc" , 96 )
162
+ tests := genBench (rng ( b ), "abc" , 96 )
162
163
for i := 0 ; i < b .N ; i ++ {
163
164
for _ , tt := range tests {
164
165
_ , two := compute (stringSeqs {tt .before , tt .after }, twosided , 100 )
@@ -170,7 +171,7 @@ func BenchmarkTwoOld(b *testing.B) {
170
171
}
171
172
172
173
func BenchmarkForwOld (b * testing.B ) {
173
- tests := genBench ("abc" , 96 )
174
+ tests := genBench (rng ( b ), "abc" , 96 )
174
175
for i := 0 ; i < b .N ; i ++ {
175
176
for _ , tt := range tests {
176
177
_ , two := compute (stringSeqs {tt .before , tt .after }, forward , 100 )
@@ -181,14 +182,21 @@ func BenchmarkForwOld(b *testing.B) {
181
182
}
182
183
}
183
184
184
- func genBench (set string , n int ) []struct { before , after string } {
185
+ // rng returns a randomly initialized PRNG whose seeds are logged so
186
+ // that occasional test failures can be deterministically replayed.
187
+ func rng (tb testing.TB ) * rand.Rand {
188
+ seed1 , seed2 := rand .Uint64 (), rand .Uint64 ()
189
+ tb .Logf ("PRNG seeds: %d, %d" , seed1 , seed2 )
190
+ return rand .New (rand .NewPCG (seed1 , seed2 ))
191
+ }
192
+
193
+ func genBench (rng * rand.Rand , set string , n int ) []struct { before , after string } {
185
194
// before and after for benchmarks. 24 strings of length n with
186
195
// before and after differing at least once, and about 5%
187
- rand .Seed (3 )
188
196
var ans []struct { before , after string }
189
- for i := 0 ; i < 24 ; i ++ {
197
+ for range 24 {
190
198
// maybe b should have an approximately known number of diffs
191
- a := randstr (set , n )
199
+ a := randstr (rng , set , n )
192
200
cnt := 0
193
201
bb := make ([]rune , 0 , n )
194
202
for _ , r := range a {
0 commit comments