EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeometryHierarchyMapJsonConverterTests.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file GeometryHierarchyMapJsonConverterTests.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #include <boost/test/unit_test.hpp>
10 
13 
14 #include <fstream>
15 #include <ostream>
16 
17 #include <nlohmann/json.hpp>
18 
19 namespace {
20 
22 using nlohmann::json;
23 
24 // helper function to create geometry ids.
25 GeometryIdentifier makeId(int volume = 0, int layer = 0, int sensitive = 0) {
26  return GeometryIdentifier().setVolume(volume).setLayer(layer).setSensitive(
27  sensitive);
28 }
29 
30 // example element for the container
31 
32 struct Thing {
33  double x = 1.0;
34  int y = 23;
35 
36  friend constexpr bool operator==(const Thing& lhs, const Thing& rhs) {
37  return (lhs.x == rhs.x) and (lhs.y == rhs.y);
38  }
39 };
40 
41 // custom Json encoder/decoder. naming is mandated by nlohman::json and thus can
42 // not match our naming guidelines.
43 
44 void to_json(json& j, const Thing& t) {
45  j = json{{"x", t.x}, {"y", t.y}};
46 }
47 
48 void from_json(const json& j, Thing& t) {
49  j.at("x").get_to(t.x);
50  j.at("y").get_to(t.y);
51 }
52 
53 std::ostream& operator<<(std::ostream& os, const Thing& t) {
54  os << nlohmann::json(t);
55  return os;
56 }
57 
58 using Container = Acts::GeometryHierarchyMap<Thing>;
60 
61 } // namespace
62 
63 BOOST_TEST_DONT_PRINT_LOG_VALUE(json::iterator)
64 BOOST_TEST_DONT_PRINT_LOG_VALUE(Container::Iterator)
65 
66 BOOST_AUTO_TEST_SUITE(GeometryHierarchyMapJsonConverter)
67 
69  Container c = {
70  {makeId(1), {2.0, -3}},
71  {makeId(2, 3), {-4.5, 5}},
72  {makeId(4, 5, 6), {7.25, -8}},
73  };
74  json j = Converter("thing").toJson(c);
75 
76  BOOST_CHECK(j.is_object());
77  // check header
78  auto header = j.find("acts-geometry-hierarchy-map");
79  BOOST_CHECK_NE(header, j.end());
80  BOOST_CHECK(header->is_object());
81  BOOST_CHECK(header->at("format-version").is_number_integer());
82  BOOST_CHECK_EQUAL(header->at("format-version").get<int>(), 0);
83  BOOST_CHECK(header->at("value-identifier").is_string());
84  BOOST_CHECK_EQUAL(header->at("value-identifier").get<std::string>(), "thing");
85  // check entries
86  auto entries = j.find("entries");
87  BOOST_CHECK_NE(entries, j.end());
88  BOOST_CHECK(entries->is_array());
89  BOOST_CHECK_EQUAL(entries->size(), 3u);
90 }
91 
93  json j = {
94  {
95  "acts-geometry-hierarchy-map",
96  {
97  {"format-version", 0},
98  {"value-identifier", "thing"},
99  },
100  },
101  {
102  "entries",
103  {
104  {
105  {"volume", 2},
106  {"layer", 3},
107  {"value", {{"x", 4.0}, {"y", 4}}},
108  },
109  {
110  {"volume", 5},
111  {"layer", 6},
112  {"sensitive", 7},
113  {"value", {{"x", 3.0}, {"y", 3}}},
114  },
115  },
116  },
117  };
118  Container c = Converter("thing").fromJson(j);
119 
120  BOOST_CHECK(not c.empty());
121  BOOST_CHECK_EQUAL(c.size(), 2);
122  {
123  auto it = c.find(makeId(2, 3));
124  BOOST_CHECK_NE(it, c.end());
125  BOOST_CHECK_EQUAL(it->x, 4.0);
126  BOOST_CHECK_EQUAL(it->y, 4);
127  }
128  {
129  auto it = c.find(makeId(5, 6, 7));
130  BOOST_CHECK_NE(it, c.end());
131  BOOST_CHECK_EQUAL(it->x, 3.0);
132  BOOST_CHECK_EQUAL(it->y, 3);
133  }
134 }
135 
136 BOOST_AUTO_TEST_CASE(FromJsonMissingHeader) {
137  json j = {
138  {"entries", {}},
139  };
140  BOOST_CHECK_THROW(Converter("an-identifier").fromJson(j),
141  std::invalid_argument);
142 }
143 
144 BOOST_AUTO_TEST_CASE(FromJsonInvalidFormatVersion) {
145  json j = {
146  {
147  "acts-geometry-hierarchy-map",
148  {
149  {"format-version", -1},
150  {"value-identifier", "an-identifier"},
151  },
152  },
153  {"entries", {}},
154  };
155  BOOST_CHECK_THROW(Converter("an-identifier").fromJson(j),
156  std::invalid_argument);
157 }
158 
159 BOOST_AUTO_TEST_CASE(FromJsonInvalidValueIdentifier) {
160  json j = {
161  {
162  "acts-geometry-hierarchy-map",
163  {
164  {"format-version", 0},
165  {"value-identifier", "an-identifier"},
166  },
167  },
168  {"entries", {}},
169  };
170  BOOST_CHECK_THROW(Converter("not-the-identifier").fromJson(j),
171  std::invalid_argument);
172 }
173 
174 BOOST_AUTO_TEST_CASE(FromJsonMissingEntries) {
175  json j = {
176  {
177  "acts-geometry-hierarchy-map",
178  {
179  {"format-version", 0},
180  {"value-identifier", "an-identifier"},
181  },
182  },
183  };
184  BOOST_CHECK_THROW(Converter("an-identifier").fromJson(j),
185  std::invalid_argument);
186 }
187 
189  Container c0 = {
190  {makeId(1), {2.0, -3}},
191  {makeId(2, 3), {-4.5, 5}},
192  {makeId(4, 5, 6), {7.25, -8}},
193  };
194  auto j = Converter("the-identifier").toJson(c0);
195  auto c1 = Converter("the-identifier").fromJson(j);
196 
197  BOOST_CHECK_EQUAL(c0.size(), c1.size());
198  for (auto i = std::min(c0.size(), c1.size()); 0 < i--;) {
199  BOOST_CHECK_EQUAL(c0.idAt(i), c1.idAt(i));
200  BOOST_CHECK_EQUAL(c0.valueAt(i), c1.valueAt(i));
201  }
202 }
203 
205  // read json data from file
206  auto path = Acts::Test::getDataPath("geometry-hierarchy-map.json");
207  auto file = std::ifstream(path, std::ifstream::in | std::ifstream::binary);
208  BOOST_CHECK(file.good());
209  json j;
210  file >> j;
211  BOOST_CHECK(file.good());
212  // convert json to container
213  Container c = Converter("thing").fromJson(j);
214  // check container content
215  BOOST_CHECK(not c.empty());
216  BOOST_CHECK_EQUAL(c.size(), 4);
217  BOOST_CHECK_NE(c.find(makeId()), c.end());
218  BOOST_CHECK_NE(c.find(makeId(1, 2)), c.end());
219  BOOST_CHECK_NE(c.find(makeId(3)), c.end());
220  BOOST_CHECK_NE(c.find(makeId(3, 4)), c.end());
221 }
222 
223 BOOST_AUTO_TEST_SUITE_END()