append(): Fixing the test for convertability after consultation with
[python/dscho.git] / Lib / test / test_types.py
bloba38eb7f7d4dd18e05a3f215468c8d6d93cd6271e
1 # Python test set -- part 6, built-in types
3 from test.test_support import *
5 print '6. Built-in types'
7 print '6.1 Truth value testing'
8 if None: raise TestFailed, 'None is true instead of false'
9 if 0: raise TestFailed, '0 is true instead of false'
10 if 0L: raise TestFailed, '0L is true instead of false'
11 if 0.0: raise TestFailed, '0.0 is true instead of false'
12 if '': raise TestFailed, '\'\' is true instead of false'
13 if (): raise TestFailed, '() is true instead of false'
14 if []: raise TestFailed, '[] is true instead of false'
15 if {}: raise TestFailed, '{} is true instead of false'
16 if not 1: raise TestFailed, '1 is false instead of true'
17 if not 1L: raise TestFailed, '1L is false instead of true'
18 if not 1.0: raise TestFailed, '1.0 is false instead of true'
19 if not 'x': raise TestFailed, '\'x\' is false instead of true'
20 if not (1, 1): raise TestFailed, '(1, 1) is false instead of true'
21 if not [1]: raise TestFailed, '[1] is false instead of true'
22 if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true'
23 def f(): pass
24 class C: pass
25 import sys
26 x = C()
27 if not f: raise TestFailed, 'f is false instead of true'
28 if not C: raise TestFailed, 'C is false instead of true'
29 if not sys: raise TestFailed, 'sys is false instead of true'
30 if not x: raise TestFailed, 'x is false instead of true'
32 print '6.2 Boolean operations'
33 if 0 or 0: raise TestFailed, '0 or 0 is true instead of false'
34 if 1 and 1: pass
35 else: raise TestFailed, '1 and 1 is false instead of false'
36 if not 1: raise TestFailed, 'not 1 is true instead of false'
38 print '6.3 Comparisons'
39 if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
40 else: raise TestFailed, 'int comparisons failed'
41 if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass
42 else: raise TestFailed, 'long int comparisons failed'
43 if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass
44 else: raise TestFailed, 'float comparisons failed'
45 if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass
46 else: raise TestFailed, 'string comparisons failed'
47 if 0 in [0] and 0 not in [1]: pass
48 else: raise TestFailed, 'membership test failed'
49 if None is None and [] is not []: pass
50 else: raise TestFailed, 'identity test failed'
52 try: float('')
53 except ValueError: pass
54 else: raise TestFailed, "float('') didn't raise ValueError"
56 try: float('5\0')
57 except ValueError: pass
58 else: raise TestFailed, "float('5\0') didn't raise ValueError"
60 try: 5.0 / 0.0
61 except ZeroDivisionError: pass
62 else: raise TestFailed, "5.0 / 0.0 didn't raise ZeroDivisionError"
64 try: 5.0 // 0.0
65 except ZeroDivisionError: pass
66 else: raise TestFailed, "5.0 // 0.0 didn't raise ZeroDivisionError"
68 try: 5.0 % 0.0
69 except ZeroDivisionError: pass
70 else: raise TestFailed, "5.0 % 0.0 didn't raise ZeroDivisionError"
72 try: 5 / 0L
73 except ZeroDivisionError: pass
74 else: raise TestFailed, "5 / 0L didn't raise ZeroDivisionError"
76 try: 5 // 0L
77 except ZeroDivisionError: pass
78 else: raise TestFailed, "5 // 0L didn't raise ZeroDivisionError"
80 try: 5 % 0L
81 except ZeroDivisionError: pass
82 else: raise TestFailed, "5 % 0L didn't raise ZeroDivisionError"
84 print '6.4 Numeric types (mostly conversions)'
85 if 0 != 0L or 0 != 0.0 or 0L != 0.0: raise TestFailed, 'mixed comparisons'
86 if 1 != 1L or 1 != 1.0 or 1L != 1.0: raise TestFailed, 'mixed comparisons'
87 if -1 != -1L or -1 != -1.0 or -1L != -1.0:
88 raise TestFailed, 'int/long/float value not equal'
89 if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
90 else: raise TestFailed, 'int() does not round properly'
91 if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass
92 else: raise TestFailed, 'long() does not round properly'
93 if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
94 else: raise TestFailed, 'float() does not work properly'
95 print '6.4.1 32-bit integers'
96 if 12 + 24 != 36: raise TestFailed, 'int op'
97 if 12 + (-24) != -12: raise TestFailed, 'int op'
98 if (-12) + 24 != 12: raise TestFailed, 'int op'
99 if (-12) + (-24) != -36: raise TestFailed, 'int op'
100 if not 12 < 24: raise TestFailed, 'int op'
101 if not -24 < -12: raise TestFailed, 'int op'
102 # Test for a particular bug in integer multiply
103 xsize, ysize, zsize = 238, 356, 4
104 if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912):
105 raise TestFailed, 'int mul commutativity'
106 # And another.
107 m = -sys.maxint - 1
108 for divisor in 1, 2, 4, 8, 16, 32:
109 j = m // divisor
110 prod = divisor * j
111 if prod != m:
112 raise TestFailed, "%r * %r == %r != %r" % (divisor, j, prod, m)
113 if type(prod) is not int:
114 raise TestFailed, ("expected type(prod) to be int, not %r" %
115 type(prod))
116 # Check for expected * overflow to long.
117 for divisor in 1, 2, 4, 8, 16, 32:
118 j = m // divisor - 1
119 prod = divisor * j
120 if type(prod) is not long:
121 raise TestFailed, ("expected type(%r) to be long, not %r" %
122 (prod, type(prod)))
123 # Check for expected * overflow to long.
124 m = sys.maxint
125 for divisor in 1, 2, 4, 8, 16, 32:
126 j = m // divisor + 1
127 prod = divisor * j
128 if type(prod) is not long:
129 raise TestFailed, ("expected type(%r) to be long, not %r" %
130 (prod, type(prod)))
132 print '6.4.2 Long integers'
133 if 12L + 24L != 36L: raise TestFailed, 'long op'
134 if 12L + (-24L) != -12L: raise TestFailed, 'long op'
135 if (-12L) + 24L != 12L: raise TestFailed, 'long op'
136 if (-12L) + (-24L) != -36L: raise TestFailed, 'long op'
137 if not 12L < 24L: raise TestFailed, 'long op'
138 if not -24L < -12L: raise TestFailed, 'long op'
139 x = sys.maxint
140 if int(long(x)) != x: raise TestFailed, 'long op'
141 try: int(long(x)+1L)
142 except OverflowError: pass
143 else:raise TestFailed, 'long op'
144 x = -x
145 if int(long(x)) != x: raise TestFailed, 'long op'
146 x = x-1
147 if int(long(x)) != x: raise TestFailed, 'long op'
148 try: int(long(x)-1L)
149 except OverflowError: pass
150 else:raise TestFailed, 'long op'
152 try: 5 << -5
153 except ValueError: pass
154 else: raise TestFailed, 'int negative shift <<'
156 try: 5L << -5L
157 except ValueError: pass
158 else: raise TestFailed, 'long negative shift <<'
160 try: 5 >> -5
161 except ValueError: pass
162 else: raise TestFailed, 'int negative shift >>'
164 try: 5L >> -5L
165 except ValueError: pass
166 else: raise TestFailed, 'long negative shift >>'
168 print '6.4.3 Floating point numbers'
169 if 12.0 + 24.0 != 36.0: raise TestFailed, 'float op'
170 if 12.0 + (-24.0) != -12.0: raise TestFailed, 'float op'
171 if (-12.0) + 24.0 != 12.0: raise TestFailed, 'float op'
172 if (-12.0) + (-24.0) != -36.0: raise TestFailed, 'float op'
173 if not 12.0 < 24.0: raise TestFailed, 'float op'
174 if not -24.0 < -12.0: raise TestFailed, 'float op'
176 print '6.5 Sequence types'
178 print '6.5.1 Strings'
179 if len('') != 0: raise TestFailed, 'len(\'\')'
180 if len('a') != 1: raise TestFailed, 'len(\'a\')'
181 if len('abcdef') != 6: raise TestFailed, 'len(\'abcdef\')'
182 if 'xyz' + 'abcde' != 'xyzabcde': raise TestFailed, 'string concatenation'
183 if 'xyz'*3 != 'xyzxyzxyz': raise TestFailed, 'string repetition *3'
184 if 0*'abcde' != '': raise TestFailed, 'string repetition 0*'
185 if min('abc') != 'a' or max('abc') != 'c': raise TestFailed, 'min/max string'
186 if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
187 else: raise TestFailed, 'in/not in string'
188 x = 'x'*103
189 if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug'
191 #extended slices for strings
192 a = '0123456789'
193 vereq(a[::], a)
194 vereq(a[::2], '02468')
195 vereq(a[1::2], '13579')
196 vereq(a[::-1],'9876543210')
197 vereq(a[::-2], '97531')
198 vereq(a[3::-2], '31')
199 vereq(a[-100:100:], a)
200 vereq(a[100:-100:-1], a[::-1])
201 vereq(a[-100L:100L:2L], '02468')
203 if have_unicode:
204 a = unicode('0123456789', 'ascii')
205 vereq(a[::], a)
206 vereq(a[::2], unicode('02468', 'ascii'))
207 vereq(a[1::2], unicode('13579', 'ascii'))
208 vereq(a[::-1], unicode('9876543210', 'ascii'))
209 vereq(a[::-2], unicode('97531', 'ascii'))
210 vereq(a[3::-2], unicode('31', 'ascii'))
211 vereq(a[-100:100:], a)
212 vereq(a[100:-100:-1], a[::-1])
213 vereq(a[-100L:100L:2L], unicode('02468', 'ascii'))
216 print '6.5.2 Tuples'
217 if len(()) != 0: raise TestFailed, 'len(())'
218 if len((1,)) != 1: raise TestFailed, 'len((1,))'
219 if len((1,2,3,4,5,6)) != 6: raise TestFailed, 'len((1,2,3,4,5,6))'
220 if (1,2)+(3,4) != (1,2,3,4): raise TestFailed, 'tuple concatenation'
221 if (1,2)*3 != (1,2,1,2,1,2): raise TestFailed, 'tuple repetition *3'
222 if 0*(1,2,3) != (): raise TestFailed, 'tuple repetition 0*'
223 if min((1,2)) != 1 or max((1,2)) != 2: raise TestFailed, 'min/max tuple'
224 if 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
225 else: raise TestFailed, 'in/not in tuple'
226 try: ()[0]
227 except IndexError: pass
228 else: raise TestFailed, "tuple index error didn't raise IndexError"
229 x = ()
230 x += ()
231 if x != (): raise TestFailed, 'tuple inplace add from () to () failed'
232 x += (1,)
233 if x != (1,): raise TestFailed, 'tuple resize from () failed'
235 # extended slicing - subscript only for tuples
236 a = (0,1,2,3,4)
237 vereq(a[::], a)
238 vereq(a[::2], (0,2,4))
239 vereq(a[1::2], (1,3))
240 vereq(a[::-1], (4,3,2,1,0))
241 vereq(a[::-2], (4,2,0))
242 vereq(a[3::-2], (3,1))
243 vereq(a[-100:100:], a)
244 vereq(a[100:-100:-1], a[::-1])
245 vereq(a[-100L:100L:2L], (0,2,4))
247 # Check that a specific bug in _PyTuple_Resize() is squashed.
248 def f():
249 for i in range(1000):
250 yield i
251 vereq(list(tuple(f())), range(1000))
253 print '6.5.3 Lists'
254 if len([]) != 0: raise TestFailed, 'len([])'
255 if len([1,]) != 1: raise TestFailed, 'len([1,])'
256 if len([1,2,3,4,5,6]) != 6: raise TestFailed, 'len([1,2,3,4,5,6])'
257 if [1,2]+[3,4] != [1,2,3,4]: raise TestFailed, 'list concatenation'
258 if [1,2]*3 != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3'
259 if [1,2]*3L != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3L'
260 if 0*[1,2,3] != []: raise TestFailed, 'list repetition 0*'
261 if 0L*[1,2,3] != []: raise TestFailed, 'list repetition 0L*'
262 if min([1,2]) != 1 or max([1,2]) != 2: raise TestFailed, 'min/max list'
263 if 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
264 else: raise TestFailed, 'in/not in list'
265 a = [1, 2, 3, 4, 5]
266 a[:-1] = a
267 if a != [1, 2, 3, 4, 5, 5]:
268 raise TestFailed, "list self-slice-assign (head)"
269 a = [1, 2, 3, 4, 5]
270 a[1:] = a
271 if a != [1, 1, 2, 3, 4, 5]:
272 raise TestFailed, "list self-slice-assign (tail)"
273 a = [1, 2, 3, 4, 5]
274 a[1:-1] = a
275 if a != [1, 1, 2, 3, 4, 5, 5]:
276 raise TestFailed, "list self-slice-assign (center)"
277 try: [][0]
278 except IndexError: pass
279 else: raise TestFailed, "list index error didn't raise IndexError"
280 try: [][0] = 5
281 except IndexError: pass
282 else: raise TestFailed, "list assignment index error didn't raise IndexError"
283 try: [].pop()
284 except IndexError: pass
285 else: raise TestFailed, "empty list.pop() didn't raise IndexError"
286 try: [1].pop(5)
287 except IndexError: pass
288 else: raise TestFailed, "[1].pop(5) didn't raise IndexError"
289 try: [][0:1] = 5
290 except TypeError: pass
291 else: raise TestFailed, "bad list slice assignment didn't raise TypeError"
292 try: [].extend(None)
293 except TypeError: pass
294 else: raise TestFailed, "list.extend(None) didn't raise TypeError"
295 a = [1, 2, 3, 4]
296 a *= 0
297 if a != []:
298 raise TestFailed, "list inplace repeat"
301 print '6.5.3a Additional list operations'
302 a = [0,1,2,3,4]
303 a[0L] = 1
304 a[1L] = 2
305 a[2L] = 3
306 if a != [1,2,3,3,4]: raise TestFailed, 'list item assignment [0L], [1L], [2L]'
307 a[0] = 5
308 a[1] = 6
309 a[2] = 7
310 if a != [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]'
311 a[-2L] = 88
312 a[-1L] = 99
313 if a != [5,6,7,88,99]: raise TestFailed, 'list item assignment [-2L], [-1L]'
314 a[-2] = 8
315 a[-1] = 9
316 if a != [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]'
317 a[:2] = [0,4]
318 a[-3:] = []
319 a[1:1] = [1,2,3]
320 if a != [0,1,2,3,4]: raise TestFailed, 'list slice assignment'
321 a[ 1L : 4L] = [7,8,9]
322 if a != [0,7,8,9,4]: raise TestFailed, 'list slice assignment using long ints'
323 del a[1:4]
324 if a != [0,4]: raise TestFailed, 'list slice deletion'
325 del a[0]
326 if a != [4]: raise TestFailed, 'list item deletion [0]'
327 del a[-1]
328 if a != []: raise TestFailed, 'list item deletion [-1]'
329 a=range(0,5)
330 del a[1L:4L]
331 if a != [0,4]: raise TestFailed, 'list slice deletion'
332 del a[0L]
333 if a != [4]: raise TestFailed, 'list item deletion [0]'
334 del a[-1L]
335 if a != []: raise TestFailed, 'list item deletion [-1]'
336 a.append(0)
337 a.append(1)
338 a.append(2)
339 if a != [0,1,2]: raise TestFailed, 'list append'
340 a.insert(0, -2)
341 a.insert(1, -1)
342 a.insert(2,0)
343 if a != [-2,-1,0,0,1,2]: raise TestFailed, 'list insert'
344 if a.count(0) != 2: raise TestFailed, ' list count'
345 if a.index(0) != 2: raise TestFailed, 'list index'
346 a.remove(0)
347 if a != [-2,-1,0,1,2]: raise TestFailed, 'list remove'
348 a.reverse()
349 if a != [2,1,0,-1,-2]: raise TestFailed, 'list reverse'
350 a.sort()
351 if a != [-2,-1,0,1,2]: raise TestFailed, 'list sort'
352 def revcmp(a, b): return cmp(b, a)
353 a.sort(revcmp)
354 if a != [2,1,0,-1,-2]: raise TestFailed, 'list sort with cmp func'
355 # The following dumps core in unpatched Python 1.5:
356 def myComparison(x,y):
357 return cmp(x%3, y%7)
358 z = range(12)
359 z.sort(myComparison)
361 try: z.sort(2)
362 except TypeError: pass
363 else: raise TestFailed, 'list sort compare function is not callable'
365 def selfmodifyingComparison(x,y):
366 z[0] = 1
367 return cmp(x, y)
368 try: z.sort(selfmodifyingComparison)
369 except TypeError: pass
370 else: raise TestFailed, 'modifying list during sort'
372 try: z.sort(lambda x, y: 's')
373 except TypeError: pass
374 else: raise TestFailed, 'list sort compare function does not return int'
376 # Test extreme cases with long ints
377 a = [0,1,2,3,4]
378 if a[ -pow(2,128L): 3 ] != [0,1,2]:
379 raise TestFailed, "list slicing with too-small long integer"
380 if a[ 3: pow(2,145L) ] != [3,4]:
381 raise TestFailed, "list slicing with too-large long integer"
384 # extended slicing
386 # subscript
387 a = [0,1,2,3,4]
388 vereq(a[::], a)
389 vereq(a[::2], [0,2,4])
390 vereq(a[1::2], [1,3])
391 vereq(a[::-1], [4,3,2,1,0])
392 vereq(a[::-2], [4,2,0])
393 vereq(a[3::-2], [3,1])
394 vereq(a[-100:100:], a)
395 vereq(a[100:-100:-1], a[::-1])
396 vereq(a[-100L:100L:2L], [0,2,4])
397 vereq(a[1000:2000:2], [])
398 vereq(a[-1000:-2000:-2], [])
399 # deletion
400 del a[::2]
401 vereq(a, [1,3])
402 a = range(5)
403 del a[1::2]
404 vereq(a, [0,2,4])
405 a = range(5)
406 del a[1::-2]
407 vereq(a, [0,2,3,4])
408 a = range(10)
409 del a[::1000]
410 vereq(a, [1, 2, 3, 4, 5, 6, 7, 8, 9])
411 # assignment
412 a = range(10)
413 a[::2] = [-1]*5
414 vereq(a, [-1, 1, -1, 3, -1, 5, -1, 7, -1, 9])
415 a = range(10)
416 a[::-4] = [10]*3
417 vereq(a, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])
418 a = range(4)
419 a[::-1] = a
420 vereq(a, [3, 2, 1, 0])
421 a = range(10)
422 b = a[:]
423 c = a[:]
424 a[2:3] = ["two", "elements"]
425 b[slice(2,3)] = ["two", "elements"]
426 c[2:3:] = ["two", "elements"]
427 vereq(a, b)
428 vereq(a, c)
430 print '6.6 Mappings == Dictionaries'
431 d = {}
432 if d.keys() != []: raise TestFailed, '{}.keys()'
433 if d.values() != []: raise TestFailed, '{}.values()'
434 if d.items() != []: raise TestFailed, '{}.items()'
435 if d.has_key('a') != 0: raise TestFailed, '{}.has_key(\'a\')'
436 if ('a' in d) != 0: raise TestFailed, "'a' in {}"
437 if ('a' not in d) != 1: raise TestFailed, "'a' not in {}"
438 if len(d) != 0: raise TestFailed, 'len({})'
439 d = {'a': 1, 'b': 2}
440 if len(d) != 2: raise TestFailed, 'len(dict)'
441 k = d.keys()
442 k.sort()
443 if k != ['a', 'b']: raise TestFailed, 'dict keys()'
444 if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass
445 else: raise TestFailed, 'dict keys()'
446 if 'a' in d and 'b' in d and 'c' not in d: pass
447 else: raise TestFailed, 'dict keys() # in/not in version'
448 if d['a'] != 1 or d['b'] != 2: raise TestFailed, 'dict item'
449 d['c'] = 3
450 d['a'] = 4
451 if d['c'] != 3 or d['a'] != 4: raise TestFailed, 'dict item assignment'
452 del d['b']
453 if d != {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion'
454 # dict.clear()
455 d = {1:1, 2:2, 3:3}
456 d.clear()
457 if d != {}: raise TestFailed, 'dict clear'
458 # dict.update()
459 d.update({1:100})
460 d.update({2:20})
461 d.update({1:1, 2:2, 3:3})
462 if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update'
463 d.clear()
464 try: d.update(None)
465 except AttributeError: pass
466 else: raise TestFailed, 'dict.update(None), AttributeError expected'
467 class SimpleUserDict:
468 def __init__(self):
469 self.d = {1:1, 2:2, 3:3}
470 def keys(self):
471 return self.d.keys()
472 def __getitem__(self, i):
473 return self.d[i]
474 d.update(SimpleUserDict())
475 if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict.update(instance)'
476 d.clear()
477 class FailingUserDict:
478 def keys(self):
479 raise ValueError
480 try: d.update(FailingUserDict())
481 except ValueError: pass
482 else: raise TestFailed, 'dict.keys() expected ValueError'
483 class FailingUserDict:
484 def keys(self):
485 class BogonIter:
486 def __iter__(self):
487 raise ValueError
488 return BogonIter()
489 try: d.update(FailingUserDict())
490 except ValueError: pass
491 else: raise TestFailed, 'iter(dict.keys()) expected ValueError'
492 class FailingUserDict:
493 def keys(self):
494 class BogonIter:
495 def __init__(self):
496 self.i = 1
497 def __iter__(self):
498 return self
499 def next(self):
500 if self.i:
501 self.i = 0
502 return 'a'
503 raise ValueError
504 return BogonIter()
505 def __getitem__(self, key):
506 return key
507 try: d.update(FailingUserDict())
508 except ValueError: pass
509 else: raise TestFailed, 'iter(dict.keys()).next() expected ValueError'
510 class FailingUserDict:
511 def keys(self):
512 class BogonIter:
513 def __init__(self):
514 self.i = ord('a')
515 def __iter__(self):
516 return self
517 def next(self):
518 if self.i <= ord('z'):
519 rtn = chr(self.i)
520 self.i += 1
521 return rtn
522 raise StopIteration
523 return BogonIter()
524 def __getitem__(self, key):
525 raise ValueError
526 try: d.update(FailingUserDict())
527 except ValueError: pass
528 else: raise TestFailed, 'dict.update(), __getitem__ expected ValueError'
529 # dict.copy()
530 d = {1:1, 2:2, 3:3}
531 if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'
532 if {}.copy() != {}: raise TestFailed, 'empty dict copy'
533 # dict.get()
534 d = {}
535 if d.get('c') is not None: raise TestFailed, 'missing {} get, no 2nd arg'
536 if d.get('c', 3) != 3: raise TestFailed, 'missing {} get, w/ 2nd arg'
537 d = {'a' : 1, 'b' : 2}
538 if d.get('c') is not None: raise TestFailed, 'missing dict get, no 2nd arg'
539 if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg'
540 if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg'
541 if d.get('a', 3) != 1: raise TestFailed, 'present dict get, w/ 2nd arg'
542 # dict.setdefault()
543 d = {}
544 if d.setdefault('key0') is not None:
545 raise TestFailed, 'missing {} setdefault, no 2nd arg'
546 if d.setdefault('key0') is not None:
547 raise TestFailed, 'present {} setdefault, no 2nd arg'
548 d.setdefault('key', []).append(3)
549 if d['key'][0] != 3:
550 raise TestFailed, 'missing {} setdefault, w/ 2nd arg'
551 d.setdefault('key', []).append(4)
552 if len(d['key']) != 2:
553 raise TestFailed, 'present {} setdefault, w/ 2nd arg'
554 # dict.popitem()
555 for copymode in -1, +1:
556 # -1: b has same structure as a
557 # +1: b is a.copy()
558 for log2size in range(12):
559 size = 2**log2size
560 a = {}
561 b = {}
562 for i in range(size):
563 a[`i`] = i
564 if copymode < 0:
565 b[`i`] = i
566 if copymode > 0:
567 b = a.copy()
568 for i in range(size):
569 ka, va = ta = a.popitem()
570 if va != int(ka): raise TestFailed, "a.popitem: %s" % str(ta)
571 kb, vb = tb = b.popitem()
572 if vb != int(kb): raise TestFailed, "b.popitem: %s" % str(tb)
573 if copymode < 0 and ta != tb:
574 raise TestFailed, "a.popitem != b.popitem: %s, %s" % (
575 str(ta), str(tb))
576 if a: raise TestFailed, 'a not empty after popitems: %s' % str(a)
577 if b: raise TestFailed, 'b not empty after popitems: %s' % str(b)
579 d.clear()
580 try: d.popitem()
581 except KeyError: pass
582 else: raise TestFailed, "{}.popitem doesn't raise KeyError"
584 # Tests for pop with specified key
585 d.clear()
586 k, v = 'abc', 'def'
587 d[k] = v
588 try: d.pop('ghi')
589 except KeyError: pass
590 else: raise TestFailed, "{}.pop(k) doesn't raise KeyError when k not in dictionary"
592 if d.pop(k) != v: raise TestFailed, "{}.pop(k) doesn't find known key/value pair"
593 if len(d) > 0: raise TestFailed, "{}.pop(k) failed to remove the specified pair"
595 try: d.pop(k)
596 except KeyError: pass
597 else: raise TestFailed, "{}.pop(k) doesn't raise KeyError when dictionary is empty"
599 d[1] = 1
600 try:
601 for i in d:
602 d[i+1] = 1
603 except RuntimeError:
604 pass
605 else:
606 raise TestFailed, "changing dict size during iteration doesn't raise Error"
608 try: type(1, 2)
609 except TypeError: pass
610 else: raise TestFailed, 'type(), w/2 args expected TypeError'
612 try: type(1, 2, 3, 4)
613 except TypeError: pass
614 else: raise TestFailed, 'type(), w/4 args expected TypeError'
616 print 'Buffers'
617 try: buffer('asdf', -1)
618 except ValueError: pass
619 else: raise TestFailed, "buffer('asdf', -1) should raise ValueError"
621 try: buffer(None)
622 except TypeError: pass
623 else: raise TestFailed, "buffer(None) should raise TypeError"
625 a = buffer('asdf')
626 hash(a)
627 b = a * 5
628 if a == b:
629 raise TestFailed, 'buffers should not be equal'
630 if str(b) != ('asdf' * 5):
631 raise TestFailed, 'repeated buffer has wrong content'
632 if str(a * 0) != '':
633 raise TestFailed, 'repeated buffer zero times has wrong content'
634 if str(a + buffer('def')) != 'asdfdef':
635 raise TestFailed, 'concatenation of buffers yields wrong content'
637 try: a[1] = 'g'
638 except TypeError: pass
639 else: raise TestFailed, "buffer assignment should raise TypeError"
641 try: a[0:1] = 'g'
642 except TypeError: pass
643 else: raise TestFailed, "buffer slice assignment should raise TypeError"