test: add malloc_byte_buffer / mutable_byte_buffer tests

This commit is contained in:
Marcus Holland-Moritz 2025-08-17 22:24:12 +02:00
parent 48898bb4d1
commit 9137791dd7
2 changed files with 104 additions and 0 deletions

View File

@ -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

103
test/byte_buffer_test.cpp Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include <cstring>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <dwarfs/malloc_byte_buffer.h>
#include <dwarfs/mapped_byte_buffer.h>
TEST(byte_buffer_test, malloc_byte_buffer) {
auto buf = dwarfs::malloc_byte_buffer::create();
static_assert(std::same_as<decltype(buf), dwarfs::mutable_byte_buffer>);
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<char*>(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<std::runtime_error>(
"operation not allowed on frozen buffer: reserve"));
EXPECT_THAT(
[&] { buf.resize(20); },
::testing::ThrowsMessage<std::runtime_error>(
"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<std::runtime_error>(
"operation not allowed on frozen buffer: append beyond capacity"));
EXPECT_THAT([&] { buf.clear(); },
::testing::ThrowsMessage<std::runtime_error>(
"operation not allowed on frozen buffer: clear"));
EXPECT_THAT([&] { buf.shrink_to_fit(); },
::testing::ThrowsMessage<std::runtime_error>(
"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);
}