Skip to content

Commit

Permalink
get rid of inclusion of pf17 inside of unbounded_variant (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 authored Jun 5, 2024
1 parent 6510714 commit 6809326
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 53 deletions.
99 changes: 86 additions & 13 deletions cetlvast/suites/unittest/test_unbounded_variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ using cetl::unbounded_variant;
using cetl::get;
using cetl::get_if;
using cetl::make_unbounded_variant;
using cetl::in_place_type_t;
using cetl::type_id;
using cetl::type_id_type;
using cetl::rtti_helper;
Expand Down Expand Up @@ -350,6 +349,7 @@ TEST_F(TestPmrUnboundedVariant, bad_unbounded_variant_access_assignment)
TEST_F(TestPmrUnboundedVariant, cppref_example)
{
using ub_var = unbounded_variant<std::max(sizeof(int), sizeof(double))>;
static_assert(std::is_void<ub_var::pmr_type>::value, "pmr_type should be `void`");

ub_var a = 1;
EXPECT_THAT(get<int>(a), 1);
Expand Down Expand Up @@ -591,7 +591,7 @@ TEST_F(TestPmrUnboundedVariant, ctor_5_in_place)
};
using ub_var = unbounded_variant<sizeof(MyType)>;

const ub_var src{in_place_type_t<MyType>{}, 'Y', 42};
const ub_var src{ub_var::in_place_type_t<MyType>{}, 'Y', 42};

const auto test = get<MyType>(src);
EXPECT_THAT(test.ch_, 'Y');
Expand All @@ -613,7 +613,7 @@ TEST_F(TestPmrUnboundedVariant, ctor_6_in_place_initializer_list)
};
using ub_var = unbounded_variant<sizeof(MyType)>;

const ub_var src{in_place_type_t<MyType>{}, {'A', 'B', 'C'}, 42};
const ub_var src{ub_var::in_place_type_t<MyType>{}, {'A', 'B', 'C'}, 42};

auto& test = get<const MyType&>(src);
EXPECT_THAT(test.size_, 3);
Expand Down Expand Up @@ -1038,8 +1038,8 @@ TEST_F(TestPmrUnboundedVariant, swap_copyable)
using ub_var = unbounded_variant<sizeof(test), true, false>;

ub_var empty{};
ub_var a{in_place_type_t<test>{}, 'A'};
ub_var b{in_place_type_t<test>{}, 'B'};
ub_var a{ub_var::in_place_type_t<test>{}, 'A'};
ub_var b{ub_var::in_place_type_t<test>{}, 'B'};

// Self swap
a.swap(a);
Expand Down Expand Up @@ -1070,8 +1070,8 @@ TEST_F(TestPmrUnboundedVariant, swap_movable)
using ub_var = unbounded_variant<sizeof(test), false, true>;

ub_var empty{};
ub_var a{in_place_type_t<test>{}, 'A'};
ub_var b{in_place_type_t<test>{}, 'B'};
ub_var a{ub_var::in_place_type_t<test>{}, 'A'};
ub_var b{ub_var::in_place_type_t<test>{}, 'B'};

// Self swap
a.swap(a);
Expand Down Expand Up @@ -1209,6 +1209,7 @@ TEST_F(TestPmrUnboundedVariant, emplace_2_initializer_list)
TEST_F(TestPmrUnboundedVariant, pmr_only_ctor)
{
using ub_var = unbounded_variant<0 /*Footprint*/, true /*Copyable*/, true /*Movable*/, 1 /*Alignment*/, pmr>;
static_assert(std::is_same<ub_var::pmr_type, pmr>::value, "pmr_type should be `pmr`");

ub_var dst{get_default_mr()};
EXPECT_THAT(dst.has_value(), false);
Expand Down Expand Up @@ -1551,8 +1552,8 @@ TEST_F(TestPmrUnboundedVariant, pmr_swap_copyable)
using ub_var = unbounded_variant<0, true, false, alignof(std::max_align_t), pmr>;

ub_var empty{get_default_mr()};
ub_var a{get_default_mr(), in_place_type_t<test>{}, 'A'};
ub_var b{get_default_mr(), in_place_type_t<test>{}, 'B'};
ub_var a{get_default_mr(), ub_var::in_place_type_t<test>{}, 'A'};
ub_var b{get_default_mr(), ub_var::in_place_type_t<test>{}, 'B'};

// Self swap
a.swap(a);
Expand Down Expand Up @@ -1583,9 +1584,9 @@ TEST_F(TestPmrUnboundedVariant, pmr_swap_movable)
using ub_var = unbounded_variant<sizeof(test), false, true, alignof(std::max_align_t), pmr>;

ub_var empty{get_mr()};
ub_var a{get_mr(), in_place_type_t<test>{}, 'A'};
ub_var a{get_mr(), ub_var::in_place_type_t<test>{}, 'A'};
EXPECT_THAT(a.get_memory_resource(), get_mr());
ub_var b{get_default_mr(), in_place_type_t<test>{}, 'B'};
ub_var b{get_default_mr(), ub_var::in_place_type_t<test>{}, 'B'};
EXPECT_THAT(b.get_memory_resource(), get_default_mr());

// Self swap
Expand Down Expand Up @@ -1628,7 +1629,7 @@ TEST_F(TestPmrUnboundedVariant, pmr_swap_movable)
EXPECT_THAT(another_empty.get_memory_resource(), get_mr());
EXPECT_THAT(empty.get_memory_resource(), get_default_mr());

const ub_var ub_vec{get_mr(), in_place_type_t<std::vector<char>>{}, {'A', 'B', 'C'}};
const ub_var ub_vec{get_mr(), ub_var::in_place_type_t<std::vector<char>>{}, {'A', 'B', 'C'}};
EXPECT_THAT(ub_vec.get_memory_resource(), get_mr());
EXPECT_THAT(get<const std::vector<char>&>(ub_vec), testing::ElementsAre('A', 'B', 'C'));
}
Expand All @@ -1638,7 +1639,7 @@ TEST_F(TestPmrUnboundedVariant, pmr_reset_memory_resource)
using test = MyMovableOnly;
using ub_var = unbounded_variant<sizeof(test), false, true, alignof(std::max_align_t), pmr>;

