[3.13] gh-121039: add Floats/ComplexesAreIdenticalMixin to test.support.testcase (GH-121071) (#123840)

* [3.13] gh-121039: add Floats/ComplexesAreIdenticalMixin to test.support.testcase (GH-121071)
(cherry picked from commit 8ef8354ef1)

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>

* +1
This commit is contained in:
Sergey B Kirpichev 2024-09-30 04:27:23 +03:00 committed by GitHub
parent 25a23cb002
commit 62e54ddc05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 63 additions and 87 deletions

View file

@ -1,6 +1,7 @@
import unittest
import sys
from test import support
from test.support.testcase import ComplexesAreIdenticalMixin
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS)
@ -42,7 +43,7 @@ class WithComplex:
def __complex__(self):
return self.value
class ComplexTest(unittest.TestCase):
class ComplexTest(ComplexesAreIdenticalMixin, unittest.TestCase):
def assertAlmostEqual(self, a, b):
if isinstance(a, complex):
@ -71,29 +72,6 @@ class ComplexTest(unittest.TestCase):
# check that relative difference < eps
self.assertTrue(abs((x-y)/y) < eps)
def assertFloatsAreIdentical(self, x, y):
"""assert that floats x and y are identical, in the sense that:
(1) both x and y are nans, or
(2) both x and y are infinities, with the same sign, or
(3) both x and y are zeros, with the same sign, or
(4) x and y are both finite and nonzero, and x == y
"""
msg = 'floats {!r} and {!r} are not identical'
if isnan(x) or isnan(y):
if isnan(x) and isnan(y):
return
elif x == y:
if x != 0.0:
return
# both zero; check that signs match
elif copysign(1.0, x) == copysign(1.0, y):
return
else:
msg += ': zeros have different signs'
self.fail(msg.format(x, y))
def assertClose(self, x, y, eps=1e-9):
"""Return true iff complexes x and y "are close"."""
self.assertCloseAbs(x.real, y.real, eps)
@ -731,8 +709,7 @@ class ComplexTest(unittest.TestCase):
for y in vals:
z = complex(x, y)
roundtrip = complex(repr(z))
self.assertFloatsAreIdentical(z.real, roundtrip.real)
self.assertFloatsAreIdentical(z.imag, roundtrip.imag)
self.assertComplexesAreIdentical(z, roundtrip)
# if we predefine some constants, then eval(repr(z)) should
# also work, except that it might change the sign of zeros