Skip to content

Commit 5daa5d0

Browse files
committed
Reduce scope of constant tests
1 parent 8b8ac96 commit 5daa5d0

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

array_api_tests/test_constants.py

+34-25
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,43 @@
1-
from ._array_module import (e, inf, nan, pi, equal, isnan, abs, full,
2-
float64, less, isinf, greater, all)
3-
from .array_helpers import one
1+
import math
42

5-
def test_e():
6-
# Check that e acts as a scalar
7-
E = full((1,), e, dtype=float64)
3+
import pytest
84

9-
# We don't require any accuracy. This is just a smoke test to check that
10-
# 'e' is actually the constant e.
11-
assert all(less(abs(E - 2.71), one((1,), dtype=float64))), "e is not the constant e"
5+
from . import dtype_helpers as dh
6+
from ._array_module import mod as xp
7+
from .typing import Array
128

13-
def test_pi():
14-
# Check that pi acts as a scalar
15-
PI = full((1,), pi, dtype=float64)
169

17-
# We don't require any accuracy. This is just a smoke test to check that
18-
# 'pi' is actually the constant π.
19-
assert all(less(abs(PI - 3.14), one((1,), dtype=float64))), "pi is not the constant π"
10+
def assert_0d_float(name: str, x: Array):
11+
assert dh.is_float_dtype(
12+
x.dtype
13+
), f"xp.asarray(xp.{name})={x!r}, but should have float dtype"
14+
15+
16+
@pytest.mark.parametrize("name, n", [("e", math.e), ("pi", math.pi)])
17+
def test_irrational(name, n):
18+
assert hasattr(xp, name)
19+
c = getattr(xp, name)
20+
floor = math.floor(n)
21+
assert c > floor, f"xp.{name}={c} <= {floor}"
22+
ceil = math.ceil(n)
23+
assert c < ceil, f"xp.{name}={c} >= {ceil}"
24+
x = xp.asarray(c)
25+
assert_0d_float("name", x)
26+
2027

2128
def test_inf():
22-
# Check that inf acts as a scalar
23-
INF = full((1,), inf, dtype=float64)
24-
zero = full((1,), 0.0, dtype=float64)
29+
assert hasattr(xp, "inf")
30+
assert math.isinf(xp.inf)
31+
assert xp.inf > 0, "xp.inf not greater than 0"
32+
x = xp.asarray(xp.inf)
33+
assert_0d_float("inf", x)
34+
assert xp.isinf(x), "xp.isinf(xp.asarray(xp.inf))=False"
2535

26-
assert all(isinf(INF)), "inf is not infinity"
27-
assert all(greater(INF, zero)), "inf is not positive"
2836

2937
def test_nan():
30-
# Check that nan acts as a scalar
31-
NAN = full((1,), nan, dtype=float64)
32-
33-
assert all(isnan(NAN)), "nan is not Not a Number"
34-
assert not all(equal(NAN, NAN)), "nan should be unequal to itself"
38+
assert hasattr(xp, "nan")
39+
assert math.isnan(xp.nan)
40+
assert xp.nan != xp.nan, "xp.nan should not have equality with itself"
41+
x = xp.asarray(xp.nan)
42+
assert_0d_float("nan", x)
43+
assert xp.isnan(x), "xp.isnan(xp.asarray(xp.nan))=False"

0 commit comments

Comments
 (0)