• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #868

16 Jan 2023 08:21PM UTC coverage: 86.487%. Remained the same
#868

push

StellarBot
Merge #6137

6137: Adding example of a simple master/slave distributed application r=hkaiser a=hkaiser

The purpose of this example is to demonstrate how HPX actions can be used to build a simple master-slave application. The master (locality 0) assigns work to the slaves (all other localities). Note that if this application is run on one locality only it uses the same locality for the master and the slave functionalities.

The slaves receive a message that encodes how many sub-tasks of a certain type they should spawn locally.


Co-authored-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>

72 of 72 new or added lines in 1 file covered. (100.0%)

174663 of 201952 relevant lines covered (86.49%)

1849169.69 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.55
/libs/core/algorithms/tests/unit/container_algorithms/is_sorted_until_range.cpp
1
//  Copyright (c) 2015 Daniel Bourgeois
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#include <hpx/local/init.hpp>
8
#include <hpx/modules/testing.hpp>
9
#include <hpx/parallel/container_algorithms/is_sorted.hpp>
10

11
#include <cstddef>
12
#include <iostream>
13
#include <iterator>
14
#include <numeric>
15
#include <random>
16
#include <string>
17
#include <vector>
18

19
#include "test_utils.hpp"
20

21
////////////////////////////////////////////////////////////////////////////////
22
int seed = std::random_device{}();
1✔
23
std::mt19937 gen(seed);
1✔
24
std::uniform_int_distribution<> dis(0, 99);
1✔
25

26
template <typename ExPolicy, typename IteratorTag>
27
void test_sorted_until1(ExPolicy policy, IteratorTag)
6✔
28
{
29
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
30
        "hpx::is_execution_policy<ExPolicy>::value");
31

32
    typedef std::vector<int>::iterator base_iterator;
33
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
34

35
    std::vector<int> c(10007);
6✔
36
    std::iota(std::begin(c), std::end(c), 0);
6✔
37

38
    iterator until = hpx::ranges::is_sorted_until(
6✔
39
        policy, iterator(std::begin(c)), iterator(std::end(c)));
6✔
40

41
    base_iterator test_index = std::end(c);
6✔
42

43
    HPX_TEST(until == iterator(test_index));
6✔
44

45
    until = hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)),
12✔
46
        iterator(std::end(c)), std::less<int>(),
6✔
47
        [](int x) { return x == 500 ? -x : x; });
46,851✔
48

49
    test_index = std::begin(c) + 500;
6✔
50

51
    HPX_TEST(until == iterator(test_index));
6✔
52
}
6✔
53

54
template <typename ExPolicy, typename IteratorTag>
55
void test_sorted_until1_async(ExPolicy p, IteratorTag)
4✔
56
{
57
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
58
        "hpx::is_execution_policy<ExPolicy>::value");
59

60
    typedef std::vector<int>::iterator base_iterator;
61
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
62

63
    std::vector<int> c(10007);
4✔
64
    std::iota(std::begin(c), std::end(c), 0);
4✔
65

66
    hpx::future<iterator> f1 = hpx::ranges::is_sorted_until(
4✔
67
        p, iterator(std::begin(c)), iterator(std::end(c)));
4✔
68

69
    base_iterator test_index = std::end(c);
4✔
70

71
    f1.wait();
4✔
72
    HPX_TEST(f1.get() == iterator(test_index));
4✔
73

74
    hpx::future<iterator> f2 = hpx::ranges::is_sorted_until(p,
4✔
75
        iterator(std::begin(c)), iterator(std::end(c)), std::less<int>(),
4✔
76
        [](int x) { return x == 500 ? -x : x; });
21,612✔
77

78
    f2.wait();
4✔
79
    test_index = std::begin(c) + 500;
4✔
80
    HPX_TEST(f2.get() == iterator(test_index));
4✔
81
}
4✔
82

83
template <typename IteratorTag>
84
void test_sorted_until1_seq(IteratorTag)
2✔
85
{
86
    typedef std::vector<int>::iterator base_iterator;
87
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
88

89
    std::vector<int> c(10007);
2✔
90
    std::iota(std::begin(c), std::end(c), 0);
2✔
91

92
    iterator until = hpx::ranges::is_sorted_until(
2✔
93
        iterator(std::begin(c)), iterator(std::end(c)));
2✔
94

95
    base_iterator test_index = std::end(c);
2✔
96

97
    HPX_TEST(until == iterator(test_index));
2✔
98

99
    until = hpx::ranges::is_sorted_until(iterator(std::begin(c)),
4✔
100
        iterator(std::end(c)), std::less<int>(),
2✔
101
        [](int x) { return x == 500 ? -x : x; });
2,000✔
102

103
    test_index = std::begin(c) + 500;
2✔
104

105
    HPX_TEST(until == iterator(test_index));
2✔
106
}
2✔
107

108
template <typename ExPolicy>
109
void test_sorted_until1(ExPolicy policy)
3✔
110
{
111
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
112
        "hpx::is_execution_policy<ExPolicy>::value");
113

114
    std::vector<int> c(10007);
3✔
115
    std::iota(std::begin(c), std::end(c), 0);
3✔
116

117
    auto until = hpx::ranges::is_sorted_until(policy, c);
3✔
118

119
    auto test_index = std::end(c);
3✔
120

121
    HPX_TEST(until == test_index);
3✔
122

123
    until = hpx::ranges::is_sorted_until(
3✔
124
        policy, c, std::less<int>(), [](int x) { return x == 500 ? -x : x; });
17,380✔
125

126
    test_index = std::begin(c) + 500;
3✔
127

128
    HPX_TEST(until == test_index);
3✔
129
}
3✔
130

131
template <typename ExPolicy>
132
void test_sorted_until1_async(ExPolicy p)
2✔
133
{
134
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
135
        "hpx::is_execution_policy<ExPolicy>::value");
136

137
    std::vector<int> c(10007);
2✔
138
    std::iota(std::begin(c), std::end(c), 0);
2✔
139

140
    auto f1 = hpx::ranges::is_sorted_until(p, c);
2✔
141

142
    auto test_index = std::end(c);
2✔
143

144
    f1.wait();
2✔
145
    HPX_TEST(f1.get() == test_index);
2✔
146

147
    auto f2 = hpx::ranges::is_sorted_until(
2✔
148
        p, c, std::less<int>(), [](int x) { return x == 500 ? -x : x; });
11,470✔
149

150
    f2.wait();
2✔
151
    test_index = std::begin(c) + 500;
2✔
152
    HPX_TEST(f2.get() == test_index);
2✔
153
}
2✔
154

155
void test_sorted_until1_seq()
1✔
156
{
157
    std::vector<int> c(10007);
1✔
158
    std::iota(std::begin(c), std::end(c), 0);
1✔
159

160
    auto until = hpx::ranges::is_sorted_until(c);
1✔
161

162
    auto test_index = std::end(c);
1✔
163

164
    HPX_TEST(until == test_index);
1✔
165

166
    until = hpx::ranges::is_sorted_until(
1✔
167
        c, std::less<int>(), [](int x) { return x == 500 ? -x : x; });
1,000✔
168

169
    test_index = std::begin(c) + 500;
1✔
170

171
    HPX_TEST(until == test_index);
1✔
172
}
1✔
173

174
template <typename IteratorTag>
175
void test_sorted_until1()
2✔
176
{
177
    using namespace hpx::execution;
178

179
    test_sorted_until1(seq, IteratorTag());
2✔
180
    test_sorted_until1(par, IteratorTag());
2✔
181
    test_sorted_until1(par_unseq, IteratorTag());
2✔
182

183
    test_sorted_until1_async(seq(task), IteratorTag());
2✔
184
    test_sorted_until1_async(par(task), IteratorTag());
2✔
185

186
    test_sorted_until1_seq(IteratorTag());
2✔
187
}
2✔
188

