Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1 | """Unit tests for the bytes type.""" |
| 2 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 3 | import os |
| 4 | import re |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 5 | import sys |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame^] | 6 | import copy |
Guido van Rossum | 0ad0812 | 2007-04-11 04:37:43 +0000 | [diff] [blame] | 7 | import pickle |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 8 | import tempfile |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 9 | import unittest |
| 10 | import test.test_support |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 11 | import test.string_tests |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 12 | import test.buffer_tests |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 13 | |
| 14 | |
| 15 | class BytesTest(unittest.TestCase): |
| 16 | |
| 17 | def test_basics(self): |
| 18 | b = bytes() |
| 19 | self.assertEqual(type(b), bytes) |
| 20 | self.assertEqual(b.__class__, bytes) |
| 21 | |
| 22 | def test_empty_sequence(self): |
| 23 | b = bytes() |
| 24 | self.assertEqual(len(b), 0) |
| 25 | self.assertRaises(IndexError, lambda: b[0]) |
| 26 | self.assertRaises(IndexError, lambda: b[1]) |
| 27 | self.assertRaises(IndexError, lambda: b[sys.maxint]) |
| 28 | self.assertRaises(IndexError, lambda: b[sys.maxint+1]) |
| 29 | self.assertRaises(IndexError, lambda: b[10**100]) |
| 30 | self.assertRaises(IndexError, lambda: b[-1]) |
| 31 | self.assertRaises(IndexError, lambda: b[-2]) |
| 32 | self.assertRaises(IndexError, lambda: b[-sys.maxint]) |
| 33 | self.assertRaises(IndexError, lambda: b[-sys.maxint-1]) |
| 34 | self.assertRaises(IndexError, lambda: b[-sys.maxint-2]) |
| 35 | self.assertRaises(IndexError, lambda: b[-10**100]) |
| 36 | |
| 37 | def test_from_list(self): |
| 38 | ints = list(range(256)) |
| 39 | b = bytes(i for i in ints) |
| 40 | self.assertEqual(len(b), 256) |
| 41 | self.assertEqual(list(b), ints) |
| 42 | |
| 43 | def test_from_index(self): |
| 44 | class C: |
| 45 | def __init__(self, i=0): |
| 46 | self.i = i |
| 47 | def __index__(self): |
| 48 | return self.i |
| 49 | b = bytes([C(), C(1), C(254), C(255)]) |
| 50 | self.assertEqual(list(b), [0, 1, 254, 255]) |
Guido van Rossum | e06b6b8 | 2006-04-23 07:43:54 +0000 | [diff] [blame] | 51 | self.assertRaises(ValueError, bytes, [C(-1)]) |
| 52 | self.assertRaises(ValueError, bytes, [C(256)]) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 53 | |
| 54 | def test_constructor_type_errors(self): |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 55 | self.assertRaises(TypeError, bytes, 0.0) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 56 | class C: |
| 57 | pass |
Guido van Rossum | e06b6b8 | 2006-04-23 07:43:54 +0000 | [diff] [blame] | 58 | self.assertRaises(TypeError, bytes, ["0"]) |
| 59 | self.assertRaises(TypeError, bytes, [0.0]) |
| 60 | self.assertRaises(TypeError, bytes, [None]) |
| 61 | self.assertRaises(TypeError, bytes, [C()]) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 62 | |
| 63 | def test_constructor_value_errors(self): |
Guido van Rossum | e06b6b8 | 2006-04-23 07:43:54 +0000 | [diff] [blame] | 64 | self.assertRaises(ValueError, bytes, [-1]) |
| 65 | self.assertRaises(ValueError, bytes, [-sys.maxint]) |
| 66 | self.assertRaises(ValueError, bytes, [-sys.maxint-1]) |
Thomas Wouters | d204a71 | 2006-08-22 13:41:17 +0000 | [diff] [blame] | 67 | self.assertRaises(ValueError, bytes, [-sys.maxint-2]) |
| 68 | self.assertRaises(ValueError, bytes, [-10**100]) |
Guido van Rossum | e06b6b8 | 2006-04-23 07:43:54 +0000 | [diff] [blame] | 69 | self.assertRaises(ValueError, bytes, [256]) |
| 70 | self.assertRaises(ValueError, bytes, [257]) |
| 71 | self.assertRaises(ValueError, bytes, [sys.maxint]) |
Thomas Wouters | d204a71 | 2006-08-22 13:41:17 +0000 | [diff] [blame] | 72 | self.assertRaises(ValueError, bytes, [sys.maxint+1]) |
| 73 | self.assertRaises(ValueError, bytes, [10**100]) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 74 | |
| 75 | def test_repr(self): |
Georg Brandl | ee91be4 | 2007-02-24 19:41:35 +0000 | [diff] [blame] | 76 | self.assertEqual(repr(bytes()), "b''") |
Guido van Rossum | 57b93ad | 2007-05-08 19:09:34 +0000 | [diff] [blame] | 77 | self.assertEqual(repr(bytes([0])), "b'\\x00'") |
| 78 | self.assertEqual(repr(bytes([0, 1, 254, 255])), |
| 79 | "b'\\x00\\x01\\xfe\\xff'") |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 80 | self.assertEqual(repr(b"abc"), "b'abc'") |
| 81 | self.assertEqual(repr(b"'"), "b'\\''") |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 82 | |
| 83 | def test_compare(self): |
| 84 | b1 = bytes([1, 2, 3]) |
| 85 | b2 = bytes([1, 2, 3]) |
| 86 | b3 = bytes([1, 3]) |
| 87 | |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 88 | self.assertEqual(b1, b2) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 89 | self.failUnless(b2 != b3) |
| 90 | self.failUnless(b1 <= b2) |
| 91 | self.failUnless(b1 <= b3) |
| 92 | self.failUnless(b1 < b3) |
| 93 | self.failUnless(b1 >= b2) |
| 94 | self.failUnless(b3 >= b2) |
| 95 | self.failUnless(b3 > b2) |
| 96 | |
| 97 | self.failIf(b1 != b2) |
| 98 | self.failIf(b2 == b3) |
| 99 | self.failIf(b1 > b2) |
| 100 | self.failIf(b1 > b3) |
| 101 | self.failIf(b1 >= b3) |
| 102 | self.failIf(b1 < b2) |
| 103 | self.failIf(b3 < b2) |
| 104 | self.failIf(b3 <= b2) |
| 105 | |
Guido van Rossum | 343e97f | 2007-04-09 00:43:24 +0000 | [diff] [blame] | 106 | def test_compare_to_str(self): |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 107 | self.assertEqual(b"abc" == str8(b"abc"), True) |
| 108 | self.assertEqual(b"ab" != str8(b"abc"), True) |
| 109 | self.assertEqual(b"ab" <= str8(b"abc"), True) |
| 110 | self.assertEqual(b"ab" < str8(b"abc"), True) |
| 111 | self.assertEqual(b"abc" >= str8(b"ab"), True) |
| 112 | self.assertEqual(b"abc" > str8(b"ab"), True) |
Guido van Rossum | 343e97f | 2007-04-09 00:43:24 +0000 | [diff] [blame] | 113 | |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 114 | self.assertEqual(b"abc" != str8(b"abc"), False) |
| 115 | self.assertEqual(b"ab" == str8(b"abc"), False) |
| 116 | self.assertEqual(b"ab" > str8(b"abc"), False) |
| 117 | self.assertEqual(b"ab" >= str8(b"abc"), False) |
| 118 | self.assertEqual(b"abc" < str8(b"ab"), False) |
| 119 | self.assertEqual(b"abc" <= str8(b"ab"), False) |
Guido van Rossum | 343e97f | 2007-04-09 00:43:24 +0000 | [diff] [blame] | 120 | |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 121 | self.assertEqual(str8(b"abc") == b"abc", True) |
| 122 | self.assertEqual(str8(b"ab") != b"abc", True) |
| 123 | self.assertEqual(str8(b"ab") <= b"abc", True) |
| 124 | self.assertEqual(str8(b"ab") < b"abc", True) |
| 125 | self.assertEqual(str8(b"abc") >= b"ab", True) |
| 126 | self.assertEqual(str8(b"abc") > b"ab", True) |
Guido van Rossum | ebea9be | 2007-04-09 00:49:13 +0000 | [diff] [blame] | 127 | |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 128 | self.assertEqual(str8(b"abc") != b"abc", False) |
| 129 | self.assertEqual(str8(b"ab") == b"abc", False) |
| 130 | self.assertEqual(str8(b"ab") > b"abc", False) |
| 131 | self.assertEqual(str8(b"ab") >= b"abc", False) |
| 132 | self.assertEqual(str8(b"abc") < b"ab", False) |
| 133 | self.assertEqual(str8(b"abc") <= b"ab", False) |
Guido van Rossum | ebea9be | 2007-04-09 00:49:13 +0000 | [diff] [blame] | 134 | |
Guido van Rossum | 1e35e76 | 2007-10-09 17:21:10 +0000 | [diff] [blame] | 135 | # Byte comparisons with unicode should always fail! |
Guido van Rossum | ebea9be | 2007-04-09 00:49:13 +0000 | [diff] [blame] | 136 | # Test this for all expected byte orders and Unicode character sizes |
Guido van Rossum | 1e35e76 | 2007-10-09 17:21:10 +0000 | [diff] [blame] | 137 | self.assertEqual(b"\0a\0b\0c" == "abc", False) |
| 138 | self.assertEqual(b"\0\0\0a\0\0\0b\0\0\0c" == "abc", False) |
| 139 | self.assertEqual(b"a\0b\0c\0" == "abc", False) |
| 140 | self.assertEqual(b"a\0\0\0b\0\0\0c\0\0\0" == "abc", False) |
| 141 | self.assertEqual(bytes() == str(), False) |
| 142 | self.assertEqual(bytes() != str(), True) |
Guido van Rossum | ebea9be | 2007-04-09 00:49:13 +0000 | [diff] [blame] | 143 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 144 | def test_nohash(self): |
| 145 | self.assertRaises(TypeError, hash, bytes()) |
| 146 | |
| 147 | def test_doc(self): |
| 148 | self.failUnless(bytes.__doc__ != None) |
| 149 | self.failUnless(bytes.__doc__.startswith("bytes(")) |
| 150 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 151 | def test_buffer_api(self): |
Guido van Rossum | 75d6f1a | 2007-05-24 17:52:00 +0000 | [diff] [blame] | 152 | short_sample = b"Hello world\n" |
| 153 | sample = short_sample + b"x"*(20 - len(short_sample)) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 154 | tfn = tempfile.mktemp() |
| 155 | try: |
| 156 | # Prepare |
| 157 | with open(tfn, "wb") as f: |
| 158 | f.write(short_sample) |
| 159 | # Test readinto |
| 160 | with open(tfn, "rb") as f: |
Guido van Rossum | 75d6f1a | 2007-05-24 17:52:00 +0000 | [diff] [blame] | 161 | b = b"x"*20 |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 162 | n = f.readinto(b) |
| 163 | self.assertEqual(n, len(short_sample)) |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 164 | self.assertEqual(list(b), list(sample)) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 165 | # Test writing in binary mode |
| 166 | with open(tfn, "wb") as f: |
| 167 | f.write(b) |
| 168 | with open(tfn, "rb") as f: |
| 169 | self.assertEqual(f.read(), sample) |
Guido van Rossum | 13633bb | 2007-04-13 18:42:35 +0000 | [diff] [blame] | 170 | # Text mode is ambiguous; don't test |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 171 | finally: |
| 172 | try: |
| 173 | os.remove(tfn) |
| 174 | except os.error: |
| 175 | pass |
| 176 | |
| 177 | def test_reversed(self): |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 178 | input = list(map(ord, "Hello")) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 179 | b = bytes(input) |
| 180 | output = list(reversed(b)) |
| 181 | input.reverse() |
| 182 | self.assertEqual(output, input) |
| 183 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 184 | def test_reverse(self): |
| 185 | b = b'hello' |
| 186 | self.assertEqual(b.reverse(), None) |
| 187 | self.assertEqual(b, b'olleh') |
| 188 | b = b'hello1' # test even number of items |
| 189 | b.reverse() |
| 190 | self.assertEqual(b, b'1olleh') |
| 191 | b = bytes() |
| 192 | b.reverse() |
| 193 | self.assertFalse(b) |
| 194 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 195 | def test_getslice(self): |
| 196 | def by(s): |
| 197 | return bytes(map(ord, s)) |
| 198 | b = by("Hello, world") |
| 199 | |
| 200 | self.assertEqual(b[:5], by("Hello")) |
| 201 | self.assertEqual(b[1:5], by("ello")) |
| 202 | self.assertEqual(b[5:7], by(", ")) |
| 203 | self.assertEqual(b[7:], by("world")) |
| 204 | self.assertEqual(b[7:12], by("world")) |
| 205 | self.assertEqual(b[7:100], by("world")) |
| 206 | |
| 207 | self.assertEqual(b[:-7], by("Hello")) |
| 208 | self.assertEqual(b[-11:-7], by("ello")) |
| 209 | self.assertEqual(b[-7:-5], by(", ")) |
| 210 | self.assertEqual(b[-5:], by("world")) |
| 211 | self.assertEqual(b[-5:12], by("world")) |
| 212 | self.assertEqual(b[-5:100], by("world")) |
| 213 | self.assertEqual(b[-100:5], by("Hello")) |
| 214 | |
Thomas Wouters | 376446d | 2006-12-19 08:30:14 +0000 | [diff] [blame] | 215 | def test_extended_getslice(self): |
| 216 | # Test extended slicing by comparing with list slicing. |
| 217 | L = list(range(255)) |
| 218 | b = bytes(L) |
| 219 | indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) |
| 220 | for start in indices: |
| 221 | for stop in indices: |
| 222 | # Skip step 0 (invalid) |
| 223 | for step in indices[1:]: |
| 224 | self.assertEqual(b[start:stop:step], bytes(L[start:stop:step])) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 225 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 226 | def test_regexps(self): |
| 227 | def by(s): |
| 228 | return bytes(map(ord, s)) |
| 229 | b = by("Hello, world") |
| 230 | self.assertEqual(re.findall(r"\w+", b), [by("Hello"), by("world")]) |
| 231 | |
| 232 | def test_setitem(self): |
| 233 | b = bytes([1, 2, 3]) |
| 234 | b[1] = 100 |
| 235 | self.assertEqual(b, bytes([1, 100, 3])) |
| 236 | b[-1] = 200 |
| 237 | self.assertEqual(b, bytes([1, 100, 200])) |
| 238 | class C: |
| 239 | def __init__(self, i=0): |
| 240 | self.i = i |
| 241 | def __index__(self): |
| 242 | return self.i |
| 243 | b[0] = C(10) |
| 244 | self.assertEqual(b, bytes([10, 100, 200])) |
| 245 | try: |
| 246 | b[3] = 0 |
| 247 | self.fail("Didn't raise IndexError") |
| 248 | except IndexError: |
| 249 | pass |
| 250 | try: |
| 251 | b[-10] = 0 |
| 252 | self.fail("Didn't raise IndexError") |
| 253 | except IndexError: |
| 254 | pass |
| 255 | try: |
| 256 | b[0] = 256 |
| 257 | self.fail("Didn't raise ValueError") |
| 258 | except ValueError: |
| 259 | pass |
| 260 | try: |
| 261 | b[0] = C(-1) |
| 262 | self.fail("Didn't raise ValueError") |
| 263 | except ValueError: |
| 264 | pass |
| 265 | try: |
| 266 | b[0] = None |
| 267 | self.fail("Didn't raise TypeError") |
| 268 | except TypeError: |
| 269 | pass |
| 270 | |
| 271 | def test_delitem(self): |
| 272 | b = bytes(range(10)) |
| 273 | del b[0] |
| 274 | self.assertEqual(b, bytes(range(1, 10))) |
| 275 | del b[-1] |
| 276 | self.assertEqual(b, bytes(range(1, 9))) |
| 277 | del b[4] |
| 278 | self.assertEqual(b, bytes([1, 2, 3, 4, 6, 7, 8])) |
| 279 | |
| 280 | def test_setslice(self): |
| 281 | b = bytes(range(10)) |
| 282 | self.assertEqual(list(b), list(range(10))) |
| 283 | |
| 284 | b[0:5] = bytes([1, 1, 1, 1, 1]) |
| 285 | self.assertEqual(b, bytes([1, 1, 1, 1, 1, 5, 6, 7, 8, 9])) |
| 286 | |
| 287 | del b[0:-5] |
| 288 | self.assertEqual(b, bytes([5, 6, 7, 8, 9])) |
| 289 | |
| 290 | b[0:0] = bytes([0, 1, 2, 3, 4]) |
| 291 | self.assertEqual(b, bytes(range(10))) |
| 292 | |
| 293 | b[-7:-3] = bytes([100, 101]) |
| 294 | self.assertEqual(b, bytes([0, 1, 2, 100, 101, 7, 8, 9])) |
| 295 | |
| 296 | b[3:5] = [3, 4, 5, 6] |
| 297 | self.assertEqual(b, bytes(range(10))) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 298 | |
Thomas Wouters | 9a6e62b | 2006-08-23 23:20:29 +0000 | [diff] [blame] | 299 | b[3:0] = [42, 42, 42] |
| 300 | self.assertEqual(b, bytes([0, 1, 2, 42, 42, 42, 3, 4, 5, 6, 7, 8, 9])) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 301 | |
Thomas Wouters | 376446d | 2006-12-19 08:30:14 +0000 | [diff] [blame] | 302 | def test_extended_set_del_slice(self): |
| 303 | indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300) |
| 304 | for start in indices: |
| 305 | for stop in indices: |
| 306 | # Skip invalid step 0 |
| 307 | for step in indices[1:]: |
| 308 | L = list(range(255)) |
| 309 | b = bytes(L) |
| 310 | # Make sure we have a slice of exactly the right length, |
| 311 | # but with different data. |
| 312 | data = L[start:stop:step] |
| 313 | data.reverse() |
| 314 | L[start:stop:step] = data |
| 315 | b[start:stop:step] = data |
| 316 | self.assertEquals(b, bytes(L)) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 317 | |
Thomas Wouters | 376446d | 2006-12-19 08:30:14 +0000 | [diff] [blame] | 318 | del L[start:stop:step] |
| 319 | del b[start:stop:step] |
| 320 | self.assertEquals(b, bytes(L)) |
| 321 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 322 | def test_setslice_trap(self): |
| 323 | # This test verifies that we correctly handle assigning self |
| 324 | # to a slice of self (the old Lambert Meertens trap). |
| 325 | b = bytes(range(256)) |
| 326 | b[8:] = b |
| 327 | self.assertEqual(b, bytes(list(range(8)) + list(range(256)))) |
| 328 | |
| 329 | def test_encoding(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 330 | sample = "Hello world\n\u1234\u5678\u9abc\udef0" |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 331 | for enc in ("utf8", "utf16"): |
| 332 | b = bytes(sample, enc) |
Guido van Rossum | 4355a47 | 2007-05-04 05:00:04 +0000 | [diff] [blame] | 333 | self.assertEqual(b, bytes(sample.encode(enc))) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 334 | self.assertRaises(UnicodeEncodeError, bytes, sample, "latin1") |
| 335 | b = bytes(sample, "latin1", "ignore") |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 336 | self.assertEqual(b, bytes(sample[:-4], "utf-8")) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 337 | |
| 338 | def test_decode(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 339 | sample = "Hello world\n\u1234\u5678\u9abc\def0\def0" |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 340 | for enc in ("utf8", "utf16"): |
| 341 | b = bytes(sample, enc) |
| 342 | self.assertEqual(b.decode(enc), sample) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 343 | sample = "Hello world\n\x80\x81\xfe\xff" |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 344 | b = bytes(sample, "latin1") |
| 345 | self.assertRaises(UnicodeDecodeError, b.decode, "utf8") |
| 346 | self.assertEqual(b.decode("utf8", "ignore"), "Hello world\n") |
| 347 | |
| 348 | def test_from_buffer(self): |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 349 | sample = str8(b"Hello world\n\x80\x81\xfe\xff") |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 350 | buf = memoryview(sample) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 351 | b = bytes(buf) |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 352 | self.assertEqual(b, bytes(sample)) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 353 | |
| 354 | def test_to_str(self): |
| 355 | sample = "Hello world\n\x80\x81\xfe\xff" |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 356 | b = bytes(sample, "utf-8") |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 357 | self.assertEqual(str(b), sample) |
| 358 | |
| 359 | def test_from_int(self): |
| 360 | b = bytes(0) |
| 361 | self.assertEqual(b, bytes()) |
| 362 | b = bytes(10) |
| 363 | self.assertEqual(b, bytes([0]*10)) |
| 364 | b = bytes(10000) |
| 365 | self.assertEqual(b, bytes([0]*10000)) |
| 366 | |
| 367 | def test_concat(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 368 | b1 = b"abc" |
| 369 | b2 = b"def" |
| 370 | self.assertEqual(b1 + b2, b"abcdef") |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 371 | self.assertEqual(b1 + str8(b"def"), b"abcdef") |
| 372 | self.assertEqual(str8(b"def") + b1, b"defabc") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 373 | self.assertRaises(TypeError, lambda: b1 + "def") |
| 374 | self.assertRaises(TypeError, lambda: "abc" + b2) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 375 | |
| 376 | def test_repeat(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 377 | b = b"abc" |
| 378 | self.assertEqual(b * 3, b"abcabcabc") |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 379 | self.assertEqual(b * 0, bytes()) |
| 380 | self.assertEqual(b * -1, bytes()) |
| 381 | self.assertRaises(TypeError, lambda: b * 3.14) |
| 382 | self.assertRaises(TypeError, lambda: 3.14 * b) |
| 383 | self.assertRaises(MemoryError, lambda: b * sys.maxint) |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 384 | |
| 385 | def test_repeat_1char(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 386 | self.assertEqual(b'x'*100, bytes([ord('x')]*100)) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 387 | |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 388 | def test_iconcat(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 389 | b = b"abc" |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 390 | b1 = b |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 391 | b += b"def" |
| 392 | self.assertEqual(b, b"abcdef") |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 393 | self.assertEqual(b, b1) |
| 394 | self.failUnless(b is b1) |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 395 | b += str8(b"xyz") |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 396 | self.assertEqual(b, b"abcdefxyz") |
| 397 | try: |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 398 | b += "" |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 399 | except TypeError: |
| 400 | pass |
| 401 | else: |
| 402 | self.fail("bytes += unicode didn't raise TypeError") |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 403 | |
| 404 | def test_irepeat(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 405 | b = b"abc" |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 406 | b1 = b |
| 407 | b *= 3 |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 408 | self.assertEqual(b, b"abcabcabc") |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 409 | self.assertEqual(b, b1) |
| 410 | self.failUnless(b is b1) |
| 411 | |
| 412 | def test_irepeat_1char(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 413 | b = b"x" |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 414 | b1 = b |
| 415 | b *= 100 |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 416 | self.assertEqual(b, bytes([ord("x")]*100)) |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 417 | self.assertEqual(b, b1) |
| 418 | self.failUnless(b is b1) |
| 419 | |
| 420 | def test_contains(self): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 421 | b = b"abc" |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 422 | self.failUnless(ord('a') in b) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 423 | self.failUnless(int(ord('a')) in b) |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 424 | self.failIf(200 in b) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 425 | self.failIf(200 in b) |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 426 | self.assertRaises(ValueError, lambda: 300 in b) |
| 427 | self.assertRaises(ValueError, lambda: -1 in b) |
| 428 | self.assertRaises(TypeError, lambda: None in b) |
| 429 | self.assertRaises(TypeError, lambda: float(ord('a')) in b) |
| 430 | self.assertRaises(TypeError, lambda: "a" in b) |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 431 | self.failUnless(b"" in b) |
| 432 | self.failUnless(b"a" in b) |
| 433 | self.failUnless(b"b" in b) |
| 434 | self.failUnless(b"c" in b) |
| 435 | self.failUnless(b"ab" in b) |
| 436 | self.failUnless(b"bc" in b) |
| 437 | self.failUnless(b"abc" in b) |
| 438 | self.failIf(b"ac" in b) |
| 439 | self.failIf(b"d" in b) |
| 440 | self.failIf(b"dab" in b) |
| 441 | self.failIf(b"abd" in b) |
Guido van Rossum | 13e5721 | 2006-04-27 22:54:26 +0000 | [diff] [blame] | 442 | |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 443 | def test_alloc(self): |
| 444 | b = bytes() |
| 445 | alloc = b.__alloc__() |
| 446 | self.assert_(alloc >= 0) |
| 447 | seq = [alloc] |
| 448 | for i in range(100): |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 449 | b += b"x" |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 450 | alloc = b.__alloc__() |
| 451 | self.assert_(alloc >= len(b)) |
| 452 | if alloc not in seq: |
| 453 | seq.append(alloc) |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 454 | |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 455 | def test_fromhex(self): |
| 456 | self.assertRaises(TypeError, bytes.fromhex) |
| 457 | self.assertRaises(TypeError, bytes.fromhex, 1) |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 458 | self.assertEquals(bytes.fromhex(''), bytes()) |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 459 | b = bytes([0x1a, 0x2b, 0x30]) |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 460 | self.assertEquals(bytes.fromhex('1a2B30'), b) |
| 461 | self.assertEquals(bytes.fromhex(' 1A 2B 30 '), b) |
| 462 | self.assertEquals(bytes.fromhex('0000'), b'\0\0') |
| 463 | self.assertRaises(TypeError, bytes.fromhex, b'1B') |
| 464 | self.assertRaises(ValueError, bytes.fromhex, 'a') |
| 465 | self.assertRaises(ValueError, bytes.fromhex, 'rt') |
| 466 | self.assertRaises(ValueError, bytes.fromhex, '1a b cd') |
| 467 | self.assertRaises(ValueError, bytes.fromhex, '\x00') |
| 468 | self.assertRaises(ValueError, bytes.fromhex, '12 \x00 34') |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 469 | |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 470 | def test_join(self): |
Guido van Rossum | cd6ae68 | 2007-05-09 19:52:16 +0000 | [diff] [blame] | 471 | self.assertEqual(b"".join([]), bytes()) |
| 472 | self.assertEqual(b"".join([bytes()]), bytes()) |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 473 | for lst in [[b"abc"], [b"a", b"bc"], [b"ab", b"c"], [b"a", b"b", b"c"]]: |
| 474 | self.assertEqual(b"".join(lst), b"abc") |
| 475 | self.assertEqual(b"".join(tuple(lst)), b"abc") |
| 476 | self.assertEqual(b"".join(iter(lst)), b"abc") |
Guido van Rossum | cd6ae68 | 2007-05-09 19:52:16 +0000 | [diff] [blame] | 477 | self.assertEqual(b".".join([b"ab", b"cd"]), b"ab.cd") |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 478 | # XXX more... |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 479 | |
| 480 | def test_literal(self): |
| 481 | tests = [ |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 482 | (b"Wonderful spam", "Wonderful spam"), |
| 483 | (br"Wonderful spam too", "Wonderful spam too"), |
| 484 | (b"\xaa\x00\000\200", "\xaa\x00\000\200"), |
| 485 | (br"\xaa\x00\000\200", r"\xaa\x00\000\200"), |
Thomas Wouters | 00e41de | 2007-02-23 19:56:57 +0000 | [diff] [blame] | 486 | ] |
| 487 | for b, s in tests: |
| 488 | self.assertEqual(b, bytes(s, 'latin-1')) |
| 489 | for c in range(128, 256): |
| 490 | self.assertRaises(SyntaxError, eval, |
| 491 | 'b"%s"' % chr(c)) |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 492 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 493 | def test_extend(self): |
| 494 | orig = b'hello' |
| 495 | a = bytes(orig) |
| 496 | a.extend(a) |
| 497 | self.assertEqual(a, orig + orig) |
| 498 | self.assertEqual(a[5:], orig) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 499 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 500 | def test_remove(self): |
| 501 | b = b'hello' |
| 502 | b.remove(ord('l')) |
| 503 | self.assertEqual(b, b'helo') |
| 504 | b.remove(ord('l')) |
| 505 | self.assertEqual(b, b'heo') |
| 506 | self.assertRaises(ValueError, lambda: b.remove(ord('l'))) |
| 507 | self.assertRaises(ValueError, lambda: b.remove(400)) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 508 | self.assertRaises(TypeError, lambda: b.remove('e')) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 509 | # remove first and last |
| 510 | b.remove(ord('o')) |
| 511 | b.remove(ord('h')) |
| 512 | self.assertEqual(b, b'e') |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 513 | self.assertRaises(TypeError, lambda: b.remove(b'e')) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 514 | |
| 515 | def test_pop(self): |
| 516 | b = b'world' |
| 517 | self.assertEqual(b.pop(), ord('d')) |
| 518 | self.assertEqual(b.pop(0), ord('w')) |
| 519 | self.assertEqual(b.pop(-2), ord('r')) |
| 520 | self.assertRaises(IndexError, lambda: b.pop(10)) |
| 521 | self.assertRaises(OverflowError, lambda: bytes().pop()) |
| 522 | |
| 523 | def test_nosort(self): |
| 524 | self.assertRaises(AttributeError, lambda: bytes().sort()) |
| 525 | |
| 526 | def test_index(self): |
| 527 | b = b'parrot' |
| 528 | self.assertEqual(b.index('p'), 0) |
| 529 | self.assertEqual(b.index('rr'), 2) |
| 530 | self.assertEqual(b.index('t'), 5) |
| 531 | self.assertRaises(ValueError, lambda: b.index('w')) |
| 532 | |
| 533 | def test_count(self): |
| 534 | b = b'mississippi' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 535 | self.assertEqual(b.count(b'i'), 4) |
| 536 | self.assertEqual(b.count(b'ss'), 2) |
| 537 | self.assertEqual(b.count(b'w'), 0) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 538 | |
| 539 | def test_append(self): |
| 540 | b = b'hell' |
| 541 | b.append(ord('o')) |
| 542 | self.assertEqual(b, b'hello') |
| 543 | self.assertEqual(b.append(100), None) |
| 544 | b = bytes() |
| 545 | b.append(ord('A')) |
| 546 | self.assertEqual(len(b), 1) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 547 | self.assertRaises(TypeError, lambda: b.append(b'o')) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 548 | |
| 549 | def test_insert(self): |
| 550 | b = b'msssspp' |
| 551 | b.insert(1, ord('i')) |
| 552 | b.insert(4, ord('i')) |
| 553 | b.insert(-2, ord('i')) |
| 554 | b.insert(1000, ord('i')) |
| 555 | self.assertEqual(b, b'mississippi') |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 556 | self.assertRaises(TypeError, lambda: b.insert(0, b'1')) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 557 | |
| 558 | def test_startswith(self): |
| 559 | b = b'hello' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 560 | self.assertFalse(bytes().startswith(b"anything")) |
| 561 | self.assertTrue(b.startswith(b"hello")) |
| 562 | self.assertTrue(b.startswith(b"hel")) |
| 563 | self.assertTrue(b.startswith(b"h")) |
| 564 | self.assertFalse(b.startswith(b"hellow")) |
| 565 | self.assertFalse(b.startswith(b"ha")) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 566 | |
| 567 | def test_endswith(self): |
| 568 | b = b'hello' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 569 | self.assertFalse(bytes().endswith(b"anything")) |
| 570 | self.assertTrue(b.endswith(b"hello")) |
| 571 | self.assertTrue(b.endswith(b"llo")) |
| 572 | self.assertTrue(b.endswith(b"o")) |
| 573 | self.assertFalse(b.endswith(b"whello")) |
| 574 | self.assertFalse(b.endswith(b"no")) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 575 | |
| 576 | def test_find(self): |
| 577 | b = b'mississippi' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 578 | self.assertEqual(b.find(b'ss'), 2) |
| 579 | self.assertEqual(b.find(b'ss', 3), 5) |
| 580 | self.assertEqual(b.find(b'ss', 1, 7), 2) |
| 581 | self.assertEqual(b.find(b'ss', 1, 3), -1) |
| 582 | self.assertEqual(b.find(b'w'), -1) |
| 583 | self.assertEqual(b.find(b'mississippian'), -1) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 584 | |
| 585 | def test_rfind(self): |
| 586 | b = b'mississippi' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 587 | self.assertEqual(b.rfind(b'ss'), 5) |
| 588 | self.assertEqual(b.rfind(b'ss', 3), 5) |
| 589 | self.assertEqual(b.rfind(b'ss', 0, 6), 2) |
| 590 | self.assertEqual(b.rfind(b'w'), -1) |
| 591 | self.assertEqual(b.rfind(b'mississippian'), -1) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 592 | |
| 593 | def test_index(self): |
| 594 | b = b'world' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 595 | self.assertEqual(b.index(b'w'), 0) |
| 596 | self.assertEqual(b.index(b'orl'), 1) |
| 597 | self.assertRaises(ValueError, b.index, b'worm') |
| 598 | self.assertRaises(ValueError, b.index, b'ldo') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 599 | |
| 600 | def test_rindex(self): |
| 601 | # XXX could be more rigorous |
| 602 | b = b'world' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 603 | self.assertEqual(b.rindex(b'w'), 0) |
| 604 | self.assertEqual(b.rindex(b'orl'), 1) |
| 605 | self.assertRaises(ValueError, b.rindex, b'worm') |
| 606 | self.assertRaises(ValueError, b.rindex, b'ldo') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 607 | |
| 608 | def test_replace(self): |
| 609 | b = b'mississippi' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 610 | self.assertEqual(b.replace(b'i', b'a'), b'massassappa') |
| 611 | self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 612 | |
| 613 | def test_translate(self): |
| 614 | b = b'hello' |
| 615 | rosetta = bytes(range(0, 256)) |
| 616 | rosetta[ord('o')] = ord('e') |
| 617 | c = b.translate(rosetta, b'l') |
| 618 | self.assertEqual(b, b'hello') |
| 619 | self.assertEqual(c, b'hee') |
| 620 | |
| 621 | def test_split(self): |
| 622 | b = b'mississippi' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 623 | self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b'']) |
| 624 | self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi']) |
| 625 | self.assertEqual(b.split(b'w'), [b]) |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 626 | |
| 627 | def test_split_whitespace(self): |
| 628 | for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf', |
| 629 | b'arf\fbarf', b'arf\vbarf'): |
| 630 | self.assertEqual(b.split(), [b'arf', b'barf']) |
| 631 | self.assertEqual(b.split(None), [b'arf', b'barf']) |
| 632 | self.assertEqual(b.split(None, 2), [b'arf', b'barf']) |
| 633 | self.assertEqual(b' a bb c '.split(None, 0), [b'a bb c ']) |
| 634 | self.assertEqual(b' a bb c '.split(None, 1), [b'a', b'bb c ']) |
| 635 | self.assertEqual(b' a bb c '.split(None, 2), [b'a', b'bb', b'c ']) |
| 636 | self.assertEqual(b' a bb c '.split(None, 3), [b'a', b'bb', b'c']) |
| 637 | |
| 638 | def test_split_buffer(self): |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 639 | self.assertEqual(b'a b'.split(memoryview(b' ')), [b'a', b'b']) |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 640 | |
| 641 | def test_split_string_error(self): |
| 642 | self.assertRaises(TypeError, b'a b'.split, ' ') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 643 | |
| 644 | def test_rsplit(self): |
| 645 | b = b'mississippi' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 646 | self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b'']) |
| 647 | self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi']) |
| 648 | self.assertEqual(b.rsplit(b'w'), [b]) |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 649 | |
| 650 | def test_rsplit_whitespace(self): |
| 651 | for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf', |
| 652 | b'arf\fbarf', b'arf\vbarf'): |
| 653 | self.assertEqual(b.rsplit(), [b'arf', b'barf']) |
| 654 | self.assertEqual(b.rsplit(None), [b'arf', b'barf']) |
| 655 | self.assertEqual(b.rsplit(None, 2), [b'arf', b'barf']) |
| 656 | self.assertEqual(b' a bb c '.rsplit(None, 0), [b' a bb c']) |
| 657 | self.assertEqual(b' a bb c '.rsplit(None, 1), [b' a bb', b'c']) |
| 658 | self.assertEqual(b' a bb c '.rsplit(None,2), [b' a', b'bb', b'c']) |
| 659 | self.assertEqual(b' a bb c '.rsplit(None, 3), [b'a', b'bb', b'c']) |
| 660 | |
| 661 | def test_rplit_buffer(self): |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 662 | self.assertEqual(b'a b'.rsplit(memoryview(b' ')), [b'a', b'b']) |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 663 | |
| 664 | def test_rplit_string_error(self): |
| 665 | self.assertRaises(TypeError, b'a b'.rsplit, ' ') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 666 | |
| 667 | def test_partition(self): |
| 668 | b = b'mississippi' |
| 669 | self.assertEqual(b.partition(b'ss'), (b'mi', b'ss', b'issippi')) |
| 670 | self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi')) |
| 671 | |
| 672 | def test_rpartition(self): |
| 673 | b = b'mississippi' |
| 674 | self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi')) |
| 675 | self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b'')) |
| 676 | |
Guido van Rossum | 0ad0812 | 2007-04-11 04:37:43 +0000 | [diff] [blame] | 677 | def test_pickling(self): |
Guido van Rossum | 99603b0 | 2007-07-20 00:22:32 +0000 | [diff] [blame] | 678 | for proto in range(pickle.HIGHEST_PROTOCOL): |
| 679 | for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0": |
| 680 | ps = pickle.dumps(b, proto) |
| 681 | q = pickle.loads(ps) |
| 682 | self.assertEqual(b, q) |
Guido van Rossum | 0ad0812 | 2007-04-11 04:37:43 +0000 | [diff] [blame] | 683 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 684 | def test_strip(self): |
| 685 | b = b'mississippi' |
| 686 | self.assertEqual(b.strip(b'i'), b'mississipp') |
| 687 | self.assertEqual(b.strip(b'm'), b'ississippi') |
| 688 | self.assertEqual(b.strip(b'pi'), b'mississ') |
| 689 | self.assertEqual(b.strip(b'im'), b'ssissipp') |
| 690 | self.assertEqual(b.strip(b'pim'), b'ssiss') |
Guido van Rossum | eb29e9a | 2007-08-08 21:55:33 +0000 | [diff] [blame] | 691 | self.assertEqual(b.strip(b), b'') |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 692 | |
| 693 | def test_lstrip(self): |
| 694 | b = b'mississippi' |
| 695 | self.assertEqual(b.lstrip(b'i'), b'mississippi') |
| 696 | self.assertEqual(b.lstrip(b'm'), b'ississippi') |
| 697 | self.assertEqual(b.lstrip(b'pi'), b'mississippi') |
| 698 | self.assertEqual(b.lstrip(b'im'), b'ssissippi') |
| 699 | self.assertEqual(b.lstrip(b'pim'), b'ssissippi') |
| 700 | |
| 701 | def test_rstrip(self): |
| 702 | b = b'mississippi' |
| 703 | self.assertEqual(b.rstrip(b'i'), b'mississipp') |
| 704 | self.assertEqual(b.rstrip(b'm'), b'mississippi') |
| 705 | self.assertEqual(b.rstrip(b'pi'), b'mississ') |
| 706 | self.assertEqual(b.rstrip(b'im'), b'mississipp') |
| 707 | self.assertEqual(b.rstrip(b'pim'), b'mississ') |
| 708 | |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 709 | def test_strip_whitespace(self): |
| 710 | b = b' \t\n\r\f\vabc \t\n\r\f\v' |
| 711 | self.assertEqual(b.strip(), b'abc') |
| 712 | self.assertEqual(b.lstrip(), b'abc \t\n\r\f\v') |
| 713 | self.assertEqual(b.rstrip(), b' \t\n\r\f\vabc') |
| 714 | |
| 715 | def test_strip_buffer(self): |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 716 | self.assertEqual(b'abc'.strip(memoryview(b'ac')), b'b') |
| 717 | self.assertEqual(b'abc'.lstrip(memoryview(b'ac')), b'bc') |
| 718 | self.assertEqual(b'abc'.rstrip(memoryview(b'ac')), b'ab') |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 719 | |
| 720 | def test_strip_string_error(self): |
| 721 | self.assertRaises(TypeError, b'abc'.strip, 'b') |
| 722 | self.assertRaises(TypeError, b'abc'.lstrip, 'b') |
| 723 | self.assertRaises(TypeError, b'abc'.rstrip, 'b') |
| 724 | |
Guido van Rossum | f9e91c9 | 2007-05-08 21:05:48 +0000 | [diff] [blame] | 725 | def test_ord(self): |
| 726 | b = b'\0A\x7f\x80\xff' |
| 727 | self.assertEqual([ord(b[i:i+1]) for i in range(len(b))], |
| 728 | [0, 65, 127, 128, 255]) |
| 729 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 730 | # Optimizations: |
Guido van Rossum | e06b6b8 | 2006-04-23 07:43:54 +0000 | [diff] [blame] | 731 | # __iter__? (optimization) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 732 | # __reversed__? (optimization) |
| 733 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 734 | # XXX More string methods? (Those that don't use character properties) |
| 735 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 736 | # There are tests in string_tests.py that are more |
| 737 | # comprehensive for things like split, partition, etc. |
| 738 | # Unfortunately they are all bundled with tests that |
| 739 | # are not appropriate for bytes |
Guido van Rossum | e06b6b8 | 2006-04-23 07:43:54 +0000 | [diff] [blame] | 740 | |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 741 | # I've started porting some of those into buffer_tests.py, we should port |
| 742 | # the rest that make sense (the code can be cleaned up to use modern |
| 743 | # unittest methods at the same time). |
| 744 | |
| 745 | class BufferPEP3137Test(unittest.TestCase, |
| 746 | test.buffer_tests.MixinBytesBufferCommonTests): |
| 747 | def marshal(self, x): |
| 748 | return bytes(x) |
| 749 | # TODO this should become: |
| 750 | #return buffer(x) |
| 751 | # once the bytes -> buffer and str8 -> bytes rename happens |
| 752 | |
| 753 | def test_returns_new_copy(self): |
| 754 | val = self.marshal(b'1234') |
| 755 | # On immutable types these MAY return a reference to themselves |
| 756 | # but on mutable types like buffer they MUST return a new copy. |
| 757 | for methname in ('zfill', 'rjust', 'ljust', 'center'): |
| 758 | method = getattr(val, methname) |
| 759 | newval = method(3) |
| 760 | self.assertEqual(val, newval) |
| 761 | self.assertTrue(val is not newval, |
| 762 | methname+' returned self on a mutable object') |
| 763 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 764 | |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 765 | class BytesAsStringTest(test.string_tests.BaseTest): |
| 766 | type2test = bytes |
| 767 | |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 768 | def fixtype(self, obj): |
| 769 | if isinstance(obj, str): |
| 770 | return obj.encode("utf-8") |
| 771 | return super().fixtype(obj) |
| 772 | |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 773 | # Currently the bytes containment testing uses a single integer |
| 774 | # value. This may not be the final design, but until then the |
| 775 | # bytes section with in a bytes containment not valid |
| 776 | def test_contains(self): |
| 777 | pass |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 778 | def test_expandtabs(self): |
| 779 | pass |
| 780 | def test_upper(self): |
| 781 | pass |
| 782 | def test_lower(self): |
| 783 | pass |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 784 | |
| 785 | |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame^] | 786 | class BytesSubclass(bytes): |
| 787 | pass |
| 788 | |
| 789 | class BytesSubclassTest(unittest.TestCase): |
| 790 | |
| 791 | def test_basic(self): |
| 792 | self.assert_(issubclass(BytesSubclass, bytes)) |
| 793 | self.assert_(isinstance(BytesSubclass(), bytes)) |
| 794 | |
| 795 | a, b = b"abcd", b"efgh" |
| 796 | _a, _b = BytesSubclass(a), BytesSubclass(b) |
| 797 | |
| 798 | # test comparison operators with subclass instances |
| 799 | self.assert_(_a == _a) |
| 800 | self.assert_(_a != _b) |
| 801 | self.assert_(_a < _b) |
| 802 | self.assert_(_a <= _b) |
| 803 | self.assert_(_b >= _a) |
| 804 | self.assert_(_b > _a) |
| 805 | self.assert_(_a is not a) |
| 806 | |
| 807 | # test concat of subclass instances |
| 808 | self.assertEqual(a + b, _a + _b) |
| 809 | self.assertEqual(a + b, a + _b) |
| 810 | self.assertEqual(a + b, _a + b) |
| 811 | |
| 812 | # test repeat |
| 813 | self.assert_(a*5 == _a*5) |
| 814 | |
| 815 | def test_join(self): |
| 816 | # Make sure join returns a NEW object for single item sequences |
| 817 | # involving a subclass. |
| 818 | # Make sure that it is of the appropriate type. |
| 819 | s1 = BytesSubclass(b"abcd") |
| 820 | s2 = b"".join([s1]) |
| 821 | self.assert_(s1 is not s2) |
| 822 | self.assert_(type(s2) is bytes) |
| 823 | |
| 824 | # Test reverse, calling join on subclass |
| 825 | s3 = s1.join([b"abcd"]) |
| 826 | self.assert_(type(s3) is bytes) |
| 827 | |
| 828 | def test_pickle(self): |
| 829 | a = BytesSubclass(b"abcd") |
| 830 | a.x = 10 |
| 831 | a.y = BytesSubclass(b"efgh") |
| 832 | for proto in range(pickle.HIGHEST_PROTOCOL): |
| 833 | b = pickle.loads(pickle.dumps(a, proto)) |
| 834 | self.assertNotEqual(id(a), id(b)) |
| 835 | self.assertEqual(a, b) |
| 836 | self.assertEqual(a.x, b.x) |
| 837 | self.assertEqual(a.y, b.y) |
| 838 | self.assertEqual(type(a), type(b)) |
| 839 | self.assertEqual(type(a.y), type(b.y)) |
| 840 | |
| 841 | def test_copy(self): |
| 842 | a = BytesSubclass(b"abcd") |
| 843 | a.x = 10 |
| 844 | a.y = BytesSubclass(b"efgh") |
| 845 | for copy_method in (copy.copy, copy.deepcopy): |
| 846 | b = copy_method(a) |
| 847 | self.assertNotEqual(id(a), id(b)) |
| 848 | self.assertEqual(a, b) |
| 849 | self.assertEqual(a.x, b.x) |
| 850 | self.assertEqual(a.y, b.y) |
| 851 | self.assertEqual(type(a), type(b)) |
| 852 | self.assertEqual(type(a.y), type(b.y)) |
| 853 | |
| 854 | def test_init_override(self): |
| 855 | class subclass(bytes): |
| 856 | def __init__(self, newarg=1, *args, **kwargs): |
| 857 | bytes.__init__(self, *args, **kwargs) |
| 858 | x = subclass(4, source=b"abcd") |
| 859 | self.assertEqual(x, b"abcd") |
| 860 | x = subclass(newarg=4, source=b"abcd") |
| 861 | self.assertEqual(x, b"abcd") |
| 862 | |
| 863 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 864 | def test_main(): |
Thomas Wouters | 5f6f27d | 2006-04-23 00:19:58 +0000 | [diff] [blame] | 865 | test.test_support.run_unittest(BytesTest) |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 866 | test.test_support.run_unittest(BytesAsStringTest) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame^] | 867 | test.test_support.run_unittest(BytesSubclassTest) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 868 | test.test_support.run_unittest(BufferPEP3137Test) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 869 | |
| 870 | if __name__ == "__main__": |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame^] | 871 | test_main() |