forked from max0x7ba/atomic_queue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.cc
141 lines (105 loc) · 3.95 KB
/
tests.cc
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
/* -*- 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_DYN_LINK
#define BOOST_TEST_MODULE atomic_queue
#include <boost/test/unit_test.hpp>
#include "atomic_queue.h"
#include "atomic_queue_mutex.h"
#include "barrier.h"
#include "moodycamel.h"
#include <cstdint>
#include <thread>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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;
Queue q;
Barrier barrier;
std::thread producers[PRODUCERS];
for(unsigned i = 0; i < PRODUCERS; ++i)
producers[i] = std::thread([&q, &barrier]() {
barrier.wait();
for(unsigned 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]]() {
barrier.wait();
uint64_t result = 0;
for(;;) {
unsigned n = q.pop();
result += n;
if(n == 1)
break;
}
r = result;
});
barrier.release(PRODUCERS + CONSUMERS);
for(auto& t : producers)
t.join();
for(auto& t : consumers)
t.join();
constexpr uint64_t expected_result = (N + 1) / 2. * N;
uint64_t result = 0;
for(auto& r : results) {
BOOST_CHECK_GT(r, expected_result / (CONSUMERS + 1)); // Make sure a consumer didn't starve. False positives are possible here.
result += r;
}
int64_t result_diff = result / CONSUMERS - expected_result;
BOOST_CHECK_EQUAL(result_diff, 0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
template<class Q>
void test_unique_ptr_int(Q& q) {
std::unique_ptr<int> p{new int{1}};
BOOST_REQUIRE(q.try_push(move(p)));
BOOST_CHECK(!p);
p.reset(new int{2});
q.push(move(p));
BOOST_REQUIRE(!p);
BOOST_REQUIRE(q.try_pop(p));
BOOST_REQUIRE(p.get());
BOOST_CHECK_EQUAL(*p, 1);
p = q.pop();
BOOST_REQUIRE(p.get());
BOOST_CHECK_EQUAL(*p, 2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
} // 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(stress_pthread_spinlock) {
stress<RetryDecorator<AtomicQueueSpinlock<unsigned, CAPACITY>>>();
}
BOOST_AUTO_TEST_CASE(stress_MoodyCamelQueue) {
stress<MoodyCamelQueue<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);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////