From 97b9ce04a47b600e5b0b1e0c57d794441c1c4572 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 1 Jun 2020 07:48:51 +0300 Subject: [PATCH] crypto.rand: cleanup test, make it less likely to fail in CI --- vlib/builtin/int.v | 40 +++++++++++++++++++++-------- vlib/builtin/string.v | 10 -------- vlib/crypto/rand/rand_test.v | 50 ++++++++++++++++-------------------- 3 files changed, 52 insertions(+), 48 deletions(-) diff --git a/vlib/builtin/int.v b/vlib/builtin/int.v index 357e226351..83a9e12ee0 100644 --- a/vlib/builtin/int.v +++ b/vlib/builtin/int.v @@ -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 +} diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index a4aa301953..32a8b8bff9 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -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) diff --git a/vlib/crypto/rand/rand_test.v b/vlib/crypto/rand/rand_test.v index 41e2b199f5..42021d243f 100644 --- a/vlib/crypto/rand/rand_test.v +++ b/vlib/crypto/rand/rand_test.v @@ -1,50 +1,44 @@ // 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{} - for _ in 0..80 { + mut unique := []u64{} + for _ in 0 .. 80 { r := rand.int_u64(max) or { assert false return } 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 }