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 "fhiclcpp/fwd.h"
24 
25 // C/C++ standard libraries
26 #include <string>
27 
28 
29 // BEGIN AtomicNumber ----------------------------------------------------------
30 /// @ingroup AtomicNumber
31 /// @{
32 
33 /// Structure to hold enough information to computed the expected results
34 struct Results_t {
35  unsigned int Z; ///< atomic number
36 }; // Results_t
37 
38 
39 //------------------------------------------------------------------------------
40 /**
41  * @fn TestConfiguration
42  * @brief Runs a test with a specific configuration
43  * @param testName name of this test for output purposes
44  * @param configuration provider configuration as a string in FHiCL format
45  * @param expected the expected values
46  * @return number of errors encountered
47  */
48 unsigned int TestConfiguration
49  (std::string testName, std::string configuration, Results_t const& expected)
50 {
51 
52  //
53  // configuration of the test
54  //
55 
56  // provide a test name and a push a configuration for "AtomicNumberService"
58  config.AddDefaultServiceConfiguration("AtomicNumberService", configuration);
59 
60  // set up a basic testing environment with that configuration
61  auto TesterEnv = testing::CreateTesterEnvironment(config);
62 
63  //
64  // computation of expected values
65  //
66  unsigned int nErrors = 0; // error count
67 
68  // create a new service provider with configuration from the environment
70  (TesterEnv.ServiceParameters("AtomicNumberService"));
71 
72  //
73  // here goes the test...
74  //
75  unsigned int Z = Zprov.Z();
76  if (Z != expected.Z) {
77  mf::LogError("AtomicNumber_test") << testName
78  << ": wrong atomic number: " << Z
79  << " (expected: " << expected.Z << ")";
80  ++nErrors;
81  }
82 
83  //
84  // done!
85  //
86  return nErrors;
87 } // TestConfiguration()
88 
89 
90 //------------------------------------------------------------------------------
91 unsigned int TestDefaultConfiguration() {
93  expected.Z = 18;
94 
95  return TestConfiguration("TestDefaultConfiguration", "", expected);
96 
97 } // TestDefaultConfiguration()
98 
99 
100 //------------------------------------------------------------------------------
101 unsigned int TestXenonConfiguration() {
102 
104  expected.Z = 54;
105 
106  return TestConfiguration(
107  "TestXenonConfiguration", // test name
108  R"(
109  AtomicNumber: 54
110  )", // provider configuration
111  expected // expected values
112  );
113 } // TestXenonConfiguration()
114 
115 
116 //------------------------------------------------------------------------------
117 int main(int argc, char** argv) {
118 
119  unsigned int nErrors = 0;
120  nErrors += TestDefaultConfiguration();
121  nErrors += TestXenonConfiguration();
122 
123  return nErrors;
124 } // main()
125 
126 /// @}
127 // END AtomicNumber ------------------------------------------------------------
const char expected[]
Definition: Exception_t.cc:22
std::string string
Definition: nybbler.cc:12
Class holding a configuration for a test environment.
Provider returning atomic number of the active material in the TPC.
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
unsigned int TestXenonConfiguration()
void AddDefaultServiceConfiguration(std::string service_name, std::string service_cfg)
Adds a default configuration for the specified service.
static Config * config
Definition: config.cpp:1054
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:38
int main(int argc, char **argv)
unsigned int TestConfiguration(std::string testName, std::string configuration, Results_t const &expected)
unsigned int Z
atomic number
unsigned int TestDefaultConfiguration()