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