12#include <catch_amalgamated.hpp>
24 return strcmp(err.what,
"Mismatch between types") == 0;
29 std::ostringstream ss{};
31 REQUIRE(
a.get<
int>() == 10);
34 REQUIRE(
b.get<
float>() == 10.1f);
37 REQUIRE(
c.get<
double>() == 10.2);
42 REQUIRE(std::string(d.
get<
const char*>()) ==
"foo");
43 REQUIRE(std::string(d.
get<std::string_view>()) ==
"foo");
44 REQUIRE(std::string(d.
get<std::string>()) ==
"foo");
47 REQUIRE(e.
get<
bool>() ==
true);
50 REQUIRE(
f.get<
bool>() ==
false);
52 REQUIRE(ss.str() ==
"1010.110.2foo");
54 std::vector<Variant> vector{1, 1.2, 1.1f,
"foo"};
60 REQUIRE(std::string(sb.
get<
const char*>()) ==
"foo");
61 REQUIRE(std::string(sc.
get<
const char*>()) ==
"foo");
62 REQUIRE(std::string(sd.
get<
const char*>()) ==
"foo");
63 REQUIRE(sa.
get<
const char*>() ==
nullptr);
65 int iarr[] = {1, 2, 3, 4, 5};
66 float farr[] = {0.2, 0.3, 123.123, 123.123, 3.005e-5, 1.1e6};
67 std::vector<double> dvec = {0.1, 0.2, 0.4, 0.9, 1.3, 14.5, 123.234, 1.213e-20};
72 REQUIRE(viarr.
size() == 5);
73 REQUIRE(viarr.
get<
int*>() != iarr);
74 for (
auto i = 0u;
i < viarr.
size(); ++
i) {
75 REQUIRE(iarr[
i] == (viarr.
get<
int*>())[
i]);
78 REQUIRE(vfarr.
size() == 6);
79 REQUIRE(vfarr.
get<
float*>() != farr);
80 for (
auto i = 0u;
i < vfarr.
size(); ++
i) {
81 REQUIRE(farr[
i] == (vfarr.
get<
float*>())[
i]);
84 REQUIRE(vdvec.
size() == dvec.size());
85 REQUIRE(vdvec.
get<
double*>() != dvec.data());
86 for (
auto i = 0u;
i < dvec.size(); ++
i) {
87 REQUIRE(dvec[
i] == (vdvec.
get<
double*>())[
i]);
94 REQUIRE(vfarr.
get<
float*>() ==
nullptr);
96 REQUIRE(fb.
get<
float*>() != farr);
97 for (
auto i = 0u;
i < fb.
size(); ++
i) {
98 REQUIRE(farr[
i] == (fb.
get<
float*>())[
i]);
100 REQUIRE(fc.
get<
float*>() != farr);
101 for (
auto i = 0u;
i < fc.
size(); ++
i) {
102 REQUIRE(farr[
i] == (fc.
get<
float*>())[
i]);
104 REQUIRE(fd.
get<
float*>() != farr);
105 for (
auto i = 0u;
i < fd.
size(); ++
i) {
106 REQUIRE(farr[
i] == (fd.
get<
float*>())[
i]);
109 std::vector<std::string> vstrings{
"s1",
"s2",
"s3"};
110 std::string
strings[] = {
"l1",
"l2",
"l3"};
114 REQUIRE(vstr.
size() == 3);
115 REQUIRE(vvstr.
size() == 3);
116 for (
auto i = 0u;
i < vstr.
size(); ++
i) {
119 for (
auto i = 0u;
i < vvstr.
size(); ++
i) {
120 REQUIRE(vstrings[
i] == (vvstr.
get<std::string*>())[
i]);
126 for (
auto i = 0u;
i < vsm.
size(); ++
i) {
129 for (
auto i = 0u;
i < vscc.
size(); ++
i) {
134 Variant vsma(std::move(vvstr));
136 for (
auto i = 0u;
i < vsma.
size(); ++
i) {
137 REQUIRE(vstrings[
i] == (vsma.
get<std::string*>())[
i]);
139 for (
auto i = 0u;
i < vscca.
size(); ++
i) {
140 REQUIRE(vstrings[
i] == (vscca.
get<std::string*>())[
i]);
143 float m[3][4] = {{0.5, 0.25, 0.125, 0.0625}, {1.0, 2.0, 3.0, 4.0}, {2.0, 0.5, 4.0, 0.25}};
147 for (
auto i = 0u;
i < 3; ++
i) {
148 for (
auto j = 0u;
j < 4; ++
j) {
149 REQUIRE(mmc(
i,
j) == mm(
i,
j));
157 for (
auto i = 0u;
i < 3; ++
i) {
158 for (
auto j = 0u;
j < 4; ++
j) {
159 REQUIRE(mmc2(
i,
j) == mm(
i,
j));
163 for (
auto i = 0u;
i < 3; ++
i) {
164 for (
auto j = 0u;
j < 4; ++
j) {
165 REQUIRE(mmc3(
i,
j) == mm(
i,
j));
168 std::stringstream ssm;
170 REQUIRE(ssm.str() ==
"{\"values\":[[0.5,0.25,0.125,0.0625],[1.0,2.0,3.0,4.0],[2.0,0.5,4.0,0.25]]}");
175 for (
auto i = 0u;
i < 3; ++
i) {
176 for (
auto j = 0u;
j < 4; ++
j) {
177 REQUIRE(laf.get(
i,
j) == lafc.get(
i,
j));
182 Variant vlafm(std::move(vlaf));
185 for (
auto i = 0U;
i < 3; ++
i) {
186 for (
auto j = 0U;
j < 4; ++
j) {
187 REQUIRE(lafc2.get(
i,
j) == mm(
i,
j));
191 for (
auto i = 0U;
i < 3; ++
i) {
192 for (
auto j = 0U;
j < 4; ++
j) {
193 REQUIRE(lafc3.get(
i,
j) == mm(
i,
j));
197 std::vector<Variant> collection;
198 collection.push_back(vlafc);
199 collection.push_back(vlafm);
200 collection.push_back(vlafa);
205 float m[3][4] = {{0.1, 0.2, 0.3, 0.4}, {0.5, 0.6, 0.7, 0.8}, {0.9, 1.0, 1.1, 1.2}};
207 for (
auto i = 0U;
i < 3; ++
i) {
208 for (
auto j = 0U;
j < 4; ++
j) {
209 REQUIRE(mm(
i,
j) ==
m[
i][
j]);
212 std::vector<float>
v = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2};
214 for (
auto i = 0U;
i < 3; ++
i) {
215 for (
auto j = 0U;
j < 4; ++
j) {
216 REQUIRE(mm(
i,
j) ==
v[
i * 4 +
j]);
219 for (
auto i = 0U;
i < 3; ++
i) {
220 auto const& vv = mm[
i];
221 for (
auto j = 0u;
j < 4; ++
j) {
222 REQUIRE(vv[
j] == mm(
i,
j));
225 std::vector<std::string> s = {
"one",
"two",
"three",
"four"};
227 for (
auto i = 0U;
i < 4; ++
i) {
228 REQUIRE(ms(
i, 0) == s[
i]);
234 std::vector<int>
v = {1, 2, 3, 4, 5, 6};
240 REQUIRE(copied.
rows == 2);
241 REQUIRE(copied.
cols == 3);
244 REQUIRE(copied(0, 0) == 1);
245 for (
auto i = 1U;
i < 6; ++
i) {
246 REQUIRE(copied(
i / 3,
i % 3) ==
v[
i]);
252 REQUIRE(copied.
rows == 2);
253 REQUIRE(copied.
cols == 3);
254 REQUIRE(copied(1, 2) == 6);
259 moved = std::move(copied);
261 REQUIRE(moved.
rows == 2);
262 REQUIRE(moved.
cols == 3);
263 REQUIRE(moved(1, 2) == 6);
264 REQUIRE(copied.
data ==
nullptr);
265 REQUIRE(copied.
rows == 0);
266 REQUIRE(copied.
cols == 0);
269 auto& selfMoved = moved;
270 moved = std::move(selfMoved);
272 REQUIRE(moved(1, 2) == 6);
276 REQUIRE(moved.
data ==
nullptr);
277 REQUIRE(moved.
rows == 0);
278 REQUIRE(moved.
cols == 0);
281 std::vector<std::string> s = {
"one",
"two"};
285 ms[0][0] =
"changed";
286 REQUIRE(msc(0, 0) ==
"one");
287 REQUIRE(msc(1, 0) ==
"two");
292 float m[2][3] = {{1, 2, 3}, {4, 5, 6}};
294 std::vector<std::string> vs{
"s1",
"s2",
"s3"};
295 auto checkLabeled = [&](
Variant const&
v) {
296 REQUIRE(
v.type() == VariantType::LabeledArrayFloat);
298 REQUIRE(la.rows() == 2);
299 REQUIRE(la.cols() == 3);
300 REQUIRE(la.get(
"r2",
"c3") == 6);
301 REQUIRE(la.getLabelsRows() == std::vector<std::string>{
"r1",
"r2"});
307 Variant moved(std::move(*copy));
316 vstr =
Variant(
"back to a string");
317 REQUIRE(vstr.
type() == VariantType::String);
318 REQUIRE(std::string(vstr.
get<
const char*>()) ==
"back to a string");
319 vstr = std::move(moved);
323 REQUIRE(vstr.
get<std::vector<std::string>>() == vs);
329 vl = std::move(self);
333 Variant vvsMoved(std::move(vvs));
334 REQUIRE(vvsMoved.
get<std::vector<std::string>>() == vs);
336 REQUIRE(vvs.
get<std::vector<std::string>>() == vs);
339 std::vector<Variant> collection;
340 for (
auto i = 0;
i < 20; ++
i) {
341 collection.emplace_back(laf);
343 collection.emplace_back(vs);
344 collection.emplace_back(
"a string");
346 std::vector<Variant> collectionCopy = collection;
348 for (
auto i = 0U;
i < collectionCopy.size();
i += 4) {
349 checkLabeled(collectionCopy[
i]);
351 REQUIRE(collectionCopy[
i + 2].get<std::vector<std::string>>() == vs);
352 REQUIRE(std::string(collectionCopy[
i + 3].get<const char*>()) ==
"a string");
356 Variant typed(VariantType::LabeledArrayFloat);
359 Variant typedString(VariantType::String);
360 Variant typedStringCopy(typedString);
361 REQUIRE(typedStringCopy.
get<
const char*>() ==
nullptr);
368 REQUIRE(vset.
type() == VariantType::ArrayString);
369 REQUIRE(vset.
get<std::vector<std::string>>() == vs);
374 float m[3][4] = {{0.1, 0.2, 0.3, 0.4}, {0.5, 0.6, 0.7, 0.8}, {0.9, 1.0, 1.1, 1.2}};
375 std::string mS[3][4] = {{
"a",
"b",
"c",
"d"}, {
"e",
"f",
"g",
"h"}, {
"i",
"l",
"m",
"n"}};
376 std::string xl[] = {
"c1",
"c2",
"c3",
"c4"};
377 std::string yl[] = {
"r1",
"r2",
"r3"};
380 for (
auto i = 0u;
i < 3; ++
i) {
381 for (
auto j = 0u;
j < 4; ++
j) {
382 REQUIRE(laf.get(yl[
i].c_str(), xl[
j].c_str()) == laf.get(
i,
j));
383 REQUIRE(laf.get(
i, xl[
j].c_str()) == laf.get(
i,
j));
384 REQUIRE(laf.get(yl[
i].c_str(),
j) == laf.get(
i,
j));
386 REQUIRE(las.get(yl[
i].c_str(), xl[
j].c_str()) == las.get(
i,
j));
387 REQUIRE(las.get(
i, xl[
j].c_str()) == las.get(
i,
j));
388 REQUIRE(las.get(yl[
i].c_str(),
j) == las.get(
i,
j));
395 std::vector<std::string> vstrings{
"0 1",
"0 2",
"0 3"};
399 auto fromTree = vectorFromBranch<std::string>(
tree);
402 REQUIRE(vvstr.
size() == vstrings.size());
403 REQUIRE(
v.size() == vstrings.size());
404 for (
auto i = 0U;
i < vvstr.
size(); ++
i) {
405 REQUIRE(vvstr.
get<std::string*>()[
i] ==
v.get<std::string*>()[
i]);
411 int iarr[] = {1, 2, 3, 4, 5};
413 std::stringstream os;
416 std::stringstream is;
418 auto v = VariantJSONHelpers::read<VariantType::ArrayInt>(is);
419 for (
auto i = 0u;
i < viarr.
size(); ++
i) {
420 REQUIRE(
v.get<
int*>()[
i] == viarr.
get<
int*>()[
i]);
424 float m[3][4] = {{0.1, 0.2, 0.3, 0.4}, {0.5, 0.6, 0.7, 0.8}, {0.9, 1.0, 1.1, 1.2}};
427 std::stringstream osm;
430 std::stringstream ism;
432 auto vm = VariantJSONHelpers::read<VariantType::Array2DFloat>(ism);
434 for (
auto i = 0u;
i < mm.rows; ++
i) {
435 for (
auto j = 0u;
j < mm.cols; ++
j) {
442 std::stringstream osl;
445 std::stringstream isl;
447 auto vlafc = VariantJSONHelpers::read<VariantType::LabeledArrayFloat>(isl);
455 std::string mS[3][4] = {{
"a",
"b",
"c",
"d"}, {
"e",
"f",
"g",
"h"}, {
"i",
"l",
"m",
"n"}};
458 std::stringstream ossl;
461 std::stringstream issl;
462 issl.str(ossl.str());
463 auto vmsa = VariantJSONHelpers::read<VariantType::LabeledArrayString>(issl);
471 std::vector<std::string> vstrings{
"myoption_one",
"myoption_two"};
473 REQUIRE(vvstr.
size() == 2);
474 std::stringstream osal;
477 std::stringstream isal;
478 isal.str(osal.str());
479 auto vvstra = VariantJSONHelpers::read<VariantType::ArrayString>(isal);
481 REQUIRE(vvstr.
size() == vvstra.size());
482 for (
auto i = 0U;
i < vstrings.size(); ++
i) {
483 REQUIRE(vstrings[
i] == vvstra.get<std::string*>()[
i]);
495 REQUIRE(error.what == std::string(
"Variant::get: Mismatch between types 4 0."));
500static_assert(!std::is_constructible_v<Variant, std::vector<int>>);
501static_assert(!std::is_constructible_v<Variant, std::vector<float>>);
502static_assert(!std::is_constructible_v<Variant, std::vector<double>>);
503static_assert(!std::is_constructible_v<Variant, std::vector<bool>>);
504static_assert(!std::is_constructible_v<Variant, std::vector<std::string>>);
506static_assert(std::is_assignable_v<Variant&, std::vector<int>>);
507static_assert(std::is_assignable_v<Variant&, std::vector<std::string>>);
508static_assert(std::is_constructible_v<Variant, std::vector<int>&>);
509static_assert(std::is_constructible_v<Variant, std::vector<int>
const&>);
510static_assert(std::is_constructible_v<Variant, std::vector<std::string>
const&>);
511static_assert(std::is_assignable_v<Variant&, std::vector<int>
const&>);
518 auto check = [&](
Variant const&
v, std::vector<T>
const& from) {
519 REQUIRE(
v.type() ==
type);
520 REQUIRE(
v.size() == from.size());
521 auto const* stored =
v.get<
T*>();
522 REQUIRE(stored != from.data());
523 for (
auto i = 0U;
i < from.size(); ++
i) {
524 REQUIRE(stored[
i] == from[
i]);
527 std::vector<T>
const constSource =
source;
528 Variant fromConst(constSource);
529 check(fromConst, constSource);
534 auto const original =
source;
537 check(fromMutable, original);
543 checkVectorCopy<int>(VariantType::ArrayInt, {1, 2, 3, 4});
544 checkVectorCopy<float>(VariantType::ArrayFloat, {0.5f, 1.5f, 2.5f});
545 checkVectorCopy<double>(VariantType::ArrayDouble, {1e-3, 1e3});
546 checkVectorCopy<std::string>(VariantType::ArrayString, {
"a",
"bb",
"ccc"});
549 std::vector<int> vi{7, 8, 9};
552 REQUIRE(
v.type() == VariantType::ArrayInt);
553 REQUIRE(
v.size() == 3);
554 REQUIRE(
v.get<
int*>() != vi.data());
555 REQUIRE(
v.get<
int*>()[2] == 9);
557 auto makeStrings = []() {
return std::vector<std::string>{
"x",
"y"}; };
559 REQUIRE(
v.type() == VariantType::ArrayString);
560 REQUIRE(
v.size() == 2);
561 REQUIRE(
v.get<std::string*>()[1] ==
"y");
562 v = std::vector<double>{1.5, 2.5, 3.5};
563 REQUIRE(
v.type() == VariantType::ArrayDouble);
564 REQUIRE(
v.size() == 3);
565 REQUIRE(
v.get<
double*>()[2] == 3.5);
568 std::vector<double>
const vd{0.25, 0.5, 0.75};
570 std::stringstream is(vdv.
asString());
571 auto read = VariantJSONHelpers::read<VariantType::ArrayDouble>(is);
572 REQUIRE(read.size() == vd.size());
573 for (
auto i = 0U;
i < vd.size(); ++
i) {
574 REQUIRE(read.get<
double*>()[
i] == vd[
i]);
580template <VariantType V,
typename T>
584 auto str =
v.asString();
585 REQUIRE(
str.find(
"undefined") == std::string::npos);
586 std::stringstream is(
str);
587 auto vr = VariantJSONHelpers::read<V>(is);
588 REQUIRE(vr.type() == V);
589 auto lar = vr.template get<LabeledArray<T>>();
590 REQUIRE(lar.rows() == la.
rows());
591 REQUIRE(lar.cols() == la.
cols());
594 for (
auto i = 0U;
i < la.
rows(); ++
i) {
595 for (
auto j = 0U;
j < la.
cols(); ++
j) {
596 REQUIRE(lar.get(
i,
j) == la.
get(
i,
j));
604 int mi[2][2] = {{1, 2}, {3, 4}};
605 float mf[2][3] = {{0.5, 0.25, 0.125}, {1, 2, 3}};
606 double md[1][2] = {{1e-3, 1e10}};
607 std::string ms[2][1] = {{
"a"}, {
"b c"}};
609 checkLabeledArrayStringRoundTrip<VariantType::LabeledArrayInt>(
LabeledArray<int>{&mi[0][0], 2, 2, {
"r1",
"r2"}, {
"c1",
"c2"}});
610 checkLabeledArrayStringRoundTrip<VariantType::LabeledArrayFloat>(
LabeledArray<float>{&mf[0][0], 2, 3, {
"r1",
"r2"}, {
"c1",
"c2",
"c3"}});
611 checkLabeledArrayStringRoundTrip<VariantType::LabeledArrayDouble>(
LabeledArray<double>{&md[0][0], 1, 2, {
"r1"}, {
"c1",
"c2"}});
612 checkLabeledArrayStringRoundTrip<VariantType::LabeledArrayString>(
LabeledArray<std::string>{&ms[0][0], 2, 1, {
"r1",
"r2"}, {
"c1"}});
614 checkLabeledArrayStringRoundTrip<VariantType::LabeledArrayInt>(
LabeledArray<int>{&mi[0][0], 2, 2});
616 checkLabeledArrayStringRoundTrip<VariantType::LabeledArrayFloat>(
LabeledArray<float>{&mf[0][0], 2, 3, {
"row \"one\"",
"row\\two"}, {
"c 1",
"c,2",
"c:3"}});
619 REQUIRE(
v.asString() == R
"({"labels_rows":["r1","r2"],"labels_cols":["c1","c2"],"values":[[1,2],[3,4]]})");
T get(uint32_t y, uint32_t x) const
Variant for configuration parameter storage. Owns stored data.
std::string asString() const
GLsizei const GLchar *const * strings
GLboolean GLboolean GLboolean b
GLsizei GLsizei GLchar * source
GLint GLint GLsizei GLint GLenum GLenum type
GLboolean GLboolean GLboolean GLboolean a
Defining ITS Vertex explicitly as messageable.
void check(const std::vector< std::string > &arguments, const std::vector< ConfigParamSpec > &workflowOptions, const std::vector< DeviceSpec > &deviceSpecs, CheckMatrix &matrix)
TEST_CASE("test_prepareArguments")
RuntimeError & error_from_ref(RuntimeErrorRef)
boost::property_tree::ptree vectorToBranch(T const *values, size_t size)
auto getLabelsRows() const
auto getLabelsCols() const
static void write(std::ostream &o, Variant const &v)
bool unknown_type(RuntimeErrorRef const &ref)
std::unique_ptr< TTree > tree((TTree *) flIn.Get(std::string(o2::base::NameConf::CTFTREENAME).c_str()))
std::vector< ReadoutWindowData > rows