1 //===- IteratorTest.cpp - Unit tests for iterator utilities ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ADT/iterator.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "gtest/gtest.h"
19 TEST(PointeeIteratorTest, Basic) {
20 int arr[4] = { 1, 2, 3, 4 };
21 SmallVector<int *, 4> V;
27 typedef pointee_iterator<SmallVectorImpl<int *>::const_iterator> test_iterator;
29 test_iterator Begin, End;
31 End = test_iterator(V.end());
33 test_iterator I = Begin;
34 for (int i = 0; i < 4; ++i) {
37 EXPECT_EQ(I, Begin + i);
38 EXPECT_EQ(I, std::next(Begin, i));
39 test_iterator J = Begin;
42 EXPECT_EQ(*V[i], Begin[i]);
50 EXPECT_EQ(i, I - Begin);
51 EXPECT_EQ(i, std::distance(Begin, I));
52 EXPECT_EQ(Begin, I - i);
54 test_iterator K = I++;
55 EXPECT_EQ(K, std::prev(I));
60 TEST(PointeeIteratorTest, SmartPointer) {
61 SmallVector<std::unique_ptr<int>, 4> V;
62 V.push_back(make_unique<int>(1));
63 V.push_back(make_unique<int>(2));
64 V.push_back(make_unique<int>(3));
65 V.push_back(make_unique<int>(4));
67 typedef pointee_iterator<
68 SmallVectorImpl<std::unique_ptr<int>>::const_iterator> test_iterator;
70 test_iterator Begin, End;
72 End = test_iterator(V.end());
74 test_iterator I = Begin;
75 for (int i = 0; i < 4; ++i) {
78 EXPECT_EQ(I, Begin + i);
79 EXPECT_EQ(I, std::next(Begin, i));
80 test_iterator J = Begin;
83 EXPECT_EQ(*V[i], Begin[i]);
91 EXPECT_EQ(i, I - Begin);
92 EXPECT_EQ(i, std::distance(Begin, I));
93 EXPECT_EQ(Begin, I - i);
95 test_iterator K = I++;
96 EXPECT_EQ(K, std::prev(I));
101 } // anonymous namespace