-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_hashlife.py
198 lines (148 loc) · 4.99 KB
/
test_hashlife.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
from hashlife import (
join,
successor,
on,
off,
construct,
centre,
expand,
inner,
pad,
crop,
is_padded,
get_zero,
baseline_life,
advance,
ffwd,
)
from lifeparsers import autoguess_life_file
from itertools import product
import os
from functools import lru_cache
from pathlib import Path
import pytest
def test_bootstrap():
# try to generate all 4x4 successors
def product_tree(pieces):
return [join(a, b, c, d) for a, b, c, d in product(pieces, repeat=4)]
# check cache is working
for i in range(2):
boot_2x2 = product_tree([on, off])
boot_4x4 = product_tree(boot_2x2)
centres = {p: successor(p, 1) for p in boot_4x4}
assert join.cache_info().currsize == 65536 + 16
assert successor.cache_info().currsize == 65536
test_fname = "lifep/gun30.lif"
test_pattern, _ = autoguess_life_file(test_fname)
def test_advance():
pat_node = construct(test_pattern)
for i in range(32):
node = advance(pat_node, i)
validate_tree(node)
for i in range(4):
node, gens = ffwd(pat_node, i)
validate_tree(node)
assert advance(get_zero(8), 8).n == 0
assert ffwd(get_zero(8), 4)[0].n == 0
def test_ffwd_large():
pat, _ = autoguess_life_file("lifep/breeder.lif")
ffwd(construct(pat), 64)
def test_get_zero():
for i in range(32):
z = get_zero(i)
assert z.k == i
assert z.n == 0
def align(pts):
min_x = min(*[x for x, y in pts])
min_y = min(*[y for x, y in pts])
return sorted([(x - min_x, y - min_y) for x, y in pts])
def same_pattern(pt_a, expanded):
return align(pt_a) == align([(x, y) for x, y, gray in expanded])
def verify_baseline(pat, n):
node = construct(pat)
assert same_pattern(pat, expand(node))
for i in range(n):
advanced = advance(node, i)
assert same_pattern(pat, expand(advanced))
pat = baseline_life(pat)
def verify_baseline_step(pat, n):
node = construct(pat)
assert same_pattern(pat, expand(node))
for i in range(n):
pat = baseline_life(pat)
node = advance(node, n)
print(len(pat),len(expand(node)))
assert same_pattern(pat, expand(node))
def verify_ffwd(pat, leaps=8):
node = construct(pat)
node, n = ffwd(node, leaps)
for i in range(n):
pat = baseline_life(pat)
assert same_pattern(pat, expand(node))
def test_gray():
pat, _ = autoguess_life_file("lifep/breeder.lif")
node = construct(pat)
total_on = len(expand(node))
for l in range(6):
expansion = expand(node, level=l)
gray_sum = sum([g for (x, y, g) in expansion])
assert gray_sum == total_on / (2 ** (l * 2))
@pytest.mark.slow
def test_pats():
verify_baseline_step(autoguess_life_file("tests/fred.rle")[0], 65000)
verify_baseline_step(autoguess_life_file("lifep/breeder.lif")[0], 2000)
verify_baseline_step(autoguess_life_file("lifep/jaws.lif")[0], 2000)
for file in Path(".").glob("lifep/*.lif"):
verify_baseline_step(autoguess_life_file(file)[0], 20)
verify_ffwd(autoguess_life_file("lifep/pi.lif")[0], 8)
verify_ffwd(autoguess_life_file("lifep/bi-gun.lif")[0], 3)
def verify_clipped(node, x1, y1, x2, y2):
pts = expand(node, clip=(x1, y1, x2, y2))
assert all([x >= x1 and x <= x2 and y > y1 and y < y2 for x, y in pts])
def test_clip():
pat, _ = autoguess_life_file("lifep/breeder.lif")
node = construct(pat)
verify_clipped(node, 0, 0, 1600, 1600)
verify_clipped(node, 0, 0, 160, 160)
verify_clipped(node, 40, 40, 1600, 1600)
verify_clipped(node, 40, 40, 160, 160)
def test_all_patterns():
for pat_fname in os.listdir("lifep"):
if pat_fname.endswith(".lif"):
pat, _ = autoguess_life_file("lifep/" + pat_fname)
verify_baseline(pat, 64)
def test_baseline():
verify_baseline(test_pattern, 64)
def test_construct():
node = construct(test_pattern)
validate_tree(node)
def test_centre():
node = construct(test_pattern)
for i in range(5):
old_node = node
node = centre(node)
assert node.k == old_node.k + 1
assert node.n == old_node.n
ctr_node = inner(node)
assert ctr_node == old_node
def test_pad():
node = construct(test_pattern)
node = pad(node)
assert is_padded(node)
node = crop(node)
assert not is_padded(node)
@lru_cache(None)
def validate_tree(node):
if node.k > 0:
assert node.n >= 0 and node.n <= 2 ** (node.k * 2)
assert node.a.k == node.b.k == node.c.k == node.b.k == node.k - 1
assert node.n == node.a.n + node.b.n + node.c.n + node.d.n
assert type(node).__name__ == "Node"
assert type(node.a).__name__ == "Node"
assert type(node.b).__name__ == "Node"
assert type(node.c).__name__ == "Node"
assert type(node.d).__name__ == "Node"
validate_tree(node.a)
validate_tree(node.b)
validate_tree(node.c)
validate_tree(node.d)