Skip to content

Commit e3be611

Browse files
committed
add new tests
1 parent 411a2e9 commit e3be611

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
@@ -966,6 +966,330 @@ def test_invalid_out(self, out):
966966
assert_raises(TypeError, numpy.add, a.asnumpy(), 2, out)
967967

968968

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

0 commit comments

Comments
 (0)