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

48 statements  

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

1import unittest 

2 

3from zserio.bitfield import ( 

4 bitfield_lowerbound, 

5 bitfield_upperbound, 

6 signed_bitfield_lowerbound, 

7 signed_bitfield_upperbound, 

8) 

9from zserio.exception import PythonRuntimeException 

10 

11 

12class BitFieldTest(unittest.TestCase): 

13 

14 def test_bitfield_lowerbound(self): 

15 with self.assertRaises(PythonRuntimeException): 

16 bitfield_lowerbound(0) 

17 with self.assertRaises(PythonRuntimeException): 

18 bitfield_lowerbound(65) 

19 

20 self.assertEqual(0, bitfield_lowerbound(1)) 

21 self.assertEqual(0, bitfield_lowerbound(2)) 

22 self.assertEqual(0, bitfield_lowerbound(8)) 

23 self.assertEqual(0, bitfield_lowerbound(16)) 

24 self.assertEqual(0, bitfield_lowerbound(32)) 

25 self.assertEqual(0, bitfield_lowerbound(64)) 

26 

27 def test_bitfield_upperbound(self): 

28 with self.assertRaises(PythonRuntimeException): 

29 bitfield_upperbound(0) 

30 with self.assertRaises(PythonRuntimeException): 

31 bitfield_upperbound(65) 

32 

33 self.assertEqual(1, bitfield_upperbound(1)) 

34 self.assertEqual(3, bitfield_upperbound(2)) 

35 self.assertEqual(255, bitfield_upperbound(8)) 

36 self.assertEqual(65535, bitfield_upperbound(16)) 

37 self.assertEqual(4294967295, bitfield_upperbound(32)) 

38 self.assertEqual(0xFFFFFFFFFFFFFFFF, bitfield_upperbound(64)) 

39 

40 def test_signed_bitfield_lowerbound(self): 

41 with self.assertRaises(PythonRuntimeException): 

42 signed_bitfield_lowerbound(0) 

43 with self.assertRaises(PythonRuntimeException): 

44 signed_bitfield_lowerbound(65) 

45 

46 self.assertEqual(-1, signed_bitfield_lowerbound(1)) 

47 self.assertEqual(-2, signed_bitfield_lowerbound(2)) 

48 self.assertEqual(-128, signed_bitfield_lowerbound(8)) 

49 self.assertEqual(-32768, signed_bitfield_lowerbound(16)) 

50 self.assertEqual(-2147483648, signed_bitfield_lowerbound(32)) 

51 self.assertEqual(-9223372036854775808, signed_bitfield_lowerbound(64)) 

52 

53 def test_signed_bitfield_upperbound(self): 

54 with self.assertRaises(PythonRuntimeException): 

55 signed_bitfield_upperbound(0) 

56 with self.assertRaises(PythonRuntimeException): 

57 signed_bitfield_upperbound(65) 

58 

59 self.assertEqual(0, signed_bitfield_upperbound(1)) 

60 self.assertEqual(1, signed_bitfield_upperbound(2)) 

61 self.assertEqual(127, signed_bitfield_upperbound(8)) 

62 self.assertEqual(32767, signed_bitfield_upperbound(16)) 

63 self.assertEqual(2147483647, signed_bitfield_upperbound(32)) 

64 self.assertEqual(9223372036854775807, signed_bitfield_upperbound(64))