189
void sorted_until_test1()
1✔
190
{
191
    test_sorted_until1<std::random_access_iterator_tag>();
1✔
192
    test_sorted_until1<std::forward_iterator_tag>();
1✔
193

194
    using namespace hpx::execution;
195

196
    test_sorted_until1(seq);
1✔
197
    test_sorted_until1(par);
1✔
198
    test_sorted_until1(par_unseq);
1✔
199

200
    test_sorted_until1_async(seq(task));
1✔
201
    test_sorted_until1_async(par(task));
1✔
202

203
    test_sorted_until1_seq();
1✔
204
}
1✔
205

206
////////////////////////////////////////////////////////////////////////////////
207
template <typename ExPolicy, typename IteratorTag>
208
void test_sorted_until2(ExPolicy policy, IteratorTag)
6✔
209
{
210
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
211
        "hpx::is_execution_policy<ExPolicy>::value");
212

213
    typedef std::vector<int>::iterator base_iterator;
214
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
215

216
    std::vector<int> c(10007);
6✔
217
    //Fill with sorted values from 0 to 10006
218
    std::iota(std::begin(c), std::end(c), 0);
6✔
219
    //Add a certain large value in middle of array to ignore
220
    int ignore = 20000;
6✔
221
    c[c.size() / 2] = ignore;
6✔
222
    //Provide custom predicate to ignore the value of ignore
223
    //pred should return true when it is given something deemed not sorted
224
    auto pred = [&ignore](int ahead, int behind) {
57,271✔
225
        return behind > ahead && behind != ignore;
57,265✔
226
    };
227

228
    iterator until = hpx::ranges::is_sorted_until(
6✔
229
        policy, iterator(std::begin(c)), iterator(std::end(c)), pred);
6✔
230

231
    base_iterator test_index = std::end(c);
6✔
232

233
    HPX_TEST(until == iterator(test_index));
6✔
234

235
    until = hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)),
12✔
236
        iterator(std::end(c)), std::less<int>(), [&](int x) {
115,261✔
237
            return x == ignore ? static_cast<int>(c.size()) / 2 : x;
115,255✔
238
        });
239

240
    test_index = std::end(c);
6✔
241

242
    HPX_TEST(until == iterator(test_index));
6✔
243
}
6✔
244

245
template <typename ExPolicy, typename IteratorTag>
246
void test_sorted_until2_async(ExPolicy p, IteratorTag)
4✔
247
{
248
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
249
        "hpx::is_execution_policy<ExPolicy>::value");
250

251
    typedef std::vector<int>::iterator base_iterator;
252
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
253

254
    std::vector<int> c(10007);
4✔
255
    //Fill with sorted values from 0 to 10006
256
    std::iota(std::begin(c), std::end(c), 0);
4✔
257
    //Add a certain large value in middle of array to ignore
258
    int ignore = 20000;
4✔
259
    c[c.size() / 2] = ignore;
4✔
260
    //Provide custom predicate to ignore the value of ignore
261
    //pred should return true when it is given something deemed not sorted
262
    auto pred = [&ignore](int ahead, int behind) {
37,486✔
263
        return behind > ahead && behind != ignore;
37,482✔
264
    };
265

266
    hpx::future<iterator> f1 = hpx::ranges::is_sorted_until(
4✔
267
        p, iterator(std::begin(c)), iterator(std::end(c)), pred);
4✔
268

269
    base_iterator test_index = std::end(c);
4✔
270
    f1.wait();
4✔
271
    HPX_TEST(f1.get() == iterator(test_index));
4✔
272

273
    hpx::future<iterator> f2 =
274
        hpx::ranges::is_sorted_until(p, iterator(std::begin(c)),
8✔
275
            iterator(std::end(c)), std::less<int>(), [&](int x) {
74,068✔
276
                return x == ignore ? static_cast<int>(c.size()) / 2 : x;
74,064✔
277
            });
278

279
    f2.wait();
4✔
280
    test_index = std::end(c);
4✔
281
    HPX_TEST(f2.get() == iterator(test_index));
4✔
282
}
4✔
283

284
template <typename IteratorTag>
285
void test_sorted_until2_seq(IteratorTag)
2✔
286
{
287
    typedef std::vector<int>::iterator base_iterator;
288
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
289

290
    std::vector<int> c(10007);
2✔
291
    //Fill with sorted values from 0 to 10006
292
    std::iota(std::begin(c), std::end(c), 0);
2✔
293
    //Add a certain large value in middle of array to ignore
294
    int ignore = 20000;
2✔
295
    c[c.size() / 2] = ignore;
2✔
296
    //Provide custom predicate to ignore the value of ignore
297
    //pred should return true when it is given something deemed not sorted
298
    auto pred = [&ignore](int ahead, int behind) {
20,014✔
299
        return behind > ahead && behind != ignore;
20,012✔
300
    };
301

302
    iterator until = hpx::ranges::is_sorted_until(
2✔
303
        iterator(std::begin(c)), iterator(std::end(c)), pred);
2✔
304

305
    base_iterator test_index = std::end(c);
2✔
306

307
    HPX_TEST(until == iterator(test_index));
2✔
308

309
    until = hpx::ranges::is_sorted_until(iterator(std::begin(c)),
4✔
310
        iterator(std::end(c)), std::less<int>(), [&](int x) {
40,026✔
311
            return x == ignore ? static_cast<int>(c.size()) / 2 : x;
40,024✔
312
        });
313

314
    test_index = std::end(c);
2✔
315

316
    HPX_TEST(until == iterator(test_index));
2✔
317
}
2✔
318

319
template <typename ExPolicy>
320
void test_sorted_until2(ExPolicy policy)
3✔
321
{
322
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
323
        "hpx::is_execution_policy<ExPolicy>::value");
324

325
    std::vector<int> c(10007);
3✔
326
    //Fill with sorted values from 0 to 10006
327
    std::iota(std::begin(c), std::end(c), 0);
3✔
328
    //Add a certain large value in middle of array to ignore
329
    int ignore = 20000;
3✔
330
    c[c.size() / 2] = ignore;
3✔
331
    //Provide custom predicate to ignore the value of ignore
332
    //pred should return true when it is given something deemed not sorted
333
    auto pred = [&ignore](int ahead, int behind) {
28,262✔
334
        return behind > ahead && behind != ignore;
28,259✔
335
    };
336

337
    auto until = hpx::ranges::is_sorted_until(policy, c, pred);
3✔
338

339
    auto test_index = std::end(c);
3✔
340

341
    HPX_TEST(until == test_index);
3✔
342

343
    until =
3✔
344
        hpx::ranges::is_sorted_until(policy, c, std::less<int>(), [&](int x) {
51,560✔
345
            return x == ignore ? static_cast<int>(c.size()) / 2 : x;
51,557✔
346
        });
347

348
    test_index = std::end(c);
3✔
349

350
    HPX_TEST(until == test_index);
3✔
351
}
3✔
352

353
template <typename ExPolicy>
354
void test_sorted_until2_async(ExPolicy p)
2✔
355
{
356
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
357
        "hpx::is_execution_policy<ExPolicy>::value");
358

359
    std::vector<int> c(10007);
2✔
360
    //Fill with sorted values from 0 to 10006
361
    std::iota(std::begin(c), std::end(c), 0);
2✔
362
    //Add a certain large value in middle of array to ignore
363
    int ignore = 20000;
2✔
364
    c[c.size() / 2] = ignore;
2✔
365
    //Provide custom predicate to ignore the value of ignore
366
    //pred should return true when it is given something deemed not sorted
367
    auto pred = [&ignore](int ahead, int behind) {
20,014✔
368
        return behind > ahead && behind != ignore;
20,012✔
369
    };
370

371
    auto f1 = hpx::ranges::is_sorted_until(p, c, pred);
2✔
372

373
    auto test_index = std::end(c);
2✔
374
    f1.wait();
2✔
375
    HPX_TEST(f1.get() == test_index);
2✔
376

377
    auto f2 = hpx::ranges::is_sorted_until(p, c, std::less<int>(), [&](int x) {
35,271✔
378
        return x == ignore ? static_cast<int>(c.size()) / 2 : x;
35,269✔
379
    });
380

381
    f2.wait();
2✔
382
    test_index = std::end(c);
2✔
383
    HPX_TEST(f2.get() == test_index);
2✔
384
}
2✔
385

386
void test_sorted_until2_seq()
1✔
387
{
388
    std::vector<int> c(10007);
1✔
389
    //Fill with sorted values from 0 to 10006
390
    std::iota(std::begin(c), std::end(c), 0);
1✔
391
    //Add a certain large value in middle of array to ignore
392
    int ignore = 20000;
1✔
393
    c[c.size() / 2] = ignore;
1✔
394
    //Provide custom predicate to ignore the value of ignore
395
    //pred should return true when it is given something deemed not sorted
396
    auto pred = [&ignore](int ahead, int behind) {
10,007✔
397
        return behind > ahead && behind != ignore;
10,006✔
398
    };
399

400
    auto until = hpx::ranges::is_sorted_until(c, pred);
1✔
401

402
    auto test_index = std::end(c);
1✔
403

404
    HPX_TEST(until == test_index);
1✔
405

406
    until = hpx::ranges::is_sorted_until(c, std::less<int>(), [&](int x) {
20,013✔
407
        return x == ignore ? static_cast<int>(c.size()) / 2 : x;
20,012✔
408
    });
409

410
    test_index = std::end(c);
1✔
411

412
    HPX_TEST(until == test_index);
1✔
413
}
1✔
414

415
template <typename IteratorTag>
416
void test_sorted_until2()
2✔
417
{
418
    using namespace hpx::execution;
419
    test_sorted_until2(seq, IteratorTag());
2✔
420
    test_sorted_until2(par, IteratorTag());
2✔
421
    test_sorted_until2(par_unseq, IteratorTag());
2✔
422

423
    test_sorted_until2_async(seq(task), IteratorTag());
2✔
424
    test_sorted_until2_async(par(task), IteratorTag());
2✔
425

426
    test_sorted_until2_seq(IteratorTag());
2✔
427
}
2✔
428

429
void sorted_until_test2()
1✔
430
{
431
    test_sorted_until2<std::random_access_iterator_tag>();
1✔
432
    test_sorted_until2<std::forward_iterator_tag>();
1✔
433

434
    using namespace hpx::execution;
435

436
    test_sorted_until2(seq);
1✔
437
    test_sorted_until2(par);
1✔
438
    test_sorted_until2(par_unseq);
1✔
439

440
    test_sorted_until2_async(seq(task));
1✔
441
    test_sorted_until2_async(par(task));
1✔
442

443
    test_sorted_until2_seq();
1✔
444
}
1✔
445

446
////////////////////////////////////////////////////////////////////////////////
447
template <typename ExPolicy, typename IteratorTag>
448
void test_sorted_until3(ExPolicy policy, IteratorTag)
6✔
449
{
450
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
451
        "hpx::is_execution_policy<ExPolicy>::value");
452

453
    typedef std::vector<int>::iterator base_iterator;
454
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
455
    //test the following:
456
    // put unsorted elements at each ends
457
    // put two unsorted elements in the middle
458

459
    std::vector<int> c1(10007);
6✔
460
    std::vector<int> c2(10007);
6✔
461
    std::iota(std::begin(c1), std::end(c1), 0);
6✔
462
    std::iota(std::begin(c2), std::end(c2), 0);
6✔
463

464
    iterator until1 =
465
        hpx::ranges::is_sorted_until(policy, iterator(std::begin(c1)),
12✔
466
            iterator(std::end(c1)), std::less<int>(), [&](int x) {
29,273✔
467
                if (x == 0)
29,281✔
468
                {
469
                    return 20000;
6✔
470
                }
471
                else if (x == static_cast<int>(c1.size()) - 1)
29,214✔
472
                {
473
                    return 0;
1✔
474
                }
475
                else
476
                {
477
                    return x;
29,225✔
478
                }
479
            });
29,216✔
480
    iterator until2 =
481
        hpx::ranges::is_sorted_until(policy, iterator(std::begin(c2)),
12✔
482
            iterator(std::end(c2)), std::less<int>(), [&](int x) {
55,500✔
483
                if (x == static_cast<int>(c2.size()) / 3 ||
55,494✔
484
                    x == 2 * static_cast<int>(c2.size()) / 3)
55,241✔
485
                {
486
                    return 0;
14✔
487
                }
488
                else
489
                {
490
                    return x;
55,233✔
491
                }
492
            });
55,246✔
493

494
    base_iterator test_index1 = std::begin(c1) + 1;
6✔
495
    base_iterator test_index2 = std::begin(c2) + c2.size() / 3;
6✔
496

497
    HPX_TEST(until1 == iterator(test_index1));
6✔
498
    HPX_TEST(until2 == iterator(test_index2));
6✔
499
}
6✔
500

501
template <typename ExPolicy, typename IteratorTag>
502
void test_sorted_until3_async(ExPolicy p, IteratorTag)
4✔
503
{
504
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
505
        "hpx::is_execution_policy<ExPolicy>::value");
506

507
    typedef std::vector<int>::iterator base_iterator;
508
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
509
    //test the following:
510
    // put unsorted elements at each ends
511
    // put two unsorted elements in the middle
512

513
    std::vector<int> c1(10007);
4✔
514
    std::vector<int> c2(10007);
4✔
515
    std::iota(std::begin(c1), std::end(c1), 0);
4✔
516
    std::iota(std::begin(c2), std::end(c2), 0);
4✔
517

518
    hpx::future<iterator> f1 =
519
        hpx::ranges::is_sorted_until(p, iterator(std::begin(c1)),
8✔
520
            iterator(std::end(c1)), std::less<int>(), [&](int x) {
26,711✔
521
                if (x == 0)
26,707✔
522
                {
523
                    return 20000;
4✔
524
                }
525
                else if (x == static_cast<int>(c1.size()) - 1)
26,691✔
526
                {
527
                    return 0;
×
528
                }
529
                else
530
                {
531
                    return x;
26,689✔
532
                }
533
            });
26,693✔
534
    hpx::future<iterator> f2 =
535
        hpx::ranges::is_sorted_until(p, iterator(std::begin(c2)),
8✔
536
            iterator(std::end(c2)), std::less<int>(), [&](int x) {
26,224✔
537
                if (x == static_cast<int>(c2.size()) / 3 ||
26,220✔
538
                    x == 2 * static_cast<int>(c2.size()) / 3)
26,061✔
539
                {
540
                    return 0;
6✔
541
                }
542
                else
543
                {
544
                    return x;
26,080✔
545
                }
546
            });
26,086✔
547

548
    base_iterator test_index1 = std::begin(c1) + 1;
4✔
549
    base_iterator test_index2 = std::begin(c2) + c2.size() / 3;
4✔
550

551
    f1.wait();
4✔
552
    HPX_TEST(f1.get() == iterator(test_index1));
4✔
553
    f2.wait();
4✔
554
    HPX_TEST(f2.get() == iterator(test_index2));
4✔
555
}
4✔
556

557
template <typename IteratorTag>
558
void test_sorted_until3_seq(IteratorTag)
2✔
559
{
560
    typedef std::vector<int>::iterator base_iterator;
561
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
562
    //test the following:
563
    // put unsorted elements at each ends
564
    // put two unsorted elements in the middle
565

566
    std::vector<int> c1(10007);
2✔
567
    std::vector<int> c2(10007);
2✔
568
    std::iota(std::begin(c1), std::end(c1), 0);
2✔
569
    std::iota(std::begin(c2), std::end(c2), 0);
2✔
570

571
    iterator until1 = hpx::ranges::is_sorted_until(iterator(std::begin(c1)),
4✔
572
        iterator(std::end(c1)), std::less<int>(), [&](int x) {
6✔
573
            if (x == 0)
4✔
574
            {
575
                return 20000;
2✔
576
            }
577
            else if (x == static_cast<int>(c1.size()) - 1)
2✔
578
            {
579
                return 0;
×
580
            }
581
            else
582
            {
583
                return x;
2✔
584
            }
585
        });
4✔
586
    iterator until2 = hpx::ranges::is_sorted_until(iterator(std::begin(c2)),
4✔
587
        iterator(std::end(c2)), std::less<int>(), [&](int x) {
13,342✔
588
            if (x == static_cast<int>(c2.size()) / 3 ||
13,340✔
589
                x == 2 * static_cast<int>(c2.size()) / 3)
13,338✔
590
            {
591
                return 0;
2✔
592
            }
593
            else
594
            {
595
                return x;
13,338✔
596
            }
597
        });
13,340✔
598

599
    base_iterator test_index1 = std::begin(c1) + 1;
2✔
600
    base_iterator test_index2 = std::begin(c2) + c2.size() / 3;
2✔
601

602
    HPX_TEST(until1 == iterator(test_index1));
2✔
603
    HPX_TEST(until2 == iterator(test_index2));
2✔
604
}
2✔
605

606
template <typename ExPolicy>
607
void test_sorted_until3(ExPolicy policy)
3✔
608
{
609
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
610
        "hpx::is_execution_policy<ExPolicy>::value");
611

612
    //test the following:
613
    // put unsorted elements at each ends
614
    // put two unsorted elements in the middle
615

616
    std::vector<int> c1(10007);
3✔
617
    std::vector<int> c2(10007);
3✔
618
    std::iota(std::begin(c1), std::end(c1), 0);
3✔
619
    std::iota(std::begin(c2), std::end(c2), 0);
3✔
620

621
    auto until1 =
622
        hpx::ranges::is_sorted_until(policy, c1, std::less<int>(), [&](int x) {
12,302✔
623
            if (x == 0)
12,296✔
624
            {
625
                return 20000;
3✔
626
            }
627
            else if (x == static_cast<int>(c1.size()) - 1)
12,326✔
628
            {
629
                return 0;
×
630
            }
631
            else
632
            {
633
                return x;
12,323✔
634
            }
635
        });
12,326✔
636
    auto until2 =
637
        hpx::ranges::is_sorted_until(policy, c2, std::less<int>(), [&](int x) {
28,707✔
638
            if (x == static_cast<int>(c2.size()) / 3 ||
28,704✔
639
                x == 2 * static_cast<int>(c2.size()) / 3)
28,080✔
640
            {
641
                return 0;
7✔
642
            }
643
            else
644
            {
645
                return x;
28,102✔
646
            }
647
        });
28,042✔
648

649
    auto test_index1 = std::begin(c1) + 1;
3✔
650
    auto test_index2 = std::begin(c2) + c2.size() / 3;
3✔
651

652
    HPX_TEST(until1 == test_index1);
3✔
653
    HPX_TEST(until2 == test_index2);
3✔
654
}
3✔
655

656
template <typename ExPolicy>
657
void test_sorted_until3_async(ExPolicy p)
2✔
658
{
659
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
660
        "hpx::is_execution_policy<ExPolicy>::value");
661

662
    //test the following:
663
    // put unsorted elements at each ends
664
    // put two unsorted elements in the middle
665

666
    std::vector<int> c1(10007);
2✔
667
    std::vector<int> c2(10007);
2✔
668
    std::iota(std::begin(c1), std::end(c1), 0);
2✔
669
    std::iota(std::begin(c2), std::end(c2), 0);
2✔
670

671
    auto f1 = hpx::ranges::is_sorted_until(p, c1, std::less<int>(), [&](int x) {
2,050✔
672
        if (x == 0)
2,048✔
673
        {
674
            return 20000;
2✔
675
        }
676
        else if (x == static_cast<int>(c1.size()) - 1)
2,046✔
677
        {
678
            return 0;
×
679
        }
680
        else
681
        {
682
            return x;
2,046✔
683
        }
684
    });
2,048✔
685
    auto f2 = hpx::ranges::is_sorted_until(p, c2, std::less<int>(), [&](int x) {
24,632✔
686
        if (x == static_cast<int>(c2.size()) / 3 ||
24,630✔
687
            x == 2 * static_cast<int>(c2.size()) / 3)
24,627✔
688
        {
689
            return 0;
5✔
690
        }
691
        else
692
        {
693
            return x;
24,625✔
694
        }
695
    });
24,630✔
696

697
    auto test_index1 = std::begin(c1) + 1;
2✔
698
    auto test_index2 = std::begin(c2) + c2.size() / 3;
2✔
699

700
    f1.wait();
2✔
701
    HPX_TEST(f1.get() == test_index1);
2✔
702
    f2.wait();
2✔
703
    HPX_TEST(f2.get() == test_index2);
2✔
704
}
2✔
705

706
void test_sorted_until3_seq()
1✔
707
{
708
    //test the following:
709
    // put unsorted elements at each ends
710
    // put two unsorted elements in the middle
711

712
    std::vector<int> c1(10007);
1✔
713
    std::vector<int> c2(10007);
1✔
714
    std::iota(std::begin(c1), std::end(c1), 0);
1✔
715
    std::iota(std::begin(c2), std::end(c2), 0);
1✔
716

717
    auto until1 =
718
        hpx::ranges::is_sorted_until(c1, std::less<int>(), [&](int x) {
3✔
719
            if (x == 0)
2✔
720
            {
721
                return 20000;
1✔
722
            }
723
            else if (x == static_cast<int>(c1.size()) - 1)
1✔
724
            {
725
                return 0;
×
726
            }
727
            else
728
            {
729
                return x;
1✔
730
            }
731
        });
2✔
732
    auto until2 =
733
        hpx::ranges::is_sorted_until(c2, std::less<int>(), [&](int x) {
6,671✔
734
            if (x == static_cast<int>(c2.size()) / 3 ||
6,670✔
735
                x == 2 * static_cast<int>(c2.size()) / 3)
6,669✔
736
            {
737
                return 0;
1✔
738
            }
739
            else
740
            {
741
                return x;
6,669✔
742
            }
743
        });
6,670✔
744

745
    auto test_index1 = std::begin(c1) + 1;
1✔
746
    auto test_index2 = std::begin(c2) + c2.size() / 3;
1✔
747

748
    HPX_TEST(until1 == test_index1);
1✔
749
    HPX_TEST(until2 == test_index2);
1✔
750
}
1✔
751

752
template <typename IteratorTag>
753
void test_sorted_until3()
2✔
754
{
755
    using namespace hpx::execution;
756
    test_sorted_until3(seq, IteratorTag());
2✔
757
    test_sorted_until3(par, IteratorTag());
2✔
758
    test_sorted_until3(par_unseq, IteratorTag());
2✔
759

760
    test_sorted_until3_async(seq(task), IteratorTag());
2✔
761
    test_sorted_until3_async(par(task), IteratorTag());
2✔
762

763
    test_sorted_until3_seq(IteratorTag());
2✔
764
}
2✔
765

766
void sorted_until_test3()
1✔
767
{
768
    test_sorted_until3<std::random_access_iterator_tag>();
1✔
769
    test_sorted_until3<std::forward_iterator_tag>();
1✔
770

771
    using namespace hpx::execution;
772

773
    test_sorted_until3(seq);
1✔
774
    test_sorted_until3(par);
1✔
775
    test_sorted_until3(par_unseq);
1✔
776

777
    test_sorted_until3_async(seq(task));
1✔
778
    test_sorted_until3_async(par(task));
1✔
779

780
    test_sorted_until3_seq();
1✔
781
}
1✔
782

783
////////////////////////////////////////////////////////////////////////////////
784
template <typename ExPolicy, typename IteratorTag>
785
void test_sorted_until_exception(ExPolicy policy, IteratorTag)
4✔
786
{
787
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
788
        "hpx::is_execution_policy<ExPolicy>::value");
