1 //=== llvm/unittest/ADT/BreadthFirstIteratorTest.cpp - BFS iterator 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/BreadthFirstIterator.h"
10 #include "TestGraph.h"
11 #include "gtest/gtest.h"
15 #include <type_traits>
23 TEST(BreadthFristIteratorTest
, Basic
) {
24 typedef bf_iterator
<Graph
<4>> BFIter
;
31 auto It
= BFIter::begin(G
);
32 auto End
= BFIter::end(G
);
33 EXPECT_EQ(It
.getLevel(), 0U);
34 EXPECT_EQ(*It
, G
.AccessNode(0));
36 EXPECT_EQ(It
.getLevel(), 1U);
37 EXPECT_EQ(*It
, G
.AccessNode(1));
39 EXPECT_EQ(It
.getLevel(), 1U);
40 EXPECT_EQ(*It
, G
.AccessNode(2));
42 EXPECT_EQ(It
.getLevel(), 2U);
43 EXPECT_EQ(*It
, G
.AccessNode(3));
48 TEST(BreadthFristIteratorTest
, Cycle
) {
49 typedef bf_iterator
<Graph
<4>> BFIter
;
62 auto It
= BFIter::begin(G
);
63 auto End
= BFIter::end(G
);
64 EXPECT_EQ(It
.getLevel(), 0U);
65 EXPECT_EQ(*It
, G
.AccessNode(0));
67 EXPECT_EQ(It
.getLevel(), 1U);
68 EXPECT_EQ(*It
, G
.AccessNode(1));
70 EXPECT_EQ(It
.getLevel(), 2U);
71 EXPECT_EQ(*It
, G
.AccessNode(2));
73 EXPECT_EQ(It
.getLevel(), 3U);
74 EXPECT_EQ(*It
, G
.AccessNode(3));
80 std::is_convertible_v
<decltype(*std::declval
<bf_iterator
<Graph
<3>>>()),
81 typename bf_iterator
<Graph
<3>>::reference
>);
83 // bf_iterator should be (at-least) a forward-iterator
84 static_assert(std::is_base_of_v
<std::forward_iterator_tag
,
85 bf_iterator
<Graph
<4>>::iterator_category
>);
87 TEST(BreadthFristIteratorTest
, MultiPassSafeWithInternalSet
) {
93 std::array
<decltype(G
)::NodeType
*, 4> NodesFirstPass
, NodesSecondPass
;
95 auto B
= bf_begin(G
), E
= bf_end(G
);
98 for (auto It
= B
; It
!= E
; ++It
)
99 NodesFirstPass
[I
++] = *It
;
102 for (auto It
= B
; It
!= E
; ++It
)
103 NodesSecondPass
[I
++] = *It
;
105 EXPECT_EQ(NodesFirstPass
, NodesSecondPass
);
108 } // end namespace llvm