forked from STEllAR-GROUP/hpx
/
test_utils.hpp
308 lines (267 loc) · 8.81 KB
/
test_utils.hpp
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright (c) 2014-2015 Hartmut Kaiser
// Copyright (c) 2018 Taeguk Kwon
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#if !defined(HPX_PARALLEL_TEST_ITERATOR_MAY_29_2014_0110PM)
#define HPX_PARALLEL_TEST_ITERATOR_MAY_29_2014_0110PM
#include <hpx/include/parallel_execution_policy.hpp>
#include <hpx/include/util.hpp>
#include <algorithm>
#include <atomic>
#include <cstddef>
#include <iterator>
#include <numeric>
#include <random>
#include <vector>
namespace test
{
///////////////////////////////////////////////////////////////////////////
template <typename BaseIterator, typename IteratorTag>
struct test_iterator
: hpx::util::iterator_adaptor<
test_iterator<BaseIterator, IteratorTag>,
BaseIterator, void, IteratorTag>
{
private:
typedef hpx::util::iterator_adaptor<
test_iterator<BaseIterator, IteratorTag>,
BaseIterator, void, IteratorTag>
base_type;
public:
test_iterator() : base_type() {}
test_iterator(BaseIterator base) : base_type(base) {}
};
///////////////////////////////////////////////////////////////////////////
template <typename BaseIterator, typename IteratorTag>
struct decorated_iterator
: hpx::util::iterator_adaptor<
decorated_iterator<BaseIterator, IteratorTag>,
BaseIterator, void, IteratorTag>
{
private:
typedef hpx::util::iterator_adaptor<
decorated_iterator<BaseIterator, IteratorTag>,
BaseIterator, void, IteratorTag>
base_type;
public:
decorated_iterator()
{}
decorated_iterator(BaseIterator base)
: base_type(base)
{}
decorated_iterator(BaseIterator base, std::function<void()> f)
: base_type(base), m_callback(f)
{}
private:
friend class hpx::util::iterator_core_access;
typename base_type::reference dereference() const
{
if (m_callback)
m_callback();
return *(this->base());
}
private:
std::function<void()> m_callback;
};
///////////////////////////////////////////////////////////////////////////
template <typename T>
struct count_instances_v
{
count_instances_v()
{
++instance_count;
++max_instance_count;
}
count_instances_v(T value)
: value_(value)
{
++instance_count;
++max_instance_count;
}
count_instances_v(count_instances_v const& rhs)
: value_(rhs.value_)
{
++instance_count;
}
count_instances_v(count_instances_v && rhs)
: value_(rhs.value_)
{
++instance_count;
}
count_instances_v& operator=(count_instances_v const& rhs)
{
value_ = rhs.value_;
return *this;
}
count_instances_v& operator=(count_instances_v && rhs)
{
value_ = rhs.value_;
return *this;
}
~count_instances_v()
{
--instance_count;
}
T value_;
static std::atomic<std::size_t> instance_count;
static std::atomic<std::size_t> max_instance_count;
};
template <typename T>
std::atomic<std::size_t> count_instances_v<T>::instance_count(0);
template <typename T>
std::atomic<std::size_t> count_instances_v<T>::max_instance_count(0);
using count_instances = count_instances_v<std::size_t>;
///////////////////////////////////////////////////////////////////////////
template <typename ExPolicy, typename IteratorTag>
struct test_num_exceptions
{
static void call(ExPolicy, hpx::exception_list const& e)
{
// The static partitioner uses four times the number of
// threads/cores for the number chunks to create.
HPX_TEST_LTE(e.size(), 4 * hpx::get_num_worker_threads());
}
};
template <typename IteratorTag>
struct test_num_exceptions<
hpx::parallel::execution::sequenced_policy, IteratorTag>
{
static void call(hpx::parallel::execution::sequenced_policy const&,
hpx::exception_list const& e)
{
HPX_TEST_EQ(e.size(), 1u);
}
};
template <typename ExPolicy>
struct test_num_exceptions<ExPolicy, std::input_iterator_tag>
{
static void call(ExPolicy, hpx::exception_list const& e)
{
HPX_TEST_EQ(e.size(), 1u);
}
};
template <>
struct test_num_exceptions<
hpx::parallel::execution::sequenced_policy, std::input_iterator_tag>
{
static void call(hpx::parallel::execution::sequenced_policy const&,
hpx::exception_list const& e)
{
HPX_TEST_EQ(e.size(), 1u);
}
};
#if defined(HPX_HAVE_GENERIC_EXECUTION_POLICY)
template <typename IteratorTag>
struct test_num_exceptions<hpx::parallel::execution_policy, IteratorTag>
{
static void call(hpx::parallel::execution_policy const& policy,
hpx::exception_list const& e)
{
using namespace hpx::parallel::v1::detail;
if (policy.type() == typeid(hpx::parallel::execution::sequenced_policy))
{
HPX_TEST_EQ(e.size(), 1u);
}
else
{
// The static partitioner uses the number of threads/cores for
// the number chunks to create.
HPX_TEST_LTE(e.size(), hpx::get_num_worker_threads());
}
}
};
template <>
struct test_num_exceptions<
hpx::parallel::execution_policy, std::input_iterator_tag>
{
static void call(hpx::parallel::execution_policy const&,
hpx::exception_list const& e)
{
HPX_TEST_EQ(e.size(), 1u);
}
};
#endif
///////////////////////////////////////////////////////////////////////////
inline std::vector<std::size_t> iota(std::size_t size, std::size_t start)
{
std::vector<std::size_t> c(size);
std::iota(std::begin(c), std::end(c), start);
return c;
}
inline std::vector<std::size_t> random_iota(std::size_t size)
{
std::vector<std::size_t> c(size);
std::iota(std::begin(c), std::end(c), 0);
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(std::begin(c), std::end(c), g);
return c;
}
template <typename T>
inline std::vector<T> random_iota(std::size_t size)
{
std::vector<T> c(size);
std::iota(std::begin(c), std::end(c), 0);
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(std::begin(c), std::end(c), g);
return c;
}
inline std::vector<std::size_t> random_fill(std::size_t size)
{
std::vector<std::size_t> c(size);
std::generate(std::begin(c), std::end(c), std::rand);
return c;
}
///////////////////////////////////////////////////////////////////////////
inline void make_ready(std::vector<hpx::promise<std::size_t> >& p,
std::vector<std::size_t>& idx)
{
std::for_each(std::begin(idx), std::end(idx),
[&p](std::size_t i)
{
p[i].set_value(i);
});
}
inline std::vector<hpx::future<std::size_t> > fill_with_futures(
std::vector<hpx::promise<std::size_t> >& p)
{
std::vector<hpx::future<std::size_t> > f;
std::transform(std::begin(p), std::end(p), std::back_inserter(f),
[](hpx::promise<std::size_t>& pr)
{
return pr.get_future();
});
return f;
}
///////////////////////////////////////////////////////////////////////////
inline std::vector<std::size_t>
fill_all_any_none(std::size_t size, std::size_t num_filled)
{
if (num_filled == 0)
return std::vector<std::size_t>(size, 0);
if (num_filled == size)
return std::vector<std::size_t>(size, 1);
std::vector<std::size_t> c(size, 0);
for (std::size_t i = 0; i < num_filled; /**/)
{
std::size_t pos = std::rand() % c.size(); //-V104
if (c[pos])
continue;
c[pos] = 1;
++i;
}
return c;
}
///////////////////////////////////////////////////////////////////////////
template <typename InputIter1, typename InputIter2>
bool equal(InputIter1 first1, InputIter1 last1,
InputIter2 first2, InputIter2 last2)
{
if (std::distance(first1, last1) != std::distance(first2, last2))
return false;
return std::equal(first1, last1, first2);
}
}
#endif