home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_b1.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  20.9 KB  |  629 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__('time')
  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. try: abs('a')
  27. except TypeError: pass
  28. else: raise TestFailed, 'abs("a")'
  29.  
  30. print 'apply'
  31. def f0(*args):
  32.     if args != (): raise TestFailed, 'f0 called with ' + `args`
  33. def f1(a1):
  34.     if a1 != 1: raise TestFailed, 'f1 called with ' + `a1`
  35. def f2(a1, a2):
  36.     if a1 != 1 or a2 != 2:
  37.         raise TestFailed, 'f2 called with ' + `a1, a2`
  38. def f3(a1, a2, a3):
  39.     if a1 != 1 or a2 != 2 or a3 != 3:
  40.         raise TestFailed, 'f3 called with ' + `a1, a2, a3`
  41. apply(f0, ())
  42. apply(f1, (1,))
  43. apply(f2, (1, 2))
  44. apply(f3, (1, 2, 3))
  45.  
  46. # A PyCFunction that takes only positional parameters should allow an
  47. # empty keyword dictionary to pass without a complaint, but raise a
  48. # TypeError if the dictionary is non-empty.
  49. apply(id, (1,), {})
  50. try:
  51.     apply(id, (1,), {"foo": 1})
  52. except TypeError:
  53.     pass
  54. else:
  55.     raise TestFailed, 'expected TypeError; no exception raised'
  56.  
  57. print 'callable'
  58. if not callable(len):raise TestFailed, 'callable(len)'
  59. def f(): pass
  60. if not callable(f): raise TestFailed, 'callable(f)'
  61. class C:
  62.     def meth(self): pass
  63. if not callable(C): raise TestFailed, 'callable(C)'
  64. x = C()
  65. if not callable(x.meth): raise TestFailed, 'callable(x.meth)'
  66. if callable(x): raise TestFailed, 'callable(x)'
  67. class D(C):
  68.     def __call__(self): pass
  69. y = D()
  70. if not callable(y): raise TestFailed, 'callable(y)'
  71. y()
  72.  
  73. print 'chr'
  74. if chr(32) != ' ': raise TestFailed, 'chr(32)'
  75. if chr(65) != 'A': raise TestFailed, 'chr(65)'
  76. if chr(97) != 'a': raise TestFailed, 'chr(97)'
  77.  
  78. # cmp
  79. print 'cmp'
  80. if cmp(-1, 1) != -1: raise TestFailed, 'cmp(-1, 1)'
  81. if cmp(1, -1) != 1: raise TestFailed, 'cmp(1, -1)'
  82. if cmp(1, 1) != 0: raise TestFailed, 'cmp(1, 1)'
  83. # verify that circular objects are handled
  84. a = []; a.append(a)
  85. b = []; b.append(b)
  86. from UserList import UserList
  87. c = UserList(); c.append(c)
  88. if cmp(a, b) != 0: raise TestFailed, "cmp(%s, %s)" % (a, b)
  89. if cmp(b, c) != 0: raise TestFailed, "cmp(%s, %s)" % (b, c)
  90. if cmp(c, a) != 0: raise TestFailed, "cmp(%s, %s)" % (c, a)
  91. if cmp(a, c) != 0: raise TestFailed, "cmp(%s, %s)" % (a, c)
  92. # okay, now break the cycles
  93. a.pop(); b.pop(); c.pop()
  94.  
  95. print 'coerce'
  96. if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)'
  97. if coerce(1, 1L) != (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
  98. if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)'
  99.  
  100. print 'compile'
  101. compile('print 1\n', '', 'exec')
  102.  
  103. print 'complex'
  104. if complex(1,10) != 1+10j: raise TestFailed, 'complex(1,10)'
  105. if complex(1,10L) != 1+10j: raise TestFailed, 'complex(1,10L)'
  106. if complex(1,10.0) != 1+10j: raise TestFailed, 'complex(1,10.0)'
  107. if complex(1L,10) != 1+10j: raise TestFailed, 'complex(1L,10)'
  108. if complex(1L,10L) != 1+10j: raise TestFailed, 'complex(1L,10L)'
  109. if complex(1L,10.0) != 1+10j: raise TestFailed, 'complex(1L,10.0)'
  110. if complex(1.0,10) != 1+10j: raise TestFailed, 'complex(1.0,10)'
  111. if complex(1.0,10L) != 1+10j: raise TestFailed, 'complex(1.0,10L)'
  112. if complex(1.0,10.0) != 1+10j: raise TestFailed, 'complex(1.0,10.0)'
  113. if complex(3.14+0j) != 3.14+0j: raise TestFailed, 'complex(3.14)'
  114. if complex(3.14) != 3.14+0j: raise TestFailed, 'complex(3.14)'
  115. if complex(314) != 314.0+0j: raise TestFailed, 'complex(314)'
  116. if complex(314L) != 314.0+0j: raise TestFailed, 'complex(314L)'
  117. if complex(3.14+0j, 0j) != 3.14+0j: raise TestFailed, 'complex(3.14, 0j)'
  118. if complex(3.14, 0.0) != 3.14+0j: raise TestFailed, 'complex(3.14, 0.0)'
  119. if complex(314, 0) != 314.0+0j: raise TestFailed, 'complex(314, 0)'
  120. if complex(314L, 0L) != 314.0+0j: raise TestFailed, 'complex(314L, 0L)'
  121. if complex(0j, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0j, 3.14j)'
  122. if complex(0.0, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0.0, 3.14j)'
  123. if complex(0j, 3.14) != 3.14j: raise TestFailed, 'complex(0j, 3.14)'
  124. if complex(0.0, 3.14) != 3.14j: raise TestFailed, 'complex(0.0, 3.14)'
  125. if complex("1") != 1+0j: raise TestFailed, 'complex("1")'
  126. if complex("1j") != 1j: raise TestFailed, 'complex("1j")'
  127.  
  128. try: complex("1", "1")
  129. except TypeError: pass
  130. else: raise TestFailed, 'complex("1", "1")'
  131.  
  132. try: complex(1, "1")
  133. except TypeError: pass
  134. else: raise TestFailed, 'complex(1, "1")'
  135.  
  136. if complex("  3.14+J  ") != 3.14+1j:  raise TestFailed, 'complex("  3.14+J  )"'
  137. if have_unicode:
  138.     if complex(unicode("  3.14+J  ")) != 3.14+1j:
  139.         raise TestFailed, 'complex(u"  3.14+J  )"'
  140.  
  141. # SF bug 543840:  complex(string) accepts strings with \0
  142. # Fixed in 2.3.
  143. try:
  144.     complex('1+1j\0j')
  145. except ValueError:
  146.     pass
  147. else:
  148.     raise TestFailed("complex('1+1j\0j') should have raised ValueError")
  149.  
  150. class Z:
  151.     def __complex__(self): return 3.14j
  152. z = Z()
  153. if complex(z) != 3.14j: raise TestFailed, 'complex(classinstance)'
  154.  
  155. print 'delattr'
  156. import sys
  157. sys.spam = 1
  158. delattr(sys, 'spam')
  159.  
  160. print 'dir'
  161. x = 1
  162. if 'x' not in dir(): raise TestFailed, 'dir()'
  163. import sys
  164. if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
  165.  
  166. print 'divmod'
  167. if divmod(12, 7) != (1, 5): raise TestFailed, 'divmod(12, 7)'
  168. if divmod(-12, 7) != (-2, 2): raise TestFailed, 'divmod(-12, 7)'
  169. if divmod(12, -7) != (-2, -2): raise TestFailed, 'divmod(12, -7)'
  170. if divmod(-12, -7) != (1, -5): raise TestFailed, 'divmod(-12, -7)'
  171. #
  172. if divmod(12L, 7L) != (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
  173. if divmod(-12L, 7L) != (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
  174. if divmod(12L, -7L) != (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
  175. if divmod(-12L, -7L) != (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
  176. #
  177. if divmod(12, 7L) != (1, 5L): raise TestFailed, 'divmod(12, 7L)'
  178. if divmod(-12, 7L) != (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
  179. if divmod(12L, -7) != (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
  180. if divmod(-12L, -7) != (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
  181. #
  182. if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
  183.     raise TestFailed, 'divmod(3.25, 1.0)'
  184. if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
  185.     raise TestFailed, 'divmod(-3.25, 1.0)'
  186. if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
  187.     raise TestFailed, 'divmod(3.25, -1.0)'
  188. if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
  189.     raise TestFailed, 'divmod(-3.25, -1.0)'
  190.  
  191. print 'eval'
  192. if eval('1+1') != 2: raise TestFailed, 'eval(\'1+1\')'
  193. if eval(' 1+1\n') != 2: raise TestFailed, 'eval(\' 1+1\\n\')'
  194. globals = {'a': 1, 'b': 2}
  195. locals = {'b': 200, 'c': 300}
  196. if eval('a', globals) != 1:
  197.     raise TestFailed, "eval(1) == %s" % eval('a', globals)
  198. if eval('a', globals, locals) != 1:
  199.     raise TestFailed, "eval(2)"
  200. if eval('b', globals, locals) != 200:
  201.     raise TestFailed, "eval(3)"
  202. if eval('c', globals, locals) != 300:
  203.     raise TestFailed, "eval(4)"
  204. if have_unicode:
  205.     if eval(unicode('1+1')) != 2: raise TestFailed, 'eval(u\'1+1\')'
  206.     if eval(unicode(' 1+1\n')) != 2: raise TestFailed, 'eval(u\' 1+1\\n\')'
  207. globals = {'a': 1, 'b': 2}
  208. locals = {'b': 200, 'c': 300}
  209. if have_unicode:
  210.     if eval(unicode('a'), globals) != 1:
  211.         raise TestFailed, "eval(1) == %s" % eval(unicode('a'), globals)
  212.     if eval(unicode('a'), globals, locals) != 1:
  213.         raise TestFailed, "eval(2)"
  214.     if eval(unicode('b'), globals, locals) != 200:
  215.         raise TestFailed, "eval(3)"
  216.     if eval(unicode('c'), globals, locals) != 300:
  217.         raise TestFailed, "eval(4)"
  218.  
  219. print 'execfile'
  220. z = 0
  221. f = open(TESTFN, 'w')
  222. f.write('z = z+1\n')
  223. f.write('z = z*2\n')
  224. f.close()
  225. execfile(TESTFN)
  226. if z != 2: raise TestFailed, "execfile(1)"
  227. globals['z'] = 0
  228. execfile(TESTFN, globals)
  229. if globals['z'] != 2: raise TestFailed, "execfile(1)"
  230. locals['z'] = 0
  231. execfile(TESTFN, globals, locals)
  232. if locals['z'] != 2: raise TestFailed, "execfile(1)"
  233. unlink(TESTFN)
  234.  
  235. print 'filter'
  236. if filter(lambda c: 'a' <= c <= 'z', 'Hello World') != 'elloorld':
  237.     raise TestFailed, 'filter (filter a string)'
  238. if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) != [1, 'hello', [3], 9]:
  239.     raise TestFailed, 'filter (remove false values)'
  240. if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) != [1, 9, 2]:
  241.     raise TestFailed, 'filter (keep positives)'
  242. class Squares:
  243.     def __init__(self, max):
  244.         self.max = max
  245.         self.sofar = []
  246.     def __len__(self): return len(self.sofar)
  247.     def __getitem__(self, i):
  248.         if not 0 <= i < self.max: raise IndexError
  249.         n = len(self.sofar)
  250.         while n <= i:
  251.             self.sofar.append(n*n)
  252.             n = n+1
  253.         return self.sofar[i]
  254. if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
  255.     raise TestFailed, 'filter(None, Squares(10))'
  256. if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]:
  257.     raise TestFailed, 'filter(oddp, Squares(10))'
  258. class StrSquares:
  259.     def __init__(self, max):
  260.         self.max = max
  261.         self.sofar = []
  262.     def __len__(self):
  263.         return len(self.sofar)
  264.     def __getitem__(self, i):
  265.         if not 0 <= i < self.max:
  266.             raise IndexError
  267.         n = len(self.sofar)
  268.         while n <= i:
  269.             self.sofar.append(str(n*n))
  270.             n = n+1
  271.         return self.sofar[i]
  272. def identity(item):
  273.     return 1
  274. filter(identity, Squares(5))
  275.  
  276. print 'float'
  277. if float(3.14) != 3.14: raise TestFailed, 'float(3.14)'
  278. if float(314) != 314.0: raise TestFailed, 'float(314)'
  279. if float(314L) != 314.0: raise TestFailed, 'float(314L)'
  280. if float("  3.14  ") != 3.14:  raise TestFailed, 'float("  3.14  ")'
  281. if have_unicode:
  282.     if float(unicode("  3.14  ")) != 3.14:
  283.         raise TestFailed, 'float(u"  3.14  ")'
  284.     if float(unicode("  \u0663.\u0661\u0664  ",'raw-unicode-escape')) != 3.14:
  285.         raise TestFailed, 'float(u"  \u0663.\u0661\u0664  ")'
  286.  
  287. print 'getattr'
  288. import sys
  289. if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
  290. try:
  291.     getattr(sys, 1)
  292. except TypeError:
  293.     pass
  294. else:
  295.     raise TestFailed, "getattr(sys, 1) should raise an exception"
  296. try:
  297.     getattr(sys, 1, "foo")
  298. except TypeError:
  299.     pass
  300. else:
  301.     raise TestFailed, 'getattr(sys, 1, "foo") should raise an exception'
  302.  
  303. print 'hasattr'
  304. import sys
  305. if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr'
  306. try:
  307.     hasattr(sys, 1)
  308. except TypeError:
  309.     pass
  310. else:
  311.     raise TestFailed, "hasattr(sys, 1) should raise an exception"
  312.  
  313. print 'hash'
  314. hash(None)
  315. if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()'
  316. hash('spam')
  317. hash((0,1,2,3))
  318. def f(): pass
  319. try: hash([])
  320. except TypeError: pass
  321. else: raise TestFailed, "hash([]) should raise an exception"
  322. try: hash({})
  323. except TypeError: pass
  324. else: raise TestFailed, "hash({}) should raise an exception"
  325.  
  326. print 'hex'
  327. if hex(16) != '0x10': raise TestFailed, 'hex(16)'
  328. if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
  329. if len(hex(-1)) != len(hex(sys.maxint)): raise TestFailed, 'len(hex(-1))'
  330. if hex(-16) not in ('0xfffffff0', '0xfffffffffffffff0'):
  331.     raise TestFailed, 'hex(-16)'
  332. if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
  333.  
  334. print 'id'
  335. id(None)
  336. id(1)
  337. id(1L)
  338. id(1.0)
  339. id('spam')
  340. id((0,1,2,3))
  341. id([0,1,2,3])
  342. id({'spam': 1, 'eggs': 2, 'ham': 3})
  343.  
  344. # Test input() later, together with raw_input
  345.  
  346. print 'int'
  347. if int(314) != 314: raise TestFailed, 'int(314)'
  348. if int(3.14) != 3: raise TestFailed, 'int(3.14)'
  349. if int(314L) != 314: raise TestFailed, 'int(314L)'
  350. # Check that conversion from float truncates towards zero
  351. if int(-3.14) != -3: raise TestFailed, 'int(-3.14)'
  352. if int(3.9) != 3: raise TestFailed, 'int(3.9)'
  353. if int(-3.9) != -3: raise TestFailed, 'int(-3.9)'
  354. if int(3.5) != 3: raise TestFailed, 'int(3.5)'
  355. if int(-3.5) != -3: raise TestFailed, 'int(-3.5)'
  356. # Different base:
  357. if int("10",16) != 16L: raise TestFailed, 'int("10",16)'
  358. if have_unicode:
  359.     if int(unicode("10"),16) != 16L:
  360.         raise TestFailed, 'int(u"10",16)'
  361. # Test conversion from strings and various anomalies
  362. L = [
  363.         ('0', 0),
  364.         ('1', 1),
  365.         ('9', 9),
  366.         ('10', 10),
  367.         ('99', 99),
  368.         ('100', 100),
  369.         ('314', 314),
  370.         (' 314', 314),
  371.         ('314 ', 314),
  372.         ('  \t\t  314  \t\t  ', 314),
  373.         (`sys.maxint`, sys.maxint),
  374.         ('  1x', ValueError),
  375.         ('  1  ', 1),
  376.         ('  1\02  ', ValueError),
  377.         ('', ValueError),
  378.         (' ', ValueError),
  379.         ('  \t\t  ', ValueError)
  380. ]
  381. if have_unicode:
  382.     L += [
  383.         (unicode('0'), 0),
  384.         (unicode('1'), 1),
  385.         (unicode('9'), 9),
  386.         (unicode('10'), 10),
  387.         (unicode('99'), 99),
  388.         (unicode('100'), 100),
  389.         (unicode('314'), 314),
  390.         (unicode(' 314'), 314),
  391.         (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
  392.         (unicode('  \t\t  314  \t\t  '), 314),
  393.         (unicode('  1x'), ValueError),
  394.         (unicode('  1  '), 1),
  395.         (unicode('  1\02  '), ValueError),
  396.         (unicode(''), ValueError),
  397.         (unicode(' '), ValueError),
  398.         (unicode('  \t\t  '), ValueError),
  399. ]
  400. for s, v in L:
  401.     for sign in "", "+", "-":
  402.         for prefix in "", " ", "\t", "  \t\t  ":
  403.             ss = prefix + sign + s
  404.             vv = v
  405.             if sign == "-" and v is not ValueError:
  406.                 vv = -v
  407.             try:
  408.                 if int(ss) != vv:
  409.                     raise TestFailed, "int(%s)" % `ss`
  410.             except v:
  411.                 pass
  412.             except ValueError, e:
  413.                 raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e)
  414. s = `-1-sys.maxint`
  415. if int(s)+1 != -sys.maxint:
  416.     raise TestFailed, "int(%s)" % `s`
  417. try:
  418.     int(s[1:])
  419. except ValueError:
  420.     pass
  421. else:
  422.     raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError"
  423. try:
  424.     int(1e100)
  425. except OverflowError:
  426.     pass
  427. else:
  428.     raise TestFailed("int(1e100) expected OverflowError")
  429. try:
  430.     int(-1e100)
  431. except OverflowError:
  432.     pass
  433. else:
  434.     raise TestFailed("int(-1e100) expected OverflowError")
  435.  
  436.  
  437. # SF bug 434186:  0x80000000/2 != 0x80000000>>1.
  438. # Worked by accident in Windows release build, but failed in debug build.
  439. # Failed in all Linux builds.
  440. x = -1-sys.maxint
  441. if x >> 1 != x//2:
  442.     raise TestFailed("x >> 1 != x/2 when x == -1-sys.maxint")
  443.  
  444. try: int('123\0')
  445. except ValueError: pass
  446. else: raise TestFailed("int('123\0') didn't raise exception")
  447.  
  448. print 'isinstance'
  449. class C:
  450.     pass
  451. class D(C):
  452.     pass
  453. class E:
  454.     pass
  455. c = C()
  456. d = D()
  457. e = E()
  458. if not isinstance(c, C): raise TestFailed, 'isinstance(c, C)'
  459. if not isinstance(d, C): raise TestFailed, 'isinstance(d, C)'
  460. if isinstance(e, C): raise TestFailed, 'isinstance(e, C)'
  461. if isinstance(c, D): raise TestFailed, 'isinstance(c, D)'
  462. if isinstance('foo', E): raise TestFailed, 'isinstance("Foo", E)'
  463. try:
  464.     isinstance(E, 'foo')
  465.     raise TestFailed, 'isinstance(E, "foo")'
  466. except TypeError:
  467.     pass
  468.  
  469. print 'issubclass'
  470. if not issubclass(D, C): raise TestFailed, 'issubclass(D, C)'
  471. if not issubclass(C, C): raise TestFailed, 'issubclass(C, C)'
  472. if issubclass(C, D): raise TestFailed, 'issubclass(C, D)'
  473. try:
  474.     issubclass('foo', E)
  475.     raise TestFailed, 'issubclass("foo", E)'
  476. except TypeError:
  477.     pass
  478. try:
  479.     issubclass(E, 'foo')
  480.     raise TestFailed, 'issubclass(E, "foo")'
  481. except TypeError:
  482.     pass
  483.  
  484. print 'len'
  485. if len('123') != 3: raise TestFailed, 'len(\'123\')'
  486. if len(()) != 0: raise TestFailed, 'len(())'
  487. if len((1, 2, 3, 4)) != 4: raise TestFailed, 'len((1, 2, 3, 4))'
  488. if len([1, 2, 3, 4]) != 4: raise TestFailed, 'len([1, 2, 3, 4])'
  489. if len({}) != 0: raise TestFailed, 'len({})'
  490. if len({'a':1, 'b': 2}) != 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
  491.  
  492. print 'list'
  493. if list([]) != []: raise TestFailed, 'list([])'
  494. l0_3 = [0, 1, 2, 3]
  495. l0_3_bis = list(l0_3)
  496. if l0_3 != l0_3_bis or l0_3 is l0_3_bis: raise TestFailed, 'list([0, 1, 2, 3])'
  497. if list(()) != []: raise TestFailed, 'list(())'
  498. if list((0, 1, 2, 3)) != [0, 1, 2, 3]: raise TestFailed, 'list((0, 1, 2, 3))'
  499. if list('') != []: raise TestFailed, 'list('')'
  500. if list('spam') != ['s', 'p', 'a', 'm']: raise TestFailed, "list('spam')"
  501.  
  502. if sys.maxint == 0x7fffffff:
  503.     # This test can currently only work on 32-bit machines.
  504.     # XXX If/when PySequence_Length() returns a ssize_t, it should be
  505.     # XXX re-enabled.
  506.     try:
  507.         # Verify clearing of bug #556025.
  508.         # This assumes that the max data size (sys.maxint) == max
  509.         # address size this also assumes that the address size is at
  510.         # least 4 bytes with 8 byte addresses, the bug is not well
  511.         # tested
  512.         #
  513.         # Note: This test is expected to SEGV under Cygwin 1.3.12 or
  514.         # earlier due to a newlib bug.  See the following mailing list
  515.         # thread for the details:
  516.  
  517.         #     http://sources.redhat.com/ml/newlib/2002/msg00369.html
  518.         list(xrange(sys.maxint // 2))
  519.     except MemoryError:
  520.         pass
  521.     else:
  522.         raise TestFailed, 'list(xrange(sys.maxint / 4))'
  523.  
  524. print 'long'
  525. if long(314) != 314L: raise TestFailed, 'long(314)'
  526. if long(3.14) != 3L: raise TestFailed, 'long(3.14)'
  527. if long(314L) != 314L: raise TestFailed, 'long(314L)'
  528. # Check that conversion from float truncates towards zero
  529. if long(-3.14) != -3L: raise TestFailed, 'long(-3.14)'
  530. if long(3.9) != 3L: raise TestFailed, 'long(3.9)'
  531. if long(-3.9) != -3L: raise TestFailed, 'long(-3.9)'
  532. if long(3.5) != 3L: raise TestFailed, 'long(3.5)'
  533. if long(-3.5) != -3L: raise TestFailed, 'long(-3.5)'
  534. if long("-3") != -3L: raise TestFailed, 'long("-3")'
  535. if have_unicode:
  536.     if long(unicode("-3")) != -3L:
  537.         raise TestFailed, 'long(u"-3")'
  538. # Different base:
  539. if long("10",16) != 16L: raise TestFailed, 'long("10",16)'
  540. if have_unicode:
  541.     if long(unicode("10"),16) != 16L:
  542.         raise TestFailed, 'long(u"10",16)'
  543. # Check conversions from string (same test set as for int(), and then some)
  544. LL = [
  545.         ('1' + '0'*20, 10L**20),
  546.         ('1' + '0'*100, 10L**100)
  547. ]
  548. if have_unicode:
  549.     L+=[
  550.         (unicode('1') + unicode('0')*20, 10L**20),
  551.         (unicode('1') + unicode('0')*100, 10L**100),
  552. ]
  553. for s, v in L + LL:
  554.     for sign in "", "+", "-":
  555.         for prefix in "", " ", "\t", "  \t\t  ":
  556.             ss = prefix + sign + s
  557.             vv = v
  558.             if sign == "-" and v is not ValueError:
  559.                 vv = -v
  560.             try:
  561.                 if long(ss) != long(vv):
  562.                     raise TestFailed, "long(%s)" % `ss`
  563.             except v:
  564.                 pass
  565.             except ValueError, e:
  566.                 raise TestFailed, "long(%s) raised ValueError: %s" % (`ss`, e)
  567.  
  568. try: long('123\0')
  569. except ValueError: pass
  570. else: raise TestFailed("long('123\0') didn't raise exception")
  571.  
  572. print 'map'
  573. if map(None, 'hello world') != ['h','e','l','l','o',' ','w','o','r','l','d']:
  574.     raise TestFailed, 'map(None, \'hello world\')'
  575. if map(None, 'abcd', 'efg') != \
  576.    [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
  577.     raise TestFailed, 'map(None, \'abcd\', \'efg\')'
  578. if map(None, range(10)) != [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
  579.     raise TestFailed, 'map(None, range(10))'
  580. if map(lambda x: x*x, range(1,4)) != [1, 4, 9]:
  581.     raise TestFailed, 'map(lambda x: x*x, range(1,4))'
  582. try:
  583.     from math import sqrt
  584. except ImportError:
  585.     def sqrt(x):
  586.         return pow(x, 0.5)
  587. if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) != [[4.0, 2.0], [9.0, 3.0]]:
  588.     raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
  589. if map(lambda x, y: x+y, [1,3,2], [9,1,4]) != [10, 4, 6]:
  590.     raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
  591. def plus(*v):
  592.     accu = 0
  593.     for i in v: accu = accu + i
  594.     return accu
  595. if map(plus, [1, 3, 7]) != [1, 3, 7]:
  596.     raise TestFailed, 'map(plus, [1, 3, 7])'
  597. if map(plus, [1, 3, 7], [4, 9, 2]) != [1+4, 3+9, 7+2]:
  598.     raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
  599. if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) != [1+4+1, 3+9+1, 7+2+0]:
  600.     raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
  601. if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
  602.     raise TestFailed, 'map(None, Squares(10))'
  603. if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
  604.     raise TestFailed, 'map(int, Squares(10))'
  605. if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
  606.     raise TestFailed, 'map(None, Squares(3), Squares(2))'
  607. if map(max, Squares(3), Squares(2)) != [0, 1, 4]:
  608.     raise TestFailed, 'map(max, Squares(3), Squares(2))'
  609.  
  610. print 'max'
  611. if max('123123') != '3': raise TestFailed, 'max(\'123123\')'
  612. if max(1, 2, 3) != 3: raise TestFailed, 'max(1, 2, 3)'
  613. if max((1, 2, 3, 1, 2, 3)) != 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
  614. if max([1, 2, 3, 1, 2, 3]) != 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
  615. #
  616. if max(1, 2L, 3.0) != 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
  617. if max(1L, 2.0, 3) != 3: raise TestFailed, 'max(1L, 2.0, 3)'
  618. if max(1.0, 2, 3L) != 3L: raise TestFailed, 'max(1.0, 2, 3L)'
  619.  
  620. print 'min'
  621. if min('123123') != '1': raise TestFailed, 'min(\'123123\')'
  622. if min(1, 2, 3) != 1: raise TestFailed, 'min(1, 2, 3)'
  623. if min((1, 2, 3, 1, 2, 3)) != 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
  624. if min([1, 2, 3, 1, 2, 3]) != 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
  625. #
  626. if min(1, 2L, 3.0) != 1: raise TestFailed, 'min(1, 2L, 3.0)'
  627. if min(1L, 2.0, 3) != 1L: raise TestFailed, 'min(1L, 2.0, 3)'
  628. if min(1.0, 2, 3L) != 1.0: raise TestFailed, 'min(1.0, 2, 3L)'
  629.