Skip to content

Commit 1bd08b5

Browse files
committed
Add more dpnp tests to cover different use cases
1 parent 0012e7a commit 1bd08b5

File tree

1 file changed

+107
-13
lines changed

1 file changed

+107
-13
lines changed

tests/test_counting.py

Lines changed: 107 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,124 @@
11
import numpy
22
import pytest
3+
from dpctl.tensor._numpy_helper import AxisError
34
from numpy.testing import (
45
assert_allclose,
6+
assert_equal,
7+
assert_raises,
58
)
69

710
import dpnp
811

912
from .helper import (
1013
get_all_dtypes,
14+
get_float_dtypes,
1115
)
1216

1317

14-
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
15-
@pytest.mark.parametrize("size", [2, 4, 8, 16, 3, 9, 27, 81])
16-
def test_count_nonzero(dtype, size):
17-
if dtype != dpnp.bool:
18-
a = numpy.arange(size, dtype=dtype)
19-
else:
20-
a = numpy.resize(numpy.arange(2, dtype=dtype), size)
18+
class TestCountNonZero:
19+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
20+
@pytest.mark.parametrize("size", [2, 4, 8, 16, 3, 9, 27, 81])
21+
def test_basic(self, dtype, size):
22+
if dtype != dpnp.bool:
23+
a = numpy.arange(size, dtype=dtype)
24+
else:
25+
a = numpy.resize(numpy.arange(2, dtype=dtype), size)
2126

22-
for i in range(int(size / 2)):
23-
a[(i * (int(size / 3) - 1)) % size] = 0
27+
for i in range(int(size / 2)):
28+
a[(i * (int(size / 3) - 1)) % size] = 0
2429

25-
ia = dpnp.array(a)
30+
ia = dpnp.array(a)
2631

27-
np_res = numpy.count_nonzero(a)
28-
dpnp_res = dpnp.count_nonzero(ia)
32+
result = dpnp.count_nonzero(ia)
33+
expected = numpy.count_nonzero(a)
34+
assert_allclose(result, expected)
2935

30-
assert_allclose(dpnp_res, np_res)
36+
@pytest.mark.parametrize("data", [[], [0], [1]])
37+
def test_trivial(self, data):
38+
a = numpy.array(data)
39+
ia = dpnp.array(a)
40+
41+
result = dpnp.count_nonzero(ia)
42+
expected = numpy.count_nonzero(a)
43+
assert_allclose(result, expected)
44+
45+
@pytest.mark.parametrize("data", [[], [0], [1]])
46+
def test_trivial_boolean_dtype(self, data):
47+
a = numpy.array(data, dtype="?")
48+
ia = dpnp.array(a)
49+
50+
result = dpnp.count_nonzero(ia)
51+
expected = numpy.count_nonzero(a)
52+
assert_allclose(result, expected)
53+
54+
@pytest.mark.parametrize("axis", [0, 1])
55+
def test_axis_basic(self, axis):
56+
a = numpy.array([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]])
57+
ia = dpnp.array(a)
58+
59+
result = dpnp.count_nonzero(ia, axis=axis)
60+
expected = numpy.count_nonzero(a, axis=axis)
61+
assert_equal(result, expected)
62+
63+
@pytest.mark.parametrize("xp", [numpy, dpnp])
64+
def test_axis_raises(self, xp):
65+
a = xp.array([[0, 1, 7, 0, 0], [3, 0, 0, 2, 19]])
66+
67+
assert_raises(ValueError, xp.count_nonzero, a, axis=(1, 1))
68+
assert_raises(TypeError, xp.count_nonzero, a, axis="foo")
69+
assert_raises(AxisError, xp.count_nonzero, a, axis=3)
70+
71+
# different exception type in numpy and dpnp
72+
with pytest.raises((ValueError, TypeError)):
73+
xp.count_nonzero(a, axis=xp.array([[1], [2]]))
74+
75+
@pytest.mark.parametrize("dt", get_all_dtypes(no_none=True))
76+
@pytest.mark.parametrize("axis", [0, 1, (0, 1), None])
77+
def test_axis_all_dtypes(self, dt, axis):
78+
a = numpy.zeros((3, 3), dtype=dt)
79+
a[0, 0] = a[1, 0] = 1
80+
ia = dpnp.array(a)
81+
82+
result = dpnp.count_nonzero(ia, axis=axis)
83+
expected = numpy.count_nonzero(a, axis=axis)
84+
assert_equal(result, expected)
85+
86+
def test_axis_empty(self):
87+
axis = ()
88+
a = numpy.array([[0, 0, 1], [1, 0, 1]])
89+
ia = dpnp.array(a)
90+
91+
result = dpnp.count_nonzero(ia, axis=axis)
92+
expected = numpy.count_nonzero(a, axis=axis)
93+
assert_equal(result, expected)
94+
95+
@pytest.mark.parametrize("axis", [None, 0, 1])
96+
def test_keepdims(self, axis):
97+
a = numpy.array([[0, 0, 1, 0], [0, 3, 5, 0], [7, 9, 2, 0]])
98+
ia = dpnp.array(a)
99+
100+
result = dpnp.count_nonzero(ia, axis=axis, keepdims=True)
101+
expected = numpy.count_nonzero(a, axis=axis, keepdims=True)
102+
assert_equal(result, expected)
103+
104+
@pytest.mark.parametrize("dt", get_all_dtypes(no_none=True))
105+
def test_out(self, dt):
106+
a = numpy.array([[0, 1, 0], [2, 0, 3]], dtype=dt)
107+
ia = dpnp.array(a)
108+
iout = dpnp.empty_like(ia, shape=ia.shape[1], dtype=dpnp.intp)
109+
110+
result = dpnp.count_nonzero(ia, axis=0, out=iout)
111+
expected = numpy.count_nonzero(a, axis=0) # no out keyword
112+
assert_equal(result, expected)
113+
assert result is iout
114+
115+
@pytest.mark.parametrize("dt", get_float_dtypes())
116+
def test_out_floating_dtype(self, dt):
117+
a = dpnp.array([[0, 1, 0], [2, 0, 3]])
118+
out = dpnp.empty_like(a, shape=a.shape[1], dtype=dt)
119+
assert_raises(ValueError, dpnp.count_nonzero, a, axis=0, out=out)
120+
121+
def test_array_method(self):
122+
a = numpy.array([[1, 0, 0], [4, 0, 6]])
123+
ia = dpnp.array(a)
124+
assert_equal(ia.nonzero(), a.nonzero())

0 commit comments

Comments
 (0)