Skip to content

Commit f586982

Browse files
committed
add new tests
1 parent 685682f commit f586982

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

918918

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

0 commit comments

Comments
 (0)