diff options
author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2016-08-18 17:15:25 +0000 |
---|---|---|
committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2016-08-18 17:15:25 +0000 |
commit | 9d748f949977dc972694fb5a9f3a11575521f182 (patch) | |
tree | a14fb0cb48a3ef038f2bbc34c1636938e77c7811 /llvm/unittests/ADT/RangeAdapterTest.cpp | |
parent | a8105c607b434a1ae2af35fd4ef0fe46f43c33cf (diff) | |
download | bcm5719-llvm-9d748f949977dc972694fb5a9f3a11575521f182.tar.gz bcm5719-llvm-9d748f949977dc972694fb5a9f3a11575521f182.zip |
Reapply "ADT: Remove references in has_rbegin for reverse()"
This reverts commit r279086, reapplying r279084. I'm not sure what I
ran before, because the compile failure for ADTTests reproduced locally.
The problem is that TestRev is calling BidirectionalVector::rbegin()
when the BidirectionalVector is const, but rbegin() is always non-const.
I've updated BidirectionalVector::rbegin() to be callable from const.
Original commit message follows.
--
As a follow-up to r278991, add some tests that check that
decltype(reverse(R).begin()) == decltype(R.rbegin()), and get them
passing by adding std::remove_reference to has_rbegin.
I'm using static_assert instead of EXPECT_TRUE (and updated the other
has_rbegin check from r278991 in the same way) since I figure that's
more helpful.
llvm-svn: 279091
Diffstat (limited to 'llvm/unittests/ADT/RangeAdapterTest.cpp')
-rw-r--r-- | llvm/unittests/ADT/RangeAdapterTest.cpp | 76 |
1 files changed, 67 insertions, 9 deletions
diff --git a/llvm/unittests/ADT/RangeAdapterTest.cpp b/llvm/unittests/ADT/RangeAdapterTest.cpp index 2b12fe47173..4c7bef53235 100644 --- a/llvm/unittests/ADT/RangeAdapterTest.cpp +++ b/llvm/unittests/ADT/RangeAdapterTest.cpp @@ -38,18 +38,18 @@ public: // begin() and end() don't have implementations as this ensures that we will // get a linker error if reverse() chooses begin()/end() over rbegin(), rend(). class BidirectionalVector { - std::vector<int> Vec; + mutable std::vector<int> Vec; public: BidirectionalVector(std::initializer_list<int> list) : Vec(list) {} typedef std::vector<int>::iterator iterator; - iterator begin(); - iterator end(); + iterator begin() const; + iterator end() const; typedef std::vector<int>::reverse_iterator reverse_iterator; - reverse_iterator rbegin() { return Vec.rbegin(); } - reverse_iterator rend() { return Vec.rend(); } + reverse_iterator rbegin() const { return Vec.rbegin(); } + reverse_iterator rend() const { return Vec.rend(); } }; /// This is the same as BidirectionalVector but with the addition of const @@ -75,6 +75,50 @@ public: const_reverse_iterator rend() const { return Vec.rend(); } }; +/// Check that types with custom iterators work. +class CustomIteratorVector { + mutable std::vector<int> V; + +public: + CustomIteratorVector(std::initializer_list<int> list) : V(list) {} + + typedef std::vector<int>::iterator iterator; + class reverse_iterator { + std::vector<int>::iterator I; + + public: + reverse_iterator() = default; + reverse_iterator(const reverse_iterator &) = default; + reverse_iterator &operator=(const reverse_iterator &) = default; + + explicit reverse_iterator(std::vector<int>::iterator I) : I(I) {} + + reverse_iterator &operator++() { + --I; + return *this; + } + reverse_iterator &operator--() { + ++I; + return *this; + } + int &operator*() const { return *std::prev(I); } + int *operator->() const { return &*std::prev(I); } + friend bool operator==(const reverse_iterator &L, + const reverse_iterator &R) { + return L.I == R.I; + } + friend bool operator!=(const reverse_iterator &L, + const reverse_iterator &R) { + return !(L == R); + } + }; + + iterator begin() const { return V.begin(); } + iterator end() const { return V.end(); } + reverse_iterator rbegin() const { return reverse_iterator(V.end()); } + reverse_iterator rend() const { return reverse_iterator(V.begin()); } +}; + template <typename R> void TestRev(const R &r) { int counter = 3; for (int i : r) @@ -98,9 +142,10 @@ TYPED_TEST(RangeAdapterLValueTest, TrivialOperation) { template <typename T> struct RangeAdapterRValueTest : testing::Test {}; -typedef ::testing::Types<std::vector<int>, std::list<int>, ReverseOnlyVector, - BidirectionalVector, - BidirectionalVectorConsts> RangeAdapterRValueTestTypes; +typedef ::testing::Types<std::vector<int>, std::list<int>, CustomIteratorVector, + ReverseOnlyVector, BidirectionalVector, + BidirectionalVectorConsts> + RangeAdapterRValueTestTypes; TYPED_TEST_CASE(RangeAdapterRValueTest, RangeAdapterRValueTestTypes); TYPED_TEST(RangeAdapterRValueTest, TrivialOperation) { @@ -108,7 +153,20 @@ TYPED_TEST(RangeAdapterRValueTest, TrivialOperation) { } TYPED_TEST(RangeAdapterRValueTest, HasRbegin) { - EXPECT_TRUE(has_rbegin<TypeParam>::value); + static_assert(has_rbegin<TypeParam>::value, "rbegin() should be defined"); +} + +TYPED_TEST(RangeAdapterRValueTest, RangeType) { + static_assert( + std::is_same< + decltype(reverse(*static_cast<TypeParam *>(nullptr)).begin()), + decltype(static_cast<TypeParam *>(nullptr)->rbegin())>::value, + "reverse().begin() should have the same type as rbegin()"); + static_assert( + std::is_same< + decltype(reverse(*static_cast<const TypeParam *>(nullptr)).begin()), + decltype(static_cast<const TypeParam *>(nullptr)->rbegin())>::value, + "reverse().begin() should have the same type as rbegin() [const]"); } } // anonymous namespace |