Home | History | Annotate | Download | only in test
      1 import test.test_support, unittest
      2 
      3 class PowTest(unittest.TestCase):
      4 
      5     def powtest(self, type):
      6         if type != float:
      7             for i in range(-1000, 1000):
      8                 self.assertEqual(pow(type(i), 0), 1)
      9                 self.assertEqual(pow(type(i), 1), type(i))
     10                 self.assertEqual(pow(type(0), 1), type(0))
     11                 self.assertEqual(pow(type(1), 1), type(1))
     12 
     13             for i in range(-100, 100):
     14                 self.assertEqual(pow(type(i), 3), i*i*i)
     15 
     16             pow2 = 1
     17             for i in range(0,31):
     18                 self.assertEqual(pow(2, i), pow2)
     19                 if i != 30 : pow2 = pow2*2
     20 
     21             for othertype in int, long:
     22                 for i in range(-10, 0) + range(1, 10):
     23                     ii = type(i)
     24                     for j in range(1, 11):
     25                         jj = -othertype(j)
     26                         pow(ii, jj)
     27 
     28         for othertype in int, long, float:
     29             for i in range(1, 100):
     30                 zero = type(0)
     31                 exp = -othertype(i/10.0)
     32                 if exp == 0:
     33                     continue
     34                 self.assertRaises(ZeroDivisionError, pow, zero, exp)
     35 
     36         il, ih = -20, 20
     37         jl, jh = -5,   5
     38         kl, kh = -10, 10
     39         asseq = self.assertEqual
     40         if type == float:
     41             il = 1
     42             asseq = self.assertAlmostEqual
     43         elif type == int:
     44             jl = 0
     45         elif type == long:
     46             jl, jh = 0, 15
     47         for i in range(il, ih+1):
     48             for j in range(jl, jh+1):
     49                 for k in range(kl, kh+1):
     50                     if k != 0:
     51                         if type == float or j < 0:
     52                             self.assertRaises(TypeError, pow, type(i), j, k)
     53                             continue
     54                         asseq(
     55                             pow(type(i),j,k),
     56                             pow(type(i),j)% type(k)
     57                         )
     58 
     59     def test_powint(self):
     60         self.powtest(int)
     61 
     62     def test_powlong(self):
     63         self.powtest(long)
     64 
     65     def test_powfloat(self):
     66         self.powtest(float)
     67 
     68     def test_other(self):
     69         # Other tests-- not very systematic
     70         self.assertEqual(pow(3,3) % 8, pow(3,3,8))
     71         self.assertEqual(pow(3,3) % -8, pow(3,3,-8))
     72         self.assertEqual(pow(3,2) % -2, pow(3,2,-2))
     73         self.assertEqual(pow(-3,3) % 8, pow(-3,3,8))
     74         self.assertEqual(pow(-3,3) % -8, pow(-3,3,-8))
     75         self.assertEqual(pow(5,2) % -8, pow(5,2,-8))
     76 
     77         self.assertEqual(pow(3L,3L) % 8, pow(3L,3L,8))
     78         self.assertEqual(pow(3L,3L) % -8, pow(3L,3L,-8))
     79         self.assertEqual(pow(3L,2) % -2, pow(3L,2,-2))
     80         self.assertEqual(pow(-3L,3L) % 8, pow(-3L,3L,8))
     81         self.assertEqual(pow(-3L,3L) % -8, pow(-3L,3L,-8))
     82         self.assertEqual(pow(5L,2) % -8, pow(5L,2,-8))
     83 
     84         for i in range(-10, 11):
     85             for j in range(0, 6):
     86                 for k in range(-7, 11):
     87                     if j >= 0 and k != 0:
     88                         self.assertEqual(
     89                             pow(i,j) % k,
     90                             pow(i,j,k)
     91                         )
     92                     if j >= 0 and k != 0:
     93                         self.assertEqual(
     94                             pow(long(i),j) % k,
     95                             pow(long(i),j,k)
     96                         )
     97 
     98     def test_bug643260(self):
     99         class TestRpow:
    100             def __rpow__(self, other):
    101                 return None
    102         None ** TestRpow() # Won't fail when __rpow__ invoked.  SF bug #643260.
    103 
    104     def test_bug705231(self):
    105         # -1.0 raised to an integer should never blow up.  It did if the
    106         # platform pow() was buggy, and Python didn't worm around it.
    107         eq = self.assertEqual
    108         a = -1.0
    109         # The next two tests can still fail if the platform floor()
    110         # function doesn't treat all large inputs as integers
    111         # test_math should also fail if that is happening
    112         eq(pow(a, 1.23e167), 1.0)
    113         eq(pow(a, -1.23e167), 1.0)
    114         for b in range(-10, 11):
    115             eq(pow(a, float(b)), b & 1 and -1.0 or 1.0)
    116         for n in range(0, 100):
    117             fiveto = float(5 ** n)
    118             # For small n, fiveto will be odd.  Eventually we run out of
    119             # mantissa bits, though, and thereafer fiveto will be even.
    120             expected = fiveto % 2.0 and -1.0 or 1.0
    121             eq(pow(a, fiveto), expected)
    122             eq(pow(a, -fiveto), expected)
    123         eq(expected, 1.0)   # else we didn't push fiveto to evenness
    124 
    125 def test_main():
    126     test.test_support.run_unittest(PowTest)
    127 
    128 if __name__ == "__main__":
    129     test_main()
    130