blob: ec0f84100f0715dc1fddccf26d24af71651714d9 [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
Guido van Rossumd3166071993-05-24 14:16:22 +000024class C: pass
Guido van Rossum85f18201992-11-27 22:53:50 +000025import 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'
Guido van Rossum80530ce1993-01-21 15:36:40 +000063print '6.4.1 32-bit integers'
64if 12 + 24 <> 36: raise TestFailed, 'int op'
65if 12 + (-24) <> -12: raise TestFailed, 'int op'
66if (-12) + 24 <> 12: raise TestFailed, 'int op'
67if (-12) + (-24) <> -36: raise TestFailed, 'int op'
68if not 12 < 24: raise TestFailed, 'int op'
69if not -24 < -12: raise TestFailed, 'int op'
70print '6.4.2 Long integers'
71if 12L + 24L <> 36L: raise TestFailed, 'long op'
72if 12L + (-24L) <> -12L: raise TestFailed, 'long op'
73if (-12L) + 24L <> 12L: raise TestFailed, 'long op'
74if (-12L) + (-24L) <> -36L: raise TestFailed, 'long op'
75if not 12L < 24L: raise TestFailed, 'long op'
76if not -24L < -12L: raise TestFailed, 'long op'
77print '6.4.3 Floating point numbers'
78if 12.0 + 24.0 <> 36.0: raise TestFailed, 'float op'
79if 12.0 + (-24.0) <> -12.0: raise TestFailed, 'float op'
80if (-12.0) + 24.0 <> 12.0: raise TestFailed, 'float op'
81if (-12.0) + (-24.0) <> -36.0: raise TestFailed, 'float op'
82if not 12.0 < 24.0: raise TestFailed, 'float op'
83if not -24.0 < -12.0: raise TestFailed, 'float op'
Guido van Rossum85f18201992-11-27 22:53:50 +000084
85print '6.5 Sequence types'
86
87print '6.5.1 Strings'
88if len('') <> 0: raise TestFailed, 'len(\'\')'
89if len('a') <> 1: raise TestFailed, 'len(\'a\')'
90if len('abcdef') <> 6: raise TestFailed, 'len(\'abcdef\')'
91if 'xyz' + 'abcde' <> 'xyzabcde': raise TestFailed, 'string concatenation'
92if 'xyz'*3 <> 'xyzxyzxyz': raise TestFailed, 'string repetition *3'
93if 0*'abcde' <> '': raise TestFailed, 'string repetition 0*'
94if min('abc') <> 'a' or max('abc') <> 'c': raise TestFailed, 'min/max string'
95if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
96else: raise TestFailed, 'in/not in string'
97
98print '6.5.2 Tuples'
99if len(()) <> 0: raise TestFailed, 'len(())'
100if len((1,)) <> 1: raise TestFailed, 'len((1,))'
101if len((1,2,3,4,5,6)) <> 6: raise TestFailed, 'len((1,2,3,4,5,6))'
102if (1,2)+(3,4) <> (1,2,3,4): raise TestFailed, 'tuple concatenation'
103if (1,2)*3 <> (1,2,1,2,1,2): raise TestFailed, 'tuple repetition *3'
104if 0*(1,2,3) <> (): raise TestFailed, 'tuple repetition 0*'
105if min((1,2)) <> 1 or max((1,2)) <> 2: raise TestFailed, 'min/max tuple'
106if 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
107else: raise TestFailed, 'in/not in tuple'
108
109print '6.5.3 Lists'
110if len([]) <> 0: raise TestFailed, 'len([])'
111if len([1,]) <> 1: raise TestFailed, 'len([1,])'
112if len([1,2,3,4,5,6]) <> 6: raise TestFailed, 'len([1,2,3,4,5,6])'
113if [1,2]+[3,4] <> [1,2,3,4]: raise TestFailed, 'list concatenation'
114if [1,2]*3 <> [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3'
115if 0*[1,2,3] <> []: raise TestFailed, 'list repetition 0*'
116if min([1,2]) <> 1 or max([1,2]) <> 2: raise TestFailed, 'min/max list'
117if 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
118else: raise TestFailed, 'in/not in list'
119
120print '6.5.3a Additional list operations'
121a = [0,1,2,3,4]
122a[0] = 5
123a[1] = 6
124a[2] = 7
125if a <> [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]'
126a[-2] = 8
127a[-1] = 9
128if a <> [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]'
129a[:2] = [0,4]
130a[-3:] = []
131a[1:1] = [1,2,3]
132if a <> [0,1,2,3,4]: raise TestFailed, 'list slice assignment'
133del a[1:4]
134if a <> [0,4]: raise TestFailed, 'list slice deletion'
135del a[0]
136if a <> [4]: raise TestFailed, 'list item deletion [0]'
137del a[-1]
138if a <> []: raise TestFailed, 'list item deletion [-1]'
139a.append(0)
140a.append(1)
141a.append(2)
142if a <> [0,1,2]: raise TestFailed, 'list append'
143a.insert(0, -2)
144a.insert(1, -1)
145a.insert(2,0)
146if a <> [-2,-1,0,0,1,2]: raise TestFailed, 'list insert'
147if a.count(0) <> 2: raise TestFailed, ' list count'
148if a.index(0) <> 2: raise TestFailed, 'list index'
149a.remove(0)
150if a <> [-2,-1,0,1,2]: raise TestFailed, 'list remove'
151a.reverse()
152if a <> [2,1,0,-1,-2]: raise TestFailed, 'list reverse'
153a.sort()
154if a <> [-2,-1,0,1,2]: raise TestFailed, 'list sort'
155
156print '6.6 Mappings == Dictionaries'
157d = {}
158if d.keys() <> []: raise TestFailed, '{}.keys()'
159if len(d) <> 0: raise TestFailed, 'len({})'
160d = {'a': 1, 'b': 2}
161if len(d) <> 2: raise TestFailed, 'len(dict)'
162k = d.keys()
163k.sort()
164if k <> ['a', 'b']: raise TestFailed, 'dict keys()'
165if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass
166else: raise TestFailed, 'dict keys()'
167if d['a'] <> 1 or d['b'] <> 2: raise TestFailed, 'dict item'
168d['c'] = 3
169d['a'] = 4
170if d['c'] <> 3 or d['a'] <> 4: raise TestFailed, 'dict item assignment'
171del d['b']
172if d <> {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion'