blob: eec9e2d7a358f58655897aeadeff34900a2f29f8 [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:
Fred Drake004d5e62000-10-23 17:22:08 +000056 raise TestFailed, 'int/long/float value not equal'
Guido van Rossum85f18201992-11-27 22:53:50 +000057if 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):
Fred Drake004d5e62000-10-23 17:22:08 +000073 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'
Guido van Rossum74629421998-05-26 14:51:55 +000081x = sys.maxint
82if int(long(x)) != x: raise TestFailed, 'long op'
83try: int(long(x)+1L)
84except OverflowError: pass
85else:raise TestFailed, 'long op'
86x = -x
87if int(long(x)) != x: raise TestFailed, 'long op'
88x = x-1
89if int(long(x)) != x: raise TestFailed, 'long op'
90try: int(long(x)-1L)
91except OverflowError: pass
92else:raise TestFailed, 'long op'
Guido van Rossum80530ce1993-01-21 15:36:40 +000093print '6.4.3 Floating point numbers'
94if 12.0 + 24.0 <> 36.0: raise TestFailed, 'float op'
95if 12.0 + (-24.0) <> -12.0: raise TestFailed, 'float op'
96if (-12.0) + 24.0 <> 12.0: raise TestFailed, 'float op'
97if (-12.0) + (-24.0) <> -36.0: raise TestFailed, 'float op'
98if not 12.0 < 24.0: raise TestFailed, 'float op'
99if not -24.0 < -12.0: raise TestFailed, 'float op'
Guido van Rossum85f18201992-11-27 22:53:50 +0000100
101print '6.5 Sequence types'
102
103print '6.5.1 Strings'
104if len('') <> 0: raise TestFailed, 'len(\'\')'
105if len('a') <> 1: raise TestFailed, 'len(\'a\')'
106if len('abcdef') <> 6: raise TestFailed, 'len(\'abcdef\')'
107if 'xyz' + 'abcde' <> 'xyzabcde': raise TestFailed, 'string concatenation'
108if 'xyz'*3 <> 'xyzxyzxyz': raise TestFailed, 'string repetition *3'
109if 0*'abcde' <> '': raise TestFailed, 'string repetition 0*'
110if min('abc') <> 'a' or max('abc') <> 'c': raise TestFailed, 'min/max string'
111if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
112else: raise TestFailed, 'in/not in string'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000113x = 'x'*103
114if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug'
Guido van Rossum85f18201992-11-27 22:53:50 +0000115
116print '6.5.2 Tuples'
117if len(()) <> 0: raise TestFailed, 'len(())'
118if len((1,)) <> 1: raise TestFailed, 'len((1,))'
119if len((1,2,3,4,5,6)) <> 6: raise TestFailed, 'len((1,2,3,4,5,6))'
120if (1,2)+(3,4) <> (1,2,3,4): raise TestFailed, 'tuple concatenation'
121if (1,2)*3 <> (1,2,1,2,1,2): raise TestFailed, 'tuple repetition *3'
122if 0*(1,2,3) <> (): raise TestFailed, 'tuple repetition 0*'
123if min((1,2)) <> 1 or max((1,2)) <> 2: raise TestFailed, 'min/max tuple'
124if 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
125else: raise TestFailed, 'in/not in tuple'
126
127print '6.5.3 Lists'
128if len([]) <> 0: raise TestFailed, 'len([])'
129if len([1,]) <> 1: raise TestFailed, 'len([1,])'
130if len([1,2,3,4,5,6]) <> 6: raise TestFailed, 'len([1,2,3,4,5,6])'
131if [1,2]+[3,4] <> [1,2,3,4]: raise TestFailed, 'list concatenation'
132if [1,2]*3 <> [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000133if [1,2]*3L <> [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3L'
Guido van Rossum85f18201992-11-27 22:53:50 +0000134if 0*[1,2,3] <> []: raise TestFailed, 'list repetition 0*'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000135if 0L*[1,2,3] <> []: raise TestFailed, 'list repetition 0L*'
Guido van Rossum85f18201992-11-27 22:53:50 +0000136if min([1,2]) <> 1 or max([1,2]) <> 2: raise TestFailed, 'min/max list'
137if 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
138else: raise TestFailed, 'in/not in list'
Guido van Rossumaffd77f1998-07-16 15:29:06 +0000139a = [1, 2, 3, 4, 5]
140a[:-1] = a
141if a != [1, 2, 3, 4, 5, 5]:
Fred Drake004d5e62000-10-23 17:22:08 +0000142 raise TestFailed, "list self-slice-assign (head)"
Guido van Rossumaffd77f1998-07-16 15:29:06 +0000143a = [1, 2, 3, 4, 5]
144a[1:] = a
145if a != [1, 1, 2, 3, 4, 5]:
Fred Drake004d5e62000-10-23 17:22:08 +0000146 raise TestFailed, "list self-slice-assign (tail)"
Guido van Rossumaffd77f1998-07-16 15:29:06 +0000147a = [1, 2, 3, 4, 5]
148a[1:-1] = a
149if a != [1, 1, 2, 3, 4, 5, 5]:
Fred Drake004d5e62000-10-23 17:22:08 +0000150 raise TestFailed, "list self-slice-assign (center)"
Guido van Rossumaffd77f1998-07-16 15:29:06 +0000151
Guido van Rossum85f18201992-11-27 22:53:50 +0000152
153print '6.5.3a Additional list operations'
154a = [0,1,2,3,4]
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000155a[0L] = 1
156a[1L] = 2
157a[2L] = 3
158if a <> [1,2,3,3,4]: raise TestFailed, 'list item assignment [0L], [1L], [2L]'
Guido van Rossum85f18201992-11-27 22:53:50 +0000159a[0] = 5
160a[1] = 6
161a[2] = 7
162if a <> [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000163a[-2L] = 88
164a[-1L] = 99
165if a <> [5,6,7,88,99]: raise TestFailed, 'list item assignment [-2L], [-1L]'
Guido van Rossum85f18201992-11-27 22:53:50 +0000166a[-2] = 8
167a[-1] = 9
168if a <> [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]'
169a[:2] = [0,4]
170a[-3:] = []
171a[1:1] = [1,2,3]
172if a <> [0,1,2,3,4]: raise TestFailed, 'list slice assignment'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000173a[ 1L : 4L] = [7,8,9]
174if a <> [0,7,8,9,4]: raise TestFailed, 'list slice assignment using long ints'
Guido van Rossum85f18201992-11-27 22:53:50 +0000175del a[1:4]
176if a <> [0,4]: raise TestFailed, 'list slice deletion'
177del a[0]
178if a <> [4]: raise TestFailed, 'list item deletion [0]'
179del a[-1]
180if a <> []: raise TestFailed, 'list item deletion [-1]'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000181a=range(0,5)
182del a[1L:4L]
183if a <> [0,4]: raise TestFailed, 'list slice deletion'
184del a[0L]
185if a <> [4]: raise TestFailed, 'list item deletion [0]'
186del a[-1L]
187if a <> []: raise TestFailed, 'list item deletion [-1]'
Guido van Rossum85f18201992-11-27 22:53:50 +0000188a.append(0)
189a.append(1)
190a.append(2)
191if a <> [0,1,2]: raise TestFailed, 'list append'
192a.insert(0, -2)
193a.insert(1, -1)
194a.insert(2,0)
195if a <> [-2,-1,0,0,1,2]: raise TestFailed, 'list insert'
196if a.count(0) <> 2: raise TestFailed, ' list count'
197if a.index(0) <> 2: raise TestFailed, 'list index'
198a.remove(0)
199if a <> [-2,-1,0,1,2]: raise TestFailed, 'list remove'
200a.reverse()
201if a <> [2,1,0,-1,-2]: raise TestFailed, 'list reverse'
202a.sort()
203if a <> [-2,-1,0,1,2]: raise TestFailed, 'list sort'
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000204def revcmp(a, b): return cmp(b, a)
205a.sort(revcmp)
206if a <> [2,1,0,-1,-2]: raise TestFailed, 'list sort with cmp func'
Guido van Rossumd151d341998-02-25 17:51:50 +0000207# The following dumps core in unpatched Python 1.5:
208def myComparison(x,y):
209 return cmp(x%3, y%7)
210z = range(12)
211z.sort(myComparison)
Guido van Rossum85f18201992-11-27 22:53:50 +0000212
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000213# Test extreme cases with long ints
214a = [0,1,2,3,4]
Fred Drake004d5e62000-10-23 17:22:08 +0000215if a[ -pow(2,128L): 3 ] != [0,1,2]:
216 raise TestFailed, "list slicing with too-small long integer"
217if a[ 3: pow(2,145L) ] != [3,4]:
218 raise TestFailed, "list slicing with too-large long integer"
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000219
Guido van Rossum85f18201992-11-27 22:53:50 +0000220print '6.6 Mappings == Dictionaries'
221d = {}
222if d.keys() <> []: raise TestFailed, '{}.keys()'
Guido van Rossumeecf0351994-12-30 17:17:46 +0000223if d.has_key('a') <> 0: raise TestFailed, '{}.has_key(\'a\')'
Guido van Rossum85f18201992-11-27 22:53:50 +0000224if len(d) <> 0: raise TestFailed, 'len({})'
225d = {'a': 1, 'b': 2}
226if len(d) <> 2: raise TestFailed, 'len(dict)'
227k = d.keys()
228k.sort()
229if k <> ['a', 'b']: raise TestFailed, 'dict keys()'
230if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass
231else: raise TestFailed, 'dict keys()'
232if d['a'] <> 1 or d['b'] <> 2: raise TestFailed, 'dict item'
233d['c'] = 3
234d['a'] = 4
235if d['c'] <> 3 or d['a'] <> 4: raise TestFailed, 'dict item assignment'
236del d['b']
237if d <> {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion'
Guido van Rossumce1fa261997-06-02 23:14:00 +0000238d = {1:1, 2:2, 3:3}
239d.clear()
240if d != {}: raise TestFailed, 'dict clear'
241d.update({1:100})
242d.update({2:20})
243d.update({1:1, 2:2, 3:3})
244if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update'
245if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'
246if {}.copy() != {}: raise TestFailed, 'empty dict copy'
Barry Warsaw9b887c71997-10-20 17:34:43 +0000247# dict.get()
Guido van Rossumfb5cef11997-10-20 20:10:43 +0000248d = {}
249if d.get('c') != None: raise TestFailed, 'missing {} get, no 2nd arg'
250if d.get('c', 3) != 3: raise TestFailed, 'missing {} get, w/ 2nd arg'
Barry Warsaw9b887c71997-10-20 17:34:43 +0000251d = {'a' : 1, 'b' : 2}
252if d.get('c') != None: raise TestFailed, 'missing dict get, no 2nd arg'
253if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg'
254if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg'
255if d.get('a', 3) != 1: raise TestFailed, 'present dict get, w/ 2nd arg'
Guido van Rossum79c9b172000-08-08 16:13:23 +0000256# dict.setdefault()
257d = {}
258if d.setdefault('key0') <> None:
Fred Drake004d5e62000-10-23 17:22:08 +0000259 raise TestFailed, 'missing {} setdefault, no 2nd arg'
Guido van Rossum79c9b172000-08-08 16:13:23 +0000260if d.setdefault('key0') <> None:
Fred Drake004d5e62000-10-23 17:22:08 +0000261 raise TestFailed, 'present {} setdefault, no 2nd arg'
Guido van Rossum79c9b172000-08-08 16:13:23 +0000262d.setdefault('key', []).append(3)
263if d['key'][0] <> 3:
Fred Drake004d5e62000-10-23 17:22:08 +0000264 raise TestFailed, 'missing {} setdefault, w/ 2nd arg'
Guido van Rossum79c9b172000-08-08 16:13:23 +0000265d.setdefault('key', []).append(4)
266if len(d['key']) <> 2:
Fred Drake004d5e62000-10-23 17:22:08 +0000267 raise TestFailed, 'present {} setdefault, w/ 2nd arg'