Skip to content

Commit b0b1230

Browse files
committed
add more tests for floor_divide
1 parent ff4788f commit b0b1230

File tree

1 file changed

+103
-14
lines changed

1 file changed

+103
-14
lines changed

tests/test_mathematical.py

Lines changed: 103 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,109 @@ def test_invalid_out(self, out):
11651165
assert_raises(TypeError, numpy.divide, a.asnumpy(), 2, out)
11661166

11671167

1168+
class TestFloordivide:
1169+
@pytest.mark.parametrize(
1170+
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1171+
)
1172+
def test_floor_divide(self, dtype):
1173+
array1_data = numpy.arange(10)
1174+
array2_data = numpy.arange(5, 15)
1175+
out = numpy.empty(10, dtype=dtype)
1176+
1177+
# DPNP
1178+
dp_array1 = dpnp.array(array1_data, dtype=dtype)
1179+
dp_array2 = dpnp.array(array2_data, dtype=dtype)
1180+
dp_out = dpnp.array(out, dtype=dtype)
1181+
result = dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1182+
1183+
# original
1184+
np_array1 = numpy.array(array1_data, dtype=dtype)
1185+
np_array2 = numpy.array(array2_data, dtype=dtype)
1186+
expected = numpy.floor_divide(np_array1, np_array2, out=out)
1187+
1188+
assert_allclose(result, expected)
1189+
assert_allclose(dp_out, out)
1190+
1191+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1192+
@pytest.mark.parametrize(
1193+
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1194+
)
1195+
def test_out_dtypes(self, dtype):
1196+
size = 10
1197+
1198+
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
1199+
np_array2 = numpy.arange(size, dtype=dtype)
1200+
np_out = numpy.empty(size, dtype=numpy.complex64)
1201+
expected = numpy.floor_divide(np_array1, np_array2, out=np_out)
1202+
1203+
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
1204+
dp_array2 = dpnp.arange(size, dtype=dtype)
1205+
1206+
dp_out = dpnp.empty(size, dtype=dpnp.complex64)
1207+
if dtype != dpnp.complex64:
1208+
# dtype of out mismatches types of input arrays
1209+
with pytest.raises(TypeError):
1210+
dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1211+
1212+
# allocate new out with expected type
1213+
dp_out = dpnp.empty(size, dtype=dtype)
1214+
1215+
result = dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1216+
assert_allclose(result, expected)
1217+
1218+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1219+
@pytest.mark.parametrize(
1220+
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1221+
)
1222+
def test_out_overlap(self, dtype):
1223+
size = 15
1224+
# DPNP
1225+
dp_a = dpnp.arange(2 * size, dtype=dtype)
1226+
dpnp.floor_divide(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1227+
1228+
# original
1229+
np_a = numpy.arange(2 * size, dtype=dtype)
1230+
numpy.floor_divide(np_a[size::], np_a[::2], out=np_a[:size:])
1231+
1232+
assert_allclose(dp_a, np_a)
1233+
1234+
@pytest.mark.parametrize(
1235+
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1236+
)
1237+
def test_inplace_strided_out(self, dtype):
1238+
size = 21
1239+
1240+
np_a = numpy.arange(size, dtype=dtype)
1241+
np_a[::3] //= 4
1242+
1243+
dp_a = dpnp.arange(size, dtype=dtype)
1244+
dp_a[::3] //= 4
1245+
1246+
assert_allclose(dp_a, np_a)
1247+
1248+
@pytest.mark.parametrize(
1249+
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
1250+
)
1251+
def test_invalid_shape(self, shape):
1252+
dp_array1 = dpnp.arange(10)
1253+
dp_array2 = dpnp.arange(5, 15)
1254+
dp_out = dpnp.empty(shape)
1255+
1256+
with pytest.raises(ValueError):
1257+
dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1258+
1259+
@pytest.mark.parametrize(
1260+
"out",
1261+
[4, (), [], (3, 7), [2, 4]],
1262+
ids=["4", "()", "[]", "(3, 7)", "[2, 4]"],
1263+
)
1264+
def test_invalid_out(self, out):
1265+
a = dpnp.arange(10)
1266+
1267+
assert_raises(TypeError, dpnp.floor_divide, a, 2, out)
1268+
assert_raises(TypeError, numpy.floor_divide, a.asnumpy(), 2, out)
1269+
1270+
11681271
class TestFmax:
11691272
@pytest.mark.parametrize(
11701273
"dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)
@@ -1941,17 +2044,3 @@ def test_inplace_remainder(dtype):
19412044
dp_a %= 4
19422045

19432046
assert_allclose(dp_a, np_a)
1944-
1945-
1946-
@pytest.mark.parametrize(
1947-
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1948-
)
1949-
def test_inplace_floor_divide(dtype):
1950-
size = 21
1951-
np_a = numpy.arange(size, dtype=dtype)
1952-
dp_a = dpnp.arange(size, dtype=dtype)
1953-
1954-
np_a //= 4
1955-
dp_a //= 4
1956-
1957-
assert_allclose(dp_a, np_a)

0 commit comments

Comments
 (0)