From fba73aec7204803730f75df390c76cd56cdf2eb3 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Sun, 25 Dec 2016 08:22:50 +0000 Subject: [ADT] Add a generic concatenating iterator and range. This allows both defining convenience iterator/range accessors on types which walk across N different independent ranges within the object, and more direct and simple usages with range based for loops such as shown in the unittest. The same facilities are used for both. They end up quite small and simple as it happens. I've also switched an iterator on `Module` to use this. I would like to add another convenience iterator that includes even more sequences as part of it and seeing this one already present motivated me to actually abstract it away and introduce a general utility. Differential Revision: https://reviews.llvm.org/D28093 llvm-svn: 290512 --- llvm/unittests/ADT/STLExtrasTest.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'llvm/unittests/ADT/STLExtrasTest.cpp') diff --git a/llvm/unittests/ADT/STLExtrasTest.cpp b/llvm/unittests/ADT/STLExtrasTest.cpp index 76881296db6..d3bef6a2e05 100644 --- a/llvm/unittests/ADT/STLExtrasTest.cpp +++ b/llvm/unittests/ADT/STLExtrasTest.cpp @@ -10,6 +10,7 @@ #include "llvm/ADT/STLExtras.h" #include "gtest/gtest.h" +#include #include using namespace llvm; @@ -253,4 +254,26 @@ TEST(STLExtrasTest, CountAdaptor) { EXPECT_EQ(1, count(v, 3)); EXPECT_EQ(1, count(v, 4)); } + +TEST(STLExtrasTest, ConcatRange) { + std::vector Expected = {1, 2, 3, 4, 5, 6, 7, 8}; + std::vector Test; + + std::vector V1234 = {1, 2, 3, 4}; + std::list L56 = {5, 6}; + SmallVector SV78 = {7, 8}; + + // Use concat across different sized ranges of different types with different + // iterators. + for (int &i : concat(V1234, L56, SV78)) + Test.push_back(i); + EXPECT_EQ(Expected, Test); + + // Use concat between a temporary, an L-value, and an R-value to make sure + // complex lifetimes work well. + Test.clear(); + for (int &i : concat(std::vector(V1234), L56, std::move(SV78))) + Test.push_back(i); + EXPECT_EQ(Expected, Test); +} } -- cgit v1.2.3