diff options
Diffstat (limited to 'unittests/ADT/IteratorTest.cpp')
-rw-r--r-- | unittests/ADT/IteratorTest.cpp | 73 |
1 files changed, 69 insertions, 4 deletions
diff --git a/unittests/ADT/IteratorTest.cpp b/unittests/ADT/IteratorTest.cpp index a8d5b33a0b49..7f261824b499 100644 --- a/unittests/ADT/IteratorTest.cpp +++ b/unittests/ADT/IteratorTest.cpp @@ -7,9 +7,9 @@ // //===----------------------------------------------------------------------===// -#include "llvm/ADT/iterator.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/iterator.h" #include "gtest/gtest.h" using namespace llvm; @@ -35,14 +35,15 @@ static_assert(std::is_same<typename AdaptedIter::reference, Shadow<3>>::value, ""); TEST(PointeeIteratorTest, Basic) { - int arr[4] = { 1, 2, 3, 4 }; + int arr[4] = {1, 2, 3, 4}; SmallVector<int *, 4> V; V.push_back(&arr[0]); V.push_back(&arr[1]); V.push_back(&arr[2]); V.push_back(&arr[3]); - typedef pointee_iterator<SmallVectorImpl<int *>::const_iterator> test_iterator; + typedef pointee_iterator<SmallVectorImpl<int *>::const_iterator> + test_iterator; test_iterator Begin, End; Begin = V.begin(); @@ -83,7 +84,8 @@ TEST(PointeeIteratorTest, SmartPointer) { V.push_back(make_unique<int>(4)); typedef pointee_iterator< - SmallVectorImpl<std::unique_ptr<int>>::const_iterator> test_iterator; + SmallVectorImpl<std::unique_ptr<int>>::const_iterator> + test_iterator; test_iterator Begin, End; Begin = V.begin(); @@ -116,6 +118,15 @@ TEST(PointeeIteratorTest, SmartPointer) { EXPECT_EQ(End, I); } +TEST(PointeeIteratorTest, Range) { + int A[] = {1, 2, 3, 4}; + SmallVector<int *, 4> V{&A[0], &A[1], &A[2], &A[3]}; + + int I = 0; + for (int II : make_pointee_range(V)) + EXPECT_EQ(A[I++], II); +} + TEST(FilterIteratorTest, Lambda) { auto IsOdd = [](int N) { return N % 2 == 1; }; int A[] = {0, 1, 2, 3, 4, 5, 6}; @@ -209,6 +220,13 @@ TEST(PointerIterator, Const) { EXPECT_EQ(A + 4, std::next(*Begin, 4)); } +TEST(PointerIterator, Range) { + int A[] = {1, 2, 3, 4}; + int I = 0; + for (int *P : make_pointer_range(A)) + EXPECT_EQ(A + I++, P); +} + TEST(ZipIteratorTest, Basic) { using namespace std; const SmallVector<unsigned, 6> pi{3, 1, 4, 1, 5, 9}; @@ -272,4 +290,51 @@ TEST(ZipIteratorTest, ZipFirstMutability) { } } +TEST(ZipIteratorTest, Filter) { + using namespace std; + vector<unsigned> pi{3, 1, 4, 1, 5, 9}; + + unsigned iters = 0; + // pi is length 6, but the zip RHS is length 7. + auto zipped = zip_first(pi, vector<bool>{1, 1, 0, 1, 1, 1, 0}); + for (auto tup : make_filter_range( + zipped, [](decltype(zipped)::value_type t) { return get<1>(t); })) { + EXPECT_EQ(get<0>(tup) & 0x01, get<1>(tup)); + get<0>(tup) += 1; + iters += 1; + } + + // Should have skipped pi[2]. + EXPECT_EQ(iters, 5u); + + // Ensure that in-place mutation works. + EXPECT_TRUE(all_of(pi, [](unsigned n) { return (n & 0x01) == 0; })); +} + +TEST(ZipIteratorTest, Reverse) { + using namespace std; + vector<unsigned> ascending{0, 1, 2, 3, 4, 5}; + + auto zipped = zip_first(ascending, vector<bool>{0, 1, 0, 1, 0, 1}); + unsigned last = 6; + for (auto tup : reverse(zipped)) { + // Check that this is in reverse. + EXPECT_LT(get<0>(tup), last); + last = get<0>(tup); + EXPECT_EQ(get<0>(tup) & 0x01, get<1>(tup)); + } + + auto odds = [](decltype(zipped)::value_type tup) { return get<1>(tup); }; + last = 6; + for (auto tup : make_filter_range(reverse(zipped), odds)) { + EXPECT_LT(get<0>(tup), last); + last = get<0>(tup); + EXPECT_TRUE(get<0>(tup) & 0x01); + get<0>(tup) += 1; + } + + // Ensure that in-place mutation works. + EXPECT_TRUE(all_of(ascending, [](unsigned n) { return (n & 0x01) == 0; })); +} + } // anonymous namespace |