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