1 import test
.test_support
, unittest
3 class PowTest(unittest
.TestCase
):
5 def powtest(self
, type):
7 for i
in range(-1000, 1000):
8 self
.assertEquals(pow(type(i
), 0), 1)
9 self
.assertEquals(pow(type(i
), 1), type(i
))
10 self
.assertEquals(pow(type(0), 1), type(0))
11 self
.assertEquals(pow(type(1), 1), type(1))
13 for i
in range(-100, 100):
14 self
.assertEquals(pow(type(i
), 3), i
*i
*i
)
18 self
.assertEquals(pow(2, i
), pow2
)
19 if i
!= 30 : pow2
= pow2
*2
21 for othertype
in int, long:
22 for i
in range(-10, 0) + range(1, 10):
24 for j
in range(1, 11):
28 for othertype
in int, long, float:
29 for i
in range(1, 100):
31 exp
= -othertype(i
/10.0)
34 self
.assertRaises(ZeroDivisionError, pow, zero
, exp
)
39 asseq
= self
.assertEqual
42 asseq
= self
.assertAlmostEqual
47 for i
in range(il
, ih
+1):
48 for j
in range(jl
, jh
+1):
49 for k
in range(kl
, kh
+1):
51 if type == float or j
< 0:
52 self
.assertRaises(TypeError, pow, type(i
), j
, k
)
56 pow(type(i
),j
)% type(k
)
59 def test_powint(self
):
62 def test_powlong(self
):
65 def test_powfloat(self
):
69 # Other tests-- not very systematic
70 self
.assertEquals(pow(3,3) % 8, pow(3,3,8))
71 self
.assertEquals(pow(3,3) % -8, pow(3,3,-8))
72 self
.assertEquals(pow(3,2) % -2, pow(3,2,-2))
73 self
.assertEquals(pow(-3,3) % 8, pow(-3,3,8))
74 self
.assertEquals(pow(-3,3) % -8, pow(-3,3,-8))
75 self
.assertEquals(pow(5,2) % -8, pow(5,2,-8))
77 self
.assertEquals(pow(3L,3L) % 8, pow(3L,3L,8))
78 self
.assertEquals(pow(3L,3L) % -8, pow(3L,3L,-8))
79 self
.assertEquals(pow(3L,2) % -2, pow(3L,2,-2))
80 self
.assertEquals(pow(-3L,3L) % 8, pow(-3L,3L,8))
81 self
.assertEquals(pow(-3L,3L) % -8, pow(-3L,3L,-8))
82 self
.assertEquals(pow(5L,2) % -8, pow(5L,2,-8))
84 for i
in range(-10, 11):
86 for k
in range(-7, 11):
98 def test_bug643260(self
):
100 def __rpow__(self
, other
):
102 None ** TestRpow() # Won't fail when __rpow__ invoked. SF bug #643260.
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.
107 eq
= self
.assertEquals
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
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
126 test
.test_support
.run_unittest(PowTest
)
128 if __name__
== "__main__":