return_optionalValues_from_fcl_t.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // test return values from FHiCL files
4 //
5 // The purpose of this test is to verify that all non-table
6 // optional values are accurately retrieved after being validated.
7 //
8 // ======================================================================
9 
10 #define BOOST_TEST_MODULE (return optional values from fcl)
11 
12 #include "boost/test/unit_test.hpp"
13 
19 #include "fhiclcpp/types/Table.h"
20 #include "fhiclcpp/types/Tuple.h"
21 
22 #include "FixtureBase.h"
23 
24 #include <limits>
25 #include <string>
26 
27 using namespace fhicl;
28 using namespace fhicl::detail;
29 using namespace std;
30 
31 namespace {
32 
33  constexpr auto tolerance = std::numeric_limits<double>::epsilon();
34 
35  struct Physics {
36  Tuple<std::string, double> energyCutoff{Name("energyCutoff")};
37  OptionalAtom<std::string> moniker{Name("moniker")};
38  };
39 
40  struct Composer {
41  Atom<std::string> composer{Name("composer")};
42  OptionalTuple<int, std::string> aSymphonyMoniker{Name("aSymphonyMoniker")};
43  };
44 
45  struct Configuration {
46  OptionalAtom<int> atom{Name("atom")};
48  OptionalTable<Physics> physics{Name("physics")};
49  OptionalSequence<int> list1{Name("list1")};
50  OptionalSequence<int, 4> list2{Name("list2")};
51  OptionalSequence<Sequence<int, 2>> list3{Name("list3")};
52  OptionalSequence<Table<Composer>, 4> list4{Name("list4")};
53  };
54 
55  struct Fixture : fhiclcpp_types::FixtureBase<Configuration> {
56  Fixture() : FixtureBase("return_optionalValues_from_fcl_t.fcl") {}
57  };
58 }
59 
60 // provide use of 'Table<Configuration> config'
61 BOOST_FIXTURE_TEST_SUITE(optionalValues_from_fcl, Fixture)
62 
63 // [1] OptionalAtom<T>
65 {
66  int i{};
67  std::string n;
68  BOOST_TEST(config().atom(i));
69  BOOST_TEST(!config().name(n));
70  BOOST_TEST(i == 5);
71  BOOST_TEST(n.empty());
72 }
73 
74 // [2] OptionalTable<T>
76 {
77  auto phys = config().physics();
78  BOOST_REQUIRE(phys.has_value());
79  auto name = phys->moniker();
80  BOOST_REQUIRE(name.has_value());
81  BOOST_TEST(*name == "Johnny");
82  BOOST_TEST(phys->energyCutoff.get<0>() == "QGSP");
83  BOOST_TEST(phys->energyCutoff.get<1>() == 14.6, tolerance);
84 }
85 
86 // [3] OptionalSequence<T>
87 BOOST_AUTO_TEST_CASE(optSeqVector_t1)
88 {
89  std::vector<int> intList;
90  BOOST_TEST(!config().list1.hasValue());
91  BOOST_TEST(!config().list1(intList));
92 }
93 
94 // [4] OptionalSequence<T,SIZE>
95 BOOST_AUTO_TEST_CASE(optSeqVector_t2)
96 {
97  std::array<int, 4> intList;
98  auto ref = {1, 2, 4, 8};
99  BOOST_TEST(config().list2.hasValue());
100  BOOST_TEST(config().list2(intList));
101  BOOST_TEST(intList == ref, boost::test_tools::per_element{});
102 }
103 
104 // [5] OptionalSequence<T>
105 BOOST_AUTO_TEST_CASE(optSeqVector_t3)
106 {
107  auto intLists = config().list3();
108  BOOST_TEST(intLists.has_value());
109 
110  decltype(intLists)::value_type const ref{
111  {{0, 1}}, {{1, 2}}, {{2, 4}}, {{3, 8}}};
112  std::size_t i{};
113  for (auto const& list : *intLists) {
114  BOOST_TEST(list == ref[i], boost::test_tools::per_element{});
115  ++i;
116  }
117 }
118 
119 // [6] OptionalSequence<T>
120 BOOST_AUTO_TEST_CASE(optSeqVector_t4)
121 {
122  std::array<Composer, 4> composers;
123  BOOST_TEST(config().list4(composers));
124 
125  std::array<std::string, 4> const ref{
126  {"Mozart", "Beethoven", "Brahms", "Mahler"}};
127 
128  std::size_t i{};
129  for (auto const& comp : composers) {
130  BOOST_TEST(comp.composer() == ref[i]);
131  ++i;
132  }
133 }
134 
135 // [7] OptionalTuple<T>
137 {
138  enum composer_t { Mozart, Beethoven, Brahms, Mahler };
139 
140  std::array<Composer, 4> composers;
141  BOOST_TEST(config().list4(composers));
142 
143  std::array<int, 4> const symphonyNumbers{{41, 3, 0, 8}};
144  std::array<std::string, 4> const symphonyMonikers{
145  {"Jupiter", "Eroica", "", "Symphony for a Thousand"}};
146  std::size_t i{};
147  for (auto const& comp : composers) {
148 
149  std::tuple<int, std::string> moniker;
150  if (i != static_cast<composer_t>(Brahms)) {
151  BOOST_TEST(comp.aSymphonyMoniker(moniker));
152  } else {
153  BOOST_TEST(!comp.aSymphonyMoniker(moniker));
154  }
155 
156  BOOST_TEST(std::get<int>(moniker) == symphonyNumbers[i]);
157  BOOST_TEST(std::get<std::string>(moniker) == symphonyMonikers[i]);
158  ++i;
159  }
160 }
161 
162 BOOST_AUTO_TEST_SUITE_END()
static QCString name
Definition: declinfo.cpp:673
std::string string
Definition: nybbler.cc:12
auto const tolerance
ChannelGroupService::Name Name
STL namespace.
static Config * config
Definition: config.cpp:1054
std::void_t< T > n
BOOST_AUTO_TEST_CASE(optAtom_t)