llvm/unittests/ADT/SequenceTest.cpp
Chandler Carruth b29c2901de [ADT] Add an 'llvm::seq' function which produces an iterator range over
a sequence of values.

It increments through the values in the half-open range: [Begin, End),
producing those values when indirecting the iterator. It should support
integers, iterators, and any other type providing these basic arithmetic
operations.

This came up in the C++ standards committee meeting, and it seemed like
a useful construct that LLVM might want as well, and I wanted to
understand how easily we could solve it. I suspect this can be used to
write simpler counting loops even in LLVM along the lines of:

  for (int i : seq(0, v.size())) {
    ...
  };

As part of this, I had to fix the lack of a proxy object returned from
the operator[] in our iterator facade.

Differential Revision: http://reviews.llvm.org/D17870

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269390 91177308-0d34-0410-b5e6-96231b3b80d8
2016-05-13 03:57:50 +00:00

40 lines
980 B
C++

//===- SequenceTest.cpp - Unit tests for a sequence abstraciton -----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/Sequence.h"
#include "gtest/gtest.h"
#include <list>
using namespace llvm;
namespace {
TEST(SequenceTest, Basic) {
int x = 0;
for (int i : seq(0, 10))
EXPECT_EQ(x++, i);
EXPECT_EQ(10, x);
auto my_seq = seq(0, 4);
EXPECT_EQ(4, my_seq.end() - my_seq.begin());
for (int i : {0, 1, 2, 3})
EXPECT_EQ(i, (int)my_seq.begin()[i]);
EXPECT_TRUE(my_seq.begin() < my_seq.end());
auto adjusted_begin = my_seq.begin() + 2;
auto adjusted_end = my_seq.end() - 2;
EXPECT_TRUE(adjusted_begin == adjusted_end);
EXPECT_EQ(2, *adjusted_begin);
EXPECT_EQ(2, *adjusted_end);
}
} // anonymous namespace