1 //==- unittests/ADT/IListIteratorBitsTest.cpp - ilist_iterator_w_bits 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/simple_ilist.h"
10 #include "gtest/gtest.h"
12 // Test that ilist_iterator_w_bits can be used to store extra information about
13 // what we're iterating over, that it's only enabled when given the relevant
14 // option, and it can be fed into various iteration utilities.
22 struct Node
: ilist_node
<Node
, ilist_iterator_bits
<true>> {
26 struct PlainNode
: ilist_node
<PlainNode
> {
32 : ilist_node
<ParentNode
, ilist_iterator_bits
<true>, ilist_parent
<Parent
>> {
35 TEST(IListIteratorBitsTest
, DefaultConstructor
) {
36 simple_ilist
<Node
, ilist_iterator_bits
<true>>::iterator I
;
37 simple_ilist
<Node
, ilist_iterator_bits
<true>>::reverse_iterator RI
;
38 simple_ilist
<Node
, ilist_iterator_bits
<true>>::const_iterator CI
;
39 simple_ilist
<Node
, ilist_iterator_bits
<true>>::const_reverse_iterator CRI
;
40 EXPECT_EQ(nullptr, I
.getNodePtr());
41 EXPECT_EQ(nullptr, CI
.getNodePtr());
42 EXPECT_EQ(nullptr, RI
.getNodePtr());
43 EXPECT_EQ(nullptr, CRI
.getNodePtr());
52 EXPECT_EQ(I
, RI
.getReverse());
53 EXPECT_EQ(RI
, I
.getReverse());
56 TEST(IListIteratorBitsTest
, ConsAndAssignment
) {
57 simple_ilist
<Node
, ilist_iterator_bits
<true>> L
;
61 simple_ilist
<Node
, ilist_iterator_bits
<true>>::iterator I
, I2
;
63 // Check that HeadInclusiveBit and TailInclusiveBit are preserved on
64 // assignment and copy construction, but not on other operations.
66 EXPECT_FALSE(I
.getHeadBit());
67 EXPECT_FALSE(I
.getTailBit());
70 EXPECT_TRUE(I
.getHeadBit());
71 EXPECT_TRUE(I
.getTailBit());
75 EXPECT_FALSE(I
.getHeadBit());
76 EXPECT_FALSE(I
.getTailBit());
82 EXPECT_TRUE(I2
.getHeadBit());
83 EXPECT_TRUE(I2
.getTailBit());
88 simple_ilist
<Node
, ilist_iterator_bits
<true>>::iterator
I3(I
);
89 EXPECT_TRUE(I3
.getHeadBit());
90 EXPECT_TRUE(I3
.getTailBit());
94 // Test that we get an ilist_iterator_w_bits out of the node given that the
95 // options are enabled.
96 using node_options
= typename
ilist_detail::compute_node_options
<
97 Node
, ilist_iterator_bits
<true>>::type
;
98 static_assert(std::is_same
<Node::self_iterator
,
99 llvm::ilist_iterator_w_bits
<node_options
, false,
102 // Now test that a plain node, without the option, gets a plain
104 using plain_node_options
=
105 typename
ilist_detail::compute_node_options
<PlainNode
>::type
;
106 static_assert(std::is_same
<
107 PlainNode::self_iterator
,
108 llvm::ilist_iterator
<plain_node_options
, false, false>>::value
);
111 TEST(IListIteratorBitsTest
, RangeIteration
) {
112 // Check that we can feed ilist_iterator_w_bits into make_range and similar.
113 // Plus, we should be able to convert it to a reverse iterator and use that.
114 simple_ilist
<Node
, ilist_iterator_bits
<true>> L
;
116 L
.insert(L
.end(), A
);
118 for (Node
&N
: make_range(L
.begin(), L
.end()))
121 simple_ilist
<Node
, ilist_iterator_bits
<true>>::iterator It
=
122 L
.begin()->getIterator();
123 auto RevIt
= It
.getReverse();
125 for (Node
&N
: make_range(RevIt
, L
.rend()))
129 TEST(IListIteratorBitsTest
, GetParent
) {
130 simple_ilist
<ParentNode
, ilist_iterator_bits
<true>, ilist_parent
<Parent
>> L
;
134 // Parents are not set automatically.
136 L
.insert(L
.end(), A
);
137 L
.end().getNodePtr()->setParent(&P
);
139 // Check we can get the node parent from all iterators, including for the
141 EXPECT_EQ(&P
, L
.begin().getNodeParent());
142 EXPECT_EQ(&P
, L
.end().getNodeParent());
143 EXPECT_EQ(&P
, L
.rbegin().getNodeParent());
144 EXPECT_EQ(&P
, L
.rend().getNodeParent());