Tim Shen | 608ca25 | 2016-08-22 21:59:26 +0000 | [diff] [blame] | 1 | //=== llvm/unittest/ADT/DepthFirstIteratorTest.cpp - DFS iterator tests ---===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Tim Shen | 608ca25 | 2016-08-22 21:59:26 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Tim Shen | 608ca25 | 2016-08-22 21:59:26 +0000 | [diff] [blame] | 9 | #include "llvm/ADT/DepthFirstIterator.h" |
Chandler Carruth | 9a67b07 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 10 | #include "TestGraph.h" |
Tim Shen | 608ca25 | 2016-08-22 21:59:26 +0000 | [diff] [blame] | 11 | #include "gtest/gtest.h" |
| 12 | |
| 13 | using namespace llvm; |
| 14 | |
| 15 | namespace llvm { |
| 16 | |
| 17 | template <typename T> struct CountedSet { |
| 18 | typedef typename SmallPtrSet<T, 4>::iterator iterator; |
| 19 | |
| 20 | SmallPtrSet<T, 4> S; |
| 21 | int InsertVisited = 0; |
| 22 | |
| 23 | std::pair<iterator, bool> insert(const T &Item) { |
| 24 | InsertVisited++; |
| 25 | return S.insert(Item); |
| 26 | } |
| 27 | |
| 28 | size_t count(const T &Item) const { return S.count(Item); } |
David Callahan | c1051ab | 2016-10-05 21:36:16 +0000 | [diff] [blame] | 29 | |
| 30 | void completed(T) { } |
Tim Shen | 608ca25 | 2016-08-22 21:59:26 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | template <typename T> class df_iterator_storage<CountedSet<T>, true> { |
| 34 | public: |
| 35 | df_iterator_storage(CountedSet<T> &VSet) : Visited(VSet) {} |
| 36 | |
| 37 | CountedSet<T> &Visited; |
| 38 | }; |
| 39 | |
| 40 | TEST(DepthFirstIteratorTest, ActuallyUpdateIterator) { |
| 41 | typedef CountedSet<Graph<3>::NodeType *> StorageT; |
| 42 | typedef df_iterator<Graph<3>, StorageT, true> DFIter; |
| 43 | |
| 44 | Graph<3> G; |
| 45 | G.AddEdge(0, 1); |
| 46 | G.AddEdge(0, 2); |
| 47 | StorageT S; |
| 48 | for (auto N : make_range(DFIter::begin(G, S), DFIter::end(G, S))) |
| 49 | (void)N; |
| 50 | |
| 51 | EXPECT_EQ(3, S.InsertVisited); |
| 52 | } |
| 53 | } |