2016-08-19 10:07:51 +08:00
|
|
|
//===- STLExtrasTest.cpp - Unit tests for STL extras ----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2016-12-26 07:41:14 +08:00
|
|
|
#include <list>
|
2016-09-30 23:43:59 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2016-08-19 10:07:51 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
int f(rank<0>) { return 0; }
|
|
|
|
int f(rank<1>) { return 1; }
|
|
|
|
int f(rank<2>) { return 2; }
|
|
|
|
int f(rank<4>) { return 4; }
|
|
|
|
|
|
|
|
TEST(STLExtrasTest, Rank) {
|
|
|
|
// We shouldn't get ambiguities and should select the overload of the same
|
|
|
|
// rank as the argument.
|
|
|
|
EXPECT_EQ(0, f(rank<0>()));
|
|
|
|
EXPECT_EQ(1, f(rank<1>()));
|
|
|
|
EXPECT_EQ(2, f(rank<2>()));
|
|
|
|
|
|
|
|
// This overload is missing so we end up back at 2.
|
|
|
|
EXPECT_EQ(2, f(rank<3>()));
|
|
|
|
|
|
|
|
// But going past 3 should work fine.
|
|
|
|
EXPECT_EQ(4, f(rank<4>()));
|
|
|
|
|
|
|
|
// And we can even go higher and just fall back to the last overload.
|
|
|
|
EXPECT_EQ(4, f(rank<5>()));
|
|
|
|
EXPECT_EQ(4, f(rank<6>()));
|
|
|
|
}
|
|
|
|
|
2016-10-06 00:54:09 +08:00
|
|
|
TEST(STLExtrasTest, EnumerateLValue) {
|
|
|
|
// Test that a simple LValue can be enumerated and gives correct results with
|
|
|
|
// multiple types, including the empty container.
|
2016-09-30 23:43:59 +08:00
|
|
|
std::vector<char> foo = {'a', 'b', 'c'};
|
2016-10-06 01:04:36 +08:00
|
|
|
typedef std::pair<std::size_t, char> CharPairType;
|
|
|
|
std::vector<CharPairType> CharResults;
|
2016-09-30 23:43:59 +08:00
|
|
|
|
|
|
|
for (auto X : llvm::enumerate(foo)) {
|
2017-03-14 00:24:10 +08:00
|
|
|
CharResults.emplace_back(X.index(), X.value());
|
2016-09-30 23:43:59 +08:00
|
|
|
}
|
2016-10-06 00:54:09 +08:00
|
|
|
ASSERT_EQ(3u, CharResults.size());
|
2016-10-06 01:04:36 +08:00
|
|
|
EXPECT_EQ(CharPairType(0u, 'a'), CharResults[0]);
|
|
|
|
EXPECT_EQ(CharPairType(1u, 'b'), CharResults[1]);
|
|
|
|
EXPECT_EQ(CharPairType(2u, 'c'), CharResults[2]);
|
2016-10-06 00:54:09 +08:00
|
|
|
|
|
|
|
// Test a const range of a different type.
|
2016-10-06 01:04:36 +08:00
|
|
|
typedef std::pair<std::size_t, int> IntPairType;
|
|
|
|
std::vector<IntPairType> IntResults;
|
2016-10-06 00:54:09 +08:00
|
|
|
const std::vector<int> bar = {1, 2, 3};
|
2016-09-30 23:43:59 +08:00
|
|
|
for (auto X : llvm::enumerate(bar)) {
|
2017-03-14 00:24:10 +08:00
|
|
|
IntResults.emplace_back(X.index(), X.value());
|
2016-09-30 23:43:59 +08:00
|
|
|
}
|
2016-10-06 00:54:09 +08:00
|
|
|
ASSERT_EQ(3u, IntResults.size());
|
2016-10-06 01:04:36 +08:00
|
|
|
EXPECT_EQ(IntPairType(0u, 1), IntResults[0]);
|
|
|
|
EXPECT_EQ(IntPairType(1u, 2), IntResults[1]);
|
|
|
|
EXPECT_EQ(IntPairType(2u, 3), IntResults[2]);
|
2016-10-06 00:54:09 +08:00
|
|
|
|
|
|
|
// Test an empty range.
|
|
|
|
IntResults.clear();
|
2017-03-10 07:48:58 +08:00
|
|
|
const std::vector<int> baz{};
|
2016-09-30 23:43:59 +08:00
|
|
|
for (auto X : llvm::enumerate(baz)) {
|
2017-03-14 00:24:10 +08:00
|
|
|
IntResults.emplace_back(X.index(), X.value());
|
2016-09-30 23:43:59 +08:00
|
|
|
}
|
2016-10-06 00:54:09 +08:00
|
|
|
EXPECT_TRUE(IntResults.empty());
|
2016-09-30 23:43:59 +08:00
|
|
|
}
|
|
|
|
|
2016-10-06 00:54:09 +08:00
|
|
|
TEST(STLExtrasTest, EnumerateModifyLValue) {
|
|
|
|
// Test that you can modify the underlying entries of an lvalue range through
|
|
|
|
// the enumeration iterator.
|
2016-09-30 23:43:59 +08:00
|
|
|
std::vector<char> foo = {'a', 'b', 'c'};
|
|
|
|
|
|
|
|
for (auto X : llvm::enumerate(foo)) {
|
2017-03-14 00:24:10 +08:00
|
|
|
++X.value();
|
2016-09-30 23:43:59 +08:00
|
|
|
}
|
|
|
|
EXPECT_EQ('b', foo[0]);
|
|
|
|
EXPECT_EQ('c', foo[1]);
|
|
|
|
EXPECT_EQ('d', foo[2]);
|
|
|
|
}
|
2016-10-06 00:54:09 +08:00
|
|
|
|
|
|
|
TEST(STLExtrasTest, EnumerateRValueRef) {
|
|
|
|
// Test that an rvalue can be enumerated.
|
2016-10-06 01:04:36 +08:00
|
|
|
typedef std::pair<std::size_t, int> PairType;
|
|
|
|
std::vector<PairType> Results;
|
2016-10-06 00:54:09 +08:00
|
|
|
|
|
|
|
auto Enumerator = llvm::enumerate(std::vector<int>{1, 2, 3});
|
|
|
|
|
|
|
|
for (auto X : llvm::enumerate(std::vector<int>{1, 2, 3})) {
|
2017-03-14 00:24:10 +08:00
|
|
|
Results.emplace_back(X.index(), X.value());
|
2016-10-06 00:54:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(3u, Results.size());
|
2016-10-06 01:04:36 +08:00
|
|
|
EXPECT_EQ(PairType(0u, 1), Results[0]);
|
|
|
|
EXPECT_EQ(PairType(1u, 2), Results[1]);
|
|
|
|
EXPECT_EQ(PairType(2u, 3), Results[2]);
|
2016-10-06 00:54:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(STLExtrasTest, EnumerateModifyRValue) {
|
|
|
|
// Test that when enumerating an rvalue, modification still works (even if
|
|
|
|
// this isn't terribly useful, it at least shows that we haven't snuck an
|
|
|
|
// extra const in there somewhere.
|
2016-10-06 01:04:36 +08:00
|
|
|
typedef std::pair<std::size_t, char> PairType;
|
|
|
|
std::vector<PairType> Results;
|
2016-10-06 00:54:09 +08:00
|
|
|
|
|
|
|
for (auto X : llvm::enumerate(std::vector<char>{'1', '2', '3'})) {
|
2017-03-14 00:24:10 +08:00
|
|
|
++X.value();
|
|
|
|
Results.emplace_back(X.index(), X.value());
|
2016-10-06 00:54:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT_EQ(3u, Results.size());
|
2016-10-06 01:04:36 +08:00
|
|
|
EXPECT_EQ(PairType(0u, '2'), Results[0]);
|
|
|
|
EXPECT_EQ(PairType(1u, '3'), Results[1]);
|
|
|
|
EXPECT_EQ(PairType(2u, '4'), Results[2]);
|
2016-10-06 00:54:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool B> struct CanMove {};
|
|
|
|
template <> struct CanMove<false> {
|
|
|
|
CanMove(CanMove &&) = delete;
|
|
|
|
|
|
|
|
CanMove() = default;
|
|
|
|
CanMove(const CanMove &) = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <bool B> struct CanCopy {};
|
|
|
|
template <> struct CanCopy<false> {
|
|
|
|
CanCopy(const CanCopy &) = delete;
|
|
|
|
|
|
|
|
CanCopy() = default;
|
2016-10-26 02:11:17 +08:00
|
|
|
CanCopy(CanCopy &&) = default;
|
2016-10-06 00:54:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <bool Moveable, bool Copyable>
|
|
|
|
struct Range : CanMove<Moveable>, CanCopy<Copyable> {
|
|
|
|
explicit Range(int &C, int &M, int &D) : C(C), M(M), D(D) {}
|
|
|
|
Range(const Range &R) : CanCopy<Copyable>(R), C(R.C), M(R.M), D(R.D) { ++C; }
|
|
|
|
Range(Range &&R) : CanMove<Moveable>(std::move(R)), C(R.C), M(R.M), D(R.D) {
|
|
|
|
++M;
|
|
|
|
}
|
|
|
|
~Range() { ++D; }
|
|
|
|
|
|
|
|
int &C;
|
|
|
|
int &M;
|
|
|
|
int &D;
|
|
|
|
|
|
|
|
int *begin() { return nullptr; }
|
|
|
|
int *end() { return nullptr; }
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(STLExtrasTest, EnumerateLifetimeSemantics) {
|
|
|
|
// Test that when enumerating lvalues and rvalues, there are no surprise
|
|
|
|
// copies or moves.
|
|
|
|
|
|
|
|
// With an rvalue, it should not be destroyed until the end of the scope.
|
|
|
|
int Copies = 0;
|
|
|
|
int Moves = 0;
|
|
|
|
int Destructors = 0;
|
|
|
|
{
|
|
|
|
auto E1 = enumerate(Range<true, false>(Copies, Moves, Destructors));
|
|
|
|
// Doesn't compile. rvalue ranges must be moveable.
|
|
|
|
// auto E2 = enumerate(Range<false, true>(Copies, Moves, Destructors));
|
|
|
|
EXPECT_EQ(0, Copies);
|
|
|
|
EXPECT_EQ(1, Moves);
|
|
|
|
EXPECT_EQ(1, Destructors);
|
|
|
|
}
|
|
|
|
EXPECT_EQ(0, Copies);
|
|
|
|
EXPECT_EQ(1, Moves);
|
|
|
|
EXPECT_EQ(2, Destructors);
|
|
|
|
|
|
|
|
Copies = Moves = Destructors = 0;
|
|
|
|
// With an lvalue, it should not be destroyed even after the end of the scope.
|
|
|
|
// lvalue ranges need be neither copyable nor moveable.
|
|
|
|
Range<false, false> R(Copies, Moves, Destructors);
|
|
|
|
{
|
|
|
|
auto Enumerator = enumerate(R);
|
|
|
|
(void)Enumerator;
|
|
|
|
EXPECT_EQ(0, Copies);
|
|
|
|
EXPECT_EQ(0, Moves);
|
|
|
|
EXPECT_EQ(0, Destructors);
|
|
|
|
}
|
|
|
|
EXPECT_EQ(0, Copies);
|
|
|
|
EXPECT_EQ(0, Moves);
|
|
|
|
EXPECT_EQ(0, Destructors);
|
|
|
|
}
|
2016-10-11 00:44:09 +08:00
|
|
|
|
|
|
|
TEST(STLExtrasTest, ApplyTuple) {
|
|
|
|
auto T = std::make_tuple(1, 3, 7);
|
2016-10-11 05:24:34 +08:00
|
|
|
auto U = llvm::apply_tuple(
|
2016-10-11 00:44:09 +08:00
|
|
|
[](int A, int B, int C) { return std::make_tuple(A - B, B - C, C - A); },
|
|
|
|
T);
|
|
|
|
|
|
|
|
EXPECT_EQ(-2, std::get<0>(U));
|
|
|
|
EXPECT_EQ(-4, std::get<1>(U));
|
|
|
|
EXPECT_EQ(6, std::get<2>(U));
|
|
|
|
|
2016-10-11 05:24:34 +08:00
|
|
|
auto V = llvm::apply_tuple(
|
2016-10-11 00:44:09 +08:00
|
|
|
[](int A, int B, int C) {
|
|
|
|
return std::make_tuple(std::make_pair(A, char('A' + A)),
|
|
|
|
std::make_pair(B, char('A' + B)),
|
|
|
|
std::make_pair(C, char('A' + C)));
|
|
|
|
},
|
|
|
|
T);
|
|
|
|
|
|
|
|
EXPECT_EQ(std::make_pair(1, 'B'), std::get<0>(V));
|
|
|
|
EXPECT_EQ(std::make_pair(3, 'D'), std::get<1>(V));
|
|
|
|
EXPECT_EQ(std::make_pair(7, 'H'), std::get<2>(V));
|
|
|
|
}
|
|
|
|
|
|
|
|
class apply_variadic {
|
|
|
|
static int apply_one(int X) { return X + 1; }
|
|
|
|
static char apply_one(char C) { return C + 1; }
|
|
|
|
static StringRef apply_one(StringRef S) { return S.drop_back(); }
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <typename... Ts>
|
|
|
|
auto operator()(Ts &&... Items)
|
|
|
|
-> decltype(std::make_tuple(apply_one(Items)...)) {
|
|
|
|
return std::make_tuple(apply_one(Items)...);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(STLExtrasTest, ApplyTupleVariadic) {
|
|
|
|
auto Items = std::make_tuple(1, llvm::StringRef("Test"), 'X');
|
2016-10-11 05:24:34 +08:00
|
|
|
auto Values = apply_tuple(apply_variadic(), Items);
|
2016-10-11 00:44:09 +08:00
|
|
|
|
|
|
|
EXPECT_EQ(2, std::get<0>(Values));
|
|
|
|
EXPECT_EQ("Tes", std::get<1>(Values));
|
|
|
|
EXPECT_EQ('Y', std::get<2>(Values));
|
|
|
|
}
|
2016-12-04 18:26:53 +08:00
|
|
|
|
|
|
|
TEST(STLExtrasTest, CountAdaptor) {
|
|
|
|
std::vector<int> v;
|
|
|
|
|
|
|
|
v.push_back(1);
|
|
|
|
v.push_back(2);
|
|
|
|
v.push_back(1);
|
|
|
|
v.push_back(4);
|
|
|
|
v.push_back(3);
|
|
|
|
v.push_back(2);
|
|
|
|
v.push_back(1);
|
|
|
|
|
|
|
|
EXPECT_EQ(3, count(v, 1));
|
|
|
|
EXPECT_EQ(2, count(v, 2));
|
|
|
|
EXPECT_EQ(1, count(v, 3));
|
2017-11-21 06:12:55 +08:00
|
|
|
EXPECT_EQ(1, count(v, 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(STLExtrasTest, for_each) {
|
|
|
|
std::vector<int> v{0, 1, 2, 3, 4};
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
llvm::for_each(v, [&count](int) { ++count; });
|
|
|
|
EXPECT_EQ(5, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(STLExtrasTest, ToVector) {
|
|
|
|
std::vector<char> v = {'a', 'b', 'c'};
|
|
|
|
auto Enumerated = to_vector<4>(enumerate(v));
|
2017-03-14 05:46:12 +08:00
|
|
|
ASSERT_EQ(3u, Enumerated.size());
|
2017-03-14 00:24:10 +08:00
|
|
|
for (size_t I = 0; I < v.size(); ++I) {
|
|
|
|
EXPECT_EQ(I, Enumerated[I].index());
|
|
|
|
EXPECT_EQ(v[I], Enumerated[I].value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-26 07:41:14 +08:00
|
|
|
TEST(STLExtrasTest, ConcatRange) {
|
|
|
|
std::vector<int> Expected = {1, 2, 3, 4, 5, 6, 7, 8};
|
|
|
|
std::vector<int> Test;
|
|
|
|
|
|
|
|
std::vector<int> V1234 = {1, 2, 3, 4};
|
|
|
|
std::list<int> L56 = {5, 6};
|
|
|
|
SmallVector<int, 2> SV78 = {7, 8};
|
|
|
|
|
|
|
|
// Use concat across different sized ranges of different types with different
|
|
|
|
// iterators.
|
|
|
|
for (int &i : concat<int>(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<int>(std::vector<int>(V1234), L56, std::move(SV78)))
|
|
|
|
Test.push_back(i);
|
|
|
|
EXPECT_EQ(Expected, Test);
|
|
|
|
}
|
2016-12-27 07:10:40 +08:00
|
|
|
|
|
|
|
TEST(STLExtrasTest, PartitionAdaptor) {
|
|
|
|
std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8};
|
|
|
|
|
|
|
|
auto I = partition(V, [](int i) { return i % 2 == 0; });
|
|
|
|
ASSERT_EQ(V.begin() + 4, I);
|
|
|
|
|
|
|
|
// Sort the two halves as partition may have messed with the order.
|
|
|
|
std::sort(V.begin(), I);
|
|
|
|
std::sort(I, V.end());
|
|
|
|
|
|
|
|
EXPECT_EQ(2, V[0]);
|
|
|
|
EXPECT_EQ(4, V[1]);
|
|
|
|
EXPECT_EQ(6, V[2]);
|
|
|
|
EXPECT_EQ(8, V[3]);
|
|
|
|
EXPECT_EQ(1, V[4]);
|
|
|
|
EXPECT_EQ(3, V[5]);
|
|
|
|
EXPECT_EQ(5, V[6]);
|
|
|
|
EXPECT_EQ(7, V[7]);
|
|
|
|
}
|
|
|
|
|
2016-12-27 07:30:44 +08:00
|
|
|
TEST(STLExtrasTest, EraseIf) {
|
|
|
|
std::vector<int> V = {1, 2, 3, 4, 5, 6, 7, 8};
|
|
|
|
|
|
|
|
erase_if(V, [](int i) { return i % 2 == 0; });
|
|
|
|
EXPECT_EQ(4u, V.size());
|
|
|
|
EXPECT_EQ(1, V[0]);
|
|
|
|
EXPECT_EQ(3, V[1]);
|
|
|
|
EXPECT_EQ(5, V[2]);
|
|
|
|
EXPECT_EQ(7, V[3]);
|
|
|
|
}
|
|
|
|
|
2017-11-21 06:12:55 +08:00
|
|
|
namespace some_namespace {
|
|
|
|
struct some_struct {
|
|
|
|
std::vector<int> data;
|
|
|
|
std::string swap_val;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<int>::const_iterator begin(const some_struct &s) {
|
|
|
|
return s.data.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int>::const_iterator end(const some_struct &s) {
|
|
|
|
return s.data.end();
|
2016-08-19 10:07:51 +08:00
|
|
|
}
|
2017-11-21 06:12:55 +08:00
|
|
|
|
|
|
|
void swap(some_struct &lhs, some_struct &rhs) {
|
|
|
|
// make swap visible as non-adl swap would even seem to
|
|
|
|
// work with std::swap which defaults to moving
|
|
|
|
lhs.swap_val = "lhs";
|
|
|
|
rhs.swap_val = "rhs";
|
|
|
|
}
|
|
|
|
} // namespace some_namespace
|
|
|
|
|
|
|
|
TEST(STLExtrasTest, ADLTest) {
|
|
|
|
some_namespace::some_struct s{{1, 2, 3, 4, 5}, ""};
|
|
|
|
some_namespace::some_struct s2{{2, 4, 6, 8, 10}, ""};
|
|
|
|
|
|
|
|
EXPECT_EQ(*adl_begin(s), 1);
|
|
|
|
EXPECT_EQ(*(adl_end(s) - 1), 5);
|
|
|
|
|
|
|
|
adl_swap(s, s2);
|
|
|
|
EXPECT_EQ(s.swap_val, "lhs");
|
|
|
|
EXPECT_EQ(s2.swap_val, "rhs");
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
llvm::for_each(s, [&count](int) { ++count; });
|
|
|
|
EXPECT_EQ(5, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|