From 23922db9c59c22e576d1267631d22f81ec0e3c73 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Mon, 9 Oct 2023 22:32:37 +0200 Subject: [PATCH] Added tests for various containers --- include/rfl/parsing/Parser.hpp | 43 ++++++++++++++++++++----- include/rfl/parsing/is_forward_list.hpp | 22 +++++++++++++ include/rfl/parsing/is_map_like.hpp | 26 +++++++++++++++ tests/test_deque.hpp | 30 +++++++++++++++++ tests/test_forward_list.hpp | 30 +++++++++++++++++ tests/test_list.hpp | 30 +++++++++++++++++ tests/test_map.hpp | 30 +++++++++++++++++ tests/test_unordered_map.hpp | 32 ++++++++++++++++++ tests/tests.cpp | 10 ++++++ 9 files changed, 245 insertions(+), 8 deletions(-) create mode 100644 include/rfl/parsing/is_forward_list.hpp create mode 100644 include/rfl/parsing/is_map_like.hpp create mode 100644 tests/test_deque.hpp create mode 100644 tests/test_forward_list.hpp create mode 100644 tests/test_list.hpp create mode 100644 tests/test_map.hpp create mode 100644 tests/test_unordered_map.hpp diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index d0769427..3967108b 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -34,6 +34,8 @@ #include "rfl/internal/no_duplicate_field_names.hpp" #include "rfl/internal/to_ptr_named_tuple.hpp" #include "rfl/named_tuple_t.hpp" +#include "rfl/parsing/is_forward_list.hpp" +#include "rfl/parsing/is_map_like.hpp" #include "rfl/parsing/is_required.hpp" #include "rfl/to_named_tuple.hpp" @@ -533,9 +535,10 @@ struct Parser> { static OutputVarType write( const WriterType& _w, const std::pair& _p) noexcept { - const auto tup = std::make_tuple(_p.first, _p.second); - return Parser>::write(_w, tup); + const auto tup = std::make_tuple(&_p.first, &_p.second); + return Parser< + ReaderType, WriterType, + std::tuple>::write(_w, tup); } }; @@ -699,7 +702,8 @@ struct Parser> { AlreadyExtracted&&... _already_extracted) noexcept { constexpr size_t i = sizeof...(AlreadyExtracted); if constexpr (i == sizeof...(Ts)) { - return std::tuple(std::make_tuple(_already_extracted...)); + return std::tuple( + std::make_tuple(std::move(_already_extracted)...)); } else { const auto extract_next = [&](auto&& new_entry) { return extract_field_by_field(_r, std::move(_vec), @@ -943,12 +947,35 @@ struct VectorParser { .value(); }; + const auto get_pair = [&](auto& _v) { + if constexpr (is_map_like()) { + using K = std::decay_t; + using V = std::decay_t; + return Parser>>::read(_r, &_v) + .value(); + } + }; + const auto to_container = [&](InputArrayType&& _arr) -> Result { auto input_vars = _r.to_vec(&_arr); VecType vec; - for (auto it = input_vars.begin(); it != input_vars.end(); ++it) { - vec.emplace_back(get_elem(*it)); + if constexpr (is_forward_list()) { + for (auto it = input_vars.rbegin(); it != input_vars.rend(); + ++it) { + vec.emplace_front(get_elem(*it)); + } + } else if constexpr (is_map_like()) { + for (auto it = input_vars.begin(); it != input_vars.end(); + ++it) { + vec.insert(get_pair(*it)); + } + } else { + for (auto it = input_vars.begin(); it != input_vars.end(); + ++it) { + vec.emplace_back(get_elem(*it)); + } } return vec; }; @@ -968,9 +995,9 @@ struct VectorParser { static OutputVarType write(const WriterType& _w, const VecType& _vec) noexcept { auto arr = _w.new_array(); - for (auto it = _vec.begin(); it != _vec.end(); ++it) { + for (const auto& v : _vec) { _w.add( - Parser>::write(_w, *it), + Parser>::write(_w, v), &arr); } return OutputVarType(arr); diff --git a/include/rfl/parsing/is_forward_list.hpp b/include/rfl/parsing/is_forward_list.hpp new file mode 100644 index 00000000..2ac6fe79 --- /dev/null +++ b/include/rfl/parsing/is_forward_list.hpp @@ -0,0 +1,22 @@ +#ifndef RFL_PARSING_IS_FORWARD_LIST_HPP_ +#define RFL_PARSING_IS_FORWARD_LIST_HPP_ + +#include +#include + +namespace rfl { +namespace parsing { + +template +class is_forward_list; + +template +class is_forward_list : public std::false_type {}; + +template +class is_forward_list> : public std::true_type {}; + +} // namespace parsing +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/is_map_like.hpp b/include/rfl/parsing/is_map_like.hpp new file mode 100644 index 00000000..c0679bfc --- /dev/null +++ b/include/rfl/parsing/is_map_like.hpp @@ -0,0 +1,26 @@ +#ifndef RFL_PARSING_IS_MAP_LIKE_HPP_ +#define RFL_PARSING_IS_MAP_LIKE_HPP_ + +#include +#include +#include + +namespace rfl { +namespace parsing { + +template +class is_map_like; + +template +class is_map_like : public std::false_type {}; + +template +class is_map_like> : public std::true_type {}; + +template +class is_map_like> : public std::true_type {}; + +} // namespace parsing +} // namespace rfl + +#endif diff --git a/tests/test_deque.hpp b/tests/test_deque.hpp new file mode 100644 index 00000000..78e8fec5 --- /dev/null +++ b/tests/test_deque.hpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_deque() { + std::cout << "test_deque" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::unique_ptr>> children = + rfl::default_value; + }; + + auto children = std::make_unique>(); + children->emplace_back(Person{.first_name = "Bart"}); + children->emplace_back(Person{.first_name = "Lisa"}); + children->emplace_back(Person{.first_name = "Maggie"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +} diff --git a/tests/test_forward_list.hpp b/tests/test_forward_list.hpp new file mode 100644 index 00000000..8ccfdb21 --- /dev/null +++ b/tests/test_forward_list.hpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_forward_list() { + std::cout << "test_forward_list" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::unique_ptr>> + children = rfl::default_value; + }; + + auto children = std::make_unique>(); + children->emplace_front(Person{.first_name = "Maggie"}); + children->emplace_front(Person{.first_name = "Lisa"}); + children->emplace_front(Person{.first_name = "Bart"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +} diff --git a/tests/test_list.hpp b/tests/test_list.hpp new file mode 100644 index 00000000..06de7e72 --- /dev/null +++ b/tests/test_list.hpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_list() { + std::cout << "test_list" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::unique_ptr>> children = + rfl::default_value; + }; + + auto children = std::make_unique>(); + children->emplace_back(Person{.first_name = "Bart"}); + children->emplace_back(Person{.first_name = "Lisa"}); + children->emplace_back(Person{.first_name = "Maggie"}); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +} diff --git a/tests/test_map.hpp b/tests/test_map.hpp new file mode 100644 index 00000000..efb793bf --- /dev/null +++ b/tests/test_map.hpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_map() { + std::cout << "test_map" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::unique_ptr>> + children = rfl::default_value; + }; + + auto children = std::make_unique>(); + children->insert(std::make_pair(1, Person{.first_name = "Bart"})); + children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); + children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[[1,{"firstName":"Bart","lastName":"Simpson"}],[2,{"firstName":"Lisa","lastName":"Simpson"}],[3,{"firstName":"Maggie","lastName":"Simpson"}]]})"); +} diff --git a/tests/test_unordered_map.hpp b/tests/test_unordered_map.hpp new file mode 100644 index 00000000..b91940b1 --- /dev/null +++ b/tests/test_unordered_map.hpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_unordered_map() { + std::cout << "test_unordered_map" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::unique_ptr>> + children = rfl::default_value; + }; + + auto children = std::make_unique>(); + children->insert(std::make_pair(1, Person{.first_name = "Bart"})); + children->insert(std::make_pair(2, Person{.first_name = "Lisa"})); + children->insert(std::make_pair(3, Person{.first_name = "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + // Unordered maps are unpredictable. We therefore only make sure that this + // compiles. + const auto json_string = rfl::json::write(homer); + const auto homer2 = rfl::json::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 5be0b764..8e458819 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -7,9 +7,13 @@ #include "test_custom_class3.hpp" #include "test_custom_class4.hpp" #include "test_default_values.hpp" +#include "test_deque.hpp" #include "test_field_variant.hpp" #include "test_flatten.hpp" +#include "test_forward_list.hpp" +#include "test_list.hpp" #include "test_literal.hpp" +#include "test_map.hpp" #include "test_optional_fields.hpp" #include "test_readme_example.hpp" #include "test_ref.hpp" @@ -20,6 +24,7 @@ #include "test_timestamp.hpp" #include "test_unique_ptr.hpp" #include "test_unique_ptr2.hpp" +#include "test_unordered_map.hpp" #include "test_variant.hpp" int main() { @@ -37,6 +42,11 @@ int main() { test_array(); test_timestamp(); test_flatten(); + test_deque(); + test_forward_list(); + test_list(); + test_map(); + test_unordered_map(); test_custom_class1(); test_custom_class2();