Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 1 | # Python test set -- part 6, built-in types |
| 2 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame^] | 3 | from test.test_support import * |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 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' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 39 | if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 40 | else: raise TestFailed, 'int comparisons failed' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 41 | if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 42 | else: raise TestFailed, 'long int comparisons failed' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 43 | if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 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 | |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 52 | try: float('') |
| 53 | except ValueError: pass |
| 54 | else: raise TestFailed, "float('') didn't raise ValueError" |
| 55 | |
| 56 | try: float('5\0') |
| 57 | except ValueError: pass |
| 58 | else: raise TestFailed, "float('5\0') didn't raise ValueError" |
| 59 | |
| 60 | try: 5.0 / 0.0 |
| 61 | except ZeroDivisionError: pass |
| 62 | else: raise TestFailed, "5.0 / 0.0 didn't raise ZeroDivisionError" |
| 63 | |
| 64 | try: 5.0 // 0.0 |
| 65 | except ZeroDivisionError: pass |
| 66 | else: raise TestFailed, "5.0 // 0.0 didn't raise ZeroDivisionError" |
| 67 | |
| 68 | try: 5.0 % 0.0 |
| 69 | except ZeroDivisionError: pass |
| 70 | else: raise TestFailed, "5.0 % 0.0 didn't raise ZeroDivisionError" |
| 71 | |
| 72 | try: 5 / 0L |
| 73 | except ZeroDivisionError: pass |
| 74 | else: raise TestFailed, "5 / 0L didn't raise ZeroDivisionError" |
| 75 | |
| 76 | try: 5 // 0L |
| 77 | except ZeroDivisionError: pass |
| 78 | else: raise TestFailed, "5 // 0L didn't raise ZeroDivisionError" |
| 79 | |
| 80 | try: 5 % 0L |
| 81 | except ZeroDivisionError: pass |
| 82 | else: raise TestFailed, "5 % 0L didn't raise ZeroDivisionError" |
| 83 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 84 | print '6.4 Numeric types (mostly conversions)' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 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: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 88 | raise TestFailed, 'int/long/float value not equal' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 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' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 95 | print '6.4.1 32-bit integers' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 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' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 100 | if not 12 < 24: raise TestFailed, 'int op' |
| 101 | if not -24 < -12: raise TestFailed, 'int op' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 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): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 105 | raise TestFailed, 'int mul commutativity' |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 106 | # And another. |
| 107 | m = -sys.maxint - 1 |
| 108 | for divisor in 1, 2, 4, 8, 16, 32: |
Tim Peters | 6d30c3e | 2001-12-05 00:30:09 +0000 | [diff] [blame] | 109 | j = m // divisor |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 110 | prod = divisor * j |
| 111 | if prod != m: |
| 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" % |
| 115 | type(prod)) |
| 116 | # Check for expected * overflow to long. |
| 117 | for divisor in 1, 2, 4, 8, 16, 32: |
Tim Peters | 6d30c3e | 2001-12-05 00:30:09 +0000 | [diff] [blame] | 118 | j = m // divisor - 1 |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 119 | prod = divisor * j |
| 120 | if type(prod) is not long: |
| 121 | raise TestFailed, ("expected type(%r) to be long, not %r" % |
| 122 | (prod, type(prod))) |
| 123 | # Check for expected * overflow to long. |
| 124 | m = sys.maxint |
| 125 | for divisor in 1, 2, 4, 8, 16, 32: |
Tim Peters | 6d30c3e | 2001-12-05 00:30:09 +0000 | [diff] [blame] | 126 | j = m // divisor + 1 |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 127 | prod = divisor * j |
| 128 | if type(prod) is not long: |
| 129 | raise TestFailed, ("expected type(%r) to be long, not %r" % |
| 130 | (prod, type(prod))) |
| 131 | |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 132 | print '6.4.2 Long integers' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 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' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 137 | if not 12L < 24L: raise TestFailed, 'long op' |
| 138 | if not -24L < -12L: raise TestFailed, 'long op' |
Guido van Rossum | 7462942 | 1998-05-26 14:51:55 +0000 | [diff] [blame] | 139 | x = sys.maxint |
| 140 | if int(long(x)) != x: raise TestFailed, 'long op' |
| 141 | try: int(long(x)+1L) |
| 142 | except OverflowError: pass |
| 143 | else:raise TestFailed, 'long op' |
| 144 | x = -x |
| 145 | if int(long(x)) != x: raise TestFailed, 'long op' |
| 146 | x = x-1 |
| 147 | if int(long(x)) != x: raise TestFailed, 'long op' |
| 148 | try: int(long(x)-1L) |
| 149 | except OverflowError: pass |
| 150 | else:raise TestFailed, 'long op' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 151 | |
| 152 | try: 5 << -5 |
| 153 | except ValueError: pass |
| 154 | else: raise TestFailed, 'int negative shift <<' |
| 155 | |
| 156 | try: 5L << -5L |
| 157 | except ValueError: pass |
| 158 | else: raise TestFailed, 'long negative shift <<' |
| 159 | |
| 160 | try: 5 >> -5 |
| 161 | except ValueError: pass |
| 162 | else: raise TestFailed, 'int negative shift >>' |
| 163 | |
| 164 | try: 5L >> -5L |
| 165 | except ValueError: pass |
| 166 | else: raise TestFailed, 'long negative shift >>' |
| 167 | |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 168 | print '6.4.3 Floating point numbers' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 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' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 173 | if not 12.0 < 24.0: raise TestFailed, 'float op' |
| 174 | if not -24.0 < -12.0: raise TestFailed, 'float op' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 175 | |
| 176 | print '6.5 Sequence types' |
| 177 | |
| 178 | print '6.5.1 Strings' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 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' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 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' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 188 | x = 'x'*103 |
| 189 | if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 190 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 191 | #extended slices for strings |
| 192 | a = '0123456789' |
| 193 | vereq(a[::], a) |
| 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') |
| 202 | |
| 203 | if have_unicode: |
| 204 | a = unicode('0123456789', 'ascii') |
| 205 | vereq(a[::], a) |
| 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')) |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 214 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 216 | print '6.5.2 Tuples' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 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' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 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' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 226 | try: ()[0] |
| 227 | except IndexError: pass |
| 228 | else: raise TestFailed, "tuple index error didn't raise IndexError" |
| 229 | x = () |
| 230 | x += () |
| 231 | if x != (): raise TestFailed, 'tuple inplace add from () to () failed' |
| 232 | x += (1,) |
| 233 | if x != (1,): raise TestFailed, 'tuple resize from () failed' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 234 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 235 | # extended slicing - subscript only for tuples |
| 236 | a = (0,1,2,3,4) |
| 237 | vereq(a[::], a) |
| 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)) |
| 246 | |
Guido van Rossum | 10f36d9 | 2002-06-21 02:14:10 +0000 | [diff] [blame] | 247 | # Check that a specific bug in _PyTuple_Resize() is squashed. |
| 248 | def f(): |
| 249 | for i in range(1000): |
| 250 | yield i |
| 251 | vereq(list(tuple(f())), range(1000)) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 252 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 253 | print '6.5.3 Lists' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 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' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 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' |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 265 | a = [1, 2, 3, 4, 5] |
| 266 | a[:-1] = a |
| 267 | if a != [1, 2, 3, 4, 5, 5]: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 268 | raise TestFailed, "list self-slice-assign (head)" |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 269 | a = [1, 2, 3, 4, 5] |
| 270 | a[1:] = a |
| 271 | if a != [1, 1, 2, 3, 4, 5]: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 272 | raise TestFailed, "list self-slice-assign (tail)" |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 273 | a = [1, 2, 3, 4, 5] |
| 274 | a[1:-1] = a |
| 275 | if a != [1, 1, 2, 3, 4, 5, 5]: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 276 | raise TestFailed, "list self-slice-assign (center)" |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 277 | try: [][0] |
| 278 | except IndexError: pass |
| 279 | else: raise TestFailed, "list index error didn't raise IndexError" |
| 280 | try: [][0] = 5 |
| 281 | except IndexError: pass |
| 282 | else: raise TestFailed, "list assignment index error didn't raise IndexError" |
| 283 | try: [].pop() |
| 284 | except IndexError: pass |
| 285 | else: raise TestFailed, "empty list.pop() didn't raise IndexError" |
| 286 | try: [1].pop(5) |
| 287 | except IndexError: pass |
| 288 | else: raise TestFailed, "[1].pop(5) didn't raise IndexError" |
| 289 | try: [][0:1] = 5 |
| 290 | except TypeError: pass |
| 291 | else: raise TestFailed, "bad list slice assignment didn't raise TypeError" |
| 292 | try: [].extend(None) |
| 293 | except TypeError: pass |
| 294 | else: raise TestFailed, "list.extend(None) didn't raise TypeError" |
| 295 | a = [1, 2, 3, 4] |
| 296 | a *= 0 |
| 297 | if a != []: |
| 298 | raise TestFailed, "list inplace repeat" |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 299 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 300 | |
| 301 | print '6.5.3a Additional list operations' |
| 302 | a = [0,1,2,3,4] |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 303 | a[0L] = 1 |
| 304 | a[1L] = 2 |
| 305 | a[2L] = 3 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 306 | 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] | 307 | a[0] = 5 |
| 308 | a[1] = 6 |
| 309 | a[2] = 7 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 310 | 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] | 311 | a[-2L] = 88 |
| 312 | a[-1L] = 99 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 313 | 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] | 314 | a[-2] = 8 |
| 315 | a[-1] = 9 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 316 | if a != [5,6,7,8,9]: raise TestFailed, 'list item assignment [-2], [-1]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 317 | a[:2] = [0,4] |
| 318 | a[-3:] = [] |
| 319 | a[1:1] = [1,2,3] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 320 | 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] | 321 | a[ 1L : 4L] = [7,8,9] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 322 | 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] | 323 | del a[1:4] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 324 | if a != [0,4]: raise TestFailed, 'list slice deletion' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 325 | del a[0] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 326 | if a != [4]: raise TestFailed, 'list item deletion [0]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 327 | del a[-1] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 328 | if a != []: raise TestFailed, 'list item deletion [-1]' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 329 | a=range(0,5) |
| 330 | del a[1L:4L] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 331 | if a != [0,4]: raise TestFailed, 'list slice deletion' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 332 | del a[0L] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 333 | if a != [4]: raise TestFailed, 'list item deletion [0]' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 334 | del a[-1L] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 335 | if a != []: raise TestFailed, 'list item deletion [-1]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 336 | a.append(0) |
| 337 | a.append(1) |
| 338 | a.append(2) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 339 | if a != [0,1,2]: raise TestFailed, 'list append' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 340 | a.insert(0, -2) |
| 341 | a.insert(1, -1) |
| 342 | a.insert(2,0) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 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' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 346 | a.remove(0) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 347 | if a != [-2,-1,0,1,2]: raise TestFailed, 'list remove' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 348 | a.reverse() |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 349 | if a != [2,1,0,-1,-2]: raise TestFailed, 'list reverse' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 350 | a.sort() |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 351 | if a != [-2,-1,0,1,2]: raise TestFailed, 'list sort' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 352 | def revcmp(a, b): return cmp(b, a) |
| 353 | a.sort(revcmp) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 354 | 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] | 355 | # The following dumps core in unpatched Python 1.5: |
| 356 | def myComparison(x,y): |
| 357 | return cmp(x%3, y%7) |
| 358 | z = range(12) |
| 359 | z.sort(myComparison) |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 360 | |
Neal Norwitz | 6fc36c5 | 2002-06-13 22:23:06 +0000 | [diff] [blame] | 361 | try: z.sort(2) |
| 362 | except TypeError: pass |
| 363 | else: raise TestFailed, 'list sort compare function is not callable' |
| 364 | |
| 365 | def selfmodifyingComparison(x,y): |
| 366 | z[0] = 1 |
| 367 | return cmp(x, y) |
| 368 | try: z.sort(selfmodifyingComparison) |
| 369 | except TypeError: pass |
| 370 | else: raise TestFailed, 'modifying list during sort' |
| 371 | |
| 372 | try: z.sort(lambda x, y: 's') |
| 373 | except TypeError: pass |
| 374 | else: raise TestFailed, 'list sort compare function does not return int' |
| 375 | |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 376 | # Test extreme cases with long ints |
| 377 | a = [0,1,2,3,4] |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 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" |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 382 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 383 | |
| 384 | # extended slicing |
| 385 | |
| 386 | # subscript |
| 387 | a = [0,1,2,3,4] |
| 388 | vereq(a[::], a) |
| 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]) |
Michael W. Hudson | 589dc93 | 2002-06-11 13:38:42 +0000 | [diff] [blame] | 397 | vereq(a[1000:2000:2], []) |
| 398 | vereq(a[-1000:-2000:-2], []) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 399 | # deletion |
| 400 | del a[::2] |
| 401 | vereq(a, [1,3]) |
| 402 | a = range(5) |
| 403 | del a[1::2] |
| 404 | vereq(a, [0,2,4]) |
| 405 | a = range(5) |
| 406 | del a[1::-2] |
| 407 | vereq(a, [0,2,3,4]) |
| 408 | # assignment |
| 409 | a = range(10) |
| 410 | a[::2] = [-1]*5 |
| 411 | vereq(a, [-1, 1, -1, 3, -1, 5, -1, 7, -1, 9]) |
| 412 | a = range(10) |
| 413 | a[::-4] = [10]*3 |
| 414 | vereq(a, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10]) |
| 415 | a = range(4) |
| 416 | a[::-1] = a |
| 417 | vereq(a, [3, 2, 1, 0]) |
Michael W. Hudson | 9c14bad | 2002-06-19 15:44:15 +0000 | [diff] [blame] | 418 | a = range(10) |
| 419 | b = a[:] |
| 420 | c = a[:] |
| 421 | a[2:3] = ["two", "elements"] |
| 422 | b[slice(2,3)] = ["two", "elements"] |
| 423 | c[2:3:] = ["two", "elements"] |
| 424 | vereq(a, b) |
| 425 | vereq(a, c) |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 426 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 427 | print '6.6 Mappings == Dictionaries' |
| 428 | d = {} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 429 | if d.keys() != []: raise TestFailed, '{}.keys()' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 430 | if d.values() != []: raise TestFailed, '{}.values()' |
| 431 | if d.items() != []: raise TestFailed, '{}.items()' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 432 | if d.has_key('a') != 0: raise TestFailed, '{}.has_key(\'a\')' |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 433 | if ('a' in d) != 0: raise TestFailed, "'a' in {}" |
| 434 | if ('a' not in d) != 1: raise TestFailed, "'a' not in {}" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 435 | if len(d) != 0: raise TestFailed, 'len({})' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 436 | d = {'a': 1, 'b': 2} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 437 | if len(d) != 2: raise TestFailed, 'len(dict)' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 438 | k = d.keys() |
| 439 | k.sort() |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 440 | if k != ['a', 'b']: raise TestFailed, 'dict keys()' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 441 | if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass |
| 442 | else: raise TestFailed, 'dict keys()' |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 443 | if 'a' in d and 'b' in d and 'c' not in d: pass |
| 444 | else: raise TestFailed, 'dict keys() # in/not in version' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 445 | if d['a'] != 1 or d['b'] != 2: raise TestFailed, 'dict item' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 446 | d['c'] = 3 |
| 447 | d['a'] = 4 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 448 | if d['c'] != 3 or d['a'] != 4: raise TestFailed, 'dict item assignment' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 449 | del d['b'] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 450 | if d != {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 451 | # dict.clear() |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 452 | d = {1:1, 2:2, 3:3} |
| 453 | d.clear() |
| 454 | if d != {}: raise TestFailed, 'dict clear' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 455 | # dict.update() |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 456 | d.update({1:100}) |
| 457 | d.update({2:20}) |
| 458 | d.update({1:1, 2:2, 3:3}) |
| 459 | if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 460 | d.clear() |
| 461 | try: d.update(None) |
| 462 | except AttributeError: pass |
| 463 | else: raise TestFailed, 'dict.update(None), AttributeError expected' |
| 464 | class SimpleUserDict: |
| 465 | def __init__(self): |
| 466 | self.d = {1:1, 2:2, 3:3} |
| 467 | def keys(self): |
| 468 | return self.d.keys() |
| 469 | def __getitem__(self, i): |
| 470 | return self.d[i] |
| 471 | d.update(SimpleUserDict()) |
| 472 | if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict.update(instance)' |
| 473 | d.clear() |
| 474 | class FailingUserDict: |
| 475 | def keys(self): |
| 476 | raise ValueError |
| 477 | try: d.update(FailingUserDict()) |
| 478 | except ValueError: pass |
| 479 | else: raise TestFailed, 'dict.keys() expected ValueError' |
| 480 | class FailingUserDict: |
| 481 | def keys(self): |
| 482 | class BogonIter: |
| 483 | def __iter__(self): |
| 484 | raise ValueError |
| 485 | return BogonIter() |
| 486 | try: d.update(FailingUserDict()) |
| 487 | except ValueError: pass |
| 488 | else: raise TestFailed, 'iter(dict.keys()) expected ValueError' |
| 489 | class FailingUserDict: |
| 490 | def keys(self): |
| 491 | class BogonIter: |
| 492 | def __init__(self): |
| 493 | self.i = 1 |
| 494 | def __iter__(self): |
| 495 | return self |
| 496 | def next(self): |
| 497 | if self.i: |
| 498 | self.i = 0 |
| 499 | return 'a' |
| 500 | raise ValueError |
| 501 | return BogonIter() |
| 502 | def __getitem__(self, key): |
| 503 | return key |
| 504 | try: d.update(FailingUserDict()) |
| 505 | except ValueError: pass |
| 506 | else: raise TestFailed, 'iter(dict.keys()).next() expected ValueError' |
| 507 | class FailingUserDict: |
| 508 | def keys(self): |
| 509 | class BogonIter: |
| 510 | def __init__(self): |
| 511 | self.i = ord('a') |
| 512 | def __iter__(self): |
| 513 | return self |
| 514 | def next(self): |
| 515 | if self.i <= ord('z'): |
| 516 | rtn = chr(self.i) |
| 517 | self.i += 1 |
| 518 | return rtn |
| 519 | raise StopIteration |
| 520 | return BogonIter() |
| 521 | def __getitem__(self, key): |
| 522 | raise ValueError |
| 523 | try: d.update(FailingUserDict()) |
| 524 | except ValueError: pass |
| 525 | else: raise TestFailed, 'dict.update(), __getitem__ expected ValueError' |
| 526 | # dict.copy() |
| 527 | d = {1:1, 2:2, 3:3} |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 528 | if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy' |
| 529 | if {}.copy() != {}: raise TestFailed, 'empty dict copy' |
Barry Warsaw | 9b887c7 | 1997-10-20 17:34:43 +0000 | [diff] [blame] | 530 | # dict.get() |
Guido van Rossum | fb5cef1 | 1997-10-20 20:10:43 +0000 | [diff] [blame] | 531 | d = {} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 532 | if d.get('c') is not None: raise TestFailed, 'missing {} get, no 2nd arg' |
Guido van Rossum | fb5cef1 | 1997-10-20 20:10:43 +0000 | [diff] [blame] | 533 | 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] | 534 | d = {'a' : 1, 'b' : 2} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 535 | if d.get('c') is not None: raise TestFailed, 'missing dict get, no 2nd arg' |
Barry Warsaw | 9b887c7 | 1997-10-20 17:34:43 +0000 | [diff] [blame] | 536 | if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg' |
| 537 | if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg' |
| 538 | if d.get('a', 3) != 1: raise TestFailed, 'present dict get, w/ 2nd arg' |
Guido van Rossum | 79c9b17 | 2000-08-08 16:13:23 +0000 | [diff] [blame] | 539 | # dict.setdefault() |
| 540 | d = {} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 541 | if d.setdefault('key0') is not None: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 542 | raise TestFailed, 'missing {} setdefault, no 2nd arg' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 543 | if d.setdefault('key0') is not None: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 544 | raise TestFailed, 'present {} setdefault, no 2nd arg' |
Guido van Rossum | 79c9b17 | 2000-08-08 16:13:23 +0000 | [diff] [blame] | 545 | d.setdefault('key', []).append(3) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 546 | if d['key'][0] != 3: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 547 | raise TestFailed, 'missing {} setdefault, w/ 2nd arg' |
Guido van Rossum | 79c9b17 | 2000-08-08 16:13:23 +0000 | [diff] [blame] | 548 | d.setdefault('key', []).append(4) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 549 | if len(d['key']) != 2: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 550 | raise TestFailed, 'present {} setdefault, w/ 2nd arg' |
Guido van Rossum | b822c61 | 2000-12-12 22:02:59 +0000 | [diff] [blame] | 551 | # dict.popitem() |
| 552 | for copymode in -1, +1: |
| 553 | # -1: b has same structure as a |
| 554 | # +1: b is a.copy() |
| 555 | for log2size in range(12): |
| 556 | size = 2**log2size |
| 557 | a = {} |
| 558 | b = {} |
| 559 | for i in range(size): |
| 560 | a[`i`] = i |
| 561 | if copymode < 0: |
| 562 | b[`i`] = i |
| 563 | if copymode > 0: |
| 564 | b = a.copy() |
| 565 | for i in range(size): |
| 566 | ka, va = ta = a.popitem() |
| 567 | if va != int(ka): raise TestFailed, "a.popitem: %s" % str(ta) |
| 568 | kb, vb = tb = b.popitem() |
| 569 | if vb != int(kb): raise TestFailed, "b.popitem: %s" % str(tb) |
| 570 | if copymode < 0 and ta != tb: |
| 571 | raise TestFailed, "a.popitem != b.popitem: %s, %s" % ( |
| 572 | str(ta), str(tb)) |
| 573 | if a: raise TestFailed, 'a not empty after popitems: %s' % str(a) |
| 574 | if b: raise TestFailed, 'b not empty after popitems: %s' % str(b) |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 575 | |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 576 | d.clear() |
| 577 | try: d.popitem() |
| 578 | except KeyError: pass |
| 579 | else: raise TestFailed, "{}.popitem doesn't raise KeyError" |
| 580 | |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 581 | # Tests for pop with specified key |
| 582 | d.clear() |
| 583 | k, v = 'abc', 'def' |
| 584 | d[k] = v |
| 585 | try: d.pop('ghi') |
| 586 | except KeyError: pass |
| 587 | else: raise TestFailed, "{}.pop(k) doesn't raise KeyError when k not in dictionary" |
| 588 | |
| 589 | if d.pop(k) != v: raise TestFailed, "{}.pop(k) doesn't find known key/value pair" |
| 590 | if len(d) > 0: raise TestFailed, "{}.pop(k) failed to remove the specified pair" |
| 591 | |
| 592 | try: d.pop(k) |
| 593 | except KeyError: pass |
| 594 | else: raise TestFailed, "{}.pop(k) doesn't raise KeyError when dictionary is empty" |
| 595 | |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 596 | d[1] = 1 |
| 597 | try: |
| 598 | for i in d: |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 599 | d[i+1] = 1 |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 600 | except RuntimeError: |
| 601 | pass |
| 602 | else: |
| 603 | raise TestFailed, "changing dict size during iteration doesn't raise Error" |
| 604 | |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 605 | try: type(1, 2) |
| 606 | except TypeError: pass |
| 607 | else: raise TestFailed, 'type(), w/2 args expected TypeError' |
| 608 | |
| 609 | try: type(1, 2, 3, 4) |
| 610 | except TypeError: pass |
| 611 | else: raise TestFailed, 'type(), w/4 args expected TypeError' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 612 | |
| 613 | print 'Buffers' |
| 614 | try: buffer('asdf', -1) |
| 615 | except ValueError: pass |
| 616 | else: raise TestFailed, "buffer('asdf', -1) should raise ValueError" |
| 617 | |
| 618 | try: buffer(None) |
| 619 | except TypeError: pass |
| 620 | else: raise TestFailed, "buffer(None) should raise TypeError" |
| 621 | |
| 622 | a = buffer('asdf') |
| 623 | hash(a) |
| 624 | b = a * 5 |
| 625 | if a == b: |
| 626 | raise TestFailed, 'buffers should not be equal' |
Fred Drake | 485f340 | 2002-05-02 04:27:20 +0000 | [diff] [blame] | 627 | if str(b) != ('asdf' * 5): |
| 628 | raise TestFailed, 'repeated buffer has wrong content' |
| 629 | if str(a * 0) != '': |
| 630 | raise TestFailed, 'repeated buffer zero times has wrong content' |
| 631 | if str(a + buffer('def')) != 'asdfdef': |
| 632 | raise TestFailed, 'concatenation of buffers yields wrong content' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 633 | |
| 634 | try: a[1] = 'g' |
| 635 | except TypeError: pass |
| 636 | else: raise TestFailed, "buffer assignment should raise TypeError" |
| 637 | |
| 638 | try: a[0:1] = 'g' |
| 639 | except TypeError: pass |
| 640 | else: raise TestFailed, "buffer slice assignment should raise TypeError" |