Class around PixMap objects that allows more python-like access. By Joe Strout.
[python/dscho.git] / Lib / test / test_types.py
blob072e6d2052fd227c4a6d2dc856dbac47e4fe2038
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'
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 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'
81 x = sys.maxint
82 if int(long(x)) != x: raise TestFailed, 'long op'
83 try: int(long(x)+1L)
84 except OverflowError: pass
85 else:raise TestFailed, 'long op'
86 x = -x
87 if int(long(x)) != x: raise TestFailed, 'long op'
88 x = x-1
89 if int(long(x)) != x: raise TestFailed, 'long op'
90 try: int(long(x)-1L)
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'
113 x = 'x'*103
114 if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug'
116 print '6.5.2 Tuples'
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'
127 print '6.5.3 Lists'
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 0*[1,2,3] <> []: raise TestFailed, 'list repetition 0*'
134 if min([1,2]) <> 1 or max([1,2]) <> 2: raise TestFailed, 'min/max list'
135 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
136 else: raise TestFailed, 'in/not in list'
137 a = [1, 2, 3, 4, 5]
138 a[:-1] = a
139 if a != [1, 2, 3, 4, 5, 5]:
140 raise TestFailed, "list self-slice-assign (head)"
141 a = [1, 2, 3, 4, 5]
142 a[1:] = a
143 if a != [1, 1, 2, 3, 4, 5]:
144 raise TestFailed, "list self-slice-assign (tail)"
145 a = [1, 2, 3, 4, 5]
146 a[1:-1] = a
147 if a != [1, 1, 2, 3, 4, 5, 5]:
148 raise TestFailed, "list self-slice-assign (center)"
151 print '6.5.3a Additional list operations'
152 a = [0,1,2,3,4]
153 a[0] = 5
154 a[1] = 6
155 a[2] = 7
156 if a <> [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]'
157 a[-2] = 8
158 a[-1] = 9
159 if a <> [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]'
160 a[:2] = [0,4]
161 a[-3:] = []
162 a[1:1] = [1,2,3]
163 if a <> [0,1,2,3,4]: raise TestFailed, 'list slice assignment'
164 del a[1:4]
165 if a <> [0,4]: raise TestFailed, 'list slice deletion'
166 del a[0]
167 if a <> [4]: raise TestFailed, 'list item deletion [0]'
168 del a[-1]
169 if a <> []: raise TestFailed, 'list item deletion [-1]'
170 a.append(0)
171 a.append(1)
172 a.append(2)
173 if a <> [0,1,2]: raise TestFailed, 'list append'
174 a.insert(0, -2)
175 a.insert(1, -1)
176 a.insert(2,0)
177 if a <> [-2,-1,0,0,1,2]: raise TestFailed, 'list insert'
178 if a.count(0) <> 2: raise TestFailed, ' list count'
179 if a.index(0) <> 2: raise TestFailed, 'list index'
180 a.remove(0)
181 if a <> [-2,-1,0,1,2]: raise TestFailed, 'list remove'
182 a.reverse()
183 if a <> [2,1,0,-1,-2]: raise TestFailed, 'list reverse'
184 a.sort()
185 if a <> [-2,-1,0,1,2]: raise TestFailed, 'list sort'
186 def revcmp(a, b): return cmp(b, a)
187 a.sort(revcmp)
188 if a <> [2,1,0,-1,-2]: raise TestFailed, 'list sort with cmp func'
189 # The following dumps core in unpatched Python 1.5:
190 def myComparison(x,y):
191 return cmp(x%3, y%7)
192 z = range(12)
193 z.sort(myComparison)
195 print '6.6 Mappings == Dictionaries'
196 d = {}
197 if d.keys() <> []: raise TestFailed, '{}.keys()'
198 if d.has_key('a') <> 0: raise TestFailed, '{}.has_key(\'a\')'
199 if len(d) <> 0: raise TestFailed, 'len({})'
200 d = {'a': 1, 'b': 2}
201 if len(d) <> 2: raise TestFailed, 'len(dict)'
202 k = d.keys()
203 k.sort()
204 if k <> ['a', 'b']: raise TestFailed, 'dict keys()'
205 if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass
206 else: raise TestFailed, 'dict keys()'
207 if d['a'] <> 1 or d['b'] <> 2: raise TestFailed, 'dict item'
208 d['c'] = 3
209 d['a'] = 4
210 if d['c'] <> 3 or d['a'] <> 4: raise TestFailed, 'dict item assignment'
211 del d['b']
212 if d <> {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion'
213 d = {1:1, 2:2, 3:3}
214 d.clear()
215 if d != {}: raise TestFailed, 'dict clear'
216 d.update({1:100})
217 d.update({2:20})
218 d.update({1:1, 2:2, 3:3})
219 if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update'
220 if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy'
221 if {}.copy() != {}: raise TestFailed, 'empty dict copy'
222 # dict.get()
223 d = {}
224 if d.get('c') != None: raise TestFailed, 'missing {} get, no 2nd arg'
225 if d.get('c', 3) != 3: raise TestFailed, 'missing {} get, w/ 2nd arg'
226 d = {'a' : 1, 'b' : 2}
227 if d.get('c') != None: raise TestFailed, 'missing dict get, no 2nd arg'
228 if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg'
229 if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg'
230 if d.get('a', 3) != 1: raise TestFailed, 'present dict get, w/ 2nd arg'