blob: 61e45e3a66eb105015f1440383cf09965cf835df [file] [log] [blame]
Lang Hames43e7b7a2017-11-10 17:41:28 +00001//===------ MappedIteratorTest.cpp - Unit tests for mapped_iterator -------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Lang Hames43e7b7a2017-11-10 17:41:28 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/ADT/STLExtras.h"
10#include "gtest/gtest.h"
11
12using namespace llvm;
13
14namespace {
15
16TEST(MappedIteratorTest, ApplyFunctionOnDereference) {
17 std::vector<int> V({0});
18
19 auto I = map_iterator(V.begin(), [](int X) { return X + 1; });
20
21 EXPECT_EQ(*I, 1) << "should have applied function in dereference";
22}
23
24TEST(MappedIteratorTest, ApplyFunctionOnArrow) {
25 struct S {
26 int Z = 0;
27 };
28
29 std::vector<int> V({0});
30 S Y;
31 S* P = &Y;
32
33 auto I = map_iterator(V.begin(), [&](int X) -> S& { return *(P + X); });
34
35 I->Z = 42;
36
37 EXPECT_EQ(Y.Z, 42) << "should have applied function during arrow";
38}
39
40TEST(MappedIteratorTest, FunctionPreservesReferences) {
41 std::vector<int> V({1});
42 std::map<int, int> M({ {1, 1} });
43
44 auto I = map_iterator(V.begin(), [&](int X) -> int& { return M[X]; });
45 *I = 42;
46
47 EXPECT_EQ(M[1], 42) << "assignment should have modified M";
48}
49
50} // anonymous namespace