forked from ad-freiburg/qlever
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JThreadTest.cpp
59 lines (51 loc) · 1.64 KB
/
JThreadTest.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
// Copyright 2023, University of Freiburg,
// Chair of Algorithms and Data Structures.
// Author: Robin Textor-Falconi <[email protected]>
#include <gtest/gtest.h>
#include <atomic>
#include <thread>
#include "util/jthread.h"
using ad_utility::JThread;
TEST(JThread, ensureJoinOnDestruction) {
#ifdef __cpp_lib_jthread
GTEST_SKIP_("std::jthread does not require unit tests");
#endif
std::atomic_bool flag = false;
{
JThread thread{[&flag]() {
// Make sure the thread doesn't end before even attempting to join
std::this_thread::sleep_for(std::chrono::milliseconds{1});
flag = true;
}};
}
EXPECT_TRUE(flag);
}
// _____________________________________________________________________________
TEST(JThread, ensureDefaultConstructedObjectIsDestroyedNormally) {
#ifdef __cpp_lib_jthread
GTEST_SKIP_("std::jthread does not require unit tests");
#endif
JThread thread{};
}
// _____________________________________________________________________________
TEST(JThread, ensureCorrectMoveSemantics) {
#ifdef __cpp_lib_jthread
GTEST_SKIP_("std::jthread does not require unit tests");
#endif
std::atomic_bool flag1 = false;
std::atomic_bool flag2 = false;
{
JThread thread{[&flag1]() {
// Make sure the thread doesn't end before even attempting to join
std::this_thread::sleep_for(std::chrono::milliseconds{1});
flag1 = true;
}};
thread = JThread{[&flag2]() {
// Make sure the thread doesn't end before even attempting to join
std::this_thread::sleep_for(std::chrono::milliseconds{1});
flag2 = true;
}};
EXPECT_TRUE(flag1);
}
EXPECT_TRUE(flag2);
}