1
0
mirror of https://github.com/golang/go synced 2024-11-17 11:34:48 -07:00

sort: use math/rand/v2 in tests and benchmarks

This allows to drop the use of deprecated rand.Seed in benchmarks.

Fixes #61142

Change-Id: I628848c0d4a500dc9a361930c393e47c3361a95c
Reviewed-on: https://go-review.googlesource.com/c/go/+/551155
Reviewed-by: Eli Bendersky <eliben@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Tobias Klauser 2024-01-23 17:36:06 +01:00 committed by Gopher Robot
parent 643d816c8b
commit 7c4ee1b165
2 changed files with 15 additions and 15 deletions

View File

@ -5,7 +5,7 @@
package sort_test
import (
"math/rand"
"math/rand/v2"
"slices"
. "sort"
"strconv"
@ -18,10 +18,10 @@ import (
// package).
func makeRandomInts(n int) []int {
rand.Seed(42)
r := rand.New(rand.NewPCG(42, 0))
ints := make([]int, n)
for i := 0; i < n; i++ {
ints[i] = rand.Intn(n)
ints[i] = r.IntN(n)
}
return ints
}
@ -92,14 +92,14 @@ func BenchmarkSlicesIsSorted(b *testing.B) {
// makeRandomStrings generates n random strings with alphabetic runes of
// varying lengths.
func makeRandomStrings(n int) []string {
rand.Seed(42)
r := rand.New(rand.NewPCG(42, 0))
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
ss := make([]string, n)
for i := 0; i < n; i++ {
var sb stringspkg.Builder
slen := 2 + rand.Intn(50)
slen := 2 + r.IntN(50)
for j := 0; j < slen; j++ {
sb.WriteRune(letters[rand.Intn(len(letters))])
sb.WriteRune(letters[r.IntN(len(letters))])
}
ss[i] = sb.String()
}
@ -156,10 +156,10 @@ func (s myStructs) Less(i, j int) bool { return s[i].n < s[j].n }
func (s myStructs) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func makeRandomStructs(n int) myStructs {
rand.Seed(42)
r := rand.New(rand.NewPCG(42, 0))
structs := make([]*myStruct, n)
for i := 0; i < n; i++ {
structs[i] = &myStruct{n: rand.Intn(n)}
structs[i] = &myStruct{n: r.IntN(n)}
}
return structs
}

View File

@ -9,7 +9,7 @@ import (
"fmt"
"internal/testenv"
"math"
"math/rand"
"math/rand/v2"
"slices"
. "sort"
"strconv"
@ -110,7 +110,7 @@ func TestSortLarge_Random(t *testing.T) {
}
data := make([]int, n)
for i := 0; i < len(data); i++ {
data[i] = rand.Intn(100)
data[i] = rand.IntN(100)
}
if IntsAreSorted(data) {
t.Fatalf("terrible rand.rand")
@ -198,7 +198,7 @@ func TestNonDeterministicComparison(t *testing.T) {
}()
td := &nonDeterministicTestingData{
r: rand.New(rand.NewSource(0)),
r: rand.New(rand.NewPCG(0, 0)),
}
for i := 0; i < 10; i++ {
@ -442,13 +442,13 @@ func testBentleyMcIlroy(t *testing.T, sort func(Interface), maxswap func(int) in
case _Sawtooth:
data[i] = i % m
case _Rand:
data[i] = rand.Intn(m)
data[i] = rand.IntN(m)
case _Stagger:
data[i] = (i*m + i) % n
case _Plateau:
data[i] = min(i, m)
case _Shuffle:
if rand.Intn(m) != 0 {
if rand.IntN(m) != 0 {
j += 2
data[i] = j
} else {
@ -648,7 +648,7 @@ func TestStability(t *testing.T) {
// random distribution
for i := 0; i < len(data); i++ {
data[i].a = rand.Intn(m)
data[i].a = rand.IntN(m)
}
if IsSorted(data) {
t.Fatalf("terrible rand.rand")
@ -704,7 +704,7 @@ func countOps(t *testing.T, algo func(Interface), name string) {
maxswap: 1<<31 - 1,
}
for i := 0; i < n; i++ {
td.data[i] = rand.Intn(n / 5)
td.data[i] = rand.IntN(n / 5)
}
algo(&td)
t.Logf("%s %8d elements: %11d Swap, %10d Less", name, n, td.nswap, td.ncmp)