home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / test / test_b1.py < prev    next >
Text File  |  1997-08-22  |  13KB  |  351 lines

  1. # Python test set -- part 4a, built-in functions a-m
  2.  
  3. from test_support import *
  4.  
  5. print '__import__'
  6. __import__('sys')
  7. __import__('strop')
  8. __import__('string')
  9. try: __import__('spamspam')
  10. except ImportError: pass
  11. else: raise TestFailed, "__import__('spamspam') should fail"
  12.  
  13. print 'abs'
  14. if abs(0) <> 0: raise TestFailed, 'abs(0)'
  15. if abs(1234) <> 1234: raise TestFailed, 'abs(1234)'
  16. if abs(-1234) <> 1234: raise TestFailed, 'abs(-1234)'
  17. #
  18. if abs(0.0) <> 0.0: raise TestFailed, 'abs(0.0)'
  19. if abs(3.14) <> 3.14: raise TestFailed, 'abs(3.14)'
  20. if abs(-3.14) <> 3.14: raise TestFailed, 'abs(-3.14)'
  21. #
  22. if abs(0L) <> 0L: raise TestFailed, 'abs(0L)'
  23. if abs(1234L) <> 1234L: raise TestFailed, 'abs(1234L)'
  24. if abs(-1234L) <> 1234L: raise TestFailed, 'abs(-1234L)'
  25.  
  26. print 'apply'
  27. def f0(*args):
  28.     if args != (): raise TestFailed, 'f0 called with ' + `args`
  29. def f1(a1):
  30.     if a1 != 1: raise TestFailed, 'f1 called with ' + `a1`
  31. def f2(a1, a2):
  32.     if a1 != 1 or a2 != 2:
  33.         raise TestFailed, 'f2 called with ' + `a1, a2`
  34. def f3(a1, a2, a3):
  35.     if a1 != 1 or a2 != 2 or a3 != 3:
  36.         raise TestFailed, 'f3 called with ' + `a1, a2, a3`
  37. apply(f0, ())
  38. apply(f1, (1,))
  39. apply(f2, (1, 2))
  40. apply(f3, (1, 2, 3))
  41.  
  42. print 'callable'
  43. if not callable(len):raise TestFailed, 'callable(len)'
  44. def f(): pass
  45. if not callable(f): raise TestFailed, 'callable(f)'
  46. class C:
  47.     def meth(self): pass
  48. if not callable(C): raise TestFailed, 'callable(C)'
  49. x = C()
  50. if not callable(x.meth): raise TestFailed, 'callable(x.meth)'
  51. if callable(x): raise TestFailed, 'callable(x)'
  52. class D(C):
  53.     def __call__(self): pass
  54. y = D()
  55. if not callable(y): raise TestFailed, 'callable(y)'
  56.  
  57. print 'chr'
  58. if chr(32) <> ' ': raise TestFailed, 'chr(32)'
  59. if chr(65) <> 'A': raise TestFailed, 'chr(65)'
  60. if chr(97) <> 'a': raise TestFailed, 'chr(97)'
  61.  
  62. print 'cmp'
  63. if cmp(-1, 1) <> -1: raise TestFailed, 'cmp(-1, 1)'
  64. if cmp(1, -1) <> 1: raise TestFailed, 'cmp(1, -1)'
  65. if cmp(1, 1) <> 0: raise TestFailed, 'cmp(1, 1)'
  66.  
  67. print 'coerce'
  68. if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)'
  69. if coerce(1, 1L) <> (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
  70. if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)'
  71.  
  72. print 'compile'
  73. compile('print 1\n', '', 'exec')
  74.  
  75. print 'complex'
  76. if complex(1,10) <> 1+10j: raise TestFailed, 'complex(1,10)'
  77. if complex(1,10L) <> 1+10j: raise TestFailed, 'complex(1,10L)'
  78. if complex(1,10.0) <> 1+10j: raise TestFailed, 'complex(1,10.0)'
  79. if complex(1L,10) <> 1+10j: raise TestFailed, 'complex(1L,10)'
  80. if complex(1L,10L) <> 1+10j: raise TestFailed, 'complex(1L,10L)'
  81. if complex(1L,10.0) <> 1+10j: raise TestFailed, 'complex(1L,10.0)'
  82. if complex(1.0,10) <> 1+10j: raise TestFailed, 'complex(1.0,10)'
  83. if complex(1.0,10L) <> 1+10j: raise TestFailed, 'complex(1.0,10L)'
  84. if complex(1.0,10.0) <> 1+10j: raise TestFailed, 'complex(1.0,10.0)'
  85. if complex(3.14+0j) <> 3.14+0j: raise TestFailed, 'complex(3.14)'
  86. if complex(3.14) <> 3.14+0j: raise TestFailed, 'complex(3.14)'
  87. if complex(314) <> 314.0+0j: raise TestFailed, 'complex(314)'
  88. if complex(314L) <> 314.0+0j: raise TestFailed, 'complex(314L)'
  89. if complex(3.14+0j, 0j) <> 3.14+0j: raise TestFailed, 'complex(3.14, 0j)'
  90. if complex(3.14, 0.0) <> 3.14+0j: raise TestFailed, 'complex(3.14, 0.0)'
  91. if complex(314, 0) <> 314.0+0j: raise TestFailed, 'complex(314, 0)'
  92. if complex(314L, 0L) <> 314.0+0j: raise TestFailed, 'complex(314L, 0L)'
  93. if complex(0j, 3.14j) <> -3.14+0j: raise TestFailed, 'complex(0j, 3.14j)'
  94. if complex(0.0, 3.14j) <> -3.14+0j: raise TestFailed, 'complex(0.0, 3.14j)'
  95. if complex(0j, 3.14) <> 3.14j: raise TestFailed, 'complex(0j, 3.14)'
  96. if complex(0.0, 3.14) <> 3.14j: raise TestFailed, 'complex(0.0, 3.14)'
  97. class Z:
  98.     def __complex__(self): return 3.14j
  99. z = Z()
  100. if complex(z) <> 3.14j: raise TestFailed, 'complex(classinstance)'
  101.  
  102. print 'delattr'
  103. import sys
  104. sys.spam = 1
  105. delattr(sys, 'spam')
  106.  
  107. print 'dir'
  108. x = 1
  109. if 'x' not in dir(): raise TestFailed, 'dir()'
  110. import sys
  111. if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
  112.  
  113. print 'divmod'
  114. if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)'
  115. if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)'
  116. if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)'
  117. if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)'
  118. #
  119. if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
  120. if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
  121. if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
  122. if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
  123. #
  124. if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)'
  125. if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
  126. if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
  127. if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
  128. #
  129. if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
  130.     raise TestFailed, 'divmod(3.25, 1.0)'
  131. if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
  132.     raise TestFailed, 'divmod(-3.25, 1.0)'
  133. if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
  134.     raise TestFailed, 'divmod(3.25, -1.0)'
  135. if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
  136.     raise TestFailed, 'divmod(-3.25, -1.0)'
  137.  
  138. print 'eval'
  139. if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
  140. if eval(' 1+1\n') <> 2: raise TestFailed, 'eval(\' 1+1\\n\')'
  141. globals = {'a': 1, 'b': 2}
  142. locals = {'b': 200, 'c': 300}
  143. if eval('a', globals) <> 1: raise TestFailed, "eval(1)"
  144. if eval('a', globals, locals) <> 1: raise TestFailed, "eval(2)"
  145. if eval('b', globals, locals) <> 200: raise TestFailed, "eval(3)"
  146. if eval('c', globals, locals) <> 300: raise TestFailed, "eval(4)"
  147.  
  148. print 'execfile'
  149. z = 0
  150. f = open(TESTFN, 'w')
  151. f.write('z = z+1\n')
  152. f.write('z = z*2\n')
  153. f.close()
  154. execfile(TESTFN)
  155. if z <> 2: raise TestFailed, "execfile(1)"
  156. globals['z'] = 0
  157. execfile(TESTFN, globals)
  158. if globals['z'] <> 2: raise TestFailed, "execfile(1)"
  159. locals['z'] = 0
  160. execfile(TESTFN, globals, locals)
  161. if locals['z'] <> 2: raise TestFailed, "execfile(1)"
  162. unlink(TESTFN)
  163.  
  164. print 'filter'
  165. if filter(lambda c: 'a' <= c <= 'z', 'Hello World') <> 'elloorld':
  166.     raise TestFailed, 'filter (filter a string)'
  167. if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) <> [1, 'hello', [3], 9]:
  168.     raise TestFailed, 'filter (remove false values)'
  169. if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) <> [1, 9, 2]:
  170.     raise TestFailed, 'filter (keep positives)'
  171. class Squares:
  172.     def __init__(self, max):
  173.         self.max = max
  174.         self.sofar = []
  175.     def __len__(self): return len(self.sofar)
  176.     def __getitem__(self, i):
  177.         if not 0 <= i < self.max: raise IndexError
  178.         n = len(self.sofar)
  179.         while n <= i:
  180.             self.sofar.append(n*n)
  181.             n = n+1
  182.         return self.sofar[i]
  183. if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
  184.     raise TestFailed, 'filter(None, Squares(10))'
  185. if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]:
  186.     raise TestFailed, 'filter(oddp, Squares(10))'
  187.  
  188. print 'float'
  189. if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
  190. if float(314) <> 314.0: raise TestFailed, 'float(314)'
  191. if float(314L) <> 314.0: raise TestFailed, 'float(314L)'
  192.  
  193. print 'getattr'
  194. import sys
  195. if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
  196.  
  197. print 'hasattr'
  198. import sys
  199. if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr'
  200.  
  201. print 'hash'
  202. hash(None)
  203. if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()'
  204. hash('spam')
  205. hash((0,1,2,3))
  206. def f(): pass
  207.  
  208. print 'hex'
  209. if hex(16) != '0x10': raise TestFailed, 'hex(16)'
  210. if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
  211. if len(hex(-1)) != len(hex(sys.maxint)): raise TestFailed, 'len(hex(-1))'
  212. if hex(-16) not in ('0xfffffff0', '0xfffffffffffffff0'):
  213.     raise TestFailed, 'hex(-16)'
  214. if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
  215.  
  216. print 'id'
  217. id(None)
  218. id(1)
  219. id(1L)
  220. id(1.0)
  221. id('spam')
  222. id((0,1,2,3))
  223. id([0,1,2,3])
  224. id({'spam': 1, 'eggs': 2, 'ham': 3})
  225.  
  226. # Test input() later, together with raw_input
  227.  
  228. print 'int'
  229. if int(314) <> 314: raise TestFailed, 'int(314)'
  230. if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
  231. if int(314L) <> 314: raise TestFailed, 'int(314L)'
  232. # Check that conversion from float truncates towards zero
  233. if int(-3.14) <> -3: raise TestFailed, 'int(-3.14)'
  234. if int(3.9) <> 3: raise TestFailed, 'int(3.9)'
  235. if int(-3.9) <> -3: raise TestFailed, 'int(-3.9)'
  236. if int(3.5) <> 3: raise TestFailed, 'int(3.5)'
  237. if int(-3.5) <> -3: raise TestFailed, 'int(-3.5)'
  238.  
  239. print 'isinstance'
  240. class C:
  241.     pass
  242. class D(C):
  243.     pass
  244. class E:
  245.     pass
  246. c = C()
  247. d = D()
  248. e = E()
  249. if not isinstance(c, C): raise TestFailed, 'isinstance(c, C)'
  250. if not isinstance(d, C): raise TestFailed, 'isinstance(d, C)'
  251. if isinstance(e, C): raise TestFailed, 'isinstance(e, C)'
  252. if isinstance(c, D): raise TestFailed, 'isinstance(c, D)'
  253. if isinstance('foo', E): raise TestFailed, 'isinstance("Foo", E)'
  254. try:
  255.     isinstance(E, 'foo')
  256.     raise TestFailed, 'isinstance(E, "foo")'
  257. except TypeError:
  258.     pass
  259.  
  260. print 'issubclass'
  261. if not issubclass(D, C): raise TestFailed, 'issubclass(D, C)'
  262. if not issubclass(C, C): raise TestFailed, 'issubclass(C, C)'
  263. if issubclass(C, D): raise TestFailed, 'issubclass(C, D)'
  264. try:
  265.     issubclass('foo', E)
  266.     raise TestFailed, 'issubclass("foo", E)'
  267. except TypeError:
  268.     pass
  269. try:
  270.     issubclass(E, 'foo')
  271.     raise TestFailed, 'issubclass(E, "foo")'
  272. except TypeError:
  273.     pass
  274.  
  275. print 'len'
  276. if len('123') <> 3: raise TestFailed, 'len(\'123\')'
  277. if len(()) <> 0: raise TestFailed, 'len(())'
  278. if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
  279. if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
  280. if len({}) <> 0: raise TestFailed, 'len({})'
  281. if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
  282.  
  283. print 'long'
  284. if long(314) <> 314L: raise TestFailed, 'long(314)'
  285. if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
  286. if long(314L) <> 314L: raise TestFailed, 'long(314L)'
  287. # Check that conversion from float truncates towards zero
  288. if long(-3.14) <> -3L: raise TestFailed, 'long(-3.14)'
  289. if long(3.9) <> 3L: raise TestFailed, 'long(3.9)'
  290. if long(-3.9) <> -3L: raise TestFailed, 'long(-3.9)'
  291. if long(3.5) <> 3L: raise TestFailed, 'long(3.5)'
  292. if long(-3.5) <> -3L: raise TestFailed, 'long(-3.5)'
  293.  
  294. print 'map'
  295. if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']:
  296.     raise TestFailed, 'map(None, \'hello world\')'
  297. if map(None, 'abcd', 'efg') <> \
  298.       [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
  299.     raise TestFailed, 'map(None, \'abcd\', \'efg\')'
  300. if map(None, range(10)) <> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
  301.     raise TestFailed, 'map(None, range(10))'
  302. if map(lambda x: x*x, range(1,4)) <> [1, 4, 9]:
  303.     raise TestFailed, 'map(lambda x: x*x, range(1,4))'
  304. try:
  305.     from math import sqrt
  306. except ImportError:
  307.     def sqrt(x):
  308.         return pow(x, 0.5)
  309. if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]:
  310.     raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
  311. if map(lambda x, y: x+y, [1,3,2], [9,1,4]) <> [10, 4, 6]:
  312.     raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
  313. def plus(*v):
  314.     accu = 0
  315.     for i in v: accu = accu + i
  316.     return accu
  317. if map(plus, [1, 3, 7]) <> [1, 3, 7]:
  318.     raise TestFailed, 'map(plus, [1, 3, 7])'
  319. if map(plus, [1, 3, 7], [4, 9, 2]) <> [1+4, 3+9, 7+2]:
  320.     raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
  321. if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) <> [1+4+1, 3+9+1, 7+2+0]:
  322.     raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
  323. if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
  324.     raise TestFailed, 'map(None, Squares(10))'
  325. if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
  326.     raise TestFailed, 'map(int, Squares(10))'
  327. if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
  328.     raise TestFailed, 'map(None: x, Squares(3), Squares(2))'
  329. if map(max, Squares(3), Squares(2)) != [0, 1, 4]:
  330.     raise TestFailed, 'map(None: x, Squares(3), Squares(2))'
  331.  
  332. print 'max'
  333. if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
  334. if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
  335. if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
  336. if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
  337. #
  338. if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
  339. if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
  340. if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'
  341.  
  342. print 'min'
  343. if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
  344. if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
  345. if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
  346. if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
  347. #
  348. if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
  349. if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
  350. if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'
  351.