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

61 statements  

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

1import unittest 

2 

3from zserio.builtin import isset, numbits 

4 

5 

6class BuiltinOperatorsTest(unittest.TestCase): 

7 

8 def test_isset(self): 

9 class DummyBitmask: 

10 def __init__(self): 

11 self._value = 0 

12 

13 @classmethod 

14 def from_value(cls: "DummyBitmask", value: int) -> "DummyBitmask": 

15 instance = cls() 

16 instance._value = value 

17 return instance 

18 

19 class Values: 

20 READ: "DummyBitmask" = None 

21 WRITE: "DummyBitmask" = None 

22 CREATE: "DummyBitmask" = None 

23 

24 def __eq__(self, other: object) -> bool: 

25 return self._value == other._value 

26 

27 def __and__(self, other: object) -> object: 

28 return DummyBitmask.from_value(self._value & other._value) 

29 

30 def __or__(self, other: object) -> object: 

31 return DummyBitmask.from_value(self._value | other._value) 

32 

33 DummyBitmask.Values.READ = DummyBitmask.from_value(1) 

34 DummyBitmask.Values.WRITE = DummyBitmask.from_value(2) 

35 DummyBitmask.Values.CREATE = DummyBitmask.from_value(1 | 2) 

36 

37 self.assertTrue(isset(DummyBitmask.Values.READ, DummyBitmask.Values.READ)) 

38 self.assertTrue(isset(DummyBitmask.Values.CREATE, DummyBitmask.Values.READ)) 

39 self.assertTrue(isset(DummyBitmask.Values.CREATE, DummyBitmask.Values.WRITE)) 

40 self.assertTrue(isset(DummyBitmask.Values.CREATE, DummyBitmask.Values.CREATE)) 

41 self.assertTrue( 

42 isset( 

43 DummyBitmask.Values.CREATE, 

44 DummyBitmask.Values.READ | DummyBitmask.Values.WRITE, 

45 ) 

46 ) 

47 self.assertFalse(isset(DummyBitmask.Values.READ, DummyBitmask.Values.WRITE)) 

48 self.assertFalse(isset(DummyBitmask.Values.READ, DummyBitmask.Values.CREATE)) 

49 

50 def test_numbits(self): 

51 self.assertEqual(0, numbits(0)) 

52 self.assertEqual(1, numbits(1)) 

53 self.assertEqual(1, numbits(2)) 

54 self.assertEqual(2, numbits(3)) 

55 self.assertEqual(2, numbits(4)) 

56 self.assertEqual(3, numbits(5)) 

57 self.assertEqual(3, numbits(6)) 

58 self.assertEqual(3, numbits(7)) 

59 self.assertEqual(3, numbits(8)) 

60 self.assertEqual(4, numbits(16)) 

61 self.assertEqual(5, numbits(32)) 

62 self.assertEqual(6, numbits(64)) 

63 self.assertEqual(7, numbits(128)) 

64 self.assertEqual(8, numbits(256)) 

65 self.assertEqual(9, numbits(512)) 

66 self.assertEqual(10, numbits(1024)) 

67 self.assertEqual(11, numbits(2048)) 

68 self.assertEqual(12, numbits(4096)) 

69 self.assertEqual(13, numbits(8192)) 

70 self.assertEqual(14, numbits(16384)) 

71 self.assertEqual(15, numbits(32768)) 

72 self.assertEqual(16, numbits(65536)) 

73 self.assertEqual(24, numbits(1 << 24)) 

74 self.assertEqual(25, numbits((1 << 24) + 1)) 

75 self.assertEqual(32, numbits(1 << 32)) 

76 self.assertEqual(33, numbits((1 << 32) + 1)) 

77 self.assertEqual(63, numbits(1 << 63)) 

78 self.assertEqual(64, numbits((1 << 63) + 1))