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
> {
30 TEST(IListIteratorBitsTest
, DefaultConstructor
) {
31 simple_ilist
<Node
, ilist_iterator_bits
<true>>::iterator I
;
32 simple_ilist
<Node
, ilist_iterator_bits
<true>>::reverse_iterator RI
;
33 simple_ilist
<Node
, ilist_iterator_bits
<true>>::const_iterator CI
;
34 simple_ilist
<Node
, ilist_iterator_bits
<true>>::const_reverse_iterator CRI
;
35 EXPECT_EQ(nullptr, I
.getNodePtr());
36 EXPECT_EQ(nullptr, CI
.getNodePtr());
37 EXPECT_EQ(nullptr, RI
.getNodePtr());
38 EXPECT_EQ(nullptr, CRI
.getNodePtr());
47 EXPECT_EQ(I
, RI
.getReverse());
48 EXPECT_EQ(RI
, I
.getReverse());
51 TEST(IListIteratorBitsTest
, ConsAndAssignment
) {
52 simple_ilist
<Node
, ilist_iterator_bits
<true>> L
;
56 simple_ilist
<Node
, ilist_iterator_bits
<true>>::iterator I
, I2
;
58 // Two sets of tests: if we've compiled in the iterator bits, then check that
59 // HeadInclusiveBit and TailInclusiveBit are preserved on assignment and copy
60 // construction, but not on other operations.
61 #ifdef EXPERIMENTAL_DEBUGINFO_ITERATORS
63 EXPECT_FALSE(I
.getHeadBit());
64 EXPECT_FALSE(I
.getTailBit());
67 EXPECT_TRUE(I
.getHeadBit());
68 EXPECT_TRUE(I
.getTailBit());
72 EXPECT_FALSE(I
.getHeadBit());
73 EXPECT_FALSE(I
.getTailBit());
79 EXPECT_TRUE(I2
.getHeadBit());
80 EXPECT_TRUE(I2
.getTailBit());
85 simple_ilist
<Node
, ilist_iterator_bits
<true>>::iterator
I3(I
);
86 EXPECT_TRUE(I3
.getHeadBit());
87 EXPECT_TRUE(I3
.getTailBit());
89 // The calls should be available, but shouldn't actually store information.
91 EXPECT_FALSE(I
.getHeadBit());
92 EXPECT_FALSE(I
.getTailBit());
95 EXPECT_FALSE(I
.getHeadBit());
96 EXPECT_FALSE(I
.getTailBit());
97 // Suppress warnings as we don't test with this variable.
103 // Test that we get an ilist_iterator_w_bits out of the node given that the
104 // options are enabled.
105 using node_options
= typename
ilist_detail::compute_node_options
<
106 Node
, ilist_iterator_bits
<true>>::type
;
107 static_assert(std::is_same
<Node::self_iterator
,
108 llvm::ilist_iterator_w_bits
<node_options
, false,
111 // Now test that a plain node, without the option, gets a plain
113 using plain_node_options
=
114 typename
ilist_detail::compute_node_options
<PlainNode
>::type
;
115 static_assert(std::is_same
<
116 PlainNode::self_iterator
,
117 llvm::ilist_iterator
<plain_node_options
, false, false>>::value
);
120 TEST(IListIteratorBitsTest
, RangeIteration
) {
121 // Check that we can feed ilist_iterator_w_bits into make_range and similar.
122 // Plus, we should be able to convert it to a reverse iterator and use that.
123 simple_ilist
<Node
, ilist_iterator_bits
<true>> L
;
125 L
.insert(L
.end(), A
);
127 for (Node
&N
: make_range(L
.begin(), L
.end()))
130 simple_ilist
<Node
, ilist_iterator_bits
<true>>::iterator It
=
131 L
.begin()->getIterator();
132 auto RevIt
= It
.getReverse();
134 for (Node
&N
: make_range(RevIt
, L
.rend()))