1 //===-- tsan_mutexset_test.cpp --------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is a part of ThreadSanitizer (TSan), a race detector.
11 //===----------------------------------------------------------------------===//
12 #include "tsan_mutexset.h"
13 #include "gtest/gtest.h"
17 static void Expect(const MutexSet
&mset
, uptr i
, u64 id
, bool write
, u64 epoch
,
19 MutexSet::Desc d
= mset
.Get(i
);
21 EXPECT_EQ(write
, d
.write
);
22 EXPECT_EQ(epoch
, d
.epoch
);
23 EXPECT_EQ(count
, d
.count
);
26 TEST(MutexSet
, Basic
) {
28 EXPECT_EQ(mset
.Size(), (uptr
)0);
31 EXPECT_EQ(mset
.Size(), (uptr
)1);
32 Expect(mset
, 0, 1, true, 2, 1);
34 EXPECT_EQ(mset
.Size(), (uptr
)0);
37 mset
.Add(5, false, 6);
38 EXPECT_EQ(mset
.Size(), (uptr
)2);
39 Expect(mset
, 0, 3, true, 4, 1);
40 Expect(mset
, 1, 5, false, 6, 1);
42 EXPECT_EQ(mset
.Size(), (uptr
)1);
44 EXPECT_EQ(mset
.Size(), (uptr
)0);
47 TEST(MutexSet
, DoubleAdd
) {
50 EXPECT_EQ(mset
.Size(), (uptr
)1);
51 Expect(mset
, 0, 1, true, 2, 1);
54 EXPECT_EQ(mset
.Size(), (uptr
)1);
55 Expect(mset
, 0, 1, true, 2, 2);
58 EXPECT_EQ(mset
.Size(), (uptr
)1);
59 Expect(mset
, 0, 1, true, 2, 1);
62 EXPECT_EQ(mset
.Size(), (uptr
)0);
65 TEST(MutexSet
, DoubleDel
) {
68 EXPECT_EQ(mset
.Size(), (uptr
)1);
70 EXPECT_EQ(mset
.Size(), (uptr
)0);
72 EXPECT_EQ(mset
.Size(), (uptr
)0);
75 TEST(MutexSet
, Remove
) {
81 EXPECT_EQ(mset
.Size(), (uptr
)2);
84 EXPECT_EQ(mset
.Size(), (uptr
)1);
85 Expect(mset
, 0, 3, true, 4, 2);
88 TEST(MutexSet
, Full
) {
90 for (uptr i
= 0; i
< MutexSet::kMaxSize
; i
++) {
91 mset
.Add(i
, true, i
+ 1);
93 EXPECT_EQ(mset
.Size(), MutexSet::kMaxSize
);
94 for (uptr i
= 0; i
< MutexSet::kMaxSize
; i
++) {
95 Expect(mset
, i
, i
, true, i
+ 1, 1);
98 for (uptr i
= 0; i
< MutexSet::kMaxSize
; i
++) {
99 mset
.Add(i
, true, i
+ 1);
101 EXPECT_EQ(mset
.Size(), MutexSet::kMaxSize
);
102 for (uptr i
= 0; i
< MutexSet::kMaxSize
; i
++) {
103 Expect(mset
, i
, i
, true, i
+ 1, 2);
107 TEST(MutexSet
, Overflow
) {
109 for (uptr i
= 0; i
< MutexSet::kMaxSize
; i
++) {
110 mset
.Add(i
, true, i
+ 1);
111 mset
.Add(i
, true, i
+ 1);
113 mset
.Add(100, true, 200);
114 EXPECT_EQ(mset
.Size(), MutexSet::kMaxSize
);
115 for (uptr i
= 0; i
< MutexSet::kMaxSize
; i
++) {
117 Expect(mset
, i
, MutexSet::kMaxSize
- 1,
118 true, MutexSet::kMaxSize
, 2);
119 else if (i
== MutexSet::kMaxSize
- 1)
120 Expect(mset
, i
, 100, true, 200, 1);
122 Expect(mset
, i
, i
, true, i
+ 1, 2);
126 } // namespace __tsan