mirror of
https://github.com/safing/portbase
synced 2025-04-19 08:59:09 +00:00
41 lines
830 B
Go
41 lines
830 B
Go
package rng
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestNumberRandomness(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
// skip in automated tests
|
|
t.Logf("Integer number bias test deactivated, as it sometimes triggers.")
|
|
t.SkipNow()
|
|
|
|
if testing.Short() {
|
|
t.Skip()
|
|
}
|
|
|
|
var subjects uint64 = 10
|
|
var testSize uint64 = 10000
|
|
|
|
results := make([]uint64, int(subjects))
|
|
for i := 0; i < int(subjects*testSize); i++ {
|
|
n, err := Number(subjects - 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
return
|
|
}
|
|
results[int(n)]++
|
|
}
|
|
|
|
// catch big mistakes in the number function, eg. massive % bias
|
|
lowerMargin := testSize - testSize/50
|
|
upperMargin := testSize + testSize/50
|
|
for subject, result := range results {
|
|
if result < lowerMargin || result > upperMargin {
|
|
t.Errorf("subject %d is outside of margins: %d", subject, result)
|
|
}
|
|
}
|
|
|
|
t.Fatal(results)
|
|
}
|