2021-11-20 15:20:42 -10:00
|
|
|
package test
|
|
|
|
|
|
|
|
|
|
import "math"
|
|
|
|
|
import "math/bits"
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
|
|
import "github.com/firestuff/subcoding/gen"
|
|
|
|
|
|
|
|
|
|
func TestRandBiasedUint64(t *testing.T) {
|
2021-11-20 17:21:46 -10:00
|
|
|
t.Parallel()
|
|
|
|
|
|
2021-11-20 15:20:42 -10:00
|
|
|
buckets := [65]uint64{}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 1000000; i++ {
|
|
|
|
|
val := gen.RandBiasedUint64()
|
|
|
|
|
buckets[bits.Len64(val)]++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var max uint64 = 0
|
|
|
|
|
var min uint64 = math.MaxUint64
|
|
|
|
|
|
|
|
|
|
for _, count := range buckets {
|
|
|
|
|
if count > max {
|
|
|
|
|
max = count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if count < min {
|
|
|
|
|
min = count
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if max - min > max / 10 {
|
|
|
|
|
t.Fatalf("Variance greater than allowed: %d > %d (max=%d min=%d)", max - min, max / 10, max, min)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-20 16:56:07 -10:00
|
|
|
|
|
|
|
|
func TestRandBiasedInt64(t *testing.T) {
|
2021-11-20 17:21:46 -10:00
|
|
|
t.Parallel()
|
|
|
|
|
|
2021-11-20 16:56:07 -10:00
|
|
|
// [0,62]: positive
|
|
|
|
|
// [63,63]: zero
|
|
|
|
|
// [64,126]: negative
|
|
|
|
|
buckets := [127]uint64{}
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 1000000; i++ {
|
|
|
|
|
val := gen.RandBiasedInt64()
|
|
|
|
|
switch {
|
|
|
|
|
case val == 0:
|
|
|
|
|
buckets[63]++
|
|
|
|
|
case val < 0:
|
|
|
|
|
buckets[63 + bits.Len64(uint64(val * -1))]++
|
|
|
|
|
case val > 0:
|
|
|
|
|
buckets[63 - bits.Len64(uint64(val))]++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var max uint64 = 0
|
|
|
|
|
var min uint64 = math.MaxUint64
|
|
|
|
|
|
|
|
|
|
for _, count := range buckets {
|
|
|
|
|
if count > max {
|
|
|
|
|
max = count
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if count < min {
|
|
|
|
|
min = count
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if max - min > max / 10 {
|
|
|
|
|
t.Fatalf("Variance greater than allowed: %d > %d (max=%d min=%d)", max - min, max / 10, max, min)
|
|
|
|
|
}
|
|
|
|
|
}
|