@@ -252,25 +252,54 @@ def test_division_int(self):
252
252
@pytest .mark .parametrize ("input_dtype" ,
253
253
[np .int8 , np .int16 , np .int32 , np .int64 ])
254
254
def test_division_int_boundary (self , input_dtype ):
255
- class ListWithDiv (list ):
256
- def
8000
span> __floordiv__ (self , divisor ):
257
- return [i // divisor for i in self ]
258
-
259
255
iinfo = np .iinfo (input_dtype )
260
256
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 )
265
261
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 ))
270
268
271
269
with np .errstate (divide = 'raise' ):
272
270
with pytest .raises (FloatingPointError ):
273
271
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
274
303
275
304
def test_division_complex (self ):
276
305
# check that implementation is correct
0 commit comments