Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 1 | # Python test set -- part 6, built-in types |
| 2 | |
| 3 | from 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 (): 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 |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 24 | class C: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 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' |
| 31 | |
| 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' |
| 37 | |
| 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' |
| 51 | |
| 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' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 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' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 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' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 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' |
Guido van Rossum | 7462942 | 1998-05-26 14:51:55 +0000 | [diff] [blame] | 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' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 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' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 100 | |
| 101 | print '6.5 Sequence types' |
| 102 | |
| 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' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 113 | x = 'x'*103 |
| 114 | if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 115 | |
| 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' |
| 126 | |
| 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' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 133 | if [1,2]*3L <> [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3L' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 134 | if 0*[1,2,3] <> []: raise TestFailed, 'list repetition 0*' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 135 | if 0L*[1,2,3] <> []: raise TestFailed, 'list repetition 0L*' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 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' |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 139 | a = [1, 2, 3, 4, 5] |
| 140 | a[:-1] = a |
| 141 | if a != [1, 2, 3, 4, 5, 5]: |
| 142 | raise TestFailed, "list self-slice-assign (head)" |
| 143 | a = [1, 2, 3, 4, 5] |
| 144 | a[1:] = a |
| 145 | if a != [1, 1, 2, 3, 4, 5]: |
| 146 | raise TestFailed, "list self-slice-assign (tail)" |
| 147 | a = [1, 2, 3, 4, 5] |
| 148 | a[1:-1] = a |
| 149 | if a != [1, 1, 2, 3, 4, 5, 5]: |
| 150 | raise TestFailed, "list self-slice-assign (center)" |
| 151 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 152 | |
| 153 | print '6.5.3a Additional list operations' |
| 154 | a = [0,1,2,3,4] |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 155 | a[0L] = 1 |
| 156 | a[1L] = 2 |
| 157 | a[2L] = 3 |
| 158 | if a <> [1,2,3,3,4]: raise TestFailed, 'list item assignment [0L], [1L], [2L]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 159 | a[0] = 5 |
| 160 | a[1] = 6 |
| 161 | a[2] = 7 |
| 162 | if a <> [5,6,7,3,4]: raise TestFailed, 'list item assignment [0], [1], [2]' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 163 | a[-2L] = 88 |
| 164 | a[-1L] = 99 |
| 165 | if a <> [5,6,7,88,99]: raise TestFailed, 'list item assignment [-2L], [-1L]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 166 | a[-2] = 8 |
| 167 | a[-1] = 9 |
| 168 | if a <> [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]' |
| 169 | a[:2] = [0,4] |
| 170 | a[-3:] = [] |
| 171 | a[1:1] = [1,2,3] |
| 172 | if a <> [0,1,2,3,4]: raise TestFailed, 'list slice assignment' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 173 | a[ 1L : 4L] = [7,8,9] |
| 174 | if a <> [0,7,8,9,4]: raise TestFailed, 'list slice assignment using long ints' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 175 | del a[1:4] |
| 176 | if a <> [0,4]: raise TestFailed, 'list slice deletion' |
| 177 | del a[0] |
| 178 | if a <> [4]: raise TestFailed, 'list item deletion [0]' |
| 179 | del a[-1] |
| 180 | if a <> []: raise TestFailed, 'list item deletion [-1]' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 181 | a=range(0,5) |
| 182 | del a[1L:4L] |
| 183 | if a <> [0,4]: raise TestFailed, 'list slice deletion' |
| 184 | del a[0L] |
| 185 | if a <> [4]: raise TestFailed, 'list item deletion [0]' |
| 186 | del a[-1L] |
| 187 | if a <> []: raise TestFailed, 'list item deletion [-1]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 188 | a.append(0) |
| 189 | a.append(1) |
| 190 | a.append(2) |
| 191 | if a <> [0,1,2]: raise TestFailed, 'list append' |
| 192 | a.insert(0, -2) |
| 193 | a.insert(1, -1) |
| 194 | a.insert(2,0) |
| 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' |
| 198 | a.remove(0) |
| 199 | if a <> [-2,-1,0,1,2]: raise TestFailed, 'list remove' |
| 200 | a.reverse() |
| 201 | if a <> [2,1,0,-1,-2]: raise TestFailed, 'list reverse' |
| 202 | a.sort() |
| 203 | if a <> [-2,-1,0,1,2]: raise TestFailed, 'list sort' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 204 | def revcmp(a, b): return cmp(b, a) |
| 205 | a.sort(revcmp) |
| 206 | if a <> [2,1,0,-1,-2]: raise TestFailed, 'list sort with cmp func' |
Guido van Rossum | d151d34 | 1998-02-25 17:51:50 +0000 | [diff] [blame] | 207 | # The following dumps core in unpatched Python 1.5: |
| 208 | def myComparison(x,y): |
| 209 | return cmp(x%3, y%7) |
| 210 | z = range(12) |
| 211 | z.sort(myComparison) |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 212 | |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 213 | # Test extreme cases with long ints |
| 214 | a = [0,1,2,3,4] |
| 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" |
| 219 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 220 | print '6.6 Mappings == Dictionaries' |
| 221 | d = {} |
| 222 | if d.keys() <> []: raise TestFailed, '{}.keys()' |
Guido van Rossum | eecf035 | 1994-12-30 17:17:46 +0000 | [diff] [blame] | 223 | if d.has_key('a') <> 0: raise TestFailed, '{}.has_key(\'a\')' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 224 | if len(d) <> 0: raise TestFailed, 'len({})' |
| 225 | d = {'a': 1, 'b': 2} |
| 226 | if len(d) <> 2: raise TestFailed, 'len(dict)' |
| 227 | k = d.keys() |
| 228 | k.sort() |
| 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' |
| 233 | d['c'] = 3 |
| 234 | d['a'] = 4 |
| 235 | if d['c'] <> 3 or d['a'] <> 4: raise TestFailed, 'dict item assignment' |
| 236 | del d['b'] |
| 237 | if d <> {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion' |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 238 | d = {1:1, 2:2, 3:3} |
| 239 | d.clear() |
| 240 | if d != {}: raise TestFailed, 'dict clear' |
| 241 | d.update({1:100}) |
| 242 | d.update({2:20}) |
| 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' |
Barry Warsaw | 9b887c7 | 1997-10-20 17:34:43 +0000 | [diff] [blame] | 247 | # dict.get() |
Guido van Rossum | fb5cef1 | 1997-10-20 20:10:43 +0000 | [diff] [blame] | 248 | d = {} |
| 249 | if d.get('c') != None: raise TestFailed, 'missing {} get, no 2nd arg' |
| 250 | if d.get('c', 3) != 3: raise TestFailed, 'missing {} get, w/ 2nd arg' |
Barry Warsaw | 9b887c7 | 1997-10-20 17:34:43 +0000 | [diff] [blame] | 251 | d = {'a' : 1, 'b' : 2} |
| 252 | if d.get('c') != 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' |