rand: further bridge gap b/w rand module and PRNG interface (#13719)

This commit is contained in:
Subhomoy Haldar 2022-03-12 13:11:12 +05:30 committed by GitHub
parent a3e9409196
commit 40504e8600
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 142 additions and 52 deletions

View file

@ -21,6 +21,28 @@ fn test_rand_bytes() ? {
dump(differences)
}
fn test_prng_rand_bytes() ? {
mut randoms := []string{}
mut rng := rand.get_current_rng()
for i in 0 .. 100 {
x := rng.bytes(i) ?.hex()
if x.len > 0 {
randoms << x
}
assert x.len == i * 2
}
mut differences := 0
for idx in 1 .. randoms.len {
start := randoms[idx]#[0..8]
prev_start := randoms[idx - 1]#[0..8]
if start != prev_start {
differences++
}
}
assert differences > 95 // normally around 98
dump(differences)
}
fn test_rand_read() ? {
max := 50
mut a := []byte{len: max}
@ -49,3 +71,33 @@ fn test_rand_read() ? {
dump(differences)
assert differences > 11700 // normally around 11758
}
fn test_prng_rand_read() ? {
max := 50
mut a := []byte{len: max}
mut differences := 0
mut rng := rand.get_current_rng()
for j in 1 .. max {
start := '00'.repeat(j)
for k in j + 1 .. max {
end := '00'.repeat(max - k)
middle := '00'.repeat(k - j)
// eprintln('> j: $j | k: $k | start: $start | middle: $middle | end: $end')
for i in 0 .. max {
a[i] = 0
}
assert a[j..k].hex() == middle
for i in 0 .. 10 {
rng.read(mut a[j..k])
// dump(a.hex())
assert a[0..j].hex() == start
assert a[k..].hex() == end
if a[j..k].hex() != middle {
differences++
}
}
}
}
dump(differences)
assert differences > 11700 // normally around 11758
}