38 bpo::options_description testOptions(
"Test options");
39 testOptions.add_options()
40 (
"aFloat", bpo::value<float>()->default_value(10.f))
41 (
"aDouble", bpo::value<double>()->default_value(20.))
42 (
"anInt", bpo::value<int>()->default_value(1))
43 (
"anInt8", bpo::value<int8_t>()->default_value(1))
44 (
"anInt16", bpo::value<int16_t>()->default_value(1))
45 (
"anUInt8", bpo::value<uint8_t>()->default_value(1))
46 (
"anUInt16", bpo::value<uint16_t>()->default_value(1))
47 (
"anUInt32", bpo::value<uint32_t>()->default_value(1))
48 (
"anUInt64", bpo::value<uint64_t>()->default_value(1))
49 (
"anInt64", bpo::value<int64_t>()->default_value(1ll))
50 (
"aBoolean", bpo::value<bool>()->zero_tokens()->default_value(
false))
51 (
"aString,s", bpo::value<std::string>()->default_value(
"something"))
52 (
"aNested.x", bpo::value<int>()->default_value(2))
53 (
"aNested.y", bpo::value<float>()->default_value(3.f));
55 fair::mq::ProgOptions* options =
new fair::mq::ProgOptions();
56 options->AddToCmdLineOptions(testOptions);
57 options->ParseAll({
"cmd",
"--aFloat",
"1.0",
66 "--anInt64",
"50000000000000",
68 "-s",
"somethingelse",
72 std::vector<ConfigParamSpec> specs{
75 ConfigParamSpec{
"anInt16", VariantType::Int16,
static_cast<int16_t
>(1), {
"an int16 option"}},
78 ConfigParamSpec{
"anUInt32", VariantType::UInt32, 1u, {
"an uint32 option"}},
80 ConfigParamSpec{
"anInt64", VariantType::Int64, 1ll, {
"an int64_t option"}},
81 ConfigParamSpec{
"aFloat", VariantType::Float, 2.0f, {
"a float option"}},
82 ConfigParamSpec{
"aDouble", VariantType::Double, 3., {
"a double option"}},
83 ConfigParamSpec{
"aString,s", VariantType::String,
"foo", {
"a string option"}},
84 ConfigParamSpec{
"aBoolean", VariantType::Bool,
true, {
"a boolean option"}},
85 ConfigParamSpec{
"aNested.x", VariantType::Int, 2, {
"an int option, nested in an object"}},
86 ConfigParamSpec{
"aNested.y", VariantType::Float, 3.f, {
"a float option, nested in an object"}},
87 ConfigParamSpec{
"aNested.z", VariantType::Float, 4.f, {
"a float option, nested in an object"}},
90 std::vector<std::unique_ptr<ParamRetriever>> retrievers;
92 retrievers.emplace_back(std::move(fairmqRetriver));
94 auto store = std::make_unique<ConfigParamStore>(specs, std::move(retrievers));
99 REQUIRE(registry.
get<
float>(
"aFloat") == 1.0);
100 REQUIRE(registry.
get<
double>(
"aDouble") == 2.0);
101 REQUIRE(registry.
get<
int>(
"anInt") == 10);
102 REQUIRE(registry.
get<
int8_t>(
"anInt8") ==
'2');
103 REQUIRE(registry.
get<int16_t>(
"anInt16") == 10);
104 REQUIRE(registry.
get<
uint8_t>(
"anUInt8") ==
'2');
108 REQUIRE(registry.
get<
int64_t>(
"anInt64") == 50000000000000ll);
109 REQUIRE(registry.
get<
bool>(
"aBoolean") ==
true);
110 REQUIRE(registry.
get<std::string>(
"aString") ==
"somethingelse");
111 REQUIRE(registry.
get<
int>(
"aNested.x") == 1);
112 REQUIRE(registry.
get<
float>(
"aNested.y") == 2.f);
113 REQUIRE(registry.
get<
float>(
"aNested.z") == 4.f);
115 auto pt = registry.
get<boost::property_tree::ptree>(
"aNested");
116 auto pt2 = registry.
get<boost::property_tree::ptree>(
"aDict");
117 REQUIRE(pt.get<
int>(
"x") == 1);
118 REQUIRE(pt.get<
float>(
"y") == 2.f);
122 REQUIRE(obj.
y == 2.f);
127 float m[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
128 int l[2][2] = {{1, 2}, {3, 4}};
130 std::vector<int> defaultVector{1, 2, 3, 4, 5};
131 std::vector<std::string> defaultStringVector{
"a",
"b",
"c"};
132 std::vector<double> defaultNestedVector{0.5, 1.5};
134 std::vector<ConfigParamSpec> specs{
136 ConfigParamSpec{
"aString", VariantType::String,
"foo", {
"a string option"}},
137 ConfigParamSpec{
"aVector", VariantType::ArrayInt, defaultVector, {
"an int array option"}},
138 ConfigParamSpec{
"aStringVector", VariantType::ArrayString, defaultStringVector, {
"a string array option"}},
140 ConfigParamSpec{
"aLabeledArray", VariantType::LabeledArrayInt,
LabeledArray<int>{&l[0][0], 2, 2, {
"r1",
"r2"}, {
"c1",
"c2"}}, {
"a labeled array option"}},
141 ConfigParamSpec{
"aNested.x", VariantType::Int, 2, {
"an int option, nested in an object"}},
142 ConfigParamSpec{
"aNested.vec", VariantType::ArrayDouble, defaultNestedVector, {
"a double array option, nested in an object"}},
144 auto store = std::make_unique<ConfigParamStore>(specs, std::vector<std::unique_ptr<ParamRetriever>>{});
148 REQUIRE(registry.
get<std::vector<int>>(
"aVector").size() == 5);
152 REQUIRE(registry.
get<
int>(
"anInt") == 10);
155 registry.
override(
"aVector", std::vector<int>{7, 8});
156 REQUIRE(registry.
get<std::vector<int>>(
"aVector") == std::vector<int>{7, 8});
157 registry.
override(
"aStringVector", std::vector<std::string>{
"x"});
158 REQUIRE(registry.
get<std::vector<std::string>>(
"aStringVector") == std::vector<std::string>{
"x"});
161 float m2[1][2] = {{0.25, 0.75}};
164 REQUIRE(a2d.rows == 1);
165 REQUIRE(a2d.cols == 2);
166 REQUIRE(a2d(0, 1) == 0.75f);
169 int l2[1][3] = {{5, 6, 7}};
172 REQUIRE(la.getLabelsRows() == std::vector<std::string>{
"row"});
173 REQUIRE(la.getLabelsCols() == std::vector<std::string>{
"x",
"y",
"z"});
174 REQUIRE(la.get(
"row",
"z") == 7);
178 REQUIRE(la.getLabelsRows().empty());
179 REQUIRE(la.getLabelsCols().empty());
180 REQUIRE(la.get(1U, 0U) == 3);
183 registry.
override(
"aNested.vec", std::vector<double>{2.5});
184 REQUIRE(registry.
get<std::vector<double>>(
"aNested.vec") == std::vector<double>{2.5});
185 REQUIRE(registry.
get<
int>(
"aNested.x") == 2);
186 REQUIRE(registry.
get<std::string>(
"aString") ==
"foo");
187 REQUIRE(registry.
get<std::vector<int>>(
"aVector") == std::vector<int>{7, 8});
190 REQUIRE(registry.
hasOption(
"aNewVector") ==
false);
191 registry.
override(
"aNewVector", std::vector<float>{1.f, 2.f});
192 REQUIRE(registry.
hasOption(
"aNewVector"));
193 REQUIRE(registry.
get<std::vector<float>>(
"aNewVector") == std::vector<float>{1.f, 2.f});
196 registry.
override(
"aString", std::vector<std::string>{
"now",
"an",
"array"});
197 REQUIRE(registry.
get<std::vector<std::string>>(
"aString") == std::vector<std::string>{
"now",
"an",
"array"});
202 int l[2][2] = {{1, 2}, {3, 4}};
204 std::vector<int> defaultVector{1, 2, 3, 4, 5};
205 std::vector<std::string> defaultStringVector{
"a",
"b",
"c"};
206 std::vector<double> defaultNestedVector{0.5, 1.5};
208 std::vector<ConfigParamSpec> specs{
210 ConfigParamSpec{
"aString", VariantType::String,
"foo", {
"a string option"}},
211 ConfigParamSpec{
"aVector", VariantType::ArrayInt, defaultVector, {
"an int array option"}},
212 ConfigParamSpec{
"aStringVector", VariantType::ArrayString, defaultStringVector, {
"a string array option"}},
213 ConfigParamSpec{
"aLabeledArray", VariantType::LabeledArrayInt,
LabeledArray<int>{&l[0][0], 2, 2, {
"r1",
"r2"}, {
"c1",
"c2"}}, {
"a labeled array option"}},
214 ConfigParamSpec{
"aNested.x", VariantType::Int, 2, {
"an int option, nested in an object"}},
215 ConfigParamSpec{
"aNested.vec", VariantType::ArrayDouble, defaultNestedVector, {
"a double array option, nested in an object"}},
217 auto store = std::make_unique<ConfigParamStore>(specs, std::vector<std::unique_ptr<ParamRetriever>>{});
224 registry.
erase(
"anInt");
225 REQUIRE(registry.
hasOption(
"anInt") ==
false);
226 REQUIRE(registry.
isSet(
"anInt") ==
false);
227 REQUIRE(registry.
isDefault(
"anInt") ==
false);
228 REQUIRE_THROWS_AS(registry.
get<
int>(
"anInt"), std::invalid_argument);
231 registry.
erase(
"aVector");
232 REQUIRE(registry.
hasOption(
"aVector") ==
false);
233 REQUIRE(registry.
isSet(
"aVector") ==
false);
234 registry.
erase(
"aLabeledArray");
235 REQUIRE(registry.
hasOption(
"aLabeledArray") ==
false);
236 REQUIRE(registry.
isSet(
"aLabeledArray") ==
false);
239 registry.
erase(
"aNested.vec");
240 REQUIRE(registry.
hasOption(
"aNested.vec") ==
false);
241 REQUIRE(registry.
get<
int>(
"aNested.x") == 2);
244 REQUIRE(registry.
get<std::string>(
"aString") ==
"foo");
245 REQUIRE(registry.
get<std::vector<std::string>>(
"aStringVector") == std::vector<std::string>{
"a",
"b",
"c"});
248 REQUIRE_NOTHROW(registry.
erase(
"anInt"));
249 REQUIRE_NOTHROW(registry.
erase(
"noSuchKey"));
250 REQUIRE_NOTHROW(registry.
erase(
"noSuchParent.x"));
251 REQUIRE_NOTHROW(registry.
erase(
"aNested.noSuchKey"));
252 REQUIRE(registry.
get<
int>(
"aNested.x") == 2);
256 REQUIRE(registry.
get<
int>(
"anInt") == 3);
257 registry.
override(
"aVector", std::vector<int>{7, 8});
258 REQUIRE(registry.
get<std::vector<int>>(
"aVector") == std::vector<int>{7, 8});