2 # Copyright 2020 The Pigweed Authors
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 # use this file except in compliance with the License. You may obtain a copy of
8 # https://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations under
15 """Tests the tokenized string encoder module."""
19 import varint_test_data
21 from pw_tokenizer.encode import encode_token_and_args
24 class TestEncodeTokenized(unittest.TestCase):
25 """Tests encoding tokenized strings with various arguments."""
26 def test_no_args(self):
27 self.assertEqual(b'\xab\xcd\x12\x34',
28 encode_token_and_args(0x3412cdab))
29 self.assertEqual(b'\x00\x00\x00\x00', encode_token_and_args(0))
32 self.assertEqual(b'\xff\xff\xff\xff\0',
33 encode_token_and_args(0xffffffff, 0))
34 self.assertEqual(b'\xff\xff\xff\xff\1',
35 encode_token_and_args(0xffffffff, -1))
36 self.assertEqual(b'\xff\xff\xff\xff\2',
37 encode_token_and_args(0xffffffff, 1))
40 self.assertEqual(b'\xff\xff\xff\xff\0\0\0\0',
41 encode_token_and_args(0xffffffff, 0.0))
42 self.assertEqual(b'\xff\xff\xff\xff\0\0\0\x80',
43 encode_token_and_args(0xffffffff, -0.0))
45 def test_string(self):
46 self.assertEqual(b'\xff\xff\xff\xff\5hello',
47 encode_token_and_args(0xffffffff, 'hello'))
48 self.assertEqual(b'\xff\xff\xff\xff\x7f' + b'!' * 127,
49 encode_token_and_args(0xffffffff, '!' * 127))
51 def test_string_too_long(self):
52 self.assertEqual(b'\xff\xff\xff\xff\xff' + b'!' * 127,
53 encode_token_and_args(0xffffffff, '!' * 128))
56 self.assertEqual(b'\xff\xff\xff\xff\4\0yo\0',
57 encode_token_and_args(0xffffffff, '\0yo\0'))
59 def test_bytes_too_long(self):
60 self.assertEqual(b'\xff\xff\xff\xff\xff' + b'?' * 127,
61 encode_token_and_args(0xffffffff, b'?' * 200))
63 def test_multiple_args(self):
64 self.assertEqual(b'\xdd\xcc\xbb\xaa\0',
65 encode_token_and_args(0xaabbccdd, 0))
68 class TestIntegerEncoding(unittest.TestCase):
69 """Test encoding variable-length integers."""
70 def test_encode_generated_data(self):
71 test_data = varint_test_data.TEST_DATA
72 self.assertGreater(len(test_data), 100)
74 for _, signed, _, unsigned, encoded in test_data:
75 # Skip numbers that are larger than 32-bits, since they aren't
76 # supported currently.
77 if int(unsigned).bit_length() > 32:
80 # Encode the value as an arg, but skip the 4 bytes for the token.
82 encode_token_and_args(0, int(signed))[4:], encoded)
84 encode_token_and_args(0, int(unsigned))[4:], encoded)
87 if __name__ == '__main__':