Skip to content

Commit cf76271

Browse files
committed
TST: add numpy tests for numeric types and dtypes
numpy commit 1d5b3397dc7beaf9ca7b2326127e74f55aecd162 (main branch Dec 2022)
1 parent d160883 commit cf76271

File tree

8 files changed

+4087
-0
lines changed

8 files changed

+4087
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ __pycache__/*
22
autogen/__pycache__
33
torch_np/__pycache__/*
44
torch_np/tests/__pycache__/*
5+
torch_np/tests/numpy_tests/core/__pycache__/*
56
.coverage
67

torch_np/tests/numpy_tests/core/test_dtype.py

Lines changed: 1827 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
""" Test functions for limits module.
2+
3+
"""
4+
import warnings
5+
import numpy as np
6+
from numpy.core import finfo, iinfo
7+
from numpy import half, single, double, longdouble
8+
from numpy.testing import assert_equal, assert_, assert_raises
9+
from numpy.core.getlimits import _discovered_machar, _float_ma
10+
11+
##################################################
12+
13+
class TestPythonFloat:
14+
def test_singleton(self):
15+
ftype = finfo(float)
16+
ftype2 = finfo(float)
17+
assert_equal(id(ftype), id(ftype2))
18+
19+
class TestHalf:
20+
def test_singleton(self):
21+
ftype = finfo(half)
22+
ftype2 = finfo(half)
23+
assert_equal(id(ftype), id(ftype2))
24+
25+
class TestSingle:
26+
def test_singleton(self):
27+
ftype = finfo(single)
28+
ftype2 = finfo(single)
29+
assert_equal(id(ftype), id(ftype2))
30+
31+
class TestDouble:
32+
def test_singleton(self):
33+
ftype = finfo(double)
34+
ftype2 = finfo(double)
35+
assert_equal(id(ftype), id(ftype2))
36+
37+
class TestLongdouble:
38+
def test_singleton(self):
39+
ftype = finfo(longdouble)
40+
ftype2 = finfo(longdouble)
41+
assert_equal(id(ftype), id(ftype2))
42+
43+
class TestFinfo:
44+
def test_basic(self):
45+
dts = list(zip(['f2', 'f4', 'f8', 'c8', 'c16'],
46+
[np.float16, np.float32, np.float64, np.complex64,
47+
np.complex128]))
48+
for dt1, dt2 in dts:
49+
for attr in ('bits', 'eps', 'epsneg', 'iexp', 'machep',
50+
'max', 'maxexp', 'min', 'minexp', 'negep', 'nexp',
51+
'nmant', 'precision', 'resolution', 'tiny',
52+
'smallest_normal', 'smallest_subnormal'):
53+
assert_equal(getattr(finfo(dt1), attr),
54+
getattr(finfo(dt2), attr), attr)
55+
assert_raises(ValueError, finfo, 'i4')
56+
57+
class TestIinfo:
58+
def test_basic(self):
59+
dts = list(zip(['i1', 'i2', 'i4', 'i8',
60+
'u1', 'u2', 'u4', 'u8'],
61+
[np.int8, np.int16, np.int32, np.int64,
62+
np.uint8, np.uint16, np.uint32, np.uint64]))
63+
for dt1, dt2 in dts:
64+
for attr in ('bits', 'min', 'max'):
65+
assert_equal(getattr(iinfo(dt1), attr),
66+
getattr(iinfo(dt2), attr), attr)
67+
assert_raises(ValueError, iinfo, 'f4')
68+
69+
def test_unsigned_max(self):
70+
types = np.sctypes['uint']
71+
for T in types:
72+
with np.errstate(over="ignore"):
73+
max_calculated = T(0) - T(1)
74+
assert_equal(iinfo(T).max, max_calculated)
75+
76+
class TestRepr:
77+
def test_iinfo_repr(self):
78+
expected = "iinfo(min=-32768, max=32767, dtype=int16)"
79+
assert_equal(repr(np.iinfo(np.int16)), expected)
80+
81+
def test_finfo_repr(self):
82+
expected = "finfo(resolution=1e-06, min=-3.4028235e+38," + \
83+
" max=3.4028235e+38, dtype=float32)"
84+
assert_equal(repr(np.finfo(np.float32)), expected)
85+
86+
87+
def test_instances():
88+
iinfo(10)
89+
finfo(3.0)
90+
91+
92+
def assert_ma_equal(discovered, ma_like):
93+
# Check MachAr-like objects same as calculated MachAr instances
94+
for key, value in discovered.__dict__.items():
95+
assert_equal(value, getattr(ma_like, key))
96+
if hasattr(value, 'shape'):
97+
assert_equal(value.shape, getattr(ma_like, key).shape)
98+
assert_equal(value.dtype, getattr(ma_like, key).dtype)
99+
100+
101+
def test_known_types():
102+
# Test we are correctly compiling parameters for known types
103+
for ftype, ma_like in ((np.float16, _float_ma[16]),
104+
(np.float32, _float_ma[32]),
105+
(np.float64, _float_ma[64])):
106+
assert_ma_equal(_discovered_machar(ftype), ma_like)
107+
# Suppress warning for broken discovery of double double on PPC
108+
with np.errstate(all='ignore'):
109+
ld_ma = _discovered_machar(np.longdouble)
110+
bytes = np.dtype(np.longdouble).itemsize
111+
if (ld_ma.it, ld_ma.maxexp) == (63, 16384) and bytes in (12, 16):
112+
# 80-bit extended precision
113+
assert_ma_equal(ld_ma, _float_ma[80])
114+
elif (ld_ma.it, ld_ma.maxexp) == (112, 16384) and bytes == 16:
115+
# IEE 754 128-bit
116+
assert_ma_equal(ld_ma, _float_ma[128])
117+
118+
119+
def test_subnormal_warning():
120+
"""Test that the subnormal is zero warning is not being raised."""
121+
with np.errstate(all='ignore'):
122+
ld_ma = _discovered_machar(np.longdouble)
123+
bytes = np.dtype(np.longdouble).itemsize
124+
with warnings.catch_warnings(record=True) as w:
125+
warnings.simplefilter('always')
126+
if (ld_ma.it, ld_ma.maxexp) == (63, 16384) and bytes in (12, 16):
127+
# 80-bit extended precision
128+
ld_ma.smallest_subnormal
129+
assert len(w) == 0
130+
elif (ld_ma.it, ld_ma.maxexp) == (112, 16384) and bytes == 16:
131+
# IEE 754 128-bit
132+
ld_ma.smallest_subnormal
133+
assert len(w) == 0
134+
else:
135+
# Double double
136+
ld_ma.smallest_subnormal
137+
# This test may fail on some platforms
138+
assert len(w) == 0
139+
140+
141+
def test_plausible_finfo():
142+
# Assert that finfo returns reasonable results for all types
143+
for ftype in np.sctypes['float'] + np.sctypes['complex']:
144+
info = np.finfo(ftype)
145+
assert_(info.nmant > 1)
146+
assert_(info.minexp < -1)
147+
assert_(info.maxexp > 1)

0 commit comments

Comments
 (0)