home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / test / test_pow.py < prev    next >
Text File  |  1997-11-24  |  3KB  |  96 lines

  1. import sys
  2. import test_support
  3.  
  4. def powtest(type):
  5.     if (type!=float): 
  6.         print "    Testing 2-argument pow() function..."
  7.         for i in range(-1000, 1000):
  8.             if (pow(type(i),0)!=1): 
  9.                 raise ValueError, 'pow('+str(i)+',0) != 1'
  10.             if (pow(type(i),1)!=type(i)):
  11.                 raise ValueError, 'pow('+str(i)+',1) != '+str(i)
  12.             if (pow(type(0),1)!=type(0)):
  13.                 raise ValueError, 'pow(0,'+str(i)+') != 0'
  14.             if (pow(type(1),1)!=type(1)):
  15.                 raise ValueError, 'pow(1,'+str(i)+') != 1'
  16.         
  17.         for i in range(-100, 100):
  18.             if (pow(type(i),3)!=i*i*i):
  19.                 raise ValueError, 'pow('+str(i)+',3) != '+str(i*i*i)
  20.     
  21.         pow2=1
  22.         for i in range(0,31):
  23.             if (pow(2,i)!=pow2):
  24.                 raise ValueError, 'pow(2,'+str(i)+') != '+str(pow2)
  25.             if (i!=30): pow2=pow2*2
  26.  
  27.     print "    Testing 3-argument pow() function..."
  28.     il, ih = -20, 20
  29.     jl, jh = -5,   5
  30.     kl, kh = -10, 10
  31.     compare = cmp
  32.     if (type==float):
  33.         il=1
  34.     compare = test_support.fcmp
  35.     elif (type==int):
  36.         jl=0
  37.     elif (type==long):
  38.         jl,jh = 0, 15
  39.     for i in range(il, ih+1):
  40.          for j in range(jl,jh+1):
  41.              for k in range(kl, kh+1):
  42.                    if (k!=0):
  43.              if compare(pow(type(i),j,k), pow(type(i),j)% type(k)):
  44.                          raise ValueError, "pow(" +str(i)+ "," +str(j)+ \
  45.                   "," +str(k)+ ") != pow(" +str(i)+ "," + \
  46.                   str(j)+ ") % " +str(k)
  47.  
  48.  
  49. print 'Testing integer mode...'
  50. powtest(int)
  51. print 'Testing long integer mode...'
  52. powtest(long)
  53. print 'Testing floating point mode...'
  54. powtest(float)
  55.  
  56. # Other tests-- not very systematic
  57.  
  58. print 'The number in both columns should match.'
  59. print pow(3,3) % 8, pow(3,3,8)
  60. print pow(3,3) % -8, pow(3,3,-8)
  61. print pow(3,2) % -2, pow(3,2,-2)
  62. print pow(-3,3) % 8, pow(-3,3,8)
  63. print pow(-3,3) % -8, pow(-3,3,-8)
  64. print pow(5,2) % -8, pow(5,2,-8)
  65. print
  66.  
  67. print pow(3L,3L) % 8, pow(3L,3L,8)
  68. print pow(3L,3L) % -8, pow(3L,3L,-8)
  69. print pow(3L,2) % -2, pow(3L,2,-2)
  70. print pow(-3L,3L) % 8, pow(-3L,3L,8)
  71. print pow(-3L,3L) % -8, pow(-3L,3L,-8)
  72. print pow(5L,2) % -8, pow(5L,2,-8)
  73. print
  74.  
  75. print pow(3.0,3.0) % 8, pow(3.0,3.0,8)
  76. print pow(3.0,3.0) % -8, pow(3.0,3.0,-8)
  77. print pow(3.0,2) % -2, pow(3.0,2,-2)
  78. print pow(5.0,2) % -8, pow(5.0,2,-8)
  79. print
  80.  
  81. for i in range(-10, 11):
  82.  for j in range(0, 6):
  83.   for k in range(-7, 11):
  84.    if (j>=0 and k!=0):
  85.     o=pow(i,j) % k
  86.     n=pow(i,j,k)
  87.     if (o!=n): print 'Integer mismatch:', i,j,k
  88.    if (j>=0 and k<>0):
  89.     o=pow(long(i),j) % k
  90.     n=pow(long(i),j,k)
  91.     if (o!=n): print 'Long mismatch:', i,j,k
  92.    if (i>=0 and k<>0):
  93.      o=pow(float(i),j) % k
  94.      n=pow(float(i),j,k)
  95.      if (o!=n): print 'Float mismatch:', i,j,k
  96.