@@ -350,14 +350,54 @@ def test_arrfuncs_zeros(dtype, arrfunc, expected):
350350 [["" , "world" ], [False , True ], True , False ],
351351 ],
352352)
353- def test_bool_cast (dtype , strings , cast_answer , any_answer , all_answer ):
353+ def test_cast_to_bool (dtype , strings , cast_answer , any_answer , all_answer ):
354354 sarr = np .array (strings , dtype = dtype )
355355 np .testing .assert_array_equal (sarr .astype ("bool" ), cast_answer )
356356
357357 assert np .any (sarr ) == any_answer
358358 assert np .all (sarr ) == all_answer
359359
360360
361+ @pytest .mark .parametrize (
362+ ("strings" , "cast_answer" ),
363+ [
364+ [[True , True ], ["True" , "True" ]],
365+ [[False , False ], ["False" , "False" ]],
366+ [[True , False ], ["True" , "False" ]],
367+ [[False , True ], ["False" , "True" ]],
368+ ],
369+ )
370+ def test_cast_from_bool (dtype , strings , cast_answer ):
371+ barr = np .array (strings , dtype = bool )
372+ np .testing .assert_array_equal (
373+ barr .astype (dtype ), np .array (cast_answer , dtype = dtype )
374+ )
375+
376+
377+ @pytest .mark .parametrize ("bitsize" , [8 , 16 , 32 , 64 ])
378+ @pytest .mark .parametrize ("signed" , [True , False ])
379+ def test_integer_casts (dtype , bitsize , signed ):
380+ idtype = f"int{ bitsize } "
381+ if signed :
382+ inp = [- (2 ** p - 1 ) for p in reversed (range (bitsize - 1 ))]
383+ inp += [2 ** p - 1 for p in range (1 , bitsize - 1 )]
384+ else :
385+ idtype = "u" + idtype
386+ inp = [2 ** p - 1 for p in range (bitsize )]
387+ ainp = np .array (inp , dtype = idtype )
388+ np .testing .assert_array_equal (ainp , ainp .astype (dtype ).astype (idtype ))
389+
390+ with pytest .raises (TypeError ):
391+ ainp .astype (dtype , casting = "safe" )
392+
393+ with pytest .raises (TypeError ):
394+ ainp .astype (dtype ).astype (idtype , casting = "safe" )
395+
396+ oob = [str (2 ** bitsize ), str (- (2 ** bitsize ))]
397+ with pytest .raises (OverflowError ):
398+ np .array (oob , dtype = dtype ).astype (idtype )
399+
400+
361401def test_take (dtype , string_list ):
362402 sarr = np .array (string_list , dtype = dtype )
363403 out = np .empty (len (string_list ), dtype = dtype )
0 commit comments