Skip to content

Commit 8f22e27

Browse files
committed
Add tests for an instance of DPNPUnaryTwoOutputsFunc class, covering both frexp and modf functions
1 parent cc5a2c1 commit 8f22e27

File tree

5 files changed

+160
-167
lines changed

5 files changed

+160
-167
lines changed

dpnp/tests/test_arithmetic.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

dpnp/tests/test_binary_ufuncs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
"""
2929
The scope includes tests with only functions which are instances of
30-
`DPNPUnaryFunc` class.
30+
`DPNPBinaryFunc` class.
3131
3232
"""
3333

dpnp/tests/test_mathematical.py

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
has_support_aspect16,
3434
has_support_aspect64,
3535
is_intel_numpy,
36-
is_win_platform,
3736
numpy_version,
3837
)
3938
from .third_party.cupy import testing
@@ -711,133 +710,6 @@ def test_errors(self):
711710
assert_raises(ExecutionPlacementError, dpnp.ediff1d, ia, to_end=to_end)
712711

713712

714-
class TestFrexp:
715-
ALL_DTYPES = get_all_dtypes(no_none=True)
716-
ALL_DTYPES_NO_COMPLEX = get_all_dtypes(
717-
no_none=True, no_float16=False, no_complex=True
718-
)
719-
ALL_FLOAT_DTYPES = get_float_dtypes(no_float16=False)
720-
721-
@pytest.mark.parametrize("dt", ALL_DTYPES_NO_COMPLEX)
722-
def test_basic(self, dt):
723-
a = get_abs_array([-2, 5, 1, 4, 3], dtype=dt)
724-
ia = dpnp.array(a)
725-
726-
res1, res2 = dpnp.frexp(ia)
727-
exp1, exp2 = numpy.frexp(a)
728-
assert_array_equal(res1, exp1)
729-
assert_array_equal(res2, exp2)
730-
731-
@pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES)
732-
def test_out(self, dt):
733-
a = numpy.array(5.7, dtype=dt)
734-
ia = dpnp.array(a)
735-
736-
out1 = numpy.empty((), dtype=dt)
737-
out2 = numpy.empty((), dtype=numpy.int32)
738-
iout1, iout2 = dpnp.array(out1), dpnp.array(out2)
739-
740-
res1, res2 = dpnp.frexp(ia, iout1)
741-
exp1, exp2 = numpy.frexp(a, out1)
742-
assert_array_equal(res1, exp1)
743-
assert_array_equal(res2, exp2)
744-
assert res1 is iout1
745-
746-
res1, res2 = dpnp.frexp(ia, None, iout2)
747-
exp1, exp2 = numpy.frexp(a, None, out2)
748-
assert_array_equal(res1, exp1)
749-
assert_array_equal(res2, exp2)
750-
assert res2 is iout2
751-
752-
res1, res2 = dpnp.frexp(ia, iout1, iout2)
753-
exp1, exp2 = numpy.frexp(a, out1, out2)
754-
assert_array_equal(res1, exp1)
755-
assert_array_equal(res2, exp2)
756-
assert res1 is iout1
757-
assert res2 is iout2
758-
759-
@pytest.mark.parametrize("dt", ALL_DTYPES_NO_COMPLEX)
760-
@pytest.mark.parametrize("out1_dt", ALL_DTYPES)
761-
@pytest.mark.parametrize("out2_dt", ALL_DTYPES)
762-
def test_out_all_dtypes(self, dt, out1_dt, out2_dt):
763-
a = numpy.ones(9, dtype=dt)
764-
ia = dpnp.array(a)
765-
766-
out1 = numpy.zeros(9, dtype=out1_dt)
767-
out2 = numpy.zeros(9, dtype=out2_dt)
768-
iout1, iout2 = dpnp.array(out1), dpnp.array(out2)
769-
770-
try:
771-
res1, res2 = dpnp.frexp(ia, out=(iout1, iout2))
772-
except TypeError:
773-
# expect numpy to fail with the same reason
774-
with pytest.raises(TypeError):
775-
_ = numpy.frexp(a, out=(out1, out2))
776-
else:
777-
exp1, exp2 = numpy.frexp(a, out=(out1, out2))
778-
assert_array_equal(res1, exp1)
779-
assert_array_equal(res2, exp2)
780-
assert res1 is iout1
781-
assert res2 is iout2
782-
783-
@pytest.mark.skipif(
784-
is_win_platform(),
785-
reason="numpy.frexp gives different answers for NAN/INF on Windows and Linux",
786-
)
787-
@pytest.mark.parametrize("stride", [-4, -2, -1, 1, 2, 4])
788-
@pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES)
789-
def test_strides_out(self, stride, dt):
790-
a = numpy.array(
791-
[numpy.nan, numpy.nan, numpy.inf, -numpy.inf, 0.0, -0.0, 1.0, -1.0],
792-
dtype=dt,
793-
)
794-
ia = dpnp.array(a)
795-
796-
out_mant = numpy.ones_like(a)
797-
out_exp = 2 * numpy.ones_like(a, dtype="i")
798-
iout_mant, iout_exp = dpnp.array(out_mant), dpnp.array(out_exp)
799-
800-
res1, res2 = dpnp.frexp(
801-
ia[::stride], out=(iout_mant[::stride], iout_exp[::stride])
802-
)
803-
exp1, exp2 = numpy.frexp(
804-
a[::stride], out=(out_mant[::stride], out_exp[::stride])
805-
)
806-
assert_array_equal(res1, exp1)
807-
assert_array_equal(res2, exp2)
808-
809-
assert_array_equal(iout_mant, out_mant)
810-
assert_array_equal(iout_exp, out_exp)
811-
812-
@pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES)
813-
def test_out1_overlap(self, dt):
814-
size = 15
815-
a = numpy.ones(2 * size, dtype=dt)
816-
ia = dpnp.array(a)
817-
818-
# out1 overlaps memory of input array
819-
_ = dpnp.frexp(ia[size::], ia[::2])
820-
_ = numpy.frexp(a[size::], a[::2])
821-
assert_array_equal(ia, a)
822-
823-
@pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES)
824-
def test_empty(self, dt):
825-
a = numpy.empty(0, dtype=dt)
826-
ia = dpnp.array(a)
827-
828-
res1, res2 = dpnp.frexp(ia)
829-
exp1, exp2 = numpy.frexp(a)
830-
assert_array_equal(res1, exp1, strict=True)
831-
assert_array_equal(res2, exp2, strict=True)
832-
833-
@pytest.mark.parametrize("xp", [numpy, dpnp])
834-
@pytest.mark.parametrize("dt", get_complex_dtypes())
835-
def test_complex_dtype(self, xp, dt):
836-
a = xp.array([-2, 5, 1, 4, 3], dtype=dt)
837-
with pytest.raises((TypeError, ValueError)):
838-
_ = xp.frexp(a)
839-
840-
841713
class TestGradient:
842714
@pytest.mark.parametrize("dt", get_all_dtypes(no_none=True, no_bool=True))
843715
def test_basic(self, dt):
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import numpy
2+
import pytest
3+
from numpy.testing import (
4+
assert_array_equal,
5+
)
6+
7+
import dpnp
8+
9+
from .helper import (
10+
generate_random_numpy_array,
11+
get_all_dtypes,
12+
get_complex_dtypes,
13+
get_float_dtypes,
14+
is_win_platform,
15+
)
16+
17+
"""
18+
The scope includes tests with only functions which are instances of
19+
`DPNPUnaryTwoOutputsFunc` class.
20+
21+
"""
22+
23+
24+
@pytest.mark.parametrize("func", ["frexp", "modf"])
25+
class TestUnaryTwoOutputs:
26+
ALL_DTYPES = get_all_dtypes(no_none=True)
27+
ALL_DTYPES_NO_COMPLEX = get_all_dtypes(
28+
no_none=True, no_float16=False, no_complex=True
29+
)
30+
ALL_FLOAT_DTYPES = get_float_dtypes(no_float16=False)
31+
32+
def _get_out_dtypes(self, func, dt):
33+
if func == "frexp":
34+
return (dt, numpy.int32)
35+
return (dt, dt)
36+
37+
@pytest.mark.parametrize("dt", ALL_DTYPES_NO_COMPLEX)
38+
def test_basic(self, func, dt):
39+
a = generate_random_numpy_array((2, 5), dtype=dt)
40+
ia = dpnp.array(a)
41+
42+
res1, res2 = getattr(dpnp, func)(ia)
43+
exp1, exp2 = getattr(numpy, func)(a)
44+
assert_array_equal(res1, exp1)
45+
assert_array_equal(res2, exp2)
46+
47+
@pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES)
48+
def test_out(self, func, dt):
49+
a = numpy.array(5.7, dtype=dt)
50+
ia = dpnp.array(a)
51+
52+
dt1, dt2 = self._get_out_dtypes(func, dt)
53+
out1 = numpy.empty((), dtype=dt1)
54+
out2 = numpy.empty((), dtype=dt2)
55+
iout1, iout2 = dpnp.array(out1), dpnp.array(out2)
56+
57+
res1, res2 = getattr(dpnp, func)(ia, iout1)
58+
exp1, exp2 = getattr(numpy, func)(a, out1)
59+
assert_array_equal(res1, exp1)
60+
assert_array_equal(res2, exp2)
61+
assert res1 is iout1
62+
63+
res1, res2 = getattr(dpnp, func)(ia, None, iout2)
64+
exp1, exp2 = getattr(numpy, func)(a, None, out2)
65+
assert_array_equal(res1, exp1)
66+
assert_array_equal(res2, exp2)
67+
assert res2 is iout2
68+
69+
res1, res2 = getattr(dpnp, func)(ia, iout1, iout2)
70+
exp1, exp2 = getattr(numpy, func)(a, out1, out2)
71+
assert_array_equal(res1, exp1)
72+
assert_array_equal(res2, exp2)
73+
assert res1 is iout1
74+
assert res2 is iout2
75+
76+
@pytest.mark.parametrize("dt", ALL_DTYPES_NO_COMPLEX)
77+
@pytest.mark.parametrize("out1_dt", ALL_DTYPES)
78+
@pytest.mark.parametrize("out2_dt", ALL_DTYPES)
79+
def test_out_all_dtypes(self, func, dt, out1_dt, out2_dt):
80+
a = numpy.ones(9, dtype=dt)
81+
ia = dpnp.array(a)
82+
83+
out1 = numpy.zeros(9, dtype=out1_dt)
84+
out2 = numpy.zeros(9, dtype=out2_dt)
85+
iout1, iout2 = dpnp.array(out1), dpnp.array(out2)
86+
87+
try:
88+
res1, res2 = getattr(dpnp, func)(ia, out=(iout1, iout2))
89+
except TypeError:
90+
# expect numpy to fail with the same reason
91+
with pytest.raises(TypeError):
92+
_ = getattr(numpy, func)(a, out=(out1, out2))
93+
else:
94+
exp1, exp2 = getattr(numpy, func)(a, out=(out1, out2))
95+
assert_array_equal(res1, exp1)
96+
assert_array_equal(res2, exp2)
97+
assert res1 is iout1
98+
assert res2 is iout2
99+
100+
@pytest.mark.parametrize("stride", [-4, -2, -1, 1, 2, 4])
101+
@pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES)
102+
def test_strides_out(self, func, stride, dt):
103+
if func == "frexp" and is_win_platform():
104+
pytest.skip(
105+
"numpy.frexp gives different answers for NAN/INF on Windows and Linux"
106+
)
107+
108+
a = numpy.array(
109+
[numpy.nan, numpy.nan, numpy.inf, -numpy.inf, 0.0, -0.0, 1.0, -1.0],
110+
dtype=dt,
111+
)
112+
ia = dpnp.array(a)
113+
114+
dt1, dt2 = self._get_out_dtypes(func, dt)
115+
out_mant = numpy.ones_like(a, dtype=dt1)
116+
out_exp = 2 * numpy.ones_like(a, dtype=dt2)
117+
iout_mant, iout_exp = dpnp.array(out_mant), dpnp.array(out_exp)
118+
119+
res1, res2 = getattr(dpnp, func)(
120+
ia[::stride], out=(iout_mant[::stride], iout_exp[::stride])
121+
)
122+
exp1, exp2 = getattr(numpy, func)(
123+
a[::stride], out=(out_mant[::stride], out_exp[::stride])
124+
)
125+
assert_array_equal(res1, exp1)
126+
assert_array_equal(res2, exp2)
127+
128+
assert_array_equal(iout_mant, out_mant)
129+
assert_array_equal(iout_exp, out_exp)
130+
131+
@pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES)
132+
def test_out1_overlap(self, func, dt):
133+
size = 15
134+
a = numpy.ones(2 * size, dtype=dt)
135+
ia = dpnp.array(a)
136+
137+
# out1 overlaps memory of input array
138+
_ = getattr(dpnp, func)(ia[size::], ia[::2])
139+
_ = getattr(numpy, func)(a[size::], a[::2])
140+
assert_array_equal(ia, a)
141+
142+
@pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES)
143+
def test_empty(self, func, dt):
144+
a = numpy.empty(0, dtype=dt)
145+
ia = dpnp.array(a)
146+
147+
res1, res2 = getattr(dpnp, func)(ia)
148+
exp1, exp2 = getattr(numpy, func)(a)
149+
assert_array_equal(res1, exp1, strict=True)
150+
assert_array_equal(res2, exp2, strict=True)
151+
152+
@pytest.mark.parametrize("xp", [numpy, dpnp])
153+
@pytest.mark.parametrize("dt", get_complex_dtypes())
154+
def test_complex_dtype(self, func, xp, dt):
155+
a = xp.array([-2, 5, 1, 4, 3], dtype=dt)
156+
with pytest.raises((TypeError, ValueError)):
157+
_ = getattr(xp, func)(a)

dpnp/tests/third_party/cupy/math_tests/test_arithmetic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import itertools
24
import warnings
35

0 commit comments

Comments
 (0)