Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Box recursive type #215

Merged
merged 3 commits into from
Aug 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions test/box/recursive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include <immer/set.hpp>
#include <immer/vector.hpp>

#include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <catch.hpp>

#include <catch2/catch_test_macros.hpp>

struct rec_vec
Expand Down Expand Up @@ -106,3 +110,80 @@ TEST_CASE("recursive set")
CHECK(v3.children.count(rec_set{13, {}}) == 1);
CHECK(v3.children.count(rec_set{14, {}}) == 0);
}

namespace example1 {

struct a_type;

struct b_type
{
b_type(int a, immer::box<a_type> val);
int a;
immer::box<a_type> val;
};

struct a_type
{
a_type();
// this does not work with std::optional, because it seems like
// `immer::box<b_type>` is still not considered complete at this point...
boost::optional<immer::box<b_type>> b;
};

b_type::b_type(int a, immer::box<a_type> val)
: a(a)
, val(val)
{}

a_type::a_type()
: b{}
{}

} // namespace example1

TEST_CASE("recursive optional")
{
auto x = example1::a_type{};
auto y = example1::b_type{42, x};
CHECK(y.a == 42);
}

namespace example2 {

struct empty_t
{};

struct a_type;

struct b_type
{
b_type(int a, immer::box<a_type> val);
int a;
immer::box<a_type> val;
};

struct a_type
{
a_type();
// this does not work with std::variant, because it seems like
// `immer::box<b_type>` is still not considered complete at this point...
boost::variant<empty_t, b_type> b;
};

b_type::b_type(int a, immer::box<a_type> val)
: a(a)
, val(val)
{}

a_type::a_type()
: b{}
{}

} // namespace example2

TEST_CASE("recursive variant")
{
auto x = example2::a_type{};
auto y = example2::b_type{42, x};
CHECK(y.a == 42);
}
Loading