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' |
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 | |
| 191 | print '6.5.2 Tuples' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 192 | if len(()) != 0: raise TestFailed, 'len(())' |
| 193 | if len((1,)) != 1: raise TestFailed, 'len((1,))' |
| 194 | if len((1,2,3,4,5,6)) != 6: raise TestFailed, 'len((1,2,3,4,5,6))' |
| 195 | if (1,2)+(3,4) != (1,2,3,4): raise TestFailed, 'tuple concatenation' |
| 196 | if (1,2)*3 != (1,2,1,2,1,2): raise TestFailed, 'tuple repetition *3' |
| 197 | if 0*(1,2,3) != (): raise TestFailed, 'tuple repetition 0*' |
| 198 | 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] | 199 | 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 |
| 200 | else: raise TestFailed, 'in/not in tuple' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 201 | try: ()[0] |
| 202 | except IndexError: pass |
| 203 | else: raise TestFailed, "tuple index error didn't raise IndexError" |
| 204 | x = () |
| 205 | x += () |
| 206 | if x != (): raise TestFailed, 'tuple inplace add from () to () failed' |
| 207 | x += (1,) |
| 208 | if x != (1,): raise TestFailed, 'tuple resize from () failed' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 209 | |
| 210 | print '6.5.3 Lists' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 211 | if len([]) != 0: raise TestFailed, 'len([])' |
| 212 | if len([1,]) != 1: raise TestFailed, 'len([1,])' |
| 213 | if len([1,2,3,4,5,6]) != 6: raise TestFailed, 'len([1,2,3,4,5,6])' |
| 214 | if [1,2]+[3,4] != [1,2,3,4]: raise TestFailed, 'list concatenation' |
| 215 | if [1,2]*3 != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3' |
| 216 | if [1,2]*3L != [1,2,1,2,1,2]: raise TestFailed, 'list repetition *3L' |
| 217 | if 0*[1,2,3] != []: raise TestFailed, 'list repetition 0*' |
| 218 | if 0L*[1,2,3] != []: raise TestFailed, 'list repetition 0L*' |
| 219 | 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] | 220 | 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 |
| 221 | else: raise TestFailed, 'in/not in list' |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 222 | a = [1, 2, 3, 4, 5] |
| 223 | a[:-1] = a |
| 224 | if a != [1, 2, 3, 4, 5, 5]: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 225 | raise TestFailed, "list self-slice-assign (head)" |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 226 | a = [1, 2, 3, 4, 5] |
| 227 | a[1:] = a |
| 228 | if a != [1, 1, 2, 3, 4, 5]: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 229 | raise TestFailed, "list self-slice-assign (tail)" |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 230 | a = [1, 2, 3, 4, 5] |
| 231 | a[1:-1] = a |
| 232 | if a != [1, 1, 2, 3, 4, 5, 5]: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 233 | raise TestFailed, "list self-slice-assign (center)" |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 234 | try: [][0] |
| 235 | except IndexError: pass |
| 236 | else: raise TestFailed, "list index error didn't raise IndexError" |
| 237 | try: [][0] = 5 |
| 238 | except IndexError: pass |
| 239 | else: raise TestFailed, "list assignment index error didn't raise IndexError" |
| 240 | try: [].pop() |
| 241 | except IndexError: pass |
| 242 | else: raise TestFailed, "empty list.pop() didn't raise IndexError" |
| 243 | try: [1].pop(5) |
| 244 | except IndexError: pass |
| 245 | else: raise TestFailed, "[1].pop(5) didn't raise IndexError" |
| 246 | try: [][0:1] = 5 |
| 247 | except TypeError: pass |
| 248 | else: raise TestFailed, "bad list slice assignment didn't raise TypeError" |
| 249 | try: [].extend(None) |
| 250 | except TypeError: pass |
| 251 | else: raise TestFailed, "list.extend(None) didn't raise TypeError" |
| 252 | a = [1, 2, 3, 4] |
| 253 | a *= 0 |
| 254 | if a != []: |
| 255 | raise TestFailed, "list inplace repeat" |
Guido van Rossum | affd77f | 1998-07-16 15:29:06 +0000 | [diff] [blame] | 256 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 257 | |
| 258 | print '6.5.3a Additional list operations' |
| 259 | a = [0,1,2,3,4] |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 260 | a[0L] = 1 |
| 261 | a[1L] = 2 |
| 262 | a[2L] = 3 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 263 | 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] | 264 | a[0] = 5 |
| 265 | a[1] = 6 |
| 266 | a[2] = 7 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 267 | 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] | 268 | a[-2L] = 88 |
| 269 | a[-1L] = 99 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 270 | 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] | 271 | a[-2] = 8 |
| 272 | a[-1] = 9 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 273 | 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] | 274 | a[:2] = [0,4] |
| 275 | a[-3:] = [] |
| 276 | a[1:1] = [1,2,3] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 277 | 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] | 278 | a[ 1L : 4L] = [7,8,9] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 279 | 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] | 280 | del a[1:4] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 281 | if a != [0,4]: raise TestFailed, 'list slice deletion' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 282 | del a[0] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 283 | if a != [4]: raise TestFailed, 'list item deletion [0]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 284 | del a[-1] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 285 | if a != []: raise TestFailed, 'list item deletion [-1]' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 286 | a=range(0,5) |
| 287 | del a[1L:4L] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 288 | if a != [0,4]: raise TestFailed, 'list slice deletion' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 289 | del a[0L] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 290 | if a != [4]: raise TestFailed, 'list item deletion [0]' |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 291 | del a[-1L] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 292 | if a != []: raise TestFailed, 'list item deletion [-1]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 293 | a.append(0) |
| 294 | a.append(1) |
| 295 | a.append(2) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 296 | if a != [0,1,2]: raise TestFailed, 'list append' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 297 | a.insert(0, -2) |
| 298 | a.insert(1, -1) |
| 299 | a.insert(2,0) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 300 | if a != [-2,-1,0,0,1,2]: raise TestFailed, 'list insert' |
| 301 | if a.count(0) != 2: raise TestFailed, ' list count' |
| 302 | if a.index(0) != 2: raise TestFailed, 'list index' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 303 | a.remove(0) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 304 | if a != [-2,-1,0,1,2]: raise TestFailed, 'list remove' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 305 | a.reverse() |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 306 | if a != [2,1,0,-1,-2]: raise TestFailed, 'list reverse' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 307 | a.sort() |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 308 | if a != [-2,-1,0,1,2]: raise TestFailed, 'list sort' |
Guido van Rossum | e61fa0a | 1993-10-22 13:56:35 +0000 | [diff] [blame] | 309 | def revcmp(a, b): return cmp(b, a) |
| 310 | a.sort(revcmp) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 311 | 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] | 312 | # The following dumps core in unpatched Python 1.5: |
| 313 | def myComparison(x,y): |
| 314 | return cmp(x%3, y%7) |
| 315 | z = range(12) |
| 316 | z.sort(myComparison) |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 317 | |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 318 | # Test extreme cases with long ints |
| 319 | a = [0,1,2,3,4] |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 320 | if a[ -pow(2,128L): 3 ] != [0,1,2]: |
| 321 | raise TestFailed, "list slicing with too-small long integer" |
| 322 | if a[ 3: pow(2,145L) ] != [3,4]: |
| 323 | raise TestFailed, "list slicing with too-large long integer" |
Andrew M. Kuchling | 5ebfa2a | 2000-02-23 22:23:17 +0000 | [diff] [blame] | 324 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 325 | print '6.6 Mappings == Dictionaries' |
| 326 | d = {} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 327 | if d.keys() != []: raise TestFailed, '{}.keys()' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 328 | if d.values() != []: raise TestFailed, '{}.values()' |
| 329 | if d.items() != []: raise TestFailed, '{}.items()' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 330 | if d.has_key('a') != 0: raise TestFailed, '{}.has_key(\'a\')' |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 331 | if ('a' in d) != 0: raise TestFailed, "'a' in {}" |
| 332 | if ('a' not in d) != 1: raise TestFailed, "'a' not in {}" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 333 | if len(d) != 0: raise TestFailed, 'len({})' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 334 | d = {'a': 1, 'b': 2} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 335 | if len(d) != 2: raise TestFailed, 'len(dict)' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 336 | k = d.keys() |
| 337 | k.sort() |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 338 | if k != ['a', 'b']: raise TestFailed, 'dict keys()' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 339 | if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass |
| 340 | else: raise TestFailed, 'dict keys()' |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 341 | if 'a' in d and 'b' in d and 'c' not in d: pass |
| 342 | else: raise TestFailed, 'dict keys() # in/not in version' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 343 | 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] | 344 | d['c'] = 3 |
| 345 | d['a'] = 4 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 346 | 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] | 347 | del d['b'] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 348 | if d != {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 349 | # dict.clear() |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 350 | d = {1:1, 2:2, 3:3} |
| 351 | d.clear() |
| 352 | if d != {}: raise TestFailed, 'dict clear' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 353 | # dict.update() |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 354 | d.update({1:100}) |
| 355 | d.update({2:20}) |
| 356 | d.update({1:1, 2:2, 3:3}) |
| 357 | if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 358 | d.clear() |
| 359 | try: d.update(None) |
| 360 | except AttributeError: pass |
| 361 | else: raise TestFailed, 'dict.update(None), AttributeError expected' |
| 362 | class SimpleUserDict: |
| 363 | def __init__(self): |
| 364 | self.d = {1:1, 2:2, 3:3} |
| 365 | def keys(self): |
| 366 | return self.d.keys() |
| 367 | def __getitem__(self, i): |
| 368 | return self.d[i] |
| 369 | d.update(SimpleUserDict()) |
| 370 | if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict.update(instance)' |
| 371 | d.clear() |
| 372 | class FailingUserDict: |
| 373 | def keys(self): |
| 374 | raise ValueError |
| 375 | try: d.update(FailingUserDict()) |
| 376 | except ValueError: pass |
| 377 | else: raise TestFailed, 'dict.keys() expected ValueError' |
| 378 | class FailingUserDict: |
| 379 | def keys(self): |
| 380 | class BogonIter: |
| 381 | def __iter__(self): |
| 382 | raise ValueError |
| 383 | return BogonIter() |
| 384 | try: d.update(FailingUserDict()) |
| 385 | except ValueError: pass |
| 386 | else: raise TestFailed, 'iter(dict.keys()) expected ValueError' |
| 387 | class FailingUserDict: |
| 388 | def keys(self): |
| 389 | class BogonIter: |
| 390 | def __init__(self): |
| 391 | self.i = 1 |
| 392 | def __iter__(self): |
| 393 | return self |
| 394 | def next(self): |
| 395 | if self.i: |
| 396 | self.i = 0 |
| 397 | return 'a' |
| 398 | raise ValueError |
| 399 | return BogonIter() |
| 400 | def __getitem__(self, key): |
| 401 | return key |
| 402 | try: d.update(FailingUserDict()) |
| 403 | except ValueError: pass |
| 404 | else: raise TestFailed, 'iter(dict.keys()).next() expected ValueError' |
| 405 | class FailingUserDict: |
| 406 | def keys(self): |
| 407 | class BogonIter: |
| 408 | def __init__(self): |
| 409 | self.i = ord('a') |
| 410 | def __iter__(self): |
| 411 | return self |
| 412 | def next(self): |
| 413 | if self.i <= ord('z'): |
| 414 | rtn = chr(self.i) |
| 415 | self.i += 1 |
| 416 | return rtn |
| 417 | raise StopIteration |
| 418 | return BogonIter() |
| 419 | def __getitem__(self, key): |
| 420 | raise ValueError |
| 421 | try: d.update(FailingUserDict()) |
| 422 | except ValueError: pass |
| 423 | else: raise TestFailed, 'dict.update(), __getitem__ expected ValueError' |
| 424 | # dict.copy() |
| 425 | d = {1:1, 2:2, 3:3} |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 426 | if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy' |
| 427 | if {}.copy() != {}: raise TestFailed, 'empty dict copy' |
Barry Warsaw | 9b887c7 | 1997-10-20 17:34:43 +0000 | [diff] [blame] | 428 | # dict.get() |
Guido van Rossum | fb5cef1 | 1997-10-20 20:10:43 +0000 | [diff] [blame] | 429 | d = {} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 430 | 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] | 431 | 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] | 432 | d = {'a' : 1, 'b' : 2} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 433 | 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] | 434 | if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg' |
| 435 | if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg' |
| 436 | 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] | 437 | # dict.setdefault() |
| 438 | d = {} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 439 | if d.setdefault('key0') is not None: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 440 | raise TestFailed, 'missing {} setdefault, no 2nd arg' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 441 | if d.setdefault('key0') is not None: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 442 | raise TestFailed, 'present {} setdefault, no 2nd arg' |
Guido van Rossum | 79c9b17 | 2000-08-08 16:13:23 +0000 | [diff] [blame] | 443 | d.setdefault('key', []).append(3) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 444 | if d['key'][0] != 3: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 445 | raise TestFailed, 'missing {} setdefault, w/ 2nd arg' |
Guido van Rossum | 79c9b17 | 2000-08-08 16:13:23 +0000 | [diff] [blame] | 446 | d.setdefault('key', []).append(4) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 447 | if len(d['key']) != 2: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 448 | raise TestFailed, 'present {} setdefault, w/ 2nd arg' |
Guido van Rossum | b822c61 | 2000-12-12 22:02:59 +0000 | [diff] [blame] | 449 | # dict.popitem() |
| 450 | for copymode in -1, +1: |
| 451 | # -1: b has same structure as a |
| 452 | # +1: b is a.copy() |
| 453 | for log2size in range(12): |
| 454 | size = 2**log2size |
| 455 | a = {} |
| 456 | b = {} |
| 457 | for i in range(size): |
| 458 | a[`i`] = i |
| 459 | if copymode < 0: |
| 460 | b[`i`] = i |
| 461 | if copymode > 0: |
| 462 | b = a.copy() |
| 463 | for i in range(size): |
| 464 | ka, va = ta = a.popitem() |
| 465 | if va != int(ka): raise TestFailed, "a.popitem: %s" % str(ta) |
| 466 | kb, vb = tb = b.popitem() |
| 467 | if vb != int(kb): raise TestFailed, "b.popitem: %s" % str(tb) |
| 468 | if copymode < 0 and ta != tb: |
| 469 | raise TestFailed, "a.popitem != b.popitem: %s, %s" % ( |
| 470 | str(ta), str(tb)) |
| 471 | if a: raise TestFailed, 'a not empty after popitems: %s' % str(a) |
| 472 | 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] | 473 | |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 474 | d.clear() |
| 475 | try: d.popitem() |
| 476 | except KeyError: pass |
| 477 | else: raise TestFailed, "{}.popitem doesn't raise KeyError" |
| 478 | |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 479 | # Tests for pop with specified key |
| 480 | d.clear() |
| 481 | k, v = 'abc', 'def' |
| 482 | d[k] = v |
| 483 | try: d.pop('ghi') |
| 484 | except KeyError: pass |
| 485 | else: raise TestFailed, "{}.pop(k) doesn't raise KeyError when k not in dictionary" |
| 486 | |
| 487 | if d.pop(k) != v: raise TestFailed, "{}.pop(k) doesn't find known key/value pair" |
| 488 | if len(d) > 0: raise TestFailed, "{}.pop(k) failed to remove the specified pair" |
| 489 | |
| 490 | try: d.pop(k) |
| 491 | except KeyError: pass |
| 492 | else: raise TestFailed, "{}.pop(k) doesn't raise KeyError when dictionary is empty" |
| 493 | |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 494 | d[1] = 1 |
| 495 | try: |
| 496 | for i in d: |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 497 | d[i+1] = 1 |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 498 | except RuntimeError: |
| 499 | pass |
| 500 | else: |
| 501 | raise TestFailed, "changing dict size during iteration doesn't raise Error" |
| 502 | |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 503 | try: type(1, 2) |
| 504 | except TypeError: pass |
| 505 | else: raise TestFailed, 'type(), w/2 args expected TypeError' |
| 506 | |
| 507 | try: type(1, 2, 3, 4) |
| 508 | except TypeError: pass |
| 509 | else: raise TestFailed, 'type(), w/4 args expected TypeError' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 510 | |
| 511 | print 'Buffers' |
| 512 | try: buffer('asdf', -1) |
| 513 | except ValueError: pass |
| 514 | else: raise TestFailed, "buffer('asdf', -1) should raise ValueError" |
| 515 | |
| 516 | try: buffer(None) |
| 517 | except TypeError: pass |
| 518 | else: raise TestFailed, "buffer(None) should raise TypeError" |
| 519 | |
| 520 | a = buffer('asdf') |
| 521 | hash(a) |
| 522 | b = a * 5 |
| 523 | if a == b: |
| 524 | raise TestFailed, 'buffers should not be equal' |
Fred Drake | 485f340 | 2002-05-02 04:27:20 +0000 | [diff] [blame] | 525 | if str(b) != ('asdf' * 5): |
| 526 | raise TestFailed, 'repeated buffer has wrong content' |
| 527 | if str(a * 0) != '': |
| 528 | raise TestFailed, 'repeated buffer zero times has wrong content' |
| 529 | if str(a + buffer('def')) != 'asdfdef': |
| 530 | raise TestFailed, 'concatenation of buffers yields wrong content' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 531 | |
| 532 | try: a[1] = 'g' |
| 533 | except TypeError: pass |
| 534 | else: raise TestFailed, "buffer assignment should raise TypeError" |
| 535 | |
| 536 | try: a[0:1] = 'g' |
| 537 | except TypeError: pass |
| 538 | else: raise TestFailed, "buffer slice assignment should raise TypeError" |