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