-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtests.py
More file actions
67 lines (56 loc) · 2.07 KB
/
tests.py
File metadata and controls
67 lines (56 loc) · 2.07 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
import timeit
from cyclicprng import CyclicPRNG
def test_random_cycle():
"""
Ensure that default cycles are randomly ordered
"""
cycle_size = 10
c = CyclicPRNG(cycle_size)
first_cycle = [c.get_random() for _ in range(cycle_size)]
second_cycle = [c.get_random() for _ in range(cycle_size)]
assert first_cycle != second_cycle
def test_consistent_cycle():
"""
Ensure that consistent cycles are in the same order
"""
cycle_size = 10
c = CyclicPRNG(cycle_size, consistent=True)
first_cycle = [c.get_random() for _ in range(cycle_size)]
second_cycle = [c.get_random() for _ in range(cycle_size)]
assert first_cycle == second_cycle
def test_cycle_init_time():
"""
Sanity checking that the prng can be initialized in a reasonable time.
ip4_addr_space = 4294967296
ip6_addr_space = 340282366920938463463374607431768211456
"""
ip4_timer = timeit.Timer(
"CyclicPRNG(4294967296)", setup="from cyclicprng import CyclicPRNG"
)
ip6_timer = timeit.Timer(
"CyclicPRNG(340282366920938463463374607431768211456)",
setup="from cyclicprng import CyclicPRNG",
)
assert all(i < 0.5 for i in ip4_timer.repeat(10, 1))
assert all(i < 0.5 for i in ip6_timer.repeat(10, 1))
def test_size_one_cycle():
"""
Edge case: In cycles of size one, [1] is the only valid cycle
"""
cycle_size = 1
c = CyclicPRNG(cycle_size)
first_cycle = [c.get_random() for _ in range(cycle_size)]
second_cycle = [c.get_random() for _ in range(cycle_size)]
assert len(first_cycle) == len(second_cycle) == 1
assert first_cycle == second_cycle == [1]
def test_size_two_cycle():
"""
Edge case: In cycles of size 2, we expect results to always alternate
i.e. [1,2,1,2] or [2,1,2,1] instead of [1,2,2,1] or [2,1,1,2]
"""
cycle_size = 2
c = CyclicPRNG(cycle_size)
first_cycle = [c.get_random() for _ in range(cycle_size)]
second_cycle = [c.get_random() for _ in range(cycle_size)]
assert len(first_cycle) == len(second_cycle) == 2
assert first_cycle == second_cycle