1 //===- unittests/ADT/IListNodeBaseTest.cpp - ilist_node_base unit tests ---===//
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 "llvm/ADT/ilist_node_base.h"
10 #include "gtest/gtest.h"
12 #include <type_traits>
20 typedef ilist_node_base
<false, void> RawNode
;
21 typedef ilist_node_base
<true, void> TrackingNode
;
22 typedef ilist_node_base
<false, Parent
> ParentNode
;
23 typedef ilist_node_base
<true, Parent
> ParentTrackingNode
;
25 TEST(IListNodeBaseTest
, DefaultConstructor
) {
27 EXPECT_EQ(nullptr, A
.getPrev());
28 EXPECT_EQ(nullptr, A
.getNext());
29 EXPECT_FALSE(A
.isKnownSentinel());
32 EXPECT_EQ(nullptr, TA
.getPrev());
33 EXPECT_EQ(nullptr, TA
.getNext());
34 EXPECT_FALSE(TA
.isKnownSentinel());
35 EXPECT_FALSE(TA
.isSentinel());
38 EXPECT_EQ(nullptr, PA
.getPrev());
39 EXPECT_EQ(nullptr, PA
.getNext());
40 EXPECT_EQ(nullptr, PA
.getNodeBaseParent());
41 EXPECT_FALSE(PA
.isKnownSentinel());
43 ParentTrackingNode PTA
;
44 EXPECT_EQ(nullptr, PTA
.getPrev());
45 EXPECT_EQ(nullptr, PTA
.getNext());
46 EXPECT_EQ(nullptr, PTA
.getNodeBaseParent());
47 EXPECT_FALSE(PTA
.isKnownSentinel());
48 EXPECT_FALSE(PTA
.isSentinel());
51 TEST(IListNodeBaseTest
, setPrevAndNext
) {
54 EXPECT_EQ(&B
, A
.getPrev());
55 EXPECT_EQ(nullptr, A
.getNext());
56 EXPECT_EQ(nullptr, B
.getPrev());
57 EXPECT_EQ(nullptr, B
.getNext());
58 EXPECT_EQ(nullptr, C
.getPrev());
59 EXPECT_EQ(nullptr, C
.getNext());
62 EXPECT_EQ(&B
, A
.getPrev());
63 EXPECT_EQ(&C
, A
.getNext());
64 EXPECT_EQ(nullptr, B
.getPrev());
65 EXPECT_EQ(nullptr, B
.getNext());
66 EXPECT_EQ(nullptr, C
.getPrev());
67 EXPECT_EQ(nullptr, C
.getNext());
69 TrackingNode TA
, TB
, TC
;
71 EXPECT_EQ(&TB
, TA
.getPrev());
72 EXPECT_EQ(nullptr, TA
.getNext());
73 EXPECT_EQ(nullptr, TB
.getPrev());
74 EXPECT_EQ(nullptr, TB
.getNext());
75 EXPECT_EQ(nullptr, TC
.getPrev());
76 EXPECT_EQ(nullptr, TC
.getNext());
79 EXPECT_EQ(&TB
, TA
.getPrev());
80 EXPECT_EQ(&TC
, TA
.getNext());
81 EXPECT_EQ(nullptr, TB
.getPrev());
82 EXPECT_EQ(nullptr, TB
.getNext());
83 EXPECT_EQ(nullptr, TC
.getPrev());
84 EXPECT_EQ(nullptr, TC
.getNext());
86 ParentNode PA
, PB
, PC
;
88 EXPECT_EQ(&PB
, PA
.getPrev());
89 EXPECT_EQ(nullptr, PA
.getNext());
90 EXPECT_EQ(nullptr, PB
.getPrev());
91 EXPECT_EQ(nullptr, PB
.getNext());
92 EXPECT_EQ(nullptr, PC
.getPrev());
93 EXPECT_EQ(nullptr, PC
.getNext());
96 EXPECT_EQ(&PB
, PA
.getPrev());
97 EXPECT_EQ(&PC
, PA
.getNext());
98 EXPECT_EQ(nullptr, PB
.getPrev());
99 EXPECT_EQ(nullptr, PB
.getNext());
100 EXPECT_EQ(nullptr, PC
.getPrev());
101 EXPECT_EQ(nullptr, PC
.getNext());
103 ParentTrackingNode PTA
, PTB
, PTC
;
105 EXPECT_EQ(&PTB
, PTA
.getPrev());
106 EXPECT_EQ(nullptr, PTA
.getNext());
107 EXPECT_EQ(nullptr, PTB
.getPrev());
108 EXPECT_EQ(nullptr, PTB
.getNext());
109 EXPECT_EQ(nullptr, PTC
.getPrev());
110 EXPECT_EQ(nullptr, PTC
.getNext());
113 EXPECT_EQ(&PTB
, PTA
.getPrev());
114 EXPECT_EQ(&PTC
, PTA
.getNext());
115 EXPECT_EQ(nullptr, PTB
.getPrev());
116 EXPECT_EQ(nullptr, PTB
.getNext());
117 EXPECT_EQ(nullptr, PTC
.getPrev());
118 EXPECT_EQ(nullptr, PTC
.getNext());
122 TEST(IListNodeBaseTest
, isKnownSentinel
) {
123 // Without sentinel tracking.
125 EXPECT_FALSE(A
.isKnownSentinel());
128 EXPECT_EQ(&B
, A
.getPrev());
129 EXPECT_EQ(&B
, A
.getNext());
130 EXPECT_FALSE(A
.isKnownSentinel());
131 A
.initializeSentinel();
132 EXPECT_FALSE(A
.isKnownSentinel());
133 EXPECT_EQ(&B
, A
.getPrev());
134 EXPECT_EQ(&B
, A
.getNext());
136 // With sentinel tracking.
138 EXPECT_FALSE(TA
.isKnownSentinel());
139 EXPECT_FALSE(TA
.isSentinel());
142 EXPECT_EQ(&TB
, TA
.getPrev());
143 EXPECT_EQ(&TB
, TA
.getNext());
144 EXPECT_FALSE(TA
.isKnownSentinel());
145 EXPECT_FALSE(TA
.isSentinel());
146 TA
.initializeSentinel();
147 EXPECT_TRUE(TA
.isKnownSentinel());
148 EXPECT_TRUE(TA
.isSentinel());
149 EXPECT_EQ(&TB
, TA
.getPrev());
150 EXPECT_EQ(&TB
, TA
.getNext());
152 // Without sentinel tracking (with Parent).
154 EXPECT_FALSE(PA
.isKnownSentinel());
157 EXPECT_EQ(&PB
, PA
.getPrev());
158 EXPECT_EQ(&PB
, PA
.getNext());
159 EXPECT_FALSE(PA
.isKnownSentinel());
160 PA
.initializeSentinel();
161 EXPECT_FALSE(PA
.isKnownSentinel());
162 EXPECT_EQ(&PB
, PA
.getPrev());
163 EXPECT_EQ(&PB
, PA
.getNext());
165 // With sentinel tracking (with Parent).
166 ParentTrackingNode PTA
, PTB
;
167 EXPECT_FALSE(PTA
.isKnownSentinel());
168 EXPECT_FALSE(PTA
.isSentinel());
171 EXPECT_EQ(&PTB
, PTA
.getPrev());
172 EXPECT_EQ(&PTB
, PTA
.getNext());
173 EXPECT_FALSE(PTA
.isKnownSentinel());
174 EXPECT_FALSE(PTA
.isSentinel());
175 PTA
.initializeSentinel();
176 EXPECT_TRUE(PTA
.isKnownSentinel());
177 EXPECT_TRUE(PTA
.isSentinel());
178 EXPECT_EQ(&PTB
, PTA
.getPrev());
179 EXPECT_EQ(&PTB
, PTA
.getNext());
182 TEST(IListNodeBaseTest
, nodeBaseParent
) {
185 const ParentNode CPA
;
186 EXPECT_EQ(nullptr, PA
.getNodeBaseParent());
187 PA
.setNodeBaseParent(&Par
);
188 EXPECT_EQ(&Par
, PA
.getNodeBaseParent());
190 ParentTrackingNode PTA
;
191 EXPECT_EQ(nullptr, PTA
.getNodeBaseParent());
192 PTA
.setNodeBaseParent(&Par
);
193 EXPECT_EQ(&Par
, PTA
.getNodeBaseParent());
195 using VarParentTy
= std::remove_pointer_t
<decltype(PA
.getNodeBaseParent())>;
196 using ConstParentTy
=
197 std::remove_pointer_t
<decltype(CPA
.getNodeBaseParent())>;
199 std::is_const_v
<ConstParentTy
> &&
200 std::is_same_v
<VarParentTy
, std::remove_const_t
<ConstParentTy
>>,
201 "`getNodeBaseParent() const` adds const to parent type");