Repository navigation
Expand file tree
/
Copy pathtest_flatten.cpp
More file actions
69 lines (52 loc) · 1.86 KB
/
Copy pathtest_flatten.cpp
File metadata and controls
69 lines (52 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <cassert>
#include <rfl.hpp>
#include <string>
#include "test_helpers.hpp"
#include "write_and_read.hpp"
namespace test_flatten {
struct Person {
rfl::Field<"firstName", std::string> first_name;
rfl::Field<"lastName", rfl::Box<std::string>> last_name;
rfl::Field<"age", int> age;
};
struct Employee {
rfl::Flatten<Person> person;
rfl::Field<"employer", rfl::Box<std::string>> employer;
rfl::Field<"salary", float> salary;
};
TEST(generic, test_flatten) {
const auto employee = Employee{
.person = Person{.first_name = "Homer",
.last_name = rfl::make_box<std::string>("Simpson"),
.age = 45},
.employer = rfl::make_box<std::string>("Mr. Burns"),
.salary = 60000.0};
write_and_read(employee);
}
} // namespace test_flatten
namespace test_flatten_cross_move {
using test_helpers::FlatBase;
using test_helpers::FlatDerived;
TEST(regression, flatten_cross_type_move_does_not_copy) {
auto source = rfl::Flatten<FlatDerived>(FlatDerived{});
source.get().copies = 0;
source.get().moves = 0;
auto dest = rfl::Flatten<FlatBase>(std::move(source));
EXPECT_TRUE(dest.get().from_rvalue)
<< "Flatten cross-type move constructor should use rvalue conversion, "
"but get() returns lvalue ref, so lvalue conversion is used instead";
}
} // namespace test_flatten_cross_move
namespace test_flatten_universal_ref_ctor {
using test_helpers::FlatBase;
using test_helpers::FlatDerived;
TEST(regression, flatten_universal_ref_ctor_forwards_rvalue) {
auto val = FlatDerived{};
val.copies = 0;
val.moves = 0;
auto flat = rfl::Flatten<FlatBase>(std::move(val));
EXPECT_TRUE(flat.get().from_rvalue)
<< "Flatten(U&&) should forward rvalue to Type's converting "
"move ctor, but value_(_value) without std::forward causes a copy";
}
} // namespace test_flatten_universal_ref_ctor