Skip to content

Commit 2260ae0

Browse files
authored
Merge pull request #52 from honno/constants
Change scope of `test_constants.py`
2 parents 36e2d65 + b1b95c6 commit 2260ae0

File tree

1 file changed

+42
-25
lines changed

1 file changed

+42
-25
lines changed

array_api_tests/test_constants.py

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,51 @@
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
2+
from typing import Any, SupportsFloat
43

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

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"
6+
from . import dtype_helpers as dh
7+
from ._array_module import mod as xp
8+
from .typing import Array
129

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

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 π"
11+
def assert_scalar_float(name: str, c: Any):
12+
assert isinstance(c, SupportsFloat), f"{name}={c!r} does not look like a float"
13+
14+
15+
def assert_0d_float(name: str, x: Array):
16+
assert dh.is_float_dtype(
17+
x.dtype
18+
), f"xp.asarray(xp.{name})={x!r}, but should have float dtype"
19+
20+
21+
@pytest.mark.parametrize("name, n", [("e", math.e), ("pi", math.pi)])
22+
def test_irrational_numbers(name, n):
23+
assert hasattr(xp, name)
24+
c = getattr(xp, name)
25+
assert_scalar_float(name, c)
26+
floor = math.floor(n)
27+
assert c > floor, f"xp.{name}={c!r} <= {floor}"
28+
ceil = math.ceil(n)
29+
assert c < ceil, f"xp.{name}={c!r} >= {ceil}"
30+
x = xp.asarray(c)
31+
assert_0d_float("name", x)
32+
2033

2134
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)
35+
assert hasattr(xp, "inf")
36+
assert_scalar_float("inf", xp.inf)
37+
assert math.isinf(xp.inf)
38+
assert xp.inf > 0, "xp.inf not greater than 0"
39+
x = xp.asarray(xp.inf)
40+
assert_0d_float("inf", x)
41+
assert xp.isinf(x), "xp.isinf(xp.asarray(xp.inf))=False"
2542

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

2944
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"
45+
assert hasattr(xp, "nan")
46+
assert_scalar_float("nan", xp.nan)
47+
assert math.isnan(xp.nan)
48+
assert xp.nan != xp.nan, "xp.nan should not have equality with itself"
49+
x = xp.asarray(xp.nan)
50+
assert_0d_float("nan", x)
51+
assert xp.isnan(x), "xp.isnan(xp.asarray(xp.nan))=False"

0 commit comments

Comments
 (0)