Skip to content

Commit f36af57

Browse files
Adds tests for atol/rtol
Also added tests for early exits to improve coverage.
1 parent ea6dd27 commit f36af57

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

dpctl/tests/test_tensor_testing.py

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,64 @@ def test_allclose_validation():
8686
dpt.allclose(x, False)
8787

8888

89-
def test_all_close_promotion():
89+
def test_allclose_type_promotion():
9090
get_queue_or_skip()
9191

9292
x1 = dpt.ones(10, dtype="i4")
9393
x2 = dpt.ones(10, dtype="i8")
9494

9595
assert dpt.allclose(x1, x2)
96+
97+
98+
def test_allclose_tolerance():
99+
get_queue_or_skip()
100+
101+
x = dpt.zeros(10, dtype="f4")
102+
atol = 1e-5
103+
y = dpt.full_like(x, atol)
104+
assert dpt.allclose(x, y, atol=atol, rtol=0)
105+
106+
# about 8e-6
107+
tol = float.fromhex("0x1.0p-17")
108+
x = dpt.ones(10, dtype="f4")
109+
y = x - tol
110+
assert dpt.allclose(x, y, atol=0, rtol=tol)
111+
112+
113+
def test_allclose_real_fp_early_exists():
114+
get_queue_or_skip()
115+
116+
x1 = dpt.asarray([0.0, dpt.inf, -dpt.inf], dtype="f4")
117+
x2 = dpt.asarray([dpt.inf, 0.0, -dpt.inf], dtype="f4")
118+
119+
# early exists, inf positions are different
120+
assert not dpt.allclose(x1, x2)
121+
122+
x2 = dpt.asarray([0.0, -dpt.inf, dpt.inf], dtype="f4")
123+
124+
# early exists, inf positions are the same, but signs differ
125+
assert not dpt.allclose(x1, x2)
126+
127+
128+
def test_allclose_complex_fp_early_exists():
129+
get_queue_or_skip()
130+
131+
x1 = dpt.asarray([0.0, dpt.inf, -dpt.inf], dtype="c8")
132+
x2 = dpt.asarray([dpt.inf, 0.0, -dpt.inf], dtype="c8")
133+
134+
# early exists, inf positions of real parts are different
135+
assert not dpt.allclose(x1, x2)
136+
137+
x2 = dpt.asarray([0.0, -dpt.inf, dpt.inf], dtype="c8")
138+
139+
# early exists, inf positions of real parts are the same, but signs differ
140+
assert not dpt.allclose(x1, x2)
141+
142+
x1 = dpt.asarray([0.0, dpt.inf * 1j, -dpt.inf * 1j], dtype="c8")
143+
x2 = dpt.asarray([dpt.inf * 1j, 0.0, -dpt.inf * 1j], dtype="c8")
144+
145+
# early exists, inf positions of imag parts are different
146+
assert not dpt.allclose(x1, x2)
147+
148+
x2 = dpt.asarray([0.0, -dpt.inf * 1j, dpt.inf * 1j], dtype="c8")
149+
assert not dpt.allclose(x1, x2)

0 commit comments

Comments
 (0)