Skip to content

Commit be3b904

Browse files
committed
add more tests for floor_divide
1 parent e3d4727 commit be3b904

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
@@ -1061,6 +1061,109 @@ def test_invalid_out(self, out):
10611061
assert_raises(TypeError, numpy.divide, a.asnumpy(), 2, out)
10621062

10631063

1064+
class TestFloordivide:
1065+
@pytest.mark.parametrize(
1066+
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1067+
)
1068+
def test_floor_divide(self, dtype):
1069+
array1_data = numpy.arange(10)
1070+
array2_data = numpy.arange(5, 15)
1071+
out = numpy.empty(10, dtype=dtype)
1072+
1073+
# DPNP
1074+
dp_array1 = dpnp.array(array1_data, dtype=dtype)
1075+
dp_array2 = dpnp.array(array2_data, dtype=dtype)
1076+
dp_out = dpnp.array(out, dtype=dtype)
1077+
result = dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1078+
1079+
# original
1080+
np_array1 = numpy.array(array1_data, dtype=dtype)
1081+
np_array2 = numpy.array(array2_data, dtype=dtype)
1082+
expected = numpy.floor_divide(np_array1, np_array2, out=out)
1083+
1084+
assert_allclose(result, expected)
1085+
assert_allclose(dp_out, out)
1086+
1087+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1088+
@pytest.mark.parametrize(
1089+
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1090+
)
1091+
def test_out_dtypes(self, dtype):
1092+
size = 10
1093+
1094+
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
1095+
np_array2 = numpy.arange(size, dtype=dtype)
1096+
np_out = numpy.empty(size, dtype=numpy.complex64)
1097+
expected = numpy.floor_divide(np_array1, np_array2, out=np_out)
1098+
1099+
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
1100+
dp_array2 = dpnp.arange(size, dtype=dtype)
1101+
1102+
dp_out = dpnp.empty(size, dtype=dpnp.complex64)
1103+
if dtype != dpnp.complex64:
1104+
# dtype of out mismatches types of input arrays
1105+
with pytest.raises(TypeError):
1106+
dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1107+
1108+
# allocate new out with expected type
1109+
dp_out = dpnp.empty(size, dtype=dtype)
1110+
1111+
result = dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1112+
assert_allclose(result, expected)
1113+
1114+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1115+
@pytest.mark.parametrize(
1116+
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1117+
)
1118+
def test_out_overlap(self, dtype):
1119+
size = 15
1120+
# DPNP
1121+
dp_a = dpnp.arange(2 * size, dtype=dtype)
1122+
dpnp.floor_divide(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1123+
1124+
# original
1125+
np_a = numpy.arange(2 * size, dtype=dtype)
1126+
numpy.floor_divide(np_a[size::], np_a[::2], out=np_a[:size:])
1127+
1128+
assert_allclose(dp_a, np_a)
1129+
1130+
@pytest.mark.parametrize(
1131+
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1132+
)
1133+
def test_inplace_strided_out(self, dtype):
1134+
size = 21
1135+
1136+
np_a = numpy.arange(size, dtype=dtype)
1137+
np_a[::3] //= 4
1138+
1139+
dp_a = dpnp.arange(size, dtype=dtype)
1140+
dp_a[::3] //= 4
1141+
1142+
assert_allclose(dp_a, np_a)
1143+
1144+
@pytest.mark.parametrize(
1145+
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
1146+
)
1147+
def test_invalid_shape(self, shape):
1148+
dp_array1 = dpnp.arange(10)
1149+
dp_array2 = dpnp.arange(5, 15)
1150+
dp_out = dpnp.empty(shape)
1151+
1152+
with pytest.raises(ValueError):
1153+
dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1154+
1155+
@pytest.mark.parametrize(
1156+
"out",
1157+
[4, (), [], (3, 7), [2, 4]],
1158+
ids=["4", "()", "[]", "(3, 7)", "[2, 4]"],
1159+
)
1160+
def test_invalid_out(self, out):
1161+
a = dpnp.arange(10)
1162+
1163+
assert_raises(TypeError, dpnp.floor_divide, a, 2, out)
1164+
assert_raises(TypeError, numpy.floor_divide, a.asnumpy(), 2, out)
1165+
1166+
10641167
class TestFmax:
10651168
@pytest.mark.parametrize(
10661169
"dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)
@@ -1837,17 +1940,3 @@ def test_inplace_remainder(dtype):
18371940
dp_a %= 4
18381941

18391942
assert_allclose(dp_a, np_a)
1840-
1841-
1842-
@pytest.mark.parametrize(
1843-
"dtype", get_all_dtypes(no_bool=True, no_none=True, no_complex=True)
1844-
)
1845-
def test_inplace_floor_divide(dtype):
1846-
size = 21
1847-
np_a = numpy.arange(size, dtype=dtype)
1848-
dp_a = dpnp.arange(size, dtype=dtype)
1849-
1850-
np_a //= 4
1851-
dp_a //= 4
1852-
1853-
assert_allclose(dp_a, np_a)

0 commit comments

Comments
 (0)