File: test_constbitstream.py

package info (click to toggle)
python-bitstring 4.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,312 kB
  • sloc: python: 11,397; makefile: 8; sh: 7
file content (274 lines) | stat: -rw-r--r-- 7,499 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python

import pytest
import sys
import bitstring
import io
import os
from bitstring import ConstBitStream as CBS
import platform

sys.path.insert(0, '..')


THIS_DIR = os.path.dirname(os.path.abspath(__file__))

class TestAll:
    def test_from_file(self):
        s = CBS(filename=os.path.join(THIS_DIR, 'test.m1v'))
        assert s[0:32].hex == '000001b3'
        assert s.read(8 * 4).hex == '000001b3'
        width = s.read(12).uint
        height = s.read(12).uint
        assert (width, height) == (352, 288)

    def test_from_file_with_offset_and_length(self):
        s = CBS(filename=os.path.join(THIS_DIR, 'test.m1v'), offset=24, length=8)
        assert s.h == 'b3'
        reconstructed = ''
        for bit in s:
            reconstructed += '1' if bit is True else '0'
        assert reconstructed == s.bin


class TestInterleavedExpGolomb:
    def test_reading(self):
        s = CBS(uie=333)
        a = s.read('uie')
        assert a == 333
        s = CBS('uie=12, sie=-9, sie=9, uie=1000000')
        u = s.unpack('uie, 2*sie, uie')
        assert u == [12, -9, 9, 1000000]

    def test_reading_errors(self):
        s = CBS(10)
        with pytest.raises(bitstring.ReadError):
            s.read('uie')
        assert s.pos == 0
        with pytest.raises(bitstring.ReadError):
            s.read('sie')
        assert s.pos == 0


class TestReadTo:
    def test_byte_aligned(self):
        a = CBS('0xaabb00aa00bb')
        b = a.readto('0x00', bytealigned=True)
        assert b == '0xaabb00'
        assert a.bytepos == 3
        b = a.readto('0xaa', bytealigned=True)
        assert b == '0xaa'
        with pytest.raises(bitstring.ReadError):
            b.readto('0xcc', bytealigned=True)

    def test_not_aligned(self):
        a = CBS('0b00111001001010011011')
        a.pos = 1
        assert a.readto('0b00') == '0b011100'
        assert a.readto('0b110') == '0b10010100110'
        with pytest.raises(ValueError):
            a.readto('')

    def test_disallow_integers(self):
        a = CBS('0x0f')
        with pytest.raises(ValueError):
            a.readto(4)

    def test_reading_lines(self):
        s = b"This is a test\nof reading lines\nof text\n"
        b = CBS(bytes=s)
        n = bitstring.Bits(bytes=b'\n')
        assert b.readto(n).bytes == b'This is a test\n'
        assert b.readto(n).bytes == b'of reading lines\n'
        assert b.readto(n).bytes == b'of text\n'


class TestSubclassing:

    def test_is_instance(self):
        class SubBits(CBS):
            pass
        a = SubBits()
        assert isinstance(a, SubBits)

    def test_class_type(self):
        class SubBits(CBS):
            pass
        assert SubBits().__class__ == SubBits


class TestPadToken:

    def test_read(self):
        s = CBS('0b100011110001')
        a = s.read('pad:1')
        assert a == None
        assert s.pos == 1
        a = s.read(3)
        assert a == CBS('0b000')
        a = s.read('pad:1')
        assert a == None
        assert s.pos == 5

    def test_read_list(self):
        s = CBS.fromstring('0b10001111001')
        t = s.readlist('pad:1, uint:3, pad:4, uint:3')
        assert t == [0, 1]
        s.pos = 0
        t = s.readlist('pad:1, pad:5')
        assert t == []
        assert s.pos == 6
        s.pos = 0
        t = s.readlist('pad:1, bin, pad:4, uint:3')
        assert t == ['000', 1]
        s.pos = 0
        t = s.readlist('pad, bin:3, pad:4, uint:3')
        assert t == ['000', 1]


