ยปCore Development>Code coverage>Lib/test/test_pow.py

# Python code coverage for Lib/test/test_pow.py

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