All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KeyedCSVparser_test.cc
Go to the documentation of this file.
1 /**
2  * @file test/Decode/DecoderTools/KeyedCSVparser_test.cc
3  * @brief Unit test for `KeyedCSVparser.h` header.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date March 4, 2022
6  * @see `icaruscode/Decode/DecoderTools/details/KeyedCSVparser.h`
7  *
8  */
9 
10 // ICARUS libraries
12 
13 // Boost libraries
14 #define BOOST_TEST_MODULE ( KeyedCSVparser_test )
15 #include <boost/test/unit_test.hpp>
16 
17 // C/C++ standard library
18 #include <string>
19 #include <vector>
20 #include <cstdint> // std::uint32_t
21 
22 
23 // -----------------------------------------------------------------------------
24 // --- KeyedCSVparser tests
25 // -----------------------------------------------------------------------------
26 
28 
29  /*
30  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31  * using namespace std::string_view_literals;
32  * icarus::details::KeyedCSVparser parser;
33  * parser.addPatterns({
34  * { "Trigger Type", 1U } // expect one value (even if contains letters)
35  * , { "TriggerWindows", 1U } // expect one value (even if contains letters)
36  * , { "TPChitTimes", icarus::details::KeyedCSVparser::FixedSize }
37  * // the first value is an integer, count of how many other values
38  * });
39  *
40  * icarus::KeyValuesData data = parser(
41  * "TriggerType, S5, Triggers, TriggerWindows, 0C0B,"
42  * " TPChits, 12, 130, 0, 0, TPChitTimes, 3, -1.1, -0.3, 0.1, PMThits, 8"sv
43  * );
44  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
45  */
46 
47  using namespace std::string_literals;
49  parser.addPatterns({
50  { "TriggerType", 1U } // expect one value (even if contains letters)
51  , { "TriggerWindows", 1U } // expect one value (even if contains letters)
53  // the first value is an integer, count of how many other values
54  });
55 
57  "TriggerType, S5, Triggers, TriggerWindows, 0C0B,"
58  " TPChits, 12, 130, 0, 0, TPChitTimes, 3, -1.1, -0.3, 0.1, PMThits, 8"s
59  );
60 
61  // deep test:
62  std::vector<int> const expectedTPChits { 12, 130, 0, 0 };
63  std::vector<float> const expectedTPCtimes { -1.1, -0.3, 0.1 };
64 
65  icarus::KeyValuesData::Item const* item = nullptr;
66 
67  std::cout << data << std::endl;
68 
69  BOOST_TEST(data.hasItem("TriggerType"));
70  item = data.findItem("TriggerType");
71  BOOST_TEST(item);
72  BOOST_TEST(&(data.getItem("TriggerType")) == item);
73  BOOST_TEST(item->nValues() == 1);
74  BOOST_TEST(item->value() == "S5");
75 
76  BOOST_TEST(data.hasItem("Triggers"));
77  item = data.findItem("Triggers");
78  BOOST_TEST(item);
79  BOOST_TEST(&(data.getItem("Triggers")) == item);
80  BOOST_TEST(item->values().empty());
81  BOOST_TEST(item->getVector<int>().empty());
82 
83  BOOST_TEST(data.hasItem("TriggerWindows"));
84  item = data.findItem("TriggerWindows");
85  BOOST_TEST(item);
86  BOOST_TEST(&(data.getItem("TriggerWindows")) == item);
87  BOOST_TEST(item->nValues() == 1);
88  BOOST_TEST(item->value(0) == "0C0B");
89  BOOST_TEST(item->getNumber<std::uint32_t>(0, 16) == 0x0C0B);
90 
91  BOOST_TEST(data.hasItem("TPChits"));
92  item = data.findItem("TPChits");
93  BOOST_TEST(item);
94  BOOST_TEST(&(data.getItem("TPChits")) == item);
95  BOOST_TEST(item->nValues() == 4);
96  BOOST_TEST(item->value(0) == "12");
97  BOOST_TEST(item->value(1) == "130");
98  BOOST_TEST(item->value(2) == "0");
99  BOOST_TEST(item->value(3) == "0");
100  BOOST_TEST(item->getVector<int>() == expectedTPChits);
101 
102  BOOST_TEST(data.hasItem("TPChitTimes"));
103  item = data.findItem("TPChitTimes");
104  BOOST_TEST(item);
105  BOOST_TEST(&(data.getItem("TPChitTimes")) == item);
106  BOOST_TEST(item->nValues() == 4);
107  BOOST_TEST(item->value(0) == "3");
108  BOOST_TEST(item->value(1) == "-1.1");
109  BOOST_TEST(item->value(2) == "-0.3");
110  BOOST_TEST(item->value(3) == "0.1");
111  BOOST_TEST(item->getSizedVector<float>() == expectedTPCtimes);
112 
113  BOOST_TEST(!data.hasItem("CRThits"));
114  item = data.findItem("CRThits");
115  BOOST_TEST(!item);
116 
117  /*
118  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
119  * std::string triggerType = data.getItem("TriggerType").value(0);
120  * std::vector<int> triggers = data.getItem("Triggers").getVector<int>();
121  * std::uint32_t triggerWindowBits
122  * = data.getItem("TriggerWindows").getNumber<std::uint32_t>(0, 16); // base 16
123  * std::vector<int> TPChits = data.getItem("TPChits").getVector<int>();
124  * std::vector<float> TPCtimes
125  * = data.getItem("TPChitTimes").getSizedVector<float>();
126  * std::vector<int> CRThits;
127  * if (auto const* item = data.findItem("CRThits"))
128  * CRThits = item->getVector<int>();
129  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
130  */
131 
132  std::string triggerType = data.getItem("TriggerType").value(0);
133  BOOST_TEST(triggerType == "S5");
134 
135  std::vector<int> triggers = data.getItem("Triggers").getVector<int>();
136  BOOST_TEST(triggers.empty());
137 
138  std::uint32_t triggerWindowBits
139  = data.getItem("TriggerWindows").getNumber<std::uint32_t>(0, 16); // base 16
140  BOOST_TEST(triggerWindowBits == 0x0C0B);
141 
142  std::vector<int> TPChits = data.getItem("TPChits").getVector<int>();
143  BOOST_TEST(TPChits == expectedTPChits);
144 
145  std::vector<float> TPCtimes
146  = data.getItem("TPChitTimes").getSizedVector<float>();
147  BOOST_TEST(TPCtimes == expectedTPCtimes);
148 
149  std::vector<int> CRThits;
150  BOOST_TEST(!data.hasItem("CRThits"));
151  BOOST_TEST(!data.findItem("CRThits"));
152  if (auto const* item = data.findItem("CRThits"))
153  CRThits = item->getVector<int>();
154  BOOST_TEST(CRThits.empty());
155 
156 } // KeyedCSVparser_documentation_test()
157 
158 
159 // -----------------------------------------------------------------------------
160 // BEGIN Test cases -----------------------------------------------------------
161 // -----------------------------------------------------------------------------
162 BOOST_AUTO_TEST_CASE(KeyedCSVparser_documentation_testcase) {
163 
165 
166 } // BOOST_AUTO_TEST_CASE(KeyedCSVparser_documentation_testcase)
167 
168 
169 // -----------------------------------------------------------------------------
170 // END Test cases -------------------------------------------------------------
171 // -----------------------------------------------------------------------------
static constexpr unsigned int FixedSize
Expected values are missing.
BOOST_AUTO_TEST_CASE(AllTests)
Parser to fill a KeyValuesData structure out of a character buffer.
Simple parser for comma-separated text.
Representation of a single item of data: a key and several values.
triggerType
Type representing the type(s) of this trigger.
Definition: BeamBits.h:128
Collection of items with key/values structure.
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
void KeyedCSVparser_documentation_test()
BEGIN_PROLOG could also be cout