Added serialization and deserialization for signed integers
[sandbox] / serial / test_binary.py
1 import unittest
2
3 import binary
4
5 EXAMPLE_REPRESENTATIONS = [
6     (binary.TAG_NULL, None, b'\x00'),
7     (binary.TAG_TRUE, True, b'\x01'),
8     (binary.TAG_FALSE, False, b'\x02'),
9     (binary.TAG_UINT8, 7, b'\x03\x07'),
10     (binary.TAG_UINT16, 7, b'\x04\x00\x07'),
11     (binary.TAG_UINT32, 7, b'\x05\x00\x00\x00\x07'),
12     (binary.TAG_UINT64, 7, b'\x06\x00\x00\x00\x00\x00\x00\x00\x07'),
13     (binary.TAG_INT8, 7, b'\x10\x07'),
14     (binary.TAG_INT16, 7, b'\x11\x00\x07'),
15     (binary.TAG_INT32, 7, b'\x12\x00\x00\x00\x07'),
16     (binary.TAG_INT64, 7, b'\x13\x00\x00\x00\x00\x00\x00\x00\x07'),
17     (binary.TAG_UINT8, 254, b'\x03\xfe'),
18     (binary.TAG_UINT16, 65534, b'\x04\xff\xfe'),
19     (binary.TAG_UINT32, 4294967294, b'\x05\xff\xff\xff\xfe'),
20     (binary.TAG_UINT64, 18446744073709551614, b'\x06\xff\xff\xff\xff\xff\xff\xff\xfe'),
21     (binary.TAG_INT8, -2, b'\x10\xfe'),
22     (binary.TAG_INT16, -2, b'\x11\xff\xfe'),
23     (binary.TAG_INT32, -2, b'\x12\xff\xff\xff\xfe'),
24     (binary.TAG_INT64, -2, b'\x13\xff\xff\xff\xff\xff\xff\xff\xfe'),
25 ]
26
27 class SerializeTests(unittest.TestCase):
28     def test_serialize(self):
29         for tag, instance, representation in EXAMPLE_REPRESENTATIONS:
30             self.assertEqual(
31                 binary.serialize(binary.TaggedObject(
32                     tag = tag,
33                     instance = instance,
34                 )),
35                 representation,
36             )
37
38 class DeserializeTests(unittest.TestCase):
39     def test_deserialize(self):
40         for tag, instance, representation in EXAMPLE_REPRESENTATIONS:
41             self.assertEqual(
42                 binary.deserialize(representation),
43                 binary.TaggedObject(
44                     tag = tag,
45                     instance = instance,
46                 ),
47             )
48
49 unittest.main()