| Chandler Carruth | 9a6be8b | 2014-04-24 03:31:23 +0000 | [diff] [blame^] | 1 | //===- 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" |
| 11 | #include "llvm/ADT/SmallVector.h" |
| 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | using namespace llvm; |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | TEST(PointeeIteratorTest, Basic) { |
| 19 | int arr[4] = { 1, 2, 3, 4 }; |
| 20 | SmallVector<int *, 4> V; |
| 21 | V.push_back(&arr[0]); |
| 22 | V.push_back(&arr[1]); |
| 23 | V.push_back(&arr[2]); |
| 24 | V.push_back(&arr[3]); |
| 25 | |
| 26 | typedef pointee_iterator<SmallVectorImpl<int *>::const_iterator> test_iterator; |
| 27 | |
| 28 | test_iterator Begin, End; |
| 29 | Begin = V.begin(); |
| 30 | End = test_iterator(V.end()); |
| 31 | |
| 32 | test_iterator I = Begin; |
| 33 | for (int i = 0; i < 4; ++i) { |
| 34 | EXPECT_EQ(*V[i], *I); |
| 35 | |
| 36 | EXPECT_EQ(I, Begin + i); |
| 37 | EXPECT_EQ(I, std::next(Begin, i)); |
| 38 | test_iterator J = Begin; |
| 39 | J += i; |
| 40 | EXPECT_EQ(I, J); |
| 41 | EXPECT_EQ(*V[i], Begin[i]); |
| 42 | |
| 43 | EXPECT_NE(I, End); |
| 44 | EXPECT_GT(End, I); |
| 45 | EXPECT_LT(I, End); |
| 46 | EXPECT_GE(I, Begin); |
| 47 | EXPECT_LE(Begin, I); |
| 48 | |
| 49 | EXPECT_EQ(i, I - Begin); |
| 50 | EXPECT_EQ(i, std::distance(Begin, I)); |
| 51 | EXPECT_EQ(Begin, I - i); |
| 52 | |
| 53 | test_iterator K = I++; |
| 54 | EXPECT_EQ(K, std::prev(I)); |
| 55 | } |
| 56 | EXPECT_EQ(End, I); |
| 57 | } |
| 58 | |
| 59 | } // anonymous namespace |