Fix[entropy]: fix `OneInA` function

This commit is contained in:
kayos@tcp.direct 2022-12-11 12:02:46 -08:00
parent 7c3c116baa
commit c7eafdbc52
Signed by: kayos
GPG Key ID: 4B841471B4BEE979
2 changed files with 19 additions and 0 deletions

View File

@ -73,6 +73,9 @@ func RNG(n int) int {
// OneInA generates a random number with a maximum of 'million' (input int).
// If the resulting random number is equal to 1, then the result is true.
func OneInA(million int) bool {
if million == 1 {
return true
}
return RNG(million) == 1
}

View File

@ -6,9 +6,13 @@ import (
"testing"
)
var dupCount = 0
func check[T comparable](zero T, one T, t *testing.T) {
if zero == one {
dupCount++
t.Errorf("hit a duplicate! %v == %v", zero, one)
t.Logf("duplicates so far: %d", dupCount)
}
}
@ -33,6 +37,18 @@ func Test_RNG(t *testing.T) {
}
}
func Test_OneInA(t *testing.T) {
for n := 0; n < 100; n++ {
yes := ""
if OneInA(1) {
yes = "hello"
}
if yes != "hello" {
t.Fatalf("OneInA failed to trigger when provided '1' as an argument")
}
}
}
func randStrChecks(zero, one string, t *testing.T, intendedLength int) {
if len(zero) != len(one) {
t.Fatalf("RandStr output length inconsistency, len(zero) is %d but wanted len(one) which is %d", len(zero), len(one))