-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtest_constants.py
56 lines (42 loc) · 1.48 KB
/
test_constants.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import math
from typing import Any, SupportsFloat
import pytest
from . import dtype_helpers as dh
from . import xp
from .typing import Array
def assert_scalar_float(name: str, c: Any):
assert isinstance(c, SupportsFloat), f"{name}={c!r} does not look like a float"
def assert_0d_float(name: str, x: Array):
assert dh.is_float_dtype(
x.dtype
), f"xp.asarray(xp.{name})={x!r}, but should have float dtype"
@pytest.mark.parametrize("name, n", [("e", math.e), ("pi", math.pi)])
def test_irrational_numbers(name, n):
assert hasattr(xp, name)
c = getattr(xp, name)
assert_scalar_float(name, c)
floor = math.floor(n)
assert c > floor, f"xp.{name}={c!r} <= {floor}"
ceil = math.ceil(n)
assert c < ceil, f"xp.{name}={c!r} >= {ceil}"
x = xp.asarray(c)
assert_0d_float("name", x)
def test_inf():
assert hasattr(xp, "inf")
assert_scalar_float("inf", xp.inf)
assert math.isinf(xp.inf)
assert xp.inf > 0, "xp.inf not greater than 0"
x = xp.asarray(xp.inf)
assert_0d_float("inf", x)
assert xp.isinf(x), "xp.isinf(xp.asarray(xp.inf))=False"
def test_nan():
assert hasattr(xp, "nan")
assert_scalar_float("nan", xp.nan)
assert math.isnan(xp.nan)
assert xp.nan != xp.nan, "xp.nan should not have equality with itself"
x = xp.asarray(xp.nan)
assert_0d_float("nan", x)
assert xp.isnan(x), "xp.isnan(xp.asarray(xp.nan))=False"
def test_newaxis():
assert hasattr(xp, "newaxis")
assert xp.newaxis is None