1 # Python test set -- part 6, built-in types
3 from 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'
52 print '6.4 Numeric types (mostly conversions)'
53 if 0 != 0L or 0 != 0.0 or 0L != 0.0: raise TestFailed
, 'mixed comparisons'
54 if 1 != 1L or 1 != 1.0 or 1L != 1.0: raise TestFailed
, 'mixed comparisons'
55 if -1 != -1L or -1 != -1.0 or -1L != -1.0:
56 raise TestFailed
, 'int/long/float value not equal'
57 if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
58 else: raise TestFailed
, 'int() does not round properly'
59 if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass
60 else: raise TestFailed
, 'long() does not round properly'
61 if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
62 else: raise TestFailed
, 'float() does not work properly'
63 print '6.4.1 32-bit integers'
64 if 12 + 24 != 36: raise TestFailed
, 'int op'
65 if 12 + (-24) != -12: raise TestFailed
, 'int op'
66 if (-12) + 24 != 12: raise TestFailed
, 'int op'
67 if (-12) + (-24) != -36: raise TestFailed
, 'int op'
68 if not 12 < 24: raise TestFailed
, 'int op'
69 if not -24 < -12: raise TestFailed
, 'int op'
70 # Test for a particular bug in integer multiply
71 xsize
, ysize
, zsize
= 238, 356, 4
72 if not (xsize
*ysize
*zsize
== zsize
*xsize
*ysize
== 338912):
73 raise TestFailed
, 'int mul commutativity'
74 print '6.4.2 Long integers'
75 if 12L + 24L != 36L: raise TestFailed
, 'long op'
76 if 12L + (-24L) != -12L: raise TestFailed
, 'long op'
77 if (-12L) + 24L != 12L: raise TestFailed
, 'long op'
78 if (-12L) + (-24L) != -36L: raise TestFailed
, 'long op'
79 if not 12L < 24L: raise TestFailed
, 'long op'
80 if not -24L < -12L: raise TestFailed
, 'long op'
82 if int(long(x
)) != x
: raise TestFailed
, 'long op'
84 except OverflowError: pass
85 else:raise TestFailed
, 'long op'
87 if int(long(x
)) != x
: raise TestFailed
, 'long op'
89 if int(long(x
)) != x
: raise TestFailed
, 'long op'
91 except OverflowError: pass
92 else:raise TestFailed
, 'long op'
93 print '6.4.3 Floating point numbers'
94 if 12.0 + 24.0 != 36.0: raise TestFailed
, 'float op'
95 if 12.0 + (-24.0) != -12.0: raise TestFailed
, 'float op'
96 if (-12.0) + 24.0 != 12.0: raise TestFailed
, 'float op'
97 if (-12.0) + (-24.0) != -36.0: raise TestFailed
, 'float op'
98 if not 12.0 < 24.0: raise TestFailed
, 'float op'
99 if not -24.0 < -12.0: raise TestFailed
, 'float op'
101 print '6.5 Sequence types'
103 print '6.5.1 Strings'
104 if len('') != 0: raise TestFailed
, 'len(\'\')'
105 if len('a') != 1: raise TestFailed
, 'len(\'a\')'
106 if len('abcdef') != 6: raise TestFailed
, 'len(\'abcdef\')'
107 if 'xyz' + 'abcde' != 'xyzabcde': raise TestFailed
, 'string concatenation'
108 if 'xyz'*3 != 'xyzxyzxyz': raise TestFailed
, 'string repetition *3'
109 if 0*'abcde' != '': raise TestFailed
, 'string repetition 0*'
110 if min('abc') != 'a' or max('abc') != 'c': raise TestFailed
, 'min/max string'
111 if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
112 else: raise TestFailed
, 'in/not in string'
114 if '%s!'%x != x
+'!': raise TestFailed
, 'nasty string formatting bug'
117 if len(()) != 0: raise TestFailed
, 'len(())'
118 if len((1,)) != 1: raise TestFailed
, 'len((1,))'
119 if len((1,2,3,4,5,6)) != 6: raise TestFailed
, 'len((1,2,3,4,5,6))'
120 if (1,2)+(3,4) != (1,2,3,4): raise TestFailed
, 'tuple concatenation'
121 if (1,2)*3 != (1,2,1,2,1,2): raise TestFailed
, 'tuple repetition *3'
122 if 0*(1,2,3) != (): raise TestFailed
, 'tuple repetition 0*'
123 if min((1,2)) != 1 or max((1,2)) != 2: raise TestFailed
, 'min/max tuple'
124 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
125 else: raise TestFailed
, 'in/not in tuple'
128 if len([]) != 0: raise TestFailed
, 'len([])'
129 if len([1,]) != 1: raise TestFailed
, 'len([1,])'
130 if len([1,2,3,4,5,6]) != 6: raise TestFailed
, 'len([1,2,3,4,5,6])'
131 if [1,2]+[3,4] != [1,2,3,4]: raise TestFailed
, 'list concatenation'
132 if [1,2]*3 != [1,2,1,2,1,2]: raise TestFailed
, 'list repetition *3'
133 if [1,2]*3L != [1,2,1,2,1,2]: raise TestFailed
, 'list repetition *3L'
134 if 0*[1,2,3] != []: raise TestFailed
, 'list repetition 0*'
135 if 0L*[1,2,3] != []: raise TestFailed
, 'list repetition 0L*'
136 if min([1,2]) != 1 or max([1,2]) != 2: raise TestFailed
, 'min/max list'
137 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
138 else: raise TestFailed
, 'in/not in list'
141 if a
!= [1, 2, 3, 4, 5, 5]:
142 raise TestFailed
, "list self-slice-assign (head)"
145 if a
!= [1, 1, 2, 3, 4, 5]:
146 raise TestFailed
, "list self-slice-assign (tail)"
149 if a
!= [1, 1, 2, 3, 4, 5, 5]:
150 raise TestFailed
, "list self-slice-assign (center)"
153 print '6.5.3a Additional list operations'
158 if a
!= [1,2,3,3,4]: raise TestFailed
, 'list item assignment [0L], [1L], [2L]'
162 if a
!= [5,6,7,3,4]: raise TestFailed
, 'list item assignment [0], [1], [2]'
165 if a
!= [5,6,7,88,99]: raise TestFailed
, 'list item assignment [-2L], [-1L]'
168 if a
!= [5,6,7,8,9]: raise TestFailed
, 'list item assignment [-2], [-1]'
172 if a
!= [0,1,2,3,4]: raise TestFailed
, 'list slice assignment'
173 a
[ 1L : 4L] = [7,8,9]
174 if a
!= [0,7,8,9,4]: raise TestFailed
, 'list slice assignment using long ints'
176 if a
!= [0,4]: raise TestFailed
, 'list slice deletion'
178 if a
!= [4]: raise TestFailed
, 'list item deletion [0]'
180 if a
!= []: raise TestFailed
, 'list item deletion [-1]'
183 if a
!= [0,4]: raise TestFailed
, 'list slice deletion'
185 if a
!= [4]: raise TestFailed
, 'list item deletion [0]'
187 if a
!= []: raise TestFailed
, 'list item deletion [-1]'
191 if a
!= [0,1,2]: raise TestFailed
, 'list append'
195 if a
!= [-2,-1,0,0,1,2]: raise TestFailed
, 'list insert'
196 if a
.count(0) != 2: raise TestFailed
, ' list count'
197 if a
.index(0) != 2: raise TestFailed
, 'list index'
199 if a
!= [-2,-1,0,1,2]: raise TestFailed
, 'list remove'
201 if a
!= [2,1,0,-1,-2]: raise TestFailed
, 'list reverse'
203 if a
!= [-2,-1,0,1,2]: raise TestFailed
, 'list sort'
204 def revcmp(a
, b
): return cmp(b
, a
)
206 if a
!= [2,1,0,-1,-2]: raise TestFailed
, 'list sort with cmp func'
207 # The following dumps core in unpatched Python 1.5:
208 def myComparison(x
,y
):
213 # Test extreme cases with long ints
215 if a
[ -pow(2,128L): 3 ] != [0,1,2]:
216 raise TestFailed
, "list slicing with too-small long integer"
217 if a
[ 3: pow(2,145L) ] != [3,4]:
218 raise TestFailed
, "list slicing with too-large long integer"
220 print '6.6 Mappings == Dictionaries'
222 if d
.keys() != []: raise TestFailed
, '{}.keys()'
223 if d
.has_key('a') != 0: raise TestFailed
, '{}.has_key(\'a\')'
224 if len(d
) != 0: raise TestFailed
, 'len({})'
226 if len(d
) != 2: raise TestFailed
, 'len(dict)'
229 if k
!= ['a', 'b']: raise TestFailed
, 'dict keys()'
230 if d
.has_key('a') and d
.has_key('b') and not d
.has_key('c'): pass
231 else: raise TestFailed
, 'dict keys()'
232 if d
['a'] != 1 or d
['b'] != 2: raise TestFailed
, 'dict item'
235 if d
['c'] != 3 or d
['a'] != 4: raise TestFailed
, 'dict item assignment'
237 if d
!= {'a': 4, 'c': 3}: raise TestFailed
, 'dict item deletion'
240 if d
!= {}: raise TestFailed
, 'dict clear'
243 d
.update({1:1, 2:2, 3:3})
244 if d
!= {1:1, 2:2, 3:3}: raise TestFailed
, 'dict update'
245 if d
.copy() != {1:1, 2:2, 3:3}: raise TestFailed
, 'dict copy'
246 if {}.copy() != {}: raise TestFailed
, 'empty dict copy'
249 if d
.get('c') is not None: raise TestFailed
, 'missing {} get, no 2nd arg'
250 if d
.get('c', 3) != 3: raise TestFailed
, 'missing {} get, w/ 2nd arg'
251 d
= {'a' : 1, 'b' : 2}
252 if d
.get('c') is not None: raise TestFailed
, 'missing dict get, no 2nd arg'
253 if d
.get('c', 3) != 3: raise TestFailed
, 'missing dict get, w/ 2nd arg'
254 if d
.get('a') != 1: raise TestFailed
, 'present dict get, no 2nd arg'
255 if d
.get('a', 3) != 1: raise TestFailed
, 'present dict get, w/ 2nd arg'
258 if d
.setdefault('key0') is not None:
259 raise TestFailed
, 'missing {} setdefault, no 2nd arg'
260 if d
.setdefault('key0') is not None:
261 raise TestFailed
, 'present {} setdefault, no 2nd arg'
262 d
.setdefault('key', []).append(3)
264 raise TestFailed
, 'missing {} setdefault, w/ 2nd arg'
265 d
.setdefault('key', []).append(4)
266 if len(d
['key']) != 2:
267 raise TestFailed
, 'present {} setdefault, w/ 2nd arg'
269 for copymode
in -1, +1:
270 # -1: b has same structure as a
272 for log2size
in range(12):
276 for i
in range(size
):
282 for i
in range(size
):
283 ka
, va
= ta
= a
.popitem()
284 if va
!= int(ka
): raise TestFailed
, "a.popitem: %s" % str(ta
)
285 kb
, vb
= tb
= b
.popitem()
286 if vb
!= int(kb
): raise TestFailed
, "b.popitem: %s" % str(tb
)
287 if copymode
< 0 and ta
!= tb
:
288 raise TestFailed
, "a.popitem != b.popitem: %s, %s" % (
290 if a
: raise TestFailed
, 'a not empty after popitems: %s' % str(a
)
291 if b
: raise TestFailed
, 'b not empty after popitems: %s' % str(b
)