-
Notifications
You must be signed in to change notification settings - Fork 0
/
35_tasks.cc
43 lines (33 loc) · 1.25 KB
/
35_tasks.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
#include <future>
#include <catch.hpp>
/******************************************************************************
*** Implementation ***********************************************************
******************************************************************************/
bool is_prime(uint64_t x) {
bool result = true;
for (uint64_t i = 2; i < x / 2; ++i) {
if (x % i == 0)
result = false;
}
return result;
}
/******************************************************************************
*** Test cases ***************************************************************
******************************************************************************/
TEST_CASE("task") {
static const uint64_t NUMBER = std::numeric_limits<uint32_t>::max() / 16;
SECTION("parallel tasks") {
auto f0 = std::async(std::launch::async, [] { return is_prime(NUMBER - 0); });
auto f1 = std::async(std::launch::async, [] { return is_prime(NUMBER - 1); });
auto f2 = std::async(std::launch::async, [] { return is_prime(NUMBER - 2); });
auto f3 = std::async(std::launch::async, [] { return is_prime(NUMBER - 3); });
f0.wait();
f1.wait();
f2.wait();
f3.wait();
CHECK_FALSE(f0.get());
CHECK_FALSE(f1.get());
CHECK_FALSE(f2.get());
CHECK_FALSE(f3.get());
}
}