Repository navigation
Expand file tree
/
Copy pathtest_box.cpp
More file actions
130 lines (93 loc) · 3.61 KB
/
Copy pathtest_box.cpp
File metadata and controls
130 lines (93 loc) · 3.61 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <cassert>
#include <rfl.hpp>
#include "write_and_read.hpp"
namespace test_box {
struct DecisionTree {
struct Leaf {
using Tag = rfl::Literal<"Leaf">;
double value;
};
struct Node {
using Tag = rfl::Literal<"Node">;
rfl::Rename<"criticalValue", double> critical_value;
rfl::Box<DecisionTree> lesser;
rfl::Box<DecisionTree> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
TEST(generic, test_box) {
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);
}
} // namespace test_box
namespace test_copyable_box
{
struct DecisionTree {
struct Leaf {
using Tag = rfl::Literal<"Leaf">;
double value;
};
struct Node {
using Tag = rfl::Literal<"Node">;
rfl::Rename<"criticalValue", double> critical_value;
rfl::CopyableBox<DecisionTree> lesser;
rfl::CopyableBox<DecisionTree> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
TEST(generic, copyable_box) {
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 tree1{.leaf_or_node = std::move(node)};
write_and_read(tree1);
const DecisionTree tree2 = tree1;
auto lesser_leaf_tree2 = rfl::get<DecisionTree::Leaf>(rfl::get<DecisionTree::Node>(tree2.leaf_or_node.get().variant()).lesser.get()->leaf_or_node.get().variant());
lesser_leaf_tree2.value = 1.0;
auto lesser_leaf_tree1 = rfl::get<DecisionTree::Leaf>(rfl::get<DecisionTree::Node>(tree1.leaf_or_node.get().variant()).lesser.get()->leaf_or_node.get().variant());
EXPECT_NE(lesser_leaf_tree1.value, lesser_leaf_tree2.value);
}
TEST(generic, copyable_box_copy_assignment) {
const auto make_leaf = [](const double _value) {
return rfl::make_copyable_box<DecisionTree>(
DecisionTree{DecisionTree::Leaf{.value = _value}});
};
const auto value_of = [](const rfl::CopyableBox<DecisionTree>& _box) {
return rfl::get<DecisionTree::Leaf>(_box->leaf_or_node.get().variant())
.value;
};
const auto box1 = make_leaf(3.0);
auto box2 = make_leaf(5.0);
box2 = box1;
EXPECT_EQ(value_of(box2), 3.0);
EXPECT_NE(box1.get(), box2.get());
const DecisionTree::Node node1{.critical_value = 10.0,
.lesser = make_leaf(1.0),
.greater = make_leaf(2.0)};
auto node2 = DecisionTree::Node{.critical_value = 20.0,
.lesser = make_leaf(4.0),
.greater = make_leaf(5.0)};
node2 = node1;
EXPECT_EQ(node2.critical_value.get(), 10.0);
EXPECT_EQ(value_of(node2.lesser), 1.0);
EXPECT_EQ(value_of(node2.greater), 2.0);
EXPECT_NE(node1.lesser.get(), node2.lesser.get());
// The source may be part of the value it replaces.
auto root = rfl::make_copyable_box<DecisionTree>(
DecisionTree{.leaf_or_node = node1});
root = rfl::get<DecisionTree::Node>(root->leaf_or_node.get().variant())
.lesser;
EXPECT_EQ(value_of(root), 1.0);
}
} // namespace test_copyable_box