Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Dec 12, 2024
1 parent 4b8535d commit e876273
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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))
Expand Down

0 comments on commit e876273

Please sign in to comment.