File: run-bytes.test

package info (click to toggle)
mypy 1.19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,412 kB
  • sloc: python: 114,754; ansic: 13,343; cpp: 11,380; makefile: 257; sh: 28
file content (403 lines) | stat: -rw-r--r-- 11,627 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# Bytes test cases (compile and run)

[case testBytesBasics]
# Note: Add tests for additional operations to testBytesOps or in a new test case

def f(x: bytes) -> bytes:
    return x

def eq(a: bytes, b: bytes) -> bool:
    return a == b

def neq(a: bytes, b: bytes) -> bool:
    return a != b
[file driver.py]
from native import f, eq, neq
assert f(b'123') == b'123'
assert f(b'\x07 \x0b " \t \x7f \xf0') == b'\x07 \x0b " \t \x7f \xf0'
assert eq(b'123', b'123')
assert not eq(b'123', b'1234')
assert not eq(b'123', b'124')
assert not eq(b'123', b'223')
assert neq(b'123', b'1234')
try:
    f('x')
    assert False
except TypeError:
    pass

[case testBytesInit]
def test_bytes_init() -> None:
    b1 = bytes([5])
    assert b1 == b'\x05'
    b2 = bytes([5, 10, 12])
    assert b2 == b'\x05\n\x0c'
    b3 = bytes(bytearray(b'foo'))
    assert b3 == b'foo'
    b4 = bytes(b'aaa')
    assert b4 == b'aaa'
    b5 = bytes(5)
    assert b5 == b'\x00\x00\x00\x00\x00'
    try:
        bytes('x')
        assert False
    except TypeError:
        pass

[case testBytesOps]
from testutil import assertRaises

def test_indexing() -> None:
    # Use bytes() to avoid constant folding
    b = b'asdf' + bytes()
    assert b[0] == 97
    assert b[1] == 115
    assert b[3] == 102
    assert b[-1] == 102
    b = b'\xae\x80\xfe\x15' + bytes()
    assert b[0] == 174
    assert b[1] == 128
    assert b[2] == 254
    assert b[3] == 21
    assert b[-4] == 174
    with assertRaises(IndexError, "index out of range"):
        b[4]
    with assertRaises(IndexError, "index out of range"):
        b[-5]
    with assertRaises(IndexError, "index out of range"):
        b[2**26]

def test_concat() -> None:
    b1 = b'123' + bytes()
    b2 = b'456' + bytes()
    assert b1 + b2 == b'123456'
    b3 = b1 + b2
    b3 = b3 + b1
    assert b3 == b'123456123'
    assert b1 == b'123'
    assert b2 == b'456'
    assert type(b1) == bytes
    assert type(b2) == bytes
    assert type(b3) == bytes
    brr1: bytes = bytearray(3)
    brr2: bytes = bytearray(range(5))
    b4 = b1 + brr1
    assert b4 == b'123\x00\x00\x00'
    assert type(brr1) == bytearray
    assert type(b4) == bytes
    brr3 = brr1 + brr2
    assert brr3 == bytearray(b'\x00\x00\x00\x00\x01\x02\x03\x04')
    assert len(brr3) == 8
    assert type(brr3) == bytearray
    brr3 = brr3 + bytearray([10])
    assert brr3 == bytearray(b'\x00\x00\x00\x00\x01\x02\x03\x04\n')
    b5 = brr2 + b2
    assert b5 == bytearray(b'\x00\x01\x02\x03\x04456')
    assert type(b5) == bytearray
    b5 = b2 + brr2
    assert b5 == b'456\x00\x01\x02\x03\x04'
    assert type(b5) == bytes

def test_join() -> None:
    seq = (b'1', b'"', b'\xf0')
    assert b'\x07'.join(seq) == b'1\x07"\x07\xf0'
    assert b', '.join(()) == b''
    assert b', '.join([bytes() + b'ab']) == b'ab'
    assert b', '.join([bytes() + b'ab', b'cd']) == b'ab, cd'

def test_len() -> None:
    # Use bytes() to avoid constant folding
    b = b'foo' + bytes()
    assert len(b) == 3
    assert len(bytes()) == 0

def test_ord() -> None:
    assert ord(b'a') == ord('a')
    assert ord(b'a' + bytes()) == ord('a')
    assert ord(b'\x00') == 0
    assert ord(b'\x00' + bytes()) == 0
    assert ord(b'\xfe') == 254
    assert ord(b'\xfe' + bytes()) == 254

    with assertRaises(TypeError):
        ord(b'aa')
    with assertRaises(TypeError):
        ord(b'')

def test_ord_bytesarray() -> None:
    assert ord(bytearray(b'a')) == ord('a')
    assert ord(bytearray(b'\x00')) == 0
    assert ord(bytearray(b'\xfe')) == 254

    with assertRaises(TypeError):
        ord(bytearray(b'aa'))
    with assertRaises(TypeError):
        ord(bytearray(b''))

[case testBytesSlicing]
def test_bytes_slicing() -> None:
    b = b'abcdefg'
    zero = int()
    ten = 10 + zero
    two = 2 + zero
    five = 5 + zero
    seven = 7 + zero
    assert b[:ten] == b'abcdefg'
    assert b[0:seven] == b'abcdefg'
    assert b[0:(len(b)+1)] == b'abcdefg'
    assert b[two:five] == b'cde'
    assert b[two:two] == b''
    assert b[-two:-two] == b''
    assert b[-ten:(-ten+1)] == b''
    assert b[:-two] == b'abcde'
    assert b[:two] == b'ab'
    assert b[:] == b'abcdefg'
    assert b[-two:] == b'fg'
    assert b[zero:] == b'abcdefg'
    assert b[:zero] == b''
    assert b[-ten:] == b'abcdefg'
    assert b[-ten:ten] == b'abcdefg'
    big_ints = [1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, 2**24, 2**63]
    for big_int in big_ints:
        assert b[1:big_int] == b'bcdefg'
        assert b[big_int:] == b''
        assert b[-big_int:-1] == b'abcdef'
        assert b[-big_int:big_int] == b'abcdefg'
        assert type(b[-big_int:-1]) == bytes
    assert type(b[-ten:]) == bytes
    assert type(b[:]) == bytes

[case testBytearrayBasics]
from typing import Any

def test_basics() -> None:
    brr1: bytes = bytearray(3)
    assert brr1 == bytearray(b'\x00\x00\x00')
    assert brr1 == b'\x00\x00\x00'
    l = [10, 20, 30, 40]
    brr2: bytes = bytearray(l)
    assert brr2 == bytearray(b'\n\x14\x1e(')
    assert brr2 == b'\n\x14\x1e('
    brr3: bytes = bytearray(range(5))
    assert brr3 == bytearray(b'\x00\x01\x02\x03\x04')
    assert brr3 == b'\x00\x01\x02\x03\x04'
    brr4: bytes = bytearray('string', 'utf-8')
    assert brr4 == bytearray(b'string')
    assert brr4 == b'string'
    assert len(brr1) == 3
    assert len(brr2) == 4

def f(b: bytes) -> bool:
    return True

def test_bytearray_passed_into_bytes() -> None:
    assert f(bytearray(3))
    brr1: Any = bytearray()
    assert f(brr1)

[case testBytearraySlicing]
def test_bytearray_slicing() -> None:
    b: bytes = bytearray(b'abcdefg')
    zero = int()
    ten = 10 + zero
    two = 2 + zero
    five = 5 + zero
    seven = 7 + zero
    assert b[:ten] == b'abcdefg'
    assert b[0:seven] == b'abcdefg'
    assert b[two:five] == b'cde'
    assert b[two:two] == b''
    assert b[-two:-two] == b''
    assert b[-ten:(-ten+1)] == b''
    assert b[:-two] == b'abcde'
    assert b[:two] == b'ab'
    assert b[:] == b'abcdefg'
    assert b[-two:] == b'fg'
    assert b[zero:] == b'abcdefg'
    assert b[:zero] == b''
    assert b[-ten:] == b'abcdefg'
    assert b[-ten:ten] == b'abcdefg'
    big_ints = [1000 * 1000 * 1000 * 1000 * 1000 * 1000 * 1000, 2**24, 2**63]
    for big_int in big_ints:
        assert b[1:big_int] == b'bcdefg'
        assert b[big_int:] == b''
        assert b[-big_int:-1] == b'abcdef'
        assert b[-big_int:big_int] == b'abcdefg'
        assert type(b[-big_int:-1]) == bytearray
    assert type(b[-ten:]) == bytearray
    assert type(b[:]) == bytearray

[case testBytearrayIndexing]
from testutil import assertRaises

def test_bytearray_indexing() -> None:
    b: bytes = bytearray(b'\xae\x80\xfe\x15')
    assert b[0] == 174
    assert b[1] == 128
    assert b[2] == 254
    assert b[3] == 21
    assert b[-4] == 174
    with assertRaises(IndexError, "index out of range"):
        b[4]
    with assertRaises(IndexError, "index out of range"):
        b[-5]
    b2 = bytearray([175, 255, 128, 22])
    assert b2[0] == 175
    assert b2[1] == 255
    assert b2[-1] == 22
    assert b2[2] == 128
    with assertRaises(ValueError, "byte must be in range(0, 256)"):
        b2[0] = -1
    with assertRaises(ValueError, "byte must be in range(0, 256)"):
        b2[0] = 256

[case testBytesJoin]
from typing import Any
from testutil import assertRaises
from a import bytes_subclass

def test_bytes_join() -> None:
    assert b' '.join([b'a', b'b']) == b'a b'
    assert b' '.join([]) == b''

    x: bytes = bytearray(b' ')
    assert x.join([b'a', b'b']) == b'a b'
    assert type(x.join([b'a', b'b'])) == bytearray

    y: bytes = bytes_subclass()
    assert y.join([]) == b'spook'

    n: Any = 5
    with assertRaises(TypeError, "can only join an iterable"):
        assert b' '.join(n)

