-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathtestMemoryResources.cxx
More file actions
143 lines (123 loc) · 4.77 KB
/
testMemoryResources.cxx
File metadata and controls
143 lines (123 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
#define BOOST_TEST_MODULE Test MemoryResources
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "MemoryResources/MemoryResources.h"
#include <fairmq/TransportFactory.h>
#include <fairmq/Tools.h>
#include <fairmq/ProgOptions.h>
#include <vector>
#include <cstring>
namespace o2::pmr
{
struct testData {
int i{1};
static int nconstructions;
testData()
{
++nconstructions;
}
testData(const testData& in) : i{in.i}
{
++nconstructions;
}
testData(const testData&& in) : i{in.i}
{
++nconstructions;
}
testData(int in) : i{in}
{
++nconstructions;
}
};
int testData::nconstructions = 0;
BOOST_AUTO_TEST_CASE(transportallocatormap_test)
{
size_t session{(size_t)getpid() * 1000};
fair::mq::ProgOptions config;
config.SetProperty<std::string>("session", std::to_string(session));
auto factoryZMQ = fair::mq::TransportFactory::CreateTransportFactory("zeromq");
auto factorySHM = fair::mq::TransportFactory::CreateTransportFactory("shmem", "transportallocatormap_test", &config);
auto allocZMQ = getTransportAllocator(factoryZMQ.get());
auto allocSHM = getTransportAllocator(factorySHM.get());
BOOST_CHECK(allocZMQ != nullptr && allocSHM != allocZMQ);
auto _tmp = getTransportAllocator(factoryZMQ.get());
BOOST_CHECK(_tmp == allocZMQ);
}
using namespace fair::mq::pmr;
BOOST_AUTO_TEST_CASE(allocator_test)
{
size_t session{(size_t)getpid() * 1000 + 1};
fair::mq::ProgOptions config;
config.SetProperty<std::string>("session", std::to_string(session));
auto factoryZMQ = fair::mq::TransportFactory::CreateTransportFactory("zeromq");
auto factorySHM = fair::mq::TransportFactory::CreateTransportFactory("shmem", "allocator_test", &config);
auto allocZMQ = getTransportAllocator(factoryZMQ.get());
auto allocSHM = getTransportAllocator(factorySHM.get());
testData::nconstructions = 0;
{
std::vector<testData, polymorphic_allocator<testData>> v(polymorphic_allocator<testData>{allocZMQ});
v.reserve(3);
BOOST_CHECK(v.capacity() == 3);
BOOST_CHECK(allocZMQ->getNumberOfMessages() == 1);
v.emplace_back(1);
v.emplace_back(2);
v.emplace_back(3);
BOOST_CHECK((std::byte*)&(*v.end()) - (std::byte*)&(*v.begin()) == 3 * sizeof(testData));
BOOST_CHECK(testData::nconstructions == 3);
}
testData::nconstructions = 0;
BOOST_CHECK(allocZMQ->getNumberOfMessages() == 0);
}
BOOST_AUTO_TEST_CASE(getMessage_test)
{
size_t session{(size_t)getpid() * 1000 + 2};
fair::mq::ProgOptions config;
config.SetProperty<std::string>("session", std::to_string(session));
auto factoryZMQ = fair::mq::TransportFactory::CreateTransportFactory("zeromq");
auto factorySHM = fair::mq::TransportFactory::CreateTransportFactory("shmem", "getMessage_test", &config);
auto allocZMQ = getTransportAllocator(factoryZMQ.get());
auto allocSHM = getTransportAllocator(factorySHM.get());
testData::nconstructions = 0;
fair::mq::MessagePtr message{nullptr};
int* messageArray{nullptr};
// test message creation on the same channel it was allocated with
{
std::vector<testData, polymorphic_allocator<testData>> v(polymorphic_allocator<testData>{allocZMQ});
v.emplace_back(1);
v.emplace_back(2);
v.emplace_back(3);
void* vectorBeginPtr = &v[0];
message = o2::pmr::getMessage(std::move(v));
BOOST_CHECK(message != nullptr);
BOOST_CHECK(message->GetData() == vectorBeginPtr);
}
BOOST_CHECK(message->GetSize() == 3 * sizeof(testData));
messageArray = static_cast<int*>(message->GetData());
BOOST_CHECK(messageArray[0] == 1 && messageArray[1] == 2 && messageArray[2] == 3);
// test message creation on a different channel than it was allocated with
{
std::vector<testData, polymorphic_allocator<testData>> v(polymorphic_allocator<testData>{allocZMQ});
v.emplace_back(4);
v.emplace_back(5);
v.emplace_back(6);
void* vectorBeginPtr = &v[0];
message = o2::pmr::getMessage(std::move(v), allocSHM);
BOOST_CHECK(message != nullptr);
BOOST_CHECK(message->GetData() != vectorBeginPtr);
}
BOOST_CHECK(message->GetSize() == 3 * sizeof(testData));
messageArray = static_cast<int*>(message->GetData());
BOOST_CHECK(messageArray[0] == 4 && messageArray[1] == 5 && messageArray[2] == 6);
}
}; // namespace o2::pmr