Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 1 | import sys |
| 2 | |
| 3 | def 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 | |
| 46 | print 'Testing integer mode...' |
| 47 | powtest(int) |
| 48 | print 'Testing long integer mode...' |
| 49 | powtest(long) |
| 50 | print 'Testing floating point mode...' |
| 51 | powtest(float) |
| 52 | |
| 53 | # Other tests-- not very systematic |
| 54 | |
| 55 | print 'The number in both columns should match.' |
| 56 | print pow(3,3) % 8, pow(3,3,8) |
| 57 | print pow(3,3) % -8, pow(3,3,-8) |
| 58 | print pow(3,2) % -2, pow(3,2,-2) |
| 59 | print pow(-3,3) % 8, pow(-3,3,8) |
| 60 | print pow(-3,3) % -8, pow(-3,3,-8) |
| 61 | print pow(5,2) % -8, pow(5,2,-8) |
| 62 | print |
| 63 | |
| 64 | print pow(3L,3L) % 8, pow(3L,3L,8) |
| 65 | print pow(3L,3L) % -8, pow(3L,3L,-8) |
| 66 | print pow(3L,2) % -2, pow(3L,2,-2) |
| 67 | print pow(-3L,3L) % 8, pow(-3L,3L,8) |
| 68 | print pow(-3L,3L) % -8, pow(-3L,3L,-8) |
| 69 | print pow(5L,2) % -8, pow(5L,2,-8) |
| 70 | print |
| 71 | |
| 72 | print pow(3.0,3.0) % 8, pow(3.0,3.0,8) |
| 73 | print pow(3.0,3.0) % -8, pow(3.0,3.0,-8) |
| 74 | print pow(3.0,2) % -2, pow(3.0,2,-2) |
| 75 | print pow(5.0,2) % -8, pow(5.0,2,-8) |
| 76 | print |
| 77 | |
| 78 | for 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 |