From 2cdc4f5698be48727c5112bda88dc9a9d1c9e71a Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Mon, 12 Jul 2021 13:39:13 +1000 Subject: [PATCH] AP_HAL: add basic tests for ringbuffer --- .../AP_HAL/utility/tests/test_ringbuffer.cpp | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 libraries/AP_HAL/utility/tests/test_ringbuffer.cpp diff --git a/libraries/AP_HAL/utility/tests/test_ringbuffer.cpp b/libraries/AP_HAL/utility/tests/test_ringbuffer.cpp new file mode 100644 index 0000000000..bdcf41446f --- /dev/null +++ b/libraries/AP_HAL/utility/tests/test_ringbuffer.cpp @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2015-2016 Intel Corporation. All rights reserved. + * + * This file is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This file is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ +#include + +#include +#include + +TEST(ByteBufferTest, Basic) +{ + const uint16_t size = 32; + ByteBuffer x{size}; + EXPECT_EQ(x.available(), 0U); + EXPECT_EQ(x.get_size(), unsigned(size)); + EXPECT_EQ(x.space(), unsigned(size-1)); + EXPECT_TRUE(x.is_empty()); + + // write a char in, check numbers again: + EXPECT_EQ(x.write((uint8_t*)"f", 1), 1U); + EXPECT_EQ(x.available(), 1U); + EXPECT_EQ(x.get_size(), unsigned(size)); + EXPECT_EQ(x.space(), unsigned(size-2)); + EXPECT_FALSE(x.is_empty()); + // clear that byte, check numbers again + x.clear(); + EXPECT_EQ(x.available(), 0U); + EXPECT_EQ(x.get_size(), size); + EXPECT_EQ(x.space(), unsigned(size-1)); + EXPECT_TRUE(x.is_empty()); + + static const char *str = "fo"; + EXPECT_EQ(x.write((uint8_t*)str, 2), 2U); + uint8_t buf[strlen(str)+5] {}; + EXPECT_EQ(x.read(buf, sizeof(buf)), 2U); + EXPECT_STREQ((char*)buf, (char*)str); +} + +TEST(ByteBufferTest, SetSize) +{ + const uint16_t size = 32; + ByteBuffer x{0}; + x.set_size(size); + EXPECT_EQ(x.available(), 0U); + EXPECT_EQ(x.get_size(), unsigned(size)); + EXPECT_EQ(x.space(), unsigned(size-1)); + EXPECT_TRUE(x.is_empty()); +} + +TEST(ObjectBufferTest, Basic) +{ + const uint16_t size = 32; + class TestData { + uint32_t test_member; + uint8_t test_member2; + }; + ObjectBuffer x{size}; + EXPECT_EQ(x.available(), 0U); + EXPECT_EQ(x.get_size(), unsigned(size)); + EXPECT_EQ(x.space(), unsigned(size)); + EXPECT_TRUE(x.is_empty()); + + // write an object in, check numbers again: + x.push(TestData{}); + EXPECT_EQ(x.available(), 1U); + EXPECT_EQ(x.get_size(), unsigned(size)); + EXPECT_EQ(x.space(), unsigned(size-1)); + EXPECT_FALSE(x.is_empty()); + // clear that byte, check numbers again + x.clear(); + EXPECT_EQ(x.available(), 0U); + EXPECT_EQ(x.get_size(), size); + EXPECT_EQ(x.space(), unsigned(size)); + EXPECT_TRUE(x.is_empty()); +} + +TEST(ObjectBufferTest, SetSize) +{ + const uint16_t size = 32; + class TestData { + uint32_t test_member; + uint8_t test_member2; + }; + ObjectBuffer x{1}; + x.set_size(size); + EXPECT_EQ(x.available(), 0U); + EXPECT_EQ(x.get_size(), unsigned(size)); + EXPECT_EQ(x.space(), unsigned(size)); + EXPECT_TRUE(x.is_empty()); +} + +AP_GTEST_MAIN()