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'
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'
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'
53 except ValueError: pass
54 else: raise TestFailed
, "float('') didn't raise ValueError"
57 except ValueError: pass
58 else: raise TestFailed
, "float('5\0') didn't raise ValueError"
61 except ZeroDivisionError: pass
62 else: raise TestFailed
, "5.0 / 0.0 didn't raise ZeroDivisionError"
65 except ZeroDivisionError: pass
66 else: raise TestFailed
, "5.0 // 0.0 didn't raise ZeroDivisionError"
69 except ZeroDivisionError: pass
70 else: raise TestFailed
, "5.0 % 0.0 didn't raise ZeroDivisionError"
73 except ZeroDivisionError: pass
74 else: raise TestFailed
, "5 / 0L didn't raise ZeroDivisionError"
77 except ZeroDivisionError: pass
78 else: raise TestFailed
, "5 // 0L didn't raise ZeroDivisionError"
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'
108 for divisor
in 1, 2, 4, 8, 16, 32:
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" %
116 # Check for expected * overflow to long.
117 for divisor
in 1, 2, 4, 8, 16, 32:
120 if type(prod
) is not long:
121 raise TestFailed
, ("expected type(%r) to be long, not %r" %
123 # Check for expected * overflow to long.
125 for divisor
in 1, 2, 4, 8, 16, 32:
128 if type(prod
) is not long:
129 raise TestFailed
, ("expected type(%r) to be long, not %r" %
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'
140 if int(long(x
)) != x
: raise TestFailed
, 'long op'
142 except OverflowError: pass
143 else:raise TestFailed
, 'long op'
145 if int(long(x
)) != x
: raise TestFailed
, 'long op'
147 if int(long(x
)) != x
: raise TestFailed
, 'long op'
149 except OverflowError: pass
150 else:raise TestFailed
, 'long op'
153 except ValueError: pass
154 else: raise TestFailed
, 'int negative shift <<'
157 except ValueError: pass
158 else: raise TestFailed
, 'long negative shift <<'
161 except ValueError: pass
162 else: raise TestFailed
, 'int negative shift >>'
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'
189 if '%s!'%x != x
+'!': raise TestFailed
, 'nasty string formatting bug'
191 #extended slices for strings
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')
204 a
= unicode('0123456789', 'ascii')
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'))
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'
227 except IndexError: pass
228 else: raise TestFailed
, "tuple index error didn't raise IndexError"
231 if x
!= (): raise TestFailed
, 'tuple inplace add from () to () failed'
233 if x
!= (1,): raise TestFailed
, 'tuple resize from () failed'
235 # extended slicing - subscript only for tuples
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.
249 for i
in range(1000):
251 vereq(list(tuple(f())), range(1000))
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'
267 if a
!= [1, 2, 3, 4, 5, 5]:
268 raise TestFailed
, "list self-slice-assign (head)"
271 if a
!= [1, 1, 2, 3, 4, 5]:
272 raise TestFailed
, "list self-slice-assign (tail)"
275 if a
!= [1, 1, 2, 3, 4, 5, 5]:
276 raise TestFailed
, "list self-slice-assign (center)"
278 except IndexError: pass
279 else: raise TestFailed
, "list index error didn't raise IndexError"
281 except IndexError: pass
282 else: raise TestFailed
, "list assignment index error didn't raise IndexError"
284 except IndexError: pass
285 else: raise TestFailed
, "empty list.pop() didn't raise IndexError"
287 except IndexError: pass
288 else: raise TestFailed
, "[1].pop(5) didn't raise IndexError"
290 except TypeError: pass
291 else: raise TestFailed
, "bad list slice assignment didn't raise TypeError"
293 except TypeError: pass
294 else: raise TestFailed
, "list.extend(None) didn't raise TypeError"
298 raise TestFailed
, "list inplace repeat"
301 print '6.5.3a Additional list operations'
306 if a
!= [1,2,3,3,4]: raise TestFailed
, 'list item assignment [0L], [1L], [2L]'
310 if a
!= [5,6,7,3,4]: raise TestFailed
, 'list item assignment [0], [1], [2]'
313 if a
!= [5,6,7,88,99]: raise TestFailed
, 'list item assignment [-2L], [-1L]'
316 if a
!= [5,6,7,8,9]: raise TestFailed
, 'list item assignment [-2], [-1]'
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'
324 if a
!= [0,4]: raise TestFailed
, 'list slice deletion'
326 if a
!= [4]: raise TestFailed
, 'list item deletion [0]'
328 if a
!= []: raise TestFailed
, 'list item deletion [-1]'
331 if a
!= [0,4]: raise TestFailed
, 'list slice deletion'
333 if a
!= [4]: raise TestFailed
, 'list item deletion [0]'
335 if a
!= []: raise TestFailed
, 'list item deletion [-1]'
339 if a
!= [0,1,2]: raise TestFailed
, 'list append'
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'
347 if a
!= [-2,-1,0,1,2]: raise TestFailed
, 'list remove'
349 if a
!= [2,1,0,-1,-2]: raise TestFailed
, 'list reverse'
351 if a
!= [-2,-1,0,1,2]: raise TestFailed
, 'list sort'
352 def revcmp(a
, b
): return cmp(b
, a
)
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
):
362 except TypeError: pass
363 else: raise TestFailed
, 'list sort compare function is not callable'
365 def selfmodifyingComparison(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
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"
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], [])
410 vereq(a
, [1, 2, 3, 4, 5, 6, 7, 8, 9])
414 vereq(a
, [-1, 1, -1, 3, -1, 5, -1, 7, -1, 9])
417 vereq(a
, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])
420 vereq(a
, [3, 2, 1, 0])
424 a
[2:3] = ["two", "elements"]
425 b
[slice(2,3)] = ["two", "elements"]
426 c
[2:3:] = ["two", "elements"]
430 print '6.6 Mappings == Dictionaries'
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({})'
440 if len(d
) != 2: raise TestFailed
, 'len(dict)'
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'
451 if d
['c'] != 3 or d
['a'] != 4: raise TestFailed
, 'dict item assignment'
453 if d
!= {'a': 4, 'c': 3}: raise TestFailed
, 'dict item deletion'
457 if d
!= {}: raise TestFailed
, 'dict clear'
461 d
.update({1:1, 2:2, 3:3})
462 if d
!= {1:1, 2:2, 3:3}: raise TestFailed
, 'dict update'
465 except AttributeError: pass
466 else: raise TestFailed
, 'dict.update(None), AttributeError expected'
467 class SimpleUserDict
:
469 self
.d
= {1:1, 2:2, 3:3}
472 def __getitem__(self
, i
):
474 d
.update(SimpleUserDict())
475 if d
!= {1:1, 2:2, 3:3}: raise TestFailed
, 'dict.update(instance)'
477 class FailingUserDict
:
480 try: d
.update(FailingUserDict())
481 except ValueError: pass
482 else: raise TestFailed
, 'dict.keys() expected ValueError'
483 class FailingUserDict
:
489 try: d
.update(FailingUserDict())
490 except ValueError: pass
491 else: raise TestFailed
, 'iter(dict.keys()) expected ValueError'
492 class FailingUserDict
:
505 def __getitem__(self
, key
):
507 try: d
.update(FailingUserDict())
508 except ValueError: pass
509 else: raise TestFailed
, 'iter(dict.keys()).next() expected ValueError'
510 class FailingUserDict
:
518 if self
.i
<= ord('z'):
524 def __getitem__(self
, key
):
526 try: d
.update(FailingUserDict())
527 except ValueError: pass
528 else: raise TestFailed
, 'dict.update(), __getitem__ expected ValueError'
531 if d
.copy() != {1:1, 2:2, 3:3}: raise TestFailed
, 'dict copy'
532 if {}.copy() != {}: raise TestFailed
, 'empty dict copy'
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'
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)
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'
555 for copymode
in -1, +1:
556 # -1: b has same structure as a
558 for log2size
in range(12):
562 for i
in range(size
):
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" % (
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
)
581 except KeyError: pass
582 else: raise TestFailed
, "{}.popitem doesn't raise KeyError"
584 # Tests for pop with specified key
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"
596 except KeyError: pass
597 else: raise TestFailed
, "{}.pop(k) doesn't raise KeyError when dictionary is empty"
606 raise TestFailed
, "changing dict size during iteration doesn't raise Error"
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'
617 try: buffer('asdf', -1)
618 except ValueError: pass
619 else: raise TestFailed
, "buffer('asdf', -1) should raise ValueError"
622 except TypeError: pass
623 else: raise TestFailed
, "buffer(None) should raise TypeError"
629 raise TestFailed
, 'buffers should not be equal'
630 if str(b
) != ('asdf' * 5):
631 raise TestFailed
, 'repeated buffer has wrong content'
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'
638 except TypeError: pass
639 else: raise TestFailed
, "buffer assignment should raise TypeError"
642 except TypeError: pass
643 else: raise TestFailed
, "buffer slice assignment should raise TypeError"