Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/tests/test_bitbuffer.py: 100%
61 statements
« prev ^ index » next coverage.py v6.5.0, created at 2024-12-05 10:43 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2024-12-05 10:43 +0000
1import unittest
3from zserio.bitbuffer import BitBuffer
4from zserio.exception import PythonRuntimeException
7class BitStreamReaderTest(unittest.TestCase):
9 def test_buffer_constructor(self):
10 bytesize = 2
11 bitbuffer = BitBuffer(bytes([1, 2]))
12 self.assertEqual(8 * bytesize, bitbuffer.bitsize)
14 empty_bitsize = 0
15 empty_bitbuffer = BitBuffer(bytes([]))
16 self.assertEqual(empty_bitsize, empty_bitbuffer.bitsize)
18 def test_buffer_bitsize_constructor(self):
19 bitsize = 11
20 bitbuffer = BitBuffer(bytes([0x01, 0xE0]), bitsize)
21 self.assertEqual(bitsize, bitbuffer.bitsize)
23 empty_bitsize = 0
24 empty_bitbuffer = BitBuffer(bytes([]), empty_bitsize)
25 self.assertEqual(empty_bitsize, empty_bitbuffer.bitsize)
27 out_of_range_bitsize = 9
28 with self.assertRaises(PythonRuntimeException):
29 BitBuffer(bytes([1]), out_of_range_bitsize) # throws!
31 def test_eq(self):
32 bitsize = 11
33 bitbuffer1 = BitBuffer(bytes([0xAB, 0xE0]), bitsize)
34 bitbuffer2 = BitBuffer(bytes([0xAB, 0xF0]), bitsize)
35 self.assertEqual(bitbuffer1, bitbuffer2)
37 bitbuffer3 = BitBuffer(bytes([0xAB, 0xFF]), bitsize)
38 self.assertEqual(bitbuffer1, bitbuffer3)
40 bitbuffer4 = BitBuffer(bytes([0xAB, 0xC0]), bitsize)
41 self.assertNotEqual(bitbuffer1, bitbuffer4)
43 bitbuffer5 = BitBuffer(bytes([0xBA, 0xE0]), bitsize)
44 self.assertNotEqual(bitbuffer1, bitbuffer5)
46 bitbuffer6 = BitBuffer(bytes([0xAB]))
47 self.assertNotEqual(bitbuffer1, bitbuffer6)
49 bitbuffer7 = BitBuffer(bytes())
50 self.assertNotEqual(bitbuffer1, bitbuffer7)
52 self.assertNotEqual(bitbuffer1, 1)
54 def test_hashcode(self):
55 bitsize = 11
56 bitbuffer1 = BitBuffer(bytes([0xAB, 0xE0]), bitsize)
57 bitbuffer2 = BitBuffer(bytes([0xAB, 0xF0]), bitsize)
58 self.assertEqual(hash(bitbuffer1), hash(bitbuffer2))
60 bitbuffer3 = BitBuffer(bytes([0xAB, 0xFF]), bitsize)
61 self.assertEqual(hash(bitbuffer1), hash(bitbuffer3))
63 bitbuffer4 = BitBuffer(bytes([0xAB, 0xC0]), bitsize)
64 self.assertNotEqual(hash(bitbuffer1), hash(bitbuffer4))
66 bitbuffer5 = BitBuffer(bytes([0xBA, 0xE0]), bitsize)
67 self.assertNotEqual(hash(bitbuffer1), hash(bitbuffer5))
69 bitbuffer6 = BitBuffer(bytes([0xAB]))
70 self.assertNotEqual(hash(bitbuffer1), hash(bitbuffer6))
72 bitbuffer7 = BitBuffer(bytes())
73 self.assertNotEqual(hash(bitbuffer1), hash(bitbuffer7))
75 def test_buffer(self):
76 bitsize = 11
77 buffer = bytes([0xAB, 0xE0])
78 bitbuffer = BitBuffer(buffer, bitsize)
79 self.assertEqual(buffer, bitbuffer.buffer)
81 def test_bitsize(self):
82 bitsize = 11
83 bitbuffer = BitBuffer(bytes([0xAB, 0xE0]), bitsize)
84 self.assertEqual(bitsize, bitbuffer.bitsize)