Project
Loading...
Searching...
No Matches
testRootSerializableKeyValueStore.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 RootSerKeyValueStore
13#define BOOST_TEST_MAIN
14#define BOOST_TEST_DYN_LINK
15#include <boost/test/unit_test.hpp>
17#include <TMemFile.h>
18#include <TH1F.h>
19#include <vector>
20#include <iostream>
21#include <TClass.h>
22
23using namespace o2;
24using namespace o2::utils;
25
26BOOST_AUTO_TEST_CASE(write_read_test)
27{
29
30 // put POD some stuff
31 double x = 1.1;
32 s.put("x", x);
33 s.put("i", 110);
34
35 // put some complex classes (need dictionary)
36 std::string str = "hello";
37 s.put("str", str);
38
39 // this should fail compiling:
40 // const char* text = "foo";
41 // s.put("cstr", text);
42
43 TH1F h1("th1name", "th1name", 100, 0, 99);
44 h1.FillRandom("gaus", 10000);
45 s.put("h1", h1);
46
47 // check basic assumption that typeinfo name is unique for basic types
48 BOOST_CHECK(strcmp(std::type_index(typeid(double*)).name(), "Pd") == 0);
49 BOOST_CHECK(strcmp(std::type_index(typeid(int)).name(), "i") == 0);
50 BOOST_CHECK(strcmp(std::type_index(typeid(unsigned int)).name(), "j") == 0);
51 BOOST_CHECK(strcmp(std::type_index(typeid(char)).name(), "c") == 0);
52 BOOST_CHECK(strcmp(std::type_index(typeid(char*)).name(), "Pc") == 0);
53 BOOST_CHECK(strcmp(std::type_index(typeid(unsigned char)).name(), "h") == 0);
54
55 // check assumption that for more complicated types the TClass name is unique
56 // (the std::type_index is not standardized)
57 BOOST_CHECK(strcmp(TClass::GetClass(typeid(std::vector<double>))->GetName(), "vector<double>") == 0);
58
59 // retrieve
60 BOOST_CHECK(s.get<std::string>("str")->compare(str) == 0);
61 BOOST_CHECK(*(s.get<double>("x")) == x);
62 BOOST_CHECK(s.has("x"));
63 BOOST_CHECK(!s.has("x_does_not_exist"));
64
65 // retrieve with state/error information
67 ErrorState state;
68 {
69 auto r1 = s.get<std::string>("str", state);
70 BOOST_CHECK(state == ErrorState::kOK);
71 auto returnedstring = s.getRef<std::string>("str", state);
72 BOOST_CHECK(state == ErrorState::kOK);
73 BOOST_CHECK(returnedstring.compare(str) == 0);
74
75 auto r2 = s.get<int>("str", state);
76 BOOST_CHECK(r2 == nullptr);
77 BOOST_CHECK(state == ErrorState::kWRONGTYPE);
78 auto r3 = s.get<int>("str2", state);
79 BOOST_CHECK(state == ErrorState::kNOSUCHKEY);
80 BOOST_CHECK(r3 == nullptr);
81
82 auto r4 = s.get<TH1F>("non-existend-histogram", state);
83 BOOST_CHECK(state == ErrorState::kNOSUCHKEY);
84 }
85
86 // put something twice
87 s.put<int>("twice", 10);
88 s.put<int>("twice", 7);
89 BOOST_CHECK(*(s.get<int>("twice")) == 7);
90
91 std::cerr << "TESTING FILE IO\n";
92 TMemFile f("tmp.root", "RECREATE");
93 f.WriteObject(&s, "map");
94
96 f.GetObject("map", s2);
97 BOOST_CHECK(s2 != nullptr);
98 if (s2) {
99 auto t1 = s2->get<double>("x");
100 BOOST_CHECK(t1 != nullptr);
101 BOOST_CHECK(t1 && *(t1) == x);
102
103 auto i1 = s2->get<int>("i");
104 BOOST_CHECK(i1 != nullptr);
105 BOOST_CHECK(i1 && *(i1) == 110);
106
107 auto t2 = s2->get<std::string>("str");
108 BOOST_CHECK(t2 && t2->compare(str) == 0);
109
110 auto t3 = s2->get<std::string>("str");
111 BOOST_CHECK(t3 && t3->compare(str) == 0);
112
113 auto histo = s2->get<TH1F>("h1");
114 BOOST_CHECK(histo);
115
116 s2->print();
117 }
118 f.Close();
119}
benchmark::State & state
const T * get(std::string const &key) const
returns object pointer for this key or nullptr if error or does not exist.
void print(bool includetypeinfo=false) const
print list of keys, values (and optionally type information)
GLint GLenum GLint x
Definition glcorearb.h:403
GLuint const GLchar * name
Definition glcorearb.h:781
GLdouble f
Definition glcorearb.h:310
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
Definition glcorearb.h:5034
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
BOOST_AUTO_TEST_CASE(write_read_test)
BOOST_CHECK(tree)
const std::string str