class TestReadingBytes:

    def test_unpacking_bytes(self):
        s = CBS(80)
        t = s.unpack('bytes:1')
        assert t[0] == b'\x00'
        a, b, c = s.unpack('bytes:1, bytes, bytes2')
        assert a == b'\x00'
        assert b == b'\x00'*7
        assert c == b'\x00'*2

    def test_unpacking_bytes_with_keywords(self):
        s = CBS('0x55'*10)
        t = s.unpack('pad:a, bytes:b, bytes, pad:a', a=4, b=6)
        assert t == [b'\x55'*6, b'\x55'*3]


class TestReadingBitsAsDefault:

    def test_read_bits(self):
        s = CBS('uint:31=14')
        v = s.read(31)
        assert v.uint == 14
        s.pos = 0

    def test_read_list_bits(self):
        s = CBS('uint:5=3, uint:3=0, uint:11=999')
        v = s.readlist([5, 3, 11])
        assert [x.uint for x in v] == [3, 0, 999]
        s.pos = 0
        v = s.readlist(['5', '3', 11])
        assert [x.uint for x in v] == [3, 0, 999]


class TestLsb0Reading:

    @classmethod
    def setup_class(cls):
        bitstring.lsb0 = True

    @classmethod
    def teardown_class(cls):
        bitstring.lsb0 = False

    def test_reading_hex(self):
        s = CBS('0xabcdef')
        assert s.read('hex:4') == 'f'
        assert s.read(4) == '0xe'
        assert s.pos == 8

    def test_reading_oct(self):
        s = CBS('0o123456')
        assert s.read('o6') == '56'
        assert s.pos == 6

    def test_reading_bin(self):
        s = CBS('0b00011')
        assert s.read('bin:3') == '011'
        assert s.pos == 3

    def test_reading_bytes(self):
        s = CBS(bytes=b'54321')
        assert s.pos == 0
        s.pos = 8
        assert s.read('bytes:2') == b'32'


class TestBytesIOCreation:

    def test_simple_creation(self):
        f = io.BytesIO(b"\x12\xff\x77helloworld")
        s = CBS(f)
        assert s[0:8] == '0x12'
        assert len(s) == 13 * 8
        s = CBS(f, offset=8, length=12)
        assert s == '0xff7'

    def test_exceptions(self):
        f = io.BytesIO(b"123456789")
        _ = CBS(f, length=9*8)
        with pytest.raises(bitstring.CreationError):
            _ = CBS(f, length=9*8 + 1)
        with pytest.raises(bitstring.CreationError):
            _ = CBS(f, length=9*8, offset=1)


class TestCreationWithPos:

    def test_default_creation(self):
        s = CBS('0xabc')
        assert s.pos == 0

    def test_positive_pos(self):
        s = CBS('0xabc', pos=0)
        assert s.pos == 0
        s = CBS('0xabc', pos=1)
        assert s.pos == 1
        s = CBS('0xabc', pos=12)
        assert s.pos == 12
        with pytest.raises(bitstring.CreationError):
            _ = CBS('0xabc', pos=13)

    def test_negative_pos(self):
        s = CBS('0xabc', pos=-1)
        assert s.pos == 11
        s = CBS('0xabc', pos=-12)
        assert s.pos == 0
        with pytest.raises(bitstring.CreationError):
            _ = CBS('0xabc', pos=-13)

    def test_string_representation(self):
        s = CBS('0b110', pos=2)
        assert s.__repr__() == "ConstBitStream('0b110', pos=2)"

    def test_string_representation_from_file(self):
        filename = os.path.join(THIS_DIR, 'test.m1v')
        s = CBS(filename=filename, pos=2001)
        assert s.__repr__() == f"ConstBitStream(filename={repr(str(filename))}, length=1002400, pos=2001)"
        s.pos = 0
        assert s.__repr__() == f"ConstBitStream(filename={repr(str(filename))}, length=1002400)"


def test_windows_file_lock_bug():
    path = os.path.join(THIS_DIR, 'temp_unit_test_file')
    # Create the file
    with open(path, mode='w') as f:
        f.write('Hello')
    # Will this lock it?
    _ = CBS(filename=path)

    try:
        with open(path, mode='w') as _:
            pass
    except OSError:
        if platform.system() == 'Windows':
            # Expected failure. See bug #308
            pass

def test_readerrors():
    s = CBS('0b110')
    s.read(3)
    with pytest.raises(bitstring.ReadError):
        _ = s.read(1)
    s.pos = 1
    with pytest.raises(bitstring.ReadError):
        _ = s.read('u3')