diff options
Diffstat (limited to 'llvm/unittests/ADT/MappedIteratorTest.cpp')
| -rw-r--r-- | llvm/unittests/ADT/MappedIteratorTest.cpp | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/MappedIteratorTest.cpp b/llvm/unittests/ADT/MappedIteratorTest.cpp new file mode 100644 index 00000000000..8c6a10306a8 --- /dev/null +++ b/llvm/unittests/ADT/MappedIteratorTest.cpp @@ -0,0 +1,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()); +} +} |

