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

109 statements  

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

1import unittest 

2 

3from test_object.api import SerializeEnum, SerializeNested, SerializeObject 

4 

5from zserio.serialization import ( 

6 serialize, 

7 deserialize, 

8 serialize_to_bytes, 

9 deserialize_from_bytes, 

10 serialize_to_file, 

11 deserialize_from_file, 

12) 

13from zserio.bitbuffer import BitBuffer 

14from zserio.exception import PythonRuntimeException 

15 

16 

17class SerializationTest(unittest.TestCase): 

18 

19 def test_serialize_enum(self): 

20 serialize_enum = SerializeEnum.VALUE3 

21 bitbuffer = serialize(serialize_enum) 

22 expected_bitsize = 8 

23 self.assertEqual(expected_bitsize, bitbuffer.bitsize) 

24 self.assertEqual(b"\x02", bitbuffer.buffer) 

25 

26 def test_serialize_parameterized_object(self): 

27 param = 0x12 

28 offset = 0 

29 optional_value = 0xDEADCAFE 

30 serialize_nested = SerializeNested(param, offset, optional_value) 

31 bitbuffer = serialize(serialize_nested) 

32 expected_bitsize = 40 

33 self.assertEqual(expected_bitsize, bitbuffer.bitsize) 

34 self.assertEqual(b"\x01\xDE\xAD\xCA\xFE", bitbuffer.buffer) 

35 

36 def test_serialize_object(self): 

37 param = 0x12 

38 offset = 0 

39 optional_value = 0xDEADCAFE 

40 serialize_nested = SerializeNested(param, offset, optional_value) 

41 serialize_object = SerializeObject(param, serialize_nested) 

42 bitbuffer = serialize(serialize_object) 

43 expected_bitsize = 48 

44 self.assertEqual(expected_bitsize, bitbuffer.bitsize) 

45 self.assertEqual(b"\x12\x02\xDE\xAD\xCA\xFE", bitbuffer.buffer) 

46 

47 def test_deserialize_enum(self): 

48 bitbuffer = BitBuffer(b"\x02", 8) 

49 serialize_enum = deserialize(SerializeEnum, bitbuffer) 

50 self.assertEqual(SerializeEnum.VALUE3, serialize_enum) 

51 

52 def test_deserialize_parameterized_object(self): 

53 bitbuffer = BitBuffer(b"\x01\xDE\xAD\xCA\xFE", 40) 

54 with self.assertRaises(TypeError): 

55 deserialize(SerializeNested, bitbuffer) 

56 serialize_nested = deserialize(SerializeNested, bitbuffer, 0x12) 

57 self.assertEqual(0x12, serialize_nested.param) 

58 self.assertEqual(0x01, serialize_nested.offset) 

59 self.assertEqual(0xDEADCAFE, serialize_nested.optional_value) 

60 

61 wrong_bitbuffer = BitBuffer(b"\x02\xDE\xAD\xCA\xFE", 39) 

62 with self.assertRaises(PythonRuntimeException): 

63 deserialize(SerializeNested, wrong_bitbuffer, 0x12) 

64 

65 def test_deserialize_object(self): 

66 bitbuffer = BitBuffer(b"\x12\x02\xDE\xAD\xCA\xFE", 48) 

67 serialize_object = deserialize(SerializeObject, bitbuffer) 

68 self.assertEqual(0x12, serialize_object.param) 

69 serialize_nested = serialize_object.nested 

70 self.assertEqual(0x12, serialize_nested.param) 

71 self.assertEqual(0x02, serialize_nested.offset) 

72 self.assertEqual(0xDEADCAFE, serialize_nested.optional_value) 

73 

74 def test_serialize_enum_to_bytes(self): 

75 serialize_enum = SerializeEnum.VALUE3 

76 buffer = serialize_to_bytes(serialize_enum) 

77 self.assertEqual(1, len(buffer)) 

78 self.assertEqual(b"\x02", buffer) 

79 

80 def test_serialize_parameterized_object_to_bytes(self): 

81 param = 0x12 

82 offset = 0 

83 optional_value = 0xDEADCAFE 

84 serialize_nested = SerializeNested(param, offset, optional_value) 

85 buffer = serialize_to_bytes(serialize_nested) 

86 self.assertEqual(5, len(buffer)) 

87 self.assertEqual(b"\x01\xDE\xAD\xCA\xFE", buffer) 

88 

89 def test_serialize_object_to_bytes(self): 

90 param = 0x12 

91 offset = 0 

92 optional_value = 0xDEADCAFE 

93 serialize_nested = SerializeNested(param, offset, optional_value) 

94 serialize_object = SerializeObject(param, serialize_nested) 

95 buffer = serialize_to_bytes(serialize_object) 

96 self.assertEqual(6, len(buffer)) 

97 self.assertEqual(b"\x12\x02\xDE\xAD\xCA\xFE", buffer) 

98 

99 def test_deserialize_enum_from_bytes(self): 

100 buffer = b"\x02" 

101 serialize_enum = deserialize_from_bytes(SerializeEnum, buffer) 

102 self.assertEqual(SerializeEnum.VALUE3, serialize_enum) 

103 

104 def test_deserialize_parameterized_object_from_bytes(self): 

105 buffer = b"\x01\xDE\xAD\xCA\xFE" 

106 with self.assertRaises(TypeError): 

107 deserialize_from_bytes(SerializeNested, buffer) 

108 serialize_nested = deserialize_from_bytes(SerializeNested, buffer, 0x12) 

109 self.assertEqual(0x12, serialize_nested.param) 

110 self.assertEqual(0x01, serialize_nested.offset) 

111 self.assertEqual(0xDEADCAFE, serialize_nested.optional_value) 

112 

113 wrong_buffer = b"\x00\xDE\xAD\xCA\xFE" 

114 with self.assertRaises(PythonRuntimeException): 

115 deserialize_from_bytes(SerializeNested, wrong_buffer, 0x12) 

116 

117 def test_deserialize_object_from_bytes(self): 

118 buffer = b"\x12\x02\xDE\xAD\xCA\xFE" 

119 serialize_object = deserialize_from_bytes(SerializeObject, buffer) 

120 self.assertEqual(0x12, serialize_object.param) 

121 serialize_nested = serialize_object.nested 

122 self.assertEqual(0x12, serialize_nested.param) 

123 self.assertEqual(0x02, serialize_nested.offset) 

124 self.assertEqual(0xDEADCAFE, serialize_nested.optional_value) 

125 

126 def test_to_file_from_file(self): 

127 param = 0x12 

128 offset = 0 

129 optional_value = 0xDEADCAFE 

130 serialize_nested = SerializeNested(param, offset, optional_value) 

131 serialize_object = SerializeObject(param, serialize_nested) 

132 filename = "SerializationTest.bin" 

133 serialize_to_file(serialize_object, filename) 

134 read_serialize_object = deserialize_from_file(SerializeObject, filename) 

135 self.assertEqual(serialize_object, read_serialize_object)