Coverage for /home/runner/work/zserio/zserio/compiler/extensions/python/runtime/tests/test_bitposition.py: 100%

34 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2024-12-05 10:43 +0000

1import unittest 

2 

3from zserio.bitposition import ( 

4 alignto, 

5 bits_to_bytes, 

6 bytes_to_bits, 

7 bitsize_to_bytesize, 

8) 

9from zserio.exception import PythonRuntimeException 

10 

11 

12class BitPositionTest(unittest.TestCase): 

13 

14 def test_alignto(self): 

15 bitposition = 5 

16 self.assertEqual(5, alignto(0, bitposition)) 

17 self.assertEqual(5, alignto(1, bitposition)) 

18 self.assertEqual(6, alignto(2, bitposition)) 

19 self.assertEqual(6, alignto(3, bitposition)) 

20 self.assertEqual(8, alignto(4, bitposition)) 

21 self.assertEqual(5, alignto(5, bitposition)) 

22 self.assertEqual(6, alignto(6, bitposition)) 

23 self.assertEqual(7, alignto(7, bitposition)) 

24 self.assertEqual(8, alignto(8, bitposition)) 

25 

26 def test_bits_to_bytes(self): 

27 self.assertEqual(1, bits_to_bytes(8)) 

28 self.assertEqual(3, bits_to_bytes(24)) 

29 with self.assertRaises(PythonRuntimeException): 

30 bits_to_bytes(4) 

31 with self.assertRaises(PythonRuntimeException): 

32 bits_to_bytes(9) 

33 

34 def test_bytes_to_bits(self): 

35 self.assertEqual(0, bytes_to_bits(0)) 

36 self.assertEqual(8, bytes_to_bits(1)) 

37 self.assertEqual(16, bytes_to_bits(2)) 

38 

39 def test_bitsize_to_bytesize(self): 

40 self.assertEqual(0, bitsize_to_bytesize(0)) 

41 self.assertEqual(1, bitsize_to_bytesize(4)) 

42 self.assertEqual(1, bitsize_to_bytesize(8)) 

43 self.assertEqual(2, bitsize_to_bytesize(9)) 

44 self.assertEqual(2, bitsize_to_bytesize(16)) 

45 self.assertEqual(3, bitsize_to_bytesize(17)) 

46 self.assertEqual(3, bitsize_to_bytesize(24))