home *** CD-ROM | disk | FTP | other *** search
/ Freelog 33 / Freelog033.iso / Progr / Python-2.2.1.exe / TEST_TYPES.PY < prev    next >
Encoding:
Python Source  |  2001-12-20  |  15.0 KB  |  400 lines

  1. # Python test set -- part 6, built-in types
  2.  
  3. from test_support import *
  4.  
  5. print '6. Built-in types'
  6.  
  7. print '6.1 Truth value testing'
  8. if None: raise TestFailed, 'None is true instead of false'
  9. if 0: raise TestFailed, '0 is true instead of false'
  10. if 0L: raise TestFailed, '0L is true instead of false'
  11. if 0.0: raise TestFailed, '0.0 is true instead of false'
  12. if '': raise TestFailed, '\'\' is true instead of false'
  13. if (): raise TestFailed, '() is true instead of false'
  14. if []: raise TestFailed, '[] is true instead of false'
  15. if {}: raise TestFailed, '{} is true instead of false'
  16. if not 1: raise TestFailed, '1 is false instead of true'
  17. if not 1L: raise TestFailed, '1L is false instead of true'
  18. if not 1.0: raise TestFailed, '1.0 is false instead of true'
  19. if not 'x': raise TestFailed, '\'x\' is false instead of true'
  20. if not (1, 1): raise TestFailed, '(1, 1) is false instead of true'
  21. if not [1]: raise TestFailed, '[1] is false instead of true'
  22. if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true'
  23. def f(): pass
  24. class C: pass
  25. import sys
  26. x = C()
  27. if not f: raise TestFailed, 'f is false instead of true'
  28. if not C: raise TestFailed, 'C is false instead of true'
  29. if not sys: raise TestFailed, 'sys is false instead of true'
  30. if not x: raise TestFailed, 'x is false instead of true'
  31.  
  32. print '6.2 Boolean operations'
  33. if 0 or 0: raise TestFailed, '0 or 0 is true instead of false'
  34. if 1 and 1: pass
  35. else: raise TestFailed, '1 and 1 is false instead of false'
  36. if not 1: raise TestFailed, 'not 1 is true instead of false'
  37.  
  38. print '6.3 Comparisons'
  39. if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
  40. else: raise TestFailed, 'int comparisons failed'
  41. if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass
  42. else: raise TestFailed, 'long int comparisons failed'
  43. if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass
  44. else: raise TestFailed, 'float comparisons failed'
  45. if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass
  46. else: raise TestFailed, 'string comparisons failed'
  47. if 0 in [0] and 0 not in [1]: pass
  48. else: raise TestFailed, 'membership test failed'
  49. if None is None and [] is not []: pass
  50. else: raise TestFailed, 'identity test failed'
  51.  
  52. print '6.4 Numeric types (mostly conversions)'
  53. if 0 != 0L or 0 != 0.0 or 0L != 0.0: raise TestFailed, 'mixed comparisons'
  54. if 1 != 1L or 1 != 1.0 or 1L != 1.0: raise TestFailed, 'mixed comparisons'
  55. if -1 != -1L or -1 != -1.0 or -1L != -1.0:
  56.     raise TestFailed, 'int/long/float value not equal'
  57. if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
  58. else: raise TestFailed, 'int() does not round properly'
  59. if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass
  60. else: raise TestFailed, 'long() does not round properly'
  61. if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
  62. else: raise TestFailed, 'float() does not work properly'
  63. print '6.4.1 32-bit integers'
  64. if 12 + 24 != 36: raise TestFailed, 'int op'
  65. if 12 + (-24) != -12: raise TestFailed, 'int op'
  66. if (-12) + 24 != 12: raise TestFailed, 'int op'
  67. if (-12) + (-24) != -36: raise TestFailed, 'int op'
  68. if not 12 < 24: raise TestFailed, 'int op'
  69. if not -24 < -12: raise TestFailed, 'int op'
  70. # Test for a particular bug in integer multiply
  71. xsize, ysize, zsize = 238, 356, 4
  72. if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912):
  73.     raise TestFailed, 'int mul commutativity'
  74. # And another.
  75. m = -sys.maxint - 1
  76. for divisor in 1, 2, 4, 8, 16, 32:
  77.     j = m // divisor
  78.     prod = divisor * j
  79.     if prod != m:
  80.         raise TestFailed, "%r * %r == %r != %r" % (divisor, j, prod, m)
  81.     if type(prod) is not int:
  82.         raise TestFailed, ("expected type(prod) to be int, not %r" %
  83.                            type(prod))
  84. # Check for expected * overflow to long.
  85. for divisor in 1, 2, 4, 8, 16, 32:
  86.     j = m // divisor - 1
  87.     prod = divisor * j
  88.     if type(prod) is not long:
  89.         raise TestFailed, ("expected type(%r) to be long, not %r" %
  90.                            (prod, type(prod)))
  91. # Check for expected * overflow to long.
  92. m = sys.maxint
  93. for divisor in 1, 2, 4, 8, 16, 32:
  94.     j = m // divisor + 1
  95.     prod = divisor * j
  96.     if type(prod) is not long:
  97.         raise TestFailed, ("expected type(%r) to be long, not %r" %
  98.                            (prod, type(prod)))
  99.  
  100. print '6.4.2 Long integers'
  101. if 12L + 24L != 36L: raise TestFailed, 'long op'
  102. if 12L + (-24L) != -12L: raise TestFailed, 'long op'
  103. if (-12L) + 24L != 12L: raise TestFailed, 'long op'
  104. if (-12L) + (-24L) != -36L: raise TestFailed, 'long op'
  105. if not 12L < 24L: raise TestFailed, 'long op'
  106. if not -24L < -12L: raise TestFailed, 'long op'
  107. x = sys.maxint
  108. if int(long(x)) != x: raise TestFailed, 'long op'
  109. try: int(long(x)+1L)
  110. except OverflowError: pass
  111. else:raise TestFailed, 'long op'
  112. x = -x
  113. if int(long(x)) != x: raise TestFailed, 'long op'
  114. x = x-1
  115. if int(long(x)) != x: raise TestFailed, 'long op'
  116. try: int(long(x)-1L)
  117. except OverflowError: pass
  118. else:raise TestFailed, 'long op'
  119. print '6.4.3 Floating point numbers'
  120. if 12.0 + 24.0 != 36.0: raise TestFailed, 'float op'
  121. if 12.0 + (-24.0) != -12.0: raise TestFailed, 'float op'
  122. if (-12.0) + 24.0 != 12.0: raise TestFailed, 'float op'
  123. if (-12.0) + (-24.0) != -36.0: raise TestFailed, 'float op'
  124. if not 12.0 < 24.0: raise TestFailed, 'float op'
  125. if not -24.0 < -12.0: raise TestFailed, 'float op'
  126.  
  127. print '6.5 Sequence types'
  128.  
  129. print '6.5.1 Strings'
  130. if len('') != 0: raise TestFailed, 'len(\'\')'
  131. if len('a') != 1: raise TestFailed, 'len(\'a\')'
  132. if len('abcdef') != 6: raise TestFailed, 'len(\'abcdef\')'
  133. if 'xyz' + 'abcde' != 'xyzabcde': raise TestFailed, 'string concatenation'
  134. if 'xyz'*3 != 'xyzxyzxyz': raise TestFailed, 'string repetition *3'
  135. if 0*'abcde' != '': raise TestFailed, 'string repetition 0*'
  136. if min('abc') != 'a' or max('abc') != 'c': raise TestFailed, 'min/max string'
  137. if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
  138. else: raise TestFailed, 'in/not in string'
  139. x = 'x'*103
  140. if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug'
  141.  
  142. print '6.5.2 Tuples'
  143. if len(()) != 0: raise TestFailed, 'len(())'
  144. if len((1,)) != 1: raise TestFailed, 'len((1,))'
  145. if len((1,2,3,4,5,6)) != 6: raise TestFailed, 'len((1,2,3,4,5,6))'
  146. if (1,2)+(3,4) != (1,2,3,4): raise TestFailed, 'tuple concatenation'
  147. if (1,2)*3 != (1,2,1,2,1,2): raise TestFailed, 'tuple repetition *3'
  148. if 0*(1,2,3) != (): raise TestFailed, 'tuple repetition 0*'
  149. if min((1,2)) != 1 or max((1,2)) != 2: raise TestFailed, 'min/max tuple'
  150. if 0 in (0,1,2) and 1 in (0,1,2) and 2 in (0,1,2) and 3 not in (0,1,2): pass
  151. else: raise TestFailed, 'in/not in tuple'
  152.  
  153. print '6.5.3 Lists'
  154. if len([]) != 0: raise TestFailed, 'len([])'
  155. if len([1,]) != 1: raise TestFailed, 'len([1,])'
  156. if len([1,2,3,4,5,6]) != 6: raise TestFailed, 'len([1,2,3,4,5,6])'
  157. if [1,2]+[3,4] != [1,2,3,4]: raise TestFailed, 'list concatenation'
  158. if [1,2]*3 != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3'
  159. if [1,2]*3L != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3L'
  160. if 0*[1,2,3] != []: raise TestFailed, 'list repetition 0*'
  161. if 0L*[1,2,3] != []: raise TestFailed, 'list repetition 0L*'
  162. if min([1,2]) != 1 or max([1,2]) != 2: raise TestFailed, 'min/max list'
  163. if 0 in [0,1,2] and 1 in [0,1,2] and 2 in [0,1,2] and 3 not in [0,1,2]: pass
  164. else: raise TestFailed, 'in/not in list'
  165. a = [1, 2, 3, 4, 5]
  166. a[:-1] = a
  167. if a != [1, 2, 3, 4, 5, 5]:
  168.     raise TestFailed, "list self-slice-assign (head)"
  169. a = [1, 2, 3, 4, 5]
  170. a[1:] = a
  171. if a != [1, 1, 2, 3, 4, 5]:
  172.     raise TestFailed, "list self-slice-assign (tail)"
  173. a = [1, 2, 3, 4, 5]
  174. a[1:-1] = a
  175. if a != [1, 1, 2, 3, 4, 5, 5]:
  176.     raise TestFailed, "list self-slice-assign (center)"
  177.  
  178.  
  179. print '6.5.3a Additional list operations'
  180. a = [0,1,2,3,4]
  181. a[0L] = 1
  182. a[1L] = 2
  183. a[2L] = 3
  184. if a != [1,2,3,3,4]: raise TestFailed, 'list item assignment [0L], [1L], [2L]'
  185. a[0] = 5
  186. a[1] = 6
  187. a[2] = 7
  188. if a != [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]'
  189. a[-2L] = 88
  190. a[-1L] = 99
  191. if a != [5,6,7,88,99]: raise TestFailed, 'list item assignment [-2L], [-1L]'
  192. a[-2] = 8
  193. a[-1] = 9
  194. if a != [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]'
  195. a[:2] = [0,4]
  196. a[-3:] = []
  197. a[1:1] = [1,2,3]
  198. if a != [0,1,2,3,4]: raise TestFailed, 'list slice assignment'
  199. a[ 1L : 4L] = [7,8,9]
  200. if a != [0,7,8,9,4]: raise TestFailed, 'list slice assignment using long ints'
  201. del a[1:4]
  202. if a != [0,4]: raise TestFailed, 'list slice deletion'
  203. del a[0]
  204. if a != [4]: raise TestFailed, 'list item deletion [0]'
  205. del a[-1]
  206. if a != []: raise TestFailed, 'list item deletion [-1]'
  207. a=range(0,5)
  208. del a[1L:4L]
  209. if a != [0,4]: raise TestFailed, 'list slice deletion'
  210. del a[0L]
  211. if a != [4]: raise TestFailed, 'list item deletion [0]'
  212. del a[-1L]
  213. if a != []: raise TestFailed, 'list item deletion [-1]'
  214. a.append(0)
  215. a.append(1)
  216. a.append(2)
  217. if a != [0,1,2]: raise TestFailed, 'list append'
  218. a.insert(0, -2)
  219. a.insert(1, -1)
  220. a.insert(2,0)
  221. if a != [-2,-1,0,0,1,2]: raise TestFailed, 'list insert'
  222. if a.count(0) != 2: raise TestFailed, ' list count'
  223. if a.index(0) != 2: raise TestFailed, 'list index'
  224. a.remove(0)
  225. if a != [-2,-1,0,1,2]: raise TestFailed, 'list remove'
  226. a.reverse()
  227. if a != [2,1,0,-1,-2]: raise TestFailed, 'list reverse'
  228. a.sort()
  229. if a != [-2,-1,0,1,2]: raise TestFailed, 'list sort'
  230. def revcmp(a, b): return cmp(b, a)
  231. a.sort(revcmp)
  232. if a != [2,1,0,-1,-2]: raise TestFailed, 'list sort with cmp func'
  233. # The following dumps core in unpatched Python 1.5:
  234. def myComparison(x,y):
  235.     return cmp(x%3, y%7)
  236. z = range(12)
  237. z.sort(myComparison)
  238.  
  239. # Test extreme cases with long ints
  240. a = [0,1,2,3,4]
  241. if a[ -pow(2,128L): 3 ] != [0,1,2]:
  242.     raise TestFailed, "list slicing with too-small long integer"
  243. if a[ 3: pow(2,145L) ] != [3,4]:
  244.     raise TestFailed, "list slicing with too-large long integer"
  245.  
  246. print '6.6 Mappings == Dictionaries'
  247. d = {}
  248. if d.keys() != []: raise TestFailed, '{}.keys()'
  249. if d.has_key('a') != 0: raise TestFailed, '{}.has_key(\'a\')'
  250. if ('a' in d) != 0: raise TestFailed, "'a' in {}"
  251. if ('a' not in d) != 1: raise TestFailed, "'a' not in {}"
  252. if len(d) != 0: raise TestFailed, 'len({})'
  253. d = {'a': 1, 'b': 2}
  254. if len(d) != 2: raise TestFailed, 'len(dict)'
  255. k = d.keys()
  256. k.sort()
  257. if k != ['a', 'b']: raise TestFailed, 'dict keys()'
  258. if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass
  259. else: raise TestFailed, 'dict keys()'
  260. if 'a' in d and 'b' in d and 'c' not in d: pass
  261. else: raise TestFailed, 'dict keys() # in/not in version'
  262. if d['a'] != 1 or d['b'] != 2: raise TestFailed, 'dict item'
  263. d['c'] = 3
  264. d['a'] = 4
  265. if d['c'] != 3 or d['a'] != 4: raise TestFailed, 'dict item assignment'
  266. del d['b']
  267. if d != {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion'
  268. # dict.clear()
  269. d = {1:1, 2:2, 3:3}
  270. d.clear()
  271. if d != {}: raise TestFailed, 'dict clear'
  272. # dict.update()
  273. d.update({1:100})
  274. d.update({2:20})
  275. d.update({1:1, 2:2, 3:3})
  276. if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update'
  277. d.clear()
  278. try: d.update(None)
  279. except AttributeError: pass
  280. else: raise TestFailed, 'dict.update(None), AttributeError expected'
  281. class SimpleUserDict:
  282.     def __init__(self):
  283.         self.d = {1:1, 2:2, 3:3}
  284.     def keys(self):
  285.         return self.d.keys()
  286.     def __getitem__(self, i):
  287.         return self.d[i]
  288. d.update(SimpleUserDict())
  289. if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict.update(instance)'
  290. d.clear()
  291. class FailingUserDict:
  292.     def keys(self):
  293.         raise ValueError
  294. try: d.update(FailingUserDict())
  295. except ValueError: pass
  296. else: raise TestFailed, 'dict.keys() expected ValueError'
  297. class FailingUserDict:
  298.     def keys(self):
  299.         class BogonIter:
  300.             def __iter__(self):
  301.                 raise ValueError
  302.         return BogonIter()
  303. try: d.update(FailingUserDict())
  304. except ValueError: pass
  305. else: raise TestFailed, 'iter(dict.keys()) expected ValueError'
  306. class FailingUserDict:
  307.     def keys(self):
  308.         class BogonIter:
  309.             def __init__(self):
  310.                 self.i = 1
  311.             def __iter__(self):
  312.                 return self
  313.             def next(self):
  314.                 if self.i:
  315.                     self.i = 0
  316.                     return 'a'
  317.                 raise ValueError
  318.         return BogonIter()
  319.     def __getitem__(self, key):
  320.         return key
  321. try: d.update(FailingUserDict())
  322. except ValueError: pass
  323. else: raise TestFailed, 'iter(dict.keys()).next() expected ValueError'
  324. class FailingUserDict:
  325.     def keys(self):
  326.         class BogonIter:
  327.             def __init__(self):
  328.                 self.i = ord('a')
  329.             def __iter__(self):
  330.                 return self
  331.             def next(self):
  332.                 if self.i <= ord('z'):
  333.                     rtn = chr(self.i)
  334.                     self.i += 1
  335.                     return rtn
  336.                 raise StopIteration
  337.         return BogonIter()
  338.     def __getitem__(self, key):
  339.         raise ValueError
  340. try: d.update(FailingUserDict())
  341. except ValueError: pass
  342. else: raise TestFailed, 'dict.update(), __getitem__ expected ValueError'
  343. # dict.copy()
  344. d = {1:1, 2:2, 3:3}
  345. if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'
  346. if {}.copy() != {}: raise TestFailed, 'empty dict copy'
  347. # dict.get()
  348. d = {}
  349. if d.get('c') is not None: raise TestFailed, 'missing {} get, no 2nd arg'
  350. if d.get('c', 3) != 3: raise TestFailed, 'missing {} get, w/ 2nd arg'
  351. d = {'a' : 1, 'b' : 2}
  352. if d.get('c') is not None: raise TestFailed, 'missing dict get, no 2nd arg'
  353. if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg'
  354. if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg'
  355. if d.get('a', 3) != 1: raise TestFailed, 'present dict get, w/ 2nd arg'
  356. # dict.setdefault()
  357. d = {}
  358. if d.setdefault('key0') is not None:
  359.     raise TestFailed, 'missing {} setdefault, no 2nd arg'
  360. if d.setdefault('key0') is not None:
  361.     raise TestFailed, 'present {} setdefault, no 2nd arg'
  362. d.setdefault('key', []).append(3)
  363. if d['key'][0] != 3:
  364.     raise TestFailed, 'missing {} setdefault, w/ 2nd arg'
  365. d.setdefault('key', []).append(4)
  366. if len(d['key']) != 2:
  367.     raise TestFailed, 'present {} setdefault, w/ 2nd arg'
  368. # dict.popitem()
  369. for copymode in -1, +1:
  370.     # -1: b has same structure as a
  371.     # +1: b is a.copy()
  372.     for log2size in range(12):
  373.         size = 2**log2size
  374.         a = {}
  375.         b = {}
  376.         for i in range(size):
  377.             a[`i`] = i
  378.             if copymode < 0:
  379.                 b[`i`] = i
  380.         if copymode > 0:
  381.             b = a.copy()
  382.         for i in range(size):
  383.             ka, va = ta = a.popitem()
  384.             if va != int(ka): raise TestFailed, "a.popitem: %s" % str(ta)
  385.             kb, vb = tb = b.popitem()
  386.             if vb != int(kb): raise TestFailed, "b.popitem: %s" % str(tb)
  387.             if copymode < 0 and ta != tb:
  388.                 raise TestFailed, "a.popitem != b.popitem: %s, %s" % (
  389.                     str(ta), str(tb))
  390.         if a: raise TestFailed, 'a not empty after popitems: %s' % str(a)
  391.         if b: raise TestFailed, 'b not empty after popitems: %s' % str(b)
  392.  
  393. try: type(1, 2)
  394. except TypeError: pass
  395. else: raise TestFailed, 'type(), w/2 args expected TypeError'
  396.  
  397. try: type(1, 2, 3, 4)
  398. except TypeError: pass
  399. else: raise TestFailed, 'type(), w/4 args expected TypeError'
  400.