Skip to content

Commit 465f402

Browse files
committed
Add an elementwise test for floor_divide
1 parent b376845 commit 465f402

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

array_api_tests/array_helpers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
int64, uint8, uint16, uint32, uint64, float32,
77
float64, nan, inf, pi, remainder, divide, isinf,
88
negative, _integer_dtypes, _floating_dtypes,
9-
_numeric_dtypes, _boolean_dtypes, _dtypes, )
9+
_numeric_dtypes, _boolean_dtypes, _dtypes,
10+
asarray)
1011
from . import _array_module
1112

1213
# These are exported here so that they can be included in the special cases
@@ -24,7 +25,8 @@
2425
'assert_isinf', 'positive_mathematical_sign',
2526
'assert_positive_mathematical_sign', 'negative_mathematical_sign',
2627
'assert_negative_mathematical_sign', 'same_sign',
27-
'assert_same_sign', 'ndindex', 'promote_dtypes']
28+
'assert_same_sign', 'ndindex', 'promote_dtypes', 'float64',
29+
'asarray']
2830

2931
def zero(shape, dtype):
3032
"""

array_api_tests/test_elementwise_functions.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
logical_or, logical_and, inrange, π, one, zero,
3232
infinity, isnegative, all as array_all, any as
3333
array_any, int_to_dtype, bool as bool_dtype,
34-
assert_integral, less_equal, isintegral,
35-
isfinite, ndindex, promote_dtypes,
36-
is_integer_dtype, is_float_dtype)
34+
assert_integral, less_equal, isintegral, isfinite,
35+
ndindex, promote_dtypes, is_integer_dtype,
36+
is_float_dtype, not_equal, float64, asarray)
3737
# We might as well use this implementation rather than requiring
3838
# mod.broadcast_shapes(). See test_equal() and others.
3939
from .test_broadcasting import broadcast_shapes
@@ -437,7 +437,25 @@ def test_floor(x):
437437
def test_floor_divide(args):
438438
x1, x2 = args
439439
sanity_check(x1, x2)
440-
# a = _array_module.floor_divide(x1, x2)
440+
if is_integer_dtype(x1.dtype):
441+
# The spec does not specify the behavior for division by 0 for integer
442+
# dtypes. A library may choose to raise an exception in this case, so
443+
# we avoid passing it in entirely.
444+
nonzero = not_equal(x2, zero(x2.shape, x2.dtype))
445+
div = _array_module.divide(
446+
asarray(x1[nonzero], dtype=float64),
447+
asarray(x2[nonzero], dtype=float64))
448+
a = _array_module.floor_divide(x1[nonzero], x2[nonzero])
449+
else:
450+
div = _array_module.divide(x1, x2)
451+
a = _array_module.floor_divide(x1, x2)
452+
453+
# TODO: The spec doesn't clearly specify the behavior of floor_divide on
454+
# infinities. See https://github.com/data-apis/array-api/issues/199.
455+
finite = isfinite(div)
456+
assert_integral(a[finite])
457+
458+
# TODO: Test the exact output for floor_divide.
441459

442460
@given(two_numeric_dtypes.flatmap(lambda i: two_array_scalars(*i)))
443461
def test_greater(args):

0 commit comments

Comments
 (0)