Browse Source

tests: Add some tests for urandom module.

pull/1795/merge
Damien George 9 years ago
parent
commit
0ae97f531d
  1. 19
      tests/extmod/urandom_basic.py
  2. 39
      tests/extmod/urandom_extra.py

19
tests/extmod/urandom_basic.py

@ -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)

39
tests/extmod/urandom_extra.py

@ -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
Loading…
Cancel
Save