[file a.py]
class bytes_subclass(bytes):
    def join(self, iter):
        return b'spook'

[case testBytesFormatting]
from testutil import assertRaises

# https://www.python.org/dev/peps/pep-0461/
def test_bytes_formatting() -> None:
    val = 10
    assert b"%x" % val == b'a'
    assert b'%4x' % val == b'   a'
    assert b'%#4x' % val == b' 0xa'
    assert b'%04X' % val == b'000A'

    assert b'%c' % 48 == b'0'
    assert b'%c' % b'a' == b'a'
    assert b'%c%c' % (48, b'a') == b'0a'

    assert b'%b' % b'abc' == b'abc'
    assert b'%b' % 'some string'.encode('utf8') == b'some string'

    assert b'%a' % 3.14 == b'3.14'
    assert b'%a' % b'abc' == b"b'abc'"
    assert b'%a' % 'def' == b"'def'"

def test_bytes_formatting_2() -> None:
    var = b'bb'
    num = 10
    assert b'aaa%bbbb%s' % (var, var) == b'aaabbbbbbb'
    assert b'aaa%dbbb%b' % (num, var) == b'aaa10bbbbb'
    assert b'%s%b' % (var, var) == b'bbbb'
    assert b'%b' % bytes() == b''
    assert b'%b' % b'' == b''

    assert b'\xff%s' % b'\xff' == b'\xff\xff'
    assert b'\xff%b' % '你好'.encode() == b'\xff\xe4\xbd\xa0\xe5\xa5\xbd'

    aa = b'\xe4\xbd\xa0\xe5\xa5\xbd%b' % b'\xe4\xbd\xa0\xe5\xa5\xbd'
    assert aa == b'\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd'
    assert aa.decode() == '你好你好'
[typing fixtures/typing-full.pyi]


class A:
    def __bytes__(self):
        return b'aaa'

def test_bytes_dunder() -> None:
    assert b'%b' % A() == b'aaa'
    assert b'%s' % A() == b'aaa'

[case testIsInstance]
from copysubclass import subbytes, subbytearray
from typing import Any
def test_bytes() -> None:
    b: Any = b''
    assert isinstance(b, bytes)
    assert isinstance(b + b'123', bytes)
    assert isinstance(b + b'\xff', bytes)
    assert isinstance(subbytes(), bytes)
    assert isinstance(subbytes(b + b'123'), bytes)
    assert isinstance(subbytes(b + b'\xff'), bytes)

    assert not isinstance(set(), bytes)
    assert not isinstance((), bytes)
    assert not isinstance((b'1',b'2',b'3'), bytes)
    assert not isinstance({b'a',b'b'}, bytes)
    assert not isinstance(int() + 1, bytes)
    assert not isinstance(str() + 'a', bytes)

def test_user_defined_bytes() -> None:
    from userdefinedbytes import bytes

    assert isinstance(bytes(), bytes)
    assert not isinstance(b'\x7f', bytes)

def test_bytearray() -> None:
    assert isinstance(bytearray(), bytearray)
    assert isinstance(bytearray(b'123'), bytearray)
    assert isinstance(bytearray(b'\xff'), bytearray)
    assert isinstance(subbytearray(), bytearray)
    assert isinstance(subbytearray(bytearray(b'123')), bytearray)
    assert isinstance(subbytearray(bytearray(b'\xff')), bytearray)

    assert not isinstance(set(), bytearray)
    assert not isinstance((), bytearray)
    assert not isinstance((bytearray(b'1'),bytearray(b'2'),bytearray(b'3')), bytearray)
    assert not isinstance([bytearray(b'a'),bytearray(b'b')], bytearray)
    assert not isinstance(int() + 1, bytearray)
    assert not isinstance(str() + 'a', bytearray)

[file copysubclass.py]
class subbytes(bytes):
    pass

class subbytearray(bytearray):
    pass

[file userdefinedbytes.py]
class bytes:
    pass

[case testBytesOptionalEquality]
from __future__ import annotations

def eq_b_opt_b(x: bytes | None, y: bytes) -> bool:
    return x == y

def ne_b_b_opt(x: bytes, y: bytes | None) -> bool:
    return x != y

def test_optional_eq() -> None:
    b = b'x'
    assert eq_b_opt_b(b, b)
    assert eq_b_opt_b(b + bytes([int()]), b + bytes([int()]))

    assert not eq_b_opt_b(b'x', b'y')
    assert not eq_b_opt_b(b'y', b'x')
    assert not eq_b_opt_b(None, b'x')

def test_optional_ne() -> None:
    b = b'x'
    assert not ne_b_b_opt(b, b)
    assert not ne_b_b_opt(b + b'y', b + bytes() + b'y')

    assert ne_b_b_opt(b'x', b'y')
    assert ne_b_b_opt(b'y', b'x')
    assert ne_b_b_opt(b'x', None)