1
from __future__ import division, absolute_import, print_function
9
import numpy.core.multiarray_tests as mt
10
from numpy.compat import long
12
from numpy.testing import assert_raises, assert_equal
15
INT64_MAX = np.iinfo(np.int64).max
16
INT64_MIN = np.iinfo(np.int64).min
19
# int128 is not two's complement, the sign bit is separate
20
INT128_MAX = 2**128 - 1
21
INT128_MIN = -INT128_MAX
25
[INT64_MIN + j for j in range(20)] +
26
[INT64_MAX - j for j in range(20)] +
27
[INT64_MID + j for j in range(-20, 20)] +
28
[2*INT64_MID + j for j in range(-20, 20)] +
29
[INT64_MID//2 + j for j in range(-20, 20)] +
34
[INT128_MIN + j for j in range(20)] +
35
[INT128_MAX - j for j in range(20)] +
36
[INT128_MID + j for j in range(-20, 20)] +
37
[2*INT128_MID + j for j in range(-20, 20)] +
38
[INT128_MID//2 + j for j in range(-20, 20)] +
39
list(range(-70, 70)) +
40
[False] # negative zero
43
INT64_POS_VALUES = [x for x in INT64_VALUES if x > 0]
46
@contextlib.contextmanager
49
Iterate over Cartesian product of *args, and if an exception is raised,
50
add information of the current iterate.
56
for v in itertools.product(*args):
64
msg = "At: %r\n%s" % (repr(value[0]),
65
traceback.format_exc())
66
raise AssertionError(msg)
69
def test_safe_binop():
70
# Test checked arithmetic routines
78
with exc_iter(ops, INT64_VALUES, INT64_VALUES) as it:
83
if not (INT64_MIN <= c <= INT64_MAX):
84
assert_raises(OverflowError, mt.extint_safe_binop, a, b, op)
86
d = mt.extint_safe_binop(a, b, op)
88
# assert_equal is slow
93
with exc_iter(INT64_VALUES) as it:
95
b = mt.extint_to_128(a)
101
with exc_iter(INT128_VALUES) as it:
103
if not (INT64_MIN <= a <= INT64_MAX):
104
assert_raises(OverflowError, mt.extint_to_64, a)
106
b = mt.extint_to_64(a)
111
def test_mul_64_64():
112
with exc_iter(INT64_VALUES, INT64_VALUES) as it:
115
d = mt.extint_mul_64_64(a, b)
121
with exc_iter(INT128_VALUES, INT128_VALUES) as it:
124
if not (INT128_MIN <= c <= INT128_MAX):
125
assert_raises(OverflowError, mt.extint_add_128, a, b)
127
d = mt.extint_add_128(a, b)
133
with exc_iter(INT128_VALUES, INT128_VALUES) as it:
136
if not (INT128_MIN <= c <= INT128_MAX):
137
assert_raises(OverflowError, mt.extint_sub_128, a, b)
139
d = mt.extint_sub_128(a, b)
145
with exc_iter(INT128_VALUES) as it:
148
c = mt.extint_neg_128(a)
154
with exc_iter(INT128_VALUES) as it:
157
b = -(((-a) << 1) & (2**128-1))
159
b = (a << 1) & (2**128-1)
160
c = mt.extint_shl_128(a)
166
with exc_iter(INT128_VALUES) as it:
172
c = mt.extint_shr_128(a)
178
with exc_iter(INT128_VALUES, INT128_VALUES) as it:
181
d = mt.extint_gt_128(a, b)
186
def test_divmod_128_64():
187
with exc_iter(INT128_VALUES, INT64_POS_VALUES) as it:
192
c, cr = divmod(-a, b)
196
d, dr = mt.extint_divmod_128_64(a, b)
198
if c != d or d != dr or b*d + dr != a:
201
assert_equal(b*d + dr, a)
204
def test_floordiv_128_64():
205
with exc_iter(INT128_VALUES, INT64_POS_VALUES) as it:
208
d = mt.extint_floordiv_128_64(a, b)
214
def test_ceildiv_128_64():
215
with exc_iter(INT128_VALUES, INT64_POS_VALUES) as it:
218
d = mt.extint_ceildiv_128_64(a, b)
224
if __name__ == "__main__":