| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2002-07-23 19:04:11 +00:00
										 |  |  | from test import test_support | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-30 18:56:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  | def powtest(type): | 
					
						
							| 
									
										
										
										
											2000-10-23 17:22:08 +00:00
										 |  |  |     if type != float: | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  |         print "    Testing 2-argument pow() function..." | 
					
						
							|  |  |  |         for i in range(-1000, 1000): | 
					
						
							| 
									
										
										
										
											2000-10-23 17:22:08 +00:00
										 |  |  |             if pow(type(i), 0) != 1: | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  |                 raise ValueError, 'pow('+str(i)+',0) != 1' | 
					
						
							| 
									
										
										
										
											2000-10-06 00:36:09 +00:00
										 |  |  |             if pow(type(i), 1) != type(i): | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  |                 raise ValueError, 'pow('+str(i)+',1) != '+str(i) | 
					
						
							| 
									
										
										
										
											2000-10-06 00:36:09 +00:00
										 |  |  |             if pow(type(0), 1) != type(0): | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  |                 raise ValueError, 'pow(0,'+str(i)+') != 0' | 
					
						
							| 
									
										
										
										
											2000-10-06 00:36:09 +00:00
										 |  |  |             if pow(type(1), 1) != type(1): | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  |                 raise ValueError, 'pow(1,'+str(i)+') != 1' | 
					
						
							| 
									
										
										
										
											2000-10-06 00:36:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  |         for i in range(-100, 100): | 
					
						
							| 
									
										
										
										
											2000-10-06 00:36:09 +00:00
										 |  |  |             if pow(type(i), 3) != i*i*i: | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  |                 raise ValueError, 'pow('+str(i)+',3) != '+str(i*i*i) | 
					
						
							| 
									
										
										
										
											2000-10-23 17:22:08 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-10-06 00:36:09 +00:00
										 |  |  |         pow2 = 1 | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  |         for i in range(0,31): | 
					
						
							| 
									
										
										
										
											2000-10-06 00:36:09 +00:00
										 |  |  |             if pow(2, i) != pow2: | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  |                 raise ValueError, 'pow(2,'+str(i)+') != '+str(pow2) | 
					
						
							| 
									
										
										
										
											2000-10-06 00:36:09 +00:00
										 |  |  |             if i != 30 : pow2 = pow2*2 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for othertype in int, long: | 
					
						
							|  |  |  |             for i in range(-10, 0) + range(1, 10): | 
					
						
							|  |  |  |                 ii = type(i) | 
					
						
							|  |  |  |                 for j in range(1, 11): | 
					
						
							|  |  |  |                     jj = -othertype(j) | 
					
						
							|  |  |  |                     try: | 
					
						
							|  |  |  |                         pow(ii, jj) | 
					
						
							|  |  |  |                     except ValueError: | 
					
						
							| 
									
										
										
										
											2001-07-12 12:51:22 +00:00
										 |  |  |                         raise ValueError, "pow(%s, %s) failed" % (ii, jj) | 
					
						
							| 
									
										
										
										
											2000-10-06 00:36:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for othertype in int, long, float: | 
					
						
							|  |  |  |         for i in range(1, 100): | 
					
						
							|  |  |  |             zero = type(0) | 
					
						
							|  |  |  |             exp = -othertype(i/10.0) | 
					
						
							|  |  |  |             if exp == 0: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 pow(zero, exp) | 
					
						
							|  |  |  |             except ZeroDivisionError: | 
					
						
							|  |  |  |                 pass # taking zero to any negative exponent should fail | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 raise ValueError, "pow(%s, %s) did not fail" % (zero, exp) | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     print "    Testing 3-argument pow() function..." | 
					
						
							|  |  |  |     il, ih = -20, 20 | 
					
						
							|  |  |  |     jl, jh = -5,   5 | 
					
						
							|  |  |  |     kl, kh = -10, 10 | 
					
						
							| 
									
										
										
										
											1997-11-24 22:24:22 +00:00
										 |  |  |     compare = cmp | 
					
						
							| 
									
										
										
										
											2000-10-06 00:36:09 +00:00
										 |  |  |     if type == float: | 
					
						
							|  |  |  |         il = 1 | 
					
						
							| 
									
										
										
										
											1998-03-26 19:42:58 +00:00
										 |  |  |         compare = test_support.fcmp | 
					
						
							| 
									
										
										
										
											2000-10-06 00:36:09 +00:00
										 |  |  |     elif type == int: | 
					
						
							|  |  |  |         jl = 0 | 
					
						
							|  |  |  |     elif type == long: | 
					
						
							|  |  |  |         jl, jh = 0, 15 | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  |     for i in range(il, ih+1): | 
					
						
							| 
									
										
										
										
											2000-10-23 17:22:08 +00:00
										 |  |  |         for j in range(jl, jh+1): | 
					
						
							|  |  |  |             for k in range(kl, kh+1): | 
					
						
							|  |  |  |                 if k != 0: | 
					
						
							| 
									
										
										
										
											2001-09-03 08:35:41 +00:00
										 |  |  |                     if type == float or j < 0: | 
					
						
							|  |  |  |                         try: | 
					
						
							|  |  |  |                             pow(type(i),j,k) | 
					
						
							|  |  |  |                         except TypeError: | 
					
						
							|  |  |  |                             pass | 
					
						
							|  |  |  |                         else: | 
					
						
							| 
									
										
										
										
											2002-04-01 19:01:39 +00:00
										 |  |  |                             raise ValueError, "expected TypeError from " + \ | 
					
						
							|  |  |  |                                 "pow%r" % ((type(i), j, k),) | 
					
						
							| 
									
										
										
										
											2001-09-03 08:35:41 +00:00
										 |  |  |                         continue | 
					
						
							| 
									
										
										
										
											2000-10-23 17:22:08 +00:00
										 |  |  |                     if compare(pow(type(i),j,k), pow(type(i),j)% type(k)): | 
					
						
							|  |  |  |                         raise ValueError, "pow(" +str(i)+ "," +str(j)+ \ | 
					
						
							|  |  |  |                              "," +str(k)+ ") != pow(" +str(i)+ "," + \ | 
					
						
							|  |  |  |                              str(j)+ ") % " +str(k) | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | print 'Testing integer mode...' | 
					
						
							|  |  |  | powtest(int) | 
					
						
							|  |  |  | print 'Testing long integer mode...' | 
					
						
							|  |  |  | powtest(long) | 
					
						
							|  |  |  | print 'Testing floating point mode...' | 
					
						
							|  |  |  | powtest(float) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Other tests-- not very systematic | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | print 'The number in both columns should match.' | 
					
						
							| 
									
										
										
										
											1999-12-23 15:36:42 +00:00
										 |  |  | print `pow(3,3) % 8`, `pow(3,3,8)` | 
					
						
							|  |  |  | print `pow(3,3) % -8`, `pow(3,3,-8)` | 
					
						
							|  |  |  | print `pow(3,2) % -2`, `pow(3,2,-2)` | 
					
						
							|  |  |  | print `pow(-3,3) % 8`, `pow(-3,3,8)` | 
					
						
							|  |  |  | print `pow(-3,3) % -8`, `pow(-3,3,-8)` | 
					
						
							|  |  |  | print `pow(5,2) % -8`, `pow(5,2,-8)` | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  | print | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-12-23 15:36:42 +00:00
										 |  |  | print `pow(3L,3L) % 8`, `pow(3L,3L,8)` | 
					
						
							|  |  |  | print `pow(3L,3L) % -8`, `pow(3L,3L,-8)` | 
					
						
							|  |  |  | print `pow(3L,2) % -2`, `pow(3L,2,-2)` | 
					
						
							|  |  |  | print `pow(-3L,3L) % 8`, `pow(-3L,3L,8)` | 
					
						
							|  |  |  | print `pow(-3L,3L) % -8`, `pow(-3L,3L,-8)` | 
					
						
							|  |  |  | print `pow(5L,2) % -8`, `pow(5L,2,-8)` | 
					
						
							| 
									
										
										
										
											1994-08-12 13:14:22 +00:00
										 |  |  | print | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | print | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | for i in range(-10, 11): | 
					
						
							| 
									
										
										
										
											2000-10-23 17:22:08 +00:00
										 |  |  |     for j in range(0, 6): | 
					
						
							|  |  |  |         for k in range(-7, 11): | 
					
						
							|  |  |  |             if j >= 0 and k != 0: | 
					
						
							|  |  |  |                 o = pow(i,j) % k | 
					
						
							|  |  |  |                 n = pow(i,j,k) | 
					
						
							|  |  |  |                 if o != n: print 'Integer mismatch:', i,j,k | 
					
						
							| 
									
										
										
										
											2000-12-12 23:11:42 +00:00
										 |  |  |             if j >= 0 and k != 0: | 
					
						
							| 
									
										
										
										
											2000-10-23 17:22:08 +00:00
										 |  |  |                 o = pow(long(i),j) % k | 
					
						
							|  |  |  |                 n = pow(long(i),j,k) | 
					
						
							| 
									
										
										
										
											2001-09-03 08:44:02 +00:00
										 |  |  |                 if o != n: print 'Integer mismatch:', i,j,k |