summaryrefslogtreecommitdiff
path: root/tests/common/packed.py
blob: 25918d26f5071f5e7905b148d3cafc765d0285a3 (plain)
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

# Tests pour valider les tampons de données


from chrysacase import ChrysalideTestCase
from pychrysalide.common import PackedBuffer


class TestPackedBuffers(ChrysalideTestCase):
    """TestCase for common.PackedBuffer*"""

    def testPackedBufferConstructor(self):
        """Validate new packed buffers."""

        pbuf = PackedBuffer()
        self.assertIsNotNone(pbuf)


    def testPackedBufferData(self):
        """Play with packed buffer data."""

        pbuf = PackedBuffer()

        data = b'0123456789'

        pbuf.extend(data, False)
        pbuf.extend(data)

        self.assertEqual(pbuf.payload_length, 2 * len(data))

        self.assertFalse(pbuf.more_data)

        pbuf.rewind()

        self.assertTrue(pbuf.more_data)

        got = pbuf.peek(1)
        self.assertEqual(got, b'0')

        got = pbuf.peek(2)
        self.assertEqual(got, b'01')

        pbuf.advance(3)

        got = pbuf.peek(2)
        self.assertEqual(got, b'34')

        pbuf.advance(8)

        got = pbuf.peek(2)
        self.assertEqual(got, b'12')

        pbuf.rewind()

        got = pbuf.extract(4)
        self.assertEqual(got, b'0123')

        got = pbuf.extract(8, True)
        self.assertEqual(got, b'10987654')