micropython/tests/extmod/urandom_extra.py
David Lechner 3dc324d3f1 tests: Format all Python code with black, except tests in basics subdir.
This adds the Python files in the tests/ directory to be formatted with
./tools/codeformat.py.  The basics/ subdirectory is excluded for now so we
aren't changing too much at once.

In a few places `# fmt: off`/`# fmt: on` was used where the code had
special formatting for readability or where the test was actually testing
the specific formatting.
2020-03-30 13:21:58 +11:00

70 lines
1.3 KiB
Python

try:
import urandom as random
except ImportError:
try:
import random
except ImportError:
print("SKIP")
raise SystemExit
try:
random.randint
except AttributeError:
print("SKIP")
raise SystemExit
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)
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):
assert 0 <= random.randint(0, 4) <= 4
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")