blob: 64c58900504420ebad018b8b81db13c917cbfe5d [file] [log] [blame]
Guido van Rossumdc1cdca1994-08-12 13:14:22 +00001import sys
Guido van Rossume2d4dd11997-11-24 22:24:22 +00002import test_support
Guido van Rossumdc1cdca1994-08-12 13:14:22 +00003
Fred Draked2562712001-08-30 18:56:30 +00004
Guido van Rossumdc1cdca1994-08-12 13:14:22 +00005def powtest(type):
Fred Drake004d5e62000-10-23 17:22:08 +00006 if type != float:
Guido van Rossumdc1cdca1994-08-12 13:14:22 +00007 print " Testing 2-argument pow() function..."
8 for i in range(-1000, 1000):
Fred Drake004d5e62000-10-23 17:22:08 +00009 if pow(type(i), 0) != 1:
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000010 raise ValueError, 'pow('+str(i)+',0) != 1'
Tim Petersc54d1902000-10-06 00:36:09 +000011 if pow(type(i), 1) != type(i):
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000012 raise ValueError, 'pow('+str(i)+',1) != '+str(i)
Tim Petersc54d1902000-10-06 00:36:09 +000013 if pow(type(0), 1) != type(0):
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000014 raise ValueError, 'pow(0,'+str(i)+') != 0'
Tim Petersc54d1902000-10-06 00:36:09 +000015 if pow(type(1), 1) != type(1):
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000016 raise ValueError, 'pow(1,'+str(i)+') != 1'
Tim Petersc54d1902000-10-06 00:36:09 +000017
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000018 for i in range(-100, 100):
Tim Petersc54d1902000-10-06 00:36:09 +000019 if pow(type(i), 3) != i*i*i:
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000020 raise ValueError, 'pow('+str(i)+',3) != '+str(i*i*i)
Fred Drake004d5e62000-10-23 17:22:08 +000021
Tim Petersc54d1902000-10-06 00:36:09 +000022 pow2 = 1
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000023 for i in range(0,31):
Tim Petersc54d1902000-10-06 00:36:09 +000024 if pow(2, i) != pow2:
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000025 raise ValueError, 'pow(2,'+str(i)+') != '+str(pow2)
Tim Petersc54d1902000-10-06 00:36:09 +000026 if i != 30 : pow2 = pow2*2
27
28 for othertype in int, long:
29 for i in range(-10, 0) + range(1, 10):
30 ii = type(i)
31 for j in range(1, 11):
32 jj = -othertype(j)
33 try:
34 pow(ii, jj)
35 except ValueError:
Guido van Rossum28358fb2001-07-12 12:51:22 +000036 raise ValueError, "pow(%s, %s) failed" % (ii, jj)
Tim Petersc54d1902000-10-06 00:36:09 +000037
38 for othertype in int, long, float:
39 for i in range(1, 100):
40 zero = type(0)
41 exp = -othertype(i/10.0)
42 if exp == 0:
43 continue
44 try:
45 pow(zero, exp)
46 except ZeroDivisionError:
47 pass # taking zero to any negative exponent should fail
48 else:
49 raise ValueError, "pow(%s, %s) did not fail" % (zero, exp)
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000050
51 print " Testing 3-argument pow() function..."
52 il, ih = -20, 20
53 jl, jh = -5, 5
54 kl, kh = -10, 10
Guido van Rossume2d4dd11997-11-24 22:24:22 +000055 compare = cmp
Tim Petersc54d1902000-10-06 00:36:09 +000056 if type == float:
57 il = 1
Guido van Rossum41360a41998-03-26 19:42:58 +000058 compare = test_support.fcmp
Tim Petersc54d1902000-10-06 00:36:09 +000059 elif type == int:
60 jl = 0
61 elif type == long:
62 jl, jh = 0, 15
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000063 for i in range(il, ih+1):
Fred Drake004d5e62000-10-23 17:22:08 +000064 for j in range(jl, jh+1):
65 for k in range(kl, kh+1):
66 if k != 0:
Tim Peters32f453e2001-09-03 08:35:41 +000067 if type == float or j < 0:
68 try:
69 pow(type(i),j,k)
70 except TypeError:
71 pass
72 else:
73 raise TestFailed("expected TypeError from "
74 "pow%r" % ((type(i), j, k)))
75 continue
Fred Drake004d5e62000-10-23 17:22:08 +000076 if compare(pow(type(i),j,k), pow(type(i),j)% type(k)):
77 raise ValueError, "pow(" +str(i)+ "," +str(j)+ \
78 "," +str(k)+ ") != pow(" +str(i)+ "," + \
79 str(j)+ ") % " +str(k)
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000080
81
82print 'Testing integer mode...'
83powtest(int)
84print 'Testing long integer mode...'
85powtest(long)
86print 'Testing floating point mode...'
87powtest(float)
88
89# Other tests-- not very systematic
90
91print 'The number in both columns should match.'
Fred Drakedb1bd5c1999-12-23 15:36:42 +000092print `pow(3,3) % 8`, `pow(3,3,8)`
93print `pow(3,3) % -8`, `pow(3,3,-8)`
94print `pow(3,2) % -2`, `pow(3,2,-2)`
95print `pow(-3,3) % 8`, `pow(-3,3,8)`
96print `pow(-3,3) % -8`, `pow(-3,3,-8)`
97print `pow(5,2) % -8`, `pow(5,2,-8)`
Guido van Rossumdc1cdca1994-08-12 13:14:22 +000098print
99
Fred Drakedb1bd5c1999-12-23 15:36:42 +0000100print `pow(3L,3L) % 8`, `pow(3L,3L,8)`
101print `pow(3L,3L) % -8`, `pow(3L,3L,-8)`
102print `pow(3L,2) % -2`, `pow(3L,2,-2)`
103print `pow(-3L,3L) % 8`, `pow(-3L,3L,8)`
104print `pow(-3L,3L) % -8`, `pow(-3L,3L,-8)`
105print `pow(5L,2) % -8`, `pow(5L,2,-8)`
Guido van Rossumdc1cdca1994-08-12 13:14:22 +0000106print
107
Guido van Rossumdc1cdca1994-08-12 13:14:22 +0000108print
109
110for i in range(-10, 11):
Fred Drake004d5e62000-10-23 17:22:08 +0000111 for j in range(0, 6):
112 for k in range(-7, 11):
113 if j >= 0 and k != 0:
114 o = pow(i,j) % k
115 n = pow(i,j,k)
116 if o != n: print 'Integer mismatch:', i,j,k
Fred Drake132dce22000-12-12 23:11:42 +0000117 if j >= 0 and k != 0:
Fred Drake004d5e62000-10-23 17:22:08 +0000118 o = pow(long(i),j) % k
119 n = pow(long(i),j,k)
Tim Peters0628a662001-09-03 08:44:02 +0000120 if o != n: print 'Integer mismatch:', i,j,k