1#!/usr/bin/env python3 2# Copyright 2020 The Pigweed Authors 3# 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 6# the License at 7# 8# https://www.apache.org/licenses/LICENSE-2.0 9# 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 14# the License. 15"""Tests the tokenized string encoder module.""" 16 17import unittest 18 19import varint_test_data 20 21from pw_tokenizer.encode import encode_token_and_args 22 23 24class TestEncodeTokenized(unittest.TestCase): 25 """Tests encoding tokenized strings with various arguments.""" 26 27 def test_no_args(self): 28 self.assertEqual(b'\xab\xcd\x12\x34', encode_token_and_args(0x3412CDAB)) 29 self.assertEqual(b'\x00\x00\x00\x00', encode_token_and_args(0)) 30 31 def test_int(self): 32 self.assertEqual( 33 b'\xff\xff\xff\xff\0', encode_token_and_args(0xFFFFFFFF, 0) 34 ) 35 self.assertEqual( 36 b'\xff\xff\xff\xff\1', encode_token_and_args(0xFFFFFFFF, -1) 37 ) 38 self.assertEqual( 39 b'\xff\xff\xff\xff\2', encode_token_and_args(0xFFFFFFFF, 1) 40 ) 41 42 def test_float(self): 43 self.assertEqual( 44 b'\xff\xff\xff\xff\0\0\0\0', encode_token_and_args(0xFFFFFFFF, 0.0) 45 ) 46 self.assertEqual( 47 b'\xff\xff\xff\xff\0\0\0\x80', 48 encode_token_and_args(0xFFFFFFFF, -0.0), 49 ) 50 51 def test_string(self): 52 self.assertEqual( 53 b'\xff\xff\xff\xff\5hello', 54 encode_token_and_args(0xFFFFFFFF, 'hello'), 55 ) 56 self.assertEqual( 57 b'\xff\xff\xff\xff\x7f' + b'!' * 127, 58 encode_token_and_args(0xFFFFFFFF, '!' * 127), 59 ) 60 61 def test_string_too_long(self): 62 self.assertEqual( 63 b'\xff\xff\xff\xff\xff' + b'!' * 127, 64 encode_token_and_args(0xFFFFFFFF, '!' * 128), 65 ) 66 67 def test_bytes(self): 68 self.assertEqual( 69 b'\xff\xff\xff\xff\4\0yo\0', 70 encode_token_and_args(0xFFFFFFFF, '\0yo\0'), 71 ) 72 73 def test_bytes_too_long(self): 74 self.assertEqual( 75 b'\xff\xff\xff\xff\xff' + b'?' * 127, 76 encode_token_and_args(0xFFFFFFFF, b'?' * 200), 77 ) 78 79 def test_multiple_args(self): 80 self.assertEqual( 81 b'\xdd\xcc\xbb\xaa\0', encode_token_and_args(0xAABBCCDD, 0) 82 ) 83 84 85class TestIntegerEncoding(unittest.TestCase): 86 """Test encoding variable-length integers.""" 87 88 def test_encode_generated_data(self): 89 test_data = varint_test_data.TEST_DATA 90 self.assertGreater(len(test_data), 100) 91 92 for _, signed, _, unsigned, encoded in test_data: 93 # Skip numbers that are larger than 32-bits, since they aren't 94 # supported currently. 95 if int(unsigned).bit_length() > 32: 96 continue 97 98 # Encode the value as an arg, but skip the 4 bytes for the token. 99 self.assertEqual(encode_token_and_args(0, int(signed))[4:], encoded) 100 self.assertEqual( 101 encode_token_and_args(0, int(unsigned))[4:], encoded 102 ) 103 104 105if __name__ == '__main__': 106 unittest.main() 107