blob: 03f7314894e01508c1734dead1467e5d9317c714 [file] [log] [blame]
Guido van Rossumdc1cdca1994-08-12 13:14:22 +00001import sys
2
3def powtest(type):
4 if (type!=float):
5 print " Testing 2-argument pow() function..."
6 for i in range(-1000, 1000):
7 if (pow(type(i),0)!=1):
8 raise ValueError, 'pow('+str(i)+',0) != 1'
9 if (pow(type(i),1)!=type(i)):
10 raise ValueError, 'pow('+str(i)+',1) != '+str(i)
11 if (pow(type(0),1)!=type(0)):
12 raise ValueError, 'pow(0,'+str(i)+') != 0'
13 if (pow(type(1),1)!=type(1)):
14 raise ValueError, 'pow(1,'+str(i)+') != 1'
15
16 for i in range(-100, 100):
17 if (pow(type(i),3)!=i*i*i):
18 raise ValueError, 'pow('+str(i)+',3) != '+str(i*i*i)
19
20 pow2=1
21 for i in range(0,31):
22 if (pow(2,i)!=pow2):
23 raise ValueError, 'pow(2,'+str(i)+') != '+str(pow2)
24 if (i!=30): pow2=pow2*2
25
26 print " Testing 3-argument pow() function..."
27 il, ih = -20, 20
28 jl, jh = -5, 5
29 kl, kh = -10, 10
30 if (type==float):
31 il=1
32 elif (type==int):
33 jl=0
34 elif (type==long):
35 jl,jh = 0, 15
36 for i in range(il, ih+1):
37 for j in range(jl,jh+1):
38 for k in range(kl, kh+1):
39 if (k!=0):
40 if (pow(type(i),j,k)!=pow(type(i),j)% type(k) ):
41 raise ValueError, "pow(" +str(i)+ "," +str(j)+ \
42 "," +str(k)+ ") != pow(" +str(i)+ "," + \
43 str(j)+ ") % " +str(k)
44
45
46print 'Testing integer mode...'
47powtest(int)
48print 'Testing long integer mode...'
49powtest(long)
50print 'Testing floating point mode...'
51powtest(float)
52
53# Other tests-- not very systematic
54
55print 'The number in both columns should match.'
56print pow(3,3) % 8, pow(3,3,8)
57print pow(3,3) % -8, pow(3,3,-8)
58print pow(3,2) % -2, pow(3,2,-2)
59print pow(-3,3) % 8, pow(-3,3,8)
60print pow(-3,3) % -8, pow(-3,3,-8)
61print pow(5,2) % -8, pow(5,2,-8)
62print
63
64print pow(3L,3L) % 8, pow(3L,3L,8)
65print pow(3L,3L) % -8, pow(3L,3L,-8)
66print pow(3L,2) % -2, pow(3L,2,-2)
67print pow(-3L,3L) % 8, pow(-3L,3L,8)
68print pow(-3L,3L) % -8, pow(-3L,3L,-8)
69print pow(5L,2) % -8, pow(5L,2,-8)
70print
71
72print pow(3.0,3.0) % 8, pow(3.0,3.0,8)
73print pow(3.0,3.0) % -8, pow(3.0,3.0,-8)
74print pow(3.0,2) % -2, pow(3.0,2,-2)
75print pow(5.0,2) % -8, pow(5.0,2,-8)
76print
77
78for i in range(-10, 11):
79 for j in range(0, 6):
80 for k in range(-7, 11):
81 if (j>=0 and k!=0):
82 o=pow(i,j) % k
83 n=pow(i,j,k)
84 if (o!=n): print 'Integer mismatch:', i,j,k
85 if (j>=0 and k<>0):
86 o=pow(long(i),j) % k
87 n=pow(long(i),j,k)
88 if (o!=n): print 'Long mismatch:', i,j,k
89 if (i>=0 and k<>0):
90 o=pow(float(i),j) % k
91 n=pow(float(i),j,k)
92 if (o!=n): print 'Float mismatch:', i,j,k