blob: fb6e9345c5875cf8e1d89be3981d9583a1b8a760 [file] [log] [blame]
Guido van Rossum85f18201992-11-27 22:53:50 +00001# Python test set -- part 6, built-in types
2
3from test_support import *
4
5print '6. Built-in types'
6
7print '6.1 Truth value testing'
8if None: raise TestFailed, 'None is true instead of false'
9if 0: raise TestFailed, '0 is true instead of false'
10if 0L: raise TestFailed, '0L is true instead of false'
11if 0.0: raise TestFailed, '0.0 is true instead of false'
12if '': raise TestFailed, '\'\' is true instead of false'
13if (): raise TestFailed, '() is true instead of false'
14if []: raise TestFailed, '[] is true instead of false'
15if {}: raise TestFailed, '{} is true instead of false'
16if not 1: raise TestFailed, '1 is false instead of true'
17if not 1L: raise TestFailed, '1L is false instead of true'
18if not 1.0: raise TestFailed, '1.0 is false instead of true'
19if not 'x': raise TestFailed, '\'x\' is false instead of true'
20if not (1, 1): raise TestFailed, '(1, 1) is false instead of true'
21if not [1]: raise TestFailed, '[1] is false instead of true'
22if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true'
23def f(): pass
24class C(): pass
25import sys
26x = C()
27if not f: raise TestFailed, 'f is false instead of true'
28if not C: raise TestFailed, 'C is false instead of true'
29if not sys: raise TestFailed, 'sys is false instead of true'
30if not x: raise TestFailed, 'x is false instead of true'
31
32print '6.2 Boolean operations'
33if 0 or 0: raise TestFailed, '0 or 0 is true instead of false'
34if 1 and 1: pass
35else: raise TestFailed, '1 and 1 is false instead of false'
36if not 1: raise TestFailed, 'not 1 is true instead of false'
37
38print '6.3 Comparisons'
39if 0 < 1 <= 1 == 1 >= 1 > 0 <> 1: pass
40else: raise TestFailed, 'int comparisons failed'
41if 0L < 1L <= 1L == 1L >= 1L > 0L <> 1L: pass
42else: raise TestFailed, 'long int comparisons failed'
43if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 <> 1.0: pass
44else: raise TestFailed, 'float comparisons failed'
45if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass
46else: raise TestFailed, 'string comparisons failed'
47if 0 in [0] and 0 not in [1]: pass
48else: raise TestFailed, 'membership test failed'
49if None is None and [] is not []: pass
50else: raise TestFailed, 'identity test failed'
51
52print '6.4 Numeric types (mostly conversions)'
53if 0 <> 0L or 0 <> 0.0 or 0L <> 0.0: raise TestFailed, 'mixed comparisons'
54if 1 <> 1L or 1 <> 1.0 or 1L <> 1.0: raise TestFailed, 'mixed comparisons'
55if -1 <> -1L or -1 <> -1.0 or -1L <> -1.0:
56 raise TestFailed, 'int/long/float value not equal'
57if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
58else: raise TestFailed, 'int() does not round properly'
59if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass
60else: raise TestFailed, 'long() does not round properly'
61if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
62else: raise TestFailed, 'float() does not work properly'
63
64print '6.5 Sequence types'
65
66print '6.5.1 Strings'
67if len('') <> 0: raise TestFailed, 'len(\'\')'
68if len('a') <> 1: raise TestFailed, 'len(\'a\')'
69if len('abcdef') <> 6: raise TestFailed, 'len(\'abcdef\')'
70if 'xyz' + 'abcde' <> 'xyzabcde': raise TestFailed, 'string concatenation'
71if 'xyz'*3 <> 'xyzxyzxyz': raise TestFailed, 'string repetition *3'
72if 0*'abcde' <> '': raise TestFailed, 'string repetition 0*'
73if min('abc') <> 'a' or max('abc') <> 'c': raise TestFailed, 'min/max string'
74if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
75else: raise TestFailed, 'in/not in string'
76
77print '6.5.2 Tuples'
78if len(()) <> 0: raise TestFailed, 'len(())'
79if len((1,)) <> 1: raise TestFailed, 'len((1,))'
80if len((1,2,3,4,5,6)) <> 6: raise TestFailed, 'len((1,2,3,4,5,6))'
81if (1,2)+(3,4) <> (1,2,3,4): raise TestFailed, 'tuple concatenation'
82if (1,2)*3 <> (1,2,1,2,1,2): raise TestFailed, 'tuple repetition *3'
83if 0*(1,2,3) <> (): raise TestFailed, 'tuple repetition 0*'
84if min((1,2)) <> 1 or max((1,2)) <> 2: raise TestFailed, 'min/max tuple'
85if 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
86else: raise TestFailed, 'in/not in tuple'
87
88print '6.5.3 Lists'
89if len([]) <> 0: raise TestFailed, 'len([])'
90if len([1,]) <> 1: raise TestFailed, 'len([1,])'
91if len([1,2,3,4,5,6]) <> 6: raise TestFailed, 'len([1,2,3,4,5,6])'
92if [1,2]+[3,4] <> [1,2,3,4]: raise TestFailed, 'list concatenation'
93if [1,2]*3 <> [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3'
94if 0*[1,2,3] <> []: raise TestFailed, 'list repetition 0*'
95if min([1,2]) <> 1 or max([1,2]) <> 2: raise TestFailed, 'min/max list'
96if 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
97else: raise TestFailed, 'in/not in list'
98
99print '6.5.3a Additional list operations'
100a = [0,1,2,3,4]
101a[0] = 5
102a[1] = 6
103a[2] = 7
104if a <> [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]'
105a[-2] = 8
106a[-1] = 9
107if a <> [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]'
108a[:2] = [0,4]
109a[-3:] = []
110a[1:1] = [1,2,3]
111if a <> [0,1,2,3,4]: raise TestFailed, 'list slice assignment'
112del a[1:4]
113if a <> [0,4]: raise TestFailed, 'list slice deletion'
114del a[0]
115if a <> [4]: raise TestFailed, 'list item deletion [0]'
116del a[-1]
117if a <> []: raise TestFailed, 'list item deletion [-1]'
118a.append(0)
119a.append(1)
120a.append(2)
121if a <> [0,1,2]: raise TestFailed, 'list append'
122a.insert(0, -2)
123a.insert(1, -1)
124a.insert(2,0)
125if a <> [-2,-1,0,0,1,2]: raise TestFailed, 'list insert'
126if a.count(0) <> 2: raise TestFailed, ' list count'
127if a.index(0) <> 2: raise TestFailed, 'list index'
128a.remove(0)
129if a <> [-2,-1,0,1,2]: raise TestFailed, 'list remove'
130a.reverse()
131if a <> [2,1,0,-1,-2]: raise TestFailed, 'list reverse'
132a.sort()
133if a <> [-2,-1,0,1,2]: raise TestFailed, 'list sort'
134
135print '6.6 Mappings == Dictionaries'
136d = {}
137if d.keys() <> []: raise TestFailed, '{}.keys()'
138if len(d) <> 0: raise TestFailed, 'len({})'
139d = {'a': 1, 'b': 2}
140if len(d) <> 2: raise TestFailed, 'len(dict)'
141k = d.keys()
142k.sort()
143if k <> ['a', 'b']: raise TestFailed, 'dict keys()'
144if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass
145else: raise TestFailed, 'dict keys()'
146if d['a'] <> 1 or d['b'] <> 2: raise TestFailed, 'dict item'
147d['c'] = 3
148d['a'] = 4
149if d['c'] <> 3 or d['a'] <> 4: raise TestFailed, 'dict item assignment'
150del d['b']
151if d <> {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion'