Debian Codegraph @

mypyc/test-data/run-bytes.test

raw
1 # Bytes test cases (compile and run)
2
3 [case testBytesBasics]
4 # Note: Add tests for additional operations to testBytesOps or in a new test case
5
6 def f(x: bytes) -> bytes:
7 return x
8
9 def eq(a: bytes, b: bytes) -> bool:
10 return a == b
11
12 def neq(a: bytes, b: bytes) -> bool:
13 return a != b
14 [file driver.py]
15 from native import f, eq, neq
16 assert f(b'123') == b'123'
17 assert f(b'\x07 \x0b " \t \x7f \xf0') == b'\x07 \x0b " \t \x7f \xf0'
18 assert eq(b'123', b'123')
19 assert not eq(b'123', b'1234')
20 assert not eq(b'123', b'124')
21 assert not eq(b'123', b'223')
22 assert neq(b'123', b'1234')
23 try:
24 f('x')
25 assert False
26 except TypeError:
27 pass
28
29 [case testBytesInit]
30 def test_bytes_init() -> None:
31 b1 = bytes([5])
32 assert b1 == b'\x05'
33 b2 = bytes([5, 10, 12])
34 assert b2 == b'\x05\n\x0c'
35 b3 = bytes(bytearray(b'foo'))
36 assert b3 == b'foo'
37 b4 = bytes(b'aaa')
38 assert b4 == b'aaa'
39 b5 = bytes(5)
40 assert b5 == b'\x00\x00\x00\x00\x00'
41 try:
42 bytes('x')
43 assert False
44 except TypeError:
45 pass
46
47 [case testBytesOps]
48 from testutil import assertRaises
49
50 def test_indexing() -> None:
51 # Use bytes() to avoid constant folding
52 b = b'asdf' + bytes()
53 assert b[0] == 97
54 assert b[1] == 115
55 assert b[3] == 102
56 assert b[-1] == 102
57 b = b'\xae\x80\xfe\x15' + bytes()
58 assert b[0] == 174
59 assert b[1] == 128
60 assert b[2] == 254
61 assert b[3] == 21
62 assert b[-4] == 174
63 with assertRaises(IndexError, "index out of range"):
64 b[4]
65 with assertRaises(IndexError, "index out of range"):
66 b[-5]
67 with assertRaises(IndexError, "index out of range"):
68 b[2**26]
69
70 def test_concat() -> None:
71 b1 = b'123' + bytes()
72 b2 = b'456' + bytes()
73 assert b1 + b2 == b'123456'
74 b3 = b1 + b2
75 b3 = b3 + b1
76 assert b3 == b'123456123'
77 assert b1 == b'123'
78 assert b2 == b'456'
79 assert type(b1) == bytes
80 assert type(b2) == bytes
81 assert type(b3) == bytes
82 brr1 = bytearray(3)
83 brr2 = bytearray(range(5))
84 b4 = b1 + brr1
85 assert b4 == b'123\x00\x00\x00'
86 assert type(brr1) == bytearray
87 assert type(b4) == bytes
88 brr3 = brr1 + brr2
89 assert brr3 == bytearray(b'\x00\x00\x00\x00\x01\x02\x03\x04')
90 assert len(brr3) == 8
91 assert type(brr3) == bytearray
92 brr3 = brr3 + bytearray([10])
93 assert brr3 == bytearray(b'\x00\x00\x00\x00\x01\x02\x03\x04\n')
94 b5 = brr2 + b2
95 assert b5 == bytearray(b'\x00\x01\x02\x03\x04456')
96 assert type(b5) == bytearray
97 b6 = b2 + brr2
98 assert b6 == b'456\x00\x01\x02\x03\x04'
99 assert type(b6) == bytes
100
101 def test_join() -> None:
102 seq = (b'1', b'"', b'\xf0')
103 assert b'\x07'.join(seq) == b'1\x07"\x07\xf0'
104 assert b', '.join(()) == b''
105 assert b', '.join([bytes() + b'ab']) == b'ab'
106 assert b', '.join([bytes() + b'ab', b'cd']) == b'ab, cd'
107
108 def test_len() -> None:
109 # Use bytes() to avoid constant folding
110 b = b'foo' + bytes()
111 assert len(b) == 3
112 assert len(bytes()) == 0
113
114 def test_ord() -> None:
115 assert ord(b'a') == ord('a')
116 assert ord(b'a' + bytes()) == ord('a')
117 assert ord(b'\x00') == 0
118 assert ord(b'\x00' + bytes()) == 0
119 assert ord(b'\xfe') == 254
120 assert ord(b'\xfe' + bytes()) == 254
121
122 with assertRaises(TypeError):
123 ord(b'aa')
124 with assertRaises(TypeError):
125 ord(b'')
126
127 def test_ord_bytesarray() -> None:
128 assert ord(bytearray(b'a')) == ord('a')
129 assert ord(bytearray(b'\x00')) == 0
130 assert ord(bytearray(b'\xfe')) == 254
131
132 with assertRaises(TypeError):
133 ord(bytearray(b'aa'))
134 with assertRaises(TypeError):
135 ord(bytearray(b''))
136
137 def test_multiply() -> None:
138 # Use bytes() and int() to avoid constant folding
139 b = b'ab' + bytes()
140 zero = int()
141 one = 1 + zero
142 three = 3 + zero
143 neg_one = -1 + zero
144
145 assert b * zero == b''
146 assert b * one == b'ab'
147 assert b * three == b'ababab'
148 assert b * neg_one == b''
149 assert zero * b == b''
150 assert one * b == b'ab'
151 assert three * b == b'ababab'
152 assert neg_one * b == b''
153
154 # Test with empty bytes
155 empty = bytes()
156 five = 5 + zero
157 assert empty * five == b''
158 assert five * empty == b''
159
160 # Test with single byte
161 single = b'\xff' + bytes()
162 four = 4 + zero
163 assert single * four == b'\xff\xff\xff\xff'
164 assert four * single == b'\xff\xff\xff\xff'
165
166 # Test type preservation
167 two = 2 + zero
168 result = b * two
169 assert type(result) == bytes
170
171 def test_translate() -> None:
172 # Identity translation table (fast path - exact bytes)
173 identity = bytes(range(256))
174 b = b'hello world' + bytes()
175 assert b.translate(identity) == b'hello world'
176
177 # ROT13-like translation for lowercase letters
178 table = bytearray(range(256))
179 for i in range(ord('a'), ord('z') + 1):
180 table[i] = ord('a') + (i - ord('a') + 13) % 26
181 table_bytes = bytes(table)
182 assert b'hello'.translate(table_bytes) == b'uryyb'
183 assert (b'abc' + bytes()).translate(table_bytes) == b'nop'
184
185 # Test with special characters
186 assert b'\x00\x01\xff'.translate(identity) == b'\x00\x01\xff'
187
188 # Test with bytearray table (slow path - fallback to Python method)
189 bytearray_table = bytearray(range(256))
190 assert b'hello'.translate(bytearray_table) == b'hello'
191 # Modify bytearray table to uppercase
192 for i in range(ord('a'), ord('z') + 1):
193 bytearray_table[i] = ord('A') + (i - ord('a'))
194 assert b'hello world'.translate(bytearray_table) == b'HELLO WORLD'
195 assert (b'test' + bytes()).translate(bytearray_table) == b'TEST'
196
197 # Test error on wrong table size
198 with assertRaises(ValueError, "translation table must be 256 characters long"):
199 b'test'.translate(b'short')
200 with assertRaises(ValueError, "translation table must be 256 characters long"):
201 b'test'.translate(bytes(100))
202
203 def test_startswith() -> None:
204 # Test default behavior
205 test = b'some string'
206 assert test.startswith(b'some')
207 assert test.startswith(b'some string')
208 assert not test.startswith(b'other')
209 assert not test.startswith(b'some string but longer')
210
211 # Test empty cases
212 assert test.startswith(b'')
213 assert b''.startswith(b'')
214 assert not b''.startswith(test)
215
216 # Test bytearray to verify slow paths
217 assert test.startswith(bytearray(b'some'))
218 assert not test.startswith(bytearray(b'other'))
219
220 test2 = bytearray(b'some string')
221 assert test2.startswith(b'some')
222 assert not test2.startswith(b'other')
223
224 def test_endswith() -> None:
225 # Test default behavior
226 test = b'some string'
227 assert test.endswith(b'string')
228 assert test.endswith(b'some string')
229 assert not test.endswith(b'other')
230 assert not test.endswith(b'some string but longer')
231
232 # Test empty cases
233 assert test.endswith(b'')
234 assert b''.endswith(b'')
235 assert not b''.endswith(test)
236
237 # Test bytearray to verify slow paths
238 assert test.endswith(bytearray(b'string'))
239 assert not test.endswith(bytearray(b'other'))
240
241 test2 = bytearray(b'some string')
242 assert test2.endswith(b'string')
243 assert not test2.endswith(b'other')
244
245 [case testBytesSlicing]
246 def test_bytes_slicing() -> None:
247 b = b'abcdefg'
248 zero = int()
249 ten = 10 + zero
250 two = 2 + zero
251 five = 5 + zero
252 seven = 7 + zero
253 assert b[:ten] == b'abcdefg'
254 assert b[0:seven] == b'abcdefg'
255 assert b[0:(len(b)+1)] == b'abcdefg'
256 assert b[two:five] == b'cde'
257 assert b[two:two] == b''
258 assert b[-two:-two] == b''
259 assert b[-ten:(-ten+1)] == b''
260 assert b[:-two] == b'abcde'
261 assert b[:two] == b'ab'
262 assert b[:] == b'abcdefg'
263 assert b[-two:] == b'fg'
264 assert b[zero:] == b'abcdefg'
265 assert b[:zero] == b''
266 assert b[-ten:] == b'abcdefg'
267 assert b[-ten:ten] == b'abcdefg'
268 big_ints = [1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, 2**24, 2**63]
269 for big_int in big_ints:
270 assert b[1:big_int] == b'bcdefg'
271 assert b[big_int:] == b''
272 assert b[-big_int:-1] == b'abcdef'
273 assert b[-big_int:big_int] == b'abcdefg'
274 assert type(b[-big_int:-1]) == bytes
275 assert type(b[-ten:]) == bytes
276 assert type(b[:]) == bytes
277
278 [case testBytearrayBasics]
279 from typing import Any
280
281 from testutil import assertRaises
282
283 def test_basics() -> None:
284 brr0 = bytearray()
285 assert brr0 == bytearray(b'')
286 assert brr0 == b''
287 assert len(brr0) == 0
288 brr1 = bytearray(3)
289 assert brr1 == bytearray(b'\x00\x00\x00')
290 assert brr1 == b'\x00\x00\x00'
291 l = [10, 20, 30, 40]
292 brr2 = bytearray(l)
293 assert brr2 == bytearray(b'\n\x14\x1e(')
294 assert brr2 == b'\n\x14\x1e('
295 brr3 = bytearray(range(5))
296 assert brr3 == bytearray(b'\x00\x01\x02\x03\x04')
297 assert brr3 == b'\x00\x01\x02\x03\x04'
298 brr4 = bytearray('string', 'utf-8')
299 assert brr4 == bytearray(b'string')
300 assert brr4 == b'string'
301 assert len(brr1) == 3
302 assert len(brr2) == 4
303
304 def test_bytearray_type_object() -> None:
305 b = bytearray()
306 assert type(b) is bytearray
307
308 def fb(b: bytes) -> str:
309 return "xy"
310
311 def test_bytearray_passed_into_bytes() -> None:
312 brr1: Any = bytearray()
313 with assertRaises(TypeError, "bytes object expected; got bytearray"):
314 fb(brr1)
315 with assertRaises(TypeError, "bytes object expected; got bytearray"):
316 b: bytes = brr1
317
318 def fba(brr: bytearray) -> str:
319 return "xy"
320
321 def test_bytes_passed_into_bytearray() -> None:
322 b: Any = b"foo"
323 with assertRaises(TypeError, "bytearray object expected; got bytes"):
324 fba(b)
325 with assertRaises(TypeError, "bytearray object expected; got bytes"):
326 brr: bytearray = b
327
328 [case testBytearraySlicing]
329 def test_bytearray_slicing() -> None:
330 b = bytearray(b'abcdefg')
331 zero = int()
332 ten = 10 + zero
333 two = 2 + zero
334 five = 5 + zero
335 seven = 7 + zero
336 assert b[:ten] == b'abcdefg'
337 assert b[0:seven] == b'abcdefg'
338 assert b[two:five] == b'cde'
339 assert b[two:two] == b''
340 assert b[-two:-two] == b''
341 assert b[-ten:(-ten+1)] == b''
342 assert b[:-two] == b'abcde'
343 assert b[:two] == b'ab'
344 assert b[:] == b'abcdefg'
345 assert b[-two:] == b'fg'
346 assert b[zero:] == b'abcdefg'
347 assert b[:zero] == b''
348 assert b[-ten:] == b'abcdefg'
349 assert b[-ten:ten] == b'abcdefg'
350 big_ints = [1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, 2**24, 2**63]
351 for big_int in big_ints:
352 assert b[1:big_int] == b'bcdefg'
353 assert b[big_int:] == b''
354 assert b[-big_int:-1] == b'abcdef'
355 assert b[-big_int:big_int] == b'abcdefg'
356 assert type(b[-big_int:-1]) == bytearray
357 assert type(b[-ten:]) == bytearray
358 assert type(b[:]) == bytearray
359
360 [case testBytearrayIndexing]
361 from testutil import assertRaises
362
363 def test_bytearray_indexing() -> None:
364 b = bytearray(b'\xae\x80\xfe\x15')
365 assert b[0] == 174
366 assert b[1] == 128
367 assert b[2] == 254
368 assert b[3] == 21
369 assert b[-4] == 174
370 with assertRaises(IndexError, "index out of range"):
371 b[4]
372 with assertRaises(IndexError, "index out of range"):
373 b[-5]
374 b2 = bytearray([175, 255, 128, 22])
375 assert b2[0] == 175
376 assert b2[1] == 255
377 assert b2[-1] == 22
378 assert b2[2] == 128
379 with assertRaises(ValueError, "byte must be in range(0, 256)"):
380 b2[0] = -1
381 with assertRaises(ValueError, "byte must be in range(0, 256)"):
382 b2[0] = 256
383
384 [case testBytesJoin]
385 from typing import Any
386 from testutil import assertRaises
387 from a import bytes_subclass
388
389 def test_bytes_join() -> None:
390 assert b' '.join([b'a', b'b']) == b'a b'
391 assert b' '.join([]) == b''
392
393 y: bytes = bytes_subclass()
394 assert y.join([]) == b'spook'
395
396 n: Any = 5
397 with assertRaises(TypeError, "can only join an iterable"):
398 assert b' '.join(n)
399
400 [file a.py]
401 class bytes_subclass(bytes):
402 def join(self, iter):
403 return b'spook'
404
405 [case testBytesFormatting]
406 from testutil import assertRaises
407
408 # https://www.python.org/dev/peps/pep-0461/
409 def test_bytes_formatting() -> None:
410 val = 10
411 assert b"%x" % val == b'a'
412 assert b'%4x' % val == b' a'
413 assert b'%#4x' % val == b' 0xa'
414 assert b'%04X' % val == b'000A'
415 assert b'%d' % val == b'10'
416 assert b'%d' % (val + 2) == b'12'
417 assert b'%d' % 0 == b'0'
418 assert b'%d' % -3 == b'-3'
419 assert b'folded: %d' % (4 - 2) == b'folded: 2'
420
421 large_num = 2**65
422 assert b'number: %d' % large_num == b'number: 36893488147419103232'
423 assert b'negative integer: %d' % (-large_num) == b'negative integer: -36893488147419103232'
424
425 assert b'%c' % 48 == b'0'
426 assert b'%c' % b'a' == b'a'
427 assert b'%c%c' % (48, b'a') == b'0a'
428
429 assert b'%b' % b'abc' == b'abc'
430 assert b'%b' % 'some string'.encode('utf8') == b'some string'
431
432 assert b'%a' % 3.14 == b'3.14'
433 assert b'%a' % b'abc' == b"b'abc'"
434 assert b'%a' % 'def' == b"'def'"
435
436 def test_bytes_formatting_2() -> None:
437 var = b'bb'
438 num = 10
439 assert b'aaa%bbbb%s' % (var, var) == b'aaabbbbbbb'
440 assert b'aaa%dbbb%b' % (num, var) == b'aaa10bbbbb'
441 assert b'%s%b' % (var, var) == b'bbbb'
442 assert b'%b' % bytes() == b''
443 assert b'%b' % b'' == b''
444
445 assert b'\xff%s' % b'\xff' == b'\xff\xff'
446 assert b'\xff%b' % '你好'.encode() == b'\xff\xe4\xbd\xa0\xe5\xa5\xbd'
447
448 aa = b'\xe4\xbd\xa0\xe5\xa5\xbd%b' % b'\xe4\xbd\xa0\xe5\xa5\xbd'
449 assert aa == b'\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd'
450 assert aa.decode() == '你好你好'
451 [typing fixtures/typing-full.pyi]
452
453
454 class A:
455 def __bytes__(self):
456 return b'aaa'
457
458 def test_bytes_dunder() -> None:
459 assert b'%b' % A() == b'aaa'
460 assert b'%s' % A() == b'aaa'
461
462 [case testIsInstance]
463 from copysubclass import subbytes, subbytearray
464 from typing import Any
465 def test_bytes() -> None:
466 b: Any = b''
467 assert isinstance(b, bytes)
468 assert isinstance(b + b'123', bytes)
469 assert isinstance(b + b'\xff', bytes)
470 assert isinstance(subbytes(), bytes)
471 assert isinstance(subbytes(b + b'123'), bytes)
472 assert isinstance(subbytes(b + b'\xff'), bytes)
473
474 assert not isinstance(set(), bytes)
475 assert not isinstance((), bytes)
476 assert not isinstance((b'1',b'2',b'3'), bytes)
477 assert not isinstance({b'a',b'b'}, bytes)
478 assert not isinstance(int() + 1, bytes)
479 assert not isinstance(str() + 'a', bytes)
480
481 def test_user_defined_bytes() -> None:
482 from userdefinedbytes import bytes
483
484 assert isinstance(bytes(), bytes)
485 assert not isinstance(b'\x7f', bytes)
486
487 def test_bytearray() -> None:
488 assert isinstance(bytearray(), bytearray)
489 assert isinstance(bytearray(b'123'), bytearray)
490 assert isinstance(bytearray(b'\xff'), bytearray)
491 assert isinstance(subbytearray(), bytearray)
492 assert isinstance(subbytearray(bytearray(b'123')), bytearray)
493 assert isinstance(subbytearray(bytearray(b'\xff')), bytearray)
494
495 assert not isinstance(set(), bytearray)
496 assert not isinstance((), bytearray)
497 assert not isinstance((bytearray(b'1'),bytearray(b'2'),bytearray(b'3')), bytearray)
498 assert not isinstance([bytearray(b'a'),bytearray(b'b')], bytearray)
499 assert not isinstance(int() + 1, bytearray)
500 assert not isinstance(str() + 'a', bytearray)
501
502 [file copysubclass.py]
503 class subbytes(bytes):
504 pass
505
506 class subbytearray(bytearray):
507 pass
508
509 [file userdefinedbytes.py]
510 class bytes:
511 pass
512
513 [case testBytesOptionalEquality]
514 from __future__ import annotations
515
516 def eq_b_opt_b(x: bytes | None, y: bytes) -> bool:
517 return x == y
518
519 def ne_b_b_opt(x: bytes, y: bytes | None) -> bool:
520 return x != y
521
522 def test_optional_eq() -> None:
523 b = b'x'
524 assert eq_b_opt_b(b, b)
525 assert eq_b_opt_b(b + bytes([int()]), b + bytes([int()]))
526
527 assert not eq_b_opt_b(b'x', b'y')
528 assert not eq_b_opt_b(b'y', b'x')
529 assert not eq_b_opt_b(None, b'x')
530
531 def test_optional_ne() -> None:
532 b = b'x'
533 assert not ne_b_b_opt(b, b)
534 assert not ne_b_b_opt(b + b'y', b + bytes() + b'y')
535
536 assert ne_b_b_opt(b'x', b'y')
537 assert ne_b_b_opt(b'y', b'x')
538 assert ne_b_b_opt(b'x', None)