From 67a481360106e3dd45334b81471629defd4a9380 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 16 Sep 2016 12:49:15 +1000 Subject: [PATCH] tests/extmod/urandom: Add urandom tests for error cases. --- tests/extmod/urandom_basic.py | 6 ++++++ tests/extmod/urandom_extra.py | 37 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/tests/extmod/urandom_basic.py b/tests/extmod/urandom_basic.py index 7e4d8bf34c..bf00035bd5 100644 --- a/tests/extmod/urandom_basic.py +++ b/tests/extmod/urandom_basic.py @@ -17,3 +17,9 @@ random.seed(1) r = random.getrandbits(16) random.seed(1) print(random.getrandbits(16) == r) + +# check that it throws an error for zero bits +try: + random.getrandbits(0) +except ValueError: + print('ValueError') diff --git a/tests/extmod/urandom_extra.py b/tests/extmod/urandom_extra.py index a9ecd881de..004fb10cc4 100644 --- a/tests/extmod/urandom_extra.py +++ b/tests/extmod/urandom_extra.py @@ -16,6 +16,31 @@ for i in range(50): 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) + assert random.randrange(2, 1, -1) in (1, 2) + +# empty range +try: + random.randrange(0) +except ValueError: + print('ValueError') + +# empty range +try: + random.randrange(2, 1) +except ValueError: + print('ValueError') + +# zero step +try: + random.randrange(2, 1, 0) +except ValueError: + print('ValueError') + +# empty range +try: + random.randrange(2, 1, 1) +except ValueError: + print('ValueError') print('randint') for i in range(50): @@ -23,11 +48,23 @@ for i in range(50): assert 2 <= random.randint(2, 6) <= 6 assert -2 <= random.randint(-2, 2) <= 2 +# empty range +try: + random.randint(2, 1) +except ValueError: + print('ValueError') + print('choice') lst = [1, 2, 5, 6] for i in range(50): assert random.choice(lst) in lst +# empty sequence +try: + random.choice([]) +except IndexError: + print('IndexError') + print('random') for i in range(50): assert 0 <= random.random() < 1