Project
Loading...
Searching...
No Matches
testConfigurableParam.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#define BOOST_TEST_MODULE Test ConfigurableParams
13#define BOOST_TEST_MAIN
14#define BOOST_TEST_DYN_LINK
15
16#include <boost/test/unit_test.hpp>
17#include <boost/property_tree/ptree.hpp>
18#include <filesystem>
19
21
22using namespace o2::conf;
23using namespace o2::conf::test;
24
25BOOST_AUTO_TEST_CASE(ConfigurableParam_Basic)
26{
27 // Tests the default parameters and also getter helpers.
28 auto& param = TestParam::Instance();
29 BOOST_CHECK_EQUAL(param.iValue, 42);
30 BOOST_CHECK_EQUAL(param.dValue, 3.14);
31 BOOST_CHECK_EQUAL(param.bValue, true);
32 BOOST_CHECK_EQUAL(param.sValue, "default");
33 BOOST_CHECK_EQUAL(static_cast<int>(param.eValue), 2);
34
35 BOOST_CHECK_EQUAL(ConfigurableParam::getValueAs<int>("TestParam.iValue"), 42);
36 BOOST_CHECK_EQUAL(ConfigurableParam::getValueAs<double>("TestParam.dValue"), 3.14);
37 BOOST_CHECK_EQUAL(ConfigurableParam::getValueAs<bool>("TestParam.bValue"), true);
38 BOOST_CHECK_EQUAL(ConfigurableParam::getValueAs<std::string>("TestParam.sValue"), "default");
39}
40
41BOOST_AUTO_TEST_CASE(ConfigurableParam_SG_Fundamental)
42{
43 // tests runtime setting and getting for fundamental types
44 ConfigurableParam::setValue("TestParam.iValue", "100");
45 ConfigurableParam::setValue("TestParam.dValue", "2.718");
46 ConfigurableParam::setValue("TestParam.bValue", "0");
47 ConfigurableParam::setValue("TestParam.sValue", "modified");
48 ConfigurableParam::setValue("TestParam.eValue", "0");
49
50 auto& param = TestParam::Instance();
51 param.printKeyValues();
52 BOOST_CHECK_EQUAL(param.iValue, 100);
53 BOOST_CHECK_EQUAL(param.dValue, 2.718);
54 BOOST_CHECK_EQUAL(param.bValue, false);
55 BOOST_CHECK_EQUAL(param.sValue, "modified");
56 BOOST_CHECK_EQUAL(static_cast<int>(param.eValue), 0);
57}
58
59BOOST_AUTO_TEST_CASE(ConfigurableParam_SG_CArray)
60{
61 // tests setting and getting for a c-style array type
62 auto& param = TestParam::Instance();
63 BOOST_CHECK_EQUAL(ConfigurableParam::getValueAs<int>("TestParam.caValue[0]"), 0);
64 BOOST_CHECK_EQUAL(ConfigurableParam::getValueAs<int>("TestParam.caValue[1]"), 1);
65 BOOST_CHECK_EQUAL(ConfigurableParam::getValueAs<int>("TestParam.caValue[2]"), 2);
66
67 ConfigurableParam::setValue("TestParam.caValue[1]", "99");
68 BOOST_CHECK_EQUAL(ConfigurableParam::getValueAs<int>("TestParam.caValue[1]"), 99);
69}
70
71BOOST_AUTO_TEST_CASE(ConfigurableParam_Provenance)
72{
73 // tests correct setting of provenance
74 BOOST_CHECK_EQUAL(ConfigurableParam::getProvenance("TestParam.iValueProvenanceTest"), ConfigurableParam::EParamProvenance::kCODE);
75 ConfigurableParam::setValue("TestParam.iValueProvenanceTest", "123");
76 BOOST_CHECK_EQUAL(ConfigurableParam::getProvenance("TestParam.iValueProvenanceTest"), ConfigurableParam::EParamProvenance::kRT);
77}
78
79BOOST_AUTO_TEST_CASE(ConfigurableParam_FileIO_Ini)
80{
81 // test for ini file serialization
82 const std::string testFileName = "test_config.ini";
83 auto iValueBefore = TestParam::Instance().iValue;
84 auto sValueBefore = TestParam::Instance().sValue;
85 ConfigurableParam::writeINI(testFileName);
86 ConfigurableParam::setValue("TestParam.iValue", "999");
87 ConfigurableParam::setValue("TestParam.sValue", testFileName);
89 BOOST_CHECK_EQUAL(TestParam::Instance().iValue, iValueBefore);
90 BOOST_CHECK_EQUAL(TestParam::Instance().sValue, sValueBefore);
91 std::remove(testFileName.c_str());
92}
93
94BOOST_AUTO_TEST_CASE(ConfigurableParam_FileIO_Json)
95{
96 // test for json file serialization
97 const std::string testFileName = "test_config.json";
98 auto iValueBefore = TestParam::Instance().iValue;
99 auto sValueBefore = TestParam::Instance().sValue;
100 ConfigurableParam::writeJSON(testFileName);
101 ConfigurableParam::setValue("TestParam.iValue", "999");
102 ConfigurableParam::setValue("TestParam.sValue", testFileName);
104 BOOST_CHECK_EQUAL(TestParam::Instance().iValue, iValueBefore);
105 BOOST_CHECK_EQUAL(TestParam::Instance().sValue, sValueBefore);
106 std::remove(testFileName.c_str());
107}
108
109BOOST_AUTO_TEST_CASE(ConfigurableParam_FileIO_ROOT)
110{
111 // test for root file serialization
112 const std::string testFileName = "test_config.root";
113 auto iValueBefore = TestParam::Instance().iValue;
114 auto sValueBefore = TestParam::Instance().sValue;
115 TFile* testFile = TFile::Open(testFileName.c_str(), "RECREATE");
116 TestParam::Instance().serializeTo(testFile);
117 testFile->Close();
118 ConfigurableParam::setValue("TestParam.iValue", "999");
119 ConfigurableParam::setValue("TestParam.sValue", testFileName);
120 ConfigurableParam::fromCCDB(testFileName);
121 BOOST_CHECK_EQUAL(TestParam::Instance().iValue, iValueBefore);
122 BOOST_CHECK_EQUAL(TestParam::Instance().sValue, sValueBefore);
123 std::remove(testFileName.c_str());
124}
125
126BOOST_AUTO_TEST_CASE(ConfigurableParam_Cli)
127{
128 // test setting values from as a cli arg string
129 ConfigurableParam::updateFromString("TestParam.iValue=55;TestParam.sValue=cli");
130 BOOST_CHECK_EQUAL(TestParam::Instance().iValue, 55);
131 BOOST_CHECK_EQUAL(TestParam::Instance().sValue, "cli");
132}
133
134BOOST_AUTO_TEST_CASE(ConfigurableParam_LiteralSuffix)
135{
136 // test setting values with the correct literal suffix
137 ConfigurableParam::updateFromString("TestParam.fValue=42.f");
138 BOOST_CHECK_EQUAL(TestParam::Instance().fValue, 42.f);
139
140 ConfigurableParam::setValue("TestParam.ullValue", "999ull");
141 BOOST_CHECK_EQUAL(TestParam::Instance().ullValue, 999ULL);
142 // check using wrong literal suffix fails, prints error to std
143 ConfigurableParam::setValue("TestParam.ullValue", "888u");
144 BOOST_CHECK_NE(TestParam::Instance().ullValue, 888);
145}
static void writeINI(std::string const &filename, std::string const &keyOnly="")
static void updateFromFile(std::string const &, std::string const &paramsList="", bool unchangedOnly=false)
static void setValue(std::string const &mainkey, std::string const &subkey, T x)
static EParamProvenance getProvenance(const std::string &key)
static void writeJSON(std::string const &filename, std::string const &keyOnly="")
static void fromCCDB(std::string filename)
static void updateFromString(std::string const &)
GLenum GLfloat param
Definition glcorearb.h:271
BOOST_AUTO_TEST_CASE(ConfigurableParam_Basic)
BOOST_CHECK_EQUAL(triggersD.size(), triggers.size())