Skip to content

Commit a4d2689

Browse files
committed
Add elementwise tests for isfinite, isinf, and isnan
1 parent 7f5b687 commit a4d2689

File tree

1 file changed

+40
-7
lines changed

1 file changed

+40
-7
lines changed

array_api_tests/test_elementwise_functions.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from hypothesis import given, assume
1818
from hypothesis.strategies import composite, just
1919

20+
import math
21+
2022
from .hypothesis_helpers import (integer_dtype_objects,
2123
floating_dtype_objects,
2224
numeric_dtype_objects,
@@ -34,7 +36,7 @@
3436
assert_integral, less_equal, isintegral, isfinite,
3537
ndindex, promote_dtypes, is_integer_dtype,
3638
is_float_dtype, not_equal, float64, asarray,
37-
dtype_ranges, full)
39+
dtype_ranges, full, true, false)
3840
# We might as well use this implementation rather than requiring
3941
# mod.broadcast_shapes(). See test_equal() and others.
4042
from .test_broadcasting import broadcast_shapes
@@ -526,18 +528,49 @@ def test_greater_equal(args):
526528

527529
@given(numeric_scalars)
528530
def test_isfinite(x):
529-
# a = _array_module.isfinite(x)
530-
pass
531+
a = _array_module.isfinite(x)
532+
TRUE = true(x.shape)
533+
if is_integer_dtype(x.dtype):
534+
assert_exactly_equal(a, TRUE)
535+
# Test that isfinite, isinf, and isnan are self-consistent.
536+
inf = logical_or(_array_module.isinf(x), _array_module.isnan(x))
537+
assert_exactly_equal(a, logical_not(inf))
538+
539+
# Test the exact value by comparing to the math version
540+
if is_float_dtype(x.dtype):
541+
for idx in ndindex(x.shape):
542+
s = float(x[idx])
543+
assert bool(a[idx]) == math.isfinite(s)
531544

532545
@given(numeric_scalars)
533546
def test_isinf(x):
534-
# a = _array_module.isinf(x)
535-
pass
547+
a = _array_module.isinf(x)
548+
FALSE = false(x.shape)
549+
if is_integer_dtype(x.dtype):
550+
assert_exactly_equal(a, FALSE)
551+
finite_or_nan = logical_or(_array_module.isfinite(x), _array_module.isnan(x))
552+
assert_exactly_equal(a, logical_not(finite_or_nan))
553+
554+
# Test the exact value by comparing to the math version
555+
if is_float_dtype(x.dtype):
556+
for idx in ndindex(x.shape):
557+
s = float(x[idx])
558+
assert bool(a[idx]) == math.isinf(s)
536559

537560
@given(numeric_scalars)
538561
def test_isnan(x):
539-
# a = _array_module.isnan(x)
540-
pass
562+
a = _array_module.isnan(x)
563+
FALSE = false(x.shape)
564+
if is_integer_dtype(x.dtype):
565+
assert_exactly_equal(a, FALSE)
566+
finite_or_inf = logical_or(_array_module.isfinite(x), _array_module.isinf(x))
567+
assert_exactly_equal(a, logical_not(finite_or_inf))
568+
569+
# Test the exact value by comparing to the math version
570+
if is_float_dtype(x.dtype):
571+
for idx in ndindex(x.shape):
572+
s = float(x[idx])
573+
assert bool(a[idx]) == math.isnan(s)
541574

542575
@given(two_numeric_dtypes.flatmap(lambda i: two_array_scalars(*i)))
543576
def test_less(args):

0 commit comments

Comments
 (0)