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