9 #include <boost/test/unit_test.hpp>
18 #include <initializer_list>
23 #include <nlohmann/json.hpp>
31 GeometryIdentifier makeId(
int volume = 0,
int layer = 0,
int sensitive = 0) {
43 return (lhs.x == rhs.x) and (lhs.y == rhs.y);
51 j = json{{
"x", t.x}, {
"y", t.y}};
55 j.at(
"x").get_to(t.x);
56 j.at(
"y").get_to(t.y);
59 std::ostream&
operator<<(std::ostream&
os,
const Thing& t) {
60 os << nlohmann::json(t);
64 class ThingDecorator {
66 void decorate(
const Thing* a_thing, nlohmann::json& a_json)
const {
67 if (a_thing !=
nullptr) {
68 a_json[
"product"] = a_thing->x * a_thing->y;
80 void Acts::decorateJson<Thing>(
const ThingDecorator* decorator,
81 const Thing& src, nlohmann::json&
dest) {
82 if (decorator !=
nullptr) {
83 decorator->decorate(&src,
dest);
87 BOOST_TEST_DONT_PRINT_LOG_VALUE(json::iterator)
88 BOOST_TEST_DONT_PRINT_LOG_VALUE(Container::Iterator)
90 BOOST_AUTO_TEST_SUITE(GeometryHierarchyMapJsonConverter)
93 ThingDecorator decorator;
95 {makeId(1), {2.0, -3}},
96 {makeId(2, 3), {-4.5, 5}},
97 {makeId(4, 5, 6), {7.25, -8}},
99 json j = Converter(
"thing").toJson(c, &decorator);
101 BOOST_CHECK(j.is_object());
103 auto header = j.find(
"acts-geometry-hierarchy-map");
104 BOOST_CHECK_NE(header, j.end());
105 BOOST_CHECK(header->is_object());
106 BOOST_CHECK(header->at(
"format-version").is_number_integer());
107 BOOST_CHECK_EQUAL(header->at(
"format-version").get<
int>(), 0);
108 BOOST_CHECK(header->at(
"value-identifier").is_string());
109 BOOST_CHECK_EQUAL(header->at(
"value-identifier").get<
std::string>(),
"thing");
111 auto entries = j.find(
"entries");
112 BOOST_CHECK_NE(entries, j.end());
113 BOOST_CHECK(entries->is_array());
114 BOOST_CHECK_EQUAL(entries->size(), 3
u);
120 "acts-geometry-hierarchy-map",
122 {
"format-version", 0},
123 {
"value-identifier",
"thing"},
132 {
"value", {{
"x", 4.0}, {
"y", 4}}},
138 {
"value", {{
"x", 3.0}, {
"y", 3}}},
143 Container
c = Converter(
"thing").fromJson(j);
145 BOOST_CHECK(not c.empty());
146 BOOST_CHECK_EQUAL(c.size(), 2);
148 auto it = c.find(makeId(2, 3));
149 BOOST_CHECK_NE(
it, c.end());
150 BOOST_CHECK_EQUAL(
it->x, 4.0);
151 BOOST_CHECK_EQUAL(
it->y, 4);
154 auto it = c.find(makeId(5, 6, 7));
155 BOOST_CHECK_NE(
it, c.end());
156 BOOST_CHECK_EQUAL(
it->x, 3.0);
157 BOOST_CHECK_EQUAL(
it->y, 3);
165 BOOST_CHECK_THROW(Converter(
"an-identifier").
fromJson(j),
166 std::invalid_argument);
172 "acts-geometry-hierarchy-map",
174 {
"format-version", -1},
175 {
"value-identifier",
"an-identifier"},
180 BOOST_CHECK_THROW(Converter(
"an-identifier").
fromJson(j),
181 std::invalid_argument);
187 "acts-geometry-hierarchy-map",
189 {
"format-version", 0},
190 {
"value-identifier",
"an-identifier"},
195 BOOST_CHECK_THROW(Converter(
"not-the-identifier").
fromJson(j),
196 std::invalid_argument);
202 "acts-geometry-hierarchy-map",
204 {
"format-version", 0},
205 {
"value-identifier",
"an-identifier"},
209 BOOST_CHECK_THROW(Converter(
"an-identifier").
fromJson(j),
210 std::invalid_argument);
214 ThingDecorator decorator;
216 {makeId(1), {2.0, -3}},
217 {makeId(2, 3), {-4.5, 5}},
218 {makeId(4, 5, 6), {7.25, -8}},
220 auto j = Converter(
"the-identifier").toJson(c0, &decorator);
221 auto c1 = Converter(
"the-identifier").fromJson(j);
223 BOOST_CHECK_EQUAL(c0.size(), c1.size());
224 for (
auto i =
std::min(c0.size(), c1.size()); 0 <
i--;) {
225 BOOST_CHECK_EQUAL(c0.idAt(
i), c1.idAt(
i));
226 BOOST_CHECK_EQUAL(c0.valueAt(
i), c1.valueAt(
i));
234 BOOST_CHECK(
file.good());
237 BOOST_CHECK(
file.good());
239 Container
c = Converter(
"thing").fromJson(j);
241 BOOST_CHECK(not c.empty());
242 BOOST_CHECK_EQUAL(c.size(), 4);
243 BOOST_CHECK_NE(c.find(makeId()), c.end());
244 BOOST_CHECK_NE(c.find(makeId(1, 2)), c.end());
245 BOOST_CHECK_NE(c.find(makeId(3)), c.end());
246 BOOST_CHECK_NE(c.find(makeId(3, 4)), c.end());
249 BOOST_AUTO_TEST_SUITE_END()