home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Lib / test / test_b2.py < prev    next >
Text File  |  1994-03-07  |  8KB  |  212 lines

  1. # Python test set -- part 4b, built-in functions n-z
  2.  
  3. from test_support import *
  4.  
  5. print 'oct'
  6. if oct(100) != '0144': raise TestFailed, 'oct(100)'
  7. if oct(100L) != '0144L': raise TestFailed, 'oct(100L)'
  8. if oct(-100) != '-0144': raise TestFailed, 'oct(-100)'
  9. if oct(-100L) != '-0144L': raise TestFailed, 'oct(-100L)'
  10.  
  11. print 'open'
  12. # NB the first 4 lines are also used to test input and raw_input, below
  13. fp = open(TESTFN, 'w')
  14. try:
  15.     fp.write('1+1\n')
  16.     fp.write('1+1\n')
  17.     fp.write('The quick brown fox jumps over the lazy dog')
  18.     fp.write('.\n')
  19.     fp.write('Dear John\n')
  20.     fp.write('XXX'*100)
  21.     fp.write('YYY'*100)
  22. finally:
  23.     fp.close()
  24. #
  25. fp = open(TESTFN, 'r')
  26. try:
  27.     if fp.readline(4) <> '1+1\n': raise TestFailed, 'readline(4) # exact'
  28.     if fp.readline(4) <> '1+1\n': raise TestFailed, 'readline(4) # exact'
  29.     if fp.readline() <> 'The quick brown fox jumps over the lazy dog.\n':
  30.         raise TestFailed, 'readline() # default'
  31.     if fp.readline(4) <> 'Dear': raise TestFailed, 'readline(4) # short'
  32.     if fp.readline(100) <> ' John\n': raise TestFailed, 'readline(100)'
  33.     if fp.read(300) <> 'XXX'*100: raise TestFailed, 'read(300)'
  34.     if fp.read(1000) <> 'YYY'*100: raise TestFailed, 'read(1000) # truncate'
  35. finally:
  36.     fp.close()
  37.  
  38. print 'ord'
  39. if ord(' ') <> 32: raise TestFailed, 'ord(\' \')'
  40. if ord('A') <> 65: raise TestFailed, 'ord(\'A\')'
  41. if ord('a') <> 97: raise TestFailed, 'ord(\'a\')'
  42.  
  43. print 'pow'
  44. if pow(0,0) <> 1: raise TestFailed, 'pow(0,0)'
  45. if pow(0,1) <> 0: raise TestFailed, 'pow(0,1)'
  46. if pow(1,0) <> 1: raise TestFailed, 'pow(1,0)'
  47. if pow(1,1) <> 1: raise TestFailed, 'pow(1,1)'
  48. #
  49. if pow(2,0) <> 1: raise TestFailed, 'pow(2,0)'
  50. if pow(2,10) <> 1024: raise TestFailed, 'pow(2,10)'
  51. if pow(2,20) <> 1024*1024: raise TestFailed, 'pow(2,20)'
  52. if pow(2,30) <> 1024*1024*1024: raise TestFailed, 'pow(2,30)'
  53. #
  54. if pow(-2,0) <> 1: raise TestFailed, 'pow(-2,0)'
  55. if pow(-2,1) <> -2: raise TestFailed, 'pow(-2,1)'
  56. if pow(-2,2) <> 4: raise TestFailed, 'pow(-2,2)'
  57. if pow(-2,3) <> -8: raise TestFailed, 'pow(-2,3)'
  58. #
  59. if pow(0L,0) <> 1: raise TestFailed, 'pow(0L,0)'
  60. if pow(0L,1) <> 0: raise TestFailed, 'pow(0L,1)'
  61. if pow(1L,0) <> 1: raise TestFailed, 'pow(1L,0)'
  62. if pow(1L,1) <> 1: raise TestFailed, 'pow(1L,1)'
  63. #
  64. if pow(2L,0) <> 1: raise TestFailed, 'pow(2L,0)'
  65. if pow(2L,10) <> 1024: raise TestFailed, 'pow(2L,10)'
  66. if pow(2L,20) <> 1024*1024: raise TestFailed, 'pow(2L,20)'
  67. if pow(2L,30) <> 1024*1024*1024: raise TestFailed, 'pow(2L,30)'
  68. #
  69. if pow(-2L,0) <> 1: raise TestFailed, 'pow(-2L,0)'
  70. if pow(-2L,1) <> -2: raise TestFailed, 'pow(-2L,1)'
  71. if pow(-2L,2) <> 4: raise TestFailed, 'pow(-2L,2)'
  72. if pow(-2L,3) <> -8: raise TestFailed, 'pow(-2L,3)'
  73. #
  74. if fcmp(pow(0.,0), 1.): raise TestFailed, 'pow(0.,0)'
  75. if fcmp(pow(0.,1), 0.): raise TestFailed, 'pow(0.,1)'
  76. if fcmp(pow(1.,0), 1.): raise TestFailed, 'pow(1.,0)'
  77. if fcmp(pow(1.,1), 1.): raise TestFailed, 'pow(1.,1)'
  78. #
  79. if fcmp(pow(2.,0), 1.): raise TestFailed, 'pow(2.,0)'
  80. if fcmp(pow(2.,10), 1024.): raise TestFailed, 'pow(2.,10)'
  81. if fcmp(pow(2.,20), 1024.*1024.): raise TestFailed, 'pow(2.,20)'
  82. if fcmp(pow(2.,30), 1024.*1024.*1024.): raise TestFailed, 'pow(2.,30)'
  83. #
  84. # XXX These don't work -- negative float to the float power...
  85. #if fcmp(pow(-2.,0), 1.): raise TestFailed, 'pow(-2.,0)'
  86. #if fcmp(pow(-2.,1), -2.): raise TestFailed, 'pow(-2.,1)'
  87. #if fcmp(pow(-2.,2), 4.): raise TestFailed, 'pow(-2.,2)'
  88. #if fcmp(pow(-2.,3), -8.): raise TestFailed, 'pow(-2.,3)'
  89.  
  90. print 'range'
  91. if range(3) <> [0, 1, 2]: raise TestFailed, 'range(3)'
  92. if range(1, 5) <> [1, 2, 3, 4]: raise TestFailed, 'range(1, 5)'
  93. if range(0) <> []: raise TestFailed, 'range(0)'
  94. if range(-3) <> []: raise TestFailed, 'range(-3)'
  95. if range(1, 10, 3) <> [1, 4, 7]: raise TestFailed, 'range(1, 10, 3)'
  96. if range(5, -5, -3) <> [5, 2, -1, -4]: raise TestFailed, 'range(5, -5, -3)'
  97.  
  98. print 'input and raw_input'
  99. import sys
  100. fp = open(TESTFN, 'r')
  101. savestdin = sys.stdin
  102. try:
  103.     sys.stdin = fp
  104.     if input() <> 2: raise TestFailed, 'input()'
  105.     if input('testing\n') <> 2: raise TestFailed, 'input()'
  106.     if raw_input() <> 'The quick brown fox jumps over the lazy dog.':
  107.         raise TestFailed, 'raw_input()'
  108.     if raw_input('testing\n') <> 'Dear John':
  109.         raise TestFailed, 'raw_input(\'testing\\n\')'
  110. finally:
  111.     sys.stdin = savestdin
  112.     fp.close()
  113.  
  114. print 'reduce'
  115. if reduce(lambda x, y: x+y, ['a', 'b', 'c'], '') <> 'abc':
  116.     raise TestFailed, 'reduce(): implode a string'
  117. if reduce(lambda x, y: x+y,
  118.       [['a', 'c'], [], ['d', 'w']], []) <> ['a','c','d','w']:
  119.     raise TestFailed, 'reduce(): append'
  120. if reduce(lambda x, y: x*y, range(2,8), 1) <> 5040:
  121.     raise TestFailed, 'reduce(): compute 7!'
  122. if reduce(lambda x, y: x*y, range(2,21), 1L) <> 2432902008176640000L:
  123.     raise TestFailed, 'reduce(): compute 20!, use long'
  124. class Squares:
  125.     def __init__(self, max):
  126.         self.max = max
  127.         self.sofar = []
  128.     def __len__(self): return len(self.sofar)
  129.     def __getitem__(self, i):
  130.         if not 0 <= i < self.max: raise IndexError
  131.         n = len(self.sofar)
  132.         while n <= i:
  133.             self.sofar.append(n*n)
  134.             n = n+1
  135.         return self.sofar[i]
  136. if reduce(lambda x, y: x+y, Squares(10)) != 285:
  137.     raise TestFailed, 'reduce(<+>, Squares(10))'
  138. if reduce(lambda x, y: x+y, Squares(10), 0) != 285:
  139.     raise TestFailed, 'reduce(<+>, Squares(10), 0)'
  140. if reduce(lambda x, y: x+y, Squares(0), 0) != 0:
  141.     raise TestFailed, 'reduce(<+>, Squares(0), 0)'
  142.  
  143.  
  144. print 'reload'
  145. import string
  146. reload(string)
  147.  
  148. print 'repr'
  149. if repr('') <> '\'\'': raise TestFailed, 'repr(\'\')'
  150. if repr(0) <> '0': raise TestFailed, 'repr(0)'
  151. if repr(0L) <> '0L': raise TestFailed, 'repr(0L)'
  152. if repr(()) <> '()': raise TestFailed, 'repr(())'
  153. if repr([]) <> '[]': raise TestFailed, 'repr([])'
  154. if repr({}) <> '{}': raise TestFailed, 'repr({})'
  155.  
  156. print 'round'
  157. if round(0.0) <> 0.0: raise TestFailed, 'round(0.0)'
  158. if round(1.0) <> 1.0: raise TestFailed, 'round(1.0)'
  159. if round(10.0) <> 10.0: raise TestFailed, 'round(10.0)'
  160. if round(1000000000.0) <> 1000000000.0:
  161.     raise TestFailed, 'round(1000000000.0)'
  162. if round(1e20) <> 1e20: raise TestFailed, 'round(1e20)'
  163.  
  164. if round(-1.0) <> -1.0: raise TestFailed, 'round(-1.0)'
  165. if round(-10.0) <> -10.0: raise TestFailed, 'round(-10.0)'
  166. if round(-1000000000.0) <> -1000000000.0:
  167.     raise TestFailed, 'round(-1000000000.0)'
  168. if round(-1e20) <> -1e20: raise TestFailed, 'round(-1e20)'
  169.  
  170. if round(0.1) <> 0.0: raise TestFailed, 'round(0.0)'
  171. if round(1.1) <> 1.0: raise TestFailed, 'round(1.0)'
  172. if round(10.1) <> 10.0: raise TestFailed, 'round(10.0)'
  173. if round(1000000000.1) <> 1000000000.0:
  174.     raise TestFailed, 'round(1000000000.0)'
  175.  
  176. if round(-1.1) <> -1.0: raise TestFailed, 'round(-1.0)'
  177. if round(-10.1) <> -10.0: raise TestFailed, 'round(-10.0)'
  178. if round(-1000000000.1) <> -1000000000.0:
  179.     raise TestFailed, 'round(-1000000000.0)'
  180.  
  181. if round(0.9) <> 1.0: raise TestFailed, 'round(0.9)'
  182. if round(9.9) <> 10.0: raise TestFailed, 'round(9.9)'
  183. if round(999999999.9) <> 1000000000.0:
  184.     raise TestFailed, 'round(999999999.9)'
  185.  
  186. if round(-0.9) <> -1.0: raise TestFailed, 'round(-0.9)'
  187. if round(-9.9) <> -10.0: raise TestFailed, 'round(-9.9)'
  188. if round(-999999999.9) <> -1000000000.0:
  189.     raise TestFailed, 'round(-999999999.9)'
  190.  
  191. print 'setattr'
  192. import sys
  193. setattr(sys, 'foobar', 1)
  194. if sys.foobar != 1: raise TestFailed, 'setattr(sys, \'foobar\', 1)'
  195.  
  196. print 'str'
  197. if str('') <> '': raise TestFailed, 'str(\'\')'
  198. if str(0) <> '0': raise TestFailed, 'str(0)'
  199. if str(0L) <> '0L': raise TestFailed, 'str(0L)'
  200. if str(()) <> '()': raise TestFailed, 'str(())'
  201. if str([]) <> '[]': raise TestFailed, 'str([])'
  202. if str({}) <> '{}': raise TestFailed, 'str({})'
  203.  
  204. print 'type'
  205. if type('') <> type('123') or type('') == type(()):
  206.     raise TestFailed, 'type()'
  207.  
  208.  
  209. # Epilogue -- unlink the temp file
  210.  
  211. unlink(TESTFN)
  212.