1# Copyright 2023 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""Tests parsing the sized-entry ring buffer's in-memory representation.""" 15 16import unittest 17 18from pw_containers import inline_var_len_entry_queue 19 20 21def _buffer(head: int, tail: int, data: bytes) -> bytes: 22 return ( 23 b''.join(i.to_bytes(4, 'little') for i in [len(data), head, tail]) 24 + data 25 ) 26 27 28class TestEncodeTokenized(unittest.TestCase): 29 def test_one_entry(self) -> None: 30 self.assertEqual( 31 list( 32 inline_var_len_entry_queue.parse( 33 _buffer(1, 6, b'?\0041234?890') 34 ) 35 ), 36 [b'1234'], 37 ) 38 39 def test_two_entries(self) -> None: 40 self.assertEqual( 41 list( 42 inline_var_len_entry_queue.parse( 43 _buffer(1, 7, b'?\00212\00234?90') 44 ) 45 ), 46 [b'12', b'34'], 47 ) 48 49 def test_two_entries_wrapped(self) -> None: 50 self.assertEqual( 51 list( 52 inline_var_len_entry_queue.parse( 53 _buffer(6, 4, b'4\00212?x\004123') 54 ) 55 ), 56 [b'1234', b'12'], 57 ) 58 59 60if __name__ == '__main__': 61 unittest.main() 62