Debian Codegraph @

unittest/gunit/mutex_lock-t.cc

raw symbols SCIP
1 /* Copyright (c) 2021, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <gtest/gtest.h>
25 #include "include/mutex_lock.h"
26
27 namespace {
28 struct FakeMutex {
29 int times_locked = 0;
30 int times_unlocked = 0;
31 };
32 void inline_mysql_mutex_lock(FakeMutex *mm, const char *, int) {
33 ASSERT_TRUE(mm->times_locked == mm->times_unlocked);
34 ++mm->times_locked;
35 }
36 void inline_mysql_mutex_unlock(FakeMutex *mm, const char *, int) {
37 ASSERT_TRUE(mm->times_locked == mm->times_unlocked + 1);
38 ++mm->times_unlocked;
39 }
40 using FakeMutexLock = Generic_mutex_lock<FakeMutex>;
41 } // namespace
42
43 namespace mutex_lock_unittest {
44 TEST(MutexLockTest, DefaultConstruct) {
45 { FakeMutexLock _; }
46 }
47 TEST(MutexLockTest, Plain) {
48 FakeMutex mm;
49 {
50 FakeMutexLock _{&mm, __FILE__, __LINE__};
51 EXPECT_EQ(1, mm.times_locked);
52 EXPECT_EQ(0, mm.times_unlocked);
53 }
54 EXPECT_EQ(1, mm.times_locked);
55 EXPECT_EQ(1, mm.times_unlocked);
56 }
57
58 TEST(MutexLockTest, MoveAssign) {
59 FakeMutex mm;
60 {
61 FakeMutexLock _;
62 {
63 _ = FakeMutexLock{&mm, __FILE__, __LINE__};
64 EXPECT_EQ(1, mm.times_locked);
65 EXPECT_EQ(0, mm.times_unlocked);
66 }
67 // Still locked, as we move-assigned to _ which is still in scope
68 EXPECT_EQ(1, mm.times_locked);
69 EXPECT_EQ(0, mm.times_unlocked);
70 }
71 EXPECT_EQ(1, mm.times_locked);
72 EXPECT_EQ(1, mm.times_unlocked);
73 }
74
75 TEST(MutexLockTest, StdMoveAssign) {
76 FakeMutex mm;
77 {
78 FakeMutexLock _;
79 {
80 FakeMutexLock local{&mm, __FILE__, __LINE__};
81 EXPECT_EQ(1, mm.times_locked);
82 EXPECT_EQ(0, mm.times_unlocked);
83 _ = std::move(local);
84 }
85 // Still locked, as we move-assigned to _ which is still in scope
86 EXPECT_EQ(1, mm.times_locked);
87 EXPECT_EQ(0, mm.times_unlocked);
88 }
89 EXPECT_EQ(1, mm.times_locked);
90 EXPECT_EQ(1, mm.times_unlocked);
91 }
92
93 TEST(MutexLockTest, MoveConstruct) {
94 FakeMutex mm;
95 {
96 FakeMutexLock src = FakeMutexLock(nullptr, __FILE__, __LINE__);
97 FakeMutexLock dst{std::move(src)};
98 EXPECT_EQ(0, mm.times_locked);
99 EXPECT_EQ(0, mm.times_unlocked);
100 }
101 {
102 FakeMutexLock src = FakeMutexLock(&mm, __FILE__, __LINE__);
103 FakeMutexLock dst{std::move(src)};
104 EXPECT_EQ(1, mm.times_locked);
105 EXPECT_EQ(0, mm.times_unlocked);
106 }
107 EXPECT_EQ(1, mm.times_locked);
108 EXPECT_EQ(1, mm.times_unlocked);
109 }
110
111 } // namespace mutex_lock_unittest