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