blob: 20b10663a2e4d0033d5b2a27c25939c1d1ad0e29 [file] [log] [blame]
Benjamin Petersonee8712c2008-05-20 21:35:26 +00001import test.support, unittest
Guido van Rossumdc1cdca1994-08-12 13:14:22 +00002
Walter Dörwald363f6d62003-02-03 20:17:19 +00003class PowTest(unittest.TestCase):
Fred Draked2562712001-08-30 18:56:30 +00004
Walter Dörwald363f6d62003-02-03 20:17:19 +00005 def powtest(self, type):
6 if type != float:
7 for i in range(-1000, 1000):
Ezio Melotti19f2aeb2010-11-21 01:30:29 +00008 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 Petersc54d1902000-10-06 00:36:09 +000012
Walter Dörwald363f6d62003-02-03 20:17:19 +000013 for i in range(-100, 100):
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000014 self.assertEqual(pow(type(i), 3), i*i*i)
Fred Drake004d5e62000-10-23 17:22:08 +000015
Walter Dörwald363f6d62003-02-03 20:17:19 +000016 pow2 = 1
Guido van Rossum805365e2007-05-07 22:24:25 +000017 for i in range(0, 31):
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000018 self.assertEqual(pow(2, i), pow2)
Walter Dörwald363f6d62003-02-03 20:17:19 +000019 if i != 30 : pow2 = pow2*2
Tim Petersc54d1902000-10-06 00:36:09 +000020
Florent Xiclunab4efb3d2010-08-14 18:24:40 +000021 for othertype in (int,):
Guido van Rossum805365e2007-05-07 22:24:25 +000022 for i in list(range(-10, 0)) + list(range(1, 10)):
Walter Dörwald363f6d62003-02-03 20:17:19 +000023 ii = type(i)
24 for j in range(1, 11):
25 jj = -othertype(j)
Tim Petersc54d1902000-10-06 00:36:09 +000026 pow(ii, jj)
Tim Petersc54d1902000-10-06 00:36:09 +000027
Florent Xiclunab4efb3d2010-08-14 18:24:40 +000028 for othertype in int, float:
Walter Dörwald363f6d62003-02-03 20:17:19 +000029 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 Rossumdc1cdca1994-08-12 13:14:22 +000035
Walter Dörwald363f6d62003-02-03 20:17:19 +000036 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 Rossume2a383d2007-01-15 16:59:06 +000045 elif type == int:
Walter Dörwald363f6d62003-02-03 20:17:19 +000046 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 Rossume2a383d2007-01-15 16:59:06 +000063 self.powtest(int)
Walter Dörwald363f6d62003-02-03 20:17:19 +000064
65 def test_powfloat(self):
66 self.powtest(float)
67
68 def test_other(self):
69 # Other tests-- not very systematic
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000070 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örwald363f6d62003-02-03 20:17:19 +000076
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000077 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örwald363f6d62003-02-03 20:17:19 +000083
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 Melotti19f2aeb2010-11-21 01:30:29 +000088 self.assertEqual(
Walter Dörwald363f6d62003-02-03 20:17:19 +000089 pow(i,j) % k,
90 pow(i,j,k)
91 )
92 if j >= 0 and k != 0:
Ezio Melotti19f2aeb2010-11-21 01:30:29 +000093 self.assertEqual(
Guido van Rossume2a383d2007-01-15 16:59:06 +000094 pow(int(i),j) % k,
95 pow(int(i),j,k)
Walter Dörwald363f6d62003-02-03 20:17:19 +000096 )
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 Rossumdc1cdca1994-08-12 13:14:22 +0000103
Tim Peterse87568d2003-05-24 20:18:24 +0000104 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 Melotti19f2aeb2010-11-21 01:30:29 +0000107 eq = self.assertEqual
Tim Peterse87568d2003-05-24 20:18:24 +0000108 a = -1.0
Guido van Rossum806c2462007-08-06 23:33:07 +0000109 # 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 Peterse87568d2003-05-24 20:18:24 +0000112 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 Rossumdc1cdca1994-08-12 13:14:22 +0000124
Walter Dörwald363f6d62003-02-03 20:17:19 +0000125def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000126 test.support.run_unittest(PowTest)
Guido van Rossumdc1cdca1994-08-12 13:14:22 +0000127
Walter Dörwald363f6d62003-02-03 20:17:19 +0000128if __name__ == "__main__":
129 test_main()