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