return_values_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 values
6 // are accurately retrieved after being validated.
7 //
8 // ======================================================================
9 
10 #define BOOST_TEST_MODULE (return values from fcl)
11 
12 #include "FixtureBase.h"
13 #include "boost/test/unit_test.hpp"
14 #include "fhiclcpp/types/Atom.h"
16 #include "fhiclcpp/types/Tuple.h"
17 
18 #include <string>
19 #include <vector>
20 
21 using namespace fhicl;
22 using namespace fhicl::detail;
23 using namespace std;
24 
25 namespace {
26 
27  constexpr auto tolerance = std::numeric_limits<double>::epsilon();
28  constexpr auto ftolerance = std::numeric_limits<float>::epsilon();
29 
30  struct Configuration {
31  Atom<int> atom{Name("atom")};
32  Sequence<int> vec{Name("vec")};
33  Sequence<int, 2> arr{Name("arr")};
34 
35  Tuple<int, double, bool> tuple{Name("tuple")};
36  Tuple<Sequence<int>, double, bool> tupWithVec{Name("tupWithVec")};
37  Tuple<Sequence<int, 2>, double, bool> tupWithArr{Name("tupWithArr")};
38  Tuple<Tuple<int, float>, double, bool> tupWithTup{Name("tupWithTup")};
39 
40  Sequence<Tuple<int, string>> vecWithTup{Name("vecWithTup")};
41  Sequence<Tuple<string, int, string>, 2> arrWithTup{Name("arrWithTup")};
42 
43  Sequence<Sequence<int>> vecOfVec{Name("vecOfVec")};
44  Sequence<Sequence<int, 2>> vecOfArr{Name("vecOfArr")};
45  Sequence<Sequence<int>, 2> arrOfVec{Name("arrOfVec")};
46  Sequence<Sequence<int, 2>, 2> arrOfArr{Name("arrOfArr")};
47  };
48 
49  struct Fixture : fhiclcpp_types::FixtureBase<Configuration> {
50  Fixture() : FixtureBase("return_values_from_fcl_t.fcl") {}
51  };
52 }
53 
54 // provide use of 'Table<Configuration> config'
55 BOOST_FIXTURE_TEST_SUITE(values_from_fcl, Fixture)
56 
57 // [1] Atom<T>
59 {
60  BOOST_TEST(config().atom() == 5);
61 }
62 
63 // [2] Sequence<T>
65 {
66  auto ref = {3, 5, 8};
67  auto val = config().vec();
68  BOOST_TEST(val == ref, boost::test_tools::per_element{});
69 }
70 
71 // [3] Sequence<T,SZ>
73 {
74  auto ref = {4, 9};
75  auto val = config().arr();
76  BOOST_TEST(val == ref, boost::test_tools::per_element{});
77 }
78 
79 // [4] Tuple<T...>
81 {
82  BOOST_TEST(config().tuple.get<0>() == 5);
83  BOOST_TEST(config().tuple.get<1>() == 4.3, tolerance);
84  BOOST_TEST(config().tuple.get<2>() == true);
85 }
86 
87 // [5] Tuple< Sequence<T>, U...>
88 BOOST_AUTO_TEST_CASE(tuple_with_vector)
89 {
90  auto ref = {4, 1, 4, 67, 89};
91  auto val = config().tupWithVec.get<0>();
92  BOOST_TEST(val == ref, boost::test_tools::per_element{});
93  BOOST_TEST(config().tupWithVec.get<1>() == 4.56789, tolerance);
94  BOOST_TEST(config().tupWithVec.get<2>() == false);
95 }
96 
97 // [6] Tuple< Sequence<T,SZ>, U...>
98 BOOST_AUTO_TEST_CASE(tuple_with_array)
99 {
100  auto ref = {5, 16};
101  auto val = config().tupWithArr.get<0>();
102  BOOST_TEST(val == ref, boost::test_tools::per_element{});
103  BOOST_TEST(config().tupWithArr.get<1>() == 46.9, tolerance);
104  BOOST_TEST(config().tupWithArr.get<2>() == true);
105 }
106 
107 // [7] Tuple< Tuple<T...>, U...>
108 BOOST_AUTO_TEST_CASE(tuple_with_tuple)
109 {
110  auto tuple0 = config().tupWithTup.get<0>();
111  BOOST_TEST(std::get<0>(tuple0) == 4);
112  BOOST_TEST(std::get<1>(tuple0) == 175.218f, ftolerance);
113  BOOST_TEST(config().tupWithTup.get<1>() == 87.03, tolerance);
114  BOOST_TEST(config().tupWithTup.get<2>() == false);
115 }
116 
117 // [8] Sequence< Tuple<T...> >
119 {
120  vector<int> const nums{11, 22};
121  vector<string> const strs{"Decay in orbit", "Radiative pion capture"};
122 
123  size_t i{};
124  for (auto const& elem : config().vecWithTup()) {
125  BOOST_TEST(std::get<0>(elem) == nums.at(i));
126  BOOST_TEST(std::get<1>(elem) == strs.at(i++));
127  }
128 }
129 
130 // [9] Sequence< Tuple<T...>, SZ >
132 {
133 
134  vector<string> const prefixes = {"Tchaikovsky wrote ", "Mahler wrote "};
135  vector<int> const nums{6, 9};
136  string const str = " symphonies";
137 
138  size_t i{};
139  for (auto const& elem : config().arrWithTup()) {
140  BOOST_TEST(std::get<0>(elem) == prefixes.at(i));
141  BOOST_TEST(std::get<1>(elem) == nums.at(i++));
142  BOOST_TEST(std::get<2>(elem) == str);
143  }
144 }
145 
146 // [10] Sequence< Sequence<T> >
148 {
149  using vec_t = vector<int>;
150  auto ref_vec = {vec_t{1, 4, 6, 8}, vec_t{1, 3}, vec_t{6}};
151  auto it = ref_vec.begin();
152 
153  for (auto const& val : config().vecOfVec()) {
154  auto ref = *it++;
155  BOOST_TEST(val == ref, boost::test_tools::per_element{});
156  }
157 
158  size_t i{};
159  for (auto const& ref : ref_vec) {
160  auto val = config().vecOfVec(i++);
161  BOOST_TEST(val == ref, boost::test_tools::per_element{});
162  }
163 }
164 
165 // [11] Sequence< Sequence<T,SZ> >
167 {
168  using array_t = array<int, 2>;
169  auto ref_vec = {array_t{{1, 6}}, array_t{{2, 12}}, array_t{{3, 18}}};
170  auto it = ref_vec.begin();
171 
172  for (auto const& val : config().vecOfArr()) {
173  auto ref = *it++;
174  BOOST_TEST(val == ref, boost::test_tools::per_element{});
175  }
176 }
177 
178 // [12] Sequence< Sequence<T>,SZ >
180 {
181  auto ref_vec = {vector<int>{1, 2, 3, 4, 5, 6, 7}, vector<int>{8, 9, 10}};
182  auto it = ref_vec.begin();
183 
184  for (auto const& val : config().arrOfVec()) {
185  auto ref = *it++;
186  BOOST_TEST(val == ref, boost::test_tools::per_element{});
187  }
188 }
189 
190 // [13] Sequence< Sequence<T,SZ>, SZ >
192 {
193  auto ref_vec = {array<int, 2>{{1, 0}}, array<int, 2>{{0, 1}}};
194  auto it = ref_vec.begin();
195 
196  for (auto const& val : config().arrOfArr()) {
197  auto ref = *it++;
198  BOOST_TEST(val == ref, boost::test_tools::per_element{});
199  }
200 }
201 
202 BOOST_AUTO_TEST_SUITE_END()
auto const tolerance
ChannelGroupService::Name Name
STL namespace.
static Config * config
Definition: config.cpp:1054
BOOST_AUTO_TEST_CASE(atom)
static QCString str