1 //===------ MappedIteratorTest.cpp - Unit tests for mapped_iterator -------===// 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/STLExtras.h" 11 #include "gtest/gtest.h" 12 13 using namespace llvm; 14 15 namespace { 16 17 TEST(MappedIteratorTest, ApplyFunctionOnDereference) { 18 std::vector<int> V({0}); 19 20 auto I = map_iterator(V.begin(), [](int X) { return X + 1; }); 21 22 EXPECT_EQ(*I, 1) << "should have applied function in dereference"; 23 } 24 25 TEST(MappedIteratorTest, ApplyFunctionOnArrow) { 26 struct S { 27 int Z = 0; 28 }; 29 30 std::vector<int> V({0}); 31 S Y; 32 S* P = &Y; 33 34 auto I = map_iterator(V.begin(), [&](int X) -> S& { return *(P + X); }); 35 36 I->Z = 42; 37 38 EXPECT_EQ(Y.Z, 42) << "should have applied function during arrow"; 39 } 40 41 TEST(MappedIteratorTest, FunctionPreservesReferences) { 42 std::vector<int> V({1}); 43 std::map<int, int> M({ {1, 1} }); 44 45 auto I = map_iterator(V.begin(), [&](int X) -> int& { return M[X]; }); 46 *I = 42; 47 48 EXPECT_EQ(M[1], 42) << "assignment should have modified M"; 49 } 50 51 } // anonymous namespace 52