Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | # Python test set -- part 6, built-in types |
2 | ||
3 | from test.test_support import * | |
4 | ||
5 | print '6. Built-in types' | |
6 | ||
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 not 1: raise TestFailed, '1 is false instead of true' | |
14 | if not 1L: raise TestFailed, '1L is false instead of true' | |
15 | if not 1.0: raise TestFailed, '1.0 is false instead of true' | |
16 | if not 'x': raise TestFailed, '\'x\' is false instead of true' | |
17 | if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true' | |
18 | def f(): pass | |
19 | class C: pass | |
20 | import sys | |
21 | x = C() | |
22 | if not f: raise TestFailed, 'f is false instead of true' | |
23 | if not C: raise TestFailed, 'C is false instead of true' | |
24 | if not sys: raise TestFailed, 'sys is false instead of true' | |
25 | if not x: raise TestFailed, 'x is false instead of true' | |
26 | ||
27 | print '6.2 Boolean operations' | |
28 | if 0 or 0: raise TestFailed, '0 or 0 is true instead of false' | |
29 | if 1 and 1: pass | |
30 | else: raise TestFailed, '1 and 1 is false instead of true' | |
31 | if not 1: raise TestFailed, 'not 1 is true instead of false' | |
32 | ||
33 | print '6.3 Comparisons' | |
34 | if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass | |
35 | else: raise TestFailed, 'int comparisons failed' | |
36 | if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass | |
37 | else: raise TestFailed, 'long int comparisons failed' | |
38 | if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass | |
39 | else: raise TestFailed, 'float comparisons failed' | |
40 | if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass | |
41 | else: raise TestFailed, 'string comparisons failed' | |
42 | if None is None: pass | |
43 | else: raise TestFailed, 'identity test failed' | |
44 | ||
45 | try: float('') | |
46 | except ValueError: pass | |
47 | else: raise TestFailed, "float('') didn't raise ValueError" | |
48 | ||
49 | try: float('5\0') | |
50 | except ValueError: pass | |
51 | else: raise TestFailed, "float('5\0') didn't raise ValueError" | |
52 | ||
53 | try: 5.0 / 0.0 | |
54 | except ZeroDivisionError: pass | |
55 | else: raise TestFailed, "5.0 / 0.0 didn't raise ZeroDivisionError" | |
56 | ||
57 | try: 5.0 // 0.0 | |
58 | except ZeroDivisionError: pass | |
59 | else: raise TestFailed, "5.0 // 0.0 didn't raise ZeroDivisionError" | |
60 | ||
61 | try: 5.0 % 0.0 | |
62 | except ZeroDivisionError: pass | |
63 | else: raise TestFailed, "5.0 % 0.0 didn't raise ZeroDivisionError" | |
64 | ||
65 | try: 5 / 0L | |
66 | except ZeroDivisionError: pass | |
67 | else: raise TestFailed, "5 / 0L didn't raise ZeroDivisionError" | |
68 | ||
69 | try: 5 // 0L | |
70 | except ZeroDivisionError: pass | |
71 | else: raise TestFailed, "5 // 0L didn't raise ZeroDivisionError" | |
72 | ||
73 | try: 5 % 0L | |
74 | except ZeroDivisionError: pass | |
75 | else: raise TestFailed, "5 % 0L didn't raise ZeroDivisionError" | |
76 | ||
77 | print '6.4 Numeric types (mostly conversions)' | |
78 | if 0 != 0L or 0 != 0.0 or 0L != 0.0: raise TestFailed, 'mixed comparisons' | |
79 | if 1 != 1L or 1 != 1.0 or 1L != 1.0: raise TestFailed, 'mixed comparisons' | |
80 | if -1 != -1L or -1 != -1.0 or -1L != -1.0: | |
81 | raise TestFailed, 'int/long/float value not equal' | |
82 | # calling built-in types without argument must return 0 | |
83 | if int() != 0: raise TestFailed, 'int() does not return 0' | |
84 | if long() != 0L: raise TestFailed, 'long() does not return 0L' | |
85 | if float() != 0.0: raise TestFailed, 'float() does not return 0.0' | |
86 | if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass | |
87 | else: raise TestFailed, 'int() does not round properly' | |
88 | if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass | |
89 | else: raise TestFailed, 'long() does not round properly' | |
90 | if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass | |
91 | else: raise TestFailed, 'float() does not work properly' | |
92 | print '6.4.1 32-bit integers' | |
93 | if 12 + 24 != 36: raise TestFailed, 'int op' | |
94 | if 12 + (-24) != -12: raise TestFailed, 'int op' | |
95 | if (-12) + 24 != 12: raise TestFailed, 'int op' | |
96 | if (-12) + (-24) != -36: raise TestFailed, 'int op' | |
97 | if not 12 < 24: raise TestFailed, 'int op' | |
98 | if not -24 < -12: raise TestFailed, 'int op' | |
99 | # Test for a particular bug in integer multiply | |
100 | xsize, ysize, zsize = 238, 356, 4 | |
101 | if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912): | |
102 | raise TestFailed, 'int mul commutativity' | |
103 | # And another. | |
104 | m = -sys.maxint - 1 | |
105 | for divisor in 1, 2, 4, 8, 16, 32: | |
106 | j = m // divisor | |
107 | prod = divisor * j | |
108 | if prod != m: | |
109 | raise TestFailed, "%r * %r == %r != %r" % (divisor, j, prod, m) | |
110 | if type(prod) is not int: | |
111 | raise TestFailed, ("expected type(prod) to be int, not %r" % | |
112 | type(prod)) | |
113 | # Check for expected * overflow to long. | |
114 | for divisor in 1, 2, 4, 8, 16, 32: | |
115 | j = m // divisor - 1 | |
116 | prod = divisor * j | |
117 | if type(prod) is not long: | |
118 | raise TestFailed, ("expected type(%r) to be long, not %r" % | |
119 | (prod, type(prod))) | |
120 | # Check for expected * overflow to long. | |
121 | m = sys.maxint | |
122 | for divisor in 1, 2, 4, 8, 16, 32: | |
123 | j = m // divisor + 1 | |
124 | prod = divisor * j | |
125 | if type(prod) is not long: | |
126 | raise TestFailed, ("expected type(%r) to be long, not %r" % | |
127 | (prod, type(prod))) | |
128 | ||
129 | print '6.4.2 Long integers' | |
130 | if 12L + 24L != 36L: raise TestFailed, 'long op' | |
131 | if 12L + (-24L) != -12L: raise TestFailed, 'long op' | |
132 | if (-12L) + 24L != 12L: raise TestFailed, 'long op' | |
133 | if (-12L) + (-24L) != -36L: raise TestFailed, 'long op' | |
134 | if not 12L < 24L: raise TestFailed, 'long op' | |
135 | if not -24L < -12L: raise TestFailed, 'long op' | |
136 | x = sys.maxint | |
137 | if int(long(x)) != x: raise TestFailed, 'long op' | |
138 | try: y = int(long(x)+1L) | |
139 | except OverflowError: raise TestFailed, 'long op' | |
140 | if not isinstance(y, long): raise TestFailed, 'long op' | |
141 | x = -x | |
142 | if int(long(x)) != x: raise TestFailed, 'long op' | |
143 | x = x-1 | |
144 | if int(long(x)) != x: raise TestFailed, 'long op' | |
145 | try: y = int(long(x)-1L) | |
146 | except OverflowError: raise TestFailed, 'long op' | |
147 | if not isinstance(y, long): raise TestFailed, 'long op' | |
148 | ||
149 | try: 5 << -5 | |
150 | except ValueError: pass | |
151 | else: raise TestFailed, 'int negative shift <<' | |
152 | ||
153 | try: 5L << -5L | |
154 | except ValueError: pass | |
155 | else: raise TestFailed, 'long negative shift <<' | |
156 | ||
157 | try: 5 >> -5 | |
158 | except ValueError: pass | |
159 | else: raise TestFailed, 'int negative shift >>' | |
160 | ||
161 | try: 5L >> -5L | |
162 | except ValueError: pass | |
163 | else: raise TestFailed, 'long negative shift >>' | |
164 | ||
165 | print '6.4.3 Floating point numbers' | |
166 | if 12.0 + 24.0 != 36.0: raise TestFailed, 'float op' | |
167 | if 12.0 + (-24.0) != -12.0: raise TestFailed, 'float op' | |
168 | if (-12.0) + 24.0 != 12.0: raise TestFailed, 'float op' | |
169 | if (-12.0) + (-24.0) != -36.0: raise TestFailed, 'float op' | |
170 | if not 12.0 < 24.0: raise TestFailed, 'float op' | |
171 | if not -24.0 < -12.0: raise TestFailed, 'float op' | |
172 | ||
173 | print '6.5 Sequence types' | |
174 | ||
175 | print '6.5.1 Strings' | |
176 | if len('') != 0: raise TestFailed, 'len(\'\')' | |
177 | if len('a') != 1: raise TestFailed, 'len(\'a\')' | |
178 | if len('abcdef') != 6: raise TestFailed, 'len(\'abcdef\')' | |
179 | if 'xyz' + 'abcde' != 'xyzabcde': raise TestFailed, 'string concatenation' | |
180 | if 'xyz'*3 != 'xyzxyzxyz': raise TestFailed, 'string repetition *3' | |
181 | if 0*'abcde' != '': raise TestFailed, 'string repetition 0*' | |
182 | if min('abc') != 'a' or max('abc') != 'c': raise TestFailed, 'min/max string' | |
183 | if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass | |
184 | else: raise TestFailed, 'in/not in string' | |
185 | x = 'x'*103 | |
186 | if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug' | |
187 | ||
188 | #extended slices for strings | |
189 | a = '0123456789' | |
190 | vereq(a[::], a) | |
191 | vereq(a[::2], '02468') | |
192 | vereq(a[1::2], '13579') | |
193 | vereq(a[::-1],'9876543210') | |
194 | vereq(a[::-2], '97531') | |
195 | vereq(a[3::-2], '31') | |
196 | vereq(a[-100:100:], a) | |
197 | vereq(a[100:-100:-1], a[::-1]) | |
198 | vereq(a[-100L:100L:2L], '02468') | |
199 | ||
200 | if have_unicode: | |
201 | a = unicode('0123456789', 'ascii') | |
202 | vereq(a[::], a) | |
203 | vereq(a[::2], unicode('02468', 'ascii')) | |
204 | vereq(a[1::2], unicode('13579', 'ascii')) | |
205 | vereq(a[::-1], unicode('9876543210', 'ascii')) | |
206 | vereq(a[::-2], unicode('97531', 'ascii')) | |
207 | vereq(a[3::-2], unicode('31', 'ascii')) | |
208 | vereq(a[-100:100:], a) | |
209 | vereq(a[100:-100:-1], a[::-1]) | |
210 | vereq(a[-100L:100L:2L], unicode('02468', 'ascii')) | |
211 | ||
212 | ||
213 | print '6.5.2 Tuples [see test_tuple.py]' | |
214 | ||
215 | print '6.5.3 Lists [see test_list.py]' | |
216 | ||
217 | print '6.6 Mappings == Dictionaries [see test_dict.py]' | |
218 | ||
219 | ||
220 | try: type(1, 2) | |
221 | except TypeError: pass | |
222 | else: raise TestFailed, 'type(), w/2 args expected TypeError' | |
223 | ||
224 | try: type(1, 2, 3, 4) | |
225 | except TypeError: pass | |
226 | else: raise TestFailed, 'type(), w/4 args expected TypeError' | |
227 | ||
228 | print 'Buffers' | |
229 | try: buffer('asdf', -1) | |
230 | except ValueError: pass | |
231 | else: raise TestFailed, "buffer('asdf', -1) should raise ValueError" | |
232 | ||
233 | try: buffer(None) | |
234 | except TypeError: pass | |
235 | else: raise TestFailed, "buffer(None) should raise TypeError" | |
236 | ||
237 | a = buffer('asdf') | |
238 | hash(a) | |
239 | b = a * 5 | |
240 | if a == b: | |
241 | raise TestFailed, 'buffers should not be equal' | |
242 | if str(b) != ('asdf' * 5): | |
243 | raise TestFailed, 'repeated buffer has wrong content' | |
244 | if str(a * 0) != '': | |
245 | raise TestFailed, 'repeated buffer zero times has wrong content' | |
246 | if str(a + buffer('def')) != 'asdfdef': | |
247 | raise TestFailed, 'concatenation of buffers yields wrong content' | |
248 | if str(buffer(a)) != 'asdf': | |
249 | raise TestFailed, 'composing buffers failed' | |
250 | if str(buffer(a, 2)) != 'df': | |
251 | raise TestFailed, 'specifying buffer offset failed' | |
252 | if str(buffer(a, 0, 2)) != 'as': | |
253 | raise TestFailed, 'specifying buffer size failed' | |
254 | if str(buffer(a, 1, 2)) != 'sd': | |
255 | raise TestFailed, 'specifying buffer offset and size failed' | |
256 | try: buffer(buffer('asdf', 1), -1) | |
257 | except ValueError: pass | |
258 | else: raise TestFailed, "buffer(buffer('asdf', 1), -1) should raise ValueError" | |
259 | if str(buffer(buffer('asdf', 0, 2), 0)) != 'as': | |
260 | raise TestFailed, 'composing length-specified buffer failed' | |
261 | if str(buffer(buffer('asdf', 0, 2), 0, 5000)) != 'as': | |
262 | raise TestFailed, 'composing length-specified buffer failed' | |
263 | if str(buffer(buffer('asdf', 0, 2), 0, -1)) != 'as': | |
264 | raise TestFailed, 'composing length-specified buffer failed' | |
265 | if str(buffer(buffer('asdf', 0, 2), 1, 2)) != 's': | |
266 | raise TestFailed, 'composing length-specified buffer failed' | |
267 | ||
268 | try: a[1] = 'g' | |
269 | except TypeError: pass | |
270 | else: raise TestFailed, "buffer assignment should raise TypeError" | |
271 | ||
272 | try: a[0:1] = 'g' | |
273 | except TypeError: pass | |
274 | else: raise TestFailed, "buffer slice assignment should raise TypeError" |