Skip to content

Commit

Permalink
Update test to remove bad rand usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jefferai committed Oct 29, 2022
1 parent fa11c6b commit f2408b3
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions 2q_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package lru

import (
"math/rand"
"crypto/rand"
"math"
"math/big"
"testing"
)

func getRand(tb testing.TB) int64 {
out, err := rand.Int(rand.Reader, big.NewInt(math.MaxInt64))
if err != nil {
tb.Fatal(err)
}
return out.Int64()
}

func Benchmark2Q_Rand(b *testing.B) {
l, err := New2Q(8192)
if err != nil {
Expand All @@ -13,7 +23,7 @@ func Benchmark2Q_Rand(b *testing.B) {

trace := make([]int64, b.N*2)
for i := 0; i < b.N*2; i++ {
trace[i] = rand.Int63() % 32768
trace[i] = getRand(b) % 32768
}

b.ResetTimer()
Expand Down Expand Up @@ -43,9 +53,9 @@ func Benchmark2Q_Freq(b *testing.B) {
trace := make([]int64, b.N*2)
for i := 0; i < b.N*2; i++ {
if i%2 == 0 {
trace[i] = rand.Int63() % 16384
trace[i] = getRand(b) % 16384
} else {
trace[i] = rand.Int63() % 32768
trace[i] = getRand(b) % 32768
}
}

Expand Down Expand Up @@ -75,8 +85,8 @@ func Test2Q_RandomOps(t *testing.T) {

n := 200000
for i := 0; i < n; i++ {
key := rand.Int63() % 512
r := rand.Int63()
key := getRand(t) % 512
r := getRand(t)
switch r % 3 {
case 0:
l.Add(key, key)
Expand Down

0 comments on commit f2408b3

Please sign in to comment.