Project
Loading...
Searching...
No Matches
test_FairMQOptionsRetriever.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include <catch_amalgamated.hpp>
13#include <boost/program_options.hpp>
18#include <fairmq/ProgOptions.h>
19#include <boost/property_tree/ptree.hpp>
20
21namespace bpo = boost::program_options;
22using namespace o2::framework;
23
24TEST_CASE("TestFairMQOptionsRetriever")
25{
26 bpo::options_description testOptions("Test options");
27 testOptions.add_options() //
28 ("aFloat", bpo::value<float>()->default_value(10.f)) //
29 ("aDouble", bpo::value<double>()->default_value(20.)) //
30 ("anInt", bpo::value<int>()->default_value(1)) //
31 ("anInt64", bpo::value<int64_t>()->default_value(1ll)) //
32 ("aBoolean", bpo::value<bool>()->zero_tokens()->default_value(false)) //
33 ("aString", bpo::value<std::string>()->default_value("something")) //
34 ("aNested.int", bpo::value<int>()->default_value(2)) //
35 ("aNested.float", bpo::value<float>()->default_value(3.f)); //
36
37 fair::mq::ProgOptions* options = new fair::mq::ProgOptions();
38 options->AddToCmdLineOptions(testOptions);
39 options->ParseAll({"cmd", "--aFloat", "1.0",
40 "--aDouble", "2.0",
41 "--anInt", "10",
42 "--anInt64", "50000000000000",
43 "--aBoolean",
44 "--aString", "somethingelse",
45 "--aNested.int", "1",
46 "--aNested.float", "2"},
47 true);
48 std::vector<ConfigParamSpec> specs{
49 ConfigParamSpec{"anInt", VariantType::Int, 1, {"an int option"}},
50 ConfigParamSpec{"anInt64", VariantType::Int64, 1ll, {"an int64_t option"}},
51 ConfigParamSpec{"aFloat", VariantType::Float, 2.0f, {"a float option"}},
52 ConfigParamSpec{"aDouble", VariantType::Double, 3., {"a double option"}},
53 ConfigParamSpec{"aString", VariantType::String, "foo", {"a string option"}},
54 ConfigParamSpec{"aBoolean", VariantType::Bool, true, {"a boolean option"}},
55 ConfigParamSpec{"aNested.int", VariantType::Int, 2, {"an int option, nested in an object"}},
56 ConfigParamSpec{"aNested.float", VariantType::Float, 3.f, {"a float option, nested in an object"}},
57 };
58 std::vector<std::unique_ptr<ParamRetriever>> retrievers;
59 std::unique_ptr<ParamRetriever> fairmqRetriver{new FairOptionsRetriever(options)};
60 retrievers.emplace_back(std::move(fairmqRetriver));
61
62 ConfigParamStore store{specs, std::move(retrievers)};
63 store.preload();
64 store.activate();
65
66 REQUIRE(store.store().get<float>("aFloat") == 1.0);
67 REQUIRE(store.store().get<double>("aDouble") == 2.0);
68 REQUIRE(store.store().get<int>("anInt") == 10);
69 REQUIRE(store.store().get<int64_t>("anInt64") == 50000000000000ll);
70 REQUIRE(store.store().get<bool>("aBoolean") == true);
71 REQUIRE(store.store().get<std::string>("aString") == "somethingelse");
72 REQUIRE(store.store().get<int>("aNested.int") == 1);
73 REQUIRE(store.store().get<float>("aNested.float") == 2.f);
74 // We can get nested objects also via their top-level ptree.
75 auto pt = store.store().get_child("aNested");
76 REQUIRE(pt.get<int>("int") == 1);
77 REQUIRE(pt.get<float>("float") == 2.f);
78}
79
80TEST_CASE("TestOptionsDefaults")
81{
82 bpo::options_description testOptions("Test options");
83 testOptions.add_options() //
84 ("aFloat", bpo::value<float>()->default_value(10.f)) //
85 ("aDouble", bpo::value<double>()->default_value(20.)) //
86 ("anInt", bpo::value<int>()->default_value(1)) //
87 ("anInt64", bpo::value<int64_t>()->default_value(-50000000000000ll)) //
88 ("aBoolean", bpo::value<bool>()->zero_tokens()->default_value(false)) //
89 ("aString", bpo::value<std::string>()->default_value("something")) //
90 ("aNested.int", bpo::value<int>()->default_value(2)) //
91 ("aNested.float", bpo::value<float>()->default_value(3.f)); //
92
93 fair::mq::ProgOptions* options = new fair::mq::ProgOptions();
94 options->AddToCmdLineOptions(testOptions);
95 options->ParseAll({"cmd"}, true);
96 std::vector<ConfigParamSpec> specs{
97 ConfigParamSpec{"anInt", VariantType::Int, 1, {"an int option"}},
98 ConfigParamSpec{"anInt64", VariantType::Int64, -50000000000000ll, {"an int64_t option"}},
99 ConfigParamSpec{"aFloat", VariantType::Float, 2.0f, {"a float option"}},
100 ConfigParamSpec{"aDouble", VariantType::Double, 3., {"a double option"}},
101 ConfigParamSpec{"aString", VariantType::String, "foo", {"a string option"}},
102 ConfigParamSpec{"aBoolean", VariantType::Bool, true, {"a boolean option"}},
103 ConfigParamSpec{"aNested.int", VariantType::Int, 2, {"an int option, nested in an object"}},
104 ConfigParamSpec{"aNested.float", VariantType::Float, 3.f, {"a float option, nested in an object"}},
105 ConfigParamSpec{"aNested.double", VariantType::Double, 4., {"a float option, nested in an object"}},
106 };
107 std::vector<std::unique_ptr<ParamRetriever>> retrievers;
108 std::unique_ptr<ParamRetriever> fairmqRetriver{new FairOptionsRetriever(options)};
109 retrievers.emplace_back(std::move(fairmqRetriver));
110
111 ConfigParamStore store{specs, std::move(retrievers)};
112 store.preload();
113 store.activate();
114
115 REQUIRE(store.store().get<float>("aFloat") == 10.f);
116 REQUIRE(store.store().get<double>("aDouble") == 20.);
117 REQUIRE(store.store().get<int>("anInt") == 1);
118 REQUIRE(store.store().get<int64_t>("anInt64") == -50000000000000ll);
119 REQUIRE(store.store().get<bool>("aBoolean") == false);
120 REQUIRE(store.store().get<std::string>("aString") == "something");
121 REQUIRE(store.store().get<int>("aNested.int") == 2);
122 REQUIRE(store.store().get<float>("aNested.float") == 3.f);
123
125 REQUIRE(store.provenance("aFloat") == "fairmq");
126 REQUIRE(store.provenance("aDouble") == "fairmq");
127 REQUIRE(store.provenance("anInt") == "fairmq");
128 REQUIRE(store.provenance("anInt64") == "fairmq");
129 REQUIRE(store.provenance("aBoolean") == "fairmq");
130 REQUIRE(store.provenance("aString") == "fairmq");
131 REQUIRE(store.provenance("aNested.int") == "fairmq");
132 REQUIRE(store.provenance("aNested.float") == "fairmq");
133 REQUIRE(store.provenance("aNested.double") == "default");
134}
135
136TEST_CASE("TestFairMQOptionsRetrieverLabeledArray")
137{
138 int m[2][2] = {{1, 2}, {3, 4}};
139 std::vector<ConfigParamSpec> specs{
140 ConfigParamSpec{"aLabeledArray", VariantType::LabeledArrayInt, LabeledArray<int>{&m[0][0], 2, 2, {"r1", "r2"}, {"c1", "c2"}}, {"a labeled array option"}},
141 };
142 // options registered as for a device
143 bpo::options_description testOptions("Test options");
145
146 auto storeFromCommandLine = [&](std::vector<std::string> const& args) {
147 auto options = std::make_unique<fair::mq::ProgOptions>();
148 options->AddToCmdLineOptions(testOptions);
149 options->ParseAll(args, true);
150 std::vector<std::unique_ptr<ParamRetriever>> retrievers;
151 retrievers.emplace_back(std::make_unique<FairOptionsRetriever>(options.get()));
152 auto store = std::make_unique<ConfigParamStore>(specs, std::move(retrievers));
153 store->preload();
154 store->activate();
155 return labeledArrayFromBranch<int>(store->store().get_child("aLabeledArray"));
156 };
157
158 // the full JSON, as forwarded by the driver
159 auto la = storeFromCommandLine({"cmd", "--aLabeledArray", R"({"labels_rows":["a","b"],"labels_cols":["x","y"],"values":[[5,6],[7,8]]})"});
160 REQUIRE(la.getLabelsRows() == std::vector<std::string>{"a", "b"});
161 REQUIRE(la.getLabelsCols() == std::vector<std::string>{"x", "y"});
162 REQUIRE(la.get("a", "y") == 6);
163
164 // no argument: the default
165 la = storeFromCommandLine({"cmd"});
166 REQUIRE(la.get("r1", "c2") == 2);
167
168 // only values: the default labels are used
169 la = storeFromCommandLine({"cmd", "--aLabeledArray", R"({"values":[[5,6],[7,8]]})"});
170 REQUIRE(la.getLabelsRows() == std::vector<std::string>{"r1", "r2"});
171 REQUIRE(la.get("r2", "c2") == 8);
172
173 // values which do not match the default labels
174 REQUIRE_THROWS(storeFromCommandLine({"cmd", "--aLabeledArray", R"({"values":[[5,6,7]]})"}));
175}
const GLfloat * m
Definition glcorearb.h:4066
Defining ITS Vertex explicitly as messageable.
Definition Cartesian.h:288
TEST_CASE("test_prepareArguments")
static void populateBoostProgramOptions(options_description &options, const std::vector< ConfigParamSpec > &specs, options_description vetos=options_description())