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 | |
Fred Drake | d256271 | 2001-08-30 18:56:30 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 5 | def powtest(type): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 6 | if type != float: |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 7 | print " Testing 2-argument pow() function..." |
| 8 | for i in range(-1000, 1000): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 9 | if pow(type(i), 0) != 1: |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 10 | raise ValueError, 'pow('+str(i)+',0) != 1' |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 11 | if pow(type(i), 1) != type(i): |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 12 | raise ValueError, 'pow('+str(i)+',1) != '+str(i) |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 13 | if pow(type(0), 1) != type(0): |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 14 | raise ValueError, 'pow(0,'+str(i)+') != 0' |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 15 | if pow(type(1), 1) != type(1): |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 16 | raise ValueError, 'pow(1,'+str(i)+') != 1' |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 18 | for i in range(-100, 100): |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 19 | if pow(type(i), 3) != i*i*i: |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 20 | raise ValueError, 'pow('+str(i)+',3) != '+str(i*i*i) |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 21 | |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 22 | pow2 = 1 |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 23 | for i in range(0,31): |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 24 | if pow(2, i) != pow2: |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 25 | raise ValueError, 'pow(2,'+str(i)+') != '+str(pow2) |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 26 | 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 Rossum | 28358fb | 2001-07-12 12:51:22 +0000 | [diff] [blame] | 36 | raise ValueError, "pow(%s, %s) failed" % (ii, jj) |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 37 | |
| 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 Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 50 | |
| 51 | print " Testing 3-argument pow() function..." |
| 52 | il, ih = -20, 20 |
| 53 | jl, jh = -5, 5 |
| 54 | kl, kh = -10, 10 |
Guido van Rossum | e2d4dd1 | 1997-11-24 22:24:22 +0000 | [diff] [blame] | 55 | compare = cmp |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 56 | if type == float: |
| 57 | il = 1 |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 58 | compare = test_support.fcmp |
Tim Peters | c54d190 | 2000-10-06 00:36:09 +0000 | [diff] [blame] | 59 | elif type == int: |
| 60 | jl = 0 |
| 61 | elif type == long: |
| 62 | jl, jh = 0, 15 |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 63 | for i in range(il, ih+1): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 64 | for j in range(jl, jh+1): |
| 65 | for k in range(kl, kh+1): |
| 66 | if k != 0: |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 67 | if type == float or j < 0: |
| 68 | try: |
| 69 | pow(type(i),j,k) |
| 70 | except TypeError: |
| 71 | pass |
| 72 | else: |
Neal Norwitz | 05c09d0 | 2002-04-01 19:01:39 +0000 | [diff] [blame] | 73 | raise ValueError, "expected TypeError from " + \ |
| 74 | "pow%r" % ((type(i), j, k),) |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 75 | continue |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 76 | 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 Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 80 | |
| 81 | |
| 82 | print 'Testing integer mode...' |
| 83 | powtest(int) |
| 84 | print 'Testing long integer mode...' |
| 85 | powtest(long) |
| 86 | print 'Testing floating point mode...' |
| 87 | powtest(float) |
| 88 | |
| 89 | # Other tests-- not very systematic |
| 90 | |
| 91 | print 'The number in both columns should match.' |
Fred Drake | db1bd5c | 1999-12-23 15:36:42 +0000 | [diff] [blame] | 92 | print `pow(3,3) % 8`, `pow(3,3,8)` |
| 93 | print `pow(3,3) % -8`, `pow(3,3,-8)` |
| 94 | print `pow(3,2) % -2`, `pow(3,2,-2)` |
| 95 | print `pow(-3,3) % 8`, `pow(-3,3,8)` |
| 96 | print `pow(-3,3) % -8`, `pow(-3,3,-8)` |
| 97 | print `pow(5,2) % -8`, `pow(5,2,-8)` |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 98 | print |
| 99 | |
Fred Drake | db1bd5c | 1999-12-23 15:36:42 +0000 | [diff] [blame] | 100 | print `pow(3L,3L) % 8`, `pow(3L,3L,8)` |
| 101 | print `pow(3L,3L) % -8`, `pow(3L,3L,-8)` |
| 102 | print `pow(3L,2) % -2`, `pow(3L,2,-2)` |
| 103 | print `pow(-3L,3L) % 8`, `pow(-3L,3L,8)` |
| 104 | print `pow(-3L,3L) % -8`, `pow(-3L,3L,-8)` |
| 105 | print `pow(5L,2) % -8`, `pow(5L,2,-8)` |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 106 | print |
| 107 | |
Guido van Rossum | dc1cdca | 1994-08-12 13:14:22 +0000 | [diff] [blame] | 108 | print |
| 109 | |
| 110 | for i in range(-10, 11): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 111 | 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 Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 117 | if j >= 0 and k != 0: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 118 | o = pow(long(i),j) % k |
| 119 | n = pow(long(i),j,k) |
Tim Peters | 0628a66 | 2001-09-03 08:44:02 +0000 | [diff] [blame] | 120 | if o != n: print 'Integer mismatch:', i,j,k |