diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e027034..9d4f4e15 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -485,6 +485,7 @@ if(WITH_TESTS) test/badfs_test.cpp test/block_merger_test.cpp test/block_range_test.cpp + test/byte_buffer_test.cpp test/checksum_test.cpp test/chmod_transformer_test.cpp test/entry_test.cpp diff --git a/test/byte_buffer_test.cpp b/test/byte_buffer_test.cpp new file mode 100644 index 00000000..86f0646e --- /dev/null +++ b/test/byte_buffer_test.cpp @@ -0,0 +1,103 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs 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. + * + * dwarfs 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 dwarfs. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-only + */ + +#include + +#include +#include + +#include +#include + +TEST(byte_buffer_test, malloc_byte_buffer) { + auto buf = dwarfs::malloc_byte_buffer::create(); + static_assert(std::same_as); + + EXPECT_TRUE(buf); + EXPECT_TRUE(buf.empty()); + + EXPECT_EQ(buf.size(), 0); + EXPECT_EQ(buf.capacity(), 0); + EXPECT_EQ(buf.data(), nullptr); + + buf.reserve(20); + EXPECT_EQ(buf.capacity(), 20); + + buf.resize(10); + EXPECT_EQ(buf.size(), 10); + EXPECT_EQ(buf.capacity(), 20); + + buf.clear(); + EXPECT_EQ(buf.size(), 0); + EXPECT_EQ(buf.capacity(), 20); + + buf.append("Hello, World!", 13); + EXPECT_EQ(buf.size(), 13); + EXPECT_EQ(std::string_view(reinterpret_cast(buf.data()), buf.size()), + "Hello, World!"); + + buf.shrink_to_fit(); + EXPECT_EQ(buf.size(), 13); + EXPECT_EQ(buf.capacity(), 13); + + buf.freeze_location(); + + EXPECT_THAT([&] { buf.reserve(30); }, + ::testing::ThrowsMessage( + "operation not allowed on frozen buffer: reserve")); + + EXPECT_THAT( + [&] { buf.resize(20); }, + ::testing::ThrowsMessage( + "operation not allowed on frozen buffer: resize beyond capacity")); + + EXPECT_NO_THROW(buf.resize(5)); + EXPECT_EQ(buf.size(), 5); + EXPECT_NO_THROW(buf.append("!", 1)); + + EXPECT_THAT( + [&] { buf.append("Too much!", 9); }, + ::testing::ThrowsMessage( + "operation not allowed on frozen buffer: append beyond capacity")); + + EXPECT_THAT([&] { buf.clear(); }, + ::testing::ThrowsMessage( + "operation not allowed on frozen buffer: clear")); + + EXPECT_THAT([&] { buf.shrink_to_fit(); }, + ::testing::ThrowsMessage( + "operation not allowed on frozen buffer: shrink_to_fit")); + + auto buf2 = dwarfs::malloc_byte_buffer::create(buf.span()); + EXPECT_TRUE(buf2); + EXPECT_FALSE(buf2.empty()); + EXPECT_EQ(buf2.size(), 6); + EXPECT_NO_THROW(buf2.resize(30)); + EXPECT_TRUE(std::memcmp(buf.data(), buf2.data(), 6) == 0); + + buf = dwarfs::malloc_byte_buffer::create(13); + + EXPECT_TRUE(buf); + EXPECT_FALSE(buf.empty()); + EXPECT_EQ(buf.size(), 13); +}