Skip to content

Commit d593660

Browse files
committed
Add an elementwise test for negative()
I am assuming for now that negative() is not defined for the smallest representable negative integer for signed integer dtypes (see data-apis/array-api#250).
1 parent 4bd931c commit d593660

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

array_api_tests/test_elementwise_functions.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -747,8 +747,24 @@ def test_multiply(args):
747747

748748
@given(numeric_scalars)
749749
def test_negative(x):
750-
# a = _array_module.negative(x)
751-
pass
750+
a = _array_module.negative(x)
751+
752+
# Negation is an involution
753+
b = _array_module.negative(a)
754+
assert_exactly_equal(x, b)
755+
756+
mask = isfinite(x)
757+
if is_integer_dtype(x.dtype):
758+
minval = dtype_ranges[x.dtype][0]
759+
if minval < 0:
760+
# negative of the smallest representable negative integer is not defined
761+
mask = not_equal(x, full(x.shape, minval, dtype=x.dtype))
762+
x = x[mask]
763+
764+
# Additive inverse
765+
y = _array_module.add(x[mask], a[mask])
766+
ZERO = zero(x[mask].shape, x.dtype)
767+
assert_exactly_equal(y, ZERO)
752768

753769
@given(two_any_dtypes.flatmap(lambda i: two_array_scalars(*i)))
754770
def test_not_equal(args):

0 commit comments

Comments
 (0)