8bc38112ac66bd8a4c0cadd8736fd05c4d0b9af5
[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     (binary.TAG_BINARY, b'\xde\xad\xbe\xef', b'\x20\x00\x00\x00\x04\xde\xad\xbe\xef'),
26     (binary.TAG_UTF8, 'Lol!', b'\x21\x00\x00\x00\x04Lol!'),
27     (binary.TAG_UTF16, 'かわ', b'\x22\x00\x00\x00\x06\xff\xfeK0\x8f0'),
28     (binary.TAG_UTF32, '漢', b'\x23\x00\x00\x00\x08\xff\xfe\x00\x00"o\x00\x00'),
29 ]
30
31 class SerializeTests(unittest.TestCase):
32     def test_serialize(self):
33         for tag, instance, representation in EXAMPLE_REPRESENTATIONS:
34             self.assertEqual(
35                 binary.serialize(binary.TaggedObject(
36                     tag = tag,
37                     instance = instance,
38                 )),
39                 representation,
40             )
41
42 class DeserializeTests(unittest.TestCase):
43     def test_deserialize(self):
44         for tag, instance, representation in EXAMPLE_REPRESENTATIONS:
45             self.assertEqual(
46                 binary.deserialize(representation),
47                 binary.TaggedObject(
48                     tag = tag,
49                     instance = instance,
50                 ),
51             )
52
53 unittest.main()