ServicePackTest_module.cc
Go to the documentation of this file.
1 /**
2  * @file ServicePackTest_module.cc
3  * @brief Tests utilities in ServicePack.h
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date November 23, 2015
6  * @see ServicePack.h
7  */
8 
9 // LArSoft libraries
10 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom<>()
14 #include "lardata/DetectorInfoServices/ServicePack.h" // lar::providerFrom<>()
16 
17 // framework libraries
21 
22 // C/C++ standard libraries
23 #include <sstream>
24 #include <string>
25 
26 namespace fhicl {
27  class ParameterSet; //
28 } // namespace fhicl
29 
30 namespace { // local
31 
32  template <typename T>
33  inline std::string
34  to_string(T const& v)
35  {
36  return std::to_string(v);
37  }
38 
39  template <typename T>
41  to_string(T const* ptr)
42  {
43  std::ostringstream sstr;
44  sstr << '<' << ((void*)ptr) << '>';
45  return sstr.str();
46  } // to_string()
47 
48 } // local namespace
49 
50 namespace lar {
51 
52  /**
53  * @brief Test module for ServicePack.h utilities depending on art farmework
54  *
55  * Currently exercises:
56  * - `lar::extractProviders()`
57  *
58  * Throws an exception on failure.
59  *
60  * Service requirements
61  * =====================
62  *
63  * This module requires the following services to be configured:
64  * - Geometry
65  * - LArPropertiesService
66  * - DetectorClocksService
67  * - DetectorPropertiesService
68  *
69  * Configuration parameters
70  * =========================
71  *
72  * Currently none.
73  *
74  */
75  class ServicePackTest : public art::EDAnalyzer {
76  public:
77  explicit ServicePackTest(fhicl::ParameterSet const&);
78 
79  private:
80  /// Run event-independent tests
81  void beginJob() override;
82 
83  /// Run event-dependent tests (none so far)
84  void
85  analyze(const art::Event& /* evt */) override
86  {}
87 
88  /// Throws if errors have been accumulated
89  void endJob() override;
90 
91  /// @{
92  /// @name Test functions
93 
94  /// Tests lar::extractProviders()
95  void extractProviders_test_plain();
96 
97  /// Tests lar::extractProviders() and permuted constructor
98  void extractProviders_test_permuted();
99 
100  /// Tests lar::extractProviders() and assignment to reduced pack
101  void extractProviders_test_reduced();
102 
103  /// All tests on lar::extractProviders()
104  void extractProviders_tests();
105 
106  /// @}
107 
108  std::vector<std::string> errors; ///< list of collected errors
109 
110  }; // ServicePackTest
111 
112  DEFINE_ART_MODULE(ServicePackTest)
113 
114 } // namespace lar
115 
116 //------------------------------------------------------------------------------
117 //--- implementation
118 //---
119 namespace lar {
120 
121  //----------------------------------------------------------------------------
122  ServicePackTest::ServicePackTest(const fhicl::ParameterSet& pset) : EDAnalyzer(pset) {}
123 
124  //----------------------------------------------------------------------------
125  void
126  ServicePackTest::beginJob()
127  {
128  extractProviders_tests();
129  } // ServicePackTest::beginJob()
130 
131  //----------------------------------------------------------------------------
132  void
133  ServicePackTest::endJob()
134  {
135  if (errors.empty()) {
136  mf::LogInfo("ServicePackTest") << "All tests were successful.";
137  return;
138  }
139 
140  mf::LogError log("ServicePackTest");
141  log << errors.size() << " errors detected:";
142 
143  for (std::string const& error : errors)
144  log << "\n - " << error;
145 
146  throw art::Exception(art::errors::LogicError) << errors.size() << " errors detected";
147  }
148 
149  //----------------------------------------------------------------------------
150  void
151  ServicePackTest::extractProviders_tests()
152  {
153  extractProviders_test_plain();
154  extractProviders_test_permuted();
155  extractProviders_test_reduced();
156  }
157 
158  //----------------------------------------------------------------------------
159  void
160  ServicePackTest::extractProviders_test_plain()
161  {
162  /*
163  * The test creates a ProviderPack and checks that its element as as
164  * expected.
165  *
166  * The expected value is extracted from the framework in the "traditional"
167  * way.
168  */
169 
170  // these are the "solutions":
171  geo::GeometryCore const* geom = lar::providerFrom<geo::Geometry>();
172  detinfo::LArProperties const* larprop = lar::providerFrom<detinfo::LArPropertiesService>();
173 
174  auto providers = lar::extractProviders<geo::Geometry, detinfo::LArPropertiesService>();
175 
176  // check time
177  if (providers.get<geo::GeometryCore>() != geom) {
178  errors.push_back("wrong geometry provider (got " +
179  ::to_string(providers.get<geo::GeometryCore>()) + ", expected " +
180  ::to_string(geom) + ")");
181  }
182  if (providers.get<detinfo::LArProperties>() != larprop) {
183  errors.push_back("wrong LAr properties provider (got " +
184  ::to_string(providers.get<detinfo::LArProperties>()) + ", expected " +
185  ::to_string(larprop) + ")");
186  }
187  } // ServicePackTest::extractProviders_test_plain()
188 
189  //----------------------------------------------------------------------------
190  void
191  ServicePackTest::extractProviders_test_permuted()
192  {
193  /*
194  * The test creates a ProviderPack and checks that its element as as
195  * expected.
196  *
197  * The expected value is extracted from the framework in the "traditional"
198  * way.
199  *
200  * The order of the providers is different from the order of the services;
201  * in this way a "wrong" ProviderPack will be (deliberately) created,
202  * and the code will have to convert it to the right pack.
203  */
204 
205  // these are the "solutions":
206  geo::GeometryCore const* geom = lar::providerFrom<geo::Geometry>();
207  detinfo::LArProperties const* larprop = lar::providerFrom<detinfo::LArPropertiesService>();
208 
209  auto providers = lar::extractProviders<geo::Geometry, detinfo::LArPropertiesService>();
210 
211  // check time
212  if (providers.get<geo::GeometryCore>() != geom) {
213  errors.push_back("wrong geometry provider (got " +
214  ::to_string(providers.get<geo::GeometryCore>()) + ", expected " +
215  ::to_string(geom) + ") [permuted]");
216  }
217  if (providers.get<detinfo::LArProperties>() != larprop) {
218  errors.push_back("wrong LAr properties provider (got " +
219  ::to_string(providers.get<detinfo::LArProperties>()) + ", expected " +
220  ::to_string(larprop) + ") [permuted]");
221  }
222  }
223 
224  //----------------------------------------------------------------------------
225  void
226  ServicePackTest::extractProviders_test_reduced()
227  {
228  /*
229  * The test creates a ProviderPack and checks that its element as as
230  * expected.
231  *
232  * The expected value is extracted from the framework in the "traditional"
233  * way.
234  *
235  * We use a smaller provider pack to store the providers;
236  * DetectorProperties will be dropped.
237  */
238 
239  // these are the "solutions":
240  geo::GeometryCore const* geom = lar::providerFrom<geo::Geometry>();
241  detinfo::LArProperties const* larprop = lar::providerFrom<detinfo::LArPropertiesService>();
242  auto providers = lar::extractProviders<geo::Geometry, detinfo::LArPropertiesService>();
243 
244  // check time
245  if (providers.get<geo::GeometryCore>() != geom) {
246  errors.push_back("wrong geometry provider (got " +
247  ::to_string(providers.get<geo::GeometryCore>()) + ", expected " +
248  ::to_string(geom) + ") [reduced]");
249  }
250  if (providers.get<detinfo::LArProperties>() != larprop) {
251  errors.push_back("wrong LAr properties provider (got " +
252  ::to_string(providers.get<detinfo::LArProperties>()) + ", expected " +
253  ::to_string(larprop) + ") [reduced]");
254  }
255  }
256 
257 } // namespace lar
std::string string
Definition: nybbler.cc:12
art framework interface to geometry description
Access the description of detector geometry.
std::string to_string(ModuleType const mt)
Definition: ModuleType.h:34
std::string to_string() const
Definition: ParameterSet.h:153