lib/encoding: make deterministic tests which rely on math/rand

Fixes https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3683
This commit is contained in:
Aliaksandr Valialkin 2023-01-23 18:41:07 -08:00
parent a7f8ce5e3d
commit 3d1cb011b6
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
8 changed files with 44 additions and 26 deletions

View file

@ -9,9 +9,10 @@ func TestCompressDecompressZSTD(t *testing.T) {
testCompressDecompressZSTD(t, []byte("a")) testCompressDecompressZSTD(t, []byte("a"))
testCompressDecompressZSTD(t, []byte("foobarbaz")) testCompressDecompressZSTD(t, []byte("foobarbaz"))
r := rand.New(rand.NewSource(1))
var b []byte var b []byte
for i := 0; i < 64*1024; i++ { for i := 0; i < 64*1024; i++ {
b = append(b, byte(rand.Int31n(256))) b = append(b, byte(r.Int31n(256)))
} }
testCompressDecompressZSTD(t, b) testCompressDecompressZSTD(t, b)
} }

View file

@ -9,6 +9,8 @@ import (
) )
func TestMarshalUnmarshalInt64Array(t *testing.T) { func TestMarshalUnmarshalInt64Array(t *testing.T) {
r := rand.New(rand.NewSource(1))
var va []int64 var va []int64
var v int64 var v int64
@ -16,7 +18,7 @@ func TestMarshalUnmarshalInt64Array(t *testing.T) {
va = va[:0] va = va[:0]
v = 0 v = 0
for i := 0; i < 8*1024; i++ { for i := 0; i < 8*1024; i++ {
v += int64(rand.NormFloat64() * 1e6) v += int64(r.NormFloat64() * 1e6)
va = append(va, v) va = append(va, v)
} }
for precisionBits := uint8(1); precisionBits < 23; precisionBits++ { for precisionBits := uint8(1); precisionBits < 23; precisionBits++ {
@ -30,7 +32,7 @@ func TestMarshalUnmarshalInt64Array(t *testing.T) {
va = va[:0] va = va[:0]
v = 0 v = 0
for i := 0; i < 8*1024; i++ { for i := 0; i < 8*1024; i++ {
v += 30e6 + int64(rand.NormFloat64()*1e6) v += 30e6 + int64(r.NormFloat64()*1e6)
va = append(va, v) va = append(va, v)
} }
for precisionBits := uint8(1); precisionBits < 24; precisionBits++ { for precisionBits := uint8(1); precisionBits < 24; precisionBits++ {
@ -44,7 +46,7 @@ func TestMarshalUnmarshalInt64Array(t *testing.T) {
va = va[:0] va = va[:0]
v = 1000 v = 1000
for i := 0; i < 6; i++ { for i := 0; i < 6; i++ {
v += int64(rand.NormFloat64() * 100) v += int64(r.NormFloat64() * 100)
va = append(va, v) va = append(va, v)
} }
for precisionBits := uint8(1); precisionBits < 65; precisionBits++ { for precisionBits := uint8(1); precisionBits < 65; precisionBits++ {
@ -55,7 +57,7 @@ func TestMarshalUnmarshalInt64Array(t *testing.T) {
va = va[:0] va = va[:0]
v = 0 v = 0
for i := 0; i < 6; i++ { for i := 0; i < 6; i++ {
v += 3000 + int64(rand.NormFloat64()*100) v += 3000 + int64(r.NormFloat64()*100)
va = append(va, v) va = append(va, v)
} }
for precisionBits := uint8(5); precisionBits < 65; precisionBits++ { for precisionBits := uint8(5); precisionBits < 65; precisionBits++ {
@ -64,11 +66,13 @@ func TestMarshalUnmarshalInt64Array(t *testing.T) {
} }
func TestMarshalInt64ArraySize(t *testing.T) { func TestMarshalInt64ArraySize(t *testing.T) {
r := rand.New(rand.NewSource(1))
var va []int64 var va []int64
v := int64(rand.Float64() * 1e9) v := int64(r.Float64() * 1e9)
for i := 0; i < 8*1024; i++ { for i := 0; i < 8*1024; i++ {
va = append(va, v) va = append(va, v)
v += 30e3 + int64(rand.NormFloat64()*1e3) v += 30e3 + int64(r.NormFloat64()*1e3)
} }
testMarshalInt64ArraySize(t, va, 1, 180, 1400) testMarshalInt64ArraySize(t, va, 1, 180, 1400)

View file

@ -9,6 +9,8 @@ import (
) )
func TestMarshalUnmarshalInt64Array(t *testing.T) { func TestMarshalUnmarshalInt64Array(t *testing.T) {
r := rand.New(rand.NewSource(1))
var va []int64 var va []int64
var v int64 var v int64
@ -16,7 +18,7 @@ func TestMarshalUnmarshalInt64Array(t *testing.T) {
va = va[:0] va = va[:0]
v = 0 v = 0
for i := 0; i < 8*1024; i++ { for i := 0; i < 8*1024; i++ {
v += int64(rand.NormFloat64() * 1e6) v += int64(r.NormFloat64() * 1e6)
va = append(va, v) va = append(va, v)
} }
for precisionBits := uint8(1); precisionBits < 17; precisionBits++ { for precisionBits := uint8(1); precisionBits < 17; precisionBits++ {
@ -30,7 +32,7 @@ func TestMarshalUnmarshalInt64Array(t *testing.T) {
va = va[:0] va = va[:0]
v = 0 v = 0
for i := 0; i < 8*1024; i++ { for i := 0; i < 8*1024; i++ {
v += 30e6 + int64(rand.NormFloat64()*1e6) v += 30e6 + int64(r.NormFloat64()*1e6)
va = append(va, v) va = append(va, v)
} }
for precisionBits := uint8(1); precisionBits < 15; precisionBits++ { for precisionBits := uint8(1); precisionBits < 15; precisionBits++ {
@ -44,7 +46,7 @@ func TestMarshalUnmarshalInt64Array(t *testing.T) {
va = va[:0] va = va[:0]
v = 1000 v = 1000
for i := 0; i < 6; i++ { for i := 0; i < 6; i++ {
v += int64(rand.NormFloat64() * 100) v += int64(r.NormFloat64() * 100)
va = append(va, v) va = append(va, v)
} }
for precisionBits := uint8(1); precisionBits < 65; precisionBits++ { for precisionBits := uint8(1); precisionBits < 65; precisionBits++ {
@ -55,7 +57,7 @@ func TestMarshalUnmarshalInt64Array(t *testing.T) {
va = va[:0] va = va[:0]
v = 0 v = 0
for i := 0; i < 6; i++ { for i := 0; i < 6; i++ {
v += 3000 + int64(rand.NormFloat64()*100) v += 3000 + int64(r.NormFloat64()*100)
va = append(va, v) va = append(va, v)
} }
for precisionBits := uint8(5); precisionBits < 65; precisionBits++ { for precisionBits := uint8(5); precisionBits < 65; precisionBits++ {
@ -64,11 +66,13 @@ func TestMarshalUnmarshalInt64Array(t *testing.T) {
} }
func TestMarshalInt64ArraySize(t *testing.T) { func TestMarshalInt64ArraySize(t *testing.T) {
r := rand.New(rand.NewSource(1))
var va []int64 var va []int64
v := int64(rand.Float64() * 1e9) v := int64(r.Float64() * 1e9)
for i := 0; i < 8*1024; i++ { for i := 0; i < 8*1024; i++ {
va = append(va, v) va = append(va, v)
v += 30e3 + int64(rand.NormFloat64()*1e3) v += 30e3 + int64(r.NormFloat64()*1e3)
} }
testMarshalInt64ArraySize(t, va, 1, 500, 1700) testMarshalInt64ArraySize(t, va, 1, 500, 1700)

View file

@ -155,12 +155,13 @@ func testMarshalUnmarshalInt64Array(t *testing.T, va []int64, precisionBits uint
} }
func TestMarshalUnmarshalTimestamps(t *testing.T) { func TestMarshalUnmarshalTimestamps(t *testing.T) {
r := rand.New(rand.NewSource(1))
const precisionBits = 3 const precisionBits = 3
var timestamps []int64 var timestamps []int64
v := int64(0) v := int64(0)
for i := 0; i < 8*1024; i++ { for i := 0; i < 8*1024; i++ {
v += 30e3 * int64(rand.NormFloat64()*5e2) v += 30e3 * int64(r.NormFloat64()*5e2)
timestamps = append(timestamps, v) timestamps = append(timestamps, v)
} }
result, mt, firstTimestamp := MarshalTimestamps(nil, timestamps, precisionBits) result, mt, firstTimestamp := MarshalTimestamps(nil, timestamps, precisionBits)
@ -174,12 +175,13 @@ func TestMarshalUnmarshalTimestamps(t *testing.T) {
} }
func TestMarshalUnmarshalValues(t *testing.T) { func TestMarshalUnmarshalValues(t *testing.T) {
r := rand.New(rand.NewSource(1))
const precisionBits = 3 const precisionBits = 3
var values []int64 var values []int64
v := int64(0) v := int64(0)
for i := 0; i < 8*1024; i++ { for i := 0; i < 8*1024; i++ {
v += int64(rand.NormFloat64() * 1e2) v += int64(r.NormFloat64() * 1e2)
values = append(values, v) values = append(values, v)
} }
result, mt, firstValue := MarshalValues(nil, values, precisionBits) result, mt, firstValue := MarshalValues(nil, values, precisionBits)

View file

@ -42,10 +42,11 @@ func BenchmarkUnmarshalGaugeArray(b *testing.B) {
} }
var benchGaugeArray = func() []int64 { var benchGaugeArray = func() []int64 {
r := rand.New(rand.NewSource(1))
a := make([]int64, 8*1024) a := make([]int64, 8*1024)
v := int64(0) v := int64(0)
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
v += int64(rand.NormFloat64() * 100) v += int64(r.NormFloat64() * 100)
a[i] = v a[i] = v
} }
return a return a
@ -230,10 +231,11 @@ var benchMarshalType = func() MarshalType {
}() }()
var benchInt64Array = func() []int64 { var benchInt64Array = func() []int64 {
r := rand.New(rand.NewSource(1))
var a []int64 var a []int64
var v int64 var v int64
for i := 0; i < 8*1024; i++ { for i := 0; i < 8*1024; i++ {
v += 30e3 + int64(rand.NormFloat64()*1e3) v += 30e3 + int64(r.NormFloat64()*1e3)
a = append(a, v) a = append(a, v)
} }
return a return a

View file

@ -146,6 +146,8 @@ func testMarshalInt64NearestDelta2(t *testing.T, va []int64, precisionBits uint8
} }
func TestMarshalUnmarshalInt64NearestDelta2(t *testing.T) { func TestMarshalUnmarshalInt64NearestDelta2(t *testing.T) {
r := rand.New(rand.NewSource(1))
testMarshalUnmarshalInt64NearestDelta2(t, []int64{0, 0}, 4) testMarshalUnmarshalInt64NearestDelta2(t, []int64{0, 0}, 4)
testMarshalUnmarshalInt64NearestDelta2(t, []int64{1, -3}, 4) testMarshalUnmarshalInt64NearestDelta2(t, []int64{1, -3}, 4)
testMarshalUnmarshalInt64NearestDelta2(t, []int64{255, 255}, 4) testMarshalUnmarshalInt64NearestDelta2(t, []int64{255, 255}, 4)
@ -198,7 +200,7 @@ func TestMarshalUnmarshalInt64NearestDelta2(t *testing.T) {
v = 787933 v = 787933
va = []int64{} va = []int64{}
for i := 0; i < 1024; i++ { for i := 0; i < 1024; i++ {
v -= 25 + int64(rand.NormFloat64()*2) v -= 25 + int64(r.NormFloat64()*2)
va = append(va, v) va = append(va, v)
} }
testMarshalUnmarshalInt64NearestDelta2(t, va, 4) testMarshalUnmarshalInt64NearestDelta2(t, va, 4)
@ -207,7 +209,7 @@ func TestMarshalUnmarshalInt64NearestDelta2(t *testing.T) {
v = 943854 v = 943854
va = []int64{} va = []int64{}
for i := 0; i < 1024; i++ { for i := 0; i < 1024; i++ {
v += 30 + rand.Int63n(5) v += 30 + r.Int63n(5)
va = append(va, v) va = append(va, v)
} }
testMarshalUnmarshalInt64NearestDelta2(t, va, 4) testMarshalUnmarshalInt64NearestDelta2(t, va, 4)
@ -216,7 +218,7 @@ func TestMarshalUnmarshalInt64NearestDelta2(t *testing.T) {
v = -12345 v = -12345
va = []int64{} va = []int64{}
for i := 0; i < 1024; i++ { for i := 0; i < 1024; i++ {
v += int64(rand.NormFloat64() * 10) v += int64(r.NormFloat64() * 10)
va = append(va, v) va = append(va, v)
} }
testMarshalUnmarshalInt64NearestDelta2(t, va, 2) testMarshalUnmarshalInt64NearestDelta2(t, va, 2)
@ -225,7 +227,7 @@ func TestMarshalUnmarshalInt64NearestDelta2(t *testing.T) {
v = -12345 v = -12345
va = []int64{} va = []int64{}
for i := 0; i < 1024; i++ { for i := 0; i < 1024; i++ {
v += rand.Int63n(15) - 1 v += r.Int63n(15) - 1
va = append(va, v) va = append(va, v)
} }
testMarshalUnmarshalInt64NearestDelta2(t, va, 3) testMarshalUnmarshalInt64NearestDelta2(t, va, 3)

View file

@ -53,6 +53,8 @@ func testMarshalInt64NearestDelta(t *testing.T, va []int64, precisionBits uint8,
} }
func TestMarshalUnmarshalInt64NearestDelta(t *testing.T) { func TestMarshalUnmarshalInt64NearestDelta(t *testing.T) {
r := rand.New(rand.NewSource(1))
testMarshalUnmarshalInt64NearestDelta(t, []int64{0}, 4) testMarshalUnmarshalInt64NearestDelta(t, []int64{0}, 4)
testMarshalUnmarshalInt64NearestDelta(t, []int64{0, 0}, 4) testMarshalUnmarshalInt64NearestDelta(t, []int64{0, 0}, 4)
testMarshalUnmarshalInt64NearestDelta(t, []int64{1, -3}, 4) testMarshalUnmarshalInt64NearestDelta(t, []int64{1, -3}, 4)
@ -105,7 +107,7 @@ func TestMarshalUnmarshalInt64NearestDelta(t *testing.T) {
v = 787933 v = 787933
va = []int64{} va = []int64{}
for i := 0; i < 1024; i++ { for i := 0; i < 1024; i++ {
v -= 25 + int64(rand.NormFloat64()*2) v -= 25 + int64(r.NormFloat64()*2)
va = append(va, v) va = append(va, v)
} }
testMarshalUnmarshalInt64NearestDelta(t, va, 4) testMarshalUnmarshalInt64NearestDelta(t, va, 4)
@ -114,7 +116,7 @@ func TestMarshalUnmarshalInt64NearestDelta(t *testing.T) {
v = 943854 v = 943854
va = []int64{} va = []int64{}
for i := 0; i < 1024; i++ { for i := 0; i < 1024; i++ {
v += 30 + rand.Int63n(5) v += 30 + r.Int63n(5)
va = append(va, v) va = append(va, v)
} }
testMarshalUnmarshalInt64NearestDelta(t, va, 4) testMarshalUnmarshalInt64NearestDelta(t, va, 4)
@ -123,7 +125,7 @@ func TestMarshalUnmarshalInt64NearestDelta(t *testing.T) {
v = -12345 v = -12345
va = []int64{} va = []int64{}
for i := 0; i < 1024; i++ { for i := 0; i < 1024; i++ {
v += int64(rand.NormFloat64() * 10) v += int64(r.NormFloat64() * 10)
va = append(va, v) va = append(va, v)
} }
testMarshalUnmarshalInt64NearestDelta(t, va, 4) testMarshalUnmarshalInt64NearestDelta(t, va, 4)
@ -132,7 +134,7 @@ func TestMarshalUnmarshalInt64NearestDelta(t *testing.T) {
v = -12345 v = -12345
va = []int64{} va = []int64{}
for i := 0; i < 1024; i++ { for i := 0; i < 1024; i++ {
v += rand.Int63n(15) - 1 v += r.Int63n(15) - 1
va = append(va, v) va = append(va, v)
} }
testMarshalUnmarshalInt64NearestDelta(t, va, 4) testMarshalUnmarshalInt64NearestDelta(t, va, 4)

View file

@ -15,9 +15,10 @@ func TestCompressDecompress(t *testing.T) {
testCrossCompressDecompress(t, []byte("a")) testCrossCompressDecompress(t, []byte("a"))
testCrossCompressDecompress(t, []byte("foobarbaz")) testCrossCompressDecompress(t, []byte("foobarbaz"))
r := rand.New(rand.NewSource(1))
var b []byte var b []byte
for i := 0; i < 64*1024; i++ { for i := 0; i < 64*1024; i++ {
b = append(b, byte(rand.Int31n(256))) b = append(b, byte(r.Int31n(256)))
} }
testCrossCompressDecompress(t, b) testCrossCompressDecompress(t, b)
} }