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 not 1: raise TestFailed, '1 is false instead of true' |
| 14 | if not 1L: raise TestFailed, '1L is false instead of true' |
| 15 | if not 1.0: raise TestFailed, '1.0 is false instead of true' |
| 16 | if not 'x': raise TestFailed, '\'x\' is false instead of true' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 17 | if not {'x': 1}: raise TestFailed, '{\'x\': 1} is false instead of true' |
| 18 | def f(): pass |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 19 | class C: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 20 | import sys |
| 21 | x = C() |
| 22 | if not f: raise TestFailed, 'f is false instead of true' |
| 23 | if not C: raise TestFailed, 'C is false instead of true' |
| 24 | if not sys: raise TestFailed, 'sys is false instead of true' |
| 25 | if not x: raise TestFailed, 'x is false instead of true' |
| 26 | |
| 27 | print '6.2 Boolean operations' |
| 28 | if 0 or 0: raise TestFailed, '0 or 0 is true instead of false' |
| 29 | if 1 and 1: pass |
Walter Dörwald | 7dcf9f8 | 2004-02-06 18:30:31 +0000 | [diff] [blame] | 30 | else: raise TestFailed, '1 and 1 is false instead of true' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 31 | if not 1: raise TestFailed, 'not 1 is true instead of false' |
| 32 | |
| 33 | print '6.3 Comparisons' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 34 | if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 35 | else: raise TestFailed, 'int comparisons failed' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 36 | if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 37 | else: raise TestFailed, 'long int comparisons failed' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 38 | 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] | 39 | else: raise TestFailed, 'float comparisons failed' |
| 40 | if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass |
| 41 | else: raise TestFailed, 'string comparisons failed' |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 42 | if None is None: pass |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 43 | else: raise TestFailed, 'identity test failed' |
| 44 | |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 45 | try: float('') |
| 46 | except ValueError: pass |
| 47 | else: raise TestFailed, "float('') didn't raise ValueError" |
| 48 | |
| 49 | try: float('5\0') |
| 50 | except ValueError: pass |
| 51 | else: raise TestFailed, "float('5\0') didn't raise ValueError" |
| 52 | |
| 53 | try: 5.0 / 0.0 |
| 54 | except ZeroDivisionError: pass |
| 55 | else: raise TestFailed, "5.0 / 0.0 didn't raise ZeroDivisionError" |
| 56 | |
| 57 | try: 5.0 // 0.0 |
| 58 | except ZeroDivisionError: pass |
| 59 | else: raise TestFailed, "5.0 // 0.0 didn't raise ZeroDivisionError" |
| 60 | |
| 61 | try: 5.0 % 0.0 |
| 62 | except ZeroDivisionError: pass |
| 63 | else: raise TestFailed, "5.0 % 0.0 didn't raise ZeroDivisionError" |
| 64 | |
| 65 | try: 5 / 0L |
| 66 | except ZeroDivisionError: pass |
| 67 | else: raise TestFailed, "5 / 0L didn't raise ZeroDivisionError" |
| 68 | |
| 69 | try: 5 // 0L |
| 70 | except ZeroDivisionError: pass |
| 71 | else: raise TestFailed, "5 // 0L didn't raise ZeroDivisionError" |
| 72 | |
| 73 | try: 5 % 0L |
| 74 | except ZeroDivisionError: pass |
| 75 | else: raise TestFailed, "5 % 0L didn't raise ZeroDivisionError" |
| 76 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 77 | print '6.4 Numeric types (mostly conversions)' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 78 | if 0 != 0L or 0 != 0.0 or 0L != 0.0: raise TestFailed, 'mixed comparisons' |
| 79 | if 1 != 1L or 1 != 1.0 or 1L != 1.0: raise TestFailed, 'mixed comparisons' |
| 80 | if -1 != -1L or -1 != -1.0 or -1L != -1.0: |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 81 | raise TestFailed, 'int/long/float value not equal' |
Guido van Rossum | aa86e35 | 2003-04-19 18:15:10 +0000 | [diff] [blame] | 82 | # calling built-in types without argument must return 0 |
| 83 | if int() != 0: raise TestFailed, 'int() does not return 0' |
| 84 | if long() != 0L: raise TestFailed, 'long() does not return 0L' |
| 85 | 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] | 86 | if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass |
| 87 | else: raise TestFailed, 'int() does not round properly' |
| 88 | if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass |
| 89 | else: raise TestFailed, 'long() does not round properly' |
| 90 | if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass |
| 91 | else: raise TestFailed, 'float() does not work properly' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 92 | print '6.4.1 32-bit integers' |
Georg Brandl | 418a1ef | 2006-02-22 11:30:06 +0000 | [diff] [blame] | 93 | # Ensure the first 256 integers are shared |
| 94 | a = 256 |
| 95 | b = 128*2 |
| 96 | if a is not b: raise TestFailed, '256 is not shared' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 97 | if 12 + 24 != 36: raise TestFailed, 'int op' |
| 98 | if 12 + (-24) != -12: raise TestFailed, 'int op' |
| 99 | if (-12) + 24 != 12: raise TestFailed, 'int op' |
| 100 | if (-12) + (-24) != -36: raise TestFailed, 'int op' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 101 | if not 12 < 24: raise TestFailed, 'int op' |
| 102 | if not -24 < -12: raise TestFailed, 'int op' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 103 | # Test for a particular bug in integer multiply |
| 104 | xsize, ysize, zsize = 238, 356, 4 |
| 105 | if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 106 | raise TestFailed, 'int mul commutativity' |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 107 | # And another. |
| 108 | m = -sys.maxint - 1 |
| 109 | for divisor in 1, 2, 4, 8, 16, 32: |
Tim Peters | 6d30c3e | 2001-12-05 00:30:09 +0000 | [diff] [blame] | 110 | j = m // divisor |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 111 | prod = divisor * j |
| 112 | if prod != m: |
| 113 | raise TestFailed, "%r * %r == %r != %r" % (divisor, j, prod, m) |
| 114 | if type(prod) is not int: |
| 115 | raise TestFailed, ("expected type(prod) to be int, not %r" % |
| 116 | type(prod)) |
| 117 | # Check for expected * overflow to long. |
| 118 | for divisor in 1, 2, 4, 8, 16, 32: |
Tim Peters | 6d30c3e | 2001-12-05 00:30:09 +0000 | [diff] [blame] | 119 | j = m // divisor - 1 |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 120 | prod = divisor * j |
| 121 | if type(prod) is not long: |
| 122 | raise TestFailed, ("expected type(%r) to be long, not %r" % |
| 123 | (prod, type(prod))) |
| 124 | # Check for expected * overflow to long. |
| 125 | m = sys.maxint |
| 126 | for divisor in 1, 2, 4, 8, 16, 32: |
Tim Peters | 6d30c3e | 2001-12-05 00:30:09 +0000 | [diff] [blame] | 127 | j = m // divisor + 1 |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 128 | prod = divisor * j |
| 129 | if type(prod) is not long: |
| 130 | raise TestFailed, ("expected type(%r) to be long, not %r" % |
| 131 | (prod, type(prod))) |
| 132 | |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 133 | print '6.4.2 Long integers' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 134 | if 12L + 24L != 36L: raise TestFailed, 'long op' |
| 135 | if 12L + (-24L) != -12L: raise TestFailed, 'long op' |
| 136 | if (-12L) + 24L != 12L: raise TestFailed, 'long op' |
| 137 | if (-12L) + (-24L) != -36L: raise TestFailed, 'long op' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 138 | if not 12L < 24L: raise TestFailed, 'long op' |
| 139 | if not -24L < -12L: raise TestFailed, 'long op' |
Guido van Rossum | 7462942 | 1998-05-26 14:51:55 +0000 | [diff] [blame] | 140 | x = sys.maxint |
| 141 | if int(long(x)) != x: raise TestFailed, 'long op' |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 142 | try: y = int(long(x)+1L) |
| 143 | except OverflowError: raise TestFailed, 'long op' |
| 144 | if not isinstance(y, long): raise TestFailed, 'long op' |
Guido van Rossum | 7462942 | 1998-05-26 14:51:55 +0000 | [diff] [blame] | 145 | x = -x |
| 146 | if int(long(x)) != x: raise TestFailed, 'long op' |
| 147 | x = x-1 |
| 148 | if int(long(x)) != x: raise TestFailed, 'long op' |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 149 | try: y = int(long(x)-1L) |
| 150 | except OverflowError: raise TestFailed, 'long op' |
| 151 | if not isinstance(y, long): raise TestFailed, 'long op' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 152 | |
| 153 | try: 5 << -5 |
| 154 | except ValueError: pass |
| 155 | else: raise TestFailed, 'int negative shift <<' |
| 156 | |
| 157 | try: 5L << -5L |
| 158 | except ValueError: pass |
| 159 | else: raise TestFailed, 'long negative shift <<' |
| 160 | |
| 161 | try: 5 >> -5 |
| 162 | except ValueError: pass |
| 163 | else: raise TestFailed, 'int negative shift >>' |
| 164 | |
| 165 | try: 5L >> -5L |
| 166 | except ValueError: pass |
| 167 | else: raise TestFailed, 'long negative shift >>' |
| 168 | |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 169 | print '6.4.3 Floating point numbers' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 170 | if 12.0 + 24.0 != 36.0: raise TestFailed, 'float op' |
| 171 | if 12.0 + (-24.0) != -12.0: raise TestFailed, 'float op' |
| 172 | if (-12.0) + 24.0 != 12.0: raise TestFailed, 'float op' |
| 173 | if (-12.0) + (-24.0) != -36.0: raise TestFailed, 'float op' |
Guido van Rossum | 80530ce | 1993-01-21 15:36:40 +0000 | [diff] [blame] | 174 | if not 12.0 < 24.0: raise TestFailed, 'float op' |
| 175 | if not -24.0 < -12.0: raise TestFailed, 'float op' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 176 | |
| 177 | print '6.5 Sequence types' |
| 178 | |
| 179 | print '6.5.1 Strings' |
Fred Drake | 132dce2 | 2000-12-12 23:11:42 +0000 | [diff] [blame] | 180 | if len('') != 0: raise TestFailed, 'len(\'\')' |
| 181 | if len('a') != 1: raise TestFailed, 'len(\'a\')' |
| 182 | if len('abcdef') != 6: raise TestFailed, 'len(\'abcdef\')' |
| 183 | if 'xyz' + 'abcde' != 'xyzabcde': raise TestFailed, 'string concatenation' |
| 184 | if 'xyz'*3 != 'xyzxyzxyz': raise TestFailed, 'string repetition *3' |
| 185 | if 0*'abcde' != '': raise TestFailed, 'string repetition 0*' |
| 186 | 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] | 187 | if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass |
| 188 | else: raise TestFailed, 'in/not in string' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 189 | x = 'x'*103 |
| 190 | if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 191 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 192 | #extended slices for strings |
| 193 | a = '0123456789' |
| 194 | vereq(a[::], a) |
| 195 | vereq(a[::2], '02468') |
| 196 | vereq(a[1::2], '13579') |
| 197 | vereq(a[::-1],'9876543210') |
| 198 | vereq(a[::-2], '97531') |
| 199 | vereq(a[3::-2], '31') |
| 200 | vereq(a[-100:100:], a) |
| 201 | vereq(a[100:-100:-1], a[::-1]) |
| 202 | vereq(a[-100L:100L:2L], '02468') |
| 203 | |
| 204 | if have_unicode: |
| 205 | a = unicode('0123456789', 'ascii') |
| 206 | vereq(a[::], a) |
| 207 | vereq(a[::2], unicode('02468', 'ascii')) |
| 208 | vereq(a[1::2], unicode('13579', 'ascii')) |
| 209 | vereq(a[::-1], unicode('9876543210', 'ascii')) |
| 210 | vereq(a[::-2], unicode('97531', 'ascii')) |
| 211 | vereq(a[3::-2], unicode('31', 'ascii')) |
| 212 | vereq(a[-100:100:], a) |
| 213 | vereq(a[100:-100:-1], a[::-1]) |
| 214 | vereq(a[-100L:100L:2L], unicode('02468', 'ascii')) |
Tim Peters | c411dba | 2002-07-16 21:35:23 +0000 | [diff] [blame] | 215 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 216 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 217 | print '6.5.2 Tuples [see test_tuple.py]' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 218 | |
Walter Dörwald | 1dde95d | 2003-12-08 11:38:45 +0000 | [diff] [blame] | 219 | print '6.5.3 Lists [see test_list.py]' |
Raymond Hettinger | 9928571 | 2003-04-24 16:52:47 +0000 | [diff] [blame] | 220 | |
Walter Dörwald | 0a6d0ff | 2004-05-31 16:29:04 +0000 | [diff] [blame] | 221 | print '6.6 Mappings == Dictionaries [see test_dict.py]' |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 222 | |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 223 | |
Guido van Rossum | 29d2606 | 2001-12-11 04:37:34 +0000 | [diff] [blame] | 224 | try: type(1, 2) |
| 225 | except TypeError: pass |
| 226 | else: raise TestFailed, 'type(), w/2 args expected TypeError' |
| 227 | |
| 228 | try: type(1, 2, 3, 4) |
| 229 | except TypeError: pass |
| 230 | else: raise TestFailed, 'type(), w/4 args expected TypeError' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 231 | |
| 232 | print 'Buffers' |
| 233 | try: buffer('asdf', -1) |
| 234 | except ValueError: pass |
| 235 | else: raise TestFailed, "buffer('asdf', -1) should raise ValueError" |
| 236 | |
| 237 | try: buffer(None) |
| 238 | except TypeError: pass |
| 239 | else: raise TestFailed, "buffer(None) should raise TypeError" |
| 240 | |
| 241 | a = buffer('asdf') |
| 242 | hash(a) |
| 243 | b = a * 5 |
| 244 | if a == b: |
| 245 | raise TestFailed, 'buffers should not be equal' |
Fred Drake | 485f340 | 2002-05-02 04:27:20 +0000 | [diff] [blame] | 246 | if str(b) != ('asdf' * 5): |
| 247 | raise TestFailed, 'repeated buffer has wrong content' |
| 248 | if str(a * 0) != '': |
| 249 | raise TestFailed, 'repeated buffer zero times has wrong content' |
| 250 | if str(a + buffer('def')) != 'asdfdef': |
| 251 | raise TestFailed, 'concatenation of buffers yields wrong content' |
Neil Schemenauer | 29302a7 | 2004-09-24 15:35:15 +0000 | [diff] [blame] | 252 | if str(buffer(a)) != 'asdf': |
| 253 | raise TestFailed, 'composing buffers failed' |
| 254 | if str(buffer(a, 2)) != 'df': |
| 255 | raise TestFailed, 'specifying buffer offset failed' |
| 256 | if str(buffer(a, 0, 2)) != 'as': |
| 257 | raise TestFailed, 'specifying buffer size failed' |
| 258 | if str(buffer(a, 1, 2)) != 'sd': |
| 259 | raise TestFailed, 'specifying buffer offset and size failed' |
Neil Schemenauer | 6bc937c | 2004-09-24 19:18:42 +0000 | [diff] [blame] | 260 | try: buffer(buffer('asdf', 1), -1) |
| 261 | except ValueError: pass |
| 262 | else: raise TestFailed, "buffer(buffer('asdf', 1), -1) should raise ValueError" |
| 263 | if str(buffer(buffer('asdf', 0, 2), 0)) != 'as': |
| 264 | raise TestFailed, 'composing length-specified buffer failed' |
| 265 | if str(buffer(buffer('asdf', 0, 2), 0, 5000)) != 'as': |
| 266 | raise TestFailed, 'composing length-specified buffer failed' |
| 267 | if str(buffer(buffer('asdf', 0, 2), 0, -1)) != 'as': |
Tim Peters | 5a59d88 | 2004-09-24 23:16:41 +0000 | [diff] [blame] | 268 | raise TestFailed, 'composing length-specified buffer failed' |
Neil Schemenauer | 6bc937c | 2004-09-24 19:18:42 +0000 | [diff] [blame] | 269 | if str(buffer(buffer('asdf', 0, 2), 1, 2)) != 's': |
| 270 | raise TestFailed, 'composing length-specified buffer failed' |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 271 | |
| 272 | try: a[1] = 'g' |
| 273 | except TypeError: pass |
| 274 | else: raise TestFailed, "buffer assignment should raise TypeError" |
| 275 | |
| 276 | try: a[0:1] = 'g' |
| 277 | except TypeError: pass |
| 278 | else: raise TestFailed, "buffer slice assignment should raise TypeError" |