@@ -7,7 +7,7 @@ package lcs
77import (
88 "fmt"
99 "log"
10- "math/rand"
10+ "math/rand/v2 "
1111 "os"
1212 "strings"
1313 "testing"
@@ -106,15 +106,16 @@ func TestRegressionOld003(t *testing.T) {
106106}
107107
108108func 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 {
111112 // TODO(adonovan): use ASCII and bytesSeqs here? The use of
112113 // 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 ))
115116 seq := runesSeqs {a , b }
116117
117- const lim = 24 // large enough to get true lcs
118+ const lim = 0 // make sure we get the lcs (24 was too small)
118119 _ , forw := compute (seq , forward , lim )
119120 _ , back := compute (seq , backward , lim )
120121 _ , two := compute (seq , twosided , lim )
@@ -158,7 +159,7 @@ func TestDiffAPI(t *testing.T) {
158159}
159160
160161func BenchmarkTwoOld (b * testing.B ) {
161- tests := genBench ("abc" , 96 )
162+ tests := genBench (rng ( b ), "abc" , 96 )
162163 for i := 0 ; i < b .N ; i ++ {
163164 for _ , tt := range tests {
164165 _ , two := compute (stringSeqs {tt .before , tt .after }, twosided , 100 )
@@ -170,7 +171,7 @@ func BenchmarkTwoOld(b *testing.B) {
170171}
171172
172173func BenchmarkForwOld (b * testing.B ) {
173- tests := genBench ("abc" , 96 )
174+ tests := genBench (rng ( b ), "abc" , 96 )
174175 for i := 0 ; i < b .N ; i ++ {
175176 for _ , tt := range tests {
176177 _ , two := compute (stringSeqs {tt .before , tt .after }, forward , 100 )
@@ -181,14 +182,21 @@ func BenchmarkForwOld(b *testing.B) {
181182 }
182183}
183184
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 } {
185194 // before and after for benchmarks. 24 strings of length n with
186195 // before and after differing at least once, and about 5%
187- rand .Seed (3 )
188196 var ans []struct { before , after string }
189- for i := 0 ; i < 24 ; i ++ {
197+ for range 24 {
190198 // maybe b should have an approximately known number of diffs
191- a := randstr (set , n )
199+ a := randstr (rng , set , n )
192200 cnt := 0
193201 bb := make ([]rune , 0 , n )
194202 for _ , r := range a {
0 commit comments