Fix test failures introduced by PR #113697 (#116941)
[llvm-project.git] / llvm / unittests / ADT / IListNodeBaseTest.cpp
blobef90c716a411874b0d7b6d685306ca3ceec5e114
1 //===- unittests/ADT/IListNodeBaseTest.cpp - ilist_node_base unit tests ---===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/ilist_node_base.h"
10 #include "gtest/gtest.h"
12 #include <type_traits>
14 using namespace llvm;
16 namespace {
18 class Parent {};
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) {
26 RawNode A;
27 EXPECT_EQ(nullptr, A.getPrev());
28 EXPECT_EQ(nullptr, A.getNext());
29 EXPECT_FALSE(A.isKnownSentinel());
31 TrackingNode TA;
32 EXPECT_EQ(nullptr, TA.getPrev());
33 EXPECT_EQ(nullptr, TA.getNext());
34 EXPECT_FALSE(TA.isKnownSentinel());
35 EXPECT_FALSE(TA.isSentinel());
37 ParentNode PA;
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) {
52 RawNode A, B, C;
53 A.setPrev(&B);
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());
61 A.setNext(&C);
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;
70 TA.setPrev(&TB);
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());
78 TA.setNext(&TC);
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;
87 PA.setPrev(&PB);
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());
95 PA.setNext(&PC);
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;
104 PTA.setPrev(&PTB);
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());
112 PTA.setNext(&PTC);
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.
124 RawNode A, B;
125 EXPECT_FALSE(A.isKnownSentinel());
126 A.setPrev(&B);
127 A.setNext(&B);
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.
137 TrackingNode TA, TB;
138 EXPECT_FALSE(TA.isKnownSentinel());
139 EXPECT_FALSE(TA.isSentinel());
140 TA.setPrev(&TB);
141 TA.setNext(&TB);
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).
153 ParentNode PA, PB;
154 EXPECT_FALSE(PA.isKnownSentinel());
155 PA.setPrev(&PB);
156 PA.setNext(&PB);
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());
169 PTA.setPrev(&PTB);
170 PTA.setNext(&PTB);
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) {
183 Parent Par;
184 ParentNode PA;
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())>;
198 static_assert(
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");
204 } // end namespace