Jpp 21.0.0-rc.1-88-g0130508c4
the software that should make you happy
Loading...
Searching...
No Matches
test_head.cc
Go to the documentation of this file.
3
4#include <iostream>
5#include <sstream>
6#include <string>
7#include <vector>
8
9namespace {
10
11void expect_equal(const std::vector<std::string>& actual,
13 const std::string& label)
14{
15 if (actual == expected) {
16 return;
17 }
18
19 std::cerr << label << " mismatch" << std::endl;
20 std::cerr << " expected:";
21 for (const auto& value : expected) {
22 std::cerr << " [" << value << "]";
23 }
24 std::cerr << std::endl;
25 std::cerr << " actual:";
26 for (const auto& value : actual) {
27 std::cerr << " [" << value << "]";
28 }
29 std::cerr << std::endl;
30 std::exit(1);
31}
32
33void expect_true(bool condition, const std::string& message)
34{
35 if (!condition) {
36 std::cerr << message << std::endl;
37 std::exit(1);
38 }
39}
40
41} // namespace
42
43int main()
44{
45 Head h;
46
47 h.set_line("seed", "42");
48 h.set_line("detector", "aaa bbb");
49 h.set_line("detector", "ccc ddd");
50 h.set_line("detector", "ccc ddd eee");
51
52 const std::vector<std::string> expected_keys = {
53 "detector",
54 "detector_1",
55 "detector_2"
56 };
57 const std::vector<std::string> expected_lines = {
58 "aaa bbb",
59 "ccc ddd",
60 "ccc ddd eee"
61 };
62
63 const auto keys = h.matching_keys("detector");
64 const auto lines = h.get_lines("detector");
65 std::ostringstream output;
66
67 expect_equal(keys, expected_keys, "matching_keys");
68 expect_equal(lines, expected_lines, "get_lines");
69
70 expect_true(write(h, output), "write(Head, ostream) returned false");
71
72 const std::string rendered = output.str();
73 expect_true(rendered.find("seed: 42") != std::string::npos,
74 "Rendered header does not contain the seed line");
75 expect_true(rendered.find("detector_2: ccc ddd eee") != std::string::npos,
76 "Rendered header does not contain the final detector line");
77
78 std::cout << "OK" << std::endl;
79 return 0;
80}
bool write(const Vec &v, std::ostream &os)
Write a Vec(tor) to a stream.
Definition io_ascii.hh:155
const Waveform< float > expected
Definition parser.t.cc:7
The Head class reflects the header of Monte-Carlo event files, which consists of keys (also referred ...
Definition Head.hh:65
std::vector< std::string > matching_keys(const std::string &tag) const
In case of duplicate keys, they are internally stored in the map with a suffix "_n".
Definition Head.hh:115
std::string set_line(std::string tag, std::string line, bool ensure_unique=true)
Set data with the given tag.
Definition Head.hh:176
std::vector< std::string > get_lines(const std::string &tag) const
Get all data compatible with the given key.
Definition Head.hh:154
int main()
Definition test_head.cc:43