789

790
    typedef std::vector<int>::iterator base_iterator;
791
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
792
    typedef test::decorated_iterator<base_iterator, IteratorTag>
793
        decorated_iterator;
794

795
    std::vector<int> c(10007);
4✔
796
    //fill first half of array with even numbers and second half
797
    //with odd numbers
798
    std::iota(std::begin(c), std::end(c), 0);
4✔
799

800
    bool caught_exception = false;
4✔
801
    try
802
    {
803
        hpx::ranges::is_sorted_until(policy,
4✔
804
            decorated_iterator(
4✔
805
                std::begin(c), []() { throw std::runtime_error("test"); }),
14✔
806
            decorated_iterator(
4✔
807
                std::end(c), []() { throw std::runtime_error("test"); }));
4✔
808
    }
4✔
809
    catch (hpx::exception_list const& e)
810
    {
811
        caught_exception = true;
4✔
812
        test::test_num_exceptions<ExPolicy, IteratorTag>::call(policy, e);
4✔
813
    }
4✔
814
    catch (...)
815
    {
816
        HPX_TEST(false);
×
817
    }
4✔
818

819
    HPX_TEST(caught_exception);
4✔
820

821
    caught_exception = false;
4✔
822
    try
823
    {
824
        hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)),
8✔
825
            iterator(std::end(c)),
4✔
826
            [](int, int) -> bool { throw std::runtime_error("test"); });
10✔
827
    }
4✔
828
    catch (hpx::exception_list const& e)
829
    {
830
        caught_exception = true;
4✔
831
        test::test_num_exceptions<ExPolicy, IteratorTag>::call(policy, e);
4✔
832
    }
4✔
833
    catch (...)
834
    {
835
        HPX_TEST(false);
×
836
    }
4✔
837

838
    HPX_TEST(caught_exception);
4✔
839

840
    caught_exception = false;
4✔
841
    try
842
    {
843
        hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)),
8✔
844
            iterator(std::end(c)), std::less<int>(),
4✔
845
            [](int) -> int { throw std::runtime_error("test"); });
10✔
846
    }
4✔
847
    catch (hpx::exception_list const& e)
848
    {
849
        caught_exception = true;
4✔
850
        test::test_num_exceptions<ExPolicy, IteratorTag>::call(policy, e);
4✔
851
    }
4✔
852
    catch (...)
853
    {
854
        HPX_TEST(false);
×
855
    }
4✔
856

857
    HPX_TEST(caught_exception);
4✔
858
}
16✔
859

860
template <typename ExPolicy, typename IteratorTag>
861
void test_sorted_until_async_exception(ExPolicy p, IteratorTag)
4✔
862
{
863
    typedef std::vector<int>::iterator base_iterator;
864
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
865
    typedef test::decorated_iterator<base_iterator, IteratorTag>
866
        decorated_iterator;
867

868
    std::vector<int> c(10007);
4✔
869
    std::iota(std::begin(c), std::end(c), 0);
4✔
870

871
    bool caught_exception = false;
4✔
872
    try
873
    {
874
        hpx::future<decorated_iterator> f = hpx::ranges::is_sorted_until(p,
4✔
875
            decorated_iterator(
4✔
876
                std::begin(c), []() { throw std::runtime_error("test"); }),
14✔
877
            decorated_iterator(
4✔
878
                std::end(c), []() { throw std::runtime_error("test"); }));
4✔
879
        f.get();
4✔
880

881
        HPX_TEST(false);
×
882
    }
4✔
883
    catch (hpx::exception_list const& e)
884
    {
885
        caught_exception = true;
4✔
886
        test::test_num_exceptions<ExPolicy, IteratorTag>::call(p, e);
4✔
887
    }
4✔
888
    catch (...)
889
    {
890
        HPX_TEST(false);
×
891
    }
4✔
892

893
    HPX_TEST(caught_exception);
4✔
894

895
    caught_exception = false;
4✔
896
    try
897
    {
898
        hpx::future<iterator> f = hpx::ranges::is_sorted_until(p,
4✔
899
            iterator(std::begin(c)), iterator(std::end(c)),
4✔
900
            [](int, int) -> int { throw std::runtime_error("test"); });
10✔
901
        f.get();
4✔
902

903
        HPX_TEST(false);
×
904
    }
4✔
905
    catch (hpx::exception_list const& e)
906
    {
907
        caught_exception = true;
4✔
908
        test::test_num_exceptions<ExPolicy, IteratorTag>::call(p, e);
4✔
909
    }
4✔
910
    catch (...)
911
    {
912
        HPX_TEST(false);
×
913
    }
4✔
914

915
    HPX_TEST(caught_exception);
4✔
916

917
    caught_exception = false;
4✔
918
    try
919
    {
920
        hpx::future<iterator> f = hpx::ranges::is_sorted_until(p,
4✔
921
            iterator(std::begin(c)), iterator(std::end(c)), std::less<int>(),
4✔
922
            [](int) -> bool { throw std::runtime_error("test"); });
10✔
923
        f.get();
4✔
924

925
        HPX_TEST(false);
×
926
    }
4✔
927
    catch (hpx::exception_list const& e)
928
    {
929
        caught_exception = true;
4✔
930
        test::test_num_exceptions<ExPolicy, IteratorTag>::call(p, e);
4✔
931
    }
4✔
932
    catch (...)
933
    {
934
        HPX_TEST(false);
×
935
    }
4✔
936

937
    HPX_TEST(caught_exception);
4✔
938
}
16✔
939

940
template <typename IteratorTag>
941
void test_sorted_until_seq_exception(IteratorTag)
2✔
942
{
943
    typedef std::vector<int>::iterator base_iterator;
944
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
945
    typedef test::decorated_iterator<base_iterator, IteratorTag>
946
        decorated_iterator;
947

948
    std::vector<int> c(10007);
2✔
949
    //fill first half of array with even numbers and second half
950
    //with odd numbers
951
    std::iota(std::begin(c), std::end(c), 0);
2✔
952

953
    bool caught_exception = false;
2✔
954
    try
955
    {
956
        hpx::ranges::is_sorted_until(
2✔
957
            decorated_iterator(
2✔
958
                std::begin(c), []() { throw std::runtime_error("test"); }),
4✔
959
            decorated_iterator(
2✔
960
                std::end(c), []() { throw std::runtime_error("test"); }));
2✔
961
    }
2✔
962
    catch (hpx::exception_list const& e)
963
    {
964
        caught_exception = true;
2✔
965
        test::test_num_exceptions<hpx::execution::sequenced_policy,
2✔
966
            IteratorTag>::call(hpx::execution::seq, e);
2✔
967
    }
2✔
968
    catch (...)
969
    {
970
        HPX_TEST(false);
×
971
    }
2✔
972

973
    HPX_TEST(caught_exception);
2✔
974

975
    caught_exception = false;
2✔
976
    try
977
    {
978
        hpx::ranges::is_sorted_until(iterator(std::begin(c)),
4✔
979
            iterator(std::end(c)),
2✔
980
            [](int, int) -> int { throw std::runtime_error("test"); });
2✔
981
    }
2✔
982
    catch (hpx::exception_list const& e)
983
    {
984
        caught_exception = true;
2✔
985
        test::test_num_exceptions<hpx::execution::sequenced_policy,
2✔
986
            IteratorTag>::call(hpx::execution::seq, e);
2✔
987
    }
2✔
988
    catch (...)
989
    {
990
        HPX_TEST(false);
×
991
    }
2✔
992

993
    HPX_TEST(caught_exception);
2✔
994

995
    caught_exception = false;
2✔
996
    try
997
    {
998
        hpx::ranges::is_sorted_until(iterator(std::begin(c)),
4✔
999
            iterator(std::end(c)), std::less<int>(),
2✔
1000
            [](int) -> int { throw std::runtime_error("test"); });
2✔
1001
    }
2✔
1002
    catch (hpx::exception_list const& e)
1003
    {
1004
        caught_exception = true;
2✔
1005
        test::test_num_exceptions<hpx::execution::sequenced_policy,
2✔
1006
            IteratorTag>::call(hpx::execution::seq, e);
2✔
1007
    }
2✔
1008
    catch (...)
1009
    {
1010
        HPX_TEST(false);
×
1011
    }
2✔
1012

1013
    HPX_TEST(caught_exception);
2✔
1014
}
8✔
1015

