Skip to content

Commit c25ef24

Browse files
committed
[libc++] Refactor the std::unique_lock tests
1 parent 1fb1a5d commit c25ef24

26 files changed

+384
-580
lines changed

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/implicit_ctad.pass.cpp renamed to libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/implicit_ctad.compile.pass.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,7 @@
1616

1717
#include <mutex>
1818

19-
#include "test_macros.h"
20-
#include "types.h"
19+
#include "checking_mutex.h"
2120

22-
int main(int, char**) {
23-
MyMutex mutex;
24-
{
25-
std::unique_lock lock(mutex);
26-
ASSERT_SAME_TYPE(decltype(lock), std::unique_lock<MyMutex>);
27-
}
28-
29-
return 0;
30-
}
21+
checking_mutex mux;
22+
static_assert(std::is_same_v<std::unique_lock<checking_mutex>, decltype(std::unique_lock{mux})>);

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_assign.compile.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
#include <mutex>
1616

17-
#include "../types.h"
17+
#include "checking_mutex.h"
1818

19-
static_assert(!std::is_copy_assignable<std::lock_guard<MyMutex> >::value, "");
19+
static_assert(!std::is_copy_assignable<std::lock_guard<checking_mutex> >::value, "");

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/copy_ctor.compile.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
#include <mutex>
1616

17-
#include "../types.h"
17+
#include "checking_mutex.h"
1818

19-
static_assert(!std::is_copy_constructible<std::lock_guard<MyMutex> >::value, "");
19+
static_assert(!std::is_copy_constructible<std::lock_guard<checking_mutex> >::value, "");

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/default.pass.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
#include <cassert>
1616
#include <mutex>
1717

18-
#include "test_macros.h"
19-
#include "../types.h"
18+
#include "checking_mutex.h"
2019

