From e87627317782f0b3b956d15ae4829b16e452bbde Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 12 Dec 2024 13:39:34 +0200 Subject: [PATCH] Add more tests. --- Lib/test/test_complex.py | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py index 242504bb79f01a..3facd38e0f732f 100644 --- a/Lib/test/test_complex.py +++ b/Lib/test/test_complex.py @@ -87,6 +87,10 @@ def assertClose(self, x, y, eps=1e-9): self.assertCloseAbs(x.real, y.real, eps) self.assertCloseAbs(x.imag, y.imag, eps) + def assertSameSign(self, x, y): + if copysign(1., x) != copysign(1., y): + self.fail(f'{x!r} and {y!r} have different signs') + def check_div(self, x, y): """Compute complex z=x*y, and check that z/x==y and z/y==x.""" z = x * y @@ -456,16 +460,14 @@ def test_pow_with_small_integer_exponents(self): self.assertComplexesAreIdentical(c**1, c) self.assertComplexesAreIdentical(c**2, c*c) self.assertComplexesAreIdentical(c**3, c*(c*c)) - self.assertComplexesAreIdentical(c**4, (c*c)*(c*c)) - self.assertComplexesAreIdentical(c**5, c*((c*c)*(c*c))) - self.assertComplexesAreIdentical(c**6, (c*c)*((c*c)*(c*c))) - self.assertComplexesAreIdentical(c**7, c*(c*c)*((c*c)*(c*c))) - self.assertComplexesAreIdentical(c**8, ((c*c)*(c*c))*((c*c)*(c*c))) + self.assertComplexesAreIdentical(c**3, (c*c)*c) if not c: continue for n in range(1, 9): with self.subTest(exponent=-n): self.assertComplexesAreIdentical(c**-n, 1/(c**n)) + + # Special cases for complex division. for x in [+2, -2]: for y in [+0.0, -0.0]: c = complex(x, y) @@ -485,6 +487,25 @@ def test_pow_with_small_integer_exponents(self): self.assertComplexesAreIdentical(c**-1, complex(+0.0*y, -1/x)) self.assertComplexesAreIdentical(c**-2, complex(-0.0, -y/x)) + # Test that zeroes has the same sign as small non-zero values. + eps = 1e-8 + pairs = [(complex(x, y), complex(x, copysign(0.0, y))) + for x in [+1, -1] for y in [+eps, -eps]] + pairs += [(complex(y, x), complex(copysign(0.0, y), x)) + for x in [+1, -1] for y in [+eps, -eps]] + for c1, c2 in pairs: + for n in exponents: + with self.subTest(value=c1, exponent=n): + r1 = c1**n + r2 = c2**n + self.assertClose(r1, r2) + self.assertSameSign(r1.real, r2.real) + self.assertSameSign(r1.imag, r2.imag) + self.assertNotEqual(r1.real, 0.0) + if n != 0: + self.assertNotEqual(r1.imag, 0.0) + self.assertTrue(r2.real == 0.0 or r2.imag == 0.0) + def test_boolcontext(self): for i in range(100): self.assertTrue(complex(random() + 1e-6, random() + 1e-6))