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