Skip to content

Commit

Permalink
Enhancements to span and other functionality, changed byte compares t…
Browse files Browse the repository at this point in the history
…o integer compares for Catch2 stringify weirdness on MSVC and macos
  • Loading branch information
cliffg-softwarelibre committed Aug 23, 2024
1 parent 717fb20 commit c4009eb
Showing 1 changed file with 104 additions and 45 deletions.
149 changes: 104 additions & 45 deletions test/shared_buffer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,35 @@
#include <list>
#include <string_view>
#include <span>
#include <array>
#include <algorithm> // std::copy
#include <bit> // std::bit_cast

#include "buffer/shared_buffer.hpp"

#include "utility/repeat.hpp"
#include "utility/make_byte_array.hpp"
#include "utility/byte_array.hpp"

constexpr std::byte Harhar { 42 };
constexpr int N = 11;
constexpr std::size_t test_data_size { 12u };
using test_data_type = std::array<std::byte, test_data_size>;
constexpr test_data_type test_data { chops::make_byte_array( 40, 41, 42, 43, 44, 60, 59, 58, 57, 56, 42, 42 ) };
char test_data_char[test_data_size] { 40, 41, 42, 43, 44, 60, 59, 58, 57, 56, 42, 42 };
const char* test_data_char_ptr {test_data_char};

template <typename SB>
bool check_sb_against_test_data(SB sb) {
REQUIRE (sb.size() == test_data_size);
test_data_type buf;
std::copy(sb.data(), sb.data()+sb.size(), buf.begin());
return chops::compare_byte_arrays(buf, test_data);
}

template <typename SB, typename PT>
SB generic_pointer_construction_test() {
auto arr = chops::make_byte_array( 40, 41, 42, 43, 44, 60, 59, 58, 57, 56, 42, 42 );
auto ptr = std::bit_cast<const PT *>(arr.data());
SB sb(ptr, arr.size());
auto ptr { std::bit_cast<const PT *>(test_data.data()) };
SB sb(ptr, test_data_size);
REQUIRE_FALSE (sb.empty());
chops::repeat(static_cast<int>(arr.size()), [&sb, arr] (int i) { REQUIRE(*(sb.data()+i) == arr[i]); } );
REQUIRE (check_sb_against_test_data(sb));
return sb;
}

Expand All @@ -49,48 +60,85 @@ void generic_pointer_append_test() {
const PT* ptr_arr { arr };
sb.append (ptr_arr, 3);
REQUIRE (sb.size() == (sav_sz + 3));
std::span<const PT> sp { arr };
std::span<const PT, 3> sp { arr };
sb.append (sp);
REQUIRE (sb.size() == (sav_sz + 6));
}

