aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/IteratorTest.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2014-04-24 03:31:23 +0000
committerChandler Carruth <chandlerc@gmail.com>2014-04-24 03:31:23 +0000
commit9a6be8b3b14972694408b2a8555751f7954744fc (patch)
tree0f5045578d87e0866b3bdbd07b362df26787ff4b /llvm/unittests/Support/IteratorTest.cpp
parent7c3ccb032ed31e73822908fd2ed9afbabe577a15 (diff)
downloadllvm-9a6be8b3b14972694408b2a8555751f7954744fc.zip
llvm-9a6be8b3b14972694408b2a8555751f7954744fc.tar.gz
llvm-9a6be8b3b14972694408b2a8555751f7954744fc.tar.bz2
[ADT] Add a generic iterator utility for adapting iterators much like
Boost's iterator_adaptor, and a specific adaptor which iterates over pointees when wrapped around an iterator over pointers. This is the result of a long discussion on IRC with Duncan Smith, Dave Blaikie, Richard Smith, and myself. Essentially, I could use some subset of the iterator facade facilities often used from Boost, and everyone seemed interested in having the functionality in a reasonably generic form. I've tried to strike a balance between the pragmatism and the established Boost design. The primary differences are: 1) Delegating to the standard iterator interface names rather than special names that then make up a second iterator-like API. 2) Using the name 'pointee_iterator' which seems more clear than 'indirect_iterator'. The whole business of calling the '*p' operation 'pointer indirection' in the standard is ... quite confusing. And 'dereference' is no better of a term for moving from a pointer to a reference. Hoping Duncan, and others continue to provide comments on this until we've got a nice, minimal abstraction. llvm-svn: 207069
Diffstat (limited to 'llvm/unittests/Support/IteratorTest.cpp')
-rw-r--r--llvm/unittests/Support/IteratorTest.cpp59
1 files changed, 59 insertions, 0 deletions
diff --git a/llvm/unittests/Support/IteratorTest.cpp b/llvm/unittests/Support/IteratorTest.cpp
new file mode 100644
index 0000000..3a16406
--- /dev/null
+++ b/llvm/unittests/Support/IteratorTest.cpp
@@ -0,0 +1,59 @@
+//===- IteratorTest.cpp - Unit tests for iterator utilities ---------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/iterator.h"
+#include "llvm/ADT/SmallVector.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+TEST(PointeeIteratorTest, Basic) {
+ 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;
+
+ test_iterator Begin, End;
+ Begin = V.begin();
+ End = test_iterator(V.end());
+
+ test_iterator I = Begin;
+ for (int i = 0; i < 4; ++i) {
+ EXPECT_EQ(*V[i], *I);
+
+ EXPECT_EQ(I, Begin + i);
+ EXPECT_EQ(I, std::next(Begin, i));
+ test_iterator J = Begin;
+ J += i;
+ EXPECT_EQ(I, J);
+ EXPECT_EQ(*V[i], Begin[i]);
+
+ EXPECT_NE(I, End);
+ EXPECT_GT(End, I);
+ EXPECT_LT(I, End);
+ EXPECT_GE(I, Begin);
+ EXPECT_LE(Begin, I);
+
+ EXPECT_EQ(i, I - Begin);
+ EXPECT_EQ(i, std::distance(Begin, I));
+ EXPECT_EQ(Begin, I - i);
+
+ test_iterator K = I++;
+ EXPECT_EQ(K, std::prev(I));
+ }
+ EXPECT_EQ(End, I);
+}
+
+} // anonymous namespace