From 5dcfda0600202b15192784afddbc2ed63036b3f5 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Tue, 3 Sep 2019 23:10:26 +1000 Subject: [PATCH] crypto.rand: improve test to actually check for difference in data --- vlib/crypto/rand/rand_test.v | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/vlib/crypto/rand/rand_test.v b/vlib/crypto/rand/rand_test.v index 7d0bc0c97b..b0a680a866 100644 --- a/vlib/crypto/rand/rand_test.v +++ b/vlib/crypto/rand/rand_test.v @@ -5,9 +5,26 @@ import crypto.rand fn test_crypto_rand() { - r := rand.read(100) or { + no_bytes := 100 + max_percentage_diff := 20 + + r1 := rand.read(no_bytes) or { assert false return } - assert r.len == 100 + 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 }