All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AtomicNumber_test.cc
Go to the documentation of this file.
1 /**
2  * @file AtomicNumber_test.cc
3  * @brief Tests the AtomicNumber service provider
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date April 13, 2016
6  * @see AtomicNumber.h
7  * @ingroup AtomicNumber
8  *
9  * The exit code of the test is the number of triggered errors.
10  * This text is expected to pass with 0 errors.
11  *
12  * This test does not exercise the configuration via file.
13  *
14  */
15 
16 
17 // LArSoft libraries
19 #include "larcorealg/TestUtils/unit_test_base.h" // testing namespace
20 
21 // support libraries
22 #include "messagefacility/MessageLogger/MessageLogger.h"
23 
24 // C/C++ standard libraries
25 #include <string>
26 
27 
28 // BEGIN AtomicNumber ----------------------------------------------------------
29 /// @ingroup AtomicNumber
30 /// @{
31 
32 /// Structure to hold enough information to computed the expected results
33 struct Results_t {
34  unsigned int Z; ///< atomic number
35 }; // Results_t
36 
37 
38 //------------------------------------------------------------------------------
39 /**
40  * @fn TestConfiguration
41  * @brief Runs a test with a specific configuration
42  * @param testName name of this test for output purposes
43  * @param configuration provider configuration as a string in FHiCL format
44  * @param expected the expected values
45  * @return number of errors encountered
46  */
47 unsigned int TestConfiguration
48  (std::string testName, std::string configuration, Results_t const& expected)
49 {
50 
51  //
52  // configuration of the test
53  //
54 
55  // provide a test name and a push a configuration for "AtomicNumberService"
57  config.AddDefaultServiceConfiguration("AtomicNumberService", configuration);
58 
59  // set up a basic testing environment with that configuration
60  auto TesterEnv = testing::CreateTesterEnvironment(config);
61 
62  //
63  // computation of expected values
64  //
65  unsigned int nErrors = 0; // error count
66 
67  // create a new service provider with configuration from the environment
69  (TesterEnv.ServiceParameters("AtomicNumberService"));
70 
71  //
72  // here goes the test...
73  //
74  unsigned int Z = Zprov.Z();
75  if (Z != expected.Z) {
76  mf::LogError("AtomicNumber_test") << testName
77  << ": wrong atomic number: " << Z
78  << " (expected: " << expected.Z << ")";
79  ++nErrors;
80  }
81 
82  //
83  // done!
84  //
85  return nErrors;
86 } // TestConfiguration()
87 
88 
89 //------------------------------------------------------------------------------
90 unsigned int TestDefaultConfiguration() {
91  Results_t expected;
92  expected.Z = 18;
93 
94  return TestConfiguration("TestDefaultConfiguration", "", expected);
95 
96 } // TestDefaultConfiguration()
97 
98 
99 //------------------------------------------------------------------------------
100 unsigned int TestXenonConfiguration() {
101 
102  Results_t expected;
103  expected.Z = 54;
104 
105  return TestConfiguration(
106  "TestXenonConfiguration", // test name
107  R"( AtomicNumber: 54 )", // provider configuration
108  expected // expected values
109  );
110 } // TestXenonConfiguration()
111 
112 
113 //------------------------------------------------------------------------------
114 int main(int argc, char** argv) {
115 
116  unsigned int nErrors = 0;
117  nErrors += TestDefaultConfiguration();
118  nErrors += TestXenonConfiguration();
119 
120  return nErrors;
121 } // main()
122 
123 /// @}
124 // END AtomicNumber ------------------------------------------------------------
125 
Class holding a configuration for a test environment.
Provider returning atomic number of the active material in the TPC.
unsigned int TestXenonConfiguration()
void AddDefaultServiceConfiguration(std::string service_name, std::string service_cfg)
Adds a default configuration for the specified service.
unsigned int Z() const
Returns the atomic number.
Definition: AtomicNumber.h:87
TESTENV CreateTesterEnvironment(CONFIG &&config, ARGS...other_args)
Constructs and returns a TesterEnvironment object.
Structure to hold enough information to computed the expected results.
Provides information about the active material in the TPC.
Definition: AtomicNumber.h:41
unsigned int TestConfiguration(std::string testName, std::string configuration, Results_t const &expected)
int main(int argc, char **argv)
Base class for unit tests using FHiCL configuration.
unsigned int Z
atomic number
unsigned int TestDefaultConfiguration()