1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

rand: fix warnings

This commit is contained in:
ka-weihe
2020-06-02 06:39:38 +02:00
committed by GitHub
parent 076089d3c5
commit 13c68eb81e
15 changed files with 90 additions and 448 deletions

View File

@ -1,44 +0,0 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import crypto.rand
fn get_random_bytes(no_bytes int) []byte {
r := rand.read(no_bytes) or {
assert false
return []
}
assert r.len == no_bytes
return r
}
fn test_crypto_rand_read() {
no_bytes := 100
r1 := get_random_bytes(no_bytes)
r2 := get_random_bytes(no_bytes)
mut equals := 0
for i in 0 .. r1.len {
if r1[i] == r2[i] {
equals++
}
}
assert (100.0 * f32(equals) / f32(no_bytes)) < 20.0
}
fn test_crypto_rand_int_u64() {
max := u64(160)
mut unique := []u64{}
for _ in 0 .. 80 {
r := rand.int_u64(max) or {
assert false
return
}
if r >= max {
assert false
}
if r !in unique {
unique << r
}
}
assert unique.len >= 10
}