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