1 //===- RangeAdapterTest.cpp - Unit tests for range adapters --------------===// 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_range.h" 11 #include "llvm/ADT/STLExtras.h" 12 #include "gtest/gtest.h" 13 14 #include <iterator> 15 #include <list> 16 #include <vector> 17 18 using namespace llvm; 19 20 namespace { 21 22 // A wrapper around vector which exposes rbegin(), rend(). 23 class ReverseOnlyVector { 24 std::vector<int> Vec; 25 26 public: 27 ReverseOnlyVector(std::initializer_list<int> list) : Vec(list) {} 28 29 typedef std::vector<int>::reverse_iterator reverse_iterator; 30 reverse_iterator rbegin() { return Vec.rbegin(); } 31 reverse_iterator rend() { return Vec.rend(); } 32 }; 33 34 // A wrapper around vector which exposes begin(), end(), rbegin() and rend(). 35 // begin() and end() don't have implementations as this ensures that we will 36 // get a linker error if reverse() chooses begin()/end() over rbegin(), rend(). 37 class BidirectionalVector { 38 std::vector<int> Vec; 39 40 public: 41 BidirectionalVector(std::initializer_list<int> list) : Vec(list) {} 42 43 typedef std::vector<int>::iterator iterator; 44 iterator begin(); 45 iterator end(); 46 47 typedef std::vector<int>::reverse_iterator reverse_iterator; 48 reverse_iterator rbegin() { return Vec.rbegin(); } 49 reverse_iterator rend() { return Vec.rend(); } 50 }; 51 52 template <typename R> void TestRev(const R &r) { 53 int counter = 3; 54 for (int i : r) 55 EXPECT_EQ(i, counter--); 56 } 57 58 // Test fixture 59 template <typename T> class RangeAdapterLValueTest : public ::testing::Test {}; 60 61 typedef ::testing::Types<std::vector<int>, std::list<int>, int[4]> 62 RangeAdapterLValueTestTypes; 63 TYPED_TEST_CASE(RangeAdapterLValueTest, RangeAdapterLValueTestTypes); 64 65 TYPED_TEST(RangeAdapterLValueTest, TrivialOperation) { 66 TypeParam v = {0, 1, 2, 3}; 67 TestRev(reverse(v)); 68 69 const TypeParam c = {0, 1, 2, 3}; 70 TestRev(reverse(c)); 71 } 72 73 template <typename T> struct RangeAdapterRValueTest : testing::Test {}; 74 75 typedef ::testing::Types<std::vector<int>, std::list<int>, ReverseOnlyVector, 76 BidirectionalVector> RangeAdapterRValueTestTypes; 77 TYPED_TEST_CASE(RangeAdapterRValueTest, RangeAdapterRValueTestTypes); 78 79 TYPED_TEST(RangeAdapterRValueTest, TrivialOperation) { 80 TestRev(reverse(TypeParam({0, 1, 2, 3}))); 81 } 82 83 } // anonymous namespace 84