10#include <gtest/gtest.h>
12#include <tracking/spacePointCreation/MapHelperFunctions.h>
13#include <framework/logging/Logger.h>
15#include <unordered_map>
29 typedef std::unordered_map<int, double>
i2dMap;
36 template<
typename Functor>
40 for (
int i = 0; i < nEntries; ++i) {
41 map.insert(std::make_pair((i % 6), funct.operator()(i)));
51 template<
typename Functor>
55 for (
int i = 0; i < nEntries; ++i) {
56 map.insert(std::make_pair(i, funct.operator()(i)));
69 double operator()(
int i)
71 return std::tan(i) / std::sin(i);
81 double operator()(
int i)
83 return std::sin(i * .5);
113 unsigned int N = _nEntries;
114 EXPECT_EQ(N, _nanMultiMap.size());
115 EXPECT_EQ(N, _nanMap.size());
116 EXPECT_EQ(N, _multimap.size());
117 EXPECT_EQ(N, _map.size());
119 ASSERT_TRUE(std::isnan(secans.operator()(0))) <<
"For the following tests to work properly this is expected to return a NaN (-nan)";
127 std::vector<int> possibleKeys = { 0, 1, 2, 3, 4, 5 };
128 for (
int key : possibleKeys) {
129 std::vector<double> nanPossibleValues, possibleValues;
130 for (
int arg = key; arg < _nEntries; arg += 6) {
131 nanPossibleValues.push_back(secans.operator()(arg));
132 possibleValues.push_back(sinHalf.operator()(arg));
135 auto keyRange = _nanMultiMap.equal_range(key);
137 EXPECT_EQ(nanPossibleValues.size(), std::distance(keyRange.first, keyRange.second));
138 i2dMultiMap::iterator multiMapIt = keyRange.first;
139 for (
int i = 0; i < std::distance(keyRange.first, keyRange.second); ++i) {
140 if (!std::isnan(multiMapIt->second)) {
141 auto findIt = std::find(nanPossibleValues.begin(), nanPossibleValues.end(), multiMapIt->second);
142 EXPECT_FALSE(findIt == nanPossibleValues.end());
144 EXPECT_TRUE(std::count_if(nanPossibleValues.begin(), nanPossibleValues.end(),
145 [](
const double & val) { return std::isnan(val); }) > 0);
149 keyRange = _multimap.equal_range(key);
150 EXPECT_EQ(possibleValues.size(), std::distance(keyRange.first, keyRange.second));
151 multiMapIt = keyRange.first;
152 for (
int i = 0; i < std::distance(keyRange.first, keyRange.second); ++i) {
153 auto findIt = std::find(possibleValues.begin(), possibleValues.end(), multiMapIt->second);
154 EXPECT_FALSE(findIt == possibleValues.end());
159 for (
unsigned int i = 0; i < N; ++i) {
160 auto range = _nanMap.equal_range(i);
161 i2dMap::iterator mapIt = range.first;
162 for (
int j = 0; j < std::distance(range.first, range.second); ++j) {
163 if (std::isnan(mapIt->second)) {
164 B2INFO(
"Not Comparing value to key " << i <<
" because value is NaN, checking if the functor returns NaN for this key");
165 EXPECT_TRUE(std::isnan(secans.operator()(i)));
168 EXPECT_DOUBLE_EQ(mapIt->second, secans.operator()(i));
172 range = _map.equal_range(i);
174 for (
int j = 0; j < std::distance(range.first, range.second); ++j) {
175 EXPECT_DOUBLE_EQ(mapIt->second, sinHalf.operator()(i));
185 EXPECT_EQ(uniqueKeys.size(), _map.size());
186 for (
size_t i = 0; i < uniqueKeys.size(); ++i) {
188 auto findIt = std::find(uniqueKeys.begin(), uniqueKeys.end(), i);
189 EXPECT_FALSE(findIt == uniqueKeys.end());
194 unsigned int expectedSize = _multimap.size() > 6 ? 6 : _multimap.size();
195 EXPECT_EQ(expectedSize, uniqueMultiKeys.size());
196 for (
size_t i = 0; i < uniqueMultiKeys.size(); ++i) {
198 auto findIt = std::find(uniqueMultiKeys.begin(), uniqueMultiKeys.end(), i);
199 EXPECT_FALSE(findIt == uniqueMultiKeys.end());
207 std::vector<int> possibleKeys = { 0, 1, 2, 3, 4, 5 };
208 for (
int key : possibleKeys) {
209 std::vector<double> expectedValues;
210 for (
int arg = key; arg < _nEntries; arg += 6) { expectedValues.push_back(sinHalf.operator()(arg)); }
211 std::vector<double> actualValues =
getValuesToKey(_multimap, key);
212 EXPECT_EQ(expectedValues.size(), actualValues.size());
215 for (
const double& d : actualValues) {
216 EXPECT_FALSE(std::find(expectedValues.begin(), expectedValues.end(), d) == expectedValues.end());
218 expectedValues.clear();
219 for (
int arg = key; arg < _nEntries; arg += 6) {
220 expectedValues.push_back(secans.operator()(arg));
223 EXPECT_EQ(expectedValues.size(), actualValues.size());
226 for (
const double& d : actualValues) {
228 B2INFO(
"Not comparing NaN value!");
231 EXPECT_FALSE(std::find(expectedValues.begin(), expectedValues.end(), d) == expectedValues.end());
236 for (
int i = 0; i < _nEntries; ++i) {
238 EXPECT_EQ(values.size(), 1);
239 EXPECT_DOUBLE_EQ(values[0], sinHalf.operator()(i));
242 EXPECT_EQ(values.size(), 1);
243 if (!std::isnan(values[0])) {
244 EXPECT_DOUBLE_EQ(values[0], secans.operator()(i));
252 std::vector<std::pair<int, unsigned int> > mapKeys =
getNValuesPerKey(_map);
253 unsigned int oneKey2Value = std::count_if(mapKeys.begin(), mapKeys.end(),
254 [](
const std::pair<int, unsigned int>& pair) { return pair.second == 1; });
255 EXPECT_EQ(_map.size(), oneKey2Value);
258 oneKey2Value = std::count_if(mapKeys.begin(), mapKeys.end(),
259 [](
const std::pair<int, unsigned int>& pair) { return pair.second == 1; });
260 EXPECT_EQ(_nanMap.size(), oneKey2Value);
264 EXPECT_EQ(mapKeys.size(), 6);
265 for (
const auto& key : mapKeys) {
266 EXPECT_EQ(key.second, key.first < 2 ? 4 : 3);
270 EXPECT_EQ(mapKeys.size(), 6);
271 for (
const auto& key : mapKeys) {
272 EXPECT_EQ(key.second, key.first < 2 ? 4 : 3);
280 std::vector<int> possibleKeys = { 0, 1, 2, 3, 4, 5 };
281 std::vector<double> summedValues;
282 std::vector<double> summedNanValues;
283 for (
int key : possibleKeys) {
284 std::vector<double> possibleValues;
285 std::vector<double> possibleNanValues;
286 for (
int arg = key; arg < _nEntries; arg += 6) {
287 possibleValues.push_back(sinHalf.operator()(arg));
288 possibleNanValues.push_back(secans.operator()(arg));
290 summedNanValues.push_back(std::accumulate(possibleNanValues.begin(), possibleNanValues.end(), 0.0));
291 summedValues.push_back(std::accumulate(possibleValues.begin(), possibleValues.end(), 0.0));
296 EXPECT_EQ(keyValPairs.size(), 6);
298 EXPECT_EQ(get<0>(keyValPairs[0]), 0);
299 EXPECT_TRUE(std::isnan(get<1>(keyValPairs[0])));
300 EXPECT_EQ(get<2>(keyValPairs[0]), 4);
304 EXPECT_EQ(keyValPairs.size(), 6);
306 EXPECT_EQ(get<0>(keyValPairs[0]), 0);
307 EXPECT_DOUBLE_EQ(get<1>(keyValPairs[0]), summedValues[0]);
308 EXPECT_EQ(get<2>(keyValPairs[0]), 4);
310 EXPECT_EQ(get<0>(keyValPairs[2]), 3);
311 EXPECT_DOUBLE_EQ(get<1>(keyValPairs[2]), summedValues[3]);
312 EXPECT_EQ(get<2>(keyValPairs[2]), 3);
315 EXPECT_EQ(get<0>(keyValPairs[0]), 3);
316 EXPECT_DOUBLE_EQ(get<1>(keyValPairs[0]), sinHalf.operator()(3));
318 EXPECT_EQ(get<0>(keyValPairs[19]), 9);
319 EXPECT_DOUBLE_EQ(get<1>(keyValPairs[19]), sinHalf.operator()(9));
326 std::vector<double> possibleValues, nanPossibleValues;
327 for (
int i = 0; i < _nEntries; ++i) {
328 possibleValues.push_back(sinHalf.operator()(i));
329 nanPossibleValues.push_back(secans.operator()(i));
332 std::sort(possibleValues.begin(), possibleValues.end());
333 std::sort(nanPossibleValues.begin(), nanPossibleValues.end());
337 EXPECT_EQ(allValues.size(), _nEntries);
338 std::sort(allValues.begin(), allValues.end());
339 for (
int i = 0; i < _nEntries; ++i) {
340 EXPECT_DOUBLE_EQ(allValues[i], possibleValues[i]);
345 EXPECT_EQ(allValues.size(), _nEntries);
346 std::sort(allValues.begin(), allValues.end());
347 for (
int i = 0; i < _nEntries; ++i) {
348 EXPECT_DOUBLE_EQ(allValues[i], possibleValues[i]);
function object that implements cosecans(x) == 1/cos(x) by tan(x) / sin(x).
int _nEntries
the number of pairs that will be put into the maps for the tests
i2dMultiMap _multimap
multimap filled with values obtained by sinHalf functor
virtual void SetUp()
set up the maps for the tests
i2dMultiMap _nanMultiMap
multimap filled with values obtained by secans functor -> contains a NaN!
i2dMap _nanMap
map filled with values obtained by secans functor -> contains a NaN!
i2dMap _map
map filled with values obtained by sinHalf functor
std::vector< std::pair< typename MapType::key_type, unsigned int > > getNValuesPerKey(const MapType &aMap)
get the unique keys of a map together with the number of values associated to each key.
i2dMap createMap(int nEntries, Functor funct)
create a multimap with
i2dMultiMap createMultiMap(int nEntries, Functor funct)
create a multimap with
std::unordered_map< int, double > i2dMap
typedef for less writing effort
std::vector< typename MapType::key_type > getUniqueKeys(const MapType &aMap)
get the unique keys of a map (i.e.
std::unordered_multimap< int, double > i2dMultiMap
typedef for less writing effort
std::vector< typename MapType::mapped_type > getValuesToKey(const MapType &aMap, typename MapType::key_type aKey)
get all values stored in the map for a given key
std::vector< typename MapType::mapped_type > getAllValues(const MapType &aMap)
get all values in the map (i.e.
std::vector< std::tuple< typename MapType::key_type, typename MapType::mapped_type, unsigned int > > getSortedKeyValueTuples(const MapType &aMap)
get the (key, value, number of values) tuples stored in the map, sorted after the following scheme (d...
Abstract base class for different kinds of events.