diff options
Diffstat (limited to 'pw_containers/py/variable_length_entry_queue_test.py')
-rw-r--r-- | pw_containers/py/variable_length_entry_queue_test.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/pw_containers/py/variable_length_entry_queue_test.py b/pw_containers/py/variable_length_entry_queue_test.py new file mode 100644 index 000000000..3343fd199 --- /dev/null +++ b/pw_containers/py/variable_length_entry_queue_test.py @@ -0,0 +1,61 @@ +# Copyright 2023 The Pigweed Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not +# use this file except in compliance with the License. You may obtain a copy of +# the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations under +# the License. +"""Tests parsing the sized-entry ring buffer's in-memory representation.""" + +import unittest + +from pw_containers import variable_length_entry_queue + + +def _buffer(head: int, tail: int, data: bytes) -> bytes: + return ( + b''.join(i.to_bytes(4, 'little') for i in [len(data), head, tail]) + + data + ) + + +class TestEncodeTokenized(unittest.TestCase): + def test_one_entry(self) -> None: + self.assertEqual( + list( + variable_length_entry_queue.parse( + _buffer(1, 6, b'?\0041234?890') + ) + ), + [b'1234'], + ) + + def test_two_entries(self) -> None: + self.assertEqual( + list( + variable_length_entry_queue.parse( + _buffer(1, 7, b'?\00212\00234?90') + ) + ), + [b'12', b'34'], + ) + + def test_two_entries_wrapped(self) -> None: + self.assertEqual( + list( + variable_length_entry_queue.parse( + _buffer(6, 4, b'4\00212?x\004123') + ) + ), + [b'1234', b'12'], + ) + + +if __name__ == '__main__': + unittest.main() |