Project
Loading...
Searching...
No Matches
test_DeviceConfigInfo.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
14#include "Framework/Variant.h"
15#include <catch_amalgamated.hpp>
16#include <boost/tokenizer.hpp>
17#include <boost/lexical_cast.hpp>
18#include <string_view>
19#include <regex>
20#include <uv.h>
21
22template <typename T>
23std::string arrayPrinter(boost::property_tree::ptree const& tree)
24{
25 std::stringstream ss;
26 int size = tree.size();
27 int count = 0;
28 ss << o2::framework::variant_array_symbol<T>::symbol << "[";
29 for (auto& element : tree) {
30 ss << element.second.get_value<T>();
31 if (count < size - 1) {
32 ss << ",";
33 }
34 ++count;
35 }
36 ss << "]";
37 return ss.str();
38}
39
40TEST_CASE("TestDeviceConfigInfo")
41{
42 using namespace o2::framework;
43 std::string configString;
44 bool result;
46 DeviceInfo info;
47
48 // Something which does not match
49 configString = "foo[NOCONFIG] foo=bar 1789372894\n";
50 std::string_view config1{configString.data() + 3, configString.size() - 4};
51 result = DeviceConfigHelper::parseConfig(config1, match);
52 REQUIRE(result == false);
53
54 // Something which does not match
55 configString = "foo[CONFIG] foobar 1789372894\n";
56 std::string_view config2{configString.data() + 3, configString.size() - 4};
57 result = DeviceConfigHelper::parseConfig(config2, match);
58 REQUIRE(result == false);
59
60 // Something which does not match
61 configString = "foo[CONFIG] foo=bar1789372894\n";
62 std::string_view config3{configString.data() + 3, configString.size() - 4};
63 result = DeviceConfigHelper::parseConfig(config3, match);
64 REQUIRE(result == false);
65
66 // Something which does not match
67 configString = "foo[CONFIG] foo=bar\n";
68 std::string_view config4{configString.data() + 3, configString.size() - 4};
69 result = DeviceConfigHelper::parseConfig(config4, match);
70 REQUIRE(result == false);
71
72 // Something which does not match
73 configString = "foo[CONFIG] foo=bar 1789372894t\n";
74 std::string_view config5{configString.data() + 3, configString.size() - 4};
75 result = DeviceConfigHelper::parseConfig(config5, match);
76 REQUIRE(result == false);
77
78 // Parse a simple configuration bit
79 configString = "foo[CONFIG] foo=bar 1789372894\n";
80 std::string_view config6{configString.data() + 3, configString.size() - 4};
81 result = DeviceConfigHelper::parseConfig(config6, match);
82 REQUIRE(result == false);
83
84 // Parse a simple configuration bit
85 configString = "foo[CONFIG] foo=bar 1789372894 prov\n";
86 std::string_view config{configString.data() + 3, configString.size() - 4};
87 REQUIRE(config == std::string("[CONFIG] foo=bar 1789372894 prov"));
88 result = DeviceConfigHelper::parseConfig(config, match);
89 REQUIRE(result == true);
90 REQUIRE(strncmp(match.beginKey, "foo", 3) == 0);
91 REQUIRE(match.timestamp == 1789372894);
92 REQUIRE(strncmp(match.beginValue, "bar", 3) == 0);
93 REQUIRE(strncmp(match.beginProvenance, "prov", 4) == 0);
94
95 // Parse a simple configuration bit with a space in the value
96 configString = "foo[CONFIG] foo=bar and foo 1789372894 prov\n";
97 std::string_view configWithSpace{configString.data() + 3, configString.size() - 4};
98 REQUIRE(configWithSpace == std::string("[CONFIG] foo=bar and foo 1789372894 prov"));
99 result = DeviceConfigHelper::parseConfig(configWithSpace, match);
100 REQUIRE(result == true);
101 REQUIRE(strncmp(match.beginKey, "foo", 3) == 0);
102 REQUIRE(match.timestamp == 1789372894);
103 REQUIRE(strncmp(match.beginValue, "bar and foo", 11) == 0);
104 REQUIRE(strncmp(match.beginProvenance, "prov", 4) == 0);
105
106 // Process a given config entry
107 result = DeviceConfigHelper::processConfig(match, info);
108 REQUIRE(result == true);
109 REQUIRE(info.currentConfig.get<std::string>("foo") == "bar and foo");
110 REQUIRE(info.currentProvenance.get<std::string>("foo") == "prov");
111
112 // Parse an array
113 configString = "foo[CONFIG] array={\"\":\"1\",\"\":\"2\",\"\":\"3\",\"\":\"4\",\"\":\"5\"} 1789372894 prov\n";
114 std::string_view configa{configString.data() + 3, configString.size() - 4};
115 result = DeviceConfigHelper::parseConfig(configa, match);
116 auto valueString = std::string(match.beginValue, match.endValue - match.beginValue);
117
118 REQUIRE(result == true);
119 REQUIRE(strncmp(match.beginKey, "array", 5) == 0);
120 REQUIRE(match.timestamp == 1789372894);
121 REQUIRE(strncmp(match.beginValue, "{\"\":\"1\",\"\":\"2\",\"\":\"3\",\"\":\"4\",\"\":\"5\"}", 35) == 0);
122 REQUIRE(strncmp(match.beginProvenance, "prov", 4) == 0);
123
124 // Process a given config entry
125 result = DeviceConfigHelper::processConfig(match, info);
126 REQUIRE(result == true);
127 REQUIRE(info.currentProvenance.get<std::string>("array") == "prov");
128 REQUIRE(arrayPrinter<int>(info.currentConfig.get_child("array")) == "i[1,2,3,4,5]");
129}
bool match(const std::vector< std::string > &queries, const char *pattern)
Definition dcs-ccdb.cxx:229
GLint GLsizei count
Definition glcorearb.h:399
GLuint64EXT * result
Definition glcorearb.h:5662
GLsizeiptr size
Definition glcorearb.h:659
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
boost::property_tree::ptree currentConfig
Current configuration for the device.
Definition DeviceInfo.h:82
boost::property_tree::ptree currentProvenance
Current provenance for the configuration keys.
Definition DeviceInfo.h:84
Temporary struct to hold a configuration parameter.
TEST_CASE("TestDeviceConfigInfo")
std::string arrayPrinter(boost::property_tree::ptree const &tree)
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))