tests: Add some tests for urandom module.

This commit is contained in:
Damien George 2016-01-24 18:35:54 +00:00
parent d22bdad6dd
commit 0ae97f531d
2 changed files with 58 additions and 0 deletions

View File

@ -0,0 +1,19 @@
try:
import urandom as random
except ImportError:
import random
# check getrandbits returns a value within the bit range
for b in (1, 2, 3, 4, 16, 32):
for i in range(50):
assert random.getrandbits(b) < (1 << b)
# check that seed(0) gives a non-zero value
random.seed(0)
print(random.getrandbits(16) != 0)
# check that PRNG is repeatable
random.seed(1)
r = random.getrandbits(16)
random.seed(1)
print(random.getrandbits(16) == r)

View File

@ -0,0 +1,39 @@
try:
import urandom as random
except ImportError:
import random
try:
random.randint
except AttributeError:
import sys
print('SKIP')
sys.exit(1)
print('randrange')
for i in range(50):
assert 0 <= random.randrange(4) < 4
assert 2 <= random.randrange(2, 6) < 6
assert -2 <= random.randrange(-2, 2) < 2
assert random.randrange(1, 9, 2) in (1, 3, 5, 7)
print('randint')
for i in range(50):
assert 0 <= random.randint(0, 4) <= 4
assert 2 <= random.randint(2, 6) <= 6
assert -2 <= random.randint(-2, 2) <= 2
print('choice')
lst = [1, 2, 5, 6]
for i in range(50):
assert random.choice(lst) in lst
print('random')
for i in range(50):
assert 0 <= random.random() < 1
print('uniform')
for i in range(50):
assert 0 <= random.uniform(0, 4) <= 4
assert 2 <= random.uniform(2, 6) <= 6
assert -2 <= random.uniform(-2, 2) <= 2