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