Skip to content

Commit

Permalink
Start testing recursive types, WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-sparus committed Feb 19, 2024
1 parent f253c4c commit e444c4a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 45 deletions.
14 changes: 14 additions & 0 deletions immer/experimental/immer-archive/json/archivable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ struct archivable
{
}

archivable(const archivable& other)
: container{other.container}
{
}

archivable& operator=(const archivable&) = default;

archivable(archivable&& other)
: container{std::move(other.container)}
{
}

archivable& operator=(archivable&&) = default;

friend bool operator==(const archivable& left, const archivable& right)
{
return left.container == right.container;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ auto to_json_with_archive(const T& serializable)
immer_archive::json_immer_output_archive<decltype(archives)>{
archives, os2};
ar2(archives);
ar2.finalize();
archives = ar2.get_output_archives();
}

Expand Down
3 changes: 2 additions & 1 deletion immer/experimental/immer-archive/rbts/load.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ class loader
node_t::delete_inner_r(ptr, n);
delete_children();
}}
: node_ptr{node_t::make_inner_n(n),
: node_ptr{n ? node_t::make_inner_n(n)
: rbtree::empty_root(),
[n, delete_children](auto* ptr) {
node_t::delete_inner(ptr, n);
delete_children();
Expand Down
7 changes: 6 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ _mk-dir name:
build-valgrind-path := "build-valgrind-" + os() + "-" + arch()

# Create a build directory for a Debug build without ASAN, so that valgrind can work
[linux]
mk-build-valgrind: (_mk-dir build-valgrind-path)
cd {{ build-valgrind-path }} ; cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Debug -Dimmer_BUILD_TESTS=ON -Dimmer_BUILD_ARCHIVE_TESTS=ON -Dimmer_BUILD_EXAMPLES=OFF -DCXX_STANDARD=20

[linux]
run-valgrind:
cd {{ build-valgrind-path }} ; ninja tests && ctest -D ExperimentalMemCheck

[linux]
run-valgrind-archive:
cd {{ build-valgrind-path }} ; ninja immer-archive-tests && valgrind --quiet --error-exitcode=99 --leak-check=full --errors-for-leak-kinds=all \
--suppressions=../test/experimental/immer-archive/valgrind.supp \
./test/experimental/immer-archive/immer-archive-tests

build-asan-path := "build-asan-" + os() + "-" + arch()

# Create a build directory for a Debug build with ASAN enabled
Expand Down
140 changes: 97 additions & 43 deletions test/experimental/immer-archive/test_special_archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,46 +577,100 @@ TEST_CASE("Special archive throws cereal::Exception")
"archive: Container ID 99 is not found"));
}

// namespace {
// struct recursive_type
// {
// int data;
// immer_archive::archivable<
// vector_one<immer_archive::archivable<immer::box<recursive_type>>>>
// children;

// template <class Archive>
// void serialize(Archive& ar)
// {
// ar(CEREAL_NVP(data), CEREAL_NVP(children));
// }
// };
// } // namespace

// TEST_CASE("Test recursive type")
// {
// auto v1 = recursive_type{
// .data = 123,
// };
// auto v2 = recursive_type{
// .data = 234,
// };
// auto v3 = recursive_type{
// .data = 345,
// .children = {v1, v2},
// };

// const auto [json_str, archives] =
// immer_archive::to_json_with_archive(v3);

// // REQUIRE(json_str == "");

// {
// auto full_load =
// immer_archive::from_json_with_archive<recursive_type>(json_str);
// (void) full_load;
// // REQUIRE(full_load == test1);
// // REQUIRE(immer_archive::to_json_with_archive(full_load).first ==
// // "");
// }
// }
namespace {
struct recursive_type
{
int data;
immer_archive::archivable<
vector_one<immer_archive::archivable<immer::box<recursive_type>>>>
children;

template <class Archive>
void serialize(Archive& ar)
{
ar(CEREAL_NVP(data), CEREAL_NVP(children));
}

auto tie() const { return std::tie(data, children); }

friend bool operator==(const recursive_type& left,
const recursive_type& right)
{
return left.tie() == right.tie();
}
};

auto get_archives_types(const recursive_type&)
{
auto names = hana::make_map(
hana::make_pair(hana::type_c<immer::box<recursive_type>>,
BOOST_HANA_STRING("boxes")),
hana::make_pair(
hana::type_c<vector_one<
immer_archive::archivable<immer::box<recursive_type>>>>,
BOOST_HANA_STRING("vectors"))

);
return names;
}
} // namespace

TEST_CASE("Test recursive type")
{
const auto v1 = recursive_type{
.data = 123,
};
const auto v2 = recursive_type{
.data = 234,
};
const auto v3 = recursive_type{
.data = 345,
.children = {immer::box<recursive_type>{v1},
immer::box<recursive_type>{v2}},
};

const auto [json_str, archives] = immer_archive::to_json_with_archive(v3);
// REQUIRE(json_str == "");

{
auto full_load =
immer_archive::from_json_with_archive<recursive_type>(json_str);
REQUIRE(full_load == v3);
REQUIRE(immer_archive::to_json_with_archive(full_load).first ==
json_str);
}
}

TEST_CASE("Test recursive type, saving the box triggers saving the box of the "
"same type")
{
const auto v1 = recursive_type{
.data = 123,
};
const auto v2 = recursive_type{
.data = 234,
};
const auto v3 = recursive_type{
.data = 345,
.children = {immer::box<recursive_type>{v1},
immer::box<recursive_type>{v2}},
};
// NOTE: v3 is boxed and inside of it, it contains more boxed values.
const auto v4 = recursive_type{
.data = 456,
.children = {immer::box<recursive_type>{v1},
immer::box<recursive_type>{v3}},
};

const auto [json_str, archives] = immer_archive::to_json_with_archive(v4);
// REQUIRE(json_str == "");

{
auto full_load =
immer_archive::from_json_with_archive<recursive_type>(json_str);
REQUIRE(full_load == v4);
// XXX This must pass:
// REQUIRE(immer_archive::to_json_with_archive(full_load).first ==
// json_str);
}
}

0 comments on commit e444c4a

Please sign in to comment.