Skip to content

Commit d23a0f2

Browse files
[3.13] Use subTest() in math.fma() tests (GH-131125) (#131132)
Use subTest() in math.fma() tests (GH-131125) (cherry picked from commit 52756c3) Co-authored-by: Sergey B Kirpichev <[email protected]>
1 parent 0980e2e commit d23a0f2

File tree

1 file changed

+73
-64
lines changed

1 file changed

+73
-64
lines changed

Lib/test/test_math.py

+73-64
Original file line numberDiff line numberDiff line change
@@ -2637,9 +2637,10 @@ def test_fma_nan_results(self):
26372637

26382638
# If any input is a NaN, the result should be a NaN, too.
26392639
for a, b in itertools.product(values, repeat=2):
2640-
self.assertIsNaN(math.fma(math.nan, a, b))
2641-
self.assertIsNaN(math.fma(a, math.nan, b))
2642-
self.assertIsNaN(math.fma(a, b, math.nan))
2640+
with self.subTest(a=a, b=b):
2641+
self.assertIsNaN(math.fma(math.nan, a, b))
2642+
self.assertIsNaN(math.fma(a, math.nan, b))
2643+
self.assertIsNaN(math.fma(a, b, math.nan))
26432644

26442645
def test_fma_infinities(self):
26452646
# Cases involving infinite inputs or results.
@@ -2651,57 +2652,62 @@ def test_fma_infinities(self):
26512652
for c in non_nans:
26522653
for infinity in [math.inf, -math.inf]:
26532654
for zero in [0.0, -0.0]:
2654-
with self.assertRaises(ValueError):
2655-
math.fma(infinity, zero, c)
2656-
with self.assertRaises(ValueError):
2657-
math.fma(zero, infinity, c)
2655+
with self.subTest(c=c, infinity=infinity, zero=zero):
2656+
with self.assertRaises(ValueError):
2657+
math.fma(infinity, zero, c)
2658+
with self.assertRaises(ValueError):
2659+
math.fma(zero, infinity, c)
26582660

26592661
# ValueError when a*b and c both infinite of opposite signs.
26602662
for b in positives:
2661-
with self.assertRaises(ValueError):
2662-
math.fma(math.inf, b, -math.inf)
2663-
with self.assertRaises(ValueError):
2664-
math.fma(math.inf, -b, math.inf)
2665-
with self.assertRaises(ValueError):
2666-
math.fma(-math.inf, -b, -math.inf)
2667-
with self.assertRaises(ValueError):
2668-
math.fma(-math.inf, b, math.inf)
2669-
with self.assertRaises(ValueError):
2670-
math.fma(b, math.inf, -math.inf)
2671-
with self.assertRaises(ValueError):
2672-
math.fma(-b, math.inf, math.inf)
2673-
with self.assertRaises(ValueError):
2674-
math.fma(-b, -math.inf, -math.inf)
2675-
with self.assertRaises(ValueError):
2676-
math.fma(b, -math.inf, math.inf)
2663+
with self.subTest(b=b):
2664+
with self.assertRaises(ValueError):
2665+
math.fma(math.inf, b, -math.inf)
2666+
with self.assertRaises(ValueError):
2667+
math.fma(math.inf, -b, math.inf)
2668+
with self.assertRaises(ValueError):
2669+
math.fma(-math.inf, -b, -math.inf)
2670+
with self.assertRaises(ValueError):
2671+
math.fma(-math.inf, b, math.inf)
2672+
with self.assertRaises(ValueError):
2673+
math.fma(b, math.inf, -math.inf)
2674+
with self.assertRaises(ValueError):
2675+
math.fma(-b, math.inf, math.inf)
2676+
with self.assertRaises(ValueError):
2677+
math.fma(-b, -math.inf, -math.inf)
2678+
with self.assertRaises(ValueError):
2679+
math.fma(b, -math.inf, math.inf)
26772680

26782681
# Infinite result when a*b and c both infinite of the same sign.
26792682
for b in positives:
2680-
self.assertEqual(math.fma(math.inf, b, math.inf), math.inf)
2681-
self.assertEqual(math.fma(math.inf, -b, -math.inf), -math.inf)
2682-
self.assertEqual(math.fma(-math.inf, -b, math.inf), math.inf)
2683-
self.assertEqual(math.fma(-math.inf, b, -math.inf), -math.inf)
2684-
self.assertEqual(math.fma(b, math.inf, math.inf), math.inf)
2685-
self.assertEqual(math.fma(-b, math.inf, -math.inf), -math.inf)
2686-
self.assertEqual(math.fma(-b, -math.inf, math.inf), math.inf)
2687-
self.assertEqual(math.fma(b, -math.inf, -math.inf), -math.inf)
2683+
with self.subTest(b=b):
2684+
self.assertEqual(math.fma(math.inf, b, math.inf), math.inf)
2685+
self.assertEqual(math.fma(math.inf, -b, -math.inf), -math.inf)
2686+
self.assertEqual(math.fma(-math.inf, -b, math.inf), math.inf)
2687+
self.assertEqual(math.fma(-math.inf, b, -math.inf), -math.inf)
2688+
self.assertEqual(math.fma(b, math.inf, math.inf), math.inf)
2689+
self.assertEqual(math.fma(-b, math.inf, -math.inf), -math.inf)
2690+
self.assertEqual(math.fma(-b, -math.inf, math.inf), math.inf)
2691+
self.assertEqual(math.fma(b, -math.inf, -math.inf), -math.inf)
26882692

26892693
# Infinite result when a*b finite, c infinite.
26902694
for a, b in itertools.product(finites, finites):
2691-
self.assertEqual(math.fma(a, b, math.inf), math.inf)
2692-
self.assertEqual(math.fma(a, b, -math.inf), -math.inf)
2695+
with self.subTest(b=b):
2696+
self.assertEqual(math.fma(a, b, math.inf), math.inf)
2697+
self.assertEqual(math.fma(a, b, -math.inf), -math.inf)
26932698

26942699
# Infinite result when a*b infinite, c finite.
26952700
for b, c in itertools.product(positives, finites):
2696-
self.assertEqual(math.fma(math.inf, b, c), math.inf)
2697-
self.assertEqual(math.fma(-math.inf, b, c), -math.inf)
2698-
self.assertEqual(math.fma(-math.inf, -b, c), math.inf)
2699-
self.assertEqual(math.fma(math.inf, -b, c), -math.inf)
2701+
with self.subTest(b=b, c=c):
2702+
self.assertEqual(math.fma(math.inf, b, c), math.inf)
2703+
self.assertEqual(math.fma(-math.inf, b, c), -math.inf)
2704+
self.assertEqual(math.fma(-math.inf, -b, c), math.inf)
2705+
self.assertEqual(math.fma(math.inf, -b, c), -math.inf)
27002706

2701-
self.assertEqual(math.fma(b, math.inf, c), math.inf)
2702-
self.assertEqual(math.fma(b, -math.inf, c), -math.inf)
2703-
self.assertEqual(math.fma(-b, -math.inf, c), math.inf)
2704-
self.assertEqual(math.fma(-b, math.inf, c), -math.inf)
2707+
self.assertEqual(math.fma(b, math.inf, c), math.inf)
2708+
self.assertEqual(math.fma(b, -math.inf, c), -math.inf)
2709+
self.assertEqual(math.fma(-b, -math.inf, c), math.inf)
2710+
self.assertEqual(math.fma(-b, math.inf, c), -math.inf)
27052711

27062712
# gh-73468: On some platforms, libc fma() doesn't implement IEE 754-2008
27072713
# properly: it doesn't use the right sign when the result is zero.
@@ -2714,23 +2720,24 @@ def test_fma_zero_result(self):
27142720

27152721
# Zero results from exact zero inputs.
27162722
for b in nonnegative_finites:
2717-
self.assertIsPositiveZero(math.fma(0.0, b, 0.0))
2718-
self.assertIsPositiveZero(math.fma(0.0, b, -0.0))
2719-
self.assertIsNegativeZero(math.fma(0.0, -b, -0.0))
2720-
self.assertIsPositiveZero(math.fma(0.0, -b, 0.0))
2721-
self.assertIsPositiveZero(math.fma(-0.0, -b, 0.0))
2722-
self.assertIsPositiveZero(math.fma(-0.0, -b, -0.0))
2723-
self.assertIsNegativeZero(math.fma(-0.0, b, -0.0))
2724-
self.assertIsPositiveZero(math.fma(-0.0, b, 0.0))
2725-
2726-
self.assertIsPositiveZero(math.fma(b, 0.0, 0.0))
2727-
self.assertIsPositiveZero(math.fma(b, 0.0, -0.0))
2728-
self.assertIsNegativeZero(math.fma(-b, 0.0, -0.0))
2729-
self.assertIsPositiveZero(math.fma(-b, 0.0, 0.0))
2730-
self.assertIsPositiveZero(math.fma(-b, -0.0, 0.0))
2731-
self.assertIsPositiveZero(math.fma(-b, -0.0, -0.0))
2732-
self.assertIsNegativeZero(math.fma(b, -0.0, -0.0))
2733-
self.assertIsPositiveZero(math.fma(b, -0.0, 0.0))
2723+
with self.subTest(b=b):
2724+
self.assertIsPositiveZero(math.fma(0.0, b, 0.0))
2725+
self.assertIsPositiveZero(math.fma(0.0, b, -0.0))
2726+
self.assertIsNegativeZero(math.fma(0.0, -b, -0.0))
2727+
self.assertIsPositiveZero(math.fma(0.0, -b, 0.0))
2728+
self.assertIsPositiveZero(math.fma(-0.0, -b, 0.0))
2729+
self.assertIsPositiveZero(math.fma(-0.0, -b, -0.0))
2730+
self.assertIsNegativeZero(math.fma(-0.0, b, -0.0))
2731+
self.assertIsPositiveZero(math.fma(-0.0, b, 0.0))
2732+
2733+
self.assertIsPositiveZero(math.fma(b, 0.0, 0.0))
2734+
self.assertIsPositiveZero(math.fma(b, 0.0, -0.0))
2735+
self.assertIsNegativeZero(math.fma(-b, 0.0, -0.0))
2736+
self.assertIsPositiveZero(math.fma(-b, 0.0, 0.0))
2737+
self.assertIsPositiveZero(math.fma(-b, -0.0, 0.0))
2738+
self.assertIsPositiveZero(math.fma(-b, -0.0, -0.0))
2739+
self.assertIsNegativeZero(math.fma(b, -0.0, -0.0))
2740+
self.assertIsPositiveZero(math.fma(b, -0.0, 0.0))
27342741

27352742
# Exact zero result from nonzero inputs.
27362743
self.assertIsPositiveZero(math.fma(2.0, 2.0, -4.0))
@@ -2836,12 +2843,14 @@ def test_random(self):
28362843
'0x1.f5467b1911fd6p-2', '0x1.b5cee3225caa5p-1'),
28372844
]
28382845
for a_hex, b_hex, c_hex, expected_hex in test_values:
2839-
a = float.fromhex(a_hex)
2840-
b = float.fromhex(b_hex)
2841-
c = float.fromhex(c_hex)
2842-
expected = float.fromhex(expected_hex)
2843-
self.assertEqual(math.fma(a, b, c), expected)
2844-
self.assertEqual(math.fma(b, a, c), expected)
2846+
with self.subTest(a_hex=a_hex, b_hex=b_hex, c_hex=c_hex,
2847+
expected_hex=expected_hex):
2848+
a = float.fromhex(a_hex)
2849+
b = float.fromhex(b_hex)
2850+
c = float.fromhex(c_hex)
2851+
expected = float.fromhex(expected_hex)
2852+
self.assertEqual(math.fma(a, b, c), expected)
2853+
self.assertEqual(math.fma(b, a, c), expected)
28452854

28462855
# Custom assertions.
28472856
def assertIsNaN(self, value):

0 commit comments

Comments
 (0)