summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/ADT/MappedIteratorTest.cpp
blob: 8c6a10306a87684fd4ca38341963a38b6f36069c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//===- llvm/unittest/ADT/APInt.cpp - APInt unit tests ---------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include <vector>

#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/iterator_range.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

template <class T, class Fn>
auto map_range(const T &range, Fn fn)
    -> decltype(make_range(map_iterator(range.begin(), fn),
                           map_iterator(range.end(), fn))) {
  return make_range(map_iterator(range.begin(), fn),
                    map_iterator(range.end(), fn));
}

static char add1(char C) { return C + 1; }

TEST(MappedIterator, FnTest) {
  std::string S("abc");
  std::string T;

  for (char C : map_range(S, add1)) {
    T.push_back(C);
  }

  EXPECT_STREQ("bcd", T.c_str());
}

TEST(MappedIterator, LambdaTest) {
  std::string S("abc");
  std::string T;

  for (char C : map_range(S, [](char C) { return C + 1; })) {
    T.push_back(C);
  }

  EXPECT_STREQ("bcd", T.c_str());
}
}
OpenPOWER on IntegriCloud