1 //===-- list_test.cpp -------------------------------------------*- C++ -*-===//
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 #include "tests/scudo_unit_test.h"
18 static ListItem Items
[6];
19 static ListItem
*X
= &Items
[0];
20 static ListItem
*Y
= &Items
[1];
21 static ListItem
*Z
= &Items
[2];
22 static ListItem
*A
= &Items
[3];
23 static ListItem
*B
= &Items
[4];
24 static ListItem
*C
= &Items
[5];
26 typedef scudo::SinglyLinkedList
<ListItem
> SLList
;
27 typedef scudo::DoublyLinkedList
<ListItem
> DLList
;
29 template <typename ListT
>
30 static void setList(ListT
*L
, ListItem
*I1
= nullptr, ListItem
*I2
= nullptr,
31 ListItem
*I3
= nullptr) {
41 template <typename ListT
>
42 static void checkList(ListT
*L
, ListItem
*I1
, ListItem
*I2
= nullptr,
43 ListItem
*I3
= nullptr, ListItem
*I4
= nullptr,
44 ListItem
*I5
= nullptr, ListItem
*I6
= nullptr) {
46 EXPECT_EQ(L
->front(), I1
);
50 EXPECT_EQ(L
->front(), I2
);
54 EXPECT_EQ(L
->front(), I3
);
58 EXPECT_EQ(L
->front(), I4
);
62 EXPECT_EQ(L
->front(), I5
);
66 EXPECT_EQ(L
->front(), I6
);
69 EXPECT_TRUE(L
->empty());
72 template <typename ListT
> static void testListCommon(void) {
76 EXPECT_EQ(L
.size(), 0U);
78 EXPECT_EQ(L
.size(), 1U);
79 EXPECT_EQ(L
.back(), X
);
80 EXPECT_EQ(L
.front(), X
);
82 EXPECT_TRUE(L
.empty());
86 EXPECT_EQ(L
.size(), 1U);
87 EXPECT_EQ(L
.back(), X
);
88 EXPECT_EQ(L
.front(), X
);
90 EXPECT_TRUE(L
.empty());
96 EXPECT_EQ(L
.size(), 3U);
97 EXPECT_EQ(L
.front(), Z
);
98 EXPECT_EQ(L
.back(), X
);
102 EXPECT_EQ(L
.size(), 2U);
103 EXPECT_EQ(L
.front(), Y
);
104 EXPECT_EQ(L
.back(), X
);
107 EXPECT_TRUE(L
.empty());
108 L
.checkConsistency();
113 EXPECT_EQ(L
.size(), 3U);
114 EXPECT_EQ(L
.front(), X
);
115 EXPECT_EQ(L
.back(), Z
);
116 L
.checkConsistency();
119 EXPECT_EQ(L
.size(), 2U);
120 EXPECT_EQ(L
.front(), Y
);
121 EXPECT_EQ(L
.back(), Z
);
124 EXPECT_TRUE(L
.empty());
125 L
.checkConsistency();
128 TEST(ScudoListTest
, LinkedListCommon
) {
129 testListCommon
<SLList
>();
130 testListCommon
<DLList
>();
133 TEST(ScudoListTest
, SinglyLinkedList
) {
141 EXPECT_EQ(L
.size(), 2U);
142 EXPECT_EQ(L
.front(), X
);
143 EXPECT_EQ(L
.back(), Z
);
144 L
.checkConsistency();
146 EXPECT_EQ(L
.size(), 1U);
147 EXPECT_EQ(L
.front(), X
);
148 EXPECT_EQ(L
.back(), X
);
149 L
.checkConsistency();
151 EXPECT_TRUE(L
.empty());
158 EXPECT_TRUE(L1
.empty());
159 EXPECT_TRUE(L2
.empty());
166 checkList(&L1
, X
, Z
, Y
);
168 setList(&L1
, X
, Y
, Z
);
169 setList(&L2
, A
, B
, C
);
171 checkList(&L1
, X
, Y
, Z
, A
, B
, C
);
172 EXPECT_TRUE(L2
.empty());
178 EXPECT_EQ(L1
.back(), X
);
179 EXPECT_EQ(L1
.front(), X
);
180 EXPECT_EQ(L1
.size(), 1U);
183 TEST(ScudoListTest
, DoublyLinkedList
) {
191 EXPECT_EQ(L
.size(), 2U);
192 EXPECT_EQ(L
.front(), X
);
193 EXPECT_EQ(L
.back(), Z
);
194 L
.checkConsistency();
196 EXPECT_EQ(L
.size(), 1U);
197 EXPECT_EQ(L
.front(), X
);
198 EXPECT_EQ(L
.back(), X
);
199 L
.checkConsistency();
201 EXPECT_TRUE(L
.empty());
205 EXPECT_EQ(L
.size(), 2U);
206 EXPECT_EQ(L
.front(), Y
);
207 EXPECT_EQ(L
.back(), X
);
208 L
.checkConsistency();
210 EXPECT_EQ(L
.size(), 1U);
211 EXPECT_EQ(L
.front(), X
);
212 EXPECT_EQ(L
.back(), X
);
213 L
.checkConsistency();
215 EXPECT_TRUE(L
.empty());