@@ -5399,12 +5399,78 @@ def test_quad_to_quad_backend_casting(src_backend, dst_backend, value):
53995399 else :
54005400 np .testing .assert_array_equal (dst_arr , res_arr )
54015401
5402+ class TestSameValueCasting :
5403+ """Test 'same_value' casting behavior for QuadPrecision."""
5404+ def test_same_value_cast (self ):
5405+ a = np .arange (30 , dtype = np .float32 )
5406+ # upcasting can never fail
5407+ b = a .astype (QuadPrecision , casting = 'same_value' )
5408+ c = b .astype (np .float32 , casting = 'same_value' )
5409+ assert np .all (c == a )
5410+ with pytest .raises (ValueError , match = "could not cast 'same_value'" ):
5411+ (b + 1e22 ).astype (np .float32 , casting = 'same_value' )
54025412
5403- def test_same_value_cast ():
5404- a = np .arange (30 , dtype = np .float32 )
5405- # upcasting can never fail
5406- b = a .astype (QuadPrecision , casting = 'same_value' )
5407- c = b .astype (np .float32 , casting = 'same_value' )
5408- assert np .all (c == a )
5409- with pytest .raises (ValueError , match = "could not cast 'same_value'" ):
5410- (b + 1e22 ).astype (np .float32 , casting = 'same_value' )
5413+
5414+ @pytest .mark .parametrize ("dtype,passing,failing" , [
5415+ # bool: only 0 and 1 are valid
5416+ ("bool" , [0 , 1 ], [2 , - 1 , 0.5 ]),
5417+
5418+ # int8: [-128, 127]
5419+ ("int8" , [- 128 , 0 , 127 ], [- 129 , 128 , 1.5 ]),
5420+
5421+ # uint8: [0, 255]
5422+ ("uint8" , [0 , 255 ], [- 1 , 256 , 2.5 ]),
5423+
5424+ # int16: [-32768, 32767]
5425+ ("int16" , [- 32768 , 0 , 32767 ], [- 32769 , 32768 , 0.1 ]),
5426+
5427+ # uint16: [0, 65535]
5428+ ("uint16" , [0 , 65535 ], [- 1 , 65536 ]),
5429+
5430+ # int32: [-2^31, 2^31-1]
5431+ ("int32" , [- 2 ** 31 , 0 , 2 ** 31 - 1 ], [- 2 ** 31 - 1 , 2 ** 31 ]),
5432+
5433+ # uint32: [0, 2^32-1]
5434+ ("uint32" , [0 , 2 ** 32 - 1 ], [- 1 , 2 ** 32 ]),
5435+
5436+ # int64: [-2^63, 2^63-1]
5437+ ("int64" , [- 2 ** 63 , 0 , 2 ** 63 - 1 ], [- 2 ** 63 - 1 , 2 ** 63 ]),
5438+
5439+ # uint64: [0, 2^64-1]
5440+ ("uint64" , [0 , 2 ** 64 - 1 ], [- 1 , 2 ** 64 ]),
5441+ ])
5442+ def test_same_value_cast_quad_to_int (self , dtype , passing , failing ):
5443+ """A 128-bit float can represent all consecutive integers exactly up to 2^113"""
5444+ for val in passing :
5445+ q = np .array ([val ], dtype = QuadPrecDType ())
5446+ result = q .astype (dtype , casting = "same_value" )
5447+ assert result == val
5448+
5449+ for val in failing :
5450+ q = np .array ([val ], dtype = QuadPrecDType ())
5451+ with pytest .raises (ValueError , match = "could not cast 'same_value'" ):
5452+ q .astype (dtype , casting = "same_value" )
5453+
5454+ @pytest .mark .parametrize ("dtype" , ["half" , "float16" ,
5455+ "float" , "float32" ,
5456+ "double" , "float64" ,
5457+ "longdouble" ,])
5458+ @pytest .mark .parametrize ("values" , [
5459+
5460+ ])
5461+ def test_same_value_cast_floats (self , dtype , values ):
5462+ pass
5463+
5464+ @pytest .mark .parametrize ("dtype" , [
5465+ "S50" , "U50" , "<U50" , ">U50" , "S100" , "U100" , "<U100" , ">U100" , np .dtypes .StringDType ()])
5466+ @pytest .mark .parametrize ("values" , [
5467+ ])
5468+ def test_same_value_cast_strings_enough_width (self , dtype , values ):
5469+ pass
5470+
5471+ @pytest .mark .parametrize ("dtype" , [
5472+ "S20" , "U20" , "<U20" , ">U20" ])
5473+ @pytest .mark .parametrize ("values" , [
5474+ ])
5475+ def test_same_value_cast_strings_small_width (self , dtype , values ):
5476+ pass
0 commit comments