-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringTests.cpp
260 lines (206 loc) · 7.2 KB
/
stringTests.cpp
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
#include <string>
#include <set>
#include <thread>
#include <vector>
#include <functional>
#include <iostream>
#include <gtest/gtest.h>
#include "ConcurrentQueueBase/ConcurrentQueueBase.hpp"
#include "ConcurrentQueueSimple/ConcurrentQueueSimple.hpp"
#include "ConcurrentQueueExtended/ConcurrentQueueExtended.hpp"
template <typename T>
class ConcurrentQueueTest: public ::testing::Test { };
TYPED_TEST_CASE_P(ConcurrentQueueTest);
namespace{
const std::string kSampleText{"Loremipsumdolorsitamet,consecteturadipiscingelit.Utfelisante,portainullamcorperid,vehiculavelpurus.Prointristiqueconsecteturarcuutvenenatis.Etiammetusdiam,dictumsedmetuseget,consequatfinibuslorem.Inhachabitasseplateadictumst.Crasacmauriseuarcususcipiteuismod.Nullacursusliberonondapibuspellentesque.Suspendissepotenti.Aliquamvestibulumluctusdolor.Vestibulumnunclectus,rutrumutipsumac,tristiqueauctorneque.Maurisquisbibendumeros.Aliquameratvolutpat.Donecaliquamnisietligulahendrerit,idaccumsannuncfermentum.Suspendissepotenti.Proinaccumsannibhutnuncluctuseuismod.Utsitamettincidunttellus,sedfermentumurna."};
const std::string kPill{""};
size_t kSamplesNum{0};
const size_t kProducersNum{
std::max(
std::thread::hardware_concurrency() / 2,
static_cast<unsigned int>(1)
)
};
bool check(const std::string &s){
std::stringstream ss(s);
std::set<size_t> pieces{};
std::string current{};
while(ss >> current){
if(current != "Sample")
return false;
size_t currentPiece;
if(!(ss >> currentPiece))
return false;
if(pieces.find(currentPiece) != pieces.end())
return false;
pieces.insert(currentPiece);
if(!(ss >> current))
return false;
if(current != kSampleText)
return false;
}
return pieces.size() == kSamplesNum;
}
std::string generate(size_t pieceNum){
std::stringstream ss;
ss << "Sample " << pieceNum << " " << kSampleText << " ";
return ss.str();
}
void produce(size_t remainder, ConcurrentQueueBase<std::string> &target){
for(size_t i = remainder; i < kSamplesNum; i += kProducersNum)
target.push(generate(i));
}
void consume(ConcurrentQueueBase<std::string> &target){
std::string first{""}, second{""}, third{""};
std::unique_lock<std::recursive_mutex> lck(target.getPopMutex());
lck.unlock();
while(true){
lck.lock();
if(!target.tryPop(&first)){
// rule 1
lck.unlock();
continue;
}
if(!target.tryPop(&second)){
lck.unlock();
// rule 2
if(first == kPill)
return target.push(first);
// rule 3
target.push(first);
continue;
}
if(!target.tryPop(&third)){
lck.unlock();
// rule 4
if(
first == kPill ||
second == kPill
)
{
target.push(first);
target.push(second);
return;
}
// rule 5
target.push(first + second);
continue;
}
lck.unlock();
// make death pill be in third number
if(first == kPill)
std::swap(first, third);
if(second == kPill)
std::swap(second, third);
// rule 6
if(third != kPill){
target.push(first + second + third);
continue;
}
// rule 7
target.push(first + second);
target.push(third);
continue;
}
}
}
TYPED_TEST_P(ConcurrentQueueTest, Init){
// Check whether initialization is ok
TypeParam q;
}
TYPED_TEST_P(ConcurrentQueueTest, ConcatenationSequential){
// Check whether push is ok sequentially
TypeParam q{};
for(size_t i = 0; i < kSamplesNum; ++i)
q.push(generate(i));
std::string res{""};
for(size_t i = 0; i < kSamplesNum; ++i){
std::string current{""};
ASSERT_TRUE(q.tryPop(¤t));
res += current;
}
{
std::string dummy{""};
ASSERT_FALSE(q.tryPop(&dummy));
}
ASSERT_TRUE(check(res));
}
TYPED_TEST_P(ConcurrentQueueTest, ConcatenationMultiThreadProducer){
// Check whether push is ok
TypeParam q{};
std::vector<std::thread> producers{};
for(size_t i = 0; i < kProducersNum; ++i)
producers.push_back(std::thread(produce, i, std::ref(q)));
for(auto &producer: producers)
producer.join();
std::string res{""};
std::string current{""};
while(q.tryPop(¤t))
res += current;
ASSERT_TRUE(check(res));
}
TYPED_TEST_P(ConcurrentQueueTest, ConcatenationMPMC){
// Check whether push and pop work fine in parallel
// Logic and rules are the same as in tests.cpp AddNumbers test
TypeParam q{};
size_t
consumersNum = std::max(
std::thread::hardware_concurrency() / 2,
static_cast<unsigned int>(1)
);
std::vector<std::thread>
consumerThreads{},
producerThreads{};
for(size_t i = 0; i < kProducersNum; ++i)
producerThreads.push_back(std::thread(produce, i, std::ref(q)));
for(size_t i = 0; i < consumersNum; ++i)
consumerThreads.push_back(std::thread(consume, std::ref(q)));
for(auto &it: producerThreads)
it.join();
q.push(kPill);
for(auto &it: consumerThreads)
it.join();
std::string first{""}, second{""};
ASSERT_TRUE(q.tryPop(&first));
ASSERT_TRUE(q.tryPop(&second));
{
std::string dummy{""};
ASSERT_FALSE(q.tryPop(&dummy));
}
if(first == kPill)
std::swap(first, second);
ASSERT_TRUE(check(first));
}
REGISTER_TYPED_TEST_CASE_P(
ConcurrentQueueTest,
Init,
ConcatenationSequential,
ConcatenationMultiThreadProducer,
ConcatenationMPMC
);
typedef ::testing::Types<ConcurrentQueueSimple<std::string>, ConcurrentQueueExtended<std::string>> ConcurrentQueueTypes;
INSTANTIATE_TYPED_TEST_CASE_P(ConcurrentQueueInstantiation, ConcurrentQueueTest, ConcurrentQueueTypes);
int main(int argc, char **argv){
{
size_t defaultN = 100;
std::stringstream ss{""};
switch(argc){
case 0: case 1:
kSamplesNum = defaultN;
break;
case 2:
ss.str(*(++argv));
if(!(ss>>kSamplesNum))
throw std::runtime_error("Invalid argument");
--argv;
break;
default:
throw std::runtime_error("Invalid number of arguments");
break;
}
}
std::cout << "Testing:" << std::endl;
std::cout << "\tSamples num: " << kSamplesNum << std::endl;
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}