Project
Loading...
Searching...
No Matches
test_ConfigParamRegistry.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>
15
16#include <fairmq/ProgOptions.h>
17#include <boost/program_options.hpp>
18
19namespace bpo = boost::program_options;
20using namespace o2::framework;
21
22struct Foo {
23 // Providing a class with a constructor which takes a ptree
24 // allows for getting the object
25 explicit Foo(boost::property_tree::ptree in)
26 : x{in.get<int>("x")},
27 y{in.get<float>("y")}
28 {
29 }
30 int x;
31 float y;
32};
33
34TEST_CASE("TestConfigParamRegistry")
35{
36 bpo::options_description testOptions("Test options");
37 testOptions.add_options() //
38 ("aFloat", bpo::value<float>()->default_value(10.f)) //
39 ("aDouble", bpo::value<double>()->default_value(20.)) //
40 ("anInt", bpo::value<int>()->default_value(1)) //
41 ("anInt8", bpo::value<int8_t>()->default_value(1)) //
42 ("anInt16", bpo::value<int16_t>()->default_value(1)) //
43 ("anUInt8", bpo::value<uint8_t>()->default_value(1)) //
44 ("anUInt16", bpo::value<uint16_t>()->default_value(1)) //
45 ("anUInt32", bpo::value<uint32_t>()->default_value(1)) //
46 ("anUInt64", bpo::value<uint64_t>()->default_value(1)) //
47 ("anInt64", bpo::value<int64_t>()->default_value(1ll)) //
48 ("aBoolean", bpo::value<bool>()->zero_tokens()->default_value(false)) //
49 ("aString,s", bpo::value<std::string>()->default_value("something")) //
50 ("aNested.x", bpo::value<int>()->default_value(2)) //
51 ("aNested.y", bpo::value<float>()->default_value(3.f)); //
52
53 fair::mq::ProgOptions* options = new fair::mq::ProgOptions();
54 options->AddToCmdLineOptions(testOptions);
55 options->ParseAll({"cmd", "--aFloat", "1.0",
56 "--aDouble", "2.0",
57 "--anInt", "10",
58 "--anInt8", "2",
59 "--anInt16", "10",
60 "--anUInt8", "2",
61 "--anUInt16", "10",
62 "--anUInt32", "10",
63 "--anUInt64", "10",
64 "--anInt64", "50000000000000",
65 "--aBoolean",
66 "-s", "somethingelse",
67 "--aNested.x", "1",
68 "--aNested.y", "2"},
69 true);
70 std::vector<ConfigParamSpec> specs{
71 ConfigParamSpec{"anInt", VariantType::Int, 1, {"an int option"}},
72 ConfigParamSpec{"anInt8", VariantType::Int8, static_cast<int8_t>(1), {"an int8 option"}},
73 ConfigParamSpec{"anInt16", VariantType::Int16, static_cast<int16_t>(1), {"an int16 option"}},
74 ConfigParamSpec{"anUInt8", VariantType::UInt8, static_cast<uint8_t>(1u), {"an uint8 option"}},
75 ConfigParamSpec{"anUInt16", VariantType::UInt16, static_cast<uint16_t>(1u), {"an uint16 option"}},
76 ConfigParamSpec{"anUInt32", VariantType::UInt32, 1u, {"an uint32 option"}},
77 ConfigParamSpec{"anUInt64", VariantType::UInt64, static_cast<uint64_t>(1ul), {"an uint64 option"}},
78 ConfigParamSpec{"anInt64", VariantType::Int64, 1ll, {"an int64_t option"}},
79 ConfigParamSpec{"aFloat", VariantType::Float, 2.0f, {"a float option"}},
80 ConfigParamSpec{"aDouble", VariantType::Double, 3., {"a double option"}},
81 ConfigParamSpec{"aString,s", VariantType::String, "foo", {"a string option"}},
82 ConfigParamSpec{"aBoolean", VariantType::Bool, true, {"a boolean option"}},
83 ConfigParamSpec{"aNested.x", VariantType::Int, 2, {"an int option, nested in an object"}},
84 ConfigParamSpec{"aNested.y", VariantType::Float, 3.f, {"a float option, nested in an object"}},
85 ConfigParamSpec{"aNested.z", VariantType::Float, 4.f, {"a float option, nested in an object"}},
86 ConfigParamSpec{"aDict", VariantType::Dict, emptyDict(), {"A dictionary"}}};
87
88 std::vector<std::unique_ptr<ParamRetriever>> retrievers;
89 std::unique_ptr<ParamRetriever> fairmqRetriver{new FairOptionsRetriever(options)};
90 retrievers.emplace_back(std::move(fairmqRetriver));
91
92 auto store = std::make_unique<ConfigParamStore>(specs, std::move(retrievers));
93 store->preload();
94 store->activate();
95 ConfigParamRegistry registry(std::move(store));
96
97 REQUIRE(registry.get<float>("aFloat") == 1.0);
98 REQUIRE(registry.get<double>("aDouble") == 2.0);
99 REQUIRE(registry.get<int>("anInt") == 10);
100 REQUIRE(registry.get<int8_t>("anInt8") == '2');
101 REQUIRE(registry.get<int16_t>("anInt16") == 10);
102 REQUIRE(registry.get<uint8_t>("anUInt8") == '2');
103 REQUIRE(registry.get<uint16_t>("anUInt16") == 10);
104 REQUIRE(registry.get<uint32_t>("anUInt32") == 10);
105 REQUIRE(registry.get<uint64_t>("anUInt64") == 10);
106 REQUIRE(registry.get<int64_t>("anInt64") == 50000000000000ll);
107 REQUIRE(registry.get<bool>("aBoolean") == true);
108 REQUIRE(registry.get<std::string>("aString") == "somethingelse");
109 REQUIRE(registry.get<int>("aNested.x") == 1);
110 REQUIRE(registry.get<float>("aNested.y") == 2.f);
111 REQUIRE(registry.get<float>("aNested.z") == 4.f);
112 // We can get nested objects also via their top-level ptree.
113 auto pt = registry.get<boost::property_tree::ptree>("aNested");
114 auto pt2 = registry.get<boost::property_tree::ptree>("aDict");
115 REQUIRE(pt.get<int>("x") == 1);
116 REQUIRE(pt.get<float>("y") == 2.f);
117 // And we can get it as a generic object as well.
118 Foo obj = registry.get<Foo>("aNested");
119 REQUIRE(obj.x == 1);
120 REQUIRE(obj.y == 2.f);
121}
GLint GLenum GLint x
Definition glcorearb.h:403
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
Variant emptyDict()
Definition Variant.h:407
Foo(boost::property_tree::ptree in)
TEST_CASE("TestConfigParamRegistry")