mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 10:46:18 +08:00
Added more tests for containers
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_deque() {
|
||||
std::cout << "test_deque" << std::endl;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::deque<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::deque<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);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_forward_list() {
|
||||
std::cout << "test_forward_list" << std::endl;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::forward_list<Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::forward_list<Person>>();
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_list() {
|
||||
std::cout << "test_list" << std::endl;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::list<Person>>> children =
|
||||
rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::list<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);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_map() {
|
||||
std::cout << "test_map" << std::endl;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::map<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::map<int, Person>>();
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_multimap() {
|
||||
std::cout << "test_multimap" << 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::multimap<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::multimap<int, Person>>();
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_multiset() {
|
||||
std::cout << "test_multiset" << 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::multiset<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::multiset<std::string>>(
|
||||
std::multiset<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_set() {
|
||||
std::cout << "test_set" << 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::set<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::set<std::string>>(
|
||||
std::set<std::string>({"Bart", "Lisa", "Maggie"}));
|
||||
|
||||
const auto homer =
|
||||
Person{.first_name = "Homer", .children = std::move(children)};
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_string_map() {
|
||||
std::cout << "test_string_map" << std::endl;
|
||||
|
||||
std::map<std::string, std::unique_ptr<std::string>> homer;
|
||||
homer.insert(
|
||||
std::make_pair("firstName", std::make_unique<std::string>("Homer")));
|
||||
homer.insert(
|
||||
std::make_pair("lastName", std::make_unique<std::string>("Simpson")));
|
||||
|
||||
write_and_read(homer);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_string_unordered_map() {
|
||||
std::cout << "test_string_unordered_map" << std::endl;
|
||||
|
||||
using Map = std::unordered_map<std::string, std::unique_ptr<std::string>>;
|
||||
Map homer;
|
||||
homer.insert(
|
||||
std::make_pair("firstName", std::make_unique<std::string>("Homer")));
|
||||
homer.insert(
|
||||
std::make_pair("lastName", std::make_unique<std::string>("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<Map>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_map() {
|
||||
std::cout << "test_unordered_map" << std::endl;
|
||||
|
||||
struct Person {
|
||||
rfl::Field<"firstName", std::string> first_name;
|
||||
rfl::Field<"lastName", std::string> last_name = "Simpson";
|
||||
rfl::Field<"children", std::unique_ptr<std::unordered_map<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_map<int, Person>>();
|
||||
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<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_multimap() {
|
||||
std::cout << "test_unordered_multimap" << 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::unordered_multimap<int, Person>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_multimap<int, Person>>();
|
||||
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<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_multiset() {
|
||||
std::cout << "test_unordered_multiset" << 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::unordered_multiset<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_multiset<std::string>>(
|
||||
std::unordered_multiset<std::string>({"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<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/flexbuf.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
void test_unordered_set() {
|
||||
std::cout << "test_unordered_set" << 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::unordered_set<std::string>>>
|
||||
children = rfl::default_value;
|
||||
};
|
||||
|
||||
auto children = std::make_unique<std::unordered_set<std::string>>(
|
||||
std::unordered_set<std::string>({"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<Person>(json_string);
|
||||
std::cout << "OK" << std::endl << std::endl;
|
||||
}
|
||||
@@ -1,12 +1,25 @@
|
||||
#include "test_all_of.hpp"
|
||||
#include "test_anonymous_fields.hpp"
|
||||
#include "test_deque.hpp"
|
||||
#include "test_field_variant.hpp"
|
||||
#include "test_forward_list.hpp"
|
||||
#include "test_list.hpp"
|
||||
#include "test_literal.hpp"
|
||||
#include "test_map.hpp"
|
||||
#include "test_multimap.hpp"
|
||||
#include "test_multiset.hpp"
|
||||
#include "test_optional_fields.hpp"
|
||||
#include "test_readme_example.hpp"
|
||||
#include "test_set.hpp"
|
||||
#include "test_string_map.hpp"
|
||||
#include "test_string_unordered_map.hpp"
|
||||
#include "test_tagged_union.hpp"
|
||||
#include "test_unique_ptr.hpp"
|
||||
#include "test_unique_ptr2.hpp"
|
||||
#include "test_unordered_map.hpp"
|
||||
#include "test_unordered_multimap.hpp"
|
||||
#include "test_unordered_multiset.hpp"
|
||||
#include "test_unordered_set.hpp"
|
||||
#include "test_variant.hpp"
|
||||
|
||||
int main() {
|
||||
@@ -20,6 +33,20 @@ int main() {
|
||||
test_field_variant();
|
||||
test_tagged_union();
|
||||
|
||||
test_deque();
|
||||
test_forward_list();
|
||||
test_list();
|
||||
test_map();
|
||||
test_multimap();
|
||||
test_multiset();
|
||||
test_set();
|
||||
test_string_map();
|
||||
test_string_unordered_map();
|
||||
test_unordered_map();
|
||||
test_unordered_multimap();
|
||||
test_unordered_multiset();
|
||||
test_unordered_set();
|
||||
|
||||
test_all_of();
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user