ChimeraTK-cppext 01.05.02
Loading...
Searching...
No Matches
testSharing.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#define BOOST_TEST_MODULE test_future_queue
4#include <boost/test/included/unit_test.hpp>
5using namespace boost::unit_test_framework;
6
7#include "future_queue.hpp"
9
10#include <thread>
11
12BOOST_AUTO_TEST_SUITE(testSharing)
13
14// test with a custom data type which is not known to the queue
15// we intentionally do not use a convenient standard-like interface to avoid
16// accidental usage of common operators etc.
17struct MovableDataType {
18 constexpr static int undef = -987654321;
20 explicit MovableDataType(int value) : _value(value) {}
21 MovableDataType(MovableDataType&& other) : _value(other._value) { other._value = undef; }
23 _value = other._value;
24 other._value = undef;
25 return *this;
26 }
27 int value() { return _value; }
28
29 private:
30 int _value{undef};
31};
32constexpr int MovableDataType::undef;
33
34/*********************************************************************************************************************/
35
36BOOST_AUTO_TEST_CASE(testCopyConstruct) {
38 BOOST_CHECK_EQUAL(queue1.size(), 10);
39 queue1.push(MovableDataType(42));
40
42 BOOST_CHECK_EQUAL(queue2.size(), 10);
43 BOOST_CHECK(queue1 == queue2);
44 BOOST_CHECK(!(queue1 != queue2));
45 queue2.push(MovableDataType(43));
46 queue1.push(MovableDataType(44));
47 queue2.push(MovableDataType(45));
48 queue2.push(MovableDataType(46));
49
51 queue2.pop(x);
52 BOOST_CHECK_EQUAL(x.value(), 42);
53 queue2.pop(x);
54 BOOST_CHECK_EQUAL(x.value(), 43);
55 queue2.pop(x);
56 BOOST_CHECK_EQUAL(x.value(), 44);
57 queue1.pop(x);
58 BOOST_CHECK_EQUAL(x.value(), 45);
59 queue1.pop(x);
60 BOOST_CHECK_EQUAL(x.value(), 46);
61
62 {
64 queue3.push(MovableDataType(12345));
65 queue1 = queue3;
66 }
67}
68
69/*********************************************************************************************************************/
70
72 std::vector<cppext::future_queue<MovableDataType>> vectorOfQueues;
73
75 vectorOfQueues.push_back(queue1);
77 vectorOfQueues.emplace_back(queue2);
78 BOOST_CHECK(queue1 != queue2);
79 BOOST_CHECK(!(queue1 == queue2));
80
81 BOOST_CHECK_EQUAL(queue1.size(), 10);
82 BOOST_CHECK_EQUAL(vectorOfQueues[0].size(), 10);
83 BOOST_CHECK_EQUAL(queue2.size(), 8);
84 BOOST_CHECK_EQUAL(vectorOfQueues[1].size(), 8);
85
86 queue1.push(MovableDataType(33));
88 vectorOfQueues[0].pop(x);
89 BOOST_CHECK_EQUAL(x.value(), 33);
90
91 vectorOfQueues[1].push(MovableDataType(44));
92 queue2.pop(x);
93 BOOST_CHECK_EQUAL(x.value(), 44);
94}
95
96/*********************************************************************************************************************/
97
98BOOST_AUTO_TEST_SUITE_END()
MovableDataType(MovableDataType &&other)
MovableDataType & operator=(MovableDataType &&other)
static constexpr int undef
Definition testEmpty.cc:18
MovableDataType(int value)
BOOST_AUTO_TEST_CASE(testCopyConstruct)