summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-01-24 18:35:54 +0000
committerDamien George <damien.p.george@gmail.com>2016-01-26 15:27:00 +0000
commit0ae97f531d700119a3b07026c9e77069710303a6 (patch)
tree61e2295021de4d420e203d373798b4165690f99b
parentd22bdad6ddd53f690a8c27b9493e6531cb59f0a2 (diff)
downloadmicropython-0ae97f531d700119a3b07026c9e77069710303a6.tar.gz
micropython-0ae97f531d700119a3b07026c9e77069710303a6.zip
tests: Add some tests for urandom module.
-rw-r--r--tests/extmod/urandom_basic.py19
-rw-r--r--tests/extmod/urandom_extra.py39
2 files changed, 58 insertions, 0 deletions
diff --git a/tests/extmod/urandom_basic.py b/tests/extmod/urandom_basic.py
new file mode 100644
index 0000000000..7e4d8bf34c
--- /dev/null
+++ b/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)
diff --git a/tests/extmod/urandom_extra.py b/tests/extmod/urandom_extra.py
new file mode 100644
index 0000000000..14ee2f0885
--- /dev/null
+++ b/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