template <typename SB>
void common_methods_test(const std::byte* buf, typename SB::size_type sz) {
void check_sb(SB sb) {
REQUIRE_FALSE (sb.empty());
REQUIRE (sb.size() == test_data_size);
REQUIRE (check_sb_against_test_data(sb));
}

REQUIRE (sz > 2);
template <typename SB>
void common_ctor_test() {

SB sb(buf, sz);
REQUIRE_FALSE (sb.empty());
{
SB sb2(buf, sz);
REQUIRE_FALSE (sb2.empty());
REQUIRE (sb == sb2);
std::span<const std::byte, test_data_size> sp { test_data };
SB sb{sp};
check_sb(sb);
}
{
std::list<std::byte> lst (buf, buf+sz);
SB sb2(lst.cbegin(), lst.cend());
REQUIRE_FALSE (sb2.empty());
REQUIRE (sb == sb2);
std::span<const std::byte> sp { test_data.data(), test_data.size() };
SB sb{sp};
check_sb(sb);
}
{
auto ba = chops::make_byte_array(buf[0], buf[1]);
SB sb2(ba.cbegin(), ba.cend());
REQUIRE_FALSE (sb2.empty());
REQUIRE (((sb2 < sb) != 0)); // uses spaceship operator
REQUIRE (sb2 != sb);
SB sb{test_data.data(), test_data.size()};
check_sb(sb);
}
{
auto ba = chops::make_byte_array(0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
SB sb2(ba.cbegin(), ba.cend());
REQUIRE_FALSE (sb2.empty());
REQUIRE (sb2 != sb);
std::span<const char, test_data_size> sp { test_data_char };
SB sb{sp};
check_sb(sb);
}
{
std::span<const char> sp { test_data_char, test_data_char+test_data_size };
SB sb{sp};
check_sb(sb);
}
{
SB sb{test_data_char_ptr, test_data_size};
check_sb(sb);
}

{
std::list<std::byte> lst {test_data.cbegin(), test_data.cend()};
SB sb {lst.cbegin(), lst.cend()};
check_sb(sb);
}
{
SB sb1{test_data.data(), test_data.size()};
SB sb2{test_data.data(), test_data.size()};
REQUIRE (sb1 == sb2);
}
{
SB sb1{test_data.data(), test_data.size()};
SB sb2{sb1};
REQUIRE (sb1 == sb2);
}

}

template <typename SB>
void common_comparison_test() {
auto ba1 { chops::make_byte_array(0x00, 0x00, 0x00) };
auto ba2 { chops::make_byte_array(0x00, 0x22, 0x33) };

SB sb1(ba1.cbegin(), ba1.cend());
SB sb2(ba2.cbegin(), ba2.cend());
REQUIRE_FALSE (sb1.empty());
REQUIRE_FALSE (sb2.empty());
REQUIRE_FALSE (sb1 == sb2);
REQUIRE (((sb1 < sb2) != 0)); // uses spaceship operator
}

template <typename SB>
void byte_vector_move_test() {

auto arr = chops::make_byte_array (0x01, 0x02, 0x03, 0x04, 0x05);
auto arr { chops::make_byte_array (0x01, 0x02, 0x03, 0x04, 0x05) };

std::vector<std::byte> bv { arr.cbegin(), arr.cend() };
SB sb(std::move(bv));
Expand All @@ -106,17 +154,24 @@ TEMPLATE_TEST_CASE ( "Generic pointer construction",
}


TEMPLATE_TEST_CASE ( "Shared buffer common methods",
"[const_shared_buffer] [common]",
TEMPLATE_TEST_CASE ( "Shared buffer common ctor methods",
"[const_shared_buffer] [mutable_shared_buffer] [common]",
chops::mutable_shared_buffer, chops::const_shared_buffer ) {
common_ctor_test<TestType>();
}

TEMPLATE_TEST_CASE ( "Shared buffer common comparison methods",
"[const_shared_buffer] [mutable_shared_buffer] [common]",
chops::mutable_shared_buffer, chops::const_shared_buffer ) {
auto arr = chops::make_byte_array ( 80, 81, 82, 83, 84, 90, 91, 92 );
common_methods_test<TestType>(arr.data(), arr.size());
common_comparison_test<TestType>();
}

TEST_CASE ( "Mutable shared buffer copy construction and assignment",
"[mutable_shared_buffer] [copy]" ) {

auto arr = chops::make_byte_array ( 80, 81, 82, 83, 84, 90, 91, 92 );
constexpr std::byte Harhar { 42 };

auto arr { chops::make_byte_array ( 80, 81, 82, 83, 84, 90, 91, 92 ) };

chops::mutable_shared_buffer sb;
REQUIRE (sb.empty());
Expand All @@ -140,18 +195,22 @@ TEST_CASE ( "Mutable shared buffer copy construction and assignment",
TEST_CASE ( "Mutable shared buffer resize and clear",
"[mutable_shared_buffer] [resize_and_clear]" ) {

constexpr int N = 11;

chops::mutable_shared_buffer sb;
REQUIRE (sb.empty());
REQUIRE (sb.size() == 0);

sb.resize(N);
REQUIRE (sb.size() == N);
chops::repeat(N, [&sb] (int i) { REQUIRE (*(sb.data() + i) == std::byte{0} ); } );
chops::repeat(N, [&sb] (int i) { REQUIRE (std::to_integer<int>(*(sb.data() + i)) == 0 ); } );

SECTION ( "Compare two resized mutable shared buffer with same size" ) {
chops::mutable_shared_buffer sb2(N);
REQUIRE (sb == sb2);
chops::repeat(N, [&sb, &sb2] (int i) {
REQUIRE (*(sb.data() + i) == std::byte{0} );
REQUIRE (*(sb2.data() + i) == std::byte{0} );
REQUIRE (std::to_integer<int>(*(sb.data() + i)) == 0 );
REQUIRE (std::to_integer<int>(*(sb2.data() + i)) == 0 );
} );
}
SECTION ( "Clear, check size" ) {
Expand All @@ -174,15 +233,15 @@ TEST_CASE ( "Mutable shared buffer swap",
REQUIRE (sb1.size() == arr2.size());
REQUIRE (sb2.size() == arr1.size());

REQUIRE (*(sb1.data()+0) == *(arr2.data()+0));
REQUIRE (*(sb1.data()+1) == *(arr2.data()+1));
REQUIRE (*(sb1.data()+2) == *(arr2.data()+2));
REQUIRE (*(sb1.data()+3) == *(arr2.data()+3));
REQUIRE (*(sb1.data()+4) == *(arr2.data()+4));
REQUIRE (std::to_integer<int>(*(sb1.data()+0)) == std::to_integer<int>(*(arr2.data()+0)));
REQUIRE (std::to_integer<int>(*(sb1.data()+1)) == std::to_integer<int>(*(arr2.data()+1)));
REQUIRE (std::to_integer<int>(*(sb1.data()+2)) == std::to_integer<int>(*(arr2.data()+2)));
REQUIRE (std::to_integer<int>(*(sb1.data()+3)) == std::to_integer<int>(*(arr2.data()+3)));
REQUIRE (std::to_integer<int>(*(sb1.data()+4)) == std::to_integer<int>(*(arr2.data()+4)));

REQUIRE (*(sb2.data()+0) == *(arr1.data()+0));
REQUIRE (*(sb2.data()+1) == *(arr1.data()+1));
REQUIRE (*(sb2.data()+2) == *(arr1.data()+2));
REQUIRE (std::to_integer<int>(*(sb2.data()+0)) == std::to_integer<int>(*(arr1.data()+0)));
REQUIRE (std::to_integer<int>(*(sb2.data()+1)) == std::to_integer<int>(*(arr1.data()+1)));
REQUIRE (std::to_integer<int>(*(sb2.data()+2)) == std::to_integer<int>(*(arr1.data()+2)));
}

TEST_CASE ( "Mutable shared buffer append",
Expand Down

0 comments on commit c4009eb

Please sign in to comment.