Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 1 | import sys |
Guido van Rossum | e2d4dd1 | 1997-11-24 22:24:22 +0000 | [diff] [blame] | 2 | import test_support |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 3 | |
| 4 | def powtest(type): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 5 | if type != float: |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 6 | print " Testing 2-argument pow() function..." |
| 7 | for i in range(-1000, 1000): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 8 | if pow(type(i), 0) != 1: |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 9 | raise ValueError, 'pow('+str(i)+',0) != 1' |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 10 | if pow(type(i), 1) != type(i): |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 11 | raise ValueError, 'pow('+str(i)+',1) != '+str(i) |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 12 | if pow(type(0), 1) != type(0): |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 13 | raise ValueError, 'pow(0,'+str(i)+') != 0' |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 14 | if pow(type(1), 1) != type(1): |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 15 | raise ValueError, 'pow(1,'+str(i)+') != 1' |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 17 | for i in range(-100, 100): |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 18 | if pow(type(i), 3) != i*i*i: |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 19 | raise ValueError, 'pow('+str(i)+',3) != '+str(i*i*i) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 20 | |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 21 | pow2 = 1 |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 22 | for i in range(0,31): |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 23 | if pow(2, i) != pow2: |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 24 | raise ValueError, 'pow(2,'+str(i)+') != '+str(pow2) |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 25 | if i != 30 : pow2 = pow2*2 |
| 26 | |
| 27 | for othertype in int, long: |
| 28 | for i in range(-10, 0) + range(1, 10): |
| 29 | ii = type(i) |
| 30 | for j in range(1, 11): |
| 31 | jj = -othertype(j) |
| 32 | try: |
| 33 | pow(ii, jj) |
| 34 | except ValueError: |
| 35 | pass # taking an int to a neg int power should fail |
| 36 | else: |
| 37 | raise ValueError, "pow(%s, %s) did not fail" % (ii, jj) |
| 38 | |
| 39 | for othertype in int, long, float: |
| 40 | for i in range(1, 100): |
| 41 | zero = type(0) |
| 42 | exp = -othertype(i/10.0) |
| 43 | if exp == 0: |
| 44 | continue |
| 45 | try: |
| 46 | pow(zero, exp) |
| 47 | except ZeroDivisionError: |
| 48 | pass # taking zero to any negative exponent should fail |
| 49 | else: |
| 50 | raise ValueError, "pow(%s, %s) did not fail" % (zero, exp) |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 51 | |
| 52 | print " Testing 3-argument pow() function..." |
| 53 | il, ih = -20, 20 |
| 54 | jl, jh = -5, 5 |
| 55 | kl, kh = -10, 10 |
Guido van Rossum | e2d4dd1 | 1997-11-24 22:24:22 +0000 | [diff] [blame] | 56 | compare = cmp |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 57 | if type == float: |
| 58 | il = 1 |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 59 | compare = test_support.fcmp |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 60 | elif type == int: |
| 61 | jl = 0 |
| 62 | elif type == long: |
| 63 | jl, jh = 0, 15 |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 64 | for i in range(il, ih+1): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 65 | for j in range(jl, jh+1): |
| 66 | for k in range(kl, kh+1): |
| 67 | if k != 0: |
| 68 | if compare(pow(type(i),j,k), pow(type(i),j)% type(k)): |
| 69 | raise ValueError, "pow(" +str(i)+ "," +str(j)+ \ |
| 70 | "," +str(k)+ ") != pow(" +str(i)+ "," + \ |
| 71 | str(j)+ ") % " +str(k) |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 72 | |
| 73 | |
| 74 | print 'Testing integer mode...' |
| 75 | powtest(int) |
| 76 | print 'Testing long integer mode...' |
| 77 | powtest(long) |
| 78 | print 'Testing floating point mode...' |
| 79 | powtest(float) |
| 80 | |
| 81 | # Other tests-- not very systematic |
| 82 | |
| 83 | print 'The number in both columns should match.' |
Fred Drake | db1bd5c | 1999-12-23 15:36:42 +0000 | [diff] [blame] | 84 | print `pow(3,3) % 8`, `pow(3,3,8)` |
| 85 | print `pow(3,3) % -8`, `pow(3,3,-8)` |
| 86 | print `pow(3,2) % -2`, `pow(3,2,-2)` |
| 87 | print `pow(-3,3) % 8`, `pow(-3,3,8)` |
| 88 | print `pow(-3,3) % -8`, `pow(-3,3,-8)` |
| 89 | print `pow(5,2) % -8`, `pow(5,2,-8)` |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 90 | print |
| 91 | |
Fred Drake | db1bd5c | 1999-12-23 15:36:42 +0000 | [diff] [blame] | 92 | print `pow(3L,3L) % 8`, `pow(3L,3L,8)` |
| 93 | print `pow(3L,3L) % -8`, `pow(3L,3L,-8)` |
| 94 | print `pow(3L,2) % -2`, `pow(3L,2,-2)` |
| 95 | print `pow(-3L,3L) % 8`, `pow(-3L,3L,8)` |
| 96 | print `pow(-3L,3L) % -8`, `pow(-3L,3L,-8)` |
| 97 | print `pow(5L,2) % -8`, `pow(5L,2,-8)` |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 98 | print |
| 99 | |
| 100 | print pow(3.0,3.0) % 8, pow(3.0,3.0,8) |
| 101 | print pow(3.0,3.0) % -8, pow(3.0,3.0,-8) |
| 102 | print pow(3.0,2) % -2, pow(3.0,2,-2) |
| 103 | print pow(5.0,2) % -8, pow(5.0,2,-8) |
| 104 | print |
| 105 | |
| 106 | for i in range(-10, 11): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 107 | for j in range(0, 6): |
| 108 | for k in range(-7, 11): |
| 109 | if j >= 0 and k != 0: |
| 110 | o = pow(i,j) % k |
| 111 | n = pow(i,j,k) |
| 112 | if o != n: print 'Integer mismatch:', i,j,k |
| 113 | if j >= 0 and k <> 0: |
| 114 | o = pow(long(i),j) % k |
| 115 | n = pow(long(i),j,k) |
| 116 | if o != n: print 'Long mismatch:', i,j,k |
| 117 | if i >= 0 and k <> 0: |
| 118 | o = pow(float(i),j) % k |
| 119 | n = pow(float(i),j,k) |
| 120 | if o != n: print 'Float mismatch:', i,j,k |