-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtest_pytools.py
More file actions
465 lines (340 loc) · 12.2 KB
/
test_pytools.py
File metadata and controls
465 lines (340 loc) · 12.2 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
__copyright__ = "Copyright (C) 2009-2021 Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import sys
import pytest
import logging
logger = logging.getLogger(__name__)
@pytest.mark.skipif("sys.version_info < (2, 5)")
def test_memoize_method_clear():
from pytools import memoize_method
class SomeClass:
def __init__(self):
self.run_count = 0
@memoize_method
def f(self):
self.run_count += 1
return 17
sc = SomeClass()
sc.f()
sc.f()
assert sc.run_count == 1
sc.f.clear_cache(sc) # pylint: disable=no-member
def test_memoize_method_with_uncached():
from pytools import memoize_method_with_uncached
class SomeClass:
def __init__(self):
self.run_count = 0
@memoize_method_with_uncached(uncached_args=[1], uncached_kwargs=["z"])
def f(self, x, y, z):
del x, y, z
self.run_count += 1
return 17
sc = SomeClass()
sc.f(17, 18, z=19)
sc.f(17, 19, z=20)
assert sc.run_count == 1
sc.f(18, 19, z=20)
assert sc.run_count == 2
sc.f.clear_cache(sc) # pylint: disable=no-member
def test_memoize_method_nested():
from pytools import memoize_method_nested
class SomeClass:
def __init__(self):
self.run_count = 0
def f(self):
@memoize_method_nested
def inner(x):
self.run_count += 1
return 2*x
inner(5)
inner(5)
sc = SomeClass()
sc.f()
assert sc.run_count == 1
def test_p_convergence_verifier():
pytest.importorskip("numpy")
from pytools.convergence import PConvergenceVerifier
pconv_verifier = PConvergenceVerifier()
for order in [2, 3, 4, 5]:
pconv_verifier.add_data_point(order, 0.1**order)
pconv_verifier()
pconv_verifier = PConvergenceVerifier()
for order in [2, 3, 4, 5]:
pconv_verifier.add_data_point(order, 0.5**order)
pconv_verifier()
pconv_verifier = PConvergenceVerifier()
for order in [2, 3, 4, 5]:
pconv_verifier.add_data_point(order, 2)
with pytest.raises(AssertionError):
pconv_verifier()
def test_memoize():
from pytools import memoize
count = [0]
@memoize(use_kwargs=True)
def f(i, j=1):
count[0] += 1
return i + j
assert f(1) == 2
assert f(1, 2) == 3
assert f(2, j=3) == 5
assert count[0] == 3
assert f(1) == 2
assert f(1, 2) == 3
assert f(2, j=3) == 5
assert count[0] == 3
def test_memoize_keyfunc():
from pytools import memoize
count = [0]
@memoize(key=lambda i, j=(1,): (i, len(j)))
def f(i, j=(1,)):
count[0] += 1
return i + len(j)
assert f(1) == 2
assert f(1, [2]) == 2
assert f(2, j=[2, 3]) == 4
assert count[0] == 2
assert f(1) == 2
assert f(1, (2,)) == 2
assert f(2, j=(2, 3)) == 4
assert count[0] == 2
def test_memoize_frozen():
from dataclasses import dataclass
from pytools import memoize_method
# {{{ check frozen dataclass
@dataclass(frozen=True)
class FrozenDataclass:
value: int
@memoize_method
def double_value(self):
return 2 * self.value
c = FrozenDataclass(10)
assert c.double_value() == 20
c.double_value.clear_cache(c) # pylint: disable=no-member
# }}}
# {{{ check class with no setattr
class FrozenClass:
value: int
def __init__(self, value):
object.__setattr__(self, "value", value)
def __setattr__(self, key, value):
raise AttributeError(f"cannot set attribute {key}")
@memoize_method
def double_value(self):
return 2 * self.value
c = FrozenClass(10)
assert c.double_value() == 20
c.double_value.clear_cache(c) # pylint: disable=no-member
# }}}
@pytest.mark.parametrize("dims", [2, 3])
def test_spatial_btree(dims, do_plot=False):
pytest.importorskip("numpy")
import numpy as np
nparticles = 2000
x = -1 + 2*np.random.rand(dims, nparticles)
x = np.sign(x)*np.abs(x)**1.9
x = (1.4 + x) % 2 - 1
bl = np.min(x, axis=-1)
tr = np.max(x, axis=-1)
print(bl, tr)
from pytools.spatial_btree import SpatialBinaryTreeBucket
tree = SpatialBinaryTreeBucket(bl, tr, max_elements_per_box=10)
for i in range(nparticles):
tree.insert(i, (x[:, i], x[:, i]))
if do_plot:
import matplotlib.pyplot as pt
pt.gca().set_aspect("equal")
pt.plot(x[0], x[1], "x")
tree.plot(fill=None)
pt.show()
def test_generate_numbered_unique_names():
from pytools import generate_numbered_unique_names
gen = generate_numbered_unique_names("a")
assert next(gen) == (0, "a")
assert next(gen) == (1, "a_0")
gen = generate_numbered_unique_names("b", 6)
assert next(gen) == (7, "b_6")
def test_cartesian_product():
from pytools import cartesian_product
expected_outputs = [
(0, 2, 4),
(0, 2, 5),
(0, 3, 4),
(0, 3, 5),
(1, 2, 4),
(1, 2, 5),
(1, 3, 4),
(1, 3, 5),
]
for i, output in enumerate(cartesian_product([0, 1], [2, 3], [4, 5])):
assert output == expected_outputs[i]
def test_find_module_git_revision():
import pytools
print(pytools.find_module_git_revision(pytools.__file__, n_levels_up=1))
def test_reshaped_view():
import pytools
import numpy as np
a = np.zeros((10, 2))
b = a.T
c = pytools.reshaped_view(a, -1)
assert c.shape == (20,)
with pytest.raises(AttributeError):
pytools.reshaped_view(b, -1)
def test_processlogger():
logging.basicConfig(level=logging.INFO)
from pytools import ProcessLogger
plog = ProcessLogger(logger, "testing the process logger",
long_threshold_seconds=0.01)
from time import sleep
with plog:
sleep(0.3)
def test_table():
import math
from pytools import Table
tbl = Table()
tbl.add_row(("i", "i^2", "i^3", "sqrt(i)"))
for i in range(8):
tbl.add_row((i, i ** 2, i ** 3, math.sqrt(i)))
print(tbl)
print()
print(tbl.latex())
def test_eoc():
from pytools.convergence import EOCRecorder
eoc = EOCRecorder()
for i in range(1, 8):
eoc.add_data_point(1.0 / i, 10 ** (-i))
p = eoc.pretty_print()
print(p)
print()
p = eoc.pretty_print(
abscissa_format="%.5e",
error_format="%.5e",
eoc_format="%5.2f")
print(p)
def test_natsorted():
from pytools import natsorted, natorder
assert natorder("1.001") < natorder("1.01")
assert natsorted(["x10", "x1", "x9"]) == ["x1", "x9", "x10"]
assert natsorted(map(str, range(100))) == list(map(str, range(100)))
assert natsorted(["x10", "x1", "x9"], reverse=True) == ["x10", "x9", "x1"]
assert natsorted([10, 1, 9], key=lambda d: "x%d" % d) == [1, 9, 10]
# {{{ object array iteration behavior
class FakeArray:
nopes = 0
def __len__(self):
FakeArray.nopes += 1
return 10
def __getitem__(self, idx):
FakeArray.nopes += 1
if idx > 10:
raise IndexError()
def test_make_obj_array_iteration():
from pytools.obj_array import make_obj_array
make_obj_array([FakeArray()])
assert FakeArray.nopes == 0, FakeArray.nopes
# }}}
def test_tag():
from pytools.tag import Taggable, Tag, UniqueTag, NonUniqueTagError
# Need a subclass that defines the copy function in order to test.
class TaggableWithCopy(Taggable):
def copy(self, **kwargs):
return TaggableWithCopy(kwargs["tags"])
class FairRibbon(Tag):
pass
class BlueRibbon(FairRibbon):
pass
class RedRibbon(FairRibbon):
pass
class ShowRibbon(FairRibbon, UniqueTag):
pass
class BestInShowRibbon(ShowRibbon):
pass
class ReserveBestInShowRibbon(ShowRibbon):
pass
class BestInClassRibbon(FairRibbon, UniqueTag):
pass
best_in_show_ribbon = BestInShowRibbon()
reserve_best_in_show_ribbon = ReserveBestInShowRibbon()
blue_ribbon = BlueRibbon()
red_ribbon = RedRibbon()
best_in_class_ribbon = BestInClassRibbon()
# Test that instantiation fails if tags is not a FrozenSet of Tags
with pytest.raises(AssertionError):
TaggableWithCopy(tags=[best_in_show_ribbon, reserve_best_in_show_ribbon,
blue_ribbon, red_ribbon])
# Test that instantiation fails if tags is not a FrozenSet of Tags
with pytest.raises(AssertionError):
TaggableWithCopy(tags=frozenset((1, reserve_best_in_show_ribbon, blue_ribbon,
red_ribbon)))
# Test that instantiation fails if there are multiple instances
# of the same UniqueTag subclass
with pytest.raises(NonUniqueTagError):
TaggableWithCopy(tags=frozenset((best_in_show_ribbon,
reserve_best_in_show_ribbon, blue_ribbon, red_ribbon)))
# Test that instantiation succeeds if there are multiple instances
# Tag subclasses.
t1 = TaggableWithCopy(frozenset([reserve_best_in_show_ribbon, blue_ribbon,
red_ribbon]))
assert t1.tags == frozenset((reserve_best_in_show_ribbon, red_ribbon,
blue_ribbon))
# Test that instantiation succeeds if there are multiple instances
# of UniqueTag of different subclasses.
t1 = TaggableWithCopy(frozenset([reserve_best_in_show_ribbon,
best_in_class_ribbon, blue_ribbon,
blue_ribbon]))
assert t1.tags == frozenset((reserve_best_in_show_ribbon, best_in_class_ribbon,
blue_ribbon))
# Test tagged() function
t2 = t1.tagged(red_ribbon)
print(t2.tags)
assert t2.tags == frozenset((reserve_best_in_show_ribbon, best_in_class_ribbon,
blue_ribbon, red_ribbon))
# Test that tagged() fails if a UniqueTag of the same subclass
# is alredy present
with pytest.raises(NonUniqueTagError):
t1.tagged(best_in_show_ribbon)
# Test without_tags() function
t4 = t2.without_tags(red_ribbon)
assert t4.tags == t1.tags
# Test that without_tags() fails if the tag is not present.
with pytest.raises(ValueError):
t4.without_tags(red_ribbon)
def test_unordered_hash():
import random
import hashlib
# FIXME: Use randbytes once >=3.9 is OK
lst = [bytes([random.randrange(256) for _ in range(20)])
for _ in range(200)]
lorig = lst[:]
random.shuffle(lst)
from pytools import unordered_hash
assert (unordered_hash(hashlib.sha256(), lorig).digest()
== unordered_hash(hashlib.sha256(), lst).digest())
assert (unordered_hash(hashlib.sha256(), lorig).digest()
== unordered_hash(hashlib.sha256(), lorig).digest())
assert (unordered_hash(hashlib.sha256(), lorig).digest()
!= unordered_hash(hashlib.sha256(), lorig[:-1]).digest())
lst[0] = b"aksdjfla;sdfjafd"
assert (unordered_hash(hashlib.sha256(), lorig).digest()
!= unordered_hash(hashlib.sha256(), lst).digest())
if __name__ == "__main__":
if len(sys.argv) > 1:
exec(sys.argv[1])
else:
from pytest import main
main([__file__])