|
6 | 6 |
|
7 | 7 | def check(length, obj): |
8 | 8 | v = packb(obj) |
9 | | - assert len(v) == length, \ |
10 | | - "%r length should be %r but get %r" % (obj, length, len(v)) |
| 9 | + assert len(v) == length, "%r length should be %r but get %r" % (obj, length, len(v)) |
11 | 10 | assert unpackb(v, use_list=0) == obj |
12 | 11 |
|
| 12 | + |
13 | 13 | def test_1(): |
14 | | - for o in [None, True, False, 0, 1, (1 << 6), (1 << 7) - 1, -1, |
15 | | - -((1<<5)-1), -(1<<5)]: |
| 14 | + for o in [ |
| 15 | + None, |
| 16 | + True, |
| 17 | + False, |
| 18 | + 0, |
| 19 | + 1, |
| 20 | + (1 << 6), |
| 21 | + (1 << 7) - 1, |
| 22 | + -1, |
| 23 | + -((1 << 5) - 1), |
| 24 | + -(1 << 5), |
| 25 | + ]: |
16 | 26 | check(1, o) |
17 | 27 |
|
| 28 | + |
18 | 29 | def test_2(): |
19 | | - for o in [1 << 7, (1 << 8) - 1, |
20 | | - -((1<<5)+1), -(1<<7) |
21 | | - ]: |
| 30 | + for o in [1 << 7, (1 << 8) - 1, -((1 << 5) + 1), -(1 << 7)]: |
22 | 31 | check(2, o) |
23 | 32 |
|
| 33 | + |
24 | 34 | def test_3(): |
25 | | - for o in [1 << 8, (1 << 16) - 1, |
26 | | - -((1<<7)+1), -(1<<15)]: |
| 35 | + for o in [1 << 8, (1 << 16) - 1, -((1 << 7) + 1), -(1 << 15)]: |
27 | 36 | check(3, o) |
28 | 37 |
|
| 38 | + |
29 | 39 | def test_5(): |
30 | | - for o in [1 << 16, (1 << 32) - 1, |
31 | | - -((1<<15)+1), -(1<<31)]: |
| 40 | + for o in [1 << 16, (1 << 32) - 1, -((1 << 15) + 1), -(1 << 31)]: |
32 | 41 | check(5, o) |
33 | 42 |
|
| 43 | + |
34 | 44 | def test_9(): |
35 | | - for o in [1 << 32, (1 << 64) - 1, |
36 | | - -((1<<31)+1), -(1<<63), |
37 | | - 1.0, 0.1, -0.1, -1.0]: |
| 45 | + for o in [ |
| 46 | + 1 << 32, |
| 47 | + (1 << 64) - 1, |
| 48 | + -((1 << 31) + 1), |
| 49 | + -(1 << 63), |
| 50 | + 1.0, |
| 51 | + 0.1, |
| 52 | + -0.1, |
| 53 | + -1.0, |
| 54 | + ]: |
38 | 55 | check(9, o) |
39 | 56 |
|
40 | 57 |
|
41 | 58 | def check_raw(overhead, num): |
42 | 59 | check(num + overhead, b" " * num) |
43 | 60 |
|
| 61 | + |
44 | 62 | def test_fixraw(): |
45 | 63 | check_raw(1, 0) |
46 | | - check_raw(1, (1<<5) - 1) |
| 64 | + check_raw(1, (1 << 5) - 1) |
| 65 | + |
47 | 66 |
|
48 | 67 | def test_raw16(): |
49 | | - check_raw(3, 1<<5) |
50 | | - check_raw(3, (1<<16) - 1) |
| 68 | + check_raw(3, 1 << 5) |
| 69 | + check_raw(3, (1 << 16) - 1) |
| 70 | + |
51 | 71 |
|
52 | 72 | def test_raw32(): |
53 | | - check_raw(5, 1<<16) |
| 73 | + check_raw(5, 1 << 16) |
54 | 74 |
|
55 | 75 |
|
56 | 76 | def check_array(overhead, num): |
57 | 77 | check(num + overhead, (None,) * num) |
58 | 78 |
|
| 79 | + |
59 | 80 | def test_fixarray(): |
60 | 81 | check_array(1, 0) |
61 | 82 | check_array(1, (1 << 4) - 1) |
62 | 83 |
|
| 84 | + |
63 | 85 | def test_array16(): |
64 | 86 | check_array(3, 1 << 4) |
65 | | - check_array(3, (1<<16)-1) |
| 87 | + check_array(3, (1 << 16) - 1) |
| 88 | + |
66 | 89 |
|
67 | 90 | def test_array32(): |
68 | | - check_array(5, (1<<16)) |
| 91 | + check_array(5, (1 << 16)) |
69 | 92 |
|
70 | 93 |
|
71 | 94 | def match(obj, buf): |
72 | 95 | assert packb(obj) == buf |
73 | 96 | assert unpackb(buf, use_list=0) == obj |
74 | 97 |
|
| 98 | + |
75 | 99 | def test_match(): |
76 | 100 | cases = [ |
77 | | - (None, b'\xc0'), |
78 | | - (False, b'\xc2'), |
79 | | - (True, b'\xc3'), |
80 | | - (0, b'\x00'), |
81 | | - (127, b'\x7f'), |
82 | | - (128, b'\xcc\x80'), |
83 | | - (256, b'\xcd\x01\x00'), |
84 | | - (-1, b'\xff'), |
85 | | - (-33, b'\xd0\xdf'), |
86 | | - (-129, b'\xd1\xff\x7f'), |
87 | | - ({1:1}, b'\x81\x01\x01'), |
| 101 | + (None, b"\xc0"), |
| 102 | + (False, b"\xc2"), |
| 103 | + (True, b"\xc3"), |
| 104 | + (0, b"\x00"), |
| 105 | + (127, b"\x7f"), |
| 106 | + (128, b"\xcc\x80"), |
| 107 | + (256, b"\xcd\x01\x00"), |
| 108 | + (-1, b"\xff"), |
| 109 | + (-33, b"\xd0\xdf"), |
| 110 | + (-129, b"\xd1\xff\x7f"), |
| 111 | + ({1: 1}, b"\x81\x01\x01"), |
88 | 112 | (1.0, b"\xcb\x3f\xf0\x00\x00\x00\x00\x00\x00"), |
89 | | - ((), b'\x90'), |
90 | | - (tuple(range(15)),b"\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e"), |
91 | | - (tuple(range(16)),b"\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"), |
92 | | - ({}, b'\x80'), |
93 | | - (dict([(x,x) for x in range(15)]), b'\x8f\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e'), |
94 | | - (dict([(x,x) for x in range(16)]), b'\xde\x00\x10\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e\x0f\x0f'), |
95 | | - ] |
| 113 | + ((), b"\x90"), |
| 114 | + ( |
| 115 | + tuple(range(15)), |
| 116 | + b"\x9f\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e", |
| 117 | + ), |
| 118 | + ( |
| 119 | + tuple(range(16)), |
| 120 | + b"\xdc\x00\x10\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", |
| 121 | + ), |
| 122 | + ({}, b"\x80"), |
| 123 | + ( |
| 124 | + dict([(x, x) for x in range(15)]), |
| 125 | + b"\x8f\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e", |
| 126 | + ), |
| 127 | + ( |
| 128 | + dict([(x, x) for x in range(16)]), |
| 129 | + b"\xde\x00\x10\x00\x00\x01\x01\x02\x02\x03\x03\x04\x04\x05\x05\x06\x06\x07\x07\x08\x08\t\t\n\n\x0b\x0b\x0c\x0c\r\r\x0e\x0e\x0f\x0f", |
| 130 | + ), |
| 131 | + ] |
96 | 132 |
|
97 | 133 | for v, p in cases: |
98 | 134 | match(v, p) |
99 | 135 |
|
100 | | -def test_unicode(): |
101 | | - assert unpackb(packb('foobar'), use_list=1) == b'foobar' |
102 | 136 |
|
| 137 | +def test_unicode(): |
| 138 | + assert unpackb(packb("foobar"), use_list=1) == b"foobar" |
0 commit comments