Project
Loading...
Searching...
No Matches
test_DataRefUtils.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 <TObject.h>
13#include <TObjString.h>
14#include <TObjArray.h>
15#include <TMessage.h>
18#include <fairmq/TransportFactory.h>
19#include <catch_amalgamated.hpp>
20
21#include <memory>
22
23using namespace o2::framework;
24
25TEST_CASE("PureRootTest")
26{
27 TBufferFile buffer(TBuffer::kWrite);
28 TObjString s("test");
29 buffer.WriteObject(&s);
30
31 TBufferFile buffer2(TBuffer::kRead, buffer.BufferSize(), buffer.Buffer(), false);
32 buffer2.SetReadMode();
33 buffer2.InitMap();
34 TClass* storedClass = buffer2.ReadClass();
35 // ReadClass advances the buffer, so we need to reset it.
36 buffer2.SetBufferOffset(0);
37 buffer2.ResetMap();
38 REQUIRE(storedClass != nullptr);
39 auto* outS = (TObjString*)buffer2.ReadObjectAny(storedClass);
40 REQUIRE(outS != nullptr);
41 REQUIRE(outS->GetString() == "test");
42}
43
44// Simple test to do root deserialization.
45TEST_CASE("TestRootSerialization")
46{
48 auto transport = fair::mq::TransportFactory::CreateTransportFactory("zeromq");
49 auto msg = transport->CreateMessage(4096);
51 auto sOrig = std::make_unique<TObjString>("test");
52 tm << sOrig.get();
55 ref.payload = (char*)msg->GetData();
56 dh.payloadSize = (size_t)msg->GetSize();
57 ref.header = reinterpret_cast<char const*>(&dh);
58
59 // Check by using the same type
60 auto s = DataRefUtils::as<TObjString>(ref);
61 REQUIRE(s.get() != nullptr);
62 REQUIRE(std::string(s->GetString().Data()) == "test");
63 REQUIRE(std::string(s->GetName()) == "test");
64
65 // Check by using the base type.
66 auto o = DataRefUtils::as<TObject>(ref);
67 REQUIRE(o.get() != nullptr);
68 REQUIRE(std::string(o->GetName()) == "test");
69}
70
71// Simple test for ROOT container deserialization.
72TEST_CASE("TestRootContainerSerialization")
73{
75 TMessage* tm = new TMessage(kMESS_OBJECT);
76 TObjArray container;
77 // the original container is explicitly non-owning, its owning
78 // flag is preserved during serialization
79 container.SetOwner(false);
80 auto object = std::make_unique<TObjString>("test");
81 container.Add(object.get());
82 tm->WriteObject(&container);
85 ref.payload = tm->Buffer();
86 dh.payloadSize = tm->BufferSize();
87 ref.header = reinterpret_cast<char const*>(&dh);
88
89 auto s = DataRefUtils::as<TObjArray>(ref);
90 REQUIRE(s.get() != nullptr);
91 REQUIRE(s->GetEntries() == 1);
92 REQUIRE(std::string(s->At(0)->GetName()) == "test");
93 // the extracted object must be owning to avoid memory leaks
94 // the get method takes care of this
95 REQUIRE(s->IsOwner());
96}
bool o
GLuint buffer
Definition glcorearb.h:655
GLint ref
Definition glcorearb.h:291
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
constexpr o2::header::SerializationMethod gSerializationMethodROOT
Definition DataHeader.h:328
the main header struct
Definition DataHeader.h:618
SerializationMethod payloadSerializationMethod
Definition DataHeader.h:651
PayloadSizeType payloadSize
Definition DataHeader.h:666
TEST_CASE("PureRootTest")
uint64_t const void const *restrict const msg
Definition x9.h:153