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