From 395118de920b22737dea14db7bd2340f95d263b9 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 3 Dec 2023 20:03:00 +0100 Subject: [PATCH] Rewrote the tests for flexbuffers --- tests/flexbuffers/CMakeLists.txt | 33 +++++++++ tests/flexbuffers/compile_on_docker.sh | 2 +- tests/flexbuffers/test_all_of.cpp | 31 ++++++++ tests/flexbuffers/test_all_of.hpp | 31 +------- tests/flexbuffers/test_anonymous_fields.cpp | 57 ++++++++++++++ tests/flexbuffers/test_anonymous_fields.hpp | 57 +------------- tests/flexbuffers/test_box.cpp | 47 ++++++++++++ tests/flexbuffers/test_box.hpp | 47 +----------- tests/flexbuffers/test_custom_class1.cpp | 42 +++++++++++ tests/flexbuffers/test_custom_class1.hpp | 42 +---------- tests/flexbuffers/test_custom_class2.cpp | 69 +++++++++++++++++ tests/flexbuffers/test_custom_class2.hpp | 69 +---------------- tests/flexbuffers/test_custom_class3.cpp | 70 ++++++++++++++++++ tests/flexbuffers/test_custom_class3.hpp | 70 +----------------- tests/flexbuffers/test_custom_constructor.cpp | 59 +++++++++++++++ tests/flexbuffers/test_custom_constructor.hpp | 59 +-------------- tests/flexbuffers/test_deque.cpp | 34 +++++++++ tests/flexbuffers/test_deque.hpp | 34 +-------- tests/flexbuffers/test_field_variant.cpp | 40 ++++++++++ tests/flexbuffers/test_field_variant.hpp | 40 +--------- tests/flexbuffers/test_flatten_anonymous.cpp | 39 ++++++++++ tests/flexbuffers/test_flatten_anonymous.hpp | 39 +--------- tests/flexbuffers/test_forward_list.cpp | 34 +++++++++ tests/flexbuffers/test_forward_list.hpp | 34 +-------- tests/flexbuffers/test_list.cpp | 34 +++++++++ tests/flexbuffers/test_list.hpp | 34 +-------- tests/flexbuffers/test_literal.cpp | 31 ++++++++ tests/flexbuffers/test_literal.hpp | 31 +------- tests/flexbuffers/test_map.cpp | 34 +++++++++ tests/flexbuffers/test_map.hpp | 34 +-------- tests/flexbuffers/test_monster_example1.cpp | 67 +++++++++++++++++ tests/flexbuffers/test_monster_example1.hpp | 67 +---------------- tests/flexbuffers/test_monster_example2.cpp | 67 +++++++++++++++++ tests/flexbuffers/test_monster_example2.hpp | 67 +---------------- tests/flexbuffers/test_multimap.cpp | 34 +++++++++ tests/flexbuffers/test_multimap.hpp | 34 +-------- tests/flexbuffers/test_multiset.cpp | 32 ++++++++ tests/flexbuffers/test_multiset.hpp | 32 +------- tests/flexbuffers/test_optional_fields.cpp | 36 +++++++++ tests/flexbuffers/test_optional_fields.hpp | 36 +-------- tests/flexbuffers/test_readme_example.cpp | 60 +++++++++++++++ tests/flexbuffers/test_readme_example.hpp | 60 +-------------- tests/flexbuffers/test_ref.cpp | 47 ++++++++++++ tests/flexbuffers/test_ref.hpp | 47 +----------- tests/flexbuffers/test_save_load.cpp | 74 +++++++++++++++++++ tests/flexbuffers/test_save_load.hpp | 74 +------------------ tests/flexbuffers/test_set.cpp | 32 ++++++++ tests/flexbuffers/test_set.hpp | 32 +------- tests/flexbuffers/test_string_map.cpp | 25 +++++++ tests/flexbuffers/test_string_map.hpp | 25 +------ .../flexbuffers/test_string_unordered_map.cpp | 30 ++++++++ .../flexbuffers/test_string_unordered_map.hpp | 30 +------- tests/flexbuffers/test_tagged_union.cpp | 37 ++++++++++ tests/flexbuffers/test_tagged_union.hpp | 37 +--------- tests/flexbuffers/test_unique_ptr.cpp | 35 +++++++++ tests/flexbuffers/test_unique_ptr.hpp | 35 +-------- tests/flexbuffers/test_unique_ptr2.cpp | 47 ++++++++++++ tests/flexbuffers/test_unique_ptr2.hpp | 47 +----------- tests/flexbuffers/test_unordered_map.cpp | 38 ++++++++++ tests/flexbuffers/test_unordered_map.hpp | 38 +--------- tests/flexbuffers/test_unordered_multimap.cpp | 38 ++++++++++ tests/flexbuffers/test_unordered_multimap.hpp | 38 +--------- tests/flexbuffers/test_unordered_multiset.cpp | 36 +++++++++ tests/flexbuffers/test_unordered_multiset.hpp | 36 +-------- tests/flexbuffers/test_unordered_set.cpp | 36 +++++++++ tests/flexbuffers/test_unordered_set.hpp | 36 +-------- tests/flexbuffers/test_variant.cpp | 37 ++++++++++ tests/flexbuffers/test_variant.hpp | 37 +--------- 68 files changed, 1562 insertions(+), 1331 deletions(-) create mode 100644 tests/flexbuffers/test_all_of.cpp create mode 100644 tests/flexbuffers/test_anonymous_fields.cpp create mode 100644 tests/flexbuffers/test_box.cpp create mode 100644 tests/flexbuffers/test_custom_class1.cpp create mode 100644 tests/flexbuffers/test_custom_class2.cpp create mode 100644 tests/flexbuffers/test_custom_class3.cpp create mode 100644 tests/flexbuffers/test_custom_constructor.cpp create mode 100644 tests/flexbuffers/test_deque.cpp create mode 100644 tests/flexbuffers/test_field_variant.cpp create mode 100644 tests/flexbuffers/test_flatten_anonymous.cpp create mode 100644 tests/flexbuffers/test_forward_list.cpp create mode 100644 tests/flexbuffers/test_list.cpp create mode 100644 tests/flexbuffers/test_literal.cpp create mode 100644 tests/flexbuffers/test_map.cpp create mode 100644 tests/flexbuffers/test_monster_example1.cpp create mode 100644 tests/flexbuffers/test_monster_example2.cpp create mode 100644 tests/flexbuffers/test_multimap.cpp create mode 100644 tests/flexbuffers/test_multiset.cpp create mode 100644 tests/flexbuffers/test_optional_fields.cpp create mode 100644 tests/flexbuffers/test_readme_example.cpp create mode 100644 tests/flexbuffers/test_ref.cpp create mode 100644 tests/flexbuffers/test_save_load.cpp create mode 100644 tests/flexbuffers/test_set.cpp create mode 100644 tests/flexbuffers/test_string_map.cpp create mode 100644 tests/flexbuffers/test_string_unordered_map.cpp create mode 100644 tests/flexbuffers/test_tagged_union.cpp create mode 100644 tests/flexbuffers/test_unique_ptr.cpp create mode 100644 tests/flexbuffers/test_unique_ptr2.cpp create mode 100644 tests/flexbuffers/test_unordered_map.cpp create mode 100644 tests/flexbuffers/test_unordered_multimap.cpp create mode 100644 tests/flexbuffers/test_unordered_multiset.cpp create mode 100644 tests/flexbuffers/test_unordered_set.cpp create mode 100644 tests/flexbuffers/test_variant.cpp diff --git a/tests/flexbuffers/CMakeLists.txt b/tests/flexbuffers/CMakeLists.txt index 6c65cd8a..7b0212ad 100644 --- a/tests/flexbuffers/CMakeLists.txt +++ b/tests/flexbuffers/CMakeLists.txt @@ -10,6 +10,39 @@ include_directories( add_executable( reflect-cpp-tests "tests.cpp" + "test_all_of.cpp" + "test_anonymous_fields.cpp" + "test_box.cpp" + "test_custom_class1.cpp" + "test_custom_class2.cpp" + "test_custom_class3.cpp" + "test_custom_constructor.cpp" + "test_deque.cpp" + "test_field_variant.cpp" + "test_flatten_anonymous.cpp" + "test_forward_list.cpp" + "test_list.cpp" + "test_literal.cpp" + "test_map.cpp" + "test_monster_example1.cpp" + "test_monster_example2.cpp" + "test_multimap.cpp" + "test_multiset.cpp" + "test_optional_fields.cpp" + "test_readme_example.cpp" + "test_ref.cpp" + "test_save_load.cpp" + "test_set.cpp" + "test_string_map.cpp" + "test_string_unordered_map.cpp" + "test_tagged_union.cpp" + "test_unique_ptr.cpp" + "test_unique_ptr2.cpp" + "test_unordered_map.cpp" + "test_unordered_multimap.cpp" + "test_unordered_multiset.cpp" + "test_unordered_set.cpp" + "test_variant.cpp" "../../src/yyjson.c" ) diff --git a/tests/flexbuffers/compile_on_docker.sh b/tests/flexbuffers/compile_on_docker.sh index 6a6277af..d9b05ed4 100644 --- a/tests/flexbuffers/compile_on_docker.sh +++ b/tests/flexbuffers/compile_on_docker.sh @@ -2,4 +2,4 @@ cd reflect-cpp/tests/flexbuffers mkdir build cd build cmake .. -DCMAKE_C_COMPILER=gcc-11 -DCMAKE_CXX_COMPILER=g++-11 -make +make -j4 diff --git a/tests/flexbuffers/test_all_of.cpp b/tests/flexbuffers/test_all_of.cpp new file mode 100644 index 00000000..13cf70db --- /dev/null +++ b/tests/flexbuffers/test_all_of.cpp @@ -0,0 +1,31 @@ +#include "test_all_of.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_all_of { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + using Age = rfl::Validator, rfl::Maximum<130>>>; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"age", Age> age; + }; + + const auto homer = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; + + write_and_read(homer); +} +} // namespace test_all_of diff --git a/tests/flexbuffers/test_all_of.hpp b/tests/flexbuffers/test_all_of.hpp index 45049094..082d508a 100644 --- a/tests/flexbuffers/test_all_of.hpp +++ b/tests/flexbuffers/test_all_of.hpp @@ -1,29 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_all_of { - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - using Age = rfl::Validator, rfl::Maximum<130>>>; - - struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"age", Age> age; - }; - - const auto homer = - Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}; - - write_and_read(homer); +namespace test_all_of{ + void test(); } -} // namespace test_all_of + \ No newline at end of file diff --git a/tests/flexbuffers/test_anonymous_fields.cpp b/tests/flexbuffers/test_anonymous_fields.cpp new file mode 100644 index 00000000..8ff2a704 --- /dev/null +++ b/tests/flexbuffers/test_anonymous_fields.cpp @@ -0,0 +1,57 @@ +#include "test_anonymous_fields.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_anonymous_fields { + +using Age = rfl::Validator, rfl::Maximum<130>>>; + +struct Person { + std::string first_name; + std::string last_name; + rfl::Timestamp<"%Y-%m-%d"> birthday; + Age age; + rfl::Email email; + std::vector children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com"}; + + const auto lisa = Person{.first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com"}; + + const auto maggie = Person{.first_name = "Maggie", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 0, + .email = "maggie@simpson.com"}; + + const auto homer = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_anonymous_fields diff --git a/tests/flexbuffers/test_anonymous_fields.hpp b/tests/flexbuffers/test_anonymous_fields.hpp index 5f6a9cad..16c47d36 100644 --- a/tests/flexbuffers/test_anonymous_fields.hpp +++ b/tests/flexbuffers/test_anonymous_fields.hpp @@ -1,55 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_anonymous_fields { - -using Age = rfl::Validator, rfl::Maximum<130>>>; - -struct Person { - std::string first_name; - std::string last_name; - rfl::Timestamp<"%Y-%m-%d"> birthday; - Age age; - rfl::Email email; - std::vector children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = "Bart", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 10, - .email = "bart@simpson.com"}; - - const auto lisa = Person{.first_name = "Lisa", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 8, - .email = "lisa@simpson.com"}; - - const auto maggie = Person{.first_name = "Maggie", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 0, - .email = "maggie@simpson.com"}; - - const auto homer = - Person{.first_name = "Homer", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 45, - .email = "homer@simpson.com", - .children = std::vector({bart, lisa, maggie})}; - - write_and_read(homer); +namespace test_anonymous_fields{ + void test(); } -} // namespace test_anonymous_fields + \ No newline at end of file diff --git a/tests/flexbuffers/test_box.cpp b/tests/flexbuffers/test_box.cpp new file mode 100644 index 00000000..00625393 --- /dev/null +++ b/tests/flexbuffers/test_box.cpp @@ -0,0 +1,47 @@ +#include "test_box.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_box { + +struct DecisionTree { + struct Leaf { + rfl::Field<"value", double> value; + }; + + struct Node { + rfl::Field<"criticalValue", double> critical_value; + rfl::Field<"left", rfl::Box> lesser; + rfl::Field<"right", rfl::Box> greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = rfl::make_box(DecisionTree{leaf1}), + .greater = rfl::make_box(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); +} +} // namespace test_box diff --git a/tests/flexbuffers/test_box.hpp b/tests/flexbuffers/test_box.hpp index a886056b..a564b9e1 100644 --- a/tests/flexbuffers/test_box.hpp +++ b/tests/flexbuffers/test_box.hpp @@ -1,45 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_box { - -struct DecisionTree { - struct Leaf { - rfl::Field<"value", double> value; - }; - - struct Node { - rfl::Field<"criticalValue", double> critical_value; - rfl::Field<"left", rfl::Box> lesser; - rfl::Field<"right", rfl::Box> greater; - }; - - using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; - - rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto leaf1 = DecisionTree::Leaf{.value = 3.0}; - - auto leaf2 = DecisionTree::Leaf{.value = 5.0}; - - auto node = DecisionTree::Node{ - .critical_value = 10.0, - .lesser = rfl::make_box(DecisionTree{leaf1}), - .greater = rfl::make_box(DecisionTree{leaf2})}; - - const DecisionTree tree{.leaf_or_node = std::move(node)}; - - write_and_read(tree); +namespace test_box{ + void test(); } -} // namespace test_box + \ No newline at end of file diff --git a/tests/flexbuffers/test_custom_class1.cpp b/tests/flexbuffers/test_custom_class1.cpp new file mode 100644 index 00000000..c0685f84 --- /dev/null +++ b/tests/flexbuffers/test_custom_class1.cpp @@ -0,0 +1,42 @@ +#include "test_custom_class1.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class1 { + +struct Person { + struct PersonImpl { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::vector> children = rfl::default_value; + }; + + using ReflectionType = PersonImpl; + + Person(const PersonImpl& _impl) : impl(_impl) {} + + Person(const std::string& _first_name) + : impl(PersonImpl{.first_name = _first_name}) {} + + const ReflectionType& reflection() const { return impl; }; + + private: + PersonImpl impl; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person("Bart"); + + write_and_read(bart); +} +} // namespace test_custom_class1 diff --git a/tests/flexbuffers/test_custom_class1.hpp b/tests/flexbuffers/test_custom_class1.hpp index cdbfc4e6..eafe6cd0 100644 --- a/tests/flexbuffers/test_custom_class1.hpp +++ b/tests/flexbuffers/test_custom_class1.hpp @@ -1,40 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_class1 { - -struct Person { - struct PersonImpl { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::vector> children = rfl::default_value; - }; - - using ReflectionType = PersonImpl; - - Person(const PersonImpl& _impl) : impl(_impl) {} - - Person(const std::string& _first_name) - : impl(PersonImpl{.first_name = _first_name}) {} - - const ReflectionType& reflection() const { return impl; }; - - private: - PersonImpl impl; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person("Bart"); - - write_and_read(bart); +namespace test_custom_class1{ + void test(); } -} // namespace test_custom_class1 + \ No newline at end of file diff --git a/tests/flexbuffers/test_custom_class2.cpp b/tests/flexbuffers/test_custom_class2.cpp new file mode 100644 index 00000000..6e8487c5 --- /dev/null +++ b/tests/flexbuffers/test_custom_class2.cpp @@ -0,0 +1,69 @@ +#include "test_custom_class2.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class2 { + +struct Person { + Person(const std::string& _first_name, const std::string& _last_name, + const int _age) + : first_name_(_first_name), last_name_(_last_name), age_(_age) {} + + const auto& first_name() const { return first_name_; } + + const auto& last_name() const { return last_name_; } + + auto age() const { return age_; } + + private: + std::string first_name_; + std::string last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"age", int> age; + + static PersonImpl from_class(const Person& _p) noexcept { + return PersonImpl{.first_name = _p.first_name(), + .last_name = _p.last_name(), + .age = _p.age()}; + } + + Person to_class() const { return Person(first_name(), last_name(), age()); } +}; + +} // namespace test_custom_class2 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // namespace parsing +} // namespace rfl + +namespace test_custom_class2 { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = test_custom_class2::Person("Bart", "Simpson", 10); + + write_and_read(bart); +} + +} // namespace test_custom_class2 diff --git a/tests/flexbuffers/test_custom_class2.hpp b/tests/flexbuffers/test_custom_class2.hpp index 161900a7..21cfbd51 100644 --- a/tests/flexbuffers/test_custom_class2.hpp +++ b/tests/flexbuffers/test_custom_class2.hpp @@ -1,67 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_class2 { - -struct Person { - Person(const std::string& _first_name, const std::string& _last_name, - const int _age) - : first_name_(_first_name), last_name_(_last_name), age_(_age) {} - - const auto& first_name() const { return first_name_; } - - const auto& last_name() const { return last_name_; } - - auto age() const { return age_; } - - private: - std::string first_name_; - std::string last_name_; - int age_; -}; - -struct PersonImpl { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"age", int> age; - - static PersonImpl from_class(const Person& _p) noexcept { - return PersonImpl{.first_name = _p.first_name(), - .last_name = _p.last_name(), - .age = _p.age()}; - } - - Person to_class() const { return Person(first_name(), last_name(), age()); } -}; - -} // namespace test_custom_class2 - -namespace rfl { -namespace parsing { - -template -struct Parser - : public CustomParser {}; - -} // namespace parsing -} // namespace rfl - -namespace test_custom_class2 { - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = test_custom_class2::Person("Bart", "Simpson", 10); - - write_and_read(bart); +namespace test_custom_class2{ + void test(); } - -} // namespace test_custom_class2 + \ No newline at end of file diff --git a/tests/flexbuffers/test_custom_class3.cpp b/tests/flexbuffers/test_custom_class3.cpp new file mode 100644 index 00000000..fdd799d4 --- /dev/null +++ b/tests/flexbuffers/test_custom_class3.cpp @@ -0,0 +1,70 @@ +#include "test_custom_class3.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_class3 { + +struct Person { + Person(const std::string& _first_name, + const rfl::Box& _last_name, int _age) + : first_name_(_first_name), + last_name_(rfl::make_box(*_last_name)), + age_(_age) {} + + const auto& first_name() const { return first_name_; } + + const auto& last_name() const { return last_name_; } + + auto age() const { return age_; } + + private: + std::string first_name_; + rfl::Box last_name_; + int age_; +}; + +struct PersonImpl { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; + + static PersonImpl from_class(const Person& _p) noexcept { + return PersonImpl{.first_name = _p.first_name(), + .last_name = rfl::make_box(*_p.last_name()), + .age = _p.age()}; + } +}; + +} // namespace test_custom_class3 + +namespace rfl { +namespace parsing { + +template +struct Parser + : public CustomParser {}; + +} // namespace parsing +} // namespace rfl + +namespace test_custom_class3 { + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = test_custom_class3::Person( + "Bart", rfl::make_box("Simpson"), 10); + + write_and_read(bart); +} + +} // namespace test_custom_class3 diff --git a/tests/flexbuffers/test_custom_class3.hpp b/tests/flexbuffers/test_custom_class3.hpp index 00d2eb5a..9a6fdab4 100644 --- a/tests/flexbuffers/test_custom_class3.hpp +++ b/tests/flexbuffers/test_custom_class3.hpp @@ -1,68 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_class3 { - -struct Person { - Person(const std::string& _first_name, - const rfl::Box& _last_name, int _age) - : first_name_(_first_name), - last_name_(rfl::make_box(*_last_name)), - age_(_age) {} - - const auto& first_name() const { return first_name_; } - - const auto& last_name() const { return last_name_; } - - auto age() const { return age_; } - - private: - std::string first_name_; - rfl::Box last_name_; - int age_; -}; - -struct PersonImpl { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", rfl::Box> last_name; - rfl::Field<"age", int> age; - - static PersonImpl from_class(const Person& _p) noexcept { - return PersonImpl{.first_name = _p.first_name(), - .last_name = rfl::make_box(*_p.last_name()), - .age = _p.age()}; - } -}; - -} // namespace test_custom_class3 - -namespace rfl { -namespace parsing { - -template -struct Parser - : public CustomParser {}; - -} // namespace parsing -} // namespace rfl - -namespace test_custom_class3 { - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = test_custom_class3::Person( - "Bart", rfl::make_box("Simpson"), 10); - - write_and_read(bart); +namespace test_custom_class3{ + void test(); } - -} // namespace test_custom_class3 + \ No newline at end of file diff --git a/tests/flexbuffers/test_custom_constructor.cpp b/tests/flexbuffers/test_custom_constructor.cpp new file mode 100644 index 00000000..bad09e24 --- /dev/null +++ b/tests/flexbuffers/test_custom_constructor.cpp @@ -0,0 +1,59 @@ +#include "test_custom_constructor.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_custom_constructor { + +struct Person { + static rfl::Result from_flexbuf( + typename rfl::flexbuf::Reader::InputVarType _obj) { + /// This only exists for the purpose of the test. + const auto change_first_name = [](auto&& _person) { + return rfl::replace(std::move(_person), + rfl::Field<"firstName", std::string>("Bart")); + }; + const auto from_nt = [](auto&& _nt) { + return rfl::from_named_tuple(std::move(_nt)); + }; + return rfl::flexbuf::read>(_obj) + .transform(from_nt) + .transform(change_first_name); + } + + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto person1 = Person{ + .first_name = "Homer", .last_name = "Simpson", .birthday = "1987-04-19"}; + + const auto bytes = rfl::flexbuf::write(person1); + + const auto res = rfl::flexbuf::read(bytes); + + if (!res) { + std::cout << "Test failed on read. Error: " << res.error().value().what() + << std::endl; + return; + } + + if (res.value().first_name() != "Bart") { + std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'" + << std::endl + << std::endl; + } else { + std::cout << "OK" << std::endl << std::endl; + } +} +} // namespace test_custom_constructor diff --git a/tests/flexbuffers/test_custom_constructor.hpp b/tests/flexbuffers/test_custom_constructor.hpp index dda87d79..013721d0 100644 --- a/tests/flexbuffers/test_custom_constructor.hpp +++ b/tests/flexbuffers/test_custom_constructor.hpp @@ -1,57 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_custom_constructor { - -struct Person { - static rfl::Result from_flexbuf( - typename rfl::flexbuf::Reader::InputVarType _obj) { - /// This only exists for the purpose of the test. - const auto change_first_name = [](auto&& _person) { - return rfl::replace(std::move(_person), - rfl::Field<"firstName", std::string>("Bart")); - }; - const auto from_nt = [](auto&& _nt) { - return rfl::from_named_tuple(std::move(_nt)); - }; - return rfl::flexbuf::read>(_obj) - .transform(from_nt) - .transform(change_first_name); - } - - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto person1 = Person{ - .first_name = "Homer", .last_name = "Simpson", .birthday = "1987-04-19"}; - - const auto bytes = rfl::flexbuf::write(person1); - - const auto res = rfl::flexbuf::read(bytes); - - if (!res) { - std::cout << "Test failed on read. Error: " << res.error().value().what() - << std::endl; - return; - } - - if (res.value().first_name() != "Bart") { - std::cout << "Expected 'Bart', got '" << res.value().first_name() << "'" - << std::endl - << std::endl; - } else { - std::cout << "OK" << std::endl << std::endl; - } +namespace test_custom_constructor{ + void test(); } -} // namespace test_custom_constructor + \ No newline at end of file diff --git a/tests/flexbuffers/test_deque.cpp b/tests/flexbuffers/test_deque.cpp new file mode 100644 index 00000000..7e1d1139 --- /dev/null +++ b/tests/flexbuffers/test_deque.cpp @@ -0,0 +1,34 @@ +#include "test_deque.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_deque { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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); +} +} // namespace test_deque diff --git a/tests/flexbuffers/test_deque.hpp b/tests/flexbuffers/test_deque.hpp index e18fb7c9..6781e880 100644 --- a/tests/flexbuffers/test_deque.hpp +++ b/tests/flexbuffers/test_deque.hpp @@ -1,32 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_deque { - -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; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - 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); +namespace test_deque{ + void test(); } -} // namespace test_deque + \ No newline at end of file diff --git a/tests/flexbuffers/test_field_variant.cpp b/tests/flexbuffers/test_field_variant.cpp new file mode 100644 index 00000000..e87ce872 --- /dev/null +++ b/tests/flexbuffers/test_field_variant.cpp @@ -0,0 +1,40 @@ +#include "test_field_variant.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_field_variant { + +struct Circle { + rfl::Field<"radius", double> radius; +}; + +struct Rectangle { + rfl::Field<"height", double> height; + rfl::Field<"width", double> width; +}; + +struct Square { + rfl::Field<"width", double> width; +}; + +using Shapes = rfl::Variant, + rfl::Field<"rectangle", Rectangle>, + rfl::Field<"square", rfl::Box>>; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Shapes r = + rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); + + write_and_read(r); +} +} // namespace test_field_variant diff --git a/tests/flexbuffers/test_field_variant.hpp b/tests/flexbuffers/test_field_variant.hpp index 4cf1f508..ba93e732 100644 --- a/tests/flexbuffers/test_field_variant.hpp +++ b/tests/flexbuffers/test_field_variant.hpp @@ -1,38 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_field_variant { - -struct Circle { - rfl::Field<"radius", double> radius; -}; - -struct Rectangle { - rfl::Field<"height", double> height; - rfl::Field<"width", double> width; -}; - -struct Square { - rfl::Field<"width", double> width; -}; - -using Shapes = rfl::Variant, - rfl::Field<"rectangle", Rectangle>, - rfl::Field<"square", rfl::Box>>; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const Shapes r = - rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); - - write_and_read(r); +namespace test_field_variant{ + void test(); } -} // namespace test_field_variant + \ No newline at end of file diff --git a/tests/flexbuffers/test_flatten_anonymous.cpp b/tests/flexbuffers/test_flatten_anonymous.cpp new file mode 100644 index 00000000..7db468dd --- /dev/null +++ b/tests/flexbuffers/test_flatten_anonymous.cpp @@ -0,0 +1,39 @@ +#include "test_flatten_anonymous.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_flatten_anonymous { + +struct Person { + std::string first_name; + rfl::Box last_name; + int age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Box employer; + float salary; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto employee = Employee{ + .person = Person{.first_name = "Homer", + .last_name = rfl::make_box("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + write_and_read(employee); +} +} // namespace test_flatten_anonymous diff --git a/tests/flexbuffers/test_flatten_anonymous.hpp b/tests/flexbuffers/test_flatten_anonymous.hpp index 656b9697..7ffa2785 100644 --- a/tests/flexbuffers/test_flatten_anonymous.hpp +++ b/tests/flexbuffers/test_flatten_anonymous.hpp @@ -1,37 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_flatten_anonymous { - -struct Person { - std::string first_name; - rfl::Box last_name; - int age; -}; - -struct Employee { - rfl::Flatten person; - rfl::Box employer; - float salary; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto employee = Employee{ - .person = Person{.first_name = "Homer", - .last_name = rfl::make_box("Simpson"), - .age = 45}, - .employer = rfl::make_box("Mr. Burns"), - .salary = 60000.0}; - - write_and_read(employee); +namespace test_flatten_anonymous{ + void test(); } -} // namespace test_flatten_anonymous + \ No newline at end of file diff --git a/tests/flexbuffers/test_forward_list.cpp b/tests/flexbuffers/test_forward_list.cpp new file mode 100644 index 00000000..4743e019 --- /dev/null +++ b/tests/flexbuffers/test_forward_list.cpp @@ -0,0 +1,34 @@ +#include "test_forward_list.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_forward_list { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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); +} +} // namespace test_forward_list diff --git a/tests/flexbuffers/test_forward_list.hpp b/tests/flexbuffers/test_forward_list.hpp index 833e8a30..9784a0c4 100644 --- a/tests/flexbuffers/test_forward_list.hpp +++ b/tests/flexbuffers/test_forward_list.hpp @@ -1,32 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_forward_list { - -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; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - 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); +namespace test_forward_list{ + void test(); } -} // namespace test_forward_list + \ No newline at end of file diff --git a/tests/flexbuffers/test_list.cpp b/tests/flexbuffers/test_list.cpp new file mode 100644 index 00000000..5190d244 --- /dev/null +++ b/tests/flexbuffers/test_list.cpp @@ -0,0 +1,34 @@ +#include "test_list.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_list { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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); +} +} // namespace test_list diff --git a/tests/flexbuffers/test_list.hpp b/tests/flexbuffers/test_list.hpp index 22f4eb60..956d36cc 100644 --- a/tests/flexbuffers/test_list.hpp +++ b/tests/flexbuffers/test_list.hpp @@ -1,32 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_list { - -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; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - 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); +namespace test_list{ + void test(); } -} // namespace test_list + \ No newline at end of file diff --git a/tests/flexbuffers/test_literal.cpp b/tests/flexbuffers/test_literal.cpp new file mode 100644 index 00000000..af92ef3e --- /dev/null +++ b/tests/flexbuffers/test_literal.cpp @@ -0,0 +1,31 @@ +#include "test_literal.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_literal { + +using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">; +using LastName = rfl::Literal<"Simpson">; + +struct Person { + rfl::Field<"firstName", FirstName> first_name; + rfl::Field<"lastName", LastName> last_name = rfl::default_value; + rfl::Field<"children", std::vector> children = rfl::default_value; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = FirstName::make<"Bart">()}; + + write_and_read(bart); +} +} // namespace test_literal diff --git a/tests/flexbuffers/test_literal.hpp b/tests/flexbuffers/test_literal.hpp index 5ade3bfd..ccd500ef 100644 --- a/tests/flexbuffers/test_literal.hpp +++ b/tests/flexbuffers/test_literal.hpp @@ -1,29 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_literal { - -using FirstName = rfl::Literal<"Homer", "Marge", "Bart", "Lisa", "Maggie">; -using LastName = rfl::Literal<"Simpson">; - -struct Person { - rfl::Field<"firstName", FirstName> first_name; - rfl::Field<"lastName", LastName> last_name = rfl::default_value; - rfl::Field<"children", std::vector> children = rfl::default_value; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = FirstName::make<"Bart">()}; - - write_and_read(bart); +namespace test_literal{ + void test(); } -} // namespace test_literal + \ No newline at end of file diff --git a/tests/flexbuffers/test_map.cpp b/tests/flexbuffers/test_map.cpp new file mode 100644 index 00000000..97a8a74b --- /dev/null +++ b/tests/flexbuffers/test_map.cpp @@ -0,0 +1,34 @@ +#include "test_map.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_map { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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); +} +} // namespace test_map diff --git a/tests/flexbuffers/test_map.hpp b/tests/flexbuffers/test_map.hpp index 4dcc9d6b..9ae49728 100644 --- a/tests/flexbuffers/test_map.hpp +++ b/tests/flexbuffers/test_map.hpp @@ -1,32 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_map { - -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; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - 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); +namespace test_map{ + void test(); } -} // namespace test_map + \ No newline at end of file diff --git a/tests/flexbuffers/test_monster_example1.cpp b/tests/flexbuffers/test_monster_example1.cpp new file mode 100644 index 00000000..3701872c --- /dev/null +++ b/tests/flexbuffers/test_monster_example1.cpp @@ -0,0 +1,67 @@ +#include "test_monster_example1.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_monster_example1 { + +using Color = rfl::Literal<"Red", "Green", "Blue">; + +struct Weapon { + std::string name; + short damage; +}; + +using Equipment = rfl::Variant>; + +struct Vec3 { + float x; + float y; + float z; +}; + +struct Monster { + Vec3 pos; + short mana = 150; + short hp = 100; + std::string name; + bool friendly = false; + std::vector inventory; + Color color = Color::make<"Blue">(); + std::vector weapons; + Equipment equipped; + std::vector path; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto sword = Weapon{.name = "Sword", .damage = 3}; + const auto axe = Weapon{.name = "Axe", .damage = 5}; + + const auto weapons = std::vector({sword, axe}); + + const auto position = Vec3{1.0f, 2.0f, 3.0f}; + + const auto inventory = + std::vector({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); + + const auto orc = Monster{.pos = position, + .mana = 150, + .hp = 80, + .name = "MyMonster", + .inventory = inventory, + .color = Color::make<"Red">(), + .weapons = weapons, + .equipped = rfl::make_field<"weapon">(axe)}; + + write_and_read(orc); +} + +} // namespace test_monster_example1 diff --git a/tests/flexbuffers/test_monster_example1.hpp b/tests/flexbuffers/test_monster_example1.hpp index 0a6bcb00..80cd680d 100644 --- a/tests/flexbuffers/test_monster_example1.hpp +++ b/tests/flexbuffers/test_monster_example1.hpp @@ -1,65 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_monster_example1 { - -using Color = rfl::Literal<"Red", "Green", "Blue">; - -struct Weapon { - std::string name; - short damage; -}; - -using Equipment = rfl::Variant>; - -struct Vec3 { - float x; - float y; - float z; -}; - -struct Monster { - Vec3 pos; - short mana = 150; - short hp = 100; - std::string name; - bool friendly = false; - std::vector inventory; - Color color = Color::make<"Blue">(); - std::vector weapons; - Equipment equipped; - std::vector path; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto sword = Weapon{.name = "Sword", .damage = 3}; - const auto axe = Weapon{.name = "Axe", .damage = 5}; - - const auto weapons = std::vector({sword, axe}); - - const auto position = Vec3{1.0f, 2.0f, 3.0f}; - - const auto inventory = - std::vector({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); - - const auto orc = Monster{.pos = position, - .mana = 150, - .hp = 80, - .name = "MyMonster", - .inventory = inventory, - .color = Color::make<"Red">(), - .weapons = weapons, - .equipped = rfl::make_field<"weapon">(axe)}; - - write_and_read(orc); +namespace test_monster_example1{ + void test(); } - -} // namespace test_monster_example1 + \ No newline at end of file diff --git a/tests/flexbuffers/test_monster_example2.cpp b/tests/flexbuffers/test_monster_example2.cpp new file mode 100644 index 00000000..3cb2ab73 --- /dev/null +++ b/tests/flexbuffers/test_monster_example2.cpp @@ -0,0 +1,67 @@ +#include "test_monster_example2.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_monster_example2 { + +using Color = rfl::Literal<"Red", "Green", "Blue">; + +struct Weapon { + rfl::Field<"name", std::string> name; + rfl::Field<"damage", short> damage; +}; + +using Equipment = rfl::Variant>; + +struct Vec3 { + rfl::Field<"x", float> x; + rfl::Field<"y", float> y; + rfl::Field<"z", float> z; +}; + +struct Monster { + rfl::Field<"pos", Vec3> pos; + rfl::Field<"mana", short> mana = 150; + rfl::Field<"hp", short> hp = 100; + rfl::Field<"name", std::string> name; + rfl::Field<"friendly", bool> friendly = false; + rfl::Field<"inventory", std::vector> inventory; + rfl::Field<"color", Color> color = Color::make<"Blue">(); + rfl::Field<"weapons", std::vector> weapons; + rfl::Field<"equipped", Equipment> equipped; + rfl::Field<"path", std::vector> path; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto sword = Weapon{.name = "Sword", .damage = 3}; + const auto axe = Weapon{.name = "Axe", .damage = 5}; + + const auto weapons = std::vector({sword, axe}); + + const auto position = Vec3{1.0f, 2.0f, 3.0f}; + + const auto inventory = + std::vector({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); + + const auto orc = Monster{.pos = position, + .mana = 150, + .hp = 80, + .name = "MyMonster", + .inventory = inventory, + .color = Color::make<"Red">(), + .weapons = weapons, + .equipped = rfl::make_field<"weapon">(axe), + .path = rfl::default_value}; + + write_and_read(orc); +} +} // namespace test_monster_example2 diff --git a/tests/flexbuffers/test_monster_example2.hpp b/tests/flexbuffers/test_monster_example2.hpp index 459bde52..9d82a452 100644 --- a/tests/flexbuffers/test_monster_example2.hpp +++ b/tests/flexbuffers/test_monster_example2.hpp @@ -1,65 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_monster_example2 { - -using Color = rfl::Literal<"Red", "Green", "Blue">; - -struct Weapon { - rfl::Field<"name", std::string> name; - rfl::Field<"damage", short> damage; -}; - -using Equipment = rfl::Variant>; - -struct Vec3 { - rfl::Field<"x", float> x; - rfl::Field<"y", float> y; - rfl::Field<"z", float> z; -}; - -struct Monster { - rfl::Field<"pos", Vec3> pos; - rfl::Field<"mana", short> mana = 150; - rfl::Field<"hp", short> hp = 100; - rfl::Field<"name", std::string> name; - rfl::Field<"friendly", bool> friendly = false; - rfl::Field<"inventory", std::vector> inventory; - rfl::Field<"color", Color> color = Color::make<"Blue">(); - rfl::Field<"weapons", std::vector> weapons; - rfl::Field<"equipped", Equipment> equipped; - rfl::Field<"path", std::vector> path; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto sword = Weapon{.name = "Sword", .damage = 3}; - const auto axe = Weapon{.name = "Axe", .damage = 5}; - - const auto weapons = std::vector({sword, axe}); - - const auto position = Vec3{1.0f, 2.0f, 3.0f}; - - const auto inventory = - std::vector({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}); - - const auto orc = Monster{.pos = position, - .mana = 150, - .hp = 80, - .name = "MyMonster", - .inventory = inventory, - .color = Color::make<"Red">(), - .weapons = weapons, - .equipped = rfl::make_field<"weapon">(axe), - .path = rfl::default_value}; - - write_and_read(orc); +namespace test_monster_example2{ + void test(); } -} // namespace test_monster_example2 + \ No newline at end of file diff --git a/tests/flexbuffers/test_multimap.cpp b/tests/flexbuffers/test_multimap.cpp new file mode 100644 index 00000000..add150d1 --- /dev/null +++ b/tests/flexbuffers/test_multimap.cpp @@ -0,0 +1,34 @@ +#include "test_multimap.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_multimap { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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); +} +} // namespace test_multimap diff --git a/tests/flexbuffers/test_multimap.hpp b/tests/flexbuffers/test_multimap.hpp index 24e7fe84..81845572 100644 --- a/tests/flexbuffers/test_multimap.hpp +++ b/tests/flexbuffers/test_multimap.hpp @@ -1,32 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_multimap { - -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; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - 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); +namespace test_multimap{ + void test(); } -} // namespace test_multimap + \ No newline at end of file diff --git a/tests/flexbuffers/test_multiset.cpp b/tests/flexbuffers/test_multiset.cpp new file mode 100644 index 00000000..6a5a31ec --- /dev/null +++ b/tests/flexbuffers/test_multiset.cpp @@ -0,0 +1,32 @@ +#include "test_multiset.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_multiset { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>( + std::multiset({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_multiset diff --git a/tests/flexbuffers/test_multiset.hpp b/tests/flexbuffers/test_multiset.hpp index 38a528cf..adfcf186 100644 --- a/tests/flexbuffers/test_multiset.hpp +++ b/tests/flexbuffers/test_multiset.hpp @@ -1,30 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_multiset { - -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; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>( - std::multiset({"Bart", "Lisa", "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read(homer); +namespace test_multiset{ + void test(); } -} // namespace test_multiset + \ No newline at end of file diff --git a/tests/flexbuffers/test_optional_fields.cpp b/tests/flexbuffers/test_optional_fields.cpp new file mode 100644 index 00000000..f15c35a8 --- /dev/null +++ b/tests/flexbuffers/test_optional_fields.cpp @@ -0,0 +1,36 @@ +#include "test_optional_fields.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_optional_fields { + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name = "Simpson"; + rfl::Field<"children", std::optional>> children = + rfl::default_value; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart"}; + + const auto lisa = Person{.first_name = "Lisa"}; + + const auto maggie = Person{.first_name = "Maggie"}; + + const auto homer = + Person{.first_name = "Homer", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_optional_fields diff --git a/tests/flexbuffers/test_optional_fields.hpp b/tests/flexbuffers/test_optional_fields.hpp index c3a11126..640cab95 100644 --- a/tests/flexbuffers/test_optional_fields.hpp +++ b/tests/flexbuffers/test_optional_fields.hpp @@ -1,34 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_optional_fields { - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name = "Simpson"; - rfl::Field<"children", std::optional>> children = - rfl::default_value; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = "Bart"}; - - const auto lisa = Person{.first_name = "Lisa"}; - - const auto maggie = Person{.first_name = "Maggie"}; - - const auto homer = - Person{.first_name = "Homer", - .children = std::vector({bart, lisa, maggie})}; - - write_and_read(homer); +namespace test_optional_fields{ + void test(); } -} // namespace test_optional_fields + \ No newline at end of file diff --git a/tests/flexbuffers/test_readme_example.cpp b/tests/flexbuffers/test_readme_example.cpp new file mode 100644 index 00000000..7bc77c80 --- /dev/null +++ b/tests/flexbuffers/test_readme_example.cpp @@ -0,0 +1,60 @@ +#include "test_readme_example.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_readme_example { + +using Age = rfl::Validator, rfl::Maximum<130>>>; + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; + rfl::Field<"age", Age> age; + rfl::Field<"email", rfl::Email> email; + rfl::Field<"children", std::vector> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com", + .children = std::vector()}; + + const auto lisa = Person{ + .first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com", + .children = rfl::default_value // same as std::vector() + }; + + const auto maggie = + rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")), + rfl::make_field<"email">(std::string("maggie@simpson.com")), + rfl::make_field<"age">(0)); + + const auto homer = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + write_and_read(homer); +} +} // namespace test_readme_example diff --git a/tests/flexbuffers/test_readme_example.hpp b/tests/flexbuffers/test_readme_example.hpp index 32fba941..68c6cf81 100644 --- a/tests/flexbuffers/test_readme_example.hpp +++ b/tests/flexbuffers/test_readme_example.hpp @@ -1,58 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_readme_example { - -using Age = rfl::Validator, rfl::Maximum<130>>>; - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; - rfl::Field<"age", Age> age; - rfl::Field<"email", rfl::Email> email; - rfl::Field<"children", std::vector> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = "Bart", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 10, - .email = "bart@simpson.com", - .children = std::vector()}; - - const auto lisa = Person{ - .first_name = "Lisa", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 8, - .email = "lisa@simpson.com", - .children = rfl::default_value // same as std::vector() - }; - - const auto maggie = - rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")), - rfl::make_field<"email">(std::string("maggie@simpson.com")), - rfl::make_field<"age">(0)); - - const auto homer = - Person{.first_name = "Homer", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 45, - .email = "homer@simpson.com", - .children = std::vector({bart, lisa, maggie})}; - - write_and_read(homer); +namespace test_readme_example{ + void test(); } -} // namespace test_readme_example + \ No newline at end of file diff --git a/tests/flexbuffers/test_ref.cpp b/tests/flexbuffers/test_ref.cpp new file mode 100644 index 00000000..41ceca1c --- /dev/null +++ b/tests/flexbuffers/test_ref.cpp @@ -0,0 +1,47 @@ +#include "test_ref.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_ref { + +struct DecisionTree { + struct Leaf { + rfl::Field<"value", double> value; + }; + + struct Node { + rfl::Field<"criticalValue", double> critical_value; + rfl::Field<"left", rfl::Ref> lesser; + rfl::Field<"right", rfl::Ref> greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = rfl::make_ref(DecisionTree{leaf1}), + .greater = rfl::make_ref(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); +} +} // namespace test_ref diff --git a/tests/flexbuffers/test_ref.hpp b/tests/flexbuffers/test_ref.hpp index 218900a4..d289ba09 100644 --- a/tests/flexbuffers/test_ref.hpp +++ b/tests/flexbuffers/test_ref.hpp @@ -1,45 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_ref { - -struct DecisionTree { - struct Leaf { - rfl::Field<"value", double> value; - }; - - struct Node { - rfl::Field<"criticalValue", double> critical_value; - rfl::Field<"left", rfl::Ref> lesser; - rfl::Field<"right", rfl::Ref> greater; - }; - - using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; - - rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto leaf1 = DecisionTree::Leaf{.value = 3.0}; - - const auto leaf2 = DecisionTree::Leaf{.value = 5.0}; - - auto node = DecisionTree::Node{ - .critical_value = 10.0, - .lesser = rfl::make_ref(DecisionTree{leaf1}), - .greater = rfl::make_ref(DecisionTree{leaf2})}; - - const DecisionTree tree{.leaf_or_node = std::move(node)}; - - write_and_read(tree); +namespace test_ref{ + void test(); } -} // namespace test_ref + \ No newline at end of file diff --git a/tests/flexbuffers/test_save_load.cpp b/tests/flexbuffers/test_save_load.cpp new file mode 100644 index 00000000..9d9d7d7e --- /dev/null +++ b/tests/flexbuffers/test_save_load.cpp @@ -0,0 +1,74 @@ +#include "test_save_load.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_save_load { + +using Age = rfl::Validator, rfl::Maximum<130>>>; + +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; + rfl::Field<"age", Age> age; + rfl::Field<"email", rfl::Email> email; + rfl::Field<"children", std::vector> children; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const auto bart = Person{.first_name = "Bart", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 10, + .email = "bart@simpson.com", + .children = std::vector()}; + + const auto lisa = Person{ + .first_name = "Lisa", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 8, + .email = "lisa@simpson.com", + .children = rfl::default_value // same as std::vector() + }; + + const auto maggie = + rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")), + rfl::make_field<"email">(std::string("maggie@simpson.com")), + rfl::make_field<"age">(0)); + + const auto homer1 = + Person{.first_name = "Homer", + .last_name = "Simpson", + .birthday = "1987-04-19", + .age = 45, + .email = "homer@simpson.com", + .children = std::vector({bart, lisa, maggie})}; + + rfl::flexbuf::save("homer.fb", homer1); + + const auto homer2 = rfl::flexbuf::load("homer.fb").value(); + + const auto bytes1 = rfl::flexbuf::write(homer1); + const auto bytes2 = rfl::flexbuf::write(homer2); + + if (bytes1 != bytes2) { + std::cout << "Test failed. Content was not identical." << std::endl + << std::endl; + return; + } + + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_save_load diff --git a/tests/flexbuffers/test_save_load.hpp b/tests/flexbuffers/test_save_load.hpp index e3fbc7dd..a8829fd1 100644 --- a/tests/flexbuffers/test_save_load.hpp +++ b/tests/flexbuffers/test_save_load.hpp @@ -1,72 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_save_load { - -using Age = rfl::Validator, rfl::Maximum<130>>>; - -struct Person { - rfl::Field<"firstName", std::string> first_name; - rfl::Field<"lastName", std::string> last_name; - rfl::Field<"birthday", rfl::Timestamp<"%Y-%m-%d">> birthday; - rfl::Field<"age", Age> age; - rfl::Field<"email", rfl::Email> email; - rfl::Field<"children", std::vector> children; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - const auto bart = Person{.first_name = "Bart", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 10, - .email = "bart@simpson.com", - .children = std::vector()}; - - const auto lisa = Person{ - .first_name = "Lisa", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 8, - .email = "lisa@simpson.com", - .children = rfl::default_value // same as std::vector() - }; - - const auto maggie = - rfl::replace(lisa, rfl::make_field<"firstName">(std::string("Maggie")), - rfl::make_field<"email">(std::string("maggie@simpson.com")), - rfl::make_field<"age">(0)); - - const auto homer1 = - Person{.first_name = "Homer", - .last_name = "Simpson", - .birthday = "1987-04-19", - .age = 45, - .email = "homer@simpson.com", - .children = std::vector({bart, lisa, maggie})}; - - rfl::flexbuf::save("homer.fb", homer1); - - const auto homer2 = rfl::flexbuf::load("homer.fb").value(); - - const auto bytes1 = rfl::flexbuf::write(homer1); - const auto bytes2 = rfl::flexbuf::write(homer2); - - if (bytes1 != bytes2) { - std::cout << "Test failed. Content was not identical." << std::endl - << std::endl; - return; - } - - std::cout << "OK" << std::endl << std::endl; +namespace test_save_load{ + void test(); } -} // namespace test_save_load + \ No newline at end of file diff --git a/tests/flexbuffers/test_set.cpp b/tests/flexbuffers/test_set.cpp new file mode 100644 index 00000000..4da6eeff --- /dev/null +++ b/tests/flexbuffers/test_set.cpp @@ -0,0 +1,32 @@ +#include "test_set.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_set { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>( + std::set({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + write_and_read(homer); +} +} // namespace test_set diff --git a/tests/flexbuffers/test_set.hpp b/tests/flexbuffers/test_set.hpp index 7ac91c0e..142a663b 100644 --- a/tests/flexbuffers/test_set.hpp +++ b/tests/flexbuffers/test_set.hpp @@ -1,30 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_set { - -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; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>( - std::set({"Bart", "Lisa", "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - write_and_read(homer); +namespace test_set{ + void test(); } -} // namespace test_set + \ No newline at end of file diff --git a/tests/flexbuffers/test_string_map.cpp b/tests/flexbuffers/test_string_map.cpp new file mode 100644 index 00000000..fc2157cb --- /dev/null +++ b/tests/flexbuffers/test_string_map.cpp @@ -0,0 +1,25 @@ +#include "test_string_map.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_string_map { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + std::map> homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + write_and_read(homer); +} +} // namespace test_string_map diff --git a/tests/flexbuffers/test_string_map.hpp b/tests/flexbuffers/test_string_map.hpp index ec6d5997..94cb975a 100644 --- a/tests/flexbuffers/test_string_map.hpp +++ b/tests/flexbuffers/test_string_map.hpp @@ -1,23 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_string_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - std::map> homer; - homer.insert( - std::make_pair("firstName", std::make_unique("Homer"))); - homer.insert( - std::make_pair("lastName", std::make_unique("Simpson"))); - - write_and_read(homer); +namespace test_string_map{ + void test(); } -} // namespace test_string_map + \ No newline at end of file diff --git a/tests/flexbuffers/test_string_unordered_map.cpp b/tests/flexbuffers/test_string_unordered_map.cpp new file mode 100644 index 00000000..07241877 --- /dev/null +++ b/tests/flexbuffers/test_string_unordered_map.cpp @@ -0,0 +1,30 @@ +#include "test_string_unordered_map.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_string_unordered_map { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + using Map = std::unordered_map>; + Map homer; + homer.insert( + std::make_pair("firstName", std::make_unique("Homer"))); + homer.insert( + std::make_pair("lastName", std::make_unique("Simpson"))); + + // Unordered maps are unpredictable. We therefore only make sure that this + // compiles. + const auto json_string = rfl::flexbuf::write(homer); + const auto homer2 = rfl::flexbuf::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_string_unordered_map diff --git a/tests/flexbuffers/test_string_unordered_map.hpp b/tests/flexbuffers/test_string_unordered_map.hpp index 9f510245..268057a3 100644 --- a/tests/flexbuffers/test_string_unordered_map.hpp +++ b/tests/flexbuffers/test_string_unordered_map.hpp @@ -1,28 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_string_unordered_map { -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - using Map = std::unordered_map>; - Map homer; - homer.insert( - std::make_pair("firstName", std::make_unique("Homer"))); - homer.insert( - std::make_pair("lastName", std::make_unique("Simpson"))); - - // Unordered maps are unpredictable. We therefore only make sure that this - // compiles. - const auto json_string = rfl::flexbuf::write(homer); - const auto homer2 = rfl::flexbuf::read(json_string); - std::cout << "OK" << std::endl << std::endl; +namespace test_string_unordered_map{ + void test(); } -} // namespace test_string_unordered_map + \ No newline at end of file diff --git a/tests/flexbuffers/test_tagged_union.cpp b/tests/flexbuffers/test_tagged_union.cpp new file mode 100644 index 00000000..f09d141d --- /dev/null +++ b/tests/flexbuffers/test_tagged_union.cpp @@ -0,0 +1,37 @@ +#include "test_tagged_union.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_tagged_union { + +struct Circle { + double radius; +}; + +struct Rectangle { + double height; + double width; +}; + +struct Square { + double width; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r); +} +} // namespace test_tagged_union diff --git a/tests/flexbuffers/test_tagged_union.hpp b/tests/flexbuffers/test_tagged_union.hpp index 9b18b64d..5d522ff9 100644 --- a/tests/flexbuffers/test_tagged_union.hpp +++ b/tests/flexbuffers/test_tagged_union.hpp @@ -1,35 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_tagged_union { - -struct Circle { - double radius; -}; - -struct Rectangle { - double height; - double width; -}; - -struct Square { - double width; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; - - const Shapes r = Rectangle{.height = 10, .width = 5}; - - write_and_read(r); +namespace test_tagged_union{ + void test(); } -} // namespace test_tagged_union + \ No newline at end of file diff --git a/tests/flexbuffers/test_unique_ptr.cpp b/tests/flexbuffers/test_unique_ptr.cpp new file mode 100644 index 00000000..c093a161 --- /dev/null +++ b/tests/flexbuffers/test_unique_ptr.cpp @@ -0,0 +1,35 @@ +#include "test_unique_ptr.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unique_ptr { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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); +} +} // namespace test_unique_ptr diff --git a/tests/flexbuffers/test_unique_ptr.hpp b/tests/flexbuffers/test_unique_ptr.hpp index 938baae5..428ea2a2 100644 --- a/tests/flexbuffers/test_unique_ptr.hpp +++ b/tests/flexbuffers/test_unique_ptr.hpp @@ -1,33 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unique_ptr { - -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; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - 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); +namespace test_unique_ptr{ + void test(); } -} // namespace test_unique_ptr + \ No newline at end of file diff --git a/tests/flexbuffers/test_unique_ptr2.cpp b/tests/flexbuffers/test_unique_ptr2.cpp new file mode 100644 index 00000000..dd02f9b0 --- /dev/null +++ b/tests/flexbuffers/test_unique_ptr2.cpp @@ -0,0 +1,47 @@ +#include "test_unique_ptr2.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unique_ptr2 { + +struct DecisionTree { + struct Leaf { + rfl::Field<"value", double> value; + }; + + struct Node { + rfl::Field<"criticalValue", double> critical_value; + rfl::Field<"left", std::unique_ptr> lesser; + rfl::Field<"right", std::unique_ptr> greater; + }; + + using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; + + rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto leaf1 = DecisionTree::Leaf{.value = 3.0}; + + auto leaf2 = DecisionTree::Leaf{.value = 5.0}; + + auto node = DecisionTree::Node{ + .critical_value = 10.0, + .lesser = std::make_unique(DecisionTree{leaf1}), + .greater = std::make_unique(DecisionTree{leaf2})}; + + const DecisionTree tree{.leaf_or_node = std::move(node)}; + + write_and_read(tree); +} +} // namespace test_unique_ptr2 diff --git a/tests/flexbuffers/test_unique_ptr2.hpp b/tests/flexbuffers/test_unique_ptr2.hpp index 73936561..74adc170 100644 --- a/tests/flexbuffers/test_unique_ptr2.hpp +++ b/tests/flexbuffers/test_unique_ptr2.hpp @@ -1,45 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unique_ptr2 { - -struct DecisionTree { - struct Leaf { - rfl::Field<"value", double> value; - }; - - struct Node { - rfl::Field<"criticalValue", double> critical_value; - rfl::Field<"left", std::unique_ptr> lesser; - rfl::Field<"right", std::unique_ptr> greater; - }; - - using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>; - - rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto leaf1 = DecisionTree::Leaf{.value = 3.0}; - - auto leaf2 = DecisionTree::Leaf{.value = 5.0}; - - auto node = DecisionTree::Node{ - .critical_value = 10.0, - .lesser = std::make_unique(DecisionTree{leaf1}), - .greater = std::make_unique(DecisionTree{leaf2})}; - - const DecisionTree tree{.leaf_or_node = std::move(node)}; - - write_and_read(tree); +namespace test_unique_ptr2{ + void test(); } -} // namespace test_unique_ptr2 + \ No newline at end of file diff --git a/tests/flexbuffers/test_unordered_map.cpp b/tests/flexbuffers/test_unordered_map.cpp new file mode 100644 index 00000000..f063937a --- /dev/null +++ b/tests/flexbuffers/test_unordered_map.cpp @@ -0,0 +1,38 @@ +#include "test_unordered_map.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unordered_map { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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::flexbuf::write(homer); + const auto homer2 = rfl::flexbuf::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_unordered_map diff --git a/tests/flexbuffers/test_unordered_map.hpp b/tests/flexbuffers/test_unordered_map.hpp index 2fa6a07a..9b059a96 100644 --- a/tests/flexbuffers/test_unordered_map.hpp +++ b/tests/flexbuffers/test_unordered_map.hpp @@ -1,36 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unordered_map { - -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; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - 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::flexbuf::write(homer); - const auto homer2 = rfl::flexbuf::read(json_string); - std::cout << "OK" << std::endl << std::endl; +namespace test_unordered_map{ + void test(); } -} // namespace test_unordered_map + \ No newline at end of file diff --git a/tests/flexbuffers/test_unordered_multimap.cpp b/tests/flexbuffers/test_unordered_multimap.cpp new file mode 100644 index 00000000..d304b26e --- /dev/null +++ b/tests/flexbuffers/test_unordered_multimap.cpp @@ -0,0 +1,38 @@ +#include "test_unordered_multimap.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unordered_multimap { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + 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::flexbuf::write(homer); + const auto homer2 = rfl::flexbuf::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_unordered_multimap diff --git a/tests/flexbuffers/test_unordered_multimap.hpp b/tests/flexbuffers/test_unordered_multimap.hpp index fda91b1a..0f36f705 100644 --- a/tests/flexbuffers/test_unordered_multimap.hpp +++ b/tests/flexbuffers/test_unordered_multimap.hpp @@ -1,36 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unordered_multimap { - -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; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - 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::flexbuf::write(homer); - const auto homer2 = rfl::flexbuf::read(json_string); - std::cout << "OK" << std::endl << std::endl; +namespace test_unordered_multimap{ + void test(); } -} // namespace test_unordered_multimap + \ No newline at end of file diff --git a/tests/flexbuffers/test_unordered_multiset.cpp b/tests/flexbuffers/test_unordered_multiset.cpp new file mode 100644 index 00000000..c72e2bc5 --- /dev/null +++ b/tests/flexbuffers/test_unordered_multiset.cpp @@ -0,0 +1,36 @@ +#include "test_unordered_multiset.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unordered_multiset { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>( + std::unordered_multiset({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + // Unordered multisets are unpredictable. We therefore only make sure that + // this compiles. + const auto json_string = rfl::flexbuf::write(homer); + const auto homer2 = rfl::flexbuf::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_unordered_multiset diff --git a/tests/flexbuffers/test_unordered_multiset.hpp b/tests/flexbuffers/test_unordered_multiset.hpp index d47e54a8..491ff7ba 100644 --- a/tests/flexbuffers/test_unordered_multiset.hpp +++ b/tests/flexbuffers/test_unordered_multiset.hpp @@ -1,34 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unordered_multiset { - -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; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>( - std::unordered_multiset({"Bart", "Lisa", "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - // Unordered multisets are unpredictable. We therefore only make sure that - // this compiles. - const auto json_string = rfl::flexbuf::write(homer); - const auto homer2 = rfl::flexbuf::read(json_string); - std::cout << "OK" << std::endl << std::endl; +namespace test_unordered_multiset{ + void test(); } -} // namespace test_unordered_multiset + \ No newline at end of file diff --git a/tests/flexbuffers/test_unordered_set.cpp b/tests/flexbuffers/test_unordered_set.cpp new file mode 100644 index 00000000..1d9bbbf3 --- /dev/null +++ b/tests/flexbuffers/test_unordered_set.cpp @@ -0,0 +1,36 @@ +#include "test_unordered_set.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_unordered_set { + +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; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + auto children = std::make_unique>( + std::unordered_set({"Bart", "Lisa", "Maggie"})); + + const auto homer = + Person{.first_name = "Homer", .children = std::move(children)}; + + // Unordered sets are unpredictable. We therefore only make sure that this + // compiles. + const auto json_string = rfl::flexbuf::write(homer); + const auto homer2 = rfl::flexbuf::read(json_string); + std::cout << "OK" << std::endl << std::endl; +} +} // namespace test_unordered_set diff --git a/tests/flexbuffers/test_unordered_set.hpp b/tests/flexbuffers/test_unordered_set.hpp index a1d43693..be2b257f 100644 --- a/tests/flexbuffers/test_unordered_set.hpp +++ b/tests/flexbuffers/test_unordered_set.hpp @@ -1,34 +1,4 @@ -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_unordered_set { - -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; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - auto children = std::make_unique>( - std::unordered_set({"Bart", "Lisa", "Maggie"})); - - const auto homer = - Person{.first_name = "Homer", .children = std::move(children)}; - - // Unordered sets are unpredictable. We therefore only make sure that this - // compiles. - const auto json_string = rfl::flexbuf::write(homer); - const auto homer2 = rfl::flexbuf::read(json_string); - std::cout << "OK" << std::endl << std::endl; +namespace test_unordered_set{ + void test(); } -} // namespace test_unordered_set + \ No newline at end of file diff --git a/tests/flexbuffers/test_variant.cpp b/tests/flexbuffers/test_variant.cpp new file mode 100644 index 00000000..2861a3ab --- /dev/null +++ b/tests/flexbuffers/test_variant.cpp @@ -0,0 +1,37 @@ +#include "test_variant.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_variant { + +struct Circle { + rfl::Field<"radius", double> radius; +}; + +struct Rectangle { + rfl::Field<"height", double> height; + rfl::Field<"width", double> width; +}; + +struct Square { + rfl::Field<"width", double> width; +}; + +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + using Shapes = std::variant>; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r); +} +} // namespace test_variant diff --git a/tests/flexbuffers/test_variant.hpp b/tests/flexbuffers/test_variant.hpp index 1b3927ab..0e58ce71 100644 --- a/tests/flexbuffers/test_variant.hpp +++ b/tests/flexbuffers/test_variant.hpp @@ -1,35 +1,4 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "write_and_read.hpp" - -namespace test_variant { - -struct Circle { - rfl::Field<"radius", double> radius; -}; - -struct Rectangle { - rfl::Field<"height", double> height; - rfl::Field<"width", double> width; -}; - -struct Square { - rfl::Field<"width", double> width; -}; - -void test() { - std::cout << std::source_location::current().function_name() << std::endl; - - using Shapes = std::variant>; - - const Shapes r = Rectangle{.height = 10, .width = 5}; - - write_and_read(r); +namespace test_variant{ + void test(); } -} // namespace test_variant + \ No newline at end of file