ub_var a{get_mr(), in_place_type_t<test>{}, 'A'};
ub_var a{get_mr(), ub_var::in_place_type_t<test>{}, 'A'};
EXPECT_TRUE(a.has_value());
EXPECT_THAT(a.get_memory_resource(), get_mr());

Expand All @@ -1647,6 +1648,78 @@ TEST_F(TestPmrUnboundedVariant, pmr_reset_memory_resource)
EXPECT_THAT(a.get_memory_resource(), get_default_mr());
}

TEST_F(TestPmrUnboundedVariant, pmr_make_unbounded_variant_cppref_example)
{
using ub_var = unbounded_variant<0, true, true, alignof(std::max_align_t), pmr>;

auto a0 = make_unbounded_variant<std::string, ub_var>(get_mr(), "Hello, cetl::unbounded_variant!\n");
auto a1 = make_unbounded_variant<std::complex<double>, ub_var>(get_mr(), 0.1, 2.3);

EXPECT_THAT(get<std::string>(a0), "Hello, cetl::unbounded_variant!\n");
EXPECT_THAT(get<std::complex<double>>(a1), std::complex<double>(0.1, 2.3));

using lambda = std::function<const char*()>;
using ub_var_lambda = cetl::unbounded_variant<0, true, true, alignof(std::max_align_t), pmr>;

auto a3 = make_unbounded_variant<lambda, ub_var_lambda>(get_mr(), [] { return "Lambda #3.\n"; });
EXPECT_TRUE(a3.has_value());
EXPECT_THAT(get<lambda>(a3)(), "Lambda #3.\n");
}

TEST_F(TestPmrUnboundedVariant, pmr_make_unbounded_variant_1)
{
using ub_var = unbounded_variant<sizeof(int), false, true, 16, pmr>;

auto src = make_unbounded_variant<int, ub_var>(get_mr(), 42);
EXPECT_THAT(get<int>(src), 42);
static_assert(std::is_same<decltype(src), ub_var>::value, "");
}

TEST_F(TestPmrUnboundedVariant, pmr_make_unbounded_variant_2_list)
{
struct MyType : rtti_helper<type_id_type<13>>
{
std::size_t size_;
int number_;

MyType(const std::initializer_list<char> chars, const int number)
{
size_ = chars.size();
number_ = number;
}
};
using ub_var = unbounded_variant<sizeof(MyType), true, true, alignof(std::max_align_t), pmr>;

const auto src = make_unbounded_variant<MyType, ub_var>(get_mr(), {'A', 'C'}, 42);
const auto& test = get<const MyType&>(src);
EXPECT_THAT(test.size_, 2);
EXPECT_THAT(test.number_, 42);
}

TEST_F(TestPmrUnboundedVariant, pmr_use_mock_as_custom_mr_type)
{
using custom_mr_mock = StrictMock<cetlvast::MemoryResourceMock>;
custom_mr_mock mr_mock{};

const auto Alignment = alignof(std::max_align_t);
using ub_var = unbounded_variant<sizeof(int), true, true, Alignment, custom_mr_mock>;
static_assert(std::is_same<ub_var::pmr_type, custom_mr_mock>::value, "should be custom memory resource mock");

auto src = make_unbounded_variant<int, ub_var>(&mr_mock, 42);
EXPECT_THAT(get<int>(src), 42);

EXPECT_CALL(mr_mock, do_allocate(sizeof(double), Alignment))
.WillOnce(
[this](std::size_t size_bytes, std::size_t alignment) { return mr_.allocate(size_bytes, alignment); });
EXPECT_CALL(mr_mock, do_deallocate(_, sizeof(double), Alignment))
.WillOnce([this](void* p, std::size_t size_bytes, std::size_t alignment) {
mr_.deallocate(p, size_bytes, alignment);
});

src = 3.1415926;
EXPECT_THAT(get<double>(src), 3.1415926);
}

} // namespace

namespace cetl
Expand Down
Loading

0 comments on commit 6809326

Please sign in to comment.