blob: 83848328c0c62c675770f6d6eed8d221535334ea [file] [log] [blame]
Chandler Carruth9a6be8b2014-04-24 03:31:23 +00001//===- IteratorTest.cpp - Unit tests for iterator utilities ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ADT/iterator.h"
Chandler Carruthd5835ee2014-04-24 21:10:35 +000011#include "llvm/ADT/STLExtras.h"
Chandler Carruth9a6be8b2014-04-24 03:31:23 +000012#include "llvm/ADT/SmallVector.h"
13#include "gtest/gtest.h"
14
15using namespace llvm;
16
17namespace {
18
19TEST(PointeeIteratorTest, Basic) {
20 int arr[4] = { 1, 2, 3, 4 };
21 SmallVector<int *, 4> V;
22 V.push_back(&arr[0]);
23 V.push_back(&arr[1]);
24 V.push_back(&arr[2]);
25 V.push_back(&arr[3]);
26
27 typedef pointee_iterator<SmallVectorImpl<int *>::const_iterator> test_iterator;
28
29 test_iterator Begin, End;
30 Begin = V.begin();
31 End = test_iterator(V.end());
32
33 test_iterator I = Begin;
34 for (int i = 0; i < 4; ++i) {
35 EXPECT_EQ(*V[i], *I);
36
37 EXPECT_EQ(I, Begin + i);
38 EXPECT_EQ(I, std::next(Begin, i));
39 test_iterator J = Begin;
40 J += i;
41 EXPECT_EQ(I, J);
42 EXPECT_EQ(*V[i], Begin[i]);
43
44 EXPECT_NE(I, End);
45 EXPECT_GT(End, I);
46 EXPECT_LT(I, End);
47 EXPECT_GE(I, Begin);
48 EXPECT_LE(Begin, I);
49
50 EXPECT_EQ(i, I - Begin);
51 EXPECT_EQ(i, std::distance(Begin, I));
52 EXPECT_EQ(Begin, I - i);
53
54 test_iterator K = I++;
55 EXPECT_EQ(K, std::prev(I));
56 }
57 EXPECT_EQ(End, I);
58}
59
Chandler Carruthd5835ee2014-04-24 21:10:35 +000060TEST(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));
66
67 typedef pointee_iterator<
68 SmallVectorImpl<std::unique_ptr<int>>::const_iterator> test_iterator;
69
70 test_iterator Begin, End;
71 Begin = V.begin();
72 End = test_iterator(V.end());
73
74 test_iterator I = Begin;
75 for (int i = 0; i < 4; ++i) {
76 EXPECT_EQ(*V[i], *I);
77
78 EXPECT_EQ(I, Begin + i);
79 EXPECT_EQ(I, std::next(Begin, i));
80 test_iterator J = Begin;
81 J += i;
82 EXPECT_EQ(I, J);
83 EXPECT_EQ(*V[i], Begin[i]);
84
85 EXPECT_NE(I, End);
86 EXPECT_GT(End, I);
87 EXPECT_LT(I, End);
88 EXPECT_GE(I, Begin);
89 EXPECT_LE(Begin, I);
90
91 EXPECT_EQ(i, I - Begin);
92 EXPECT_EQ(i, std::distance(Begin, I));
93 EXPECT_EQ(Begin, I - i);
94
95 test_iterator K = I++;
96 EXPECT_EQ(K, std::prev(I));
97 }
98 EXPECT_EQ(End, I);
99}
100
Chandler Carruth9a6be8b2014-04-24 03:31:23 +0000101} // anonymous namespace