[CI][Github] Do not fail premerge job
[llvm-project.git] / libc / test / src / __support / CPP / mutex_test.cpp
bloba68c84cfc78af143ac6fd80d84f77f866bf6a420
1 //===-- Unittests for mutex -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/CPP/mutex.h"
10 #include "test/UnitTest/Test.h"
12 using LIBC_NAMESPACE::cpp::adopt_lock;
13 using LIBC_NAMESPACE::cpp::lock_guard;
15 // Simple struct for testing cpp::lock_guard. It defines methods 'lock' and
16 // 'unlock' which are required for the cpp::lock_guard class template.
17 struct Mutex {
18 // Flag to show whether this mutex is locked.
19 bool locked = false;
21 // Flag to show if this mutex has been double locked.
22 bool double_locked = false;
24 // Flag to show if this mutex has been double unlocked.
25 bool double_unlocked = false;
27 Mutex() {}
29 void lock() {
30 if (locked)
31 double_locked = true;
33 locked = true;
36 void unlock() {
37 if (!locked)
38 double_unlocked = true;
40 locked = false;
44 TEST(LlvmLibcMutexTest, Basic) {
45 Mutex m;
46 ASSERT_FALSE(m.locked);
47 ASSERT_FALSE(m.double_locked);
48 ASSERT_FALSE(m.double_unlocked);
51 lock_guard lg(m);
52 ASSERT_TRUE(m.locked);
53 ASSERT_FALSE(m.double_locked);
56 ASSERT_FALSE(m.locked);
57 ASSERT_FALSE(m.double_unlocked);
60 TEST(LlvmLibcMutexTest, AcquireLocked) {
61 Mutex m;
62 ASSERT_FALSE(m.locked);
63 ASSERT_FALSE(m.double_locked);
64 ASSERT_FALSE(m.double_unlocked);
66 // Lock the mutex before placing a lock guard on it.
67 m.lock();
68 ASSERT_TRUE(m.locked);
69 ASSERT_FALSE(m.double_locked);
72 lock_guard lg(m, adopt_lock);
73 ASSERT_TRUE(m.locked);
74 ASSERT_FALSE(m.double_locked);
77 ASSERT_FALSE(m.locked);
78 ASSERT_FALSE(m.double_unlocked);