All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lardataalg/test/DetectorInfo/ProviderPack_test.cc
Go to the documentation of this file.
1 /**
2  * @file ProviderPack_test.cc
3  * @brief Unit test for ProviderPack class
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date November 20th, 2015
6  * @version 1.0
7  * @see ProviderPack.h
8  */
9 
10 // Define the following non-zero to exclude include code that is required
11 // not to be compilable
12 #ifndef PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
13 # define PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS 1
14 #endif // !PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
15 
16 // Boost libraries
17 /*
18  * Boost Magic: define the name of the module;
19  * and do that before the inclusion of Boost unit test headers
20  * because it will change what they provide.
21  * Among the those, there is a main() function and some wrapping catching
22  * unhandled exceptions and considering them test failures, and probably more.
23  * This also makes fairly complicate to receive parameters from the command line
24  * (for example, a random seed).
25  */
26 #define BOOST_TEST_MODULE ( ProviderPack_test )
27 #include <boost/test/unit_test.hpp>
28 
29 // LArSoft libraries
30 #include "lardata/DetectorInfo/ProviderPack.h"
31 
32 // C/C++ standard libraries
33 #include <string>
34 #include <ostream>
35 
36 namespace svc {
37 
38  /// A service provider class
39  struct ProviderA {
40 
42 
43  operator std::string() const
44  { return "ProviderA[" + std::to_string(count) + "]"; }
45 
46  unsigned int count;
47 
48  static unsigned int max_count;
49  }; // ProviderA
50  unsigned int ProviderA::max_count = 0;
51 
52  /// A service provider class
53  struct ProviderB {
54 
56 
57  operator std::string() const
58  { return "ProviderB[" + std::to_string(count) + "]"; }
59 
60  unsigned int count;
61 
62  static unsigned int max_count;
63  }; // ProviderB
64  unsigned int ProviderB::max_count = 0;
65 
66  /// A service provider class
67  struct ProviderC {
68 
70 
71  operator std::string() const
72  { return "ProviderC[" + std::to_string(count) + "]"; }
73 
74  unsigned int count;
75 
76  static unsigned int max_count;
77  }; // ProviderC
78  unsigned int ProviderC::max_count = 0;
79 
80  /// A service provider class
81  struct ProviderD {
82 
84 
85  operator std::string() const
86  { return "ProviderD[" + std::to_string(count) + "]"; }
87 
88  unsigned int count;
89 
90  static unsigned int max_count;
91  }; // ProviderD
92  unsigned int ProviderD::max_count = 0;
93 
94 
95 } // namespace svc
96 
97 
98 BOOST_AUTO_TEST_CASE(test_ProviderPack) {
99 
100  // instantiate a ProviderPack with two classes
101  svc::ProviderA providerA;
102  svc::ProviderB providerB;
103  svc::ProviderC providerC;
104  auto SP1 = lar::makeProviderPack(&providerA, &providerB, &providerC);
105 
106  // get element A
107  static_assert
108  (decltype(SP1)::has<svc::ProviderA>(), "We don't believe to have ProviderA!!");
109  auto myA = SP1.get<svc::ProviderA>();
110  static_assert(std::is_same<decltype(myA), svc::ProviderA const*>(),
111  "Failed to get the element of type A");
112  BOOST_TEST(myA == &providerA);
113 
114  // get element B
115  static_assert
116  (decltype(SP1)::has<svc::ProviderB>(), "We don't believe to have ProviderB!!");
117  auto myB = SP1.get<svc::ProviderB>();
118  static_assert(std::is_same<decltype(myB), svc::ProviderB const*>(),
119  "Failed to get the element of type B");
120  BOOST_TEST(myB == &providerB);
121 
122  // get element C
123  static_assert
124  (decltype(SP1)::has<svc::ProviderC>(), "We don't believe to have ProviderC!!");
125  auto myC = SP1.get<svc::ProviderC>();
126  static_assert(std::is_same<decltype(myC), svc::ProviderC const*>(),
127  "Failed to get the element of type C");
128  BOOST_TEST(myC == &providerC);
129 
130 
131  // set element A
132  svc::ProviderA providerA2;
133  SP1.set(&providerA2);
134  myA = SP1.get<svc::ProviderA>();
135  BOOST_TEST(myA == &providerA2);
136 
137  // get element D
138  // should be a compilation error
139 #if PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
140  BOOST_TEST_MESSAGE(" (test to get a non-existing provider type skipped)");
141 #else
142  SP1.get<svc::ProviderD>();
143 #endif // !PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
144 
145  // check what we believe we have
146  static_assert
147  (!decltype(SP1)::has<svc::ProviderD>(), "We believe to have ProviderD!!");
148 
149  // default constructor: all null
151  BOOST_TEST(SP2.get<svc::ProviderA>() == nullptr);
152  BOOST_TEST(SP2.get<svc::ProviderB>() == nullptr);
153 
154  // extraction constructor
156  BOOST_TEST(SP3.get<svc::ProviderA>() == SP1.get<svc::ProviderA>());
157  BOOST_TEST(SP3.get<svc::ProviderB>() == SP1.get<svc::ProviderB>());
158 
159  // multiple elements of the same type
160  // should be a compilation error
161 #if PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
162  BOOST_TEST_MESSAGE
163  (" (test to create a pack with many providers with same type skipped)");
164 #else
166  <svc::ProviderA, svc::ProviderB, svc::ProviderA, svc::ProviderD> SP3;
167 #endif // !PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
168 
169 
170 } // BOOST_AUTO_TEST_CASE(test_ProviderPack)
BOOST_AUTO_TEST_CASE(AllTests)
Provider const * get() const
Returns the provider with the specified type.
Definition: ProviderPack.h:193
ProviderPack< Providers...> makeProviderPack(Providers const *...providers)
Function to create a ProviderPack from the function arguments.
Definition: ProviderPack.h:272
std::string to_string(WindowPattern const &pattern)
Container for a list of pointers to providers.
Definition: ProviderPack.h:114