@@ -692,3 +692,74 @@ def test_hyperbolic_functions(op, val):
692692 if float_result == 0.0 :
693693 assert np .signbit (float_result ) == np .signbit (
694694 quad_result ), f"Zero sign mismatch for { op } ({ val } )"
695+
696+
697+ class TestTypePomotionWithPythonAbstractTypes :
698+ """Tests for common_dtype handling of Python abstract dtypes (PyLongDType, PyFloatDType)"""
699+
700+ def test_promotion_with_python_int (self ):
701+ """Test that Python int promotes to QuadPrecDType"""
702+ # Create array from Python int
703+ arr = np .array ([1 , 2 , 3 ], dtype = QuadPrecDType )
704+ assert arr .dtype .name == "QuadPrecDType128"
705+ assert len (arr ) == 3
706+ assert float (arr [0 ]) == 1.0
707+ assert float (arr [1 ]) == 2.0
708+ assert float (arr [2 ]) == 3.0
709+
710+ def test_promotion_with_python_float (self ):
711+ """Test that Python float promotes to QuadPrecDType"""
712+ # Create array from Python float
713+ arr = np .array ([1.5 , 2.7 , 3.14 ], dtype = QuadPrecDType )
714+ assert arr .dtype .name == "QuadPrecDType128"
715+ assert len (arr ) == 3
716+ np .testing .assert_allclose (float (arr [0 ]), 1.5 , rtol = 1e-15 )
717+ np .testing .assert_allclose (float (arr [1 ]), 2.7 , rtol = 1e-15 )
718+ np .testing .assert_allclose (float (arr [2 ]), 3.14 , rtol = 1e-15 )
719+
720+ def test_result_dtype_binary_ops_with_python_types (self ):
721+ """Test that binary operations between QuadPrecDType and Python scalars return QuadPrecDType"""
722+ quad_arr = np .array ([QuadPrecision ("1.0" ), QuadPrecision ("2.0" )])
723+
724+ # Addition with Python int
725+ result = quad_arr + 5
726+ assert result .dtype .name == "QuadPrecDType128"
727+ assert float (result [0 ]) == 6.0
728+ assert float (result [1 ]) == 7.0
729+
730+ # Multiplication with Python float
731+ result = quad_arr * 2.5
732+ assert result .dtype .name == "QuadPrecDType128"
733+ np .testing .assert_allclose (float (result [0 ]), 2.5 , rtol = 1e-15 )
734+ np .testing .assert_allclose (float (result [1 ]), 5.0 , rtol = 1e-15 )
735+
736+ def test_concatenate_with_python_types (self ):
737+ """Test concatenation handles Python numeric types correctly"""
738+ quad_arr = np .array ([QuadPrecision ("1.0" )])
739+ # This should work if promotion is correct
740+ int_arr = np .array ([2 ], dtype = np .int64 )
741+
742+ # The result dtype should be QuadPrecDType
743+ result = np .concatenate ([quad_arr , int_arr .astype (QuadPrecDType )])
744+ assert result .dtype .name == "QuadPrecDType128"
745+ assert len (result ) == 2
746+
747+
748+ @pytest .mark .parametrize ("func,args,expected" , [
749+ # arange tests
750+ (np .arange , (0 , 10 ), list (range (10 ))),
751+ (np .arange , (0 , 10 , 2 ), [0 , 2 , 4 , 6 , 8 ]),
752+ (np .arange , (0.0 , 5.0 , 0.5 ), [0.0 , 0.5 , 1.0 , 1.5 , 2.0 , 2.5 , 3.0 , 3.5 , 4.0 , 4.5 ]),
753+ (np .arange , (10 , 0 , - 1 ), [10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 ]),
754+ (np .arange , (- 5 , 5 ), list (range (- 5 , 5 ))),
755+ # linspace tests
756+ (np .linspace , (0 , 10 , 11 ), list (range (11 ))),
757+ (np .linspace , (0 , 1 , 5 ), [0.0 , 0.25 , 0.5 , 0.75 , 1.0 ]),
758+ ])
759+ def test_fill_function (func , args , expected ):
760+ """Test quadprec_fill function with arange and linspace"""
761+ arr = func (* args , dtype = QuadPrecDType ())
762+ assert arr .dtype .name == "QuadPrecDType128"
763+ assert len (arr ) == len (expected )
764+ for i , exp_val in enumerate (expected ):
765+ np .testing .assert_allclose (float (arr [i ]), float (exp_val ), rtol = 1e-15 , atol = 1e-15 )
0 commit comments