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' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 13 | if {}: raise TestFailed, '{} is true instead of false' |
| 14 | if not 1: raise TestFailed, '1 is false instead of true' |
| 15 | if not 1L: raise TestFailed, '1L is false instead of true' |
| 16 | if not 1.0: raise TestFailed, '1.0 is false instead of true' |
| 17 | if not 'x': raise TestFailed, '\'x\' is false instead of true' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 18 | if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true' |
| 19 | def f(): pass |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 20 | class C: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 21 | import sys |
| 22 | x = C() |
| 23 | if not f: raise TestFailed, 'f is false instead of true' |
| 24 | if not C: raise TestFailed, 'C is false instead of true' |
| 25 | if not sys: raise TestFailed, 'sys is false instead of true' |
| 26 | if not x: raise TestFailed, 'x is false instead of true' |
| 27 | |
| 28 | print '6.2 Boolean operations' |
| 29 | if 0 or 0: raise TestFailed, '0 or 0 is true instead of false' |
| 30 | if 1 and 1: pass |
| 31 | else: raise TestFailed, '1 and 1 is false instead of false' |
| 32 | if not 1: raise TestFailed, 'not 1 is true instead of false' |
| 33 | |
| 34 | print '6.3 Comparisons' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 35 | if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 36 | else: raise TestFailed, 'int comparisons failed' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 37 | if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 38 | else: raise TestFailed, 'long int comparisons failed' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 39 | 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] | 40 | else: raise TestFailed, 'float comparisons failed' |
| 41 | if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass |
| 42 | else: raise TestFailed, 'string comparisons failed' |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 43 | if None is None: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 44 | else: raise TestFailed, 'identity test failed' |
| 45 | |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 46 | try: float('') |
| 47 | except ValueError: pass |
| 48 | else: raise TestFailed, "float('') didn't raise ValueError" |
| 49 | |
| 50 | try: float('5\0') |
| 51 | except ValueError: pass |
| 52 | else: raise TestFailed, "float('5\0') didn't raise ValueError" |
| 53 | |
| 54 | try: 5.0 / 0.0 |
| 55 | except ZeroDivisionError: pass |
| 56 | else: raise TestFailed, "5.0 / 0.0 didn't raise ZeroDivisionError" |
| 57 | |
| 58 | try: 5.0 // 0.0 |
| 59 | except ZeroDivisionError: pass |
| 60 | else: raise TestFailed, "5.0 // 0.0 didn't raise ZeroDivisionError" |
| 61 | |
| 62 | try: 5.0 % 0.0 |
| 63 | except ZeroDivisionError: pass |
| 64 | else: raise TestFailed, "5.0 % 0.0 didn't raise ZeroDivisionError" |
| 65 | |
| 66 | try: 5 / 0L |
| 67 | except ZeroDivisionError: pass |
| 68 | else: raise TestFailed, "5 / 0L didn't raise ZeroDivisionError" |
| 69 | |
| 70 | try: 5 // 0L |
| 71 | except ZeroDivisionError: pass |
| 72 | else: raise TestFailed, "5 // 0L didn't raise ZeroDivisionError" |
| 73 | |
| 74 | try: 5 % 0L |
| 75 | except ZeroDivisionError: pass |
| 76 | else: raise TestFailed, "5 % 0L didn't raise ZeroDivisionError" |
| 77 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 78 | print '6.4 Numeric types (mostly conversions)' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 79 | if 0 != 0L or 0 != 0.0 or 0L != 0.0: raise TestFailed, 'mixed comparisons' |
| 80 | if 1 != 1L or 1 != 1.0 or 1L != 1.0: raise TestFailed, 'mixed comparisons' |
| 81 | if -1 != -1L or -1 != -1.0 or -1L != -1.0: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 82 | raise TestFailed, 'int/long/float value not equal' |
Guido van Rossum | aa86e35 | 2003-04-19 18:15:10 +0000 | [diff] [blame] | 83 | # calling built-in types without argument must return 0 |
| 84 | if int() != 0: raise TestFailed, 'int() does not return 0' |
| 85 | if long() != 0L: raise TestFailed, 'long() does not return 0L' |
| 86 | if float() != 0.0: raise TestFailed, 'float() does not return 0.0' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 87 | if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass |
| 88 | else: raise TestFailed, 'int() does not round properly' |
| 89 | if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass |
| 90 | else: raise TestFailed, 'long() does not round properly' |
| 91 | if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass |
| 92 | else: raise TestFailed, 'float() does not work properly' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 93 | print '6.4.1 32-bit integers' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 94 | if 12 + 24 != 36: raise TestFailed, 'int op' |
| 95 | if 12 + (-24) != -12: raise TestFailed, 'int op' |
| 96 | if (-12) + 24 != 12: raise TestFailed, 'int op' |
| 97 | if (-12) + (-24) != -36: raise TestFailed, 'int op' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 98 | if not 12 < 24: raise TestFailed, 'int op' |
| 99 | if not -24 < -12: raise TestFailed, 'int op' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 100 | # Test for a particular bug in integer multiply |
| 101 | xsize, ysize, zsize = 238, 356, 4 |
| 102 | if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 103 | raise TestFailed, 'int mul commutativity' |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 104 | # And another. |
| 105 | m = -sys.maxint - 1 |
| 106 | for divisor in 1, 2, 4, 8, 16, 32: |
Tim Peters | 6d30c3e | 2001-12-05 00:30:09 +0000 | [diff] [blame] | 107 | j = m // divisor |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 108 | prod = divisor * j |
| 109 | if prod != m: |
| 110 | raise TestFailed, "%r * %r == %r != %r" % (divisor, j, prod, m) |
| 111 | if type(prod) is not int: |
| 112 | raise TestFailed, ("expected type(prod) to be int, not %r" % |
| 113 | type(prod)) |
| 114 | # Check for expected * overflow to long. |
| 115 | for divisor in 1, 2, 4, 8, 16, 32: |
Tim Peters | 6d30c3e | 2001-12-05 00:30:09 +0000 | [diff] [blame] | 116 | j = m // divisor - 1 |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 117 | prod = divisor * j |
| 118 | if type(prod) is not long: |
| 119 | raise TestFailed, ("expected type(%r) to be long, not %r" % |
| 120 | (prod, type(prod))) |
| 121 | # Check for expected * overflow to long. |
| 122 | m = sys.maxint |
| 123 | for divisor in 1, 2, 4, 8, 16, 32: |
Tim Peters | 6d30c3e | 2001-12-05 00:30:09 +0000 | [diff] [blame] | 124 | j = m // divisor + 1 |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 125 | prod = divisor * j |
| 126 | if type(prod) is not long: |
| 127 | raise TestFailed, ("expected type(%r) to be long, not %r" % |
| 128 | (prod, type(prod))) |
| 129 | |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 130 | print '6.4.2 Long integers' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 131 | if 12L + 24L != 36L: raise TestFailed, 'long op' |
| 132 | if 12L + (-24L) != -12L: raise TestFailed, 'long op' |
| 133 | if (-12L) + 24L != 12L: raise TestFailed, 'long op' |
| 134 | if (-12L) + (-24L) != -36L: raise TestFailed, 'long op' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 135 | if not 12L < 24L: raise TestFailed, 'long op' |
| 136 | if not -24L < -12L: raise TestFailed, 'long op' |
Guido van Rossum | 7462942 | 1998-05-26 14:51:55 +0000 | [diff] [blame] | 137 | x = sys.maxint |
| 138 | if int(long(x)) != x: raise TestFailed, 'long op' |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 139 | try: y = int(long(x)+1L) |
| 140 | except OverflowError: raise TestFailed, 'long op' |
| 141 | if not isinstance(y, long): raise TestFailed, 'long op' |
Guido van Rossum | 7462942 | 1998-05-26 14:51:55 +0000 | [diff] [blame] | 142 | x = -x |
| 143 | if int(long(x)) != x: raise TestFailed, 'long op' |
| 144 | x = x-1 |
| 145 | if int(long(x)) != x: raise TestFailed, 'long op' |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 146 | try: y = int(long(x)-1L) |
| 147 | except OverflowError: raise TestFailed, 'long op' |
| 148 | if not isinstance(y, long): raise TestFailed, 'long op' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 149 | |
| 150 | try: 5 << -5 |
| 151 | except ValueError: pass |
| 152 | else: raise TestFailed, 'int negative shift <<' |
| 153 | |
| 154 | try: 5L << -5L |
| 155 | except ValueError: pass |
| 156 | else: raise TestFailed, 'long negative shift <<' |
| 157 | |
| 158 | try: 5 >> -5 |
| 159 | except ValueError: pass |
| 160 | else: raise TestFailed, 'int negative shift >>' |
| 161 | |
| 162 | try: 5L >> -5L |
| 163 | except ValueError: pass |
| 164 | else: raise TestFailed, 'long negative shift >>' |
| 165 | |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 166 | print '6.4.3 Floating point numbers' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 167 | if 12.0 + 24.0 != 36.0: raise TestFailed, 'float op' |
| 168 | if 12.0 + (-24.0) != -12.0: raise TestFailed, 'float op' |
| 169 | if (-12.0) + 24.0 != 12.0: raise TestFailed, 'float op' |
| 170 | if (-12.0) + (-24.0) != -36.0: raise TestFailed, 'float op' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 171 | if not 12.0 < 24.0: raise TestFailed, 'float op' |
| 172 | if not -24.0 < -12.0: raise TestFailed, 'float op' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 173 | |
| 174 | print '6.5 Sequence types' |
| 175 | |
| 176 | print '6.5.1 Strings' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 177 | if len('') != 0: raise TestFailed, 'len(\'\')' |
| 178 | if len('a') != 1: raise TestFailed, 'len(\'a\')' |
| 179 | if len('abcdef') != 6: raise TestFailed, 'len(\'abcdef\')' |
| 180 | if 'xyz' + 'abcde' != 'xyzabcde': raise TestFailed, 'string concatenation' |
| 181 | if 'xyz'*3 != 'xyzxyzxyz': raise TestFailed, 'string repetition *3' |
| 182 | if 0*'abcde' != '': raise TestFailed, 'string repetition 0*' |
| 183 | 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] | 184 | if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass |
| 185 | else: raise TestFailed, 'in/not in string' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 186 | x = 'x'*103 |
| 187 | if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 188 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 189 | #extended slices for strings |
| 190 | a = '0123456789' |
| 191 | vereq(a[::], a) |
| 192 | vereq(a[::2], '02468') |
| 193 | vereq(a[1::2], '13579') |
| 194 | vereq(a[::-1],'9876543210') |
| 195 | vereq(a[::-2], '97531') |
| 196 | vereq(a[3::-2], '31') |
| 197 | vereq(a[-100:100:], a) |
| 198 | vereq(a[100:-100:-1], a[::-1]) |
| 199 | vereq(a[-100L:100L:2L], '02468') |
| 200 | |
| 201 | if have_unicode: |
| 202 | a = unicode('0123456789', 'ascii') |
| 203 | vereq(a[::], a) |
| 204 | vereq(a[::2], unicode('02468', 'ascii')) |
| 205 | vereq(a[1::2], unicode('13579', 'ascii')) |
| 206 | vereq(a[::-1], unicode('9876543210', 'ascii')) |
| 207 | vereq(a[::-2], unicode('97531', 'ascii')) |
| 208 | vereq(a[3::-2], unicode('31', 'ascii')) |
| 209 | vereq(a[-100:100:], a) |
| 210 | vereq(a[100:-100:-1], a[::-1]) |
| 211 | vereq(a[-100L:100L:2L], unicode('02468', 'ascii')) |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 212 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 213 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 214 | print '6.5.2 Tuples [see test_tuple.py]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 215 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 216 | print '6.5.3 Lists [see test_list.py]' |
Raymond Hettinger | 9928571 | 2003-04-24 16:52:47 +0000 | [diff] [blame] | 217 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 218 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 219 | print '6.6 Mappings == Dictionaries' |
Guido van Rossum | aa86e35 | 2003-04-19 18:15:10 +0000 | [diff] [blame] | 220 | # calling built-in types without argument must return empty |
| 221 | if dict() != {}: raise TestFailed,'dict() does not return {}' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 222 | d = {} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 223 | if d.keys() != []: raise TestFailed, '{}.keys()' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 224 | if d.values() != []: raise TestFailed, '{}.values()' |
| 225 | if d.items() != []: raise TestFailed, '{}.items()' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 226 | if d.has_key('a') != 0: raise TestFailed, '{}.has_key(\'a\')' |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 227 | if ('a' in d) != 0: raise TestFailed, "'a' in {}" |
| 228 | if ('a' not in d) != 1: raise TestFailed, "'a' not in {}" |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 229 | if len(d) != 0: raise TestFailed, 'len({})' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 230 | d = {'a': 1, 'b': 2} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 231 | if len(d) != 2: raise TestFailed, 'len(dict)' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 232 | k = d.keys() |
| 233 | k.sort() |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 234 | if k != ['a', 'b']: raise TestFailed, 'dict keys()' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 235 | if d.has_key('a') and d.has_key('b') and not d.has_key('c'): pass |
| 236 | else: raise TestFailed, 'dict keys()' |
Guido van Rossum | 0dbb4fb | 2001-04-20 16:50:40 +0000 | [diff] [blame] | 237 | if 'a' in d and 'b' in d and 'c' not in d: pass |
| 238 | else: raise TestFailed, 'dict keys() # in/not in version' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 239 | 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] | 240 | d['c'] = 3 |
| 241 | d['a'] = 4 |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 242 | 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] | 243 | del d['b'] |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 244 | if d != {'a': 4, 'c': 3}: raise TestFailed, 'dict item deletion' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 245 | # dict.clear() |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 246 | d = {1:1, 2:2, 3:3} |
| 247 | d.clear() |
| 248 | if d != {}: raise TestFailed, 'dict clear' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 249 | # dict.update() |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 250 | d.update({1:100}) |
| 251 | d.update({2:20}) |
| 252 | d.update({1:1, 2:2, 3:3}) |
| 253 | if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict update' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 254 | d.clear() |
| 255 | try: d.update(None) |
| 256 | except AttributeError: pass |
| 257 | else: raise TestFailed, 'dict.update(None), AttributeError expected' |
| 258 | class SimpleUserDict: |
| 259 | def __init__(self): |
| 260 | self.d = {1:1, 2:2, 3:3} |
| 261 | def keys(self): |
| 262 | return self.d.keys() |
| 263 | def __getitem__(self, i): |
| 264 | return self.d[i] |
| 265 | d.update(SimpleUserDict()) |
| 266 | if d != {1:1, 2:2, 3:3}: raise TestFailed, 'dict.update(instance)' |
| 267 | d.clear() |
| 268 | class FailingUserDict: |
| 269 | def keys(self): |
| 270 | raise ValueError |
| 271 | try: d.update(FailingUserDict()) |
| 272 | except ValueError: pass |
| 273 | else: raise TestFailed, 'dict.keys() expected ValueError' |
| 274 | class FailingUserDict: |
| 275 | def keys(self): |
| 276 | class BogonIter: |
| 277 | def __iter__(self): |
| 278 | raise ValueError |
| 279 | return BogonIter() |
| 280 | try: d.update(FailingUserDict()) |
| 281 | except ValueError: pass |
| 282 | else: raise TestFailed, 'iter(dict.keys()) expected ValueError' |
| 283 | class FailingUserDict: |
| 284 | def keys(self): |
| 285 | class BogonIter: |
| 286 | def __init__(self): |
| 287 | self.i = 1 |
| 288 | def __iter__(self): |
| 289 | return self |
| 290 | def next(self): |
| 291 | if self.i: |
| 292 | self.i = 0 |
| 293 | return 'a' |
| 294 | raise ValueError |
| 295 | return BogonIter() |
| 296 | def __getitem__(self, key): |
| 297 | return key |
| 298 | try: d.update(FailingUserDict()) |
| 299 | except ValueError: pass |
| 300 | else: raise TestFailed, 'iter(dict.keys()).next() expected ValueError' |
| 301 | class FailingUserDict: |
| 302 | def keys(self): |
| 303 | class BogonIter: |
| 304 | def __init__(self): |
| 305 | self.i = ord('a') |
| 306 | def __iter__(self): |
| 307 | return self |
| 308 | def next(self): |
| 309 | if self.i <= ord('z'): |
| 310 | rtn = chr(self.i) |
| 311 | self.i += 1 |
| 312 | return rtn |
| 313 | raise StopIteration |
| 314 | return BogonIter() |
| 315 | def __getitem__(self, key): |
| 316 | raise ValueError |
| 317 | try: d.update(FailingUserDict()) |
| 318 | except ValueError: pass |
| 319 | else: raise TestFailed, 'dict.update(), __getitem__ expected ValueError' |
Raymond Hettinger | e33d3df | 2002-11-27 07:29:33 +0000 | [diff] [blame] | 320 | # dict.fromkeys() |
| 321 | if dict.fromkeys('abc') != {'a':None, 'b':None, 'c':None}: |
| 322 | raise TestFailed, 'dict.fromkeys did not work as a class method' |
| 323 | d = {} |
| 324 | if d.fromkeys('abc') is d: |
| 325 | raise TestFailed, 'dict.fromkeys did not return a new dict' |
| 326 | if d.fromkeys('abc') != {'a':None, 'b':None, 'c':None}: |
| 327 | raise TestFailed, 'dict.fromkeys failed with default value' |
| 328 | if d.fromkeys((4,5),0) != {4:0, 5:0}: |
| 329 | raise TestFailed, 'dict.fromkeys failed with specified value' |
| 330 | if d.fromkeys([]) != {}: |
| 331 | raise TestFailed, 'dict.fromkeys failed with null sequence' |
| 332 | def g(): |
| 333 | yield 1 |
| 334 | if d.fromkeys(g()) != {1:None}: |
| 335 | raise TestFailed, 'dict.fromkeys failed with a generator' |
| 336 | try: {}.fromkeys(3) |
| 337 | except TypeError: pass |
| 338 | else: raise TestFailed, 'dict.fromkeys failed to raise TypeError' |
| 339 | class dictlike(dict): pass |
| 340 | if dictlike.fromkeys('a') != {'a':None}: |
| 341 | raise TestFailed, 'dictsubclass.fromkeys did not inherit' |
| 342 | if dictlike().fromkeys('a') != {'a':None}: |
| 343 | raise TestFailed, 'dictsubclass.fromkeys did not inherit' |
| 344 | if type(dictlike.fromkeys('a')) is not dictlike: |
| 345 | raise TestFailed, 'dictsubclass.fromkeys created wrong type' |
| 346 | if type(dictlike().fromkeys('a')) is not dictlike: |
| 347 | raise TestFailed, 'dictsubclass.fromkeys created wrong type' |
Raymond Hettinger | b02bb5e | 2002-12-04 07:32:25 +0000 | [diff] [blame] | 348 | from UserDict import UserDict |
| 349 | class mydict(dict): |
Raymond Hettinger | babc83a | 2002-12-07 09:04:29 +0000 | [diff] [blame] | 350 | def __new__(cls): |
| 351 | return UserDict() |
Raymond Hettinger | e03e5b1 | 2002-12-07 08:10:51 +0000 | [diff] [blame] | 352 | ud = mydict.fromkeys('ab') |
| 353 | if ud != {'a':None, 'b':None} or not isinstance(ud,UserDict): |
| 354 | raise TestFailed, 'fromkeys did not instantiate using __new__' |
Barry Warsaw | 4177538 | 2001-06-26 20:09:28 +0000 | [diff] [blame] | 355 | # dict.copy() |
| 356 | d = {1:1, 2:2, 3:3} |
Guido van Rossum | ce1fa26 | 1997-06-02 23:14:00 +0000 | [diff] [blame] | 357 | if d.copy() != {1:1, 2:2, 3:3}: raise TestFailed, 'dict copy' |
| 358 | if {}.copy() != {}: raise TestFailed, 'empty dict copy' |
Barry Warsaw | 9b887c7 | 1997-10-20 17:34:43 +0000 | [diff] [blame] | 359 | # dict.get() |
Guido van Rossum | fb5cef1 | 1997-10-20 20:10:43 +0000 | [diff] [blame] | 360 | d = {} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 361 | 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] | 362 | 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] | 363 | d = {'a' : 1, 'b' : 2} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 364 | 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] | 365 | if d.get('c', 3) != 3: raise TestFailed, 'missing dict get, w/ 2nd arg' |
| 366 | if d.get('a') != 1: raise TestFailed, 'present dict get, no 2nd arg' |
| 367 | 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] | 368 | # dict.setdefault() |
| 369 | d = {} |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 370 | if d.setdefault('key0') is not None: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 371 | raise TestFailed, 'missing {} setdefault, no 2nd arg' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 372 | if d.setdefault('key0') is not None: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 373 | raise TestFailed, 'present {} setdefault, no 2nd arg' |
Guido van Rossum | 79c9b17 | 2000-08-08 16:13:23 +0000 | [diff] [blame] | 374 | d.setdefault('key', []).append(3) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 375 | if d['key'][0] != 3: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 376 | raise TestFailed, 'missing {} setdefault, w/ 2nd arg' |
Guido van Rossum | 79c9b17 | 2000-08-08 16:13:23 +0000 | [diff] [blame] | 377 | d.setdefault('key', []).append(4) |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 378 | if len(d['key']) != 2: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 379 | raise TestFailed, 'present {} setdefault, w/ 2nd arg' |
Guido van Rossum | b822c61 | 2000-12-12 22:02:59 +0000 | [diff] [blame] | 380 | # dict.popitem() |
| 381 | for copymode in -1, +1: |
| 382 | # -1: b has same structure as a |
| 383 | # +1: b is a.copy() |
| 384 | for log2size in range(12): |
| 385 | size = 2**log2size |
| 386 | a = {} |
| 387 | b = {} |
| 388 | for i in range(size): |
| 389 | a[`i`] = i |
| 390 | if copymode < 0: |
| 391 | b[`i`] = i |
| 392 | if copymode > 0: |
| 393 | b = a.copy() |
| 394 | for i in range(size): |
| 395 | ka, va = ta = a.popitem() |
| 396 | if va != int(ka): raise TestFailed, "a.popitem: %s" % str(ta) |
| 397 | kb, vb = tb = b.popitem() |
| 398 | if vb != int(kb): raise TestFailed, "b.popitem: %s" % str(tb) |
| 399 | if copymode < 0 and ta != tb: |
| 400 | raise TestFailed, "a.popitem != b.popitem: %s, %s" % ( |
| 401 | str(ta), str(tb)) |
| 402 | if a: raise TestFailed, 'a not empty after popitems: %s' % str(a) |
| 403 | 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] | 404 | |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 405 | d.clear() |
| 406 | try: d.popitem() |
| 407 | except KeyError: pass |
| 408 | else: raise TestFailed, "{}.popitem doesn't raise KeyError" |
| 409 | |
Guido van Rossum | e027d98 | 2002-04-12 15:11:59 +0000 | [diff] [blame] | 410 | # Tests for pop with specified key |
| 411 | d.clear() |
| 412 | k, v = 'abc', 'def' |
| 413 | d[k] = v |
| 414 | try: d.pop('ghi') |
| 415 | except KeyError: pass |
| 416 | else: raise TestFailed, "{}.pop(k) doesn't raise KeyError when k not in dictionary" |
| 417 | |
| 418 | if d.pop(k) != v: raise TestFailed, "{}.pop(k) doesn't find known key/value pair" |
| 419 | if len(d) > 0: raise TestFailed, "{}.pop(k) failed to remove the specified pair" |
| 420 | |
| 421 | try: d.pop(k) |
| 422 | except KeyError: pass |
| 423 | else: raise TestFailed, "{}.pop(k) doesn't raise KeyError when dictionary is empty" |
| 424 | |
Neal Norwitz | d5a65a7 | 2003-02-23 23:11:41 +0000 | [diff] [blame] | 425 | # verify longs/ints get same value when key > 32 bits (for 64-bit archs) |
| 426 | # see SF bug #689659 |
| 427 | x = 4503599627370496L |
| 428 | y = 4503599627370496 |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 429 | h = {x: 'anything', y: 'something else'} |
Neal Norwitz | d5a65a7 | 2003-02-23 23:11:41 +0000 | [diff] [blame] | 430 | if h[x] != h[y]: |
| 431 | raise TestFailed, "long/int key should match" |
| 432 | |
Raymond Hettinger | a3e1e4c | 2003-03-06 23:54:28 +0000 | [diff] [blame] | 433 | if d.pop(k, v) != v: raise TestFailed, "{}.pop(k, v) doesn't return default value" |
| 434 | d[k] = v |
| 435 | if d.pop(k, 1) != v: raise TestFailed, "{}.pop(k, v) doesn't find known key/value pair" |
| 436 | |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 437 | d[1] = 1 |
| 438 | try: |
| 439 | for i in d: |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 440 | d[i+1] = 1 |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 441 | except RuntimeError: |
| 442 | pass |
| 443 | else: |
| 444 | raise TestFailed, "changing dict size during iteration doesn't raise Error" |
| 445 | |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 446 | try: type(1, 2) |
| 447 | except TypeError: pass |
| 448 | else: raise TestFailed, 'type(), w/2 args expected TypeError' |
| 449 | |
| 450 | try: type(1, 2, 3, 4) |
| 451 | except TypeError: pass |
| 452 | else: raise TestFailed, 'type(), w/4 args expected TypeError' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 453 | |
| 454 | print 'Buffers' |
| 455 | try: buffer('asdf', -1) |
| 456 | except ValueError: pass |
| 457 | else: raise TestFailed, "buffer('asdf', -1) should raise ValueError" |
| 458 | |
| 459 | try: buffer(None) |
| 460 | except TypeError: pass |
| 461 | else: raise TestFailed, "buffer(None) should raise TypeError" |
| 462 | |
| 463 | a = buffer('asdf') |
| 464 | hash(a) |
| 465 | b = a * 5 |
| 466 | if a == b: |
| 467 | raise TestFailed, 'buffers should not be equal' |
Fred Drake | 485f340 | 2002-05-02 04:27:20 +0000 | [diff] [blame] | 468 | if str(b) != ('asdf' * 5): |
| 469 | raise TestFailed, 'repeated buffer has wrong content' |
| 470 | if str(a * 0) != '': |
| 471 | raise TestFailed, 'repeated buffer zero times has wrong content' |
| 472 | if str(a + buffer('def')) != 'asdfdef': |
| 473 | raise TestFailed, 'concatenation of buffers yields wrong content' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 474 | |
| 475 | try: a[1] = 'g' |
| 476 | except TypeError: pass |
| 477 | else: raise TestFailed, "buffer assignment should raise TypeError" |
| 478 | |
| 479 | try: a[0:1] = 'g' |
| 480 | except TypeError: pass |
| 481 | else: raise TestFailed, "buffer slice assignment should raise TypeError" |