forked from max0x7ba/atomic_queue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cc
More file actions
268 lines (209 loc) · 8.42 KB
/
tests.cc
File metadata and controls
268 lines (209 loc) · 8.42 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
// Copyright (c) 2019 Maxim Egorushkin. MIT License. See the full licence in file LICENSE.
#define BOOST_TEST_MODULE atomic_queue
#include <boost/test/unit_test.hpp>
#include "atomic_queue/atomic_queue.h"
#include "atomic_queue/barrier.h"
#include <cstdint>
#include <thread>
#include <string>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
using namespace ::atomic_queue;
namespace {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check that all push'es are ever pop'ed once with multiple producer and multiple consumers.
template<class Queue>
void stress() {
constexpr int PRODUCERS = 3;
constexpr int CONSUMERS = 3;
constexpr unsigned N = 1000000;
using T = typename Queue::value_type;
constexpr T STOP = -1;
Queue q;
Barrier barrier;
std::thread producers[PRODUCERS];
for(unsigned i = 0; i < PRODUCERS; ++i)
producers[i] = std::thread([&q, &barrier, N=N]() {
barrier.wait();
for(T n = N; n; --n)
q.push(n);
});
uint64_t results[CONSUMERS];
std::thread consumers[CONSUMERS];
for(unsigned i = 0; i < CONSUMERS; ++i)
consumers[i] = std::thread([&q, &barrier, &r = results[i], STOP = STOP]() {
barrier.wait();
uint64_t result = 0;
for(T n; (n = q.pop()) != STOP;)
result += n;
r = result;
});
barrier.release(PRODUCERS + CONSUMERS);
for(auto& t : producers)
t.join();
for(int i = CONSUMERS; i--;)
q.push(STOP);
for(auto& t : consumers)
t.join();
constexpr uint64_t expected_result = (N + 1) / 2. * N * PRODUCERS;
uint64_t result = 0;
for(auto& r : results) {
BOOST_WARN_GT(r, (expected_result / CONSUMERS / 10)); // Make sure a consumer didn't starve. False positives are possible here.
result += r;
}
int64_t result_diff = result - expected_result;
BOOST_CHECK_EQUAL(result_diff, 0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Q>
void test_unique_ptr_int(Q& q) {
BOOST_CHECK(q.was_empty());
BOOST_CHECK_EQUAL(q.was_size(), 0u);
std::unique_ptr<int> p{new int{1}};
BOOST_REQUIRE(q.try_push(std::move(p)));
BOOST_CHECK(!p);
BOOST_CHECK(!q.was_empty());
BOOST_CHECK_EQUAL(q.was_size(), 1u);
p.reset(new int{2});
q.push(std::move(p));
BOOST_REQUIRE(!p);
BOOST_CHECK(!q.was_empty());
BOOST_CHECK_EQUAL(q.was_size(), 2u);
BOOST_REQUIRE(q.try_pop(p));
BOOST_REQUIRE(p.get());
BOOST_CHECK_EQUAL(*p, 1);
BOOST_CHECK(!q.was_empty());
BOOST_CHECK_EQUAL(q.was_size(), 1u);
p = q.pop();
BOOST_REQUIRE(p.get());
BOOST_CHECK_EQUAL(*p, 2);
BOOST_CHECK(q.was_empty());
BOOST_CHECK_EQUAL(q.was_size(), 0u);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class T, class State>
struct test_stateful_allocator : std::allocator<T> {
State state;
test_stateful_allocator() = delete;
// disambiguate constructor with std::nullptr_t
// std::in_place available since C++17
test_stateful_allocator(std::nullptr_t, const State& s) noexcept
: state(s) {}
test_stateful_allocator(const test_stateful_allocator& other) noexcept
: std::allocator<T>(other), state(other.state) {}
template<class U>
test_stateful_allocator(const test_stateful_allocator<U, State>& other) noexcept
: state(other.state) {}
test_stateful_allocator& operator=(const test_stateful_allocator& other) noexcept {
state = other.state;
return *this;
}
~test_stateful_allocator() noexcept = default;
template<class U>
struct rebind {
using other = test_stateful_allocator<U, State>;
};
};
// Required by boost-test
template<class T, class State>
std::ostream& operator<<(std::ostream& os, const test_stateful_allocator<T, State>& allocator) {
return os << allocator.state;
}
template<class T1, class T2, class State>
bool operator==(const test_stateful_allocator<T1, State>& lhs, const test_stateful_allocator<T2, State>& rhs) {
return lhs.state == rhs.state;
}
template<class T1, class T2, class State>
bool operator!=(const test_stateful_allocator<T1, State>& lhs, const test_stateful_allocator<T2, State>& rhs) {
return !(lhs.state == rhs.state);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} // namespace
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
constexpr unsigned CAPACITY = 1024;
BOOST_AUTO_TEST_CASE(stress_AtomicQueue) {
stress<RetryDecorator<AtomicQueue<unsigned, CAPACITY>>>();
}
BOOST_AUTO_TEST_CASE(stress_BlockingAtomicQueue) {
stress<AtomicQueue<unsigned, CAPACITY>>();
}
BOOST_AUTO_TEST_CASE(stress_AtomicQueue2) {
stress<RetryDecorator<AtomicQueue2<unsigned, CAPACITY>>>();
}
BOOST_AUTO_TEST_CASE(stress_BlockingAtomicQueue2) {
stress<AtomicQueue2<unsigned, CAPACITY>>();
}
BOOST_AUTO_TEST_CASE(move_only_2) {
AtomicQueue2<std::unique_ptr<int>, 2> q;
test_unique_ptr_int(q);
}
BOOST_AUTO_TEST_CASE(move_only_b2) {
AtomicQueueB2<std::unique_ptr<int>> q(2);
test_unique_ptr_int(q);
}
BOOST_AUTO_TEST_CASE(allocator_constructor_only_b) {
using allocator_type = test_stateful_allocator<int, std::string>;
const auto allocator = allocator_type(nullptr, "Capybara");
AtomicQueueB<int, allocator_type> q(2, allocator);
BOOST_CHECK_EQUAL(q.get_allocator(), allocator);
auto q2 = std::move(q);
BOOST_CHECK_EQUAL(q2.get_allocator(), allocator);
}
BOOST_AUTO_TEST_CASE(allocator_constructor_only_b2) {
using allocator_type = test_stateful_allocator<std::unique_ptr<int>, std::string>;
const auto allocator = allocator_type(nullptr, "Fox");
AtomicQueueB2<std::unique_ptr<int>, allocator_type> q(2, allocator);
BOOST_CHECK_EQUAL(q.get_allocator(), allocator);
auto q2 = std::move(q);
BOOST_CHECK_EQUAL(q2.get_allocator(), allocator);
}
BOOST_AUTO_TEST_CASE(move_constructor_assignment) {
AtomicQueueB2<std::unique_ptr<int>> q(2);
auto q2 = std::move(q);
q = std::move(q2);
AtomicQueueB<int> p(2);
auto p2 = std::move(p);
p = std::move(p2);
}
BOOST_AUTO_TEST_CASE(try_push) {
using Queue = atomic_queue::AtomicQueueB2<
/* T = */ float,
/* A = */ std::allocator<float>,
/* MAXIMIZE_THROUGHPUT */ true,
/* TOTAL_ORDER = */ true,
/* SPSC = */ true
>;
constexpr unsigned CAPACITY = CACHE_LINE_SIZE * CACHE_LINE_SIZE;
Queue q(CAPACITY);
BOOST_CHECK_EQUAL(q.capacity(), CAPACITY);
BOOST_CHECK(q.was_empty());
BOOST_CHECK_EQUAL(q.was_size(), 0u);
for(unsigned i = 1; i <= CAPACITY; ++i)
BOOST_CHECK(q.try_push(i));
BOOST_CHECK(!q.was_empty());
BOOST_CHECK_EQUAL(q.was_size(), CAPACITY);
for(unsigned i = 1; i <= CAPACITY; ++i)
BOOST_CHECK(!q.try_push(i));
BOOST_CHECK(!q.was_empty());
BOOST_CHECK_EQUAL(q.was_size(), CAPACITY);
}
BOOST_AUTO_TEST_CASE(size) {
atomic_queue::RetryDecorator<atomic_queue::AtomicQueueB2<float>> q(10);
BOOST_CHECK_EQUAL(q.capacity(), CACHE_LINE_SIZE * CACHE_LINE_SIZE);
BOOST_CHECK(q.was_empty());
BOOST_CHECK(!q.was_full());
}
BOOST_AUTO_TEST_CASE(power_of_2) {
using atomic_queue::details::round_up_to_power_of_2;
static_assert(round_up_to_power_of_2(0u) == 0u, "");
static_assert(round_up_to_power_of_2(1u) == 1u, "");
static_assert(round_up_to_power_of_2(2u) == 2u, "");
static_assert(round_up_to_power_of_2(3u) == 4u, "");
static_assert(round_up_to_power_of_2(127u) == 128u, "");
static_assert(round_up_to_power_of_2(128u) == 128u, "");
static_assert(round_up_to_power_of_2(129u) == 256u, "");
static_assert(round_up_to_power_of_2(0x40000000u - 1) == 0x40000000u, "");
static_assert(round_up_to_power_of_2(0x40000000u ) == 0x40000000u, "");
static_assert(round_up_to_power_of_2(0x40000000u + 1) == 0x80000000u, "");
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////