ProviderPack_test.cc
Go to the documentation of this file.
1 /**
2  * @file ProviderPack_test.cxx
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 <cetlib/quiet_unit_test.hpp> // BOOST_AUTO_TEST_CASE()
28 #include <boost/test/test_tools.hpp> // BOOST_CHECK(), BOOST_CHECK_EQUAL()
29 
30 // GArSoft libraries
31 #include "CoreUtils/ProviderPack.h"
32 
33 // C/C++ standard libraries
34 #include <string>
35 #include <ostream>
36 
37 namespace svc {
38 
39  /// A service provider class
40  struct ProviderA {
41 
43 
44  operator std::string() const
45  { return "ProviderA[" + std::to_string(count) + "]"; }
46 
47  unsigned int count;
48 
49  static unsigned int max_count;
50  }; // ProviderA
51  unsigned int ProviderA::max_count = 0;
52 
53  /// A service provider class
54  struct ProviderB {
55 
57 
58  operator std::string() const
59  { return "ProviderB[" + std::to_string(count) + "]"; }
60 
61  unsigned int count;
62 
63  static unsigned int max_count;
64  }; // ProviderB
65  unsigned int ProviderB::max_count = 0;
66 
67  /// A service provider class
68  struct ProviderC {
69 
71 
72  operator std::string() const
73  { return "ProviderC[" + std::to_string(count) + "]"; }
74 
75  unsigned int count;
76 
77  static unsigned int max_count;
78  }; // ProviderC
79  unsigned int ProviderC::max_count = 0;
80 
81  /// A service provider class
82  struct ProviderD {
83 
85 
86  operator std::string() const
87  { return "ProviderD[" + std::to_string(count) + "]"; }
88 
89  unsigned int count;
90 
91  static unsigned int max_count;
92  }; // ProviderD
93  unsigned int ProviderD::max_count = 0;
94 
95 
96 } // namespace svc
97 
98 
99 BOOST_AUTO_TEST_CASE(test_ProviderPack) {
100 
101  // instantiate a ProviderPack with two classes
102  svc::ProviderA providerA;
103  svc::ProviderB providerB;
104  svc::ProviderC providerC;
105  auto SP1 = gar::makeProviderPack(&providerA, &providerB, &providerC);
106 
107  // get element A
108  static_assert
109  (decltype(SP1)::has<svc::ProviderA>(), "We don't believe to have ProviderA!!");
110  auto myA = SP1.get<svc::ProviderA>();
111  static_assert(std::is_same<decltype(myA), svc::ProviderA const*>(),
112  "Failed to get the element of type A");
113  BOOST_TEST(myA == &providerA);
114 
115  // get element B
116  static_assert
117  (decltype(SP1)::has<svc::ProviderB>(), "We don't believe to have ProviderB!!");
118  auto myB = SP1.get<svc::ProviderB>();
119  static_assert(std::is_same<decltype(myB), svc::ProviderB const*>(),
120  "Failed to get the element of type B");
121  BOOST_TEST(myB == &providerB);
122 
123  // get element C
124  static_assert
125  (decltype(SP1)::has<svc::ProviderC>(), "We don't believe to have ProviderC!!");
126  auto myC = SP1.get<svc::ProviderC>();
127  static_assert(std::is_same<decltype(myC), svc::ProviderC const*>(),
128  "Failed to get the element of type C");
129  BOOST_TEST(myC == &providerC);
130 
131 
132  // set element A
133  svc::ProviderA providerA2;
134  SP1.set(&providerA2);
135  myA = SP1.get<svc::ProviderA>();
136  BOOST_TEST(myA == &providerA2);
137 
138  // get element D
139  // should be a compilation error
140 #if PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
141  BOOST_TEST_MESSAGE(" (test to get a non-existing provider type skipped)");
142 #else
143  SP1.get<svc::ProviderD>();
144 #endif // !PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
145 
146  // check what we believe we have
147  static_assert
148  (!decltype(SP1)::has<svc::ProviderD>(), "We believe to have ProviderD!!");
149 
150  // default constructor: all null
152  BOOST_TEST(SP2.get<svc::ProviderA>() == nullptr);
153  BOOST_TEST(SP2.get<svc::ProviderB>() == nullptr);
154 
155  // extraction constructor
157  BOOST_TEST(SP3.get<svc::ProviderA>() == SP1.get<svc::ProviderA>());
158  BOOST_TEST(SP3.get<svc::ProviderB>() == SP1.get<svc::ProviderB>());
159 
160  // multiple elements of the same type
161  // should be a compilation error
162 #if PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
163  BOOST_TEST_MESSAGE
164  (" (test to create a pack with many providers with same type skipped)");
165 #else
167  <svc::ProviderA, svc::ProviderB, svc::ProviderA, svc::ProviderD> SP3;
168 #endif // !PROVIDERPACK_TEST_SKIP_COMPILATION_ERRORS
169 
170 
171 } // BOOST_AUTO_TEST_CASE(test_ProviderPack)
static unsigned int max_count
A service provider class.
std::string string
Definition: nybbler.cc:12
Provider const * get() const
Returns the provider with the specified type.
Definition: ProviderPack.h:141
A service provider class.
static unsigned int max_count
BOOST_AUTO_TEST_CASE(test_ProviderPack)
Container for a list of pointers to providers.
Definition: ProviderPack.h:90
unsigned int count
A service provider class.
static unsigned int max_count
unsigned int count
A service provider class.
unsigned int count
ProviderPack< Providers... > makeProviderPack(Providers const *...providers)
Function to create a ParameterPack from the function arguments.
Definition: ProviderPack.h:182
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
static unsigned int max_count
unsigned int count