This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_is_integer.py
202 lines (174 loc) · 7.56 KB
/
test_is_integer.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
198
199
200
201
import numpy as np
import pytest
from is_integer_ufunc import is_integer
def get_dtypes(label, bits):
return [getattr(np, name) for n in bits if hasattr(np, name := f'{label}{n}')]
float_dtypes = get_dtypes('float', [16, 32, 64, 80, 96, 128, 256])
cplx_dtypes = get_dtypes('complex', [64, 128, 160, 192, 256, 512])
int_dtypes = get_dtypes('int', [8, 16, 32, 64, 128, 256])
uint_dtypes = get_dtypes('uint', [8, 16, 32, 64, 128, 256])
# This does not have to be overly complete
odd_dtypes = [np.bytes_, np.string_, np.unicode_, np.object_, np.void,
np.timedelta64, np.datetime64]
class TestIsInteger:
float_dtypes = (np.half, np.single, np.double)
complex_dtypes = (np.complex64, np.complex128, np.complex256)
def test_objects(self):
"""
Check common python types.
"""
assert is_integer(False), 'Python bool'
assert is_integer(3), 'Python int'
assert is_integer(3.0), 'Python float'
assert not is_integer(-3.1), 'Python float'
with pytest.raises(TypeError):
is_integer('abc'), 'Python string'
def test_scalars(self):
"""
Verify that a small sample of each scalar type works properly.
"""
for dtype in int_dtypes + float_dtypes:
for k in np.arange(-5, 6):
n = dtype(k)
assert is_integer(n), f'{k}: {n.dtype.name}({n})'
for dtype in uint_dtypes:
i = np.iinfo(dtype)
for k in np.arange(i.max - 5, i.max + 1):
n = dtype(k)
assert is_integer(n), f'{k}: {n.dtype.name}({n})'
for k in np.arange(0, 6):
n = dtype(k)
assert is_integer(n), f'{k}: {n.dtype.name}({n})'
for dtype in float_dtypes:
i = np.finfo(dtype)
assert is_integer(i.min), f'{i.min.dtype.name}({i.min})'
assert is_integer(i.max), f'{i.max.dtype.name}({i.max})'
assert not is_integer(i.resolution), f'{i.resolution.dtype.name}({i.resolution})'
assert not is_integer(-i.resolution), f'{i.resolution.dtype.name}(-{i.resolution})'
def test_zerodims(self):
"""
Test 2D array with a zero dimension.
"""
for a in (np.empty((3, 0), dtype=int), np.empty((0, 5), dtype=float)):
v = is_integer(a)
assert v.dtype == np.bool_
assert np.array_equal(v, np.empty(a.shape, dtype=bool))
with pytest.raises(TypeError):
is_integer(np.empty(0, dtype=np.string_))
def test_multidims(self):
"""
Verify that operates on multidimensional arrays.
"""
shape = (80, 53, 17, 20)
rng = np.random.default_rng(0xBEEF)
for dtype in float_dtypes:
i = np.finfo(dtype)
x = rng.uniform(-1000, 1000, size=shape).astype(dtype)
if dtype == np.float128:
x[40:] *= rng.uniform(-1000, 1000, size=(40,) + shape[1:])
x[:40, ...] = rng.integers(-1000, 1000, size=(40,) + shape[1:])
rng.shuffle(x.ravel())
result = is_integer(x)
actual = ((x % 1) == 0)
assert result.shape == shape, f'Multidim shape {i.dtype.name}'
assert np.array_equal(result, actual), f'Multidim {i.dtype.name}'
for dtype in int_dtypes + uint_dtypes:
i = np.iinfo(dtype)
x = rng.integers(i.min, i.max + 1, size=shape, dtype=dtype)
result = is_integer(x)
assert result.shape == shape, f'Multidim shape {i.dtype.name}'
assert np.array_equiv(result, True), f'Multidim {i.dtype.name}'
def test_zeros(self):
"""
Test positive and negative zeros.
"""
for dtype in int_dtypes + uint_dtypes:
name = dtype(0).dtype.name
assert is_integer(dtype(0)), f'scalar zero {name}'
arr = np.zeros((10, 2, 14), dtype=dtype)
result = is_integer(arr)
assert result.shape == arr.shape, f'shape zero {name}'
assert np.array_equiv(result, True), f'array zero {name}'
rng = np.random.default_rng(0xEA717)
for dtype in float_dtypes:
name = dtype(0).dtype.name
assert is_integer(dtype('+0')), f'+scalar zero {name}'
assert is_integer(dtype('-0')), f'-scalar zero {name}'
arr = np.zeros((5, 5, 5), dtype=dtype)
arr[rng.integers(2, size=arr.shape, dtype=bool)] = dtype('-0')
result = is_integer(arr)
assert result.shape == arr.shape, f'shape zero {name}'
assert np.array_equiv(result, True), f'array zero {name}'
def test_nans(self):
"""
Verify nans. This is a bit incomplete because getting all the possible
nans for longdouble is painful.
"""
for dtype in float_dtypes:
name = dtype(0).dtype.name
assert not is_integer(dtype('nan')), f'scalar nan {name}'
with pytest.warns(RuntimeWarning):
arr = np.ones((4, 4, 4, 4), dtype=dtype) / dtype(0)
result = is_integer(arr)
assert result.shape == arr.shape, f'shape nan {name}'
assert np.array_equiv(result, False), f'array nan {name}'
def test_unpseudonormal(self):
"""
Verify all the bad inputs for long double
(only if float128 is actually 80-bit extended).
"""
for n in (80, 96, 128):
name = f'float{n}'
if hasattr(np, name):
dtype = getattr(np, name)
# Find extended double, not proper quad datatypes
if np.finfo(dtype).nmant == 63:
# TODO: Construct a unnormal, denormal, etc
pass
def test_infs(self):
"""
Test infinities.
"""
for dtype in float_dtypes:
name = dtype(0).dtype.name
for inf in (dtype('+inf'), dtype('-inf')):
assert not is_integer(inf), f'scalar {inf} {name}'
arr = np.full((10, 12), inf, dtype=dtype)
result = is_integer(arr)
assert result.shape == arr.shape, f'shape {inf} {name}'
assert np.array_equiv(result, False), f'array {inf} {name}'
def test_complex(self):
"""
Verify that only real values pass.
"""
rng = np.random.default_rng()
for dtype in cplx_dtypes:
name = dtype(0).dtype.name
a = dtype('3.0+0j')
b = dtype('3.1+0j')
c = dtype('0+1j')
d = dtype('6+2j')
# Scalars: real yes, real no, imaginary, complex
assert is_integer(a), f'scalar real (1) {name}'
assert not is_integer(b), f'scalar real (0) {name}'
assert not is_integer(c), f'scalar imag {name}'
assert not is_integer(d), f'scalar cplx {name}'
# Array of mixed type
arr = np.array([[a, b], [c, d]], dtype=dtype)
result = is_integer(arr)
assert result.shape == arr.shape, f'shape cplx {name}'
assert np.array_equal(result, [[True, False], [False, False]]), f'array cplx {name}'
def test_odd(self):
"""
Check string, unicode, object, datetime, timedela.
"""
for dtype in odd_dtypes:
x = np.zeros((3, 3), dtype=dtype)
with pytest.raises(TypeError):
is_integer(x)
if __name__ == '__main__':
inst = TestIsInteger()
for name in dir(inst):
if name.startswith('test_') and callable(value := getattr(inst, name)):
print(f'Running {name}')
value()