From 7e03ec78124eb12894faf0b2e3474355ca9c0987 Mon Sep 17 00:00:00 2001 From: ShaharNaveh <50263213+ShaharNaveh@users.noreply.github.com> Date: Sat, 23 Aug 2025 22:06:50 +0300 Subject: [PATCH] Update test_math --- Lib/test/test_math.py | 150 +++++++++++++++++++++++------------------- 1 file changed, 83 insertions(+), 67 deletions(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 16d12f968..0907c5465 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -1202,6 +1202,13 @@ class MathTests(unittest.TestCase): self.assertEqual(math.ldexp(NINF, n), NINF) self.assertTrue(math.isnan(math.ldexp(NAN, n))) + @unittest.expectedFailure # TODO: RUSTPYTHON + @requires_IEEE_754 + def testLdexp_denormal(self): + # Denormal output incorrectly rounded (truncated) + # on some Windows. + self.assertEqual(math.ldexp(6993274598585239, -1126), 1e-323) + def testLog(self): self.assertRaises(TypeError, math.log) self.assertRaises(TypeError, math.log, 1, 2, 3) @@ -2014,8 +2021,7 @@ class MathTests(unittest.TestCase): else: self.fail("sqrt(-1) didn't raise ValueError") - # TODO: RUSTPYTHON - @unittest.expectedFailure + @unittest.expectedFailure # TODO: RUSTPYTHON @requires_IEEE_754 def test_testfile(self): # Some tests need to be skipped on ancient OS X versions. @@ -2640,9 +2646,10 @@ class FMATests(unittest.TestCase): # If any input is a NaN, the result should be a NaN, too. for a, b in itertools.product(values, repeat=2): - self.assertIsNaN(math.fma(math.nan, a, b)) - self.assertIsNaN(math.fma(a, math.nan, b)) - self.assertIsNaN(math.fma(a, b, math.nan)) + with self.subTest(a=a, b=b): + self.assertIsNaN(math.fma(math.nan, a, b)) + self.assertIsNaN(math.fma(a, math.nan, b)) + self.assertIsNaN(math.fma(a, b, math.nan)) def test_fma_infinities(self): # Cases involving infinite inputs or results. @@ -2654,86 +2661,93 @@ class FMATests(unittest.TestCase): for c in non_nans: for infinity in [math.inf, -math.inf]: for zero in [0.0, -0.0]: - with self.assertRaises(ValueError): - math.fma(infinity, zero, c) - with self.assertRaises(ValueError): - math.fma(zero, infinity, c) + with self.subTest(c=c, infinity=infinity, zero=zero): + with self.assertRaises(ValueError): + math.fma(infinity, zero, c) + with self.assertRaises(ValueError): + math.fma(zero, infinity, c) # ValueError when a*b and c both infinite of opposite signs. for b in positives: - with self.assertRaises(ValueError): - math.fma(math.inf, b, -math.inf) - with self.assertRaises(ValueError): - math.fma(math.inf, -b, math.inf) - with self.assertRaises(ValueError): - math.fma(-math.inf, -b, -math.inf) - with self.assertRaises(ValueError): - math.fma(-math.inf, b, math.inf) - with self.assertRaises(ValueError): - math.fma(b, math.inf, -math.inf) - with self.assertRaises(ValueError): - math.fma(-b, math.inf, math.inf) - with self.assertRaises(ValueError): - math.fma(-b, -math.inf, -math.inf) - with self.assertRaises(ValueError): - math.fma(b, -math.inf, math.inf) + with self.subTest(b=b): + with self.assertRaises(ValueError): + math.fma(math.inf, b, -math.inf) + with self.assertRaises(ValueError): + math.fma(math.inf, -b, math.inf) + with self.assertRaises(ValueError): + math.fma(-math.inf, -b, -math.inf) + with self.assertRaises(ValueError): + math.fma(-math.inf, b, math.inf) + with self.assertRaises(ValueError): + math.fma(b, math.inf, -math.inf) + with self.assertRaises(ValueError): + math.fma(-b, math.inf, math.inf) + with self.assertRaises(ValueError): + math.fma(-b, -math.inf, -math.inf) + with self.assertRaises(ValueError): + math.fma(b, -math.inf, math.inf) # Infinite result when a*b and c both infinite of the same sign. for b in positives: - self.assertEqual(math.fma(math.inf, b, math.inf), math.inf) - self.assertEqual(math.fma(math.inf, -b, -math.inf), -math.inf) - self.assertEqual(math.fma(-math.inf, -b, math.inf), math.inf) - self.assertEqual(math.fma(-math.inf, b, -math.inf), -math.inf) - self.assertEqual(math.fma(b, math.inf, math.inf), math.inf) - self.assertEqual(math.fma(-b, math.inf, -math.inf), -math.inf) - self.assertEqual(math.fma(-b, -math.inf, math.inf), math.inf) - self.assertEqual(math.fma(b, -math.inf, -math.inf), -math.inf) + with self.subTest(b=b): + self.assertEqual(math.fma(math.inf, b, math.inf), math.inf) + self.assertEqual(math.fma(math.inf, -b, -math.inf), -math.inf) + self.assertEqual(math.fma(-math.inf, -b, math.inf), math.inf) + self.assertEqual(math.fma(-math.inf, b, -math.inf), -math.inf) + self.assertEqual(math.fma(b, math.inf, math.inf), math.inf) + self.assertEqual(math.fma(-b, math.inf, -math.inf), -math.inf) + self.assertEqual(math.fma(-b, -math.inf, math.inf), math.inf) + self.assertEqual(math.fma(b, -math.inf, -math.inf), -math.inf) # Infinite result when a*b finite, c infinite. for a, b in itertools.product(finites, finites): - self.assertEqual(math.fma(a, b, math.inf), math.inf) - self.assertEqual(math.fma(a, b, -math.inf), -math.inf) + with self.subTest(b=b): + self.assertEqual(math.fma(a, b, math.inf), math.inf) + self.assertEqual(math.fma(a, b, -math.inf), -math.inf) # Infinite result when a*b infinite, c finite. for b, c in itertools.product(positives, finites): - self.assertEqual(math.fma(math.inf, b, c), math.inf) - self.assertEqual(math.fma(-math.inf, b, c), -math.inf) - self.assertEqual(math.fma(-math.inf, -b, c), math.inf) - self.assertEqual(math.fma(math.inf, -b, c), -math.inf) + with self.subTest(b=b, c=c): + self.assertEqual(math.fma(math.inf, b, c), math.inf) + self.assertEqual(math.fma(-math.inf, b, c), -math.inf) + self.assertEqual(math.fma(-math.inf, -b, c), math.inf) + self.assertEqual(math.fma(math.inf, -b, c), -math.inf) - self.assertEqual(math.fma(b, math.inf, c), math.inf) - self.assertEqual(math.fma(b, -math.inf, c), -math.inf) - self.assertEqual(math.fma(-b, -math.inf, c), math.inf) - self.assertEqual(math.fma(-b, math.inf, c), -math.inf) + self.assertEqual(math.fma(b, math.inf, c), math.inf) + self.assertEqual(math.fma(b, -math.inf, c), -math.inf) + self.assertEqual(math.fma(-b, -math.inf, c), math.inf) + self.assertEqual(math.fma(-b, math.inf, c), -math.inf) # gh-73468: On some platforms, libc fma() doesn't implement IEE 754-2008 # properly: it doesn't use the right sign when the result is zero. @unittest.skipIf( - sys.platform.startswith(("freebsd", "wasi", "netbsd")) - or (sys.platform == "android" and platform.machine() == "x86_64"), + sys.platform.startswith(("freebsd", "wasi", "netbsd", "emscripten")) + or (sys.platform == "android" and platform.machine() == "x86_64") + or support.linked_to_musl(), # gh-131032 f"this platform doesn't implement IEE 754-2008 properly") def test_fma_zero_result(self): nonnegative_finites = [0.0, 1e-300, 2.3, 1e300] # Zero results from exact zero inputs. for b in nonnegative_finites: - self.assertIsPositiveZero(math.fma(0.0, b, 0.0)) - self.assertIsPositiveZero(math.fma(0.0, b, -0.0)) - self.assertIsNegativeZero(math.fma(0.0, -b, -0.0)) - self.assertIsPositiveZero(math.fma(0.0, -b, 0.0)) - self.assertIsPositiveZero(math.fma(-0.0, -b, 0.0)) - self.assertIsPositiveZero(math.fma(-0.0, -b, -0.0)) - self.assertIsNegativeZero(math.fma(-0.0, b, -0.0)) - self.assertIsPositiveZero(math.fma(-0.0, b, 0.0)) + with self.subTest(b=b): + self.assertIsPositiveZero(math.fma(0.0, b, 0.0)) + self.assertIsPositiveZero(math.fma(0.0, b, -0.0)) + self.assertIsNegativeZero(math.fma(0.0, -b, -0.0)) + self.assertIsPositiveZero(math.fma(0.0, -b, 0.0)) + self.assertIsPositiveZero(math.fma(-0.0, -b, 0.0)) + self.assertIsPositiveZero(math.fma(-0.0, -b, -0.0)) + self.assertIsNegativeZero(math.fma(-0.0, b, -0.0)) + self.assertIsPositiveZero(math.fma(-0.0, b, 0.0)) - self.assertIsPositiveZero(math.fma(b, 0.0, 0.0)) - self.assertIsPositiveZero(math.fma(b, 0.0, -0.0)) - self.assertIsNegativeZero(math.fma(-b, 0.0, -0.0)) - self.assertIsPositiveZero(math.fma(-b, 0.0, 0.0)) - self.assertIsPositiveZero(math.fma(-b, -0.0, 0.0)) - self.assertIsPositiveZero(math.fma(-b, -0.0, -0.0)) - self.assertIsNegativeZero(math.fma(b, -0.0, -0.0)) - self.assertIsPositiveZero(math.fma(b, -0.0, 0.0)) + self.assertIsPositiveZero(math.fma(b, 0.0, 0.0)) + self.assertIsPositiveZero(math.fma(b, 0.0, -0.0)) + self.assertIsNegativeZero(math.fma(-b, 0.0, -0.0)) + self.assertIsPositiveZero(math.fma(-b, 0.0, 0.0)) + self.assertIsPositiveZero(math.fma(-b, -0.0, 0.0)) + self.assertIsPositiveZero(math.fma(-b, -0.0, -0.0)) + self.assertIsNegativeZero(math.fma(b, -0.0, -0.0)) + self.assertIsPositiveZero(math.fma(b, -0.0, 0.0)) # Exact zero result from nonzero inputs. self.assertIsPositiveZero(math.fma(2.0, 2.0, -4.0)) @@ -2839,12 +2853,14 @@ class FMATests(unittest.TestCase): '0x1.f5467b1911fd6p-2', '0x1.b5cee3225caa5p-1'), ] for a_hex, b_hex, c_hex, expected_hex in test_values: - a = float.fromhex(a_hex) - b = float.fromhex(b_hex) - c = float.fromhex(c_hex) - expected = float.fromhex(expected_hex) - self.assertEqual(math.fma(a, b, c), expected) - self.assertEqual(math.fma(b, a, c), expected) + with self.subTest(a_hex=a_hex, b_hex=b_hex, c_hex=c_hex, + expected_hex=expected_hex): + a = float.fromhex(a_hex) + b = float.fromhex(b_hex) + c = float.fromhex(c_hex) + expected = float.fromhex(expected_hex) + self.assertEqual(math.fma(a, b, c), expected) + self.assertEqual(math.fma(b, a, c), expected) # Custom assertions. def assertIsNaN(self, value):