forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sparsearray.py
More file actions
301 lines (242 loc) · 7.13 KB
/
test_sparsearray.py
File metadata and controls
301 lines (242 loc) · 7.13 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
from panda3d import core
import pickle
def test_sparse_array_type():
assert core.SparseArray.get_class_type().name == "SparseArray"
def test_sparse_array_set_bit_to():
"""Tests SparseArray behavior for set_bit_to()."""
s = core.SparseArray()
s.set_bit_to(5, True)
assert s.get_bit(5)
s = core.SparseArray.all_on()
s.set_bit_to(5, False)
assert not s.get_bit(5)
def test_sparse_array_clear():
"""Tests SparseArray behavior for clear()."""
s = core.SparseArray.all_on()
s.clear()
assert s.is_zero()
assert not s.is_inverse()
assert s.get_num_subranges() == 0
assert s.get_num_on_bits() == 0
assert s.get_num_bits() == 0
s = core.SparseArray()
s.set_range(5, 10)
s.clear()
assert s.is_zero()
assert not s.is_inverse()
assert s.get_num_subranges() == 0
assert s.get_num_on_bits() == 0
assert s.get_num_bits() == 0
def test_sparse_array_clear_range():
# Not using parametrize because there are too many values for that.
for mask in range(0x7f):
for begin in range(8):
for size in range(8):
b = core.BitArray(mask)
s = core.SparseArray(b)
s.clear_range(begin, size)
b.clear_range(begin, size)
assert core.BitArray(s) == b
assert s == core.SparseArray(b)
def test_sparse_array_set_clear_ranges():
"""Tests SparseArray behavior for setting and clearing ranges."""
# test clear_range with single overlapping on-range
# (clear_range extends beyond highest on-bit)
s = core.SparseArray()
s.set_range(2, 3)
s.clear_range(3, 3)
assert s.get_bit(2)
assert not s.get_bit(3)
# same as above, using set_range_to
s = core.SparseArray()
s.set_range_to(True, 2, 3)
s.set_range_to(False, 3, 3)
assert s.get_bit(2)
assert not s.get_bit(3)
# test clear_range using off-range which overlaps two on-ranges
# (lowest off-bit in lowest on-range, highest off-bit in highest on-range)
s = core.SparseArray()
s.set_range(2, 3)
s.set_range(7, 3)
s.clear_range(3, 6)
assert s.get_bit(2)
assert not s.get_bit(3)
assert not s.get_bit(8)
assert s.get_bit(9)
# same as above, using set_range_to
s = core.SparseArray()
s.set_range_to(True, 2, 3)
s.set_range_to(True, 7, 3)
s.set_range_to(False, 3, 6)
assert s.get_bit(2)
assert not s.get_bit(3)
assert not s.get_bit(8)
assert s.get_bit(9)
def test_sparse_array_set_range():
"""Tests SparseArray behavior for set_range()."""
# test set_range with single overlapping off-range
# (set_range extends beyond highest off-bit)
s = core.SparseArray.all_on()
s.clear_range(2, 3)
s.set_range(3, 3)
assert not s.get_bit(2)
assert s.get_bit(3)
# same as above, using set_range_to
s = core.SparseArray.all_on()
s.set_range_to(False, 2, 3)
s.set_range_to(True, 3, 3)
assert not s.get_bit(2)
assert s.get_bit(3)
# test set_range using on-range which overlaps two off-ranges
# (lowest on-bit in lowest off-range, highest on-bit in highest off-range)
s = core.SparseArray.all_on()
s.clear_range(2, 3)
s.clear_range(7, 3)
s.set_range(3, 6)
assert not s.get_bit(2)
assert s.get_bit(3)
assert s.get_bit(8)
assert not s.get_bit(9)
# same as above, using set_range_to
s = core.SparseArray.all_on()
s.set_range_to(False, 2, 3)
s.set_range_to(False, 7, 3)
s.set_range_to(True, 3, 6)
assert not s.get_bit(2)
assert s.get_bit(3)
assert s.get_bit(8)
assert not s.get_bit(9)
def test_sparse_array_bits_in_common():
"""Tests SparseArray behavior for has_bits_in_common()."""
s = core.SparseArray()
t = core.SparseArray()
s.set_range(2, 4)
t.set_range(5, 4)
assert s.has_bits_in_common(t)
s = core.SparseArray()
t = core.SparseArray()
s.set_range(2, 4)
t.set_range(6, 4)
assert not s.has_bits_in_common(t)
def test_sparse_array_operations():
"""Tests SparseArray behavior for various operations."""
# test bitshift to left
s = core.SparseArray()
s.set_bit(2)
t = s << 2
assert t.get_bit(4)
assert not t.get_bit(2)
# test bitshift to right
s = core.SparseArray()
s.set_bit(4)
t = s >> 2
assert t.get_bit(2)
assert not t.get_bit(4)
# test bitwise AND
s = core.SparseArray()
t = core.SparseArray()
s.set_bit(2)
s.set_bit(3)
t.set_bit(1)
t.set_bit(3)
u = s & t
assert not u.get_bit(0)
assert not u.get_bit(1)
assert not u.get_bit(2)
assert u.get_bit(3)
# test bitwise OR
s = core.SparseArray()
t = core.SparseArray()
s.set_bit(2)
s.set_bit(3)
t.set_bit(1)
t.set_bit(3)
u = s | t
assert not u.get_bit(0)
assert u.get_bit(1)
assert u.get_bit(2)
assert u.get_bit(3)
# test bitwise XOR
s = core.SparseArray()
t = core.SparseArray()
s.set_bit(2)
s.set_bit(3)
t.set_bit(1)
t.set_bit(3)
u = s ^ t
assert not u.get_bit(0)
assert u.get_bit(1)
assert u.get_bit(2)
assert not u.get_bit(3)
def test_sparse_array_augm_assignment():
"""Tests SparseArray behavior for augmented assignments."""
# test in-place bitshift to left
s = t = core.SparseArray()
t <<= 2
assert s is t
# test in-place bitshift to right
s = t = core.SparseArray()
t >>= 2
assert s is t
# test in-place bitwise AND
s = t = core.SparseArray()
u = core.SparseArray()
t &= u
assert s is t
# test in-place bitwise OR
s = t = core.SparseArray()
u = core.SparseArray()
t |= u
assert s is t
# test in-place bitwise XOR
s = t = core.SparseArray()
u = core.SparseArray()
t ^= u
assert s is t
def test_sparse_array_nonzero():
sa = core.SparseArray()
assert not sa
sa.set_bit(0)
assert sa
sa = core.SparseArray.all_on()
assert sa
sa.clear_range(0, 100)
assert sa
def test_sparse_array_getstate():
sa = core.SparseArray()
assert sa.__getstate__() == ()
sa = core.SparseArray()
sa.invert_in_place()
assert sa.__getstate__() == (0,)
sa = core.SparseArray()
sa.set_range(0, 2)
sa.set_range(4, 4)
assert sa.__getstate__() == (0, 2, 4, 8)
sa = core.SparseArray()
sa.invert_in_place()
sa.clear_range(2, 4)
assert sa.__getstate__() == (0, 2, 6)
sa = core.SparseArray()
sa.invert_in_place()
sa.clear_range(0, 2)
sa.clear_range(4, 4)
assert sa.__getstate__() == (2, 4, 8)
def test_sparse_array_pickle():
sa = core.SparseArray()
assert sa == pickle.loads(pickle.dumps(sa, -1))
sa = core.SparseArray()
sa.invert_in_place()
assert sa == pickle.loads(pickle.dumps(sa, -1))
sa = core.SparseArray()
sa.set_range(0, 2)
sa.set_range(4, 4)
assert sa == pickle.loads(pickle.dumps(sa, -1))
sa = core.SparseArray()
sa.invert_in_place()
sa.clear_range(2, 4)
assert sa == pickle.loads(pickle.dumps(sa, -1))
sa = core.SparseArray()
sa.invert_in_place()
sa.clear_range(0, 2)
sa.clear_range(4, 4)
assert sa == pickle.loads(pickle.dumps(sa, -1))