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

crypto.rand: cleanup test, make it less likely to fail in CI

This commit is contained in:
Delyan Angelov 2020-06-01 07:48:51 +03:00
parent 11e6734912
commit 97b9ce04a4
3 changed files with 52 additions and 48 deletions

View File

@ -364,16 +364,6 @@ pub fn (nn byteptr) str() string {
// ----- utilities functions -----
pub fn (a []byte) contains(val byte) bool {
for aa in a {
if aa == val {
return true
}
}
return false
}
/*
pub fn (c rune) str() string {
fst_byte := int(c)>>8 * 3 & 0xff
@ -413,3 +403,33 @@ pub fn (b []byte) clone() []byte {
return res
}
// TODO generic
pub fn (a []byte) contains(val byte) bool {
for aa in a {
if aa == val {
return true
}
}
return false
}
// TODO generic
fn (ar []int) contains(val int) bool {
for s in ar {
if s == val {
return true
}
}
return false
}
// TODO generic
pub fn (a []u64) contains(val u64) bool {
for aa in a {
if aa == val {
return true
}
}
return false
}

View File

@ -846,16 +846,6 @@ fn (ar []string) contains(val string) bool {
return false
}
// TODO generic
fn (ar []int) contains(val int) bool {
for s in ar {
if s == val {
return true
}
}
return false
}
/*
pub fn (a []string) to_c() voidptr {
mut res := malloc(sizeof(byteptr) * a.len)

View File

@ -1,37 +1,33 @@
// 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
max_percentage_diff := 20
r1 := rand.read(no_bytes) or {
assert false
return
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 r1.len == no_bytes
r2 := rand.read(no_bytes) or {
assert false
return
}
assert r2.len == no_bytes
mut difference := 0
for i, _ in r1 {
difference += if r1[i] == r2[i] {0} else {1}
}
diff_percentage := f32(100) - (f32(difference)/f32(no_bytes)*100)
assert diff_percentage <= max_percentage_diff
assert (100.0 * f32(equals) / f32(no_bytes)) < 20.0
}
fn test_crypto_rand_int_u64() {
max := u64(160)
mut unique := []int{}
mut unique := []u64{}
for _ in 0 .. 80 {
r := rand.int_u64(max) or {
assert false
@ -39,12 +35,10 @@ fn test_crypto_rand_int_u64() {
}
if r >= max {
assert false
return
}
n := int(r)
if n !in unique {
unique << n
if r !in unique {
unique << r
}
}
assert unique.len >= 40
assert unique.len >= 10
}