From 629dd0c0368f1f6f1897a7747a286fee3b689942 Mon Sep 17 00:00:00 2001 From: Patrick Urbanke Date: Sun, 12 Nov 2023 00:08:56 +0100 Subject: [PATCH] Added more tests --- include/rfl/flexbuf/Writer.hpp | 4 +- tests/flexbuffers/test_all_of.hpp | 25 ++++++++++ tests/flexbuffers/test_anonymous_fields.hpp | 51 +++++++++++++++++++++ tests/flexbuffers/test_field_variant.hpp | 34 ++++++++++++++ tests/flexbuffers/test_literal.hpp | 26 +++++++++++ tests/flexbuffers/test_optional_fields.hpp | 30 ++++++++++++ tests/flexbuffers/test_tagged_union.hpp | 35 ++++++++++++++ tests/flexbuffers/test_unique_ptr.hpp | 29 ++++++++++++ tests/flexbuffers/test_unique_ptr2.hpp | 43 +++++++++++++++++ tests/flexbuffers/test_variant.hpp | 32 +++++++++++++ tests/flexbuffers/tests.cpp | 19 ++++++++ 11 files changed, 326 insertions(+), 2 deletions(-) create mode 100644 tests/flexbuffers/test_all_of.hpp create mode 100644 tests/flexbuffers/test_anonymous_fields.hpp create mode 100644 tests/flexbuffers/test_field_variant.hpp create mode 100644 tests/flexbuffers/test_literal.hpp create mode 100644 tests/flexbuffers/test_optional_fields.hpp create mode 100644 tests/flexbuffers/test_tagged_union.hpp create mode 100644 tests/flexbuffers/test_unique_ptr.hpp create mode 100644 tests/flexbuffers/test_unique_ptr2.hpp create mode 100644 tests/flexbuffers/test_variant.hpp diff --git a/include/rfl/flexbuf/Writer.hpp b/include/rfl/flexbuf/Writer.hpp index d6bc430f..079d5d84 100644 --- a/include/rfl/flexbuf/Writer.hpp +++ b/include/rfl/flexbuf/Writer.hpp @@ -34,14 +34,14 @@ struct Writer { ~Writer() = default; - void add(const OutputVarType _var, OutputArrayType* _arr) const noexcept { + void add(const OutputVarType& _var, OutputArrayType* _arr) const noexcept { (*_arr)->push_back(_var); } OutputVarType empty_var() const noexcept { return Ref::make(); } template - OutputVarType from_basic_type(const T _var) const noexcept { + OutputVarType from_basic_type(const T& _var) const noexcept { return Ref>::make(_var); } diff --git a/tests/flexbuffers/test_all_of.hpp b/tests/flexbuffers/test_all_of.hpp new file mode 100644 index 00000000..b568c6d0 --- /dev/null +++ b/tests/flexbuffers/test_all_of.hpp @@ -0,0 +1,25 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_all_of() { + std::cout << "test_all_of" << 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); +} diff --git a/tests/flexbuffers/test_anonymous_fields.hpp b/tests/flexbuffers/test_anonymous_fields.hpp new file mode 100644 index 00000000..0b1e229f --- /dev/null +++ b/tests/flexbuffers/test_anonymous_fields.hpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_anonymous_fields() { + std::cout << "test_anonymous_fields" << std::endl; + + 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; + }; + + 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); +} diff --git a/tests/flexbuffers/test_field_variant.hpp b/tests/flexbuffers/test_field_variant.hpp new file mode 100644 index 00000000..f8cff6db --- /dev/null +++ b/tests/flexbuffers/test_field_variant.hpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_field_variant() { + std::cout << "test_field_variant" << std::endl; + + 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 = std::variant, + rfl::Field<"rectangle", Rectangle>, + rfl::Field<"square", rfl::Box>>; + + const Shapes r = + rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); + + write_and_read(r); +} diff --git a/tests/flexbuffers/test_literal.hpp b/tests/flexbuffers/test_literal.hpp new file mode 100644 index 00000000..5bff332a --- /dev/null +++ b/tests/flexbuffers/test_literal.hpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +// Makes sure that we can set default values. +void test_literal() { + std::cout << "test_literal" << std::endl; + + 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; + }; + + const auto bart = Person{.first_name = FirstName::make<"Bart">()}; + + write_and_read(bart); +} diff --git a/tests/flexbuffers/test_optional_fields.hpp b/tests/flexbuffers/test_optional_fields.hpp new file mode 100644 index 00000000..fd5c777d --- /dev/null +++ b/tests/flexbuffers/test_optional_fields.hpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_optional_fields() { + std::cout << "test_optional_fields" << std::endl; + + 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; + }; + + 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); +} diff --git a/tests/flexbuffers/test_tagged_union.hpp b/tests/flexbuffers/test_tagged_union.hpp new file mode 100644 index 00000000..a33b6fbe --- /dev/null +++ b/tests/flexbuffers/test_tagged_union.hpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +// Makes sure that we can set default values. +void test_tagged_union() { + std::cout << "test_tagged_union" << std::endl; + + struct Circle { + rfl::Field<"shape", rfl::Literal<"Circle">> shape = rfl::default_value; + rfl::Field<"radius", double> radius; + }; + + struct Rectangle { + rfl::Field<"shape", rfl::Literal<"Rectangle">> shape = rfl::default_value; + rfl::Field<"height", double> height; + rfl::Field<"width", double> width; + }; + + struct Square { + rfl::Field<"shape", rfl::Literal<"Square">> shape = rfl::default_value; + rfl::Field<"width", double> width; + }; + + using Shapes = rfl::TaggedUnion<"shape", Circle, Square, Rectangle>; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r); +} diff --git a/tests/flexbuffers/test_unique_ptr.hpp b/tests/flexbuffers/test_unique_ptr.hpp new file mode 100644 index 00000000..3f36e5d0 --- /dev/null +++ b/tests/flexbuffers/test_unique_ptr.hpp @@ -0,0 +1,29 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_unique_ptr() { + std::cout << "test_unique_ptr" << 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); +} diff --git a/tests/flexbuffers/test_unique_ptr2.hpp b/tests/flexbuffers/test_unique_ptr2.hpp new file mode 100644 index 00000000..dcfaa060 --- /dev/null +++ b/tests/flexbuffers/test_unique_ptr2.hpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_unique_ptr2() { + std::cout << "test_unique_ptr2" << std::endl; + + struct DecisionTree { + struct Leaf { + rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value; + rfl::Field<"value", double> value; + }; + + struct Node { + rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value; + 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; + }; + + 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); +} diff --git a/tests/flexbuffers/test_variant.hpp b/tests/flexbuffers/test_variant.hpp new file mode 100644 index 00000000..da4e1f70 --- /dev/null +++ b/tests/flexbuffers/test_variant.hpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +// Makes sure that we can set default values. +void test_variant() { + std::cout << "test_variant" << std::endl; + + 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 = std::variant>; + + const Shapes r = Rectangle{.height = 10, .width = 5}; + + write_and_read(r); +} diff --git a/tests/flexbuffers/tests.cpp b/tests/flexbuffers/tests.cpp index fa4a3115..0ffa6ca0 100644 --- a/tests/flexbuffers/tests.cpp +++ b/tests/flexbuffers/tests.cpp @@ -1,7 +1,26 @@ +#include "test_all_of.hpp" +#include "test_anonymous_fields.hpp" +#include "test_field_variant.hpp" +#include "test_literal.hpp" +#include "test_optional_fields.hpp" #include "test_readme_example.hpp" +#include "test_tagged_union.hpp" +#include "test_unique_ptr.hpp" +#include "test_unique_ptr2.hpp" +#include "test_variant.hpp" int main() { test_readme_example(); + test_anonymous_fields(); + test_optional_fields(); + test_unique_ptr(); + test_unique_ptr2(); + test_literal(); + test_variant(); + test_field_variant(); + test_tagged_union(); + + test_all_of(); return 0; }