home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / test / test_types.py < prev    next >
Text File  |  1997-10-20  |  8KB  |  201 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. print '6.4.2 Long integers'
  75. if 12L + 24L <> 36L: raise TestFailed, 'long op'
  76. if 12L + (-24L) <> -12L: raise TestFailed, 'long op'
  77. if (-12L) + 24L <> 12L: raise TestFailed, 'long op'
  78. if (-12L) + (-24L) <> -36L: raise TestFailed, 'long op'
  79. if not 12L < 24L: raise TestFailed, 'long op'
  80. if not -24L < -12L: raise TestFailed, 'long op'
  81. print '6.4.3 Floating point numbers'
  82. if 12.0 + 24.0 <> 36.0: raise TestFailed, 'float op'
  83. if 12.0 + (-24.0) <> -12.0: raise TestFailed, 'float op'
  84. if (-12.0) + 24.0 <> 12.0: raise TestFailed, 'float op'
  85. if (-12.0) + (-24.0) <> -36.0: raise TestFailed, 'float op'
  86. if not 12.0 < 24.0: raise TestFailed, 'float op'
  87. if not -24.0 < -12.0: raise TestFailed, 'float op'
  88.  
  89. print '6.5 Sequence types'
  90.  
  91. print '6.5.1 Strings'
  92. if len('') <> 0: raise TestFailed, 'len(\'\')'
  93. if len('a') <> 1: raise TestFailed, 'len(\'a\')'
  94. if len('abcdef') <> 6: raise TestFailed, 'len(\'abcdef\')'
  95. if 'xyz' + 'abcde' <> 'xyzabcde': raise TestFailed, 'string concatenation'
  96. if 'xyz'*3 <> 'xyzxyzxyz': raise TestFailed, 'string repetition *3'
  97. if 0*'abcde' <> '': raise TestFailed, 'string repetition 0*'
  98. if min('abc') <> 'a' or max('abc') <> 'c': raise TestFailed, 'min/max string'
  99. if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
  100. else: raise TestFailed, 'in/not in string'
  101. x = 'x'*103
  102. if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug'
  103.  
  104. print '6.5.2 Tuples'
  105. if len(()) <> 0: raise TestFailed, 'len(())'
  106. if len((1,)) <> 1: raise TestFailed, 'len((1,))'
  107. if len((1,2,3,4,5,6)) <> 6: raise TestFailed, 'len((1,2,3,4,5,6))'
  108. if (1,2)+(3,4) <> (1,2,3,4): raise TestFailed, 'tuple concatenation'
  109. if (1,2)*3 <> (1,2,1,2,1,2): raise TestFailed, 'tuple repetition *3'
  110. if 0*(1,2,3) <> (): raise TestFailed, 'tuple repetition 0*'
  111. if min((1,2)) <> 1 or max((1,2)) <> 2: raise TestFailed, 'min/max tuple'
  112. 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
  113. else: raise TestFailed, 'in/not in tuple'
  114.  
  115. print '6.5.3 Lists'
  116. if len([]) <> 0: raise TestFailed, 'len([])'
  117. if len([1,]) <> 1: raise TestFailed, 'len([1,])'
  118. if len([1,2,3,4,5,6]) <> 6: raise TestFailed, 'len([1,2,3,4,5,6])'
  119. if [1,2]+[3,4] <> [1,2,3,4]: raise TestFailed, 'list concatenation'
  120. if [1,2]*3 <> [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3'
  121. if 0*[1,2,3] <> []: raise TestFailed, 'list repetition 0*'
  122. if min([1,2]) <> 1 or max([1,2]) <> 2: raise TestFailed, 'min/max list'
  123. 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
  124. else: raise TestFailed, 'in/not in list'
  125.  
  126. print '6.5.3a Additional list operations'
  127. a = [0,1,2,3,4]
  128. a[0] = 5
  129. a[1] = 6
  130. a[2] = 7
  131. if a <> [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]'
  132. a[-2] = 8
  133. a[-1] = 9
  134. if a <> [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]'
  135. a[:2] = [0,4]
  136. a[-3:] = []
  137. a[1:1] = [1,2,3]
  138. if a <> [0,1,2,3,4]: raise TestFailed, 'list slice assignment'
  139. del a[1:4]
  140. if a <> [0,4]: raise TestFailed, 'list slice deletion'
  141. del a[0]
  142. if a <> [4]: raise TestFailed, 'list item deletion [0]'
  143. del a[-1]
  144. if a <> []: raise TestFailed, 'list item deletion [-1]'
  145. a.append(0)
  146. a.append(1)
  147. a.append(2)
  148. if a <> [0,1,2]: raise TestFailed, 'list append'
  149. a.insert(0, -2)
  150. a.insert(1, -1)
  151. a.insert(2,0)
  152. if a <> [-2,-1,0,0,1,2]: raise TestFailed, 'list insert'
  153. if a.count(0) <> 2: raise TestFailed, ' list count'
  154. if a.index(0) <> 2: raise TestFailed, 'list index'
  155. a.remove(0)
  156. if a <> [-2,-1,0,1,2]: raise TestFailed, 'list remove'
  157. a.reverse()
  158. if a <> [2,1,0,-1,-2]: raise TestFailed, 'list reverse'
  159. a.sort()
  160. if a <> [-2,-1,0,1,2]: raise TestFailed, 'list sort'
  161. def revcmp(a, b): return cmp(b, a)
  162. a.sort(revcmp)
  163. if a <> [2,1,0,-1,-2]: raise TestFailed, 'list sort with cmp func'
  164.  
  165. print '6.6 Mappings == Dictionaries'
  166. d = {}
  167. if d.keys() <> []: raise TestFailed, '{}.keys()'
  168. if d.has_key('a') <> 0: raise TestFailed, '{}.has_key(\'a\')'
  169. if len(d) <> 0: raise TestFailed, 'len({})'
  170. d = {'a': 1, 'b': 2}
  171. if len(d) <> 2: raise TestFailed, 'len(dict)'
  172. k = d.keys()
  173. k.sort()
  174. if k <> ['a', 'b']: raise TestFailed, 'dict keys()'
  175. if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass
  176. else: raise TestFailed, 'dict keys()'
  177. if d['a'] <> 1 or d['b'] <> 2: raise TestFailed, 'dict item'
  178. d['c'] = 3
  179. d['a'] = 4
  180. if d['c'] <> 3 or d['a'] <> 4: raise TestFailed, 'dict item assignment'
  181. del d['b']
  182. if d <> {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion'
  183. d = {1:1, 2:2, 3:3}
  184. d.clear()
  185. if d != {}: raise TestFailed, 'dict clear'
  186. d.update({1:100})
  187. d.update({2:20})
  188. d.update({1:1, 2:2, 3:3})
  189. if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update'
  190. if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'
  191. if {}.copy() != {}: raise TestFailed, 'empty dict copy'
  192. # dict.get()
  193. d = {}
  194. if d.get('c') != None: raise TestFailed, 'missing {} get, no 2nd arg'
  195. if d.get('c', 3) != 3: raise TestFailed, 'missing {} get, w/ 2nd arg'
  196. d = {'a' : 1, 'b' : 2}
  197. if d.get('c') != None: raise TestFailed, 'missing dict get, no 2nd arg'
  198. if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg'
  199. if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg'
  200. if d.get('a', 3) != 1: raise TestFailed, 'present dict get, w/ 2nd arg'
  201.