-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_rng.py
More file actions
72 lines (53 loc) · 2.04 KB
/
test_rng.py
File metadata and controls
72 lines (53 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Testing module for the RNG Model."""
import sys
import pecos as pc
from pecos.engines.cvm.rng_model import RNGModel
def test_set_seed() -> None:
"""Verifies that a seed is set properly for our RNG model."""
rng = RNGModel(shot_id=0)
seed = 42
rng.set_seed(seed)
assert rng.seed == seed
def test_random_number() -> None:
"""Verifies that the random number generated is an int type."""
rng = RNGModel(shot_id=0)
random = rng.rng_random()
assert isinstance(random, int)
def test_bounded_random() -> None:
"""Verifies that a single generated random number is within bounds."""
rng = RNGModel(shot_id=0)
rng.set_seed(42)
bound = 16
rng.set_bound(bound)
assert rng.current_bound == bound
random_number = rng.rng_random()
assert 0 <= random_number < bound
def test_set_idx_raises_for_backwards_index() -> None:
"""Verifies that an error is raised when specifying an index that was already consumed in the RNG stream."""
rng = RNGModel(shot_id=0)
rng.set_seed(42)
rng.set_index(4)
try:
rng.set_index(3)
except ValueError as exc:
expected_error_msg = "RNGindex(3) cannot move backward: current stream index is 4"
assert str(exc) == expected_error_msg
def test_set_idx() -> None:
"""Verifies that the idx is set properly for our model."""
rng = RNGModel(shot_id=0)
rng.set_seed(42)
idx = 4
rng.set_index(idx)
assert rng.count == idx
def test_multiple_bounded_rand() -> None:
"""For several randomly generated number, with a random bound, verifies that its appropriate."""
rng = RNGModel(shot_id=0)
rng.set_seed(42)
# Use platform-appropriate upper bound for randint
# Windows: i32 max is 2^31 - 1 (2147483647), Unix: i64 allows 2^32
max_bound = 2**31 - 1 if sys.platform == "win32" else 2**32
for _ in range(100):
random_bound = int(pc.random.randint(1, max_bound, 1)[0])
rng.set_bound(random_bound)
random_number = rng.rng_random()
assert 0 <= random_number < random_bound