0001 import test.test_support, unittest 0002 0003 class PowTest(unittest.TestCase): 0004 0005 def powtest(self, type): 0006 if type != float: 0007 for i in range(-1000, 1000): 0008 self.assertEquals(pow(type(i), 0), 1) 0009 self.assertEquals(pow(type(i), 1), type(i)) 0010 self.assertEquals(pow(type(0), 1), type(0)) 0011 self.assertEquals(pow(type(1), 1), type(1)) 0012 0013 for i in range(-100, 100): 0014 self.assertEquals(pow(type(i), 3), i*i*i) 0015 0016 pow2 = 1 0017 for i in range(0,31): 0018 self.assertEquals(pow(2, i), pow2) 0019 if i != 30 : pow2 = pow2*2 0020 0021 for othertype in int, long: 0022 for i in range(-10, 0) + range(1, 10): 0023 ii = type(i) 0024 for j in range(1, 11): 0025 jj = -othertype(j) 0026 pow(ii, jj) 0027 0028 for othertype in int, long, float: 0029 for i in range(1, 100): 0030 zero = type(0) 0031 exp = -othertype(i/10.0) 0032 if exp == 0: 0033 continue 0034 self.assertRaises(ZeroDivisionError, pow, zero, exp) 0035 0036 il, ih = -20, 20 0037 jl, jh = -5, 5 0038 kl, kh = -10, 10 0039 asseq = self.assertEqual 0040 if type == float: 0041 il = 1 0042 asseq = self.assertAlmostEqual 0043 elif type == int: 0044 jl = 0 0045 elif type == long: 0046 jl, jh = 0, 15 0047 for i in range(il, ih+1): 0048 for j in range(jl, jh+1): 0049 for k in range(kl, kh+1): 0050 if k != 0: 0051 if type == float or j < 0: 0052 self.assertRaises(TypeError, pow, type(i), j, k) 0053 continue 0054 asseq( 0055 pow(type(i),j,k), 0056 pow(type(i),j)% type(k) 0057 ) 0058 0059 def test_powint(self): 0060 self.powtest(int) 0061 0062 def test_powlong(self): 0063 self.powtest(long) 0064 0065 def test_powfloat(self): 0066 self.powtest(float) 0067 0068 def test_other(self): 0069 # Other tests-- not very systematic 0070 self.assertEquals(pow(3,3) % 8, pow(3,3,8)) 0071 self.assertEquals(pow(3,3) % -8, pow(3,3,-8)) 0072 self.assertEquals(pow(3,2) % -2, pow(3,2,-2)) 0073 self.assertEquals(pow(-3,3) % 8, pow(-3,3,8)) 0074 self.assertEquals(pow(-3,3) % -8, pow(-3,3,-8)) 0075 self.assertEquals(pow(5,2) % -8, pow(5,2,-8)) 0076 0077 self.assertEquals(pow(3L,3L) % 8, pow(3L,3L,8)) 0078 self.assertEquals(pow(3L,3L) % -8, pow(3L,3L,-8)) 0079 self.assertEquals(pow(3L,2) % -2, pow(3L,2,-2)) 0080 self.assertEquals(pow(-3L,3L) % 8, pow(-3L,3L,8)) 0081 self.assertEquals(pow(-3L,3L) % -8, pow(-3L,3L,-8)) 0082 self.assertEquals(pow(5L,2) % -8, pow(5L,2,-8)) 0083 0084 for i in range(-10, 11): 0085 for j in range(0, 6): 0086 for k in range(-7, 11): 0087 if j >= 0 and k != 0: 0088 self.assertEquals( 0089 pow(i,j) % k, 0090 pow(i,j,k) 0091 ) 0092 if j >= 0 and k != 0: 0093 self.assertEquals( 0094 pow(long(i),j) % k, 0095 pow(long(i),j,k) 0096 ) 0097 0098 def test_bug643260(self): 0099 class TestRpow: 0100 def __rpow__(self, other): 0101 return None 0102 None ** TestRpow() # Won't fail when __rpow__ invoked. SF bug #643260. 0103 0104 def test_bug705231(self): 0105 # -1.0 raised to an integer should never blow up. It did if the 0106 # platform pow() was buggy, and Python didn't worm around it. 0107 eq = self.assertEquals 0108 a = -1.0 0109 eq(pow(a, 1.23e167), 1.0) 0110 eq(pow(a, -1.23e167), 1.0) 0111 for b in range(-10, 11): 0112 eq(pow(a, float(b)), b & 1 and -1.0 or 1.0) 0113 for n in range(0, 100): 0114 fiveto = float(5 ** n) 0115 # For small n, fiveto will be odd. Eventually we run out of 0116 # mantissa bits, though, and thereafer fiveto will be even. 0117 expected = fiveto % 2.0 and -1.0 or 1.0 0118 eq(pow(a, fiveto), expected) 0119 eq(pow(a, -fiveto), expected) 0120 eq(expected, 1.0) # else we didn't push fiveto to evenness 0121 0122 def test_main(): 0123 test.test_support.run_unittest(PowTest) 0124 0125 if __name__ == "__main__": 0126 test_main() 0127
Generated by PyXR 0.9.4