All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KeyValueParser_test.cc
Go to the documentation of this file.
1 /**
2  * @file KeyValueParser_test.cc
3  * @brief Unit test `icarus::details::KeyValueParser`
4  * @date May 18, 2022
5  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
6  * @see icaruscode/PMT/Algorithms/KeyValueParser.h
7  *
8  */
9 
10 // ICARUS libraries
13 
14 // Boost libraries
15 #define BOOST_TEST_MODULE ( KeyValueParser_test )
16 #include <boost/test/unit_test.hpp>
17 
18 // C/C++ standard libraries
19 #include <iostream>
20 #include <sstream>
21 #include <vector>
22 #include <array>
23 #include <string>
24 #include <cstdint> // std::size_t
25 
26 
27 // -----------------------------------------------------------------------------
28 // --- implementation detail tests
29 // -----------------------------------------------------------------------------
30 void SPR_test() {
31 
32  std::string const info { R"( # SPR test input file Description: " This is a test for the key-values parser with default settings. It is expected to be used to describe the Single Photoelectron Response. " Contact: Gianluca Petrillo (petrillo@slac.stanford.edu) Gain: 9.7e6 # from amplitude 4 mV Tick: '2 ns' Samples: 0.0 1.0 2.5 \ 4.5 3.0 2.5 Samples:+1.8 1.6 1.2 0.8 0.8 0.7 0.7 0.6 )" };
33 
35 
36  std::istringstream in { info };
37  icarus::KeyValuesData const data { parser(in) };
38 
39  std::cout << "Input:\n" << std::string(80, '-') << "\n"
40  << info
41  << "\n" << std::string(80, '-')
42  << "\nParsed:\n" << data
43  << std::endl;
44 
45  BOOST_TEST(data.hasItem("Description"));
46  if (data.hasItem("Description")) {
47  icarus::KeyValuesData::Item const& item = data.getItem("Description");
48  BOOST_TEST(item.nValues() == 1);
49  BOOST_TEST(item.value() == "\n"
50  "This is a test for the key-values parser with default settings.\n"
51  "It is expected to be used to describe the Single Photoelectron Response.\n"
52  );
53  }
54 
55  BOOST_TEST(data.hasItem("Contact"));
56  if (data.hasItem("Contact")) {
57  icarus::KeyValuesData::Item const& item = data.getItem("Contact");
58  BOOST_TEST(item.nValues() == 3);
59  BOOST_TEST(item.value(0) == "Gianluca");
60  BOOST_TEST(item.value(1) == "Petrillo");
61  BOOST_TEST(item.value(2) == "(petrillo@slac.stanford.edu)");
62  }
63 
64  BOOST_TEST(data.hasItem("Gain"));
65  if (data.hasItem("Gain")) {
66  icarus::KeyValuesData::Item const& item = data.getItem("Gain");
67  BOOST_TEST(item.nValues() == 1);
68  BOOST_TEST
69  (item.getNumber<double>(0) == 9.7e6, boost::test_tools::tolerance(0.001));
70  }
71 
72  BOOST_TEST(data.hasItem("Tick"));
73  if (data.hasItem("Tick")) {
74  icarus::KeyValuesData::Item const& item = data.getItem("Tick");
75  BOOST_TEST(item.nValues() == 1);
76  BOOST_TEST(item.value() == "2 ns");
77  }
78 
79  BOOST_TEST(data.hasItem("Samples"));
80  if (data.hasItem("Samples")) {
81  icarus::KeyValuesData::Item const& item = data.getItem("Samples");
82  std::array const samples
83  { 0.0, 1.0, 2.5, 4.5, 3.0, 2.5, 1.8, 1.6, 1.2, 0.8, 0.8, 0.7, 0.7, 0.6 };
84  std::vector<double> const values = item.getVector<double>();
85  BOOST_CHECK_EQUAL_COLLECTIONS
86  (values.begin(), values.end(), samples.cbegin(), samples.cend());
87  BOOST_CHECK_THROW
89  }
90 
91 } // SPR_test()
92 
93 
94 // -----------------------------------------------------------------------------
95 // BEGIN Test cases -----------------------------------------------------------
96 // -----------------------------------------------------------------------------
97 
98 BOOST_AUTO_TEST_CASE(parse_testcase) {
99 
100  SPR_test();
101 
102 } // BOOST_AUTO_TEST_CASE(parse_testcase)
103 
104 
105 // -----------------------------------------------------------------------------
106 // END Test cases -------------------------------------------------------------
107 // -----------------------------------------------------------------------------
108 
BOOST_AUTO_TEST_CASE(AllTests)
T getNumber(std::size_t index, unsigned int base) const
Returns the requested value, converted into a number of type T
void SPR_test()
Simple parser for &quot;key: value&quot; text.
auto const tolerance
Representation of a single item of data: a key and several values.
Simple parsed data format.
then echo ***************************************echo array
Definition: find_fhicl.sh:28
Collection of items with key/values structure.
std::size_t nValues() const noexcept
Returns the number of values currently present.
std::vector< T > getVector(Conv converter={}) const
Returns all the values, each converted into type T
Parser to fill a KeyValuesData structure out of a character buffer.
if &&[-z"$BASH_VERSION"] then echo Attempting to switch to bash bash shellSwitch exit fi &&["$1"= 'shellSwitch'] shift declare a IncludeDirectives for Dir in
BEGIN_PROLOG could also be cout