basedOpinionated utility library |
git clone git://git.dimitrijedobrota.com/based.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
commit | 4d55232d42d00afb41a2b65d88e291bddc2cc1ea |
parent | 2272f546a59ff1312ab61aecfb0e2388b27d2fc9 |
author | Dimitrije Dobrota < mail@dimitrijedobrota.com > |
date | Tue, 29 Apr 2025 12:13:13 +0200 |
Test buffer
M | include/based/template.hpp | | | +++ --- |
M | test/CMakeLists.txt | | | ++++ |
A | test/source/buffer_test.cpp | | | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 73 insertions(+), 3 deletions(-)
diff --git a/ include/based/template.hpp b/ include/based/template.hpp
@@ -275,9 +275,9 @@
struct buffer
void swap(buffer& that) noexcept
{
alignas(alignment) char tmp[size]; // NOLINT array
::memcpy(tmp, this->m_space, size);
::memcpy(this->m_space, that.m_space, size);
::memcpy(that.m_space, tmp, size);
::memcpy(tmp, this->m_space, size); // NOLINT array
::memcpy(this->m_space, that.m_space, size); // NOLINT array
::memcpy(that.m_space, tmp, size); // NOLINT array
}
};
diff --git a/ test/CMakeLists.txt b/ test/CMakeLists.txt
@@ -56,13 +56,17 @@
add_test(find_mismatch_n_test)
add_test(find_if_unguarded_test)
# ---- List ----
add_test(list_test)
# ---- String ----
add_test(string_literal_test)
# ---- Template ----
add_test(signature_test)
add_test(buffer_test)
# ---- End-of-file commands ----
diff --git a/ test/source/buffer_test.cpp b/ test/source/buffer_test.cpp
@@ -0,0 +1,66 @@
#define CATCH_CONFIG_RUNTIME_STATIC_REQUIRE
#include <catch2/catch_test_macros.hpp>
#include "based/template.hpp"
template struct based::buffer<sizeof(void*)>;
TEST_CASE("valid type", "[template/buffer]")
{
SECTION("small buffer")
{
using buffer = based::buffer<sizeof(std::uint8_t)>;
STATIC_REQUIRE(buffer::valid_type<std::uint8_t>());
STATIC_REQUIRE_FALSE(buffer::valid_type<std::size_t>());
STATIC_REQUIRE_FALSE(buffer::valid_type<char[5]>()); // NOLINT array
}
SECTION("big buffer")
{
using buffer = based::buffer<sizeof(std::size_t), alignof(std::size_t)>;
STATIC_REQUIRE(buffer::valid_type<std::uint8_t>());
STATIC_REQUIRE(buffer::valid_type<std::size_t>());
STATIC_REQUIRE_FALSE(buffer::valid_type<char[5]>()); // NOLINT array
}
}
TEST_CASE("buffer", "[template/buffer]")
{
using buffer = based::buffer<sizeof(std::size_t)>;
static constexpr std::uint8_t value = 8;
buffer buf(std::in_place_type<std::uint8_t>, value);
REQUIRE(*buf.as<std::uint8_t>() == value);
SECTION("emplace")
{
static constexpr std::uint16_t new_value = 10;
buf.emplace<std::uint16_t>(new_value);
REQUIRE(*buf.as<std::uint16_t>() == new_value);
}
SECTION("swap")
{
static constexpr std::uint16_t new_value = 10;
buffer new_buf(std::in_place_type<std::uint16_t>, new_value);
buf.swap(new_buf);
REQUIRE(*buf.as<std::uint16_t>() == new_value);
REQUIRE(*new_buf.as<std::uint8_t>() == value);
}
}
TEST_CASE("const buffer", "[template/buffer]")
{
using buffer = based::buffer<sizeof(std::size_t)>;
static constexpr std::uint8_t value = 8;
const buffer buf(std::in_place_type<std::uint8_t>, value);
REQUIRE(*buf.as<std::uint8_t>() == value);
}