Skip to content

Commit c80e404

Browse files
committed
add new tests
1 parent 2c91a90 commit c80e404

File tree

1 file changed

+324
-0
lines changed

1 file changed

+324
-0
lines changed

tests/test_mathematical.py

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,330 @@ def test_invalid_out(self, out):
940940
assert_raises(TypeError, numpy.add, a.asnumpy(), 2, out)
941941

942942

943+
class TestFmax:
944+
@pytest.mark.parametrize(
945+
"dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)
946+
)
947+
def test_fmax(self, dtype):
948+
array1_data = numpy.arange(10)
949+
array2_data = numpy.arange(5, 15)
950+
out = numpy.empty(10, dtype=dtype)
951+
952+
# DPNP
953+
dp_array1 = dpnp.array(array1_data, dtype=dtype)
954+
dp_array2 = dpnp.array(array2_data, dtype=dtype)
955+
dp_out = dpnp.array(out, dtype=dtype)
956+
result = dpnp.fmax(dp_array1, dp_array2, out=dp_out)
957+
958+
# original
959+
np_array1 = numpy.array(array1_data, dtype=dtype)
960+
np_array2 = numpy.array(array2_data, dtype=dtype)
961+
expected = numpy.fmax(np_array1, np_array2, out=out)
962+
963+
assert_allclose(expected, result)
964+
assert_allclose(out, dp_out)
965+
966+
@pytest.mark.parametrize(
967+
"dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)
968+
)
969+
def test_out_dtypes(self, dtype):
970+
size = 10
971+
972+
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
973+
np_array2 = numpy.arange(size, dtype=dtype)
974+
np_out = numpy.empty(size, dtype=numpy.float32)
975+
expected = numpy.fmax(np_array1, np_array2, out=np_out)
976+
977+
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
978+
dp_array2 = dpnp.arange(size, dtype=dtype)
979+
with pytest.raises(TypeError):
980+
dpnp.fmax(dp_array1, dp_array2, out=np_out)
981+
982+
dp_out = dpnp.empty(size, dtype=dpnp.float32)
983+
result = dpnp.fmax(dp_array1, dp_array2, out=dp_out)
984+
assert_array_equal(expected, result)
985+
986+
@pytest.mark.parametrize(
987+
"dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)
988+
)
989+
def test_out_overlap(self, dtype):
990+
size = 15
991+
# DPNP
992+
dp_a = dpnp.arange(2 * size, dtype=dtype)
993+
dpnp.fmax(dp_a[size::], dp_a[::2], out=dp_a[:size:])
994+
995+
# original
996+
np_a = numpy.arange(2 * size, dtype=dtype)
997+
numpy.fmax(np_a[size::], np_a[::2], out=np_a[:size:])
998+
999+
assert_allclose(np_a, dp_a)
1000+
1001+
@pytest.mark.parametrize(
1002+
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
1003+
)
1004+
def test_invalid_shape(self, shape):
1005+
dp_array1 = dpnp.arange(10)
1006+
dp_array2 = dpnp.arange(5, 15)
1007+
dp_out = dpnp.empty(shape)
1008+
1009+
with pytest.raises(ValueError):
1010+
dpnp.fmax(dp_array1, dp_array2, out=dp_out)
1011+
1012+
@pytest.mark.parametrize(
1013+
"out",
1014+
[4, (), [], (3, 7), [2, 4]],
1015+
ids=["4", "()", "[]", "(3, 7)", "[2, 4]"],
1016+
)
1017+
def test_invalid_out(self, out):
1018+
a = dpnp.arange(10)
1019+
1020+
assert_raises(TypeError, dpnp.fmax, a, 2, out)
1021+
assert_raises(TypeError, numpy.fmax, a.asnumpy(), 2, out)
1022+
1023+
1024+
class TestFmin:
1025+
@pytest.mark.parametrize(
1026+
"dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)
1027+
)
1028+
def test_fmin(self, dtype):
1029+
array1_data = numpy.arange(10)
1030+
array2_data = numpy.arange(5, 15)
1031+
out = numpy.empty(10, dtype=dtype)
1032+
1033+
# DPNP
1034+
dp_array1 = dpnp.array(array1_data, dtype=dtype)
1035+
dp_array2 = dpnp.array(array2_data, dtype=dtype)
1036+
dp_out = dpnp.array(out, dtype=dtype)
1037+
result = dpnp.fmin(dp_array1, dp_array2, out=dp_out)
1038+
1039+
# original
1040+
np_array1 = numpy.array(array1_data, dtype=dtype)
1041+
np_array2 = numpy.array(array2_data, dtype=dtype)
1042+
expected = numpy.fmin(np_array1, np_array2, out=out)
1043+
1044+
assert_allclose(expected, result)
1045+
assert_allclose(out, dp_out)
1046+
1047+
@pytest.mark.parametrize(
1048+
"dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)
1049+
)
1050+
def test_out_dtypes(self, dtype):
1051+
size = 10
1052+
1053+
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
1054+
np_array2 = numpy.arange(size, dtype=dtype)
1055+
np_out = numpy.empty(size, dtype=numpy.float32)
1056+
expected = numpy.fmin(np_array1, np_array2, out=np_out)
1057+
1058+
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
1059+
dp_array2 = dpnp.arange(size, dtype=dtype)
1060+
with pytest.raises(TypeError):
1061+
dpnp.fmin(dp_array1, dp_array2, out=np_out)
1062+
1063+
dp_out = dpnp.empty(size, dtype=dpnp.float32)
1064+
result = dpnp.fmin(dp_array1, dp_array2, out=dp_out)
1065+
assert_array_equal(expected, result)
1066+
1067+
@pytest.mark.parametrize(
1068+
"dtype", get_all_dtypes(no_bool=True, no_complex=True, no_none=True)
1069+
)
1070+
def test_out_overlap(self, dtype):
1071+
size = 15
1072+
# DPNP
1073+
dp_a = dpnp.arange(2 * size, dtype=dtype)
1074+
dpnp.fmin(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1075+
1076+
# original
1077+
np_a = numpy.arange(2 * size, dtype=dtype)
1078+
numpy.fmin(np_a[size::], np_a[::2], out=np_a[:size:])
1079+
1080+
assert_allclose(np_a, dp_a)
1081+
1082+
@pytest.mark.parametrize(
1083+
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
1084+
)
1085+
def test_invalid_shape(self, shape):
1086+
dp_array1 = dpnp.arange(10)
1087+
dp_array2 = dpnp.arange(5, 15)
1088+
dp_out = dpnp.empty(shape)
1089+
1090+
with pytest.raises(ValueError):
1091+
dpnp.fmin(dp_array1, dp_array2, out=dp_out)
1092+
1093+
@pytest.mark.parametrize(
1094+
"out",
1095+
[4, (), [], (3, 7), [2, 4]],
1096+
ids=["4", "()", "[]", "(3, 7)", "[2, 4]"],
1097+
)
1098+
def test_invalid_out(self, out):
1099+
a = dpnp.arange(10)
1100+
1101+
assert_raises(TypeError, dpnp.fmin, a, 2, out)
1102+
assert_raises(TypeError, numpy.fmin, a.asnumpy(), 2, out)
1103+
1104+
1105+
class TestMaximum:
1106+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
1107+
def test_maximum(self, dtype):
1108+
array1_data = numpy.arange(10)
1109+
array2_data = numpy.arange(5, 15)
1110+
out = numpy.empty(10, dtype=dtype)
1111+
1112+
# DPNP
1113+
dp_array1 = dpnp.array(array1_data, dtype=dtype)
1114+
dp_array2 = dpnp.array(array2_data, dtype=dtype)
1115+
dp_out = dpnp.array(out, dtype=dtype)
1116+
result = dpnp.maximum(dp_array1, dp_array2, out=dp_out)
1117+
1118+
# original
1119+
np_array1 = numpy.array(array1_data, dtype=dtype)
1120+
np_array2 = numpy.array(array2_data, dtype=dtype)
1121+
expected = numpy.maximum(np_array1, np_array2, out=out)
1122+
1123+
assert_allclose(expected, result)
1124+
assert_allclose(out, dp_out)
1125+
1126+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
1127+
def test_out_dtypes(self, dtype):
1128+
size = 2 if dtype == dpnp.bool else 10
1129+
1130+
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
1131+
np_array2 = numpy.arange(size, dtype=dtype)
1132+
np_out = numpy.empty(size, dtype=numpy.complex64)
1133+
expected = numpy.maximum(np_array1, np_array2, out=np_out)
1134+
1135+
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
1136+
dp_array2 = dpnp.arange(size, dtype=dtype)
1137+
1138+
dp_out = dpnp.empty(size, dtype=dpnp.complex64)
1139+
if dtype != dpnp.complex64:
1140+
# dtype of out mismatches types of input arrays
1141+
with pytest.raises(TypeError):
1142+
dpnp.maximum(dp_array1, dp_array2, out=dp_out)
1143+
1144+
# allocate new out with expected type
1145+
dp_out = dpnp.empty(size, dtype=dtype)
1146+
1147+
result = dpnp.maximum(dp_array1, dp_array2, out=dp_out)
1148+
assert_array_equal(expected, result)
1149+
1150+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
1151+
def test_out_overlap(self, dtype):
1152+
size = 1 if dtype == dpnp.bool else 15
1153+
# DPNP
1154+
dp_a = dpnp.arange(2 * size, dtype=dtype)
1155+
dpnp.maximum(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1156+
1157+
# original
1158+
np_a = numpy.arange(2 * size, dtype=dtype)
1159+
numpy.maximum(np_a[size::], np_a[::2], out=np_a[:size:])
1160+
1161+
assert_allclose(np_a, dp_a)
1162+
1163+
@pytest.mark.parametrize(
1164+
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
1165+
)
1166+
def test_invalid_shape(self, shape):
1167+
dp_array1 = dpnp.arange(10)
1168+
dp_array2 = dpnp.arange(5, 15)
1169+
dp_out = dpnp.empty(shape)
1170+
1171+
with pytest.raises(ValueError):
1172+
dpnp.maximum(dp_array1, dp_array2, out=dp_out)
1173+
1174+
@pytest.mark.parametrize(
1175+
"out",
1176+
[4, (), [], (3, 7), [2, 4]],
1177+
ids=["4", "()", "[]", "(3, 7)", "[2, 4]"],
1178+
)
1179+
def test_invalid_out(self, out):
1180+
a = dpnp.arange(10)
1181+
1182+
assert_raises(TypeError, dpnp.maximum, a, 2, out)
1183+
assert_raises(TypeError, numpy.maximum, a.asnumpy(), 2, out)
1184+
1185+
1186+
class TestMinimum:
1187+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
1188+
def test_minimum(self, dtype):
1189+
array1_data = numpy.arange(10)
1190+
array2_data = numpy.arange(5, 15)
1191+
out = numpy.empty(10, dtype=dtype)
1192+
1193+
# DPNP
1194+
dp_array1 = dpnp.array(array1_data, dtype=dtype)
1195+
dp_array2 = dpnp.array(array2_data, dtype=dtype)
1196+
dp_out = dpnp.array(out, dtype=dtype)
1197+
result = dpnp.minimum(dp_array1, dp_array2, out=dp_out)
1198+
1199+
# original
1200+
np_array1 = numpy.array(array1_data, dtype=dtype)
1201+
np_array2 = numpy.array(array2_data, dtype=dtype)
1202+
expected = numpy.minimum(np_array1, np_array2, out=out)
1203+
1204+
assert_allclose(expected, result)
1205+
assert_allclose(out, dp_out)
1206+
1207+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
1208+
def test_out_dtypes(self, dtype):
1209+
size = 2 if dtype == dpnp.bool else 10
1210+
1211+
np_array1 = numpy.arange(size, 2 * size, dtype=dtype)
1212+
np_array2 = numpy.arange(size, dtype=dtype)
1213+
np_out = numpy.empty(size, dtype=numpy.complex64)
1214+
expected = numpy.minimum(np_array1, np_array2, out=np_out)
1215+
1216+
dp_array1 = dpnp.arange(size, 2 * size, dtype=dtype)
1217+
dp_array2 = dpnp.arange(size, dtype=dtype)
1218+
1219+
dp_out = dpnp.empty(size, dtype=dpnp.complex64)
1220+
if dtype != dpnp.complex64:
1221+
# dtype of out mismatches types of input arrays
1222+
with pytest.raises(TypeError):
1223+
dpnp.minimum(dp_array1, dp_array2, out=dp_out)
1224+
1225+
# allocate new out with expected type
1226+
dp_out = dpnp.empty(size, dtype=dtype)
1227+
1228+
result = dpnp.minimum(dp_array1, dp_array2, out=dp_out)
1229+
assert_array_equal(expected, result)
1230+
1231+
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
1232+
def test_out_overlap(self, dtype):
1233+
size = 1 if dtype == dpnp.bool else 15
1234+
# DPNP
1235+
dp_a = dpnp.arange(2 * size, dtype=dtype)
1236+
dpnp.minimum(dp_a[size::], dp_a[::2], out=dp_a[:size:])
1237+
1238+
# original
1239+
np_a = numpy.arange(2 * size, dtype=dtype)
1240+
numpy.minimum(np_a[size::], np_a[::2], out=np_a[:size:])
1241+
1242+
assert_allclose(np_a, dp_a)
1243+
1244+
@pytest.mark.parametrize(
1245+
"shape", [(0,), (15,), (2, 2)], ids=["(0,)", "(15, )", "(2,2)"]
1246+
)
1247+
def test_invalid_shape(self, shape):
1248+
dp_array1 = dpnp.arange(10)
1249+
dp_array2 = dpnp.arange(5, 15)
1250+
dp_out = dpnp.empty(shape)
1251+
1252+
with pytest.raises(ValueError):
1253+
dpnp.minimum(dp_array1, dp_array2, out=dp_out)
1254+
1255+
@pytest.mark.parametrize(
1256+
"out",
1257+
[4, (), [], (3, 7), [2, 4]],
1258+
ids=["4", "()", "[]", "(3, 7)", "[2, 4]"],
1259+
)
1260+
def test_invalid_out(self, out):
1261+
a = dpnp.arange(10)
1262+
1263+
assert_raises(TypeError, dpnp.minimum, a, 2, out)
1264+
assert_raises(TypeError, numpy.minimum, a.asnumpy(), 2, out)
1265+
1266+
9431267
class TestMultiply:
9441268
@pytest.mark.parametrize("dtype", get_all_dtypes(no_none=True))
9451269
def test_multiply(self, dtype):

0 commit comments

Comments
 (0)