1 //===-- sanitizer_list_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/AddressSanitizer runtime.
11 //===----------------------------------------------------------------------===//
12 #include "sanitizer_common/sanitizer_list.h"
13 #include "gtest/gtest.h"
15 namespace __sanitizer
{
21 typedef IntrusiveList
<ListItem
> List
;
23 static List static_list
;
25 static void SetList(List
*l
, ListItem
*x
= 0,
26 ListItem
*y
= 0, ListItem
*z
= 0) {
28 if (x
) l
->push_back(x
);
29 if (y
) l
->push_back(y
);
30 if (z
) l
->push_back(z
);
33 static void CheckList(List
*l
, ListItem
*i1
, ListItem
*i2
= 0, ListItem
*i3
= 0,
34 ListItem
*i4
= 0, ListItem
*i5
= 0, ListItem
*i6
= 0) {
36 CHECK_EQ(l
->front(), i1
);
40 CHECK_EQ(l
->front(), i2
);
44 CHECK_EQ(l
->front(), i3
);
48 CHECK_EQ(l
->front(), i4
);
52 CHECK_EQ(l
->front(), i5
);
56 CHECK_EQ(l
->front(), i6
);
62 TEST(SanitizerCommon
, IntrusiveList
) {
64 CHECK_EQ(static_list
.size(), 0);
69 ListItem
*x
= &items
[0];
70 ListItem
*y
= &items
[1];
71 ListItem
*z
= &items
[2];
72 ListItem
*a
= &items
[3];
73 ListItem
*b
= &items
[4];
74 ListItem
*c
= &items
[5];
76 CHECK_EQ(l
.size(), 0);
78 CHECK_EQ(l
.size(), 1);
79 CHECK_EQ(l
.back(), x
);
80 CHECK_EQ(l
.front(), x
);
86 CHECK_EQ(l
.size(), 1);
87 CHECK_EQ(l
.back(), x
);
88 CHECK_EQ(l
.front(), x
);
96 CHECK_EQ(l
.size(), 3);
97 CHECK_EQ(l
.front(), z
);
98 CHECK_EQ(l
.back(), x
);
102 CHECK_EQ(l
.size(), 2);
103 CHECK_EQ(l
.front(), y
);
104 CHECK_EQ(l
.back(), x
);
108 l
.CheckConsistency();
113 CHECK_EQ(l
.size(), 3);
114 CHECK_EQ(l
.front(), x
);
115 CHECK_EQ(l
.back(), z
);
116 l
.CheckConsistency();
119 CHECK_EQ(l
.size(), 2);
120 CHECK_EQ(l
.front(), y
);
121 CHECK_EQ(l
.back(), z
);
125 l
.CheckConsistency();
131 CHECK_EQ(l
.size(), 2);
132 CHECK_EQ(l
.front(), x
);
133 CHECK_EQ(l
.back(), z
);
134 l
.CheckConsistency();
136 CHECK_EQ(l
.size(), 1);
137 CHECK_EQ(l
.front(), x
);
138 CHECK_EQ(l
.back(), x
);
139 l
.CheckConsistency();
147 l1
.append_front(&l2
);
158 SetList(&l1
, x
, y
, z
);
159 SetList(&l2
, a
, b
, c
);
161 CheckList(&l1
, x
, y
, z
, a
, b
, c
);
166 l1
.append_front(&l2
);
167 CheckList(&l1
, x
, y
);
171 TEST(SanitizerCommon
, IntrusiveListAppendEmpty
) {
179 CHECK_EQ(l
.back(), &i
);
180 CHECK_EQ(l
.front(), &i
);
181 CHECK_EQ(l
.size(), 1);
183 CHECK_EQ(l
.back(), &i
);
184 CHECK_EQ(l
.front(), &i
);
185 CHECK_EQ(l
.size(), 1);
188 } // namespace __sanitizer