blob: c3d59cb9412e331ccd2a5761c00fa88c7eb510ba [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'
Guido van Rossumb6775db1994-08-01 11:34:53 +000070# Test for a particular bug in integer multiply
71xsize, ysize, zsize = 238, 356, 4
72if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912):
73 raise TestFailed, 'int mul commutativity'
Guido van Rossum80530ce1993-01-21 15:36:40 +000074print '6.4.2 Long integers'
75if 12L + 24L <> 36L: raise TestFailed, 'long op'
76if 12L + (-24L) <> -12L: raise TestFailed, 'long op'
77if (-12L) + 24L <> 12L: raise TestFailed, 'long op'
78if (-12L) + (-24L) <> -36L: raise TestFailed, 'long op'
79if not 12L < 24L: raise TestFailed, 'long op'
80if not -24L < -12L: raise TestFailed, 'long op'
81print '6.4.3 Floating point numbers'
82if 12.0 + 24.0 <> 36.0: raise TestFailed, 'float op'
83if 12.0 + (-24.0) <> -12.0: raise TestFailed, 'float op'
84if (-12.0) + 24.0 <> 12.0: raise TestFailed, 'float op'
85if (-12.0) + (-24.0) <> -36.0: raise TestFailed, 'float op'
86if not 12.0 < 24.0: raise TestFailed, 'float op'
87if not -24.0 < -12.0: raise TestFailed, 'float op'
Guido van Rossum85f18201992-11-27 22:53:50 +000088
89print '6.5 Sequence types'
90
91print '6.5.1 Strings'
92if len('') <> 0: raise TestFailed, 'len(\'\')'
93if len('a') <> 1: raise TestFailed, 'len(\'a\')'
94if len('abcdef') <> 6: raise TestFailed, 'len(\'abcdef\')'
95if 'xyz' + 'abcde' <> 'xyzabcde': raise TestFailed, 'string concatenation'
96if 'xyz'*3 <> 'xyzxyzxyz': raise TestFailed, 'string repetition *3'
97if 0*'abcde' <> '': raise TestFailed, 'string repetition 0*'
98if min('abc') <> 'a' or max('abc') <> 'c': raise TestFailed, 'min/max string'
99if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
100else: raise TestFailed, 'in/not in string'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000101x = 'x'*103
102if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug'
Guido van Rossum85f18201992-11-27 22:53:50 +0000103
104print '6.5.2 Tuples'
105if len(()) <> 0: raise TestFailed, 'len(())'
106if len((1,)) <> 1: raise TestFailed, 'len((1,))'
107if len((1,2,3,4,5,6)) <> 6: raise TestFailed, 'len((1,2,3,4,5,6))'
108if (1,2)+(3,4) <> (1,2,3,4): raise TestFailed, 'tuple concatenation'
109if (1,2)*3 <> (1,2,1,2,1,2): raise TestFailed, 'tuple repetition *3'
110if 0*(1,2,3) <> (): raise TestFailed, 'tuple repetition 0*'
111if min((1,2)) <> 1 or max((1,2)) <> 2: raise TestFailed, 'min/max tuple'
112if 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
113else: raise TestFailed, 'in/not in tuple'
114
115print '6.5.3 Lists'
116if len([]) <> 0: raise TestFailed, 'len([])'
117if len([1,]) <> 1: raise TestFailed, 'len([1,])'
118if len([1,2,3,4,5,6]) <> 6: raise TestFailed, 'len([1,2,3,4,5,6])'
119if [1,2]+[3,4] <> [1,2,3,4]: raise TestFailed, 'list concatenation'
120if [1,2]*3 <> [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3'
121if 0*[1,2,3] <> []: raise TestFailed, 'list repetition 0*'
122if min([1,2]) <> 1 or max([1,2]) <> 2: raise TestFailed, 'min/max list'
123if 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
124else: raise TestFailed, 'in/not in list'
125
126print '6.5.3a Additional list operations'
127a = [0,1,2,3,4]
128a[0] = 5
129a[1] = 6
130a[2] = 7
131if a <> [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]'
132a[-2] = 8
133a[-1] = 9
134if a <> [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]'
135a[:2] = [0,4]
136a[-3:] = []
137a[1:1] = [1,2,3]
138if a <> [0,1,2,3,4]: raise TestFailed, 'list slice assignment'
139del a[1:4]
140if a <> [0,4]: raise TestFailed, 'list slice deletion'
141del a[0]
142if a <> [4]: raise TestFailed, 'list item deletion [0]'
143del a[-1]
144if a <> []: raise TestFailed, 'list item deletion [-1]'
145a.append(0)
146a.append(1)
147a.append(2)
148if a <> [0,1,2]: raise TestFailed, 'list append'
149a.insert(0, -2)
150a.insert(1, -1)
151a.insert(2,0)
152if a <> [-2,-1,0,0,1,2]: raise TestFailed, 'list insert'
153if a.count(0) <> 2: raise TestFailed, ' list count'
154if a.index(0) <> 2: raise TestFailed, 'list index'
155a.remove(0)
156if a <> [-2,-1,0,1,2]: raise TestFailed, 'list remove'
157a.reverse()
158if a <> [2,1,0,-1,-2]: raise TestFailed, 'list reverse'
159a.sort()
160if a <> [-2,-1,0,1,2]: raise TestFailed, 'list sort'
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000161def revcmp(a, b): return cmp(b, a)
162a.sort(revcmp)
163if a <> [2,1,0,-1,-2]: raise TestFailed, 'list sort with cmp func'
Guido van Rossum85f18201992-11-27 22:53:50 +0000164
165print '6.6 Mappings == Dictionaries'
166d = {}
167if d.keys() <> []: raise TestFailed, '{}.keys()'
Guido van Rossumeecf0351994-12-30 17:17:46 +0000168if d.has_key('a') <> 0: raise TestFailed, '{}.has_key(\'a\')'
Guido van Rossum85f18201992-11-27 22:53:50 +0000169if len(d) <> 0: raise TestFailed, 'len({})'
170d = {'a': 1, 'b': 2}
171if len(d) <> 2: raise TestFailed, 'len(dict)'
172k = d.keys()
173k.sort()
174if k <> ['a', 'b']: raise TestFailed, 'dict keys()'
175if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass
176else: raise TestFailed, 'dict keys()'
177if d['a'] <> 1 or d['b'] <> 2: raise TestFailed, 'dict item'
178d['c'] = 3
179d['a'] = 4
180if d['c'] <> 3 or d['a'] <> 4: raise TestFailed, 'dict item assignment'
181del d['b']
182if d <> {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion'
Guido van Rossumce1fa261997-06-02 23:14:00 +0000183d = {1:1, 2:2, 3:3}
184d.clear()
185if d != {}: raise TestFailed, 'dict clear'
186d.update({1:100})
187d.update({2:20})
188d.update({1:1, 2:2, 3:3})
189if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update'
190if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'
191if {}.copy() != {}: raise TestFailed, 'empty dict copy'
Barry Warsaw9b887c71997-10-20 17:34:43 +0000192# dict.get()
193d = {'a' : 1, 'b' : 2}
194if d.get('c') != None: raise TestFailed, 'missing dict get, no 2nd arg'
195if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg'
196if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg'
197if d.get('a', 3) != 1: raise TestFailed, 'present dict get, w/ 2nd arg'