Skip to content

Commit

Permalink
tests: Add some tests for urandom module.
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgeorge committed Jan 26, 2016
1 parent d22bdad commit 0ae97f5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/extmod/urandom_basic.py
Original file line number Diff line number Diff line change
@@ -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 changes: 39 additions & 0 deletions tests/extmod/urandom_extra.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0ae97f5

Please sign in to comment.