blob: c0c1c88490faa64badb458e9ff4169c3be531b9d [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'
Guido van Rossum80530ce1993-01-21 15:36:40 +000074print '6.4.2 Long integers'
Fred Drake132dce22000-12-12 23:11:42 +000075if 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'
Guido van Rossum80530ce1993-01-21 15:36:40 +000079if 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'
Fred Drake132dce22000-12-12 23:11:42 +000094if 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'
Guido van Rossum80530ce1993-01-21 15:36:40 +000098if 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'
Fred Drake132dce22000-12-12 23:11:42 +0000104if 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'
Guido van Rossum85f18201992-11-27 22:53:50 +0000111if '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'
Fred Drake132dce22000-12-12 23:11:42 +0000117if 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'
Guido van Rossum85f18201992-11-27 22:53:50 +0000124if 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'
Fred Drake132dce22000-12-12 23:11:42 +0000128if 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'
133if [1,2]*3L != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3L'
134if 0*[1,2,3] != []: raise TestFailed, 'list repetition 0*'
135if 0L*[1,2,3] != []: raise TestFailed, 'list repetition 0L*'
136if min([1,2]) != 1 or max([1,2]) != 2: raise TestFailed, 'min/max list'
Guido van Rossum85f18201992-11-27 22:53:50 +0000137if 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
Fred Drake132dce22000-12-12 23:11:42 +0000158if 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
Fred Drake132dce22000-12-12 23:11:42 +0000162if 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
Fred Drake132dce22000-12-12 23:11:42 +0000165if 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
Fred Drake132dce22000-12-12 23:11:42 +0000168if a != [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]'
Guido van Rossum85f18201992-11-27 22:53:50 +0000169a[:2] = [0,4]
170a[-3:] = []
171a[1:1] = [1,2,3]
Fred Drake132dce22000-12-12 23:11:42 +0000172if 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]
Fred Drake132dce22000-12-12 23:11:42 +0000174if 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]
Fred Drake132dce22000-12-12 23:11:42 +0000176if a != [0,4]: raise TestFailed, 'list slice deletion'
Guido van Rossum85f18201992-11-27 22:53:50 +0000177del a[0]
Fred Drake132dce22000-12-12 23:11:42 +0000178if a != [4]: raise TestFailed, 'list item deletion [0]'
Guido van Rossum85f18201992-11-27 22:53:50 +0000179del a[-1]
Fred Drake132dce22000-12-12 23:11:42 +0000180if a != []: raise TestFailed, 'list item deletion [-1]'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000181a=range(0,5)
182del a[1L:4L]
Fred Drake132dce22000-12-12 23:11:42 +0000183if a != [0,4]: raise TestFailed, 'list slice deletion'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000184del a[0L]
Fred Drake132dce22000-12-12 23:11:42 +0000185if a != [4]: raise TestFailed, 'list item deletion [0]'
Andrew M. Kuchling5ebfa2a2000-02-23 22:23:17 +0000186del a[-1L]
Fred Drake132dce22000-12-12 23:11:42 +0000187if a != []: raise TestFailed, 'list item deletion [-1]'
Guido van Rossum85f18201992-11-27 22:53:50 +0000188a.append(0)
189a.append(1)
190a.append(2)
Fred Drake132dce22000-12-12 23:11:42 +0000191if a != [0,1,2]: raise TestFailed, 'list append'
Guido van Rossum85f18201992-11-27 22:53:50 +0000192a.insert(0, -2)
193a.insert(1, -1)
194a.insert(2,0)
Fred Drake132dce22000-12-12 23:11:42 +0000195if 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'
Guido van Rossum85f18201992-11-27 22:53:50 +0000198a.remove(0)
Fred Drake132dce22000-12-12 23:11:42 +0000199if a != [-2,-1,0,1,2]: raise TestFailed, 'list remove'
Guido van Rossum85f18201992-11-27 22:53:50 +0000200a.reverse()
Fred Drake132dce22000-12-12 23:11:42 +0000201if a != [2,1,0,-1,-2]: raise TestFailed, 'list reverse'
Guido van Rossum85f18201992-11-27 22:53:50 +0000202a.sort()
Fred Drake132dce22000-12-12 23:11:42 +0000203if 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)
Fred Drake132dce22000-12-12 23:11:42 +0000206if 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 = {}
Fred Drake132dce22000-12-12 23:11:42 +0000222if d.keys() != []: raise TestFailed, '{}.keys()'
223if d.has_key('a') != 0: raise TestFailed, '{}.has_key(\'a\')'
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +0000224if ('a' in d) != 0: raise TestFailed, "'a' in {}"
225if ('a' not in d) != 1: raise TestFailed, "'a' not in {}"
Fred Drake132dce22000-12-12 23:11:42 +0000226if len(d) != 0: raise TestFailed, 'len({})'
Guido van Rossum85f18201992-11-27 22:53:50 +0000227d = {'a': 1, 'b': 2}
Fred Drake132dce22000-12-12 23:11:42 +0000228if len(d) != 2: raise TestFailed, 'len(dict)'
Guido van Rossum85f18201992-11-27 22:53:50 +0000229k = d.keys()
230k.sort()
Fred Drake132dce22000-12-12 23:11:42 +0000231if k != ['a', 'b']: raise TestFailed, 'dict keys()'
Guido van Rossum85f18201992-11-27 22:53:50 +0000232if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass
233else: raise TestFailed, 'dict keys()'
Guido van Rossum0dbb4fb2001-04-20 16:50:40 +0000234if 'a' in d and 'b' in d and 'c' not in d: pass
235else: raise TestFailed, 'dict keys() # in/not in version'
Fred Drake132dce22000-12-12 23:11:42 +0000236if d['a'] != 1 or d['b'] != 2: raise TestFailed, 'dict item'
Guido van Rossum85f18201992-11-27 22:53:50 +0000237d['c'] = 3
238d['a'] = 4
Fred Drake132dce22000-12-12 23:11:42 +0000239if d['c'] != 3 or d['a'] != 4: raise TestFailed, 'dict item assignment'
Guido van Rossum85f18201992-11-27 22:53:50 +0000240del d['b']
Fred Drake132dce22000-12-12 23:11:42 +0000241if d != {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion'
Barry Warsaw41775382001-06-26 20:09:28 +0000242# dict.clear()
Guido van Rossumce1fa261997-06-02 23:14:00 +0000243d = {1:1, 2:2, 3:3}
244d.clear()
245if d != {}: raise TestFailed, 'dict clear'
Barry Warsaw41775382001-06-26 20:09:28 +0000246# dict.update()
Guido van Rossumce1fa261997-06-02 23:14:00 +0000247d.update({1:100})
248d.update({2:20})
249d.update({1:1, 2:2, 3:3})
250if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update'
Barry Warsaw41775382001-06-26 20:09:28 +0000251d.clear()
252try: d.update(None)
253except AttributeError: pass
254else: raise TestFailed, 'dict.update(None), AttributeError expected'
255class SimpleUserDict:
256 def __init__(self):
257 self.d = {1:1, 2:2, 3:3}
258 def keys(self):
259 return self.d.keys()
260 def __getitem__(self, i):
261 return self.d[i]
262d.update(SimpleUserDict())
263if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict.update(instance)'
264d.clear()
265class FailingUserDict:
266 def keys(self):
267 raise ValueError
268try: d.update(FailingUserDict())
269except ValueError: pass
270else: raise TestFailed, 'dict.keys() expected ValueError'
271class FailingUserDict:
272 def keys(self):
273 class BogonIter:
274 def __iter__(self):
275 raise ValueError
276 return BogonIter()
277try: d.update(FailingUserDict())
278except ValueError: pass
279else: raise TestFailed, 'iter(dict.keys()) expected ValueError'
280class FailingUserDict:
281 def keys(self):
282 class BogonIter:
283 def __init__(self):
284 self.i = 1
285 def __iter__(self):
286 return self
287 def next(self):
288 if self.i:
289 self.i = 0
290 return 'a'
291 raise ValueError
292 return BogonIter()
293 def __getitem__(self, key):
294 return key
295try: d.update(FailingUserDict())
296except ValueError: pass
297else: raise TestFailed, 'iter(dict.keys()).next() expected ValueError'
298class FailingUserDict:
299 def keys(self):
300 class BogonIter:
301 def __init__(self):
302 self.i = ord('a')
303 def __iter__(self):
304 return self
305 def next(self):
306 if self.i <= ord('z'):
307 rtn = chr(self.i)
308 self.i += 1
309 return rtn
310 raise StopIteration
311 return BogonIter()
312 def __getitem__(self, key):
313 raise ValueError
314try: d.update(FailingUserDict())
315except ValueError: pass
316else: raise TestFailed, 'dict.update(), __getitem__ expected ValueError'
317# dict.copy()
318d = {1:1, 2:2, 3:3}
Guido van Rossumce1fa261997-06-02 23:14:00 +0000319if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'
320if {}.copy() != {}: raise TestFailed, 'empty dict copy'
Barry Warsaw9b887c71997-10-20 17:34:43 +0000321# dict.get()
Guido van Rossumfb5cef11997-10-20 20:10:43 +0000322d = {}
Fred Drake132dce22000-12-12 23:11:42 +0000323if d.get('c') is not None: raise TestFailed, 'missing {} get, no 2nd arg'
Guido van Rossumfb5cef11997-10-20 20:10:43 +0000324if d.get('c', 3) != 3: raise TestFailed, 'missing {} get, w/ 2nd arg'
Barry Warsaw9b887c71997-10-20 17:34:43 +0000325d = {'a' : 1, 'b' : 2}
Fred Drake132dce22000-12-12 23:11:42 +0000326if d.get('c') is not None: raise TestFailed, 'missing dict get, no 2nd arg'
Barry Warsaw9b887c71997-10-20 17:34:43 +0000327if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg'
328if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg'
329if d.get('a', 3) != 1: raise TestFailed, 'present dict get, w/ 2nd arg'
Guido van Rossum79c9b172000-08-08 16:13:23 +0000330# dict.setdefault()
331d = {}
Fred Drake132dce22000-12-12 23:11:42 +0000332if d.setdefault('key0') is not None:
Fred Drake004d5e62000-10-23 17:22:08 +0000333 raise TestFailed, 'missing {} setdefault, no 2nd arg'
Fred Drake132dce22000-12-12 23:11:42 +0000334if d.setdefault('key0') is not None:
Fred Drake004d5e62000-10-23 17:22:08 +0000335 raise TestFailed, 'present {} setdefault, no 2nd arg'
Guido van Rossum79c9b172000-08-08 16:13:23 +0000336d.setdefault('key', []).append(3)
Fred Drake132dce22000-12-12 23:11:42 +0000337if d['key'][0] != 3:
Fred Drake004d5e62000-10-23 17:22:08 +0000338 raise TestFailed, 'missing {} setdefault, w/ 2nd arg'
Guido van Rossum79c9b172000-08-08 16:13:23 +0000339d.setdefault('key', []).append(4)
Fred Drake132dce22000-12-12 23:11:42 +0000340if len(d['key']) != 2:
Fred Drake004d5e62000-10-23 17:22:08 +0000341 raise TestFailed, 'present {} setdefault, w/ 2nd arg'
Guido van Rossumb822c612000-12-12 22:02:59 +0000342# dict.popitem()
343for copymode in -1, +1:
344 # -1: b has same structure as a
345 # +1: b is a.copy()
346 for log2size in range(12):
347 size = 2**log2size
348 a = {}
349 b = {}
350 for i in range(size):
351 a[`i`] = i
352 if copymode < 0:
353 b[`i`] = i
354 if copymode > 0:
355 b = a.copy()
356 for i in range(size):
357 ka, va = ta = a.popitem()
358 if va != int(ka): raise TestFailed, "a.popitem: %s" % str(ta)
359 kb, vb = tb = b.popitem()
360 if vb != int(kb): raise TestFailed, "b.popitem: %s" % str(tb)
361 if copymode < 0 and ta != tb:
362 raise TestFailed, "a.popitem != b.popitem: %s, %s" % (
363 str(ta), str(tb))
364 if a: raise TestFailed, 'a not empty after popitems: %s' % str(a)
365 if b: raise TestFailed, 'b not empty after popitems: %s' % str(b)