diff options
author | Sergey B Kirpichev <skirpichev@gmail.com> | 2024-09-08 13:01:54 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-08 13:01:54 (GMT) |
commit | 8ef8354ef15e00d484ac2ded9442b789c24b11e0 (patch) | |
tree | 322c9d996df0fb404fde248f1e84fa66d4fab910 /Lib/test/support | |
parent | beee91cdcc0dbecab252f7c5c7c51e2adb8edc26 (diff) | |
download | cpython-8ef8354ef15e00d484ac2ded9442b789c24b11e0.zip cpython-8ef8354ef15e00d484ac2ded9442b789c24b11e0.tar.gz cpython-8ef8354ef15e00d484ac2ded9442b789c24b11e0.tar.bz2 |
gh-121039: add Floats/ComplexesAreIdenticalMixin to test.support.testcase (GH-121071)
Diffstat (limited to 'Lib/test/support')
-rw-r--r-- | Lib/test/support/testcase.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/Lib/test/support/testcase.py b/Lib/test/support/testcase.py index 1e4363b..fad1e4c 100644 --- a/Lib/test/support/testcase.py +++ b/Lib/test/support/testcase.py @@ -1,3 +1,6 @@ +from math import copysign, isnan + + class ExceptionIsLikeMixin: def assertExceptionIsLike(self, exc, template): """ @@ -23,3 +26,40 @@ class ExceptionIsLikeMixin: self.assertEqual(len(exc.exceptions), len(template.exceptions)) for e, t in zip(exc.exceptions, template.exceptions): self.assertExceptionIsLike(e, t) + + +class FloatsAreIdenticalMixin: + def assertFloatsAreIdentical(self, x, y): + """Fail unless 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)) + + +class ComplexesAreIdenticalMixin(FloatsAreIdenticalMixin): + def assertComplexesAreIdentical(self, x, y): + """Fail unless complex numbers x and y have equal values and signs. + + In particular, if x and y both have real (or imaginary) part + zero, but the zeros have different signs, this test will fail. + + """ + self.assertFloatsAreIdentical(x.real, y.real) + self.assertFloatsAreIdentical(x.imag, y.imag) |