Skip to content

Commit 8b1656e

Browse files
committed
add more tests for floor_divide
1 parent 0e50f82 commit 8b1656e

File tree

1 file changed

+95
-14
lines changed

1 file changed

+95
-14
lines changed

tests/test_mathematical.py

Lines changed: 95 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,101 @@ 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("dtype", get_float_dtypes())
1066+
def test_floor_divide(self, dtype):
1067+
array1_data = numpy.arange(10)
1068+
array2_data = numpy.arange(5, 15)
1069+
out = numpy.empty(10, dtype=dtype)
1070+
1071+
# DPNP
1072+
dp_array1 = dpnp.array(array1_data, dtype=dtype)
1073+
dp_array2 = dpnp.array(array2_data, dtype=dtype)
1074+
dp_out = dpnp.array(out, dtype=dtype)
1075+
result = dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1076+
1077+
# original
1078+
np_array1 = numpy.array(array1_data, dtype=dtype)
1079+
np_array2 = numpy.array(array2_data, dtype=dtype)
1080+
expected = numpy.floor_divide(np_array1, np_array2, out=out)
1081+
1082+
assert_dtype_allclose(result, expected)
1083+
assert_dtype_allclose(dp_out, out)
1084+
1085+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1086+
@pytest.mark.parametrize("dtype", get_float_dtypes())
1087+
def test_out_dtypes(self, dtype):
1088+
size = 10
1089+
1090+
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
1091+
np_array2 = numpy.arange(size, dtype=dtype)
1092+
np_out = numpy.empty(size, dtype=numpy.complex64)
1093+
expected = numpy.floor_divide(np_array1, np_array2, out=np_out)
1094+
1095+
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
1096+
dp_array2 = dpnp.arange(size, dtype=dtype)
1097+
1098+
dp_out = dpnp.empty(size, dtype=dpnp.complex64)
1099+
if dtype != dpnp.complex64:
1100+
# dtype of out mismatches types of input arrays
1101+
with pytest.raises(TypeError):
1102+
dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1103+
1104+
# allocate new out with expected type
1105+
dp_out = dpnp.empty(size, dtype=dtype)
1106+
1107+
result = dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1108+
assert_dtype_allclose(result, expected)
1109+
1110+
@pytest.mark.usefixtures("suppress_divide_invalid_numpy_warnings")
1111+
@pytest.mark.parametrize("dtype", get_float_dtypes())
1112+
def test_out_overlap(self, dtype):
1113+
size = 15
1114+
# DPNP
1115+
dp_a = dpnp.arange(2 * size, dtype=dtype)
1116+
dpnp.floor_divide(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1117+
1118+
# original
1119+
np_a = numpy.arange(2 * size, dtype=dtype)
1120+
numpy.floor_divide(np_a[size::], np_a[::2], out=np_a[:size:])
1121+
1122+
assert_dtype_allclose(dp_a, np_a)
1123+
1124+
@pytest.mark.parametrize("dtype", get_float_dtypes())
1125+
def test_inplace_strided_out(self, dtype):
1126+
size = 21
1127+
1128+
np_a = numpy.arange(size, dtype=dtype)
1129+
np_a[::3] //= 4
1130+
1131+
dp_a = dpnp.arange(size, dtype=dtype)
1132+
dp_a[::3] //= 4
1133+
1134+
assert_allclose(dp_a, np_a)
1135+
1136+
@pytest.mark.parametrize(
1137+
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
1138+
)
1139+
def test_invalid_shape(self, shape):
1140+
dp_array1 = dpnp.arange(10)
1141+
dp_array2 = dpnp.arange(5, 15)
1142+
dp_out = dpnp.empty(shape)
1143+
1144+
with pytest.raises(ValueError):
1145+
dpnp.floor_divide(dp_array1, dp_array2, out=dp_out)
1146+
1147+
@pytest.mark.parametrize(
1148+
"out",
1149+
[4, (), [], (3, 7), [2, 4]],
1150+
ids=["4", "()", "[]", "(3, 7)", "[2, 4]"],
1151+
)
1152+
def test_invalid_out(self, out):
1153+
a = dpnp.arange(10)
1154+
1155+
assert_raises(TypeError, dpnp.floor_divide, a, 2, out)
1156+
assert_raises(TypeError, numpy.floor_divide, a.asnumpy(), 2, out)
1157+
1158+
10641159
class TestFmax:
10651160
@pytest.mark.parametrize(
10661161
"dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)
@@ -1836,17 +1931,3 @@ def test_inplace_remainder(dtype):
18361931
dp_a %= 4
18371932

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

0 commit comments

Comments
 (0)