Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 1 | """Unit tests for the bytes and buffer types. |
| 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 30 | b = buffer() |
| 31 | self.assertEqual(type(b), buffer) |
| 32 | self.assertEqual(b.__class__, buffer) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 33 | |
| 34 | def test_empty_sequence(self): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 35 | b = buffer() |
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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 51 | b = buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 61 | b = buffer([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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 63 | self.assertRaises(ValueError, buffer, [C(-1)]) |
| 64 | self.assertRaises(ValueError, buffer, [C(256)]) |
| 65 | |
| 66 | def test_from_ssize(self): |
| 67 | self.assertEqual(buffer(0), b'') |
| 68 | self.assertEqual(buffer(1), b'\x00') |
| 69 | self.assertEqual(buffer(5), b'\x00\x00\x00\x00\x00') |
| 70 | self.assertRaises(ValueError, buffer, -1) |
| 71 | |
| 72 | self.assertEqual(buffer('0', 'ascii'), b'0') |
| 73 | self.assertEqual(buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 76 | self.assertRaises(TypeError, buffer, 0.0) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 77 | class C: |
| 78 | pass |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 79 | self.assertRaises(TypeError, buffer, ["0"]) |
| 80 | self.assertRaises(TypeError, buffer, [0.0]) |
| 81 | self.assertRaises(TypeError, buffer, [None]) |
| 82 | self.assertRaises(TypeError, buffer, [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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 85 | self.assertRaises(ValueError, buffer, [-1]) |
| 86 | self.assertRaises(ValueError, buffer, [-sys.maxint]) |
| 87 | self.assertRaises(ValueError, buffer, [-sys.maxint-1]) |
| 88 | self.assertRaises(ValueError, buffer, [-sys.maxint-2]) |
| 89 | self.assertRaises(ValueError, buffer, [-10**100]) |
| 90 | self.assertRaises(ValueError, buffer, [256]) |
| 91 | self.assertRaises(ValueError, buffer, [257]) |
| 92 | self.assertRaises(ValueError, buffer, [sys.maxint]) |
| 93 | self.assertRaises(ValueError, buffer, [sys.maxint+1]) |
| 94 | self.assertRaises(ValueError, buffer, [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: |
| 99 | self.assertEqual(f(buffer()), "buffer(b'')") |
| 100 | self.assertEqual(f(buffer([0])), "buffer(b'\\x00')") |
| 101 | self.assertEqual(f(buffer([0, 1, 254, 255])), |
| 102 | "buffer(b'\\x00\\x01\\xfe\\xff')") |
| 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 109 | b1 = buffer([1, 2, 3]) |
| 110 | b2 = buffer([1, 2, 3]) |
| 111 | b3 = buffer([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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 131 | def test_compare_bytes_to_buffer(self): |
| 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 168 | self.assertEqual(buffer() == str(), False) |
| 169 | self.assertEqual(buffer() != 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 172 | self.assertRaises(TypeError, hash, buffer()) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 173 | |
| 174 | def test_doc(self): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 175 | self.failUnless(buffer.__doc__ != None) |
| 176 | self.failUnless(buffer.__doc__.startswith("buffer("), buffer.__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 | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 180 | def test_buffer_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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 190 | b = buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 208 | b = buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 214 | b = buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 217 | b = buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 220 | b = buffer() |
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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 226 | return buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 247 | b = buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 253 | self.assertEqual(b[start:stop:step], buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 257 | return buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 262 | b = buffer([1, 2, 3]) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 263 | b[1] = 100 |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 264 | self.assertEqual(b, buffer([1, 100, 3])) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 265 | b[-1] = 200 |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 266 | self.assertEqual(b, buffer([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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 273 | self.assertEqual(b, buffer([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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 301 | b = buffer(range(10)) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 302 | del b[0] |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 303 | self.assertEqual(b, buffer(range(1, 10))) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 304 | del b[-1] |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 305 | self.assertEqual(b, buffer(range(1, 9))) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 306 | del b[4] |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 307 | self.assertEqual(b, buffer([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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 310 | b = buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 313 | b[0:5] = buffer([1, 1, 1, 1, 1]) |
| 314 | self.assertEqual(b, buffer([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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 317 | self.assertEqual(b, buffer([5, 6, 7, 8, 9])) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 318 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 319 | b[0:0] = buffer([0, 1, 2, 3, 4]) |
| 320 | self.assertEqual(b, buffer(range(10))) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 321 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 322 | b[-7:-3] = buffer([100, 101]) |
| 323 | self.assertEqual(b, buffer([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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 326 | self.assertEqual(b, buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 329 | self.assertEqual(b, buffer([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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 338 | b = buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 345 | self.assertEquals(b, buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 349 | self.assertEquals(b, buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 354 | b = buffer(range(256)) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 355 | b[8:] = b |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 356 | self.assertEqual(b, buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 361 | b = buffer(sample, enc) |
| 362 | self.assertEqual(b, buffer(sample.encode(enc))) |
| 363 | self.assertRaises(UnicodeEncodeError, buffer, sample, "latin1") |
| 364 | b = buffer(sample, "latin1", "ignore") |
| 365 | self.assertEqual(b, buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 370 | b = buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 373 | b = buffer(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 | |
| 377 | def test_from_buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 380 | b = buffer(buf) |
| 381 | self.assertEqual(b, buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 390 | b = buffer(0) |
| 391 | self.assertEqual(b, buffer()) |
| 392 | b = buffer(10) |
| 393 | self.assertEqual(b, buffer([0]*10)) |
| 394 | b = buffer(10000) |
| 395 | self.assertEqual(b, buffer([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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 407 | for b in b"abc", buffer(b"abc"): |
| 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) |
| 413 | # XXX Shouldn't bytes and buffer agree on what to raise? |
| 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 418 | self.assertEqual(b'x'*100, buffer([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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 421 | b = buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 437 | b = buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 445 | b = buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 453 | for b in b"abc", buffer(b"abc"): |
| 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) |
| 463 | for f in bytes, buffer: |
| 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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 477 | b = buffer() |
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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 489 | self.assertRaises(TypeError, buffer.fromhex) |
| 490 | self.assertRaises(TypeError, buffer.fromhex, 1) |
| 491 | self.assertEquals(buffer.fromhex(''), buffer()) |
| 492 | b = buffer([0x1a, 0x2b, 0x30]) |
| 493 | self.assertEquals(buffer.fromhex('1a2B30'), b) |
| 494 | self.assertEquals(buffer.fromhex(' 1A 2B 30 '), b) |
| 495 | self.assertEquals(buffer.fromhex('0000'), b'\0\0') |
| 496 | self.assertRaises(TypeError, buffer.fromhex, b'1B') |
| 497 | self.assertRaises(ValueError, buffer.fromhex, 'a') |
| 498 | self.assertRaises(ValueError, buffer.fromhex, 'rt') |
| 499 | self.assertRaises(ValueError, buffer.fromhex, '1a b cd') |
| 500 | self.assertRaises(ValueError, buffer.fromhex, '\x00') |
| 501 | self.assertRaises(ValueError, buffer.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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 521 | self.assertEqual(b, buffer(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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 528 | a = buffer(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) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 532 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 533 | def test_remove(self): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 534 | b = buffer(b'hello') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 535 | b.remove(ord('l')) |
| 536 | self.assertEqual(b, b'helo') |
| 537 | b.remove(ord('l')) |
| 538 | self.assertEqual(b, b'heo') |
| 539 | self.assertRaises(ValueError, lambda: b.remove(ord('l'))) |
| 540 | self.assertRaises(ValueError, lambda: b.remove(400)) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 541 | self.assertRaises(TypeError, lambda: b.remove('e')) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 542 | # remove first and last |
| 543 | b.remove(ord('o')) |
| 544 | b.remove(ord('h')) |
| 545 | self.assertEqual(b, b'e') |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 546 | self.assertRaises(TypeError, lambda: b.remove(b'e')) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 547 | |
| 548 | def test_pop(self): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 549 | b = buffer(b'world') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 550 | self.assertEqual(b.pop(), ord('d')) |
| 551 | self.assertEqual(b.pop(0), ord('w')) |
| 552 | self.assertEqual(b.pop(-2), ord('r')) |
| 553 | self.assertRaises(IndexError, lambda: b.pop(10)) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 554 | self.assertRaises(OverflowError, lambda: buffer().pop()) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 555 | |
| 556 | def test_nosort(self): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 557 | self.assertRaises(AttributeError, lambda: buffer().sort()) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 558 | |
| 559 | def test_index(self): |
| 560 | b = b'parrot' |
| 561 | self.assertEqual(b.index('p'), 0) |
| 562 | self.assertEqual(b.index('rr'), 2) |
| 563 | self.assertEqual(b.index('t'), 5) |
| 564 | self.assertRaises(ValueError, lambda: b.index('w')) |
| 565 | |
| 566 | def test_count(self): |
| 567 | b = b'mississippi' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 568 | self.assertEqual(b.count(b'i'), 4) |
| 569 | self.assertEqual(b.count(b'ss'), 2) |
| 570 | self.assertEqual(b.count(b'w'), 0) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 571 | |
| 572 | def test_append(self): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 573 | b = buffer(b'hell') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 574 | b.append(ord('o')) |
| 575 | self.assertEqual(b, b'hello') |
| 576 | self.assertEqual(b.append(100), None) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 577 | b = buffer() |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 578 | b.append(ord('A')) |
| 579 | self.assertEqual(len(b), 1) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 580 | self.assertRaises(TypeError, lambda: b.append(b'o')) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 581 | |
| 582 | def test_insert(self): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 583 | b = buffer(b'msssspp') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 584 | b.insert(1, ord('i')) |
| 585 | b.insert(4, ord('i')) |
| 586 | b.insert(-2, ord('i')) |
| 587 | b.insert(1000, ord('i')) |
| 588 | self.assertEqual(b, b'mississippi') |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 589 | self.assertRaises(TypeError, lambda: b.insert(0, b'1')) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 590 | |
| 591 | def test_startswith(self): |
| 592 | b = b'hello' |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 593 | self.assertFalse(buffer().startswith(b"anything")) |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 594 | self.assertTrue(b.startswith(b"hello")) |
| 595 | self.assertTrue(b.startswith(b"hel")) |
| 596 | self.assertTrue(b.startswith(b"h")) |
| 597 | self.assertFalse(b.startswith(b"hellow")) |
| 598 | self.assertFalse(b.startswith(b"ha")) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 599 | |
| 600 | def test_endswith(self): |
| 601 | b = b'hello' |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 602 | self.assertFalse(buffer().endswith(b"anything")) |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 603 | self.assertTrue(b.endswith(b"hello")) |
| 604 | self.assertTrue(b.endswith(b"llo")) |
| 605 | self.assertTrue(b.endswith(b"o")) |
| 606 | self.assertFalse(b.endswith(b"whello")) |
| 607 | self.assertFalse(b.endswith(b"no")) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 608 | |
| 609 | def test_find(self): |
| 610 | b = b'mississippi' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 611 | self.assertEqual(b.find(b'ss'), 2) |
| 612 | self.assertEqual(b.find(b'ss', 3), 5) |
| 613 | self.assertEqual(b.find(b'ss', 1, 7), 2) |
| 614 | self.assertEqual(b.find(b'ss', 1, 3), -1) |
| 615 | self.assertEqual(b.find(b'w'), -1) |
| 616 | self.assertEqual(b.find(b'mississippian'), -1) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 617 | |
| 618 | def test_rfind(self): |
| 619 | b = b'mississippi' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 620 | self.assertEqual(b.rfind(b'ss'), 5) |
| 621 | self.assertEqual(b.rfind(b'ss', 3), 5) |
| 622 | self.assertEqual(b.rfind(b'ss', 0, 6), 2) |
| 623 | self.assertEqual(b.rfind(b'w'), -1) |
| 624 | self.assertEqual(b.rfind(b'mississippian'), -1) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 625 | |
| 626 | def test_index(self): |
| 627 | b = b'world' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 628 | self.assertEqual(b.index(b'w'), 0) |
| 629 | self.assertEqual(b.index(b'orl'), 1) |
| 630 | self.assertRaises(ValueError, b.index, b'worm') |
| 631 | self.assertRaises(ValueError, b.index, b'ldo') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 632 | |
| 633 | def test_rindex(self): |
| 634 | # XXX could be more rigorous |
| 635 | b = b'world' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 636 | self.assertEqual(b.rindex(b'w'), 0) |
| 637 | self.assertEqual(b.rindex(b'orl'), 1) |
| 638 | self.assertRaises(ValueError, b.rindex, b'worm') |
| 639 | self.assertRaises(ValueError, b.rindex, b'ldo') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 640 | |
| 641 | def test_replace(self): |
| 642 | b = b'mississippi' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 643 | self.assertEqual(b.replace(b'i', b'a'), b'massassappa') |
| 644 | self.assertEqual(b.replace(b'ss', b'x'), b'mixixippi') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 645 | |
| 646 | def test_translate(self): |
| 647 | b = b'hello' |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 648 | rosetta = buffer(range(0, 256)) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 649 | rosetta[ord('o')] = ord('e') |
| 650 | c = b.translate(rosetta, b'l') |
| 651 | self.assertEqual(b, b'hello') |
| 652 | self.assertEqual(c, b'hee') |
| 653 | |
| 654 | def test_split(self): |
| 655 | b = b'mississippi' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 656 | self.assertEqual(b.split(b'i'), [b'm', b'ss', b'ss', b'pp', b'']) |
| 657 | self.assertEqual(b.split(b'ss'), [b'mi', b'i', b'ippi']) |
| 658 | self.assertEqual(b.split(b'w'), [b]) |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 659 | |
| 660 | def test_split_whitespace(self): |
| 661 | for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf', |
| 662 | b'arf\fbarf', b'arf\vbarf'): |
| 663 | self.assertEqual(b.split(), [b'arf', b'barf']) |
| 664 | self.assertEqual(b.split(None), [b'arf', b'barf']) |
| 665 | self.assertEqual(b.split(None, 2), [b'arf', b'barf']) |
| 666 | self.assertEqual(b' a bb c '.split(None, 0), [b'a bb c ']) |
| 667 | self.assertEqual(b' a bb c '.split(None, 1), [b'a', b'bb c ']) |
| 668 | self.assertEqual(b' a bb c '.split(None, 2), [b'a', b'bb', b'c ']) |
| 669 | self.assertEqual(b' a bb c '.split(None, 3), [b'a', b'bb', b'c']) |
| 670 | |
| 671 | def test_split_buffer(self): |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 672 | 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] | 673 | |
| 674 | def test_split_string_error(self): |
| 675 | self.assertRaises(TypeError, b'a b'.split, ' ') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 676 | |
| 677 | def test_rsplit(self): |
| 678 | b = b'mississippi' |
Guido van Rossum | 09549f4 | 2007-08-27 20:40:10 +0000 | [diff] [blame] | 679 | self.assertEqual(b.rsplit(b'i'), [b'm', b'ss', b'ss', b'pp', b'']) |
| 680 | self.assertEqual(b.rsplit(b'ss'), [b'mi', b'i', b'ippi']) |
| 681 | self.assertEqual(b.rsplit(b'w'), [b]) |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 682 | |
| 683 | def test_rsplit_whitespace(self): |
| 684 | for b in (b' arf barf ', b'arf\tbarf', b'arf\nbarf', b'arf\rbarf', |
| 685 | b'arf\fbarf', b'arf\vbarf'): |
| 686 | self.assertEqual(b.rsplit(), [b'arf', b'barf']) |
| 687 | self.assertEqual(b.rsplit(None), [b'arf', b'barf']) |
| 688 | self.assertEqual(b.rsplit(None, 2), [b'arf', b'barf']) |
| 689 | self.assertEqual(b' a bb c '.rsplit(None, 0), [b' a bb c']) |
| 690 | self.assertEqual(b' a bb c '.rsplit(None, 1), [b' a bb', b'c']) |
| 691 | self.assertEqual(b' a bb c '.rsplit(None,2), [b' a', b'bb', b'c']) |
| 692 | self.assertEqual(b' a bb c '.rsplit(None, 3), [b'a', b'bb', b'c']) |
| 693 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 694 | def test_rsplit_buffer(self): |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 695 | 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] | 696 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 697 | def test_rsplit_string_error(self): |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 698 | self.assertRaises(TypeError, b'a b'.rsplit, ' ') |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 699 | |
| 700 | def test_partition(self): |
| 701 | b = b'mississippi' |
| 702 | self.assertEqual(b.partition(b'ss'), (b'mi', b'ss', b'issippi')) |
| 703 | self.assertEqual(b.rpartition(b'w'), (b'', b'', b'mississippi')) |
| 704 | |
| 705 | def test_rpartition(self): |
| 706 | b = b'mississippi' |
| 707 | self.assertEqual(b.rpartition(b'ss'), (b'missi', b'ss', b'ippi')) |
| 708 | self.assertEqual(b.rpartition(b'i'), (b'mississipp', b'i', b'')) |
| 709 | |
Guido van Rossum | 0ad0812 | 2007-04-11 04:37:43 +0000 | [diff] [blame] | 710 | def test_pickling(self): |
Guido van Rossum | 99603b0 | 2007-07-20 00:22:32 +0000 | [diff] [blame] | 711 | for proto in range(pickle.HIGHEST_PROTOCOL): |
| 712 | for b in b"", b"a", b"abc", b"\xffab\x80", b"\0\0\377\0\0": |
| 713 | ps = pickle.dumps(b, proto) |
| 714 | q = pickle.loads(ps) |
| 715 | self.assertEqual(b, q) |
Guido van Rossum | 0ad0812 | 2007-04-11 04:37:43 +0000 | [diff] [blame] | 716 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 717 | def test_strip(self): |
| 718 | b = b'mississippi' |
| 719 | self.assertEqual(b.strip(b'i'), b'mississipp') |
| 720 | self.assertEqual(b.strip(b'm'), b'ississippi') |
| 721 | self.assertEqual(b.strip(b'pi'), b'mississ') |
| 722 | self.assertEqual(b.strip(b'im'), b'ssissipp') |
| 723 | self.assertEqual(b.strip(b'pim'), b'ssiss') |
Guido van Rossum | eb29e9a | 2007-08-08 21:55:33 +0000 | [diff] [blame] | 724 | self.assertEqual(b.strip(b), b'') |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 725 | |
| 726 | def test_lstrip(self): |
| 727 | b = b'mississippi' |
| 728 | self.assertEqual(b.lstrip(b'i'), b'mississippi') |
| 729 | self.assertEqual(b.lstrip(b'm'), b'ississippi') |
| 730 | self.assertEqual(b.lstrip(b'pi'), b'mississippi') |
| 731 | self.assertEqual(b.lstrip(b'im'), b'ssissippi') |
| 732 | self.assertEqual(b.lstrip(b'pim'), b'ssissippi') |
| 733 | |
| 734 | def test_rstrip(self): |
| 735 | b = b'mississippi' |
| 736 | self.assertEqual(b.rstrip(b'i'), b'mississipp') |
| 737 | self.assertEqual(b.rstrip(b'm'), b'mississippi') |
| 738 | self.assertEqual(b.rstrip(b'pi'), b'mississ') |
| 739 | self.assertEqual(b.rstrip(b'im'), b'mississipp') |
| 740 | self.assertEqual(b.rstrip(b'pim'), b'mississ') |
| 741 | |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 742 | def test_strip_whitespace(self): |
| 743 | b = b' \t\n\r\f\vabc \t\n\r\f\v' |
| 744 | self.assertEqual(b.strip(), b'abc') |
| 745 | self.assertEqual(b.lstrip(), b'abc \t\n\r\f\v') |
| 746 | self.assertEqual(b.rstrip(), b' \t\n\r\f\vabc') |
| 747 | |
| 748 | def test_strip_buffer(self): |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 749 | self.assertEqual(b'abc'.strip(memoryview(b'ac')), b'b') |
| 750 | self.assertEqual(b'abc'.lstrip(memoryview(b'ac')), b'bc') |
| 751 | self.assertEqual(b'abc'.rstrip(memoryview(b'ac')), b'ab') |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 752 | |
| 753 | def test_strip_string_error(self): |
| 754 | self.assertRaises(TypeError, b'abc'.strip, 'b') |
| 755 | self.assertRaises(TypeError, b'abc'.lstrip, 'b') |
| 756 | self.assertRaises(TypeError, b'abc'.rstrip, 'b') |
| 757 | |
Guido van Rossum | f9e91c9 | 2007-05-08 21:05:48 +0000 | [diff] [blame] | 758 | def test_ord(self): |
| 759 | b = b'\0A\x7f\x80\xff' |
| 760 | self.assertEqual([ord(b[i:i+1]) for i in range(len(b))], |
| 761 | [0, 65, 127, 128, 255]) |
| 762 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 763 | def test_partition_buffer_doesnt_share_nullstring(self): |
| 764 | a, b, c = buffer(b"x").partition(b"y") |
| 765 | self.assertEqual(b, b"") |
| 766 | self.assertEqual(c, b"") |
| 767 | self.assert_(b is not c) |
| 768 | b += b"!" |
| 769 | self.assertEqual(c, b"") |
| 770 | a, b, c = buffer(b"x").partition(b"y") |
| 771 | self.assertEqual(b, b"") |
| 772 | self.assertEqual(c, b"") |
| 773 | # Same for rpartition |
| 774 | b, c, a = buffer(b"x").rpartition(b"y") |
| 775 | self.assertEqual(b, b"") |
| 776 | self.assertEqual(c, b"") |
| 777 | self.assert_(b is not c) |
| 778 | b += b"!" |
| 779 | self.assertEqual(c, b"") |
| 780 | c, b, a = buffer(b"x").rpartition(b"y") |
| 781 | self.assertEqual(b, b"") |
| 782 | self.assertEqual(c, b"") |
| 783 | |
| 784 | |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 785 | # Optimizations: |
Guido van Rossum | e06b6b8 | 2006-04-23 07:43:54 +0000 | [diff] [blame] | 786 | # __iter__? (optimization) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 787 | # __reversed__? (optimization) |
| 788 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 789 | # XXX More string methods? (Those that don't use character properties) |
| 790 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 791 | # There are tests in string_tests.py that are more |
| 792 | # comprehensive for things like split, partition, etc. |
| 793 | # Unfortunately they are all bundled with tests that |
| 794 | # are not appropriate for bytes |
Guido van Rossum | e06b6b8 | 2006-04-23 07:43:54 +0000 | [diff] [blame] | 795 | |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 796 | # I've started porting some of those into buffer_tests.py, we should port |
| 797 | # the rest that make sense (the code can be cleaned up to use modern |
| 798 | # unittest methods at the same time). |
| 799 | |
| 800 | class BufferPEP3137Test(unittest.TestCase, |
| 801 | test.buffer_tests.MixinBytesBufferCommonTests): |
| 802 | def marshal(self, x): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 803 | return buffer(x) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 804 | # TODO this should become: |
| 805 | #return buffer(x) |
| 806 | # once the bytes -> buffer and str8 -> bytes rename happens |
| 807 | |
| 808 | def test_returns_new_copy(self): |
| 809 | val = self.marshal(b'1234') |
| 810 | # On immutable types these MAY return a reference to themselves |
| 811 | # but on mutable types like buffer they MUST return a new copy. |
| 812 | for methname in ('zfill', 'rjust', 'ljust', 'center'): |
| 813 | method = getattr(val, methname) |
| 814 | newval = method(3) |
| 815 | self.assertEqual(val, newval) |
| 816 | self.assertTrue(val is not newval, |
| 817 | methname+' returned self on a mutable object') |
| 818 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 819 | |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 820 | class BytesAsStringTest(test.string_tests.BaseTest): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 821 | type2test = buffer |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 822 | |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 823 | def fixtype(self, obj): |
| 824 | if isinstance(obj, str): |
| 825 | return obj.encode("utf-8") |
| 826 | return super().fixtype(obj) |
| 827 | |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 828 | # Currently the bytes containment testing uses a single integer |
| 829 | # value. This may not be the final design, but until then the |
| 830 | # bytes section with in a bytes containment not valid |
| 831 | def test_contains(self): |
| 832 | pass |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 833 | def test_expandtabs(self): |
| 834 | pass |
| 835 | def test_upper(self): |
| 836 | pass |
| 837 | def test_lower(self): |
| 838 | pass |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 839 | |
| 840 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 841 | class BufferSubclass(buffer): |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 842 | pass |
| 843 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 844 | class BufferSubclassTest(unittest.TestCase): |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 845 | |
| 846 | def test_basic(self): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 847 | self.assert_(issubclass(BufferSubclass, buffer)) |
| 848 | self.assert_(isinstance(BufferSubclass(), buffer)) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 849 | |
| 850 | a, b = b"abcd", b"efgh" |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 851 | _a, _b = BufferSubclass(a), BufferSubclass(b) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 852 | |
| 853 | # test comparison operators with subclass instances |
| 854 | self.assert_(_a == _a) |
| 855 | self.assert_(_a != _b) |
| 856 | self.assert_(_a < _b) |
| 857 | self.assert_(_a <= _b) |
| 858 | self.assert_(_b >= _a) |
| 859 | self.assert_(_b > _a) |
| 860 | self.assert_(_a is not a) |
| 861 | |
| 862 | # test concat of subclass instances |
| 863 | self.assertEqual(a + b, _a + _b) |
| 864 | self.assertEqual(a + b, a + _b) |
| 865 | self.assertEqual(a + b, _a + b) |
| 866 | |
| 867 | # test repeat |
| 868 | self.assert_(a*5 == _a*5) |
| 869 | |
| 870 | def test_join(self): |
| 871 | # Make sure join returns a NEW object for single item sequences |
| 872 | # involving a subclass. |
| 873 | # Make sure that it is of the appropriate type. |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 874 | s1 = BufferSubclass(b"abcd") |
| 875 | s2 = buffer().join([s1]) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 876 | self.assert_(s1 is not s2) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 877 | self.assert_(type(s2) is buffer, type(s2)) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 878 | |
| 879 | # Test reverse, calling join on subclass |
| 880 | s3 = s1.join([b"abcd"]) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 881 | self.assert_(type(s3) is buffer) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 882 | |
| 883 | def test_pickle(self): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 884 | a = BufferSubclass(b"abcd") |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 885 | a.x = 10 |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 886 | a.y = BufferSubclass(b"efgh") |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 887 | for proto in range(pickle.HIGHEST_PROTOCOL): |
| 888 | b = pickle.loads(pickle.dumps(a, proto)) |
| 889 | self.assertNotEqual(id(a), id(b)) |
| 890 | self.assertEqual(a, b) |
| 891 | self.assertEqual(a.x, b.x) |
| 892 | self.assertEqual(a.y, b.y) |
| 893 | self.assertEqual(type(a), type(b)) |
| 894 | self.assertEqual(type(a.y), type(b.y)) |
| 895 | |
| 896 | def test_copy(self): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 897 | a = BufferSubclass(b"abcd") |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 898 | a.x = 10 |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 899 | a.y = BufferSubclass(b"efgh") |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 900 | for copy_method in (copy.copy, copy.deepcopy): |
| 901 | b = copy_method(a) |
| 902 | self.assertNotEqual(id(a), id(b)) |
| 903 | self.assertEqual(a, b) |
| 904 | self.assertEqual(a.x, b.x) |
| 905 | self.assertEqual(a.y, b.y) |
| 906 | self.assertEqual(type(a), type(b)) |
| 907 | self.assertEqual(type(a.y), type(b.y)) |
| 908 | |
| 909 | def test_init_override(self): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 910 | class subclass(buffer): |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 911 | def __init__(self, newarg=1, *args, **kwargs): |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 912 | buffer.__init__(self, *args, **kwargs) |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 913 | x = subclass(4, source=b"abcd") |
| 914 | self.assertEqual(x, b"abcd") |
| 915 | x = subclass(newarg=4, source=b"abcd") |
| 916 | self.assertEqual(x, b"abcd") |
| 917 | |
| 918 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 919 | def test_main(): |
Thomas Wouters | 5f6f27d | 2006-04-23 00:19:58 +0000 | [diff] [blame] | 920 | test.test_support.run_unittest(BytesTest) |
Georg Brandl | c788554 | 2007-03-06 19:16:20 +0000 | [diff] [blame] | 921 | test.test_support.run_unittest(BytesAsStringTest) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame^] | 922 | test.test_support.run_unittest(BufferSubclassTest) |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 923 | test.test_support.run_unittest(BufferPEP3137Test) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 924 | |
| 925 | if __name__ == "__main__": |
Guido van Rossum | a6c04be | 2007-11-03 00:24:24 +0000 | [diff] [blame] | 926 | test_main() |