From 0ae97f531d700119a3b07026c9e77069710303a6 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sun, 24 Jan 2016 18:35:54 +0000 Subject: [PATCH] tests: Add some tests for urandom module. --- tests/extmod/urandom_basic.py | 19 +++++++++++++++++ tests/extmod/urandom_extra.py | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 tests/extmod/urandom_basic.py create mode 100644 tests/extmod/urandom_extra.py 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