1016
template <typename ExPolicy>
1017
void test_sorted_until_exception(ExPolicy policy)
2✔
1018
{
1019
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
1020
        "hpx::is_execution_policy<ExPolicy>::value");
1021

1022
    std::vector<int> c(10007);
2✔
1023
    //fill first half of array with even numbers and second half
1024
    //with odd numbers
1025
    std::iota(std::begin(c), std::end(c), 0);
2✔
1026

1027
    bool caught_exception = false;
2✔
1028
    try
1029
    {
1030
        hpx::ranges::is_sorted_until(policy, c,
2✔
1031
            [](int, int) -> bool { throw std::runtime_error("test"); });
5✔
1032
    }
2✔
1033
    catch (hpx::exception_list const& e)
1034
    {
1035
        caught_exception = true;
2✔
1036
        test::test_num_exceptions_base<ExPolicy>::call(policy, e);
2✔
1037
    }
2✔
1038
    catch (...)
1039
    {
1040
        HPX_TEST(false);
×
1041
    }
2✔
1042

1043
    HPX_TEST(caught_exception);
2✔
1044

1045
    caught_exception = false;
2✔
1046
    try
1047
    {
1048
        hpx::ranges::is_sorted_until(policy, c, std::less<int>(),
2✔
1049
            [](int) -> int { throw std::runtime_error("test"); });
5✔
1050
    }
2✔
1051
    catch (hpx::exception_list const& e)
1052
    {
1053
        caught_exception = true;
2✔
1054
        test::test_num_exceptions_base<ExPolicy>::call(policy, e);
2✔
1055
    }
2✔
1056
    catch (...)
1057
    {
1058
        HPX_TEST(false);
×
1059
    }
2✔
1060

1061
    HPX_TEST(caught_exception);
2✔
1062
}
6✔
1063

1064
template <typename ExPolicy>
1065
void test_sorted_until_async_exception(ExPolicy p)
2✔
1066
{
1067
    std::vector<int> c(10007);
2✔
1068
    std::iota(std::begin(c), std::end(c), 0);
2✔
1069

1070
    bool caught_exception = false;
2✔
1071
    try
1072
    {
1073
        auto f = hpx::ranges::is_sorted_until(
2✔
1074
            p, c, [](int, int) -> int { throw std::runtime_error("test"); });
5✔
1075
        f.get();
2✔
1076

1077
        HPX_TEST(false);
×
1078
    }
2✔
1079
    catch (hpx::exception_list const& e)
1080
    {
1081
        caught_exception = true;
2✔
1082
        test::test_num_exceptions_base<ExPolicy>::call(p, e);
2✔
1083
    }
2✔
1084
    catch (...)
1085
    {
1086
        HPX_TEST(false);
×
1087
    }
2✔
1088

1089
    HPX_TEST(caught_exception);
2✔
1090

1091
    caught_exception = false;
2✔
1092
    try
1093
    {
1094
        auto f = hpx::ranges::is_sorted_until(p, c, std::less<int>(),
2✔
1095
            [](int) -> bool { throw std::runtime_error("test"); });
5✔
1096
        f.get();
2✔
1097

1098
        HPX_TEST(false);
×
1099
    }
2✔
1100
    catch (hpx::exception_list const& e)
1101
    {
1102
        caught_exception = true;
2✔
1103
        test::test_num_exceptions_base<ExPolicy>::call(p, e);
2✔
1104
    }
2✔
1105
    catch (...)
1106
    {
1107
        HPX_TEST(false);
×
1108
    }
2✔
1109

1110
    HPX_TEST(caught_exception);
2✔
1111
}
6✔
1112

1113
void test_sorted_until_seq_exception()
1✔
1114
{
1115
    std::vector<int> c(10007);
1✔
1116
    //fill first half of array with even numbers and second half
1117
    //with odd numbers
1118
    std::iota(std::begin(c), std::end(c), 0);
1✔
1119

1120
    bool caught_exception = false;
1✔
1121
    try
1122
    {
1123
        hpx::ranges::is_sorted_until(
1✔
1124
            c, [](int, int) -> int { throw std::runtime_error("test"); });
1✔
1125
    }
1✔
1126
    catch (hpx::exception_list const& e)
1127
    {
1128
        caught_exception = true;
1✔
1129
        test::test_num_exceptions_base<hpx::execution::sequenced_policy>::call(
1✔
1130
            hpx::execution::seq, e);
1✔
1131
    }
1✔
1132
    catch (...)
1133
    {
1134
        HPX_TEST(false);
×
1135
    }
1✔
1136

1137
    HPX_TEST(caught_exception);
1✔
1138

1139
    caught_exception = false;
1✔
1140
    try
1141
    {
1142
        hpx::ranges::is_sorted_until(c, std::less<int>(),
1✔
1143
            [](int) -> int { throw std::runtime_error("test"); });
1✔
1144
    }
1✔
1145
    catch (hpx::exception_list const& e)
1146
    {
1147
        caught_exception = true;
1✔
1148
        test::test_num_exceptions_base<hpx::execution::sequenced_policy>::call(
1✔
1149
            hpx::execution::seq, e);
1✔
1150
    }
1✔
1151
    catch (...)
1152
    {
1153
        HPX_TEST(false);
×
1154
    }
1✔
1155

1156
    HPX_TEST(caught_exception);
1✔
1157
}
3✔
1158

1159
template <typename IteratorTag>
1160
void test_sorted_until_exception()
2✔
1161
{
1162
    using namespace hpx::execution;
1163
    //If the execution policy object is of type vector_execution_policy,
1164
    //  std::terminate shall be called. Therefore we do not test exceptions
1165
    //  with a vector execution policy
1166
    test_sorted_until_exception(seq, IteratorTag());
2✔
1167
    test_sorted_until_exception(par, IteratorTag());
2✔
1168

1169
    test_sorted_until_async_exception(seq(task), IteratorTag());
2✔
1170
    test_sorted_until_async_exception(par(task), IteratorTag());
2✔
1171

1172
    test_sorted_until_seq_exception(IteratorTag());
2✔
1173
}
2✔
1174

1175
void sorted_until_exception_test()
1✔
1176
{
1177
    test_sorted_until_exception<std::random_access_iterator_tag>();
1✔
1178
    test_sorted_until_exception<std::forward_iterator_tag>();
1✔
1179

1180
    using namespace hpx::execution;
1181

1182
    test_sorted_until_exception(seq);
1✔
1183
    test_sorted_until_exception(par);
1✔
1184

1185
    test_sorted_until_async_exception(seq(task));
1✔
1186
    test_sorted_until_async_exception(par(task));
1✔
1187

1188
    test_sorted_until_seq_exception();
1✔
1189
}
1✔
1190

1191
////////////////////////////////////////////////////////////////////////////////
1192
template <typename ExPolicy, typename IteratorTag>
1193
void test_sorted_until_bad_alloc(ExPolicy policy, IteratorTag)
4✔
1194
{
1195
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
1196
        "hpx::is_execution_policy<ExPolicy>::value");
1197

1198
    typedef std::vector<int>::iterator base_iterator;
1199
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
1200
    typedef test::decorated_iterator<base_iterator, IteratorTag>
1201
        decorated_iterator;
1202

1203
    std::vector<int> c(10007);
4✔
1204
    //fill first half of array with even numbers and second half
1205
    //with odd numbers
1206
    std::iota(std::begin(c), std::end(c), 0);
