blob: e6f440f171a0460bcca61b7c5308b47f9b4fe58b [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'
Fred Drake132dce22000-12-12 23:11:42 +000039if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
Guido van Rossum85f18201992-11-27 22:53:50 +000040else: raise TestFailed, 'int comparisons failed'
Fred Drake132dce22000-12-12 23:11:42 +000041if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass
Guido van Rossum85f18201992-11-27 22:53:50 +000042else: raise TestFailed, 'long int comparisons failed'
Fred Drake132dce22000-12-12 23:11:42 +000043if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass
Guido van Rossum85f18201992-11-27 22:53:50 +000044else: 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)'
Fred Drake132dce22000-12-12 23:11:42 +000053if 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'
Fred Drake132dce22000-12-12 23:11:42 +000064if 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'
Guido van Rossum80530ce1993-01-21 15:36:40 +000068if 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'
Tim Petersa3c01ce2001-12-04 23:05:10 +000074# And another.
75m = -sys.maxint - 1
76for divisor in 1, 2, 4, 8, 16, 32:
Tim Peters6d30c3e2001-12-05 00:30:09 +000077 j = m // divisor
Tim Petersa3c01ce2001-12-04 23:05:10 +000078 prod = divisor * j
79 if prod != m:
80 raise TestFailed, "%r * %r == %r != %r" % (divisor, j, prod, m)
81 if type(prod) is not int:
82 raise TestFailed, ("expected type(prod) to be int, not %r" %
83 type(prod))
84# Check for expected * overflow to long.
85for divisor in 1, 2, 4, 8, 16, 32:
Tim Peters6d30c3e2001-12-05 00:30:09 +000086 j = m // divisor - 1
Tim Petersa3c01ce2001-12-04 23:05:10 +000087 prod = divisor * j
88 if type(prod) is not long:
89 raise TestFailed, ("expected type(%r) to be long, not %r" %
90 (prod, type(prod)))
91# Check for expected * overflow to long.
92m = sys.maxint
93for divisor in 1, 2, 4, 8, 16, 32:
Tim Peters6d30c3e2001-12-05 00:30:09 +000094 j = m // divisor + 1
Tim Petersa3c01ce2001-12-04 23:05:10 +000095 prod = divisor * j
96 if type(prod) is not long:
97 raise TestFailed, ("expected type(%r) to be long, not %r" %
98 (prod, type(prod)))
99
Guido van Rossum80530ce1993-01-21 15:36:40 +0000100print '6.4.2 Long integers'
Fred Drake132dce22000-12-12 23:11:42 +0000101if 12L + 24L != 36L: raise TestFailed, 'long op'
102if 12L + (-24L) != -12L: raise TestFailed, 'long op'
103if (-12L) + 24L != 12L: raise TestFailed, 'long op'
104if (-12L) + (-24L) != -36L: raise TestFailed, 'long op'
Guido van Rossum80530ce1993-01-21 15:36:40 +0000105if not 12L < 24L: raise TestFailed, 'long op'
106if not -24L < -12L: raise TestFailed, 'long op'
Guido van Rossum74629421998-05-26 14:51:55 +0000107x = sys.maxint
108if int(long(x)) != x: raise TestFailed, 'long op'
109try: int(long(x)+1L)
110except OverflowError: pass
111else:raise TestFailed, 'long op'
112x = -x
113if int(long(x)) != x: raise TestFailed, 'long op'
114x = x-1
115if int(long(x)) != x: raise TestFailed, 'long op'
116try: int(long(x)-1L)
117except OverflowError: pass
118else:raise TestFailed, 'long op'
Guido van Rossum80530ce1993-01-21 15:36:40 +0000119print '6.4.3 Floating point numbers'
Fred Drake132dce22000-12-12 23:11:42 +0000120if 12.0 + 24.0 != 36.0: raise TestFailed, 'float op'
121if 12.0 + (-24.0) != -12.0: raise TestFailed, 'float op'
122if (-12.0) + 24.0 != 12.0: raise TestFailed, 'float op'
123if (-12.0) + (-24.0) != -36.0: raise TestFailed, 'float op'
Guido van Rossum80530ce1993-01-21 15:36:40 +0000124if not 12.0 < 24.0: raise TestFailed, 'float op'
125if not -24.0 < -12.0: raise TestFailed, 'float op'
Guido van Rossum85f18201992-11-27 22:53:50 +0000126
127print '6.5 Sequence types'
128
129print '6.5.1 Strings'
Fred Drake132dce22000-12-12 23:11:42 +0000130if len('') != 0: raise TestFailed, 'len(\'\')'
131if len('a') != 1: raise TestFailed, 'len(\'a\')'
132if len('abcdef') != 6: raise TestFailed, 'len(\'abcdef\')'
133if 'xyz' + 'abcde' != 'xyzabcde': raise TestFailed, 'string concatenation'
134if 'xyz'*3 != 'xyzxyzxyz': raise TestFailed, 'string repetition *3'
135if 0*'abcde' != '': raise TestFailed, 'string repetition 0*'
136if min('abc') != 'a' or max('abc') != 'c': raise TestFailed, 'min/max string'
Guido van Rossum85f18201992-11-27 22:53:50 +0000137if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
138else: raise TestFailed, 'in/not in string'
Guido van Rossumb6775db1994-08-01 11:34:53 +0000139x = 'x'*103
140if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug'
Guido van Rossum85f18201992-11-27 22:53:50 +0000141
142print '6.5.2 Tuples'
Fred Drake132dce22000-12-12 23:11:42 +0000143if len(()) != 0: raise TestFailed, 'len(())'
144if len((1,)) != 1: raise TestFailed, 'len((1,))'
145if len((1,2,3,4,5,6)) != 6: raise TestFailed, 'len((1,2,3,4,5,6))'
146if (1,2)+(3,4) != (1,2,3,4): raise TestFailed, 'tuple concatenation'
147if (1,2)*3 != (1,2,1,2,1,2): raise TestFailed, 'tuple repetition *3'
148if 0*(1,2,3) != (): raise TestFailed, 'tuple repetition 0*'
149if min((1,2)) != 1 or max((1,2)) != 2: raise TestFailed, 'min/max tuple'
Guido van Rossum85f18201992-11-27 22:53:50 +0000150if 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
151else: raise TestFailed, 'in/not in tuple'
152
153print '6.5.3 Lists'
Fred Drake132dce22000-12-12 23:11:42 +0000154if len([]) != 0: raise TestFailed, 'len([])'
155if len([1,]) != 1: raise TestFailed, 'len([1,])'
156if len([1,2,3,4,5,6]) != 6: raise TestFailed, 'len([1,2,3,4,5,6])'
157if [1,2]+[3,4] != [1,2,3,4]: raise TestFailed, 'list concatenation'
158if [1,2]*3 != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3'
159if [1,2]*3L != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3L'
160if 0*[1,2,3] != []: raise TestFailed, 'list repetition 0*'
161if 0L*[1,2,3] != []: raise TestFailed, 'list repetition 0L*'
162if min([1,2]) != 1 or max([1,2]) != 2: raise TestFailed, 'min/max list'
Guido van Rossum85f18201992-11-27 22:53:50 +0000163if 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
164else: raise TestFailed, 'in/not in list'
Guido van Rossumaffd77f1998-07-16 15:29:06 +0000165a = [1, 2, 3, 4, 5]
166a[:-1] = a
167if a != [1, 2, 3, 4, 5, 5]:
Fred Drake004d5e62000-10-23 17:22:08 +0000168 raise TestFailed, "list self-slice-assign (head)"
Guido van Rossumaffd77f1998-07-16 15:29:06 +0000169a = [1, 2, 3, 4, 5]
170a[1:] = a
171if a != [1, 1, 2, 3, 4, 5]:
Fred Drake004d5e62000-10-23 17:22:08 +0000172 raise TestFailed, "list self-slice-assign (tail)"
Guido van Rossumaffd77f1998-07-16 15:29:06 +0000173a = [1, 2, 3, 4, 5]
174a[1:-1] = a
175if a != [1, 1, 2, 3, 4, 5, 5]:
Fred Drake004d5e62000-10-23 17:22:08 +0000176 raise TestFailed, "list self-slice-assign (center)"
Guido van Rossumaffd77f1998-07-16 15:29:06 +0000177
Guido van Rossum85f18201992-11-27 22:53:50 +0000178
179print '6.5.3a Additional list operations'
180a = [0,1,2,3,4]
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000181a[0L] = 1
182a[1L] = 2
183a[2L] = 3
Fred Drake132dce22000-12-12 23:11:42 +0000184if a != [1,2,3,3,4]: raise TestFailed, 'list item assignment [0L], [1L], [2L]'
Guido van Rossum85f18201992-11-27 22:53:50 +0000185a[0] = 5
186a[1] = 6
187a[2] = 7
Fred Drake132dce22000-12-12 23:11:42 +0000188if a != [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000189a[-2L] = 88
190a[-1L] = 99
Fred Drake132dce22000-12-12 23:11:42 +0000191if a != [5,6,7,88,99]: raise TestFailed, 'list item assignment [-2L], [-1L]'
Guido van Rossum85f18201992-11-27 22:53:50 +0000192a[-2] = 8
193a[-1] = 9
Fred Drake132dce22000-12-12 23:11:42 +0000194if a != [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]'
Guido van Rossum85f18201992-11-27 22:53:50 +0000195a[:2] = [0,4]
196a[-3:] = []
197a[1:1] = [1,2,3]
Fred Drake132dce22000-12-12 23:11:42 +0000198if a != [0,1,2,3,4]: raise TestFailed, 'list slice assignment'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000199a[ 1L : 4L] = [7,8,9]
Fred Drake132dce22000-12-12 23:11:42 +0000200if a != [0,7,8,9,4]: raise TestFailed, 'list slice assignment using long ints'
Guido van Rossum85f18201992-11-27 22:53:50 +0000201del a[1:4]
Fred Drake132dce22000-12-12 23:11:42 +0000202if a != [0,4]: raise TestFailed, 'list slice deletion'
Guido van Rossum85f18201992-11-27 22:53:50 +0000203del a[0]
Fred Drake132dce22000-12-12 23:11:42 +0000204if a != [4]: raise TestFailed, 'list item deletion [0]'
Guido van Rossum85f18201992-11-27 22:53:50 +0000205del a[-1]
Fred Drake132dce22000-12-12 23:11:42 +0000206if a != []: raise TestFailed, 'list item deletion [-1]'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000207a=range(0,5)
208del a[1L:4L]
Fred Drake132dce22000-12-12 23:11:42 +0000209if a != [0,4]: raise TestFailed, 'list slice deletion'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000210del a[0L]
Fred Drake132dce22000-12-12 23:11:42 +0000211if a != [4]: raise TestFailed, 'list item deletion [0]'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000212del a[-1L]
Fred Drake132dce22000-12-12 23:11:42 +0000213if a != []: raise TestFailed, 'list item deletion [-1]'
Guido van Rossum85f18201992-11-27 22:53:50 +0000214a.append(0)
215a.append(1)
216a.append(2)
Fred Drake132dce22000-12-12 23:11:42 +0000217if a != [0,1,2]: raise TestFailed, 'list append'
Guido van Rossum85f18201992-11-27 22:53:50 +0000218a.insert(0, -2)
219a.insert(1, -1)
220a.insert(2,0)
Fred Drake132dce22000-12-12 23:11:42 +0000221if a != [-2,-1,0,0,1,2]: raise TestFailed, 'list insert'
222if a.count(0) != 2: raise TestFailed, ' list count'
223if a.index(0) != 2: raise TestFailed, 'list index'
Guido van Rossum85f18201992-11-27 22:53:50 +0000224a.remove(0)
Fred Drake132dce22000-12-12 23:11:42 +0000225if a != [-2,-1,0,1,2]: raise TestFailed, 'list remove'
Guido van Rossum85f18201992-11-27 22:53:50 +0000226a.reverse()
Fred Drake132dce22000-12-12 23:11:42 +0000227if a != [2,1,0,-1,-2]: raise TestFailed, 'list reverse'
Guido van Rossum85f18201992-11-27 22:53:50 +0000228a.sort()
Fred Drake132dce22000-12-12 23:11:42 +0000229if a != [-2,-1,0,1,2]: raise TestFailed, 'list sort'
Guido van Rossume61fa0a1993-10-22 13:56:35 +0000230def revcmp(a, b): return cmp(b, a)
231a.sort(revcmp)
Fred Drake132dce22000-12-12 23:11:42 +0000232if a != [2,1,0,-1,-2]: raise TestFailed, 'list sort with cmp func'
Guido van Rossumd151d341998-02-25 17:51:50 +0000233# The following dumps core in unpatched Python 1.5:
234def myComparison(x,y):
235 return cmp(x%3, y%7)
236z = range(12)
237z.sort(myComparison)
Guido van Rossum85f18201992-11-27 22:53:50 +0000238
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000239# Test extreme cases with long ints
240a = [0,1,2,3,4]
Fred Drake004d5e62000-10-23 17:22:08 +0000241if a[ -pow(2,128L): 3 ] != [0,1,2]:
242 raise TestFailed, "list slicing with too-small long integer"
243if a[ 3: pow(2,145L) ] != [3,4]:
244 raise TestFailed, "list slicing with too-large long integer"
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000245
Guido van Rossum85f18201992-11-27 22:53:50 +0000246print '6.6 Mappings == Dictionaries'
247d = {}
Fred Drake132dce22000-12-12 23:11:42 +0000248if d.keys() != []: raise TestFailed, '{}.keys()'
249if d.has_key('a') != 0: raise TestFailed, '{}.has_key(\'a\')'
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +0000250if ('a' in d) != 0: raise TestFailed, "'a' in {}"
251if ('a' not in d) != 1: raise TestFailed, "'a' not in {}"
Fred Drake132dce22000-12-12 23:11:42 +0000252if len(d) != 0: raise TestFailed, 'len({})'
Guido van Rossum85f18201992-11-27 22:53:50 +0000253d = {'a': 1, 'b': 2}
Fred Drake132dce22000-12-12 23:11:42 +0000254if len(d) != 2: raise TestFailed, 'len(dict)'
Guido van Rossum85f18201992-11-27 22:53:50 +0000255k = d.keys()
256k.sort()
Fred Drake132dce22000-12-12 23:11:42 +0000257if k != ['a', 'b']: raise TestFailed, 'dict keys()'
Guido van Rossum85f18201992-11-27 22:53:50 +0000258if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass
259else: raise TestFailed, 'dict keys()'
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +0000260if 'a' in d and 'b' in d and 'c' not in d: pass
261else: raise TestFailed, 'dict keys() # in/not in version'
Fred Drake132dce22000-12-12 23:11:42 +0000262if d['a'] != 1 or d['b'] != 2: raise TestFailed, 'dict item'
Guido van Rossum85f18201992-11-27 22:53:50 +0000263d['c'] = 3
264d['a'] = 4
Fred Drake132dce22000-12-12 23:11:42 +0000265if d['c'] != 3 or d['a'] != 4: raise TestFailed, 'dict item assignment'
Guido van Rossum85f18201992-11-27 22:53:50 +0000266del d['b']
Fred Drake132dce22000-12-12 23:11:42 +0000267if d != {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion'
Barry Warsaw41775382001-06-26 20:09:28 +0000268# dict.clear()
Guido van Rossumce1fa261997-06-02 23:14:00 +0000269d = {1:1, 2:2, 3:3}
270d.clear()
271if d != {}: raise TestFailed, 'dict clear'
Barry Warsaw41775382001-06-26 20:09:28 +0000272# dict.update()
Guido van Rossumce1fa261997-06-02 23:14:00 +0000273d.update({1:100})
274d.update({2:20})
275d.update({1:1, 2:2, 3:3})
276if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update'
Barry Warsaw41775382001-06-26 20:09:28 +0000277d.clear()
278try: d.update(None)
279except AttributeError: pass
280else: raise TestFailed, 'dict.update(None), AttributeError expected'
281class SimpleUserDict:
282 def __init__(self):
283 self.d = {1:1, 2:2, 3:3}
284 def keys(self):
285 return self.d.keys()
286 def __getitem__(self, i):
287 return self.d[i]
288d.update(SimpleUserDict())
289if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict.update(instance)'
290d.clear()
291class FailingUserDict:
292 def keys(self):
293 raise ValueError
294try: d.update(FailingUserDict())
295except ValueError: pass
296else: raise TestFailed, 'dict.keys() expected ValueError'
297class FailingUserDict:
298 def keys(self):
299 class BogonIter:
300 def __iter__(self):
301 raise ValueError
302 return BogonIter()
303try: d.update(FailingUserDict())
304except ValueError: pass
305else: raise TestFailed, 'iter(dict.keys()) expected ValueError'
306class FailingUserDict:
307 def keys(self):
308 class BogonIter:
309 def __init__(self):
310 self.i = 1
311 def __iter__(self):
312 return self
313 def next(self):
314 if self.i:
315 self.i = 0
316 return 'a'
317 raise ValueError
318 return BogonIter()
319 def __getitem__(self, key):
320 return key
321try: d.update(FailingUserDict())
322except ValueError: pass
323else: raise TestFailed, 'iter(dict.keys()).next() expected ValueError'
324class FailingUserDict:
325 def keys(self):
326 class BogonIter:
327 def __init__(self):
328 self.i = ord('a')
329 def __iter__(self):
330 return self
331 def next(self):
332 if self.i <= ord('z'):
333 rtn = chr(self.i)
334 self.i += 1
335 return rtn
336 raise StopIteration
337 return BogonIter()
338 def __getitem__(self, key):
339 raise ValueError
340try: d.update(FailingUserDict())
341except ValueError: pass
342else: raise TestFailed, 'dict.update(), __getitem__ expected ValueError'
343# dict.copy()
344d = {1:1, 2:2, 3:3}
Guido van Rossumce1fa261997-06-02 23:14:00 +0000345if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'
346if {}.copy() != {}: raise TestFailed, 'empty dict copy'
Barry Warsaw9b887c71997-10-20 17:34:43 +0000347# dict.get()
Guido van Rossumfb5cef11997-10-20 20:10:43 +0000348d = {}
Fred Drake132dce22000-12-12 23:11:42 +0000349if d.get('c') is not None: raise TestFailed, 'missing {} get, no 2nd arg'
Guido van Rossumfb5cef11997-10-20 20:10:43 +0000350if d.get('c', 3) != 3: raise TestFailed, 'missing {} get, w/ 2nd arg'
Barry Warsaw9b887c71997-10-20 17:34:43 +0000351d = {'a' : 1, 'b' : 2}
Fred Drake132dce22000-12-12 23:11:42 +0000352if d.get('c') is not None: raise TestFailed, 'missing dict get, no 2nd arg'
Barry Warsaw9b887c71997-10-20 17:34:43 +0000353if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg'
354if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg'
355if d.get('a', 3) != 1: raise TestFailed, 'present dict get, w/ 2nd arg'
Guido van Rossum79c9b172000-08-08 16:13:23 +0000356# dict.setdefault()
357d = {}
Fred Drake132dce22000-12-12 23:11:42 +0000358if d.setdefault('key0') is not None:
Fred Drake004d5e62000-10-23 17:22:08 +0000359 raise TestFailed, 'missing {} setdefault, no 2nd arg'
Fred Drake132dce22000-12-12 23:11:42 +0000360if d.setdefault('key0') is not None:
Fred Drake004d5e62000-10-23 17:22:08 +0000361 raise TestFailed, 'present {} setdefault, no 2nd arg'
Guido van Rossum79c9b172000-08-08 16:13:23 +0000362d.setdefault('key', []).append(3)
Fred Drake132dce22000-12-12 23:11:42 +0000363if d['key'][0] != 3:
Fred Drake004d5e62000-10-23 17:22:08 +0000364 raise TestFailed, 'missing {} setdefault, w/ 2nd arg'
Guido van Rossum79c9b172000-08-08 16:13:23 +0000365d.setdefault('key', []).append(4)
Fred Drake132dce22000-12-12 23:11:42 +0000366if len(d['key']) != 2:
Fred Drake004d5e62000-10-23 17:22:08 +0000367 raise TestFailed, 'present {} setdefault, w/ 2nd arg'
Guido van Rossumb822c612000-12-12 22:02:59 +0000368# dict.popitem()
369for copymode in -1, +1:
370 # -1: b has same structure as a
371 # +1: b is a.copy()
372 for log2size in range(12):
373 size = 2**log2size
374 a = {}
375 b = {}
376 for i in range(size):
377 a[`i`] = i
378 if copymode < 0:
379 b[`i`] = i
380 if copymode > 0:
381 b = a.copy()
382 for i in range(size):
383 ka, va = ta = a.popitem()
384 if va != int(ka): raise TestFailed, "a.popitem: %s" % str(ta)
385 kb, vb = tb = b.popitem()
386 if vb != int(kb): raise TestFailed, "b.popitem: %s" % str(tb)
387 if copymode < 0 and ta != tb:
388 raise TestFailed, "a.popitem != b.popitem: %s, %s" % (
389 str(ta), str(tb))
390 if a: raise TestFailed, 'a not empty after popitems: %s' % str(a)
391 if b: raise TestFailed, 'b not empty after popitems: %s' % str(b)
Guido van Rossum29d26062001-12-11 04:37:34 +0000392
393try: type(1, 2)
394except TypeError: pass
395else: raise TestFailed, 'type(), w/2 args expected TypeError'
396
397try: type(1, 2, 3, 4)
398except TypeError: pass
399else: raise TestFailed, 'type(), w/4 args expected TypeError'
400