-
Notifications
You must be signed in to change notification settings - Fork 107
/
test_utils.py
149 lines (111 loc) · 4.08 KB
/
test_utils.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
#!/usr/bin/env python3
__doc__ = """Test utility scripts"""
import pytest
import numpy as np
from elastica.utils import (
isqrt,
perm_parity,
grouper,
extend_instance,
_bspline,
Tolerance,
)
from itertools import chain
from numpy.random import randint
from numpy.testing import assert_allclose
def test_isqrt_with_zero():
assert isqrt(0) == 0
# Splitting tests for small and big
# integers as this should be the common use case
@pytest.mark.parametrize("inp, out", [(1, 1), (4, 2), (9, 3)])
def test_isqrt_small_numbers(inp, out):
assert isqrt(inp) == out
@pytest.mark.parametrize("inp, out", [(56 ** 2, 56), (98 ** 2, 98)])
def test_isqrt_large_numbers(inp, out):
assert isqrt(inp) == out
@pytest.mark.parametrize("seq", [list(range(3)), [2, 0, 1], [1, 2, 3]])
def test_perm_parity_correctness_on_even_sequences(seq):
assert perm_parity(seq) == 1
@pytest.mark.parametrize("seq", [[1, 0, 2], [2, 1, 0], [4, 3, 2, 1]])
def test_perm_parity_correctness_on_odd_sequences(seq):
assert perm_parity(seq) == -1
@pytest.mark.parametrize("chunksize", [2, 3])
def test_grouper_correctness_for_perfect_sequences(chunksize):
"""Checks correctness when the length of the sequence is divisible
by chunksize
Parameters
----------
chunksize : granularity in which the sequence is grouped
Returns
-------
"""
# length of the sequence
# max size expected for symplectic algorithms does not exceed five
seq_len = chunksize * randint(1, 5)
correct_seq = [None] * seq_len
# Fill in the sequence with pseudo-random numbers, always of perfect size
for i_seq in range(seq_len):
start = randint(1, 10)
# grouper yields a tuple
correct_seq[i_seq] = (*range(start, start + chunksize),)
# We pass the expanded sequence out using chain*
test_seq = list(grouper(list(chain(*correct_seq)), chunksize))
assert test_seq == correct_seq
@pytest.mark.parametrize("chunksize", [2, 3])
def test_grouper_correctness_for_imperfect_sequences(chunksize):
"""Checks correctness when the length of the sequence is NOT divisible
by chunksize
Parameters
----------
chunksize : granularity in which the sequence is grouped
Returns
-------
"""
# length of the sequence
# max size expected for symplectic algorithms does not exceed five
seq_len = chunksize * randint(1, 5)
correct_seq = [None] * seq_len
# Fill in the sequence with pseudo-random numbers, always of perfect size
# Except the last one where we introduce imperfection
for i_seq in range(seq_len - 1):
start = randint(1, 10)
# grouper yields a tuple
correct_seq[i_seq] = (*range(start, start + chunksize),)
# Imperfect sequence not divisivle by chunksize
correct_seq[-1] = (*range(start, start + chunksize - 1),)
# We pass the expanded sequence out using chain*
test_seq = list(grouper(list(chain(*correct_seq)), chunksize))
assert test_seq == correct_seq
class TestExtendInstance:
A = type("A", (), {})
Aext = type("Aext", (), {})
B = type("B", (), {})
Bext = type("Bext", (), {})
@pytest.mark.parametrize("class_and_extension", [(A, Aext), (B, Bext)])
def test_extend_instance_correctness(self, class_and_extension):
(cls, ext_cls) = class_and_extension
cls_obj = cls()
assert ext_cls not in cls_obj.__class__.__bases__
extend_instance(cls_obj, ext_cls)
assert ext_cls in cls_obj.__class__.__bases__
def test_bspline_implementation():
t_coeff = np.array([0.0, 1.0, 1.0, 1.0, 0.0])
base_length = 2.0
position = np.linspace(0, base_length, 10)
my_spline, ctr_pts, ctr_coeffs = _bspline(t_coeff, base_length)
correct_values = np.array(
[
0.0,
0.52949246,
0.82853224,
0.96296296,
0.99862826,
0.99862826,
0.96296296,
0.82853224,
0.52949246,
0.0,
]
)
test_values = my_spline(position)
assert_allclose(test_values, correct_values, atol=Tolerance.atol())