4✔
1207

1208
    bool caught_bad_alloc = false;
4✔
1209
    try
1210
    {
1211
        hpx::ranges::is_sorted_until(policy,
4✔
1212
            decorated_iterator(
4✔
1213
                std::begin(c), []() { throw std::runtime_error("test"); }),
14✔
1214
            decorated_iterator(
4✔
1215
                std::end(c), []() { throw std::runtime_error("test"); }));
4✔
1216
    }
4✔
1217
    catch (hpx::exception_list const& e)
1218
    {
1219
        caught_bad_alloc = true;
4✔
1220
    }
4✔
1221
    catch (...)
1222
    {
1223
        HPX_TEST(false);
×
1224
    }
4✔
1225

1226
    HPX_TEST(caught_bad_alloc);
4✔
1227

1228
    caught_bad_alloc = false;
4✔
1229
    try
1230
    {
1231
        hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)),
8✔
1232
            iterator(std::end(c)),
4✔
1233
            [](int, int) -> bool { throw std::runtime_error("test"); });
10✔
1234
    }
4✔
1235
    catch (hpx::exception_list const& e)
1236
    {
1237
        caught_bad_alloc = true;
4✔
1238
    }
4✔
1239
    catch (...)
1240
    {
1241
        HPX_TEST(false);
×
1242
    }
4✔
1243

1244
    HPX_TEST(caught_bad_alloc);
4✔
1245

1246
    caught_bad_alloc = false;
4✔
1247
    try
1248
    {
1249
        hpx::ranges::is_sorted_until(policy, iterator(std::begin(c)),
8✔
1250
            iterator(std::end(c)), std::less<int>(),
4✔
1251
            [](int) -> int { throw std::runtime_error("test"); });
10✔
1252
    }
4✔
1253
    catch (hpx::exception_list const& e)
1254
    {
1255
        caught_bad_alloc = true;
4✔
1256
    }
4✔
1257
    catch (...)
1258
    {
1259
        HPX_TEST(false);
×
1260
    }
4✔
1261

1262
    HPX_TEST(caught_bad_alloc);
4✔
1263
}
16✔
1264

1265
template <typename ExPolicy, typename IteratorTag>
1266
void test_sorted_until_async_bad_alloc(ExPolicy p, IteratorTag)
4✔
1267
{
1268
    typedef std::vector<int>::iterator base_iterator;
1269
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
1270
    typedef test::decorated_iterator<base_iterator, IteratorTag>
1271
        decorated_iterator;
1272

1273
    std::vector<int> c(10007);
4✔
1274
    std::iota(std::begin(c), std::end(c), 0);
4✔
1275

1276
    bool caught_bad_alloc = false;
4✔
1277
    try
1278
    {
1279
        hpx::future<decorated_iterator> f = hpx::ranges::is_sorted_until(p,
4✔
1280
            decorated_iterator(
4✔
1281
                std::begin(c), []() { throw std::runtime_error("test"); }),
14✔
1282
            decorated_iterator(
4✔
1283
                std::end(c), []() { throw std::runtime_error("test"); }));
4✔
1284
        f.get();
4✔
1285

1286
        HPX_TEST(false);
×
1287
    }
4✔
1288
    catch (hpx::exception_list const& e)
1289
    {
1290
        caught_bad_alloc = true;
4✔
1291
    }
4✔
1292
    catch (...)
1293
    {
1294
        HPX_TEST(false);
×
1295
    }
4✔
1296

1297
    HPX_TEST(caught_bad_alloc);
4✔
1298

1299
    caught_bad_alloc = false;
4✔
1300
    try
1301
    {
1302
        hpx::future<iterator> f = hpx::ranges::is_sorted_until(p,
4✔
1303
            iterator(std::begin(c)), iterator(std::end(c)),
4✔
1304
            [](int, int) -> int { throw std::runtime_error("test"); });
10✔
1305
        f.get();
4✔
1306

1307
        HPX_TEST(false);
×
1308
    }
4✔
1309
    catch (hpx::exception_list const& e)
1310
    {
1311
        caught_bad_alloc = true;
4✔
1312
    }
4✔
1313
    catch (...)
1314
    {
1315
        HPX_TEST(false);
×
1316
    }
4✔
1317

1318
    HPX_TEST(caught_bad_alloc);
4✔
1319

1320
    caught_bad_alloc = false;
4✔
1321
    try
1322
    {
1323
        hpx::future<iterator> f = hpx::ranges::is_sorted_until(p,
4✔
1324
            iterator(std::begin(c)), iterator(std::end(c)), std::less<int>(),
4✔
1325
            [](int) -> bool { throw std::runtime_error("test"); });
10✔
1326
        f.get();
4✔
1327

1328
        HPX_TEST(false);
×
1329
    }
4✔
1330
    catch (hpx::exception_list const& e)
1331
    {
1332
        caught_bad_alloc = true;
4✔
1333
    }
4✔
1334
    catch (...)
1335
    {
1336
        HPX_TEST(false);
×
1337
    }
4✔
1338

1339
    HPX_TEST(caught_bad_alloc);
4✔
1340
}
16✔
1341

1342
template <typename IteratorTag>
1343
void test_sorted_until_seq_bad_alloc(IteratorTag)
2✔
1344
{
1345
    typedef std::vector<int>::iterator base_iterator;
1346
    typedef test::test_iterator<base_iterator, IteratorTag> iterator;
1347
    typedef test::decorated_iterator<base_iterator, IteratorTag>
1348
        decorated_iterator;
1349

1350
    std::vector<int> c(10007);
2✔
1351
    //fill first half of array with even numbers and second half
1352
    //with odd numbers
1353
    std::iota(std::begin(c), std::end(c), 0);
2✔
1354

1355
    bool caught_bad_alloc = false;
2✔
1356
    try
1357
    {
1358
        hpx::ranges::is_sorted_until(
2✔
1359
            decorated_iterator(
2✔
1360
                std::begin(c), []() { throw std::runtime_error("test"); }),
4✔
1361
            decorated_iterator(
2✔
1362
                std::end(c), []() { throw std::runtime_error("test"); }));
2✔
1363
    }
2✔
1364
    catch (hpx::exception_list const& e)
1365
    {
1366
        caught_bad_alloc = true;
2✔
1367
    }
2✔
1368
    catch (...)
1369
    {
1370
        HPX_TEST(false);
×
1371
    }
2✔
1372

1373
    HPX_TEST(caught_bad_alloc);
2✔
1374

1375
    caught_bad_alloc = false;
2✔
1376
    try
1377
    {
1378
        hpx::ranges::is_sorted_until(iterator(std::begin(c)),
4✔
1379
            iterator(std::end(c)),
2✔
1380
            [](int, int) -> int { throw std::runtime_error("test"); });
2✔
1381
    }
2✔
1382
    catch (hpx::exception_list const& e)
1383
    {
1384
        caught_bad_alloc = true;
2✔
1385
    }
2✔
1386
    catch (...)
1387
    {
1388
        HPX_TEST(false);
×
1389
    }
2✔
1390

1391
    HPX_TEST(caught_bad_alloc);
2✔
1392

1393
    caught_bad_alloc = false;
2✔
1394
    try
1395
    {
1396
        hpx::ranges::is_sorted_until(iterator(std::begin(c)),
4✔
1397
            iterator(std::end(c)), std::less<int>(),
2✔
1398
            [](int) -> int { throw std::runtime_error("test"); });
2✔
1399
    }
2✔
1400
    catch (hpx::exception_list const& e)
1401
    {
1402
        caught_bad_alloc = true;
2✔
1403
    }
2✔
1404
    catch (...)
1405
    {
1406
        HPX_TEST(false);
×
1407
    }
2✔
1408

1409
    HPX_TEST(caught_bad_alloc);
2✔
1410
}
8✔
1411

