Skip to content

Commit 5536c87

Browse files
committed
Merge branch 'master' of github.com:Quansight/array-api-tests
2 parents 8fc3e5f + bb527e6 commit 5536c87

11 files changed

+801
-993
lines changed

array_api_tests/array_helpers.py

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
zeros, ones, full, bool, int8, int16, int32,
66
int64, uint8, uint16, uint32, uint64, float32,
77
float64, nan, inf, pi, remainder, divide, isinf,
8-
negative, _integer_dtypes, _floating_dtypes,
9-
_numeric_dtypes, _boolean_dtypes, _dtypes,
10-
asarray)
11-
from . import _array_module
12-
8+
negative, asarray)
139
# These are exported here so that they can be included in the special cases
1410
# tests from this file.
1511
from ._array_module import logical_not, subtract, floor, ceil, where
12+
from . import dtype_helpers as dh
13+
1614

1715
__all__ = ['all', 'any', 'logical_and', 'logical_or', 'logical_not', 'less',
1816
'less_equal', 'greater', 'subtract', 'negative', 'floor', 'ceil',
@@ -25,9 +23,8 @@
2523
'assert_isinf', 'positive_mathematical_sign',
2624
'assert_positive_mathematical_sign', 'negative_mathematical_sign',
2725
'assert_negative_mathematical_sign', 'same_sign',
28-
'assert_same_sign', 'ndindex', 'promote_dtypes', 'float64',
29-
'asarray', 'is_integer_dtype', 'is_float_dtype', 'dtype_ranges',
30-
'full', 'true', 'false', 'isnan']
26+
'assert_same_sign', 'ndindex', 'float64',
27+
'asarray', 'full', 'true', 'false', 'isnan']
3128

3229
def zero(shape, dtype):
3330
"""
@@ -111,7 +108,7 @@ def isnegzero(x):
111108
# TODO: If copysign or signbit are added to the spec, use those instead.
112109
shape = x.shape
113110
dtype = x.dtype
114-
if is_integer_dtype(dtype):
111+
if dh.is_int_dtype(dtype):
115112
return false(shape)
116113
return equal(divide(one(shape, dtype), x), -infinity(shape, dtype))
117114

@@ -122,7 +119,7 @@ def isposzero(x):
122119
# TODO: If copysign or signbit are added to the spec, use those instead.
123120
shape = x.shape
124121
dtype = x.dtype
125-
if is_integer_dtype(dtype):
122+
if dh.is_int_dtype(dtype):
126123
return true(shape)
127124
return equal(divide(one(shape, dtype), x), infinity(shape, dtype))
128125

@@ -311,37 +308,6 @@ def same_sign(x, y):
311308
def assert_same_sign(x, y):
312309
assert all(same_sign(x, y)), "The input arrays do not have the same sign"
313310

314-
integer_dtype_objects = [getattr(_array_module, t) for t in _integer_dtypes]
315-
floating_dtype_objects = [getattr(_array_module, t) for t in _floating_dtypes]
316-
numeric_dtype_objects = [getattr(_array_module, t) for t in _numeric_dtypes]
317-
boolean_dtype_objects = [getattr(_array_module, t) for t in _boolean_dtypes]
318-
integer_or_boolean_dtype_objects = integer_dtype_objects + boolean_dtype_objects
319-
dtype_objects = [getattr(_array_module, t) for t in _dtypes]
320-
321-
def is_integer_dtype(dtype):
322-
if dtype is None:
323-
return False
324-
return dtype in [int8, int16, int32, int64, uint8, uint16, uint32, uint64]
325-
326-
def is_float_dtype(dtype):
327-
if dtype is None:
328-
# numpy.dtype('float64') == None gives True
329-
return False
330-
# TODO: Return True even for floating point dtypes that aren't part of the
331-
# spec, like np.float16
332-
return dtype in [float32, float64]
333-
334-
dtype_ranges = {
335-
int8: [-128, +127],
336-
int16: [-32_768, +32_767],
337-
int32: [-2_147_483_648, +2_147_483_647],
338-
int64: [-9_223_372_036_854_775_808, +9_223_372_036_854_775_807],
339-
uint8: [0, +255],
340-
uint16: [0, +65_535],
341-
uint32: [0, +4_294_967_295],
342-
uint64: [0, +18_446_744_073_709_551_615],
343-
}
344-
345311
def int_to_dtype(x, n, signed):
346312
"""
347313
Convert the Python integer x into an n bit signed or unsigned number.
@@ -363,22 +329,3 @@ def ndindex(shape):
363329
364330
"""
365331
return itertools.product(*[range(i) for i in shape])
366-
367-
def promote_dtypes(dtype1, dtype2):
368-
"""
369-
Special case of result_type() which uses the exact type promotion table
370-
from the spec.
371-
"""
372-
from .test_type_promotion import dtype_mapping, promotion_table
373-
374-
# Equivalent to this, but some libraries may not work properly with using
375-
# dtype objects as dict keys
376-
#
377-
# d1, d2 = reverse_dtype_mapping[dtype1], reverse_dtype_mapping[dtype2]
378-
379-
d1 = [i for i in dtype_mapping if dtype_mapping[i] == dtype1][0]
380-
d2 = [i for i in dtype_mapping if dtype_mapping[i] == dtype2][0]
381-
382-
if (d1, d2) not in promotion_table:
383-
raise ValueError(f"{d1} and {d2} are not type promotable according to the spec (this may indicate a bug in the test suite).")
384-
return dtype_mapping[promotion_table[d1, d2]]

0 commit comments

Comments
 (0)