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