2120
int main(int, char**) {
22-
std::unique_lock<MyMutex> ul;
21+
std::unique_lock<checking_mutex> ul;
2322
assert(!ul.owns_lock());
2423
assert(ul.mutex() == nullptr);
2524

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_assign.pass.cpp

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,20 @@
1515
#include <cassert>
1616
#include <mutex>
1717

18-
#include "nasty_containers.h"
19-
#include "../types.h"
20-
#include "test_macros.h"
18+
#include "checking_mutex.h"
2119

2220
int main(int, char**) {
23-
{
24-
typedef MyMutex M;
25-
M m0;
26-
M m1;
27-
std::unique_lock<M> lk0(m0);
28-
std::unique_lock<M> lk1(m1);
29-
lk1 = std::move(lk0);
30-
assert(lk1.mutex() == std::addressof(m0));
31-
assert(lk1.owns_lock() == true);
32-
assert(lk0.mutex() == nullptr);
33-
assert(lk0.owns_lock() == false);
34-
}
35-
{
36-
typedef nasty_mutex M;
37-
M m0;
38-
M m1;
39-
std::unique_lock<M> lk0(m0);
40-
std::unique_lock<M> lk1(m1);
41-
lk1 = std::move(lk0);
42-
assert(lk1.mutex() == std::addressof(m0));
43-
assert(lk1.owns_lock() == true);
44-
assert(lk0.mutex() == nullptr);
45-
assert(lk0.owns_lock() == false);
46-
}
21+
checking_mutex m0;
22+
checking_mutex m1;
23+
std::unique_lock<checking_mutex> lk0(m0);
24+
std::unique_lock<checking_mutex> lk1(m1);
25+
26+
lk1 = std::move(lk0);
27+
28+
assert(lk1.mutex() == std::addressof(m0));
29+
assert(lk1.owns_lock());
30+
assert(lk0.mutex() == nullptr);
31+
assert(!lk0.owns_lock());
4732

4833
return 0;
4934
}

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_ctor.pass.cpp

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
//
9-
// UNSUPPORTED: c++03
108

119
// <mutex>
1210

@@ -17,31 +15,17 @@
1715
#include <cassert>
1816
#include <mutex>
1917

20-
#include "nasty_containers.h"
21-
#include "../types.h"
22-
#include "test_macros.h"
18+
#include "checking_mutex.h"
2319

2420
int main(int, char**) {
25-
{
26-
typedef MyMutex M;
27-
M m;
28-
std::unique_lock<M> lk0(m);
29-
std::unique_lock<M> lk = std::move(lk0);
30-
assert(lk.mutex() == std::addressof(m));
31-
assert(lk.owns_lock() == true);
32-
assert(lk0.mutex() == nullptr);
33-
assert(lk0.owns_lock() == false);
34-
}
35-
{
36-
typedef nasty_mutex M;
37-
M m;
38-
std::unique_lock<M> lk0(m);
39-
std::unique_lock<M> lk = std::move(lk0);
40-
assert(lk.mutex() == std::addressof(m));
41-
assert(lk.owns_lock() == true);
42-
assert(lk0.mutex() == nullptr);
43-
assert(lk0.owns_lock() == false);
44-
}
21+
checking_mutex m;
22+
std::unique_lock<checking_mutex> lk0(m);
23+
std::unique_lock<checking_mutex> lk = std::move(lk0);
24+
25+
assert(lk.mutex() == std::addressof(m));
26+
assert(lk.owns_lock());
27+
assert(lk0.mutex() == nullptr);
28+
assert(!lk0.owns_lock());
4529

4630
return 0;
4731
}

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex.pass.cpp

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
//
9-
// UNSUPPORTED: no-threads
10-
// ALLOW_RETRIES: 2
118

129
// <mutex>
1310

@@ -19,45 +16,22 @@
1916
// -> unique_lock<_Mutex>; // C++17
2017

2118
#include <cassert>
22-
#include <chrono>
23-
#include <cstdlib>
2419
#include <mutex>
25-
#include <thread>
2620

27-
#include "make_test_thread.h"
21+
#include "checking_mutex.h"
2822
#include "test_macros.h"
2923

30-
std::mutex m;
31-
32-
typedef std::chrono::system_clock Clock;
33-
typedef Clock::time_point time_point;
34-
typedef Clock::duration duration;
35-
typedef std::chrono::milliseconds ms;
36-
typedef std::chrono::nanoseconds ns;
37-
38-
void f()
39-
{
40-
time_point t0 = Clock::now();
41-
time_point t1;
42-
{
43-
std::unique_lock<std::mutex> ul(m);
44-
t1 = Clock::now();
45-
}
46-
ns d = t1 - t0 - ms(250);
47-
assert(d < ms(50)); // within 50ms
48-
}
24+
int main(int, char**) {
25+
checking_mutex mux;
4926

50-
int main(int, char**)
51-
{
52-
m.lock();
53-
std::thread t = support::make_test_thread(f);
54-
std::this_thread::sleep_for(ms(250));
55-
m.unlock();
56-
t.join();
27+
{
28+
std::unique_lock<checking_mutex> lock(mux);
29+
assert(mux.current_state == checking_mutex::locked_via_lock);
30+
}
31+
assert(mux.current_state == checking_mutex::unlocked);
5732

5833
#if TEST_STD_VER >= 17
59-
std::unique_lock ul(m);
60-
static_assert((std::is_same<decltype(ul), std::unique_lock<decltype(m)>>::value), "" );
34+
static_assert(std::is_same_v<std::unique_lock<checking_mutex>, decltype(std::unique_lock{mux})>, "");
6135
#endif
6236

6337
return 0;

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_adopt_lock.pass.cpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
//
9-
// UNSUPPORTED: c++03
108

119
// <mutex>
1210

@@ -17,27 +15,16 @@
1715
#include <cassert>
1816
#include <mutex>
1917

20-
#include "nasty_containers.h"
21-
#include "../types.h"
22-
#include "test_macros.h"
18+
#include "checking_mutex.h"
2319

2420
int main(int, char**) {
25-
{
26-
typedef MyMutex M;
27-
M m;
28-
m.lock();
29-
std::unique_lock<M> lk(m, std::adopt_lock);
30-
assert(lk.mutex() == std::addressof(m));
31-
assert(lk.owns_lock() == true);
32-
}
33-
{
34-
typedef nasty_mutex M;
35-
M m;
36-
m.lock();
37-
std::unique_lock<M> lk(m, std::adopt_lock);
38-
assert(lk.mutex() == std::addressof(m));
39-
assert(lk.owns_lock() == true);
40-
}
21+
checking_mutex m;
22+
m.lock();
23+
m.last_try = checking_mutex::none;
24+
std::unique_lock<checking_mutex> lk(m, std::adopt_lock_t());
25+
assert(m.last_try == checking_mutex::none);
26+
assert(lk.mutex() == std::addressof(m));
27+
assert(lk.owns_lock());
4128

4229
return 0;
4330
}

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_defer_lock.pass.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
//
9-
// UNSUPPORTED: c++03
108

119
// <mutex>
1210

@@ -17,25 +15,14 @@
1715
#include <cassert>
1816
#include <mutex>
1917

20-
#include "nasty_containers.h"
21-
#include "../types.h"
22-
#include "test_macros.h"
18+
#include "checking_mutex.h"
2319

2420
int main(int, char**) {
25-
{
26-
typedef MyMutex M;
27-
M m;
28-
std::unique_lock<M> lk(m, std::defer_lock);
29-
assert(lk.mutex() == std::addressof(m));
30-
assert(lk.owns_lock() == false);
31-
}
32-
{
33-
typedef nasty_mutex M;
34-
M m;
35-
std::unique_lock<M> lk(m, std::defer_lock);
36-
assert(lk.mutex() == std::addressof(m));
37-
assert(lk.owns_lock() == false);
38-
}
21+
checking_mutex m;
22+
std::unique_lock<checking_mutex> lk(m, std::defer_lock_t());
23+
assert(m.last_try == checking_mutex::none);
24+
assert(lk.mutex() == std::addressof(m));
25+
assert(lk.owns_lock() == false);
3926

4027
return 0;
4128
}

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_duration.pass.cpp

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,69 +5,36 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
//
9-
// UNSUPPORTED: no-threads
10-
// ALLOW_RETRIES: 2
118

129
// <mutex>
1310

14-
// class timed_mutex;
15-
1611
// template <class Rep, class Period>
17-
// unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
12+
// unique_lock::unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
1813

19-
#include <mutex>
20-
#include <thread>
21-
#include <cstdlib>
2214
#include <cassert>
15+
#include <chrono>
16+
#include <mutex>
2317

24-
#include "make_test_thread.h"
25-
#include "test_macros.h"
26-
27-
std::timed_mutex m;
28-
29-
typedef std::chrono::steady_clock Clock;
30-
typedef Clock::time_point time_point;
31-
typedef Clock::duration duration;
32-
typedef std::chrono::milliseconds ms;
33-
typedef std::chrono::nanoseconds ns;
34-
35-
void f1()
36-
{
37-
time_point t0 = Clock::now();
38-
std::unique_lock<std::timed_mutex> lk(m, ms(300));
39-
assert(lk.owns_lock() == true);
40-
time_point t1 = Clock::now();
41-
ns d = t1 - t0 - ms(250);
42-
assert(d < ms(50)); // within 50ms
43-
}
44-
45-
void f2()
46-
{
47-
time_point t0 = Clock::now();
48-
std::unique_lock<std::timed_mutex> lk(m, ms(250));
49-
assert(lk.owns_lock() == false);
50-
time_point t1 = Clock::now();
51-
ns d = t1 - t0 - ms(250);
52-
assert(d < ms(50)); // within 50ms
53-
}
54-
55-
int main(int, char**)
56-
{
57-
{
58-
m.lock();
59-
std::thread t = support::make_test_thread(f1);
60-
std::this_thread::sleep_for(ms(250));
61-
m.unlock();
62-
t.join();
63-
}
64-
{
65-
m.lock();
66-
std::thread t = support::make_test_thread(f2);
67-
std::this_thread::sleep_for(ms(300));
68-
m.unlock();
69-
t.join();
70-
}
18+
#include "checking_mutex.h"
19+
20+
int main(int, char**) {
21+
checking_mutex mux;
22+
{ // check successful lock
23+
mux.reject = false;
24+
std::unique_lock<checking_mutex> lock(mux, std::chrono::seconds());
25+
assert(mux.current_state == checking_mutex::locked_via_try_lock_for);
26+
assert(lock.owns_lock());
27+
}
28+
assert(mux.current_state == checking_mutex::unlocked);
29+
30+
{ // check unsuccessful lock
31+
mux.reject = true;
32+
std::unique_lock<checking_mutex> lock(mux, std::chrono::seconds());
33+
assert(mux.current_state == checking_mutex::unlocked);
34+
assert(mux.last_try == checking_mutex::locked_via_try_lock_for);
35+
assert(!lock.owns_lock());
36+
}
37+
assert(mux.current_state == checking_mutex::unlocked);
7138

7239
return 0;
7340
}

0 commit comments

Comments
 (0)