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