All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SortByPointers_test.cc
Go to the documentation of this file.
1 /**
2  * @file SortByPointers_test.cc
3  * @brief Unit test for `SortByPointers.h` utilities
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date September 28th, 2017
6  * @see SortByPointers.h
7  */
8 
9 // Boost libraries
10 #define BOOST_TEST_MODULE ( SortByPointers_test )
11 #include <boost/test/unit_test.hpp>
12 
13 // LArSoft libraries
15 
16 // C/C++ standard libraries
17 #include <vector>
18 #include <algorithm> // std::sort()
19 #include <type_traits> // std::decay_t, std::is_same
20 #include <cstdlib> // std::abs()
21 
22 
23 //------------------------------------------------------------------------------
24 template <typename Data>
25 struct AbsSorter {
26 
27  bool operator()(Data a, Data b) const { return std::abs(a) < std::abs(b); }
28  bool operator()(Data const* a, Data const* b) const
29  { return this->operator()(*a, *b); }
30 
31 }; // struct AbsSorter
32 
33 
35 
36  std::vector<int> data = { 8, -7, 5, 9, -2 };
37 
38  AbsSorter<int> absSorter;
39 
40  auto sortedData = data;
41  std::sort(sortedData.begin(), sortedData.end(), absSorter);
42 
44  [absSorter](auto& coll){ std::sort(coll.begin(), coll.end(), absSorter); }
45  );
46 
47  BOOST_CHECK_EQUAL_COLLECTIONS
48  (data.cbegin(), data.cend(), sortedData.cbegin(), sortedData.cend());
49 
50 } // test_SortByPointers()
51 
52 
53 //------------------------------------------------------------------------------
55 
56  std::vector<int> data = { 8, -7, 5 };
57 
58  std::vector<int*> expectedDataPtr = { &(data[0]), &(data[1]), &(data[2]) };
59 
60  auto dataPtr = util::makePointerVector(data);
61 
62  static_assert(
63  std::is_same<std::decay_t<decltype(dataPtr)>, std::vector<int*>>(),
64  "Unexpected data type from makePointerVector()");
65 
66  BOOST_CHECK_EQUAL_COLLECTIONS(
67  dataPtr.cbegin(), dataPtr.cend(),
68  expectedDataPtr.cbegin(), expectedDataPtr.cend()
69  );
70 
71 } // test_makePointerVector()
72 
73 
74 //------------------------------------------------------------------------------
76 
77  std::vector<int> data = { 1, 2, 3, 4 };
78 
79  std::vector<int*> const dataPtr
80  = { &(data[2]), &(data[3]), &(data[0]), &(data[1]) };
81  std::vector<int> expectedMovedData = { 3, 4, 1, 2 };
82 
83  std::vector<int> movedData;
84  util::MoveFromPointers(movedData, dataPtr);
85 
86  BOOST_CHECK_EQUAL_COLLECTIONS(
87  movedData.cbegin(), movedData.cend(),
88  expectedMovedData.cbegin(), expectedMovedData.cend()
89  );
90 
91 } // test_MoveFromPointers()
92 
93 
94 //------------------------------------------------------------------------------
95 BOOST_AUTO_TEST_CASE(SortByPointers_testcase) {
96 
98 
99 } // BOOST_AUTO_TEST_CASE(SortByPointers_testcase)
100 
101 
102 BOOST_AUTO_TEST_CASE(makePointerVector_testcase) {
103 
105 
106 } // BOOST_AUTO_TEST_CASE(makePointerVector_testcase)
107 
108 
109 BOOST_AUTO_TEST_CASE(MoveFromPointers_testcase) {
110 
112 
113 } // BOOST_AUTO_TEST_CASE(MoveFromPointers_testcase)
114 
BOOST_AUTO_TEST_CASE(AllTests)
bool operator()(Data a, Data b) const
Silly utility to sort vectors indirectly.
auto makePointerVector(Coll &coll)
Creates a STL vector with pointers to data from another collection.
bool operator()(Data const *a, Data const *b) const
void test_makePointerVector()
process_name gaushit a
void test_SortByPointers()
T abs(T value)
void SortByPointers(Coll &coll, Sorter sorter)
Applies sorting indirectly, minimizing data copy.
void test_MoveFromPointers()
void MoveFromPointers(Coll &dest, PtrColl &src)
Moves the content from a collection of pointers to one of data.