Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | import test.support, unittest |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 2 | |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 3 | class PowTest(unittest.TestCase): |
Fred Drake | d256271 | 2001-08-30 18:56:30 +0000 | [diff] [blame] | 4 | |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 5 | def powtest(self, type): |
| 6 | if type != float: |
| 7 | for i in range(-1000, 1000): |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 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)) |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 12 | |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 13 | for i in range(-100, 100): |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 14 | self.assertEqual(pow(type(i), 3), i*i*i) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 15 | |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 16 | pow2 = 1 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 17 | for i in range(0, 31): |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 18 | self.assertEqual(pow(2, i), pow2) |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 19 | if i != 30 : pow2 = pow2*2 |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 20 | |
Florent Xicluna | b4efb3d | 2010-08-14 18:24:40 +0000 | [diff] [blame] | 21 | for othertype in (int,): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 22 | for i in list(range(-10, 0)) + list(range(1, 10)): |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 23 | ii = type(i) |
| 24 | for j in range(1, 11): |
| 25 | jj = -othertype(j) |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 26 | pow(ii, jj) |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 27 | |
Florent Xicluna | b4efb3d | 2010-08-14 18:24:40 +0000 | [diff] [blame] | 28 | for othertype in int, float: |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 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) |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 35 | |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 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 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 45 | elif type == int: |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 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): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 63 | self.powtest(int) |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 64 | |
| 65 | def test_powfloat(self): |
| 66 | self.powtest(float) |
| 67 | |
| 68 | def test_other(self): |
| 69 | # Other tests-- not very systematic |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 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)) |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 76 | |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 77 | self.assertEqual(pow(3,3) % 8, pow(3,3,8)) |
| 78 | self.assertEqual(pow(3,3) % -8, pow(3,3,-8)) |
| 79 | self.assertEqual(pow(3,2) % -2, pow(3,2,-2)) |
| 80 | self.assertEqual(pow(-3,3) % 8, pow(-3,3,8)) |
| 81 | self.assertEqual(pow(-3,3) % -8, pow(-3,3,-8)) |
| 82 | self.assertEqual(pow(5,2) % -8, pow(5,2,-8)) |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 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: |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 88 | self.assertEqual( |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 89 | pow(i,j) % k, |
| 90 | pow(i,j,k) |
| 91 | ) |
| 92 | if j >= 0 and k != 0: |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 93 | self.assertEqual( |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 94 | pow(int(i),j) % k, |
| 95 | pow(int(i),j,k) |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 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. |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 103 | |
Tim Peters | e87568d | 2003-05-24 20:18:24 +0000 | [diff] [blame] | 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. |
Ezio Melotti | 19f2aeb | 2010-11-21 01:30:29 +0000 | [diff] [blame] | 107 | eq = self.assertEqual |
Tim Peters | e87568d | 2003-05-24 20:18:24 +0000 | [diff] [blame] | 108 | a = -1.0 |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 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 |
Tim Peters | e87568d | 2003-05-24 20:18:24 +0000 | [diff] [blame] | 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 |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 124 | |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 125 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 126 | test.support.run_unittest(PowTest) |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 127 | |
Walter Dörwald | 363f6d6 | 2003-02-03 20:17:19 +0000 | [diff] [blame] | 128 | if __name__ == "__main__": |
| 129 | test_main() |