Added more tests

This commit is contained in:
Patrick Urbanke
2023-11-12 00:08:56 +01:00
parent 2a948e809a
commit 629dd0c036
11 changed files with 326 additions and 2 deletions
+2 -2
View File
@@ -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<OutputNull>::make(); }
template <class T>
OutputVarType from_basic_type(const T _var) const noexcept {
OutputVarType from_basic_type(const T& _var) const noexcept {
return Ref<OutputValue<T>>::make(_var);
}
+25
View File
@@ -0,0 +1,25 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/flexbuf.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
void test_all_of() {
std::cout << "test_all_of" << std::endl;
using Age = rfl::Validator<unsigned int,
rfl::AllOf<rfl::Minimum<0>, 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);
}
@@ -0,0 +1,51 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/flexbuf.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
void test_anonymous_fields() {
std::cout << "test_anonymous_fields" << std::endl;
using Age = rfl::Validator<unsigned int,
rfl::AllOf<rfl::Minimum<0>, 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<Person> 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<Person>({bart, lisa, maggie})};
write_and_read(homer);
}
+34
View File
@@ -0,0 +1,34 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/flexbuf.hpp>
#include <string>
#include <vector>
#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<"circle", Circle>,
rfl::Field<"rectangle", Rectangle>,
rfl::Field<"square", rfl::Box<Square>>>;
const Shapes r =
rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5});
write_and_read(r);
}
+26
View File
@@ -0,0 +1,26 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/flexbuf.hpp>
#include <string>
#include <vector>
#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<Person>> children = rfl::default_value;
};
const auto bart = Person{.first_name = FirstName::make<"Bart">()};
write_and_read(bart);
}
@@ -0,0 +1,30 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/flexbuf.hpp>
#include <string>
#include <vector>
#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<std::vector<Person>>> 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<Person>({bart, lisa, maggie})};
write_and_read(homer);
}
+35
View File
@@ -0,0 +1,35 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/flexbuf.hpp>
#include <string>
#include <vector>
#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);
}
+29
View File
@@ -0,0 +1,29 @@
#include <iostream>
#include <memory>
#include <rfl.hpp>
#include <rfl/flexbuf.hpp>
#include <string>
#include <vector>
#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<std::vector<Person>>> children =
rfl::default_value;
};
auto children = std::make_unique<std::vector<Person>>();
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);
}
+43
View File
@@ -0,0 +1,43 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/flexbuf.hpp>
#include <string>
#include <vector>
#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<DecisionTree>> lesser;
rfl::Field<"right", std::unique_ptr<DecisionTree>> 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>(DecisionTree{leaf1}),
.greater = std::make_unique<DecisionTree>(DecisionTree{leaf2})};
const DecisionTree tree{.leaf_or_node = std::move(node)};
write_and_read(tree);
}
+32
View File
@@ -0,0 +1,32 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <rfl/flexbuf.hpp>
#include <string>
#include <vector>
#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<Circle, Rectangle, std::unique_ptr<Square>>;
const Shapes r = Rectangle{.height = 10, .width = 5};
write_and_read(r);
}
+19
View File
@@ -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;
}