1412
template <typename ExPolicy>
1413
void test_sorted_until_bad_alloc(ExPolicy policy)
2✔
1414
{
1415
    static_assert(hpx::is_execution_policy<ExPolicy>::value,
1416
        "hpx::is_execution_policy<ExPolicy>::value");
1417

1418
    std::vector<int> c(10007);
2✔
1419
    //fill first half of array with even numbers and second half
1420
    //with odd numbers
1421
    std::iota(std::begin(c), std::end(c), 0);
2✔
1422

1423
    bool caught_bad_alloc = false;
2✔
1424
    try
1425
    {
1426
        hpx::ranges::is_sorted_until(policy, c,
2✔
1427
            [](int, int) -> bool { throw std::runtime_error("test"); });
5✔
1428
    }
2✔
1429
    catch (hpx::exception_list const& e)
1430
    {
1431
        caught_bad_alloc = true;
2✔
1432
    }
2✔
1433
    catch (...)
1434
    {
1435
        HPX_TEST(false);
×
1436
    }
2✔
1437

1438
    HPX_TEST(caught_bad_alloc);
2✔
1439

1440
    caught_bad_alloc = false;
2✔
1441
    try
1442
    {
1443
        hpx::ranges::is_sorted_until(policy, c, std::less<int>(),
2✔
1444
            [](int) -> int { throw std::runtime_error("test"); });
5✔
1445
    }
2✔
1446
    catch (hpx::exception_list const& e)
1447
    {
1448
        caught_bad_alloc = true;
2✔
1449
    }
2✔
1450
    catch (...)
1451
    {
1452
        HPX_TEST(false);
×
1453
    }
2✔
1454

1455
    HPX_TEST(caught_bad_alloc);
2✔
1456
}
6✔
1457

1458
template <typename ExPolicy>
1459
void test_sorted_until_async_bad_alloc(ExPolicy p)
2✔
1460
{
1461
    std::vector<int> c(10007);
2✔
1462
    std::iota(std::begin(c), std::end(c), 0);
2✔
1463

1464
    bool caught_bad_alloc = false;
2✔
1465
    try
1466
    {
1467
        auto f = hpx::ranges::is_sorted_until(
2✔
1468
            p, c, [](int, int) -> int { throw std::runtime_error("test"); });
5✔
1469
        f.get();
2✔
1470

1471
        HPX_TEST(false);
×
1472
    }
2✔
1473
    catch (hpx::exception_list const& e)
1474
    {
1475
        caught_bad_alloc = true;
2✔
1476
    }
2✔
1477
    catch (...)
1478
    {
1479
        HPX_TEST(false);
×
1480
    }
2✔
1481

1482
    HPX_TEST(caught_bad_alloc);
2✔
1483

1484
    caught_bad_alloc = false;
2✔
1485
    try
1486
    {
1487
        auto f = hpx::ranges::is_sorted_until(p, c, std::less<int>(),
2✔
1488
            [](int) -> bool { throw std::runtime_error("test"); });
5✔
1489
        f.get();
2✔
1490

1491
        HPX_TEST(false);
×
1492
    }
2✔
1493
    catch (hpx::exception_list const& e)
1494
    {
1495
        caught_bad_alloc = true;
2✔
1496
    }
2✔
1497
    catch (...)
1498
    {
1499
        HPX_TEST(false);
×
1500
    }
2✔
1501

1502
    HPX_TEST(caught_bad_alloc);
2✔
1503
}
6✔
1504

1505
void test_sorted_until_seq_bad_alloc()
1✔
1506
{
1507
    std::vector<int> c(10007);
1✔
1508
    //fill first half of array with even numbers and second half
1509
    //with odd numbers
1510
    std::iota(std::begin(c), std::end(c), 0);
1✔
1511

1512
    bool caught_bad_alloc = false;
1✔
1513
    try
1514
    {
1515
        hpx::ranges::is_sorted_until(
1✔
1516
            c, [](int, int) -> int { throw std::runtime_error("test"); });
1✔
1517
    }
1✔
1518
    catch (hpx::exception_list const& e)
1519
    {
1520
        caught_bad_alloc = true;
1✔
1521
    }
1✔
1522
    catch (...)
1523
    {
1524
        HPX_TEST(false);
×
1525
    }
1✔
1526

1527
    HPX_TEST(caught_bad_alloc);
1✔
1528

1529
    caught_bad_alloc = false;
1✔
1530
    try
1531
    {
1532
        hpx::ranges::is_sorted_until(c, std::less<int>(),
1✔
1533
            [](int) -> int { throw std::runtime_error("test"); });
1✔
1534
    }
1✔
1535
    catch (hpx::exception_list const& e)
1536
    {
1537
        caught_bad_alloc = true;
1✔
1538
    }
1✔
1539
    catch (...)
1540
    {
1541
        HPX_TEST(false);
×
1542
    }
1✔
1543

1544
    HPX_TEST(caught_bad_alloc);
1✔
1545
}
3✔
1546

1547
template <typename IteratorTag>
1548
void test_sorted_until_bad_alloc()
2✔
1549
{
1550
    using namespace hpx::execution;
1551

1552
    // If the execution policy object is of type vector_execution_policy,
1553
    // std::terminate shall be called. therefore we do not test exceptions
1554
    // with a vector execution policy
1555
    test_sorted_until_bad_alloc(par, IteratorTag());
2✔
1556
    test_sorted_until_bad_alloc(seq, IteratorTag());
2✔
1557

1558
    test_sorted_until_async_bad_alloc(seq(task), IteratorTag());
2✔
1559
    test_sorted_until_async_bad_alloc(par(task), IteratorTag());
2✔
1560

1561
    test_sorted_until_seq_bad_alloc(IteratorTag());
2✔
1562
}
2✔
1563

1564
void sorted_until_bad_alloc_test()
1✔
1565
{
1566
    test_sorted_until_bad_alloc<std::random_access_iterator_tag>();
1✔
1567
    test_sorted_until_bad_alloc<std::forward_iterator_tag>();
1✔
1568

1569
    using namespace hpx::execution;
1570

1571
    test_sorted_until_bad_alloc(par);
1✔
1572
    test_sorted_until_bad_alloc(seq);
1✔
1573

1574
    test_sorted_until_async_bad_alloc(seq(task));
1✔
1575
    test_sorted_until_async_bad_alloc(par(task));
1✔
1576

1577
    test_sorted_until_seq_bad_alloc();
1✔
1578
}
1✔
1579

1580
int hpx_main()
1✔
1581
{
1582
    sorted_until_test1();
1✔
1583
    sorted_until_test2();
1✔
1584
    sorted_until_test3();
1✔
1585
    sorted_until_exception_test();
1✔
1586
    sorted_until_bad_alloc_test();
1✔
1587

1588
    std::vector<int> c(100);
1✔
1589
    hpx::future<void> f =
1590
        hpx::dataflow(hpx::ranges::is_sorted, hpx::execution::par, c);
1✔
1591
    f = hpx::dataflow(
1✔
1592
        hpx::unwrapping(hpx::ranges::is_sorted), hpx::execution::par, c, f);
1✔
1593
    f.get();
1✔
1594

1595
    return hpx::local::finalize();
1✔
1596
}
1✔
1597

1598
int main(int argc, char* argv[])
1✔
1599
{
1600
    using namespace hpx::program_options;
1601
    options_description desc_commandline(
1✔
1602
        "Usage: " HPX_APPLICATION_STRING " [options]");
1✔
1603

1604
    std::vector<std::string> const cfg = {"hpx.os_threads=all"};
1✔
1605

1606
    hpx::local::init_params init_args;
1✔
1607
    init_args.desc_cmdline = desc_commandline;
1✔
1608
    init_args.cfg = cfg;
1✔
1609

1610
    HPX_TEST_EQ_MSG(hpx::local::init(hpx_main, argc, argv, init_args), 0,
1✔
1611
        "HPX main exited with non-zero status");
1612

1613
    return hpx::util::report_errors();
1✔
1614
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc