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 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)) 30 31 def test_int(self): 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)) 38 39 def test_float(self): 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)) 44 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)) 50 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)) 54 55 def test_bytes(self): 56 self.assertEqual(b'\xff\xff\xff\xff\4\0yo\0', 57 encode_token_and_args(0xffffffff, '\0yo\0')) 58 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)) 62 63 def test_multiple_args(self): 64 self.assertEqual(b'\xdd\xcc\xbb\xaa\0', 65 encode_token_and_args(0xaabbccdd, 0)) 66 67 68class 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) 73 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: 78 continue 79 80 # Encode the value as an arg, but skip the 4 bytes for the token. 81 self.assertEqual( 82 encode_token_and_args(0, int(signed))[4:], encoded) 83 self.assertEqual( 84 encode_token_and_args(0, int(unsigned))[4:], encoded) 85 86 87if __name__ == '__main__': 88 unittest.main() 89