Added tests for Ref, Box and custom constructors

This commit is contained in:
Patrick Urbanke
2023-11-12 15:04:10 +01:00
parent 470fa31142
commit a68622e57f
5 changed files with 156 additions and 2 deletions
+8 -2
View File
@@ -13,13 +13,19 @@ namespace flexbuf {
using InputVarType = typename Reader::InputVarType;
/// Parses an object from flexbuf var.
template <class T>
Result<T> read(const InputVarType& _obj) {
const auto r = Reader();
return Parser<T>::read(r, _obj);
}
/// Parses an object from flexbuf using reflection.
template <class T>
Result<T> read(const unsigned char* _bytes, const size_t _size) {
const InputVarType root =
flexbuffers::GetRoot(reinterpret_cast<const uint8_t*>(_bytes), _size);
const auto r = Reader();
return Parser<T>::read(r, root);
return read<T>(root);
}
/// Parses an object from flexbuf using reflection.
+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_box() {
std::cout << "test_box" << 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", rfl::Box<DecisionTree>> lesser;
rfl::Field<"right", rfl::Box<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 = rfl::make_box<DecisionTree>(DecisionTree{leaf1}),
.greater = rfl::make_box<DecisionTree>(DecisionTree{leaf2})};
const DecisionTree tree{.leaf_or_node = std::move(node)};
write_and_read(tree);
}
@@ -0,0 +1,55 @@
#include <iostream>
#include <rfl.hpp>
#include <rfl/flexbuf.hpp>
#include <string>
#include <vector>
#include "write_and_read.hpp"
// Makes sure that the example in the README works as expected.
// It would be embarrassing if it didn't...
void test_custom_constructor() {
std::cout << "test_custom_constructor" << std::endl;
struct Person {
static rfl::Result<Person> 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<Person>(std::move(_nt));
};
return rfl::flexbuf::read<rfl::named_tuple_t<Person>>(_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;
};
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<Person>(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;
}
}
+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_ref() {
std::cout << "test_ref" << 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", rfl::Ref<DecisionTree>> lesser;
rfl::Field<"right", rfl::Ref<DecisionTree>> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
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>(DecisionTree{leaf1}),
.greater = rfl::make_ref<DecisionTree>(DecisionTree{leaf2})};
const DecisionTree tree{.leaf_or_node = std::move(node)};
write_and_read(tree);
}
+7
View File
@@ -1,8 +1,10 @@
#include "test_all_of.hpp"
#include "test_anonymous_fields.hpp"
#include "test_box.hpp"
#include "test_custom_class1.hpp"
#include "test_custom_class2.hpp"
#include "test_custom_class3.hpp"
#include "test_custom_constructor.hpp"
#include "test_deque.hpp"
#include "test_field_variant.hpp"
#include "test_forward_list.hpp"
@@ -13,6 +15,7 @@
#include "test_multiset.hpp"
#include "test_optional_fields.hpp"
#include "test_readme_example.hpp"
#include "test_ref.hpp"
#include "test_set.hpp"
#include "test_string_map.hpp"
#include "test_string_unordered_map.hpp"
@@ -38,6 +41,8 @@ int main() {
test_custom_class1();
test_custom_class2();
test_custom_class3();
test_box();
test_ref();
test_deque();
test_forward_list();
@@ -53,6 +58,8 @@ int main() {
test_unordered_multiset();
test_unordered_set();
test_custom_constructor();
test_all_of();
return 0;