8000 TST: Minor changes to floor divide | Added cases for timedelta divide · numpy/numpy@f93ca93 · GitHub
[go: up one dir, main page]

Skip to content

Commit f93ca93

Browse files
committed
TST: Minor changes to floor divide | Added cases for timedelta divide
1 parent 0e2116f commit f93ca93

File tree

1 file changed

+41
-12
lines changed

1 file changed

+41
-12
lines changed

numpy/core/tests/test_umath.py

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,25 +252,54 @@ def test_division_int(self):
252252
@pytest.mark.parametrize("input_dtype",
253253
[np.int8, np.int16, np.int32, np.int64])
254254
def test_division_int_boundary(self, input_dtype):
255-
class ListWithDiv(list):
256-
def __floordiv__(self, divisor):
257-
return [i//divisor for i in self]
258-
259255
iinfo = np.iinfo(input_dtype)
260256

261-
# Create array with min, 25th percentile, 0, 75th percentile, max
262-
arr = ListWithDiv([iinfo.min, iinfo.min//2, 0, iinfo.max//2, iinfo.max])
263-
dividends = [iinfo.min, iinfo.min//2, iinfo.max//2, iinfo.max]
264-
a = np.array(arr, dtype = input_dtype)
257+
# Create list with min, 25th percentile, 0, 75th percentile, max
258+
lst = [iinfo.min, iinfo.min//2, 0, iinfo.max//2, iinfo.max]
259+
divisors = [iinfo.min, iinfo.min//2, iinfo.max//2, iinfo.max]
260+
a = np.array(lst, dtype=input_dtype)
265261

266-
for dividend in dividends:
267-
div_a = a // dividend
268-
div_arr = arr // dividend
269-
assert_(all(div_a == div_arr))
262+
for divisor in divisors:
263+
div_a = a // divisor
264+
b = a.copy(); b //= divisor
265+
div_lst = [i // divisor for i in lst]
266+
assert_(all(div_a == div_lst))
267+
assert_(all(div_a == b))
270268

271269
with np.errstate(divide='raise'):
272270
with pytest.raises(FloatingPointError):
273271
a // 0
272+
with pytest.raises(FloatingPointError):
273+
a //= 0
274+
275+
@pytest.mark.parametrize(
276+
"dividend,divisor,quotient",
277+
[(np.timedelta64(2,'Y'), np.timedelta64(2,'M'), 12),
278+
(np.timedelta64(2,'Y'), np.timedelta64(-2,'M'), -12),
279+
(np.timedelta64(-2,'Y'), np.timedelta64(2,'M'), -12),
280+
(np.timedelta64(-2,'Y'), np.timedelta64(-2,'M'), 12),
281+
(np.timedelta64(2,'M'), np.timedelta64(-2,'Y'), -1),
282+
(np.timedelta64(2,'Y'), np.timedelta64(0,'M'), 0),
283+
(np.timedelta64(2,'Y'), 2, np.timedelta64(1,'Y')),
284+
(np.timedelta64(2,'Y'), -2, np.timedelta64(-1,'Y')),
285+
(np.timedelta64(-2,'Y'), 2, np.timedelta64(-1,'Y')),
286+
(np.timedelta64(-2,'Y'), -2, np.timedelta64(1,'Y')),
287+
(np.timedelta64(-2,'Y'), -2, np.timedelta64(1,'Y')),
288+
(np.timedelta64(-2,'Y'), -3, np.timedelta64(0,'Y')),
289+
(np.timedelta64(-2,'Y'), 0, np.timedelta64('Nat','Y')),
290+
])
291+
def test_division_int_timedelta(self, dividend, divisor, quotient):
292+
# If either divisor is 0 or quotient is Nat, check for division by 0
293+
if divisor and (isinstance(quotient, int) or not np.isnat(quotient)):
294+
assert_(dividend // divisor == quotient)
295+
# Test for arrays as well
296+
assert_(all(
297+
np.array([dividend]*5) // divisor \
298+
== np.array([quotient]*5)))
299+
else:
300+
with np.errstate(divide='raise', invalid='raise'):
301+
with pytest.raises(FloatingPointError):
302+
dividend // divisor
274303

275304
def test_division_complex(self):
276305
# check that implementation is correct

0 commit comments

Comments
 (0)
0