home *** CD-ROM | disk | FTP | other *** search
/ Freelog 33 / Freelog033.iso / Progr / Python-2.2.1.exe / TEST_B1.PY < prev    next >
Encoding:
Python Source  |  2002-03-13  |  20.5 KB  |  594 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. try: complex("1", "1")
  128. except TypeError: pass
  129. else: raise TestFailed, 'complex("1", "1")'
  130. try: complex(1, "1")
  131. except TypeError: pass
  132. else: raise TestFailed, 'complex(1, "1")'
  133. if complex("  3.14+J  ") != 3.14+1j:  raise TestFailed, 'complex("  3.14+J  )"'
  134. if have_unicode:
  135.     if complex(unicode("  3.14+J  ")) != 3.14+1j:
  136.         raise TestFailed, 'complex(u"  3.14+J  )"'
  137. class Z:
  138.     def __complex__(self): return 3.14j
  139. z = Z()
  140. if complex(z) != 3.14j: raise TestFailed, 'complex(classinstance)'
  141.  
  142. print 'delattr'
  143. import sys
  144. sys.spam = 1
  145. delattr(sys, 'spam')
  146.  
  147. print 'dir'
  148. x = 1
  149. if 'x' not in dir(): raise TestFailed, 'dir()'
  150. import sys
  151. if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
  152.  
  153. print 'divmod'
  154. if divmod(12, 7) != (1, 5): raise TestFailed, 'divmod(12, 7)'
  155. if divmod(-12, 7) != (-2, 2): raise TestFailed, 'divmod(-12, 7)'
  156. if divmod(12, -7) != (-2, -2): raise TestFailed, 'divmod(12, -7)'
  157. if divmod(-12, -7) != (1, -5): raise TestFailed, 'divmod(-12, -7)'
  158. #
  159. if divmod(12L, 7L) != (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
  160. if divmod(-12L, 7L) != (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
  161. if divmod(12L, -7L) != (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
  162. if divmod(-12L, -7L) != (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
  163. #
  164. if divmod(12, 7L) != (1, 5L): raise TestFailed, 'divmod(12, 7L)'
  165. if divmod(-12, 7L) != (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
  166. if divmod(12L, -7) != (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
  167. if divmod(-12L, -7) != (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
  168. #
  169. if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
  170.     raise TestFailed, 'divmod(3.25, 1.0)'
  171. if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
  172.     raise TestFailed, 'divmod(-3.25, 1.0)'
  173. if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
  174.     raise TestFailed, 'divmod(3.25, -1.0)'
  175. if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
  176.     raise TestFailed, 'divmod(-3.25, -1.0)'
  177.  
  178. print 'eval'
  179. if eval('1+1') != 2: raise TestFailed, 'eval(\'1+1\')'
  180. if eval(' 1+1\n') != 2: raise TestFailed, 'eval(\' 1+1\\n\')'
  181. globals = {'a': 1, 'b': 2}
  182. locals = {'b': 200, 'c': 300}
  183. if eval('a', globals) != 1:
  184.     raise TestFailed, "eval(1) == %s" % eval('a', globals)
  185. if eval('a', globals, locals) != 1:
  186.     raise TestFailed, "eval(2)"
  187. if eval('b', globals, locals) != 200:
  188.     raise TestFailed, "eval(3)"
  189. if eval('c', globals, locals) != 300:
  190.     raise TestFailed, "eval(4)"
  191. if have_unicode:
  192.     if eval(unicode('1+1')) != 2: raise TestFailed, 'eval(u\'1+1\')'
  193.     if eval(unicode(' 1+1\n')) != 2: raise TestFailed, 'eval(u\' 1+1\\n\')'
  194. globals = {'a': 1, 'b': 2}
  195. locals = {'b': 200, 'c': 300}
  196. if have_unicode:
  197.     if eval(unicode('a'), globals) != 1:
  198.         raise TestFailed, "eval(1) == %s" % eval(unicode('a'), globals)
  199.     if eval(unicode('a'), globals, locals) != 1:
  200.         raise TestFailed, "eval(2)"
  201.     if eval(unicode('b'), globals, locals) != 200:
  202.         raise TestFailed, "eval(3)"
  203.     if eval(unicode('c'), globals, locals) != 300:
  204.         raise TestFailed, "eval(4)"
  205.  
  206. print 'execfile'
  207. z = 0
  208. f = open(TESTFN, 'w')
  209. f.write('z = z+1\n')
  210. f.write('z = z*2\n')
  211. f.close()
  212. execfile(TESTFN)
  213. if z != 2: raise TestFailed, "execfile(1)"
  214. globals['z'] = 0
  215. execfile(TESTFN, globals)
  216. if globals['z'] != 2: raise TestFailed, "execfile(1)"
  217. locals['z'] = 0
  218. execfile(TESTFN, globals, locals)
  219. if locals['z'] != 2: raise TestFailed, "execfile(1)"
  220. unlink(TESTFN)
  221.  
  222. print 'filter'
  223. if filter(lambda c: 'a' <= c <= 'z', 'Hello World') != 'elloorld':
  224.     raise TestFailed, 'filter (filter a string)'
  225. if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) != [1, 'hello', [3], 9]:
  226.     raise TestFailed, 'filter (remove false values)'
  227. if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) != [1, 9, 2]:
  228.     raise TestFailed, 'filter (keep positives)'
  229. class Squares:
  230.     def __init__(self, max):
  231.         self.max = max
  232.         self.sofar = []
  233.     def __len__(self): return len(self.sofar)
  234.     def __getitem__(self, i):
  235.         if not 0 <= i < self.max: raise IndexError
  236.         n = len(self.sofar)
  237.         while n <= i:
  238.             self.sofar.append(n*n)
  239.             n = n+1
  240.         return self.sofar[i]
  241. if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
  242.     raise TestFailed, 'filter(None, Squares(10))'
  243. if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]:
  244.     raise TestFailed, 'filter(oddp, Squares(10))'
  245. class StrSquares:
  246.     def __init__(self, max):
  247.         self.max = max
  248.         self.sofar = []
  249.     def __len__(self):
  250.         return len(self.sofar)
  251.     def __getitem__(self, i):
  252.         if not 0 <= i < self.max:
  253.             raise IndexError
  254.         n = len(self.sofar)
  255.         while n <= i:
  256.             self.sofar.append(str(n*n))
  257.             n = n+1
  258.         return self.sofar[i]
  259. def identity(item):
  260.     return 1
  261. filter(identity, Squares(5))
  262.  
  263. print 'float'
  264. if float(3.14) != 3.14: raise TestFailed, 'float(3.14)'
  265. if float(314) != 314.0: raise TestFailed, 'float(314)'
  266. if float(314L) != 314.0: raise TestFailed, 'float(314L)'
  267. if float("  3.14  ") != 3.14:  raise TestFailed, 'float("  3.14  ")'
  268. if have_unicode:
  269.     if float(unicode("  3.14  ")) != 3.14:
  270.         raise TestFailed, 'float(u"  3.14  ")'
  271.     if float(unicode("  \u0663.\u0661\u0664  ",'raw-unicode-escape')) != 3.14:
  272.         raise TestFailed, 'float(u"  \u0663.\u0661\u0664  ")'
  273.  
  274. print 'getattr'
  275. import sys
  276. if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
  277. try:
  278.     getattr(sys, 1)
  279. except TypeError:
  280.     pass
  281. else:
  282.     raise TestFailed, "getattr(sys, 1) should raise an exception"
  283. try:
  284.     getattr(sys, 1, "foo")
  285. except TypeError:
  286.     pass
  287. else:
  288.     raise TestFailed, 'getattr(sys, 1, "foo") should raise an exception'
  289.  
  290. print 'hasattr'
  291. import sys
  292. if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr'
  293. try:
  294.     hasattr(sys, 1)
  295. except TypeError:
  296.     pass
  297. else:
  298.     raise TestFailed, "hasattr(sys, 1) should raise an exception"
  299.  
  300. print 'hash'
  301. hash(None)
  302. if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()'
  303. hash('spam')
  304. hash((0,1,2,3))
  305. def f(): pass
  306. try: hash([])
  307. except TypeError: pass
  308. else: raise TestFailed, "hash([]) should raise an exception"
  309. try: hash({})
  310. except TypeError: pass
  311. else: raise TestFailed, "hash({}) should raise an exception"
  312.  
  313. print 'hex'
  314. if hex(16) != '0x10': raise TestFailed, 'hex(16)'
  315. if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
  316. if len(hex(-1)) != len(hex(sys.maxint)): raise TestFailed, 'len(hex(-1))'
  317. if hex(-16) not in ('0xfffffff0', '0xfffffffffffffff0'):
  318.     raise TestFailed, 'hex(-16)'
  319. if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
  320.  
  321. print 'id'
  322. id(None)
  323. id(1)
  324. id(1L)
  325. id(1.0)
  326. id('spam')
  327. id((0,1,2,3))
  328. id([0,1,2,3])
  329. id({'spam': 1, 'eggs': 2, 'ham': 3})
  330.  
  331. # Test input() later, together with raw_input
  332.  
  333. print 'int'
  334. if int(314) != 314: raise TestFailed, 'int(314)'
  335. if int(3.14) != 3: raise TestFailed, 'int(3.14)'
  336. if int(314L) != 314: raise TestFailed, 'int(314L)'
  337. # Check that conversion from float truncates towards zero
  338. if int(-3.14) != -3: raise TestFailed, 'int(-3.14)'
  339. if int(3.9) != 3: raise TestFailed, 'int(3.9)'
  340. if int(-3.9) != -3: raise TestFailed, 'int(-3.9)'
  341. if int(3.5) != 3: raise TestFailed, 'int(3.5)'
  342. if int(-3.5) != -3: raise TestFailed, 'int(-3.5)'
  343. # Different base:
  344. if int("10",16) != 16L: raise TestFailed, 'int("10",16)'
  345. if have_unicode:
  346.     if int(unicode("10"),16) != 16L:
  347.         raise TestFailed, 'int(u"10",16)'
  348. # Test conversion from strings and various anomalies
  349. L = [
  350.         ('0', 0),
  351.         ('1', 1),
  352.         ('9', 9),
  353.         ('10', 10),
  354.         ('99', 99),
  355.         ('100', 100),
  356.         ('314', 314),
  357.         (' 314', 314),
  358.         ('314 ', 314),
  359.         ('  \t\t  314  \t\t  ', 314),
  360.         (`sys.maxint`, sys.maxint),
  361.         ('  1x', ValueError),
  362.         ('  1  ', 1),
  363.         ('  1\02  ', ValueError),
  364.         ('', ValueError),
  365.         (' ', ValueError),
  366.         ('  \t\t  ', ValueError)
  367. ]
  368. if have_unicode:
  369.     L += [
  370.         (unicode('0'), 0),
  371.         (unicode('1'), 1),
  372.         (unicode('9'), 9),
  373.         (unicode('10'), 10),
  374.         (unicode('99'), 99),
  375.         (unicode('100'), 100),
  376.         (unicode('314'), 314),
  377.         (unicode(' 314'), 314),
  378.         (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
  379.         (unicode('  \t\t  314  \t\t  '), 314),
  380.         (unicode('  1x'), ValueError),
  381.         (unicode('  1  '), 1),
  382.         (unicode('  1\02  '), ValueError),
  383.         (unicode(''), ValueError),
  384.         (unicode(' '), ValueError),
  385.         (unicode('  \t\t  '), ValueError),
  386. ]
  387. for s, v in L:
  388.     for sign in "", "+", "-":
  389.         for prefix in "", " ", "\t", "  \t\t  ":
  390.             ss = prefix + sign + s
  391.             vv = v
  392.             if sign == "-" and v is not ValueError:
  393.                 vv = -v
  394.             try:
  395.                 if int(ss) != vv:
  396.                     raise TestFailed, "int(%s)" % `ss`
  397.             except v:
  398.                 pass
  399.             except ValueError, e:
  400.                 raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e)
  401. s = `-1-sys.maxint`
  402. if int(s)+1 != -sys.maxint:
  403.     raise TestFailed, "int(%s)" % `s`
  404. try:
  405.     int(s[1:])
  406. except ValueError:
  407.     pass
  408. else:
  409.     raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError"
  410. try:
  411.     int(1e100)
  412. except OverflowError:
  413.     pass
  414. else:
  415.     raise TestFailed("int(1e100) expected OverflowError")
  416. try:
  417.     int(-1e100)
  418. except OverflowError:
  419.     pass
  420. else:
  421.     raise TestFailed("int(-1e100) expected OverflowError")
  422.  
  423.  
  424. # SF bug 434186:  0x80000000/2 != 0x80000000>>1.
  425. # Worked by accident in Windows release build, but failed in debug build.
  426. # Failed in all Linux builds.
  427. x = -1-sys.maxint
  428. if x >> 1 != x//2:
  429.     raise TestFailed("x >> 1 != x/2 when x == -1-sys.maxint")
  430.  
  431. try: int('123\0')
  432. except ValueError: pass
  433. else: raise TestFailed("int('123\0') didn't raise exception")
  434.  
  435. print 'isinstance'
  436. class C:
  437.     pass
  438. class D(C):
  439.     pass
  440. class E:
  441.     pass
  442. c = C()
  443. d = D()
  444. e = E()
  445. if not isinstance(c, C): raise TestFailed, 'isinstance(c, C)'
  446. if not isinstance(d, C): raise TestFailed, 'isinstance(d, C)'
  447. if isinstance(e, C): raise TestFailed, 'isinstance(e, C)'
  448. if isinstance(c, D): raise TestFailed, 'isinstance(c, D)'
  449. if isinstance('foo', E): raise TestFailed, 'isinstance("Foo", E)'
  450. try:
  451.     isinstance(E, 'foo')
  452.     raise TestFailed, 'isinstance(E, "foo")'
  453. except TypeError:
  454.     pass
  455.  
  456. print 'issubclass'
  457. if not issubclass(D, C): raise TestFailed, 'issubclass(D, C)'
  458. if not issubclass(C, C): raise TestFailed, 'issubclass(C, C)'
  459. if issubclass(C, D): raise TestFailed, 'issubclass(C, D)'
  460. try:
  461.     issubclass('foo', E)
  462.     raise TestFailed, 'issubclass("foo", E)'
  463. except TypeError:
  464.     pass
  465. try:
  466.     issubclass(E, 'foo')
  467.     raise TestFailed, 'issubclass(E, "foo")'
  468. except TypeError:
  469.     pass
  470.  
  471. print 'len'
  472. if len('123') != 3: raise TestFailed, 'len(\'123\')'
  473. if len(()) != 0: raise TestFailed, 'len(())'
  474. if len((1, 2, 3, 4)) != 4: raise TestFailed, 'len((1, 2, 3, 4))'
  475. if len([1, 2, 3, 4]) != 4: raise TestFailed, 'len([1, 2, 3, 4])'
  476. if len({}) != 0: raise TestFailed, 'len({})'
  477. if len({'a':1, 'b': 2}) != 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
  478.  
  479. print 'list'
  480. if list([]) != []: raise TestFailed, 'list([])'
  481. l0_3 = [0, 1, 2, 3]
  482. l0_3_bis = list(l0_3)
  483. if l0_3 != l0_3_bis or l0_3 is l0_3_bis: raise TestFailed, 'list([0, 1, 2, 3])'
  484. if list(()) != []: raise TestFailed, 'list(())'
  485. if list((0, 1, 2, 3)) != [0, 1, 2, 3]: raise TestFailed, 'list((0, 1, 2, 3))'
  486. if list('') != []: raise TestFailed, 'list('')'
  487. if list('spam') != ['s', 'p', 'a', 'm']: raise TestFailed, "list('spam')"
  488.  
  489. print 'long'
  490. if long(314) != 314L: raise TestFailed, 'long(314)'
  491. if long(3.14) != 3L: raise TestFailed, 'long(3.14)'
  492. if long(314L) != 314L: raise TestFailed, 'long(314L)'
  493. # Check that conversion from float truncates towards zero
  494. if long(-3.14) != -3L: raise TestFailed, 'long(-3.14)'
  495. if long(3.9) != 3L: raise TestFailed, 'long(3.9)'
  496. if long(-3.9) != -3L: raise TestFailed, 'long(-3.9)'
  497. if long(3.5) != 3L: raise TestFailed, 'long(3.5)'
  498. if long(-3.5) != -3L: raise TestFailed, 'long(-3.5)'
  499. if long("-3") != -3L: raise TestFailed, 'long("-3")'
  500. if have_unicode:
  501.     if long(unicode("-3")) != -3L:
  502.         raise TestFailed, 'long(u"-3")'
  503. # Different base:
  504. if long("10",16) != 16L: raise TestFailed, 'long("10",16)'
  505. if have_unicode:
  506.     if long(unicode("10"),16) != 16L:
  507.         raise TestFailed, 'long(u"10",16)'
  508. # Check conversions from string (same test set as for int(), and then some)
  509. LL = [
  510.         ('1' + '0'*20, 10L**20),
  511.         ('1' + '0'*100, 10L**100)
  512. ]
  513. if have_unicode:
  514.     L+=[
  515.         (unicode('1') + unicode('0')*20, 10L**20),
  516.         (unicode('1') + unicode('0')*100, 10L**100),
  517. ]
  518. for s, v in L + LL:
  519.     for sign in "", "+", "-":
  520.         for prefix in "", " ", "\t", "  \t\t  ":
  521.             ss = prefix + sign + s
  522.             vv = v
  523.             if sign == "-" and v is not ValueError:
  524.                 vv = -v
  525.             try:
  526.                 if long(ss) != long(vv):
  527.                     raise TestFailed, "long(%s)" % `ss`
  528.             except v:
  529.                 pass
  530.             except ValueError, e:
  531.                 raise TestFailed, "long(%s) raised ValueError: %s" % (`ss`, e)
  532.  
  533. try: long('123\0')
  534. except ValueError: pass
  535. else: raise TestFailed("long('123\0') didn't raise exception")
  536.  
  537. print 'map'
  538. if map(None, 'hello world') != ['h','e','l','l','o',' ','w','o','r','l','d']:
  539.     raise TestFailed, 'map(None, \'hello world\')'
  540. if map(None, 'abcd', 'efg') != \
  541.    [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
  542.     raise TestFailed, 'map(None, \'abcd\', \'efg\')'
  543. if map(None, range(10)) != [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
  544.     raise TestFailed, 'map(None, range(10))'
  545. if map(lambda x: x*x, range(1,4)) != [1, 4, 9]:
  546.     raise TestFailed, 'map(lambda x: x*x, range(1,4))'
  547. try:
  548.     from math import sqrt
  549. except ImportError:
  550.     def sqrt(x):
  551.         return pow(x, 0.5)
  552. if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) != [[4.0, 2.0], [9.0, 3.0]]:
  553.     raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
  554. if map(lambda x, y: x+y, [1,3,2], [9,1,4]) != [10, 4, 6]:
  555.     raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
  556. def plus(*v):
  557.     accu = 0
  558.     for i in v: accu = accu + i
  559.     return accu
  560. if map(plus, [1, 3, 7]) != [1, 3, 7]:
  561.     raise TestFailed, 'map(plus, [1, 3, 7])'
  562. if map(plus, [1, 3, 7], [4, 9, 2]) != [1+4, 3+9, 7+2]:
  563.     raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
  564. if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) != [1+4+1, 3+9+1, 7+2+0]:
  565.     raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
  566. if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
  567.     raise TestFailed, 'map(None, Squares(10))'
  568. if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
  569.     raise TestFailed, 'map(int, Squares(10))'
  570. if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
  571.     raise TestFailed, 'map(None, Squares(3), Squares(2))'
  572. if map(max, Squares(3), Squares(2)) != [0, 1, 4]:
  573.     raise TestFailed, 'map(max, Squares(3), Squares(2))'
  574.  
  575. print 'max'
  576. if max('123123') != '3': raise TestFailed, 'max(\'123123\')'
  577. if max(1, 2, 3) != 3: raise TestFailed, 'max(1, 2, 3)'
  578. if max((1, 2, 3, 1, 2, 3)) != 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
  579. if max([1, 2, 3, 1, 2, 3]) != 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
  580. #
  581. if max(1, 2L, 3.0) != 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
  582. if max(1L, 2.0, 3) != 3: raise TestFailed, 'max(1L, 2.0, 3)'
  583. if max(1.0, 2, 3L) != 3L: raise TestFailed, 'max(1.0, 2, 3L)'
  584.  
  585. print 'min'
  586. if min('123123') != '1': raise TestFailed, 'min(\'123123\')'
  587. if min(1, 2, 3) != 1: raise TestFailed, 'min(1, 2, 3)'
  588. if min((1, 2, 3, 1, 2, 3)) != 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
  589. if min([1, 2, 3, 1, 2, 3]) != 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
  590. #
  591. if min(1, 2L, 3.0) != 1: raise TestFailed, 'min(1, 2L, 3.0)'
  592. if min(1L, 2.0, 3) != 1L: raise TestFailed, 'min(1L, 2.0, 3)'
  593. if min(1.0, 2, 3L) != 1.0: raise TestFailed, 'min(1.0, 2, 3L)'
  594.