Skip to content

Commit 3a5316a

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

26 files changed

+392
-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: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,23 @@
1313
// unique_lock& operator=(unique_lock&& u);
1414

1515
#include <cassert>
16+
#include <memory>
1617
#include <mutex>
1718

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

2221
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-
}
22+
checking_mutex m0;
23+
checking_mutex m1;
24+
std::unique_lock<checking_mutex> lk0(m0);
25+
std::unique_lock<checking_mutex> lk1(m1);
26+
27+
lk1 = std::move(lk0);
28+
29+
assert(lk1.mutex() == std::addressof(m0));
30+
assert(lk1.owns_lock());
31+
assert(lk0.mutex() == nullptr);
32+
assert(!lk0.owns_lock());
4733

4834
return 0;
4935
}

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

Lines changed: 10 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

@@ -15,33 +13,20 @@
1513
// unique_lock(unique_lock&& u);
1614

1715
#include <cassert>
16+
#include <memory>
1817
#include <mutex>
1918

20-
#include "nasty_containers.h"
21-
#include "../types.h"
22-
#include "test_macros.h"
19+
#include "checking_mutex.h"
2320

2421
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-
}
22+
checking_mutex m;
23+
std::unique_lock<checking_mutex> lk0(m);
24+
std::unique_lock<checking_mutex> lk = std::move(lk0);
25+
26+
assert(lk.mutex() == std::addressof(m));
27+
assert(lk.owns_lock());
28+
assert(lk0.mutex() == nullptr);
29+
assert(!lk0.owns_lock());
4530

4631
return 0;
4732
}

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: 9 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

@@ -15,29 +13,19 @@
1513
// unique_lock(mutex_type& m, adopt_lock_t);
1614

1715
#include <cassert>
16+
#include <memory>
1817
#include <mutex>
1918

20-
#include "nasty_containers.h"
21-
#include "../types.h"
22-
#include "test_macros.h"
19+
#include "checking_mutex.h"
2320

2421
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-
}
22+
checking_mutex m;
23+
m.lock();
24+
m.last_try = checking_mutex::none;
25+
std::unique_lock<checking_mutex> lk(m, std::adopt_lock_t());
26+
assert(m.last_try == checking_mutex::none);
27+
assert(lk.mutex() == std::addressof(m));
28+
assert(lk.owns_lock());
4129

4230
return 0;
4331
}

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

Lines changed: 7 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

@@ -15,27 +13,17 @@
1513
// unique_lock(mutex_type& m, defer_lock_t);
1614

1715
#include <cassert>
16+
#include <memory>
1817
#include <mutex>
1918

20-
#include "nasty_containers.h"
21-
#include "../types.h"
22-
#include "test_macros.h"
19+
#include "checking_mutex.h"
2320

2421
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-
}
22+
checking_mutex m;
23+
std::unique_lock<checking_mutex> lk(m, std::defer_lock_t());
24+
assert(m.last_try == checking_mutex::none);
25+
assert(lk.mutex() == std::addressof(m));
26+
assert(lk.owns_lock() == false);
3927

4028
return 0;
4129
}

0 commit comments

Comments
 (0)