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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/containers/extend.h"
#include <initializer_list>
#include <type_traits>
#include <utility>
#include <vector>
#include "base/containers/adapters.h"
#include "base/containers/circular_deque.h"
#include "base/containers/span.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
using testing::ElementsAre;
struct NonCopyable {
char c_;
explicit NonCopyable(char c) : c_(c) {}
NonCopyable(NonCopyable&&) = default;
NonCopyable& operator=(NonCopyable&& other) = default;
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
};
bool operator==(const NonCopyable& a, const NonCopyable& b) {
return a.c_ == b.c_;
}
static_assert(std::is_move_constructible_v<NonCopyable>, "");
static_assert(!std::is_copy_constructible_v<NonCopyable>, "");
struct CopyableMovable {
bool copied_ = false;
char c_;
explicit CopyableMovable(char c) : c_(c) {}
CopyableMovable(const CopyableMovable& other) : copied_(true), c_(other.c_) {}
CopyableMovable& operator=(const CopyableMovable&) = default;
CopyableMovable(CopyableMovable&&) = default;
CopyableMovable& operator=(CopyableMovable&& other) = default;
};
bool operator==(const CopyableMovable& a, const CopyableMovable& b) {
return a.c_ == b.c_;
}
} // namespace
TEST(ExtendTest, ExtendWithMove) {
std::vector<NonCopyable> dst;
for (char c : {'a', 'b', 'c', 'd'}) {
dst.emplace_back(c);
}
std::vector<NonCopyable> src;
for (char c : {'e', 'f', 'g'}) {
src.emplace_back(c);
}
std::vector<NonCopyable> expected;
for (char c : {'a', 'b', 'c', 'd', 'e', 'f', 'g'}) {
expected.emplace_back(c);
}
Extend(dst, std::move(src));
EXPECT_EQ(dst, expected);
EXPECT_TRUE(src.empty()); // NOLINT(bugprone-use-after-move)
}
TEST(ExtendTest, ExtendCopyableWithMove) {
std::vector<CopyableMovable> dst;
for (char c : {'a', 'b', 'c', 'd'}) {
dst.emplace_back(c);
}
std::vector<CopyableMovable> src;
for (char c : {'e', 'f', 'g'}) {
src.emplace_back(c);
}
std::vector<CopyableMovable> expected;
for (char c : {'a', 'b', 'c', 'd', 'e', 'f', 'g'}) {
expected.emplace_back(c);
}
Extend(dst, std::move(src));
EXPECT_EQ(dst, expected);
EXPECT_TRUE(src.empty()); // NOLINT(bugprone-use-after-move)
}
TEST(ExtendTest, ExtendWithCopy) {
std::vector<CopyableMovable> dst;
for (char c : {'a', 'b', 'c', 'd'}) {
dst.emplace_back(c);
}
std::vector<CopyableMovable> src;
for (char c : {'e', 'f', 'g'}) {
src.emplace_back(c);
}
std::vector<CopyableMovable> expected;
for (char c : {'a', 'b', 'c', 'd', 'e', 'f', 'g'}) {
expected.emplace_back(c);
}
EXPECT_FALSE(dst[0].copied_);
Extend(dst, src);
EXPECT_EQ(dst, expected);
EXPECT_FALSE(dst[0].copied_);
EXPECT_TRUE(dst[dst.size() - 1].copied_);
}
TEST(ExtendTest, ExtendWithSpan) {
static constexpr uint8_t kRawArray[] = {3, 4, 5};
static constexpr auto kVectorGenerator = []() -> std::vector<uint8_t> {
return {9, 10, 11};
};
static const std::vector<uint8_t> kConstVector = kVectorGenerator();
static std::vector<uint8_t> kMutVector = kVectorGenerator();
std::vector<uint8_t> dst;
// Selects overload for span<const uint8_t, 3>.
Extend(dst, span(kRawArray));
EXPECT_THAT(dst, ElementsAre(3, 4, 5));
// Selects overload for span<uint8_t, 3>.
static std::array<uint8_t, 3> kArray = {6, 7, 8};
Extend(dst, span(kArray));
EXPECT_THAT(dst, ElementsAre(3, 4, 5, 6, 7, 8));
// Selects overload for span<const uint8_t, dynamic_extent>.
Extend(dst, span(kConstVector));
EXPECT_THAT(dst, ElementsAre(3, 4, 5, 6, 7, 8, 9, 10, 11));
// Selects overload for span<uint8_t, dynamic_extent>.
Extend(dst, span(kMutVector));
EXPECT_THAT(dst, ElementsAre(3, 4, 5, 6, 7, 8, 9, 10, 11, 9, 10, 11));
// Input is convertible to span.
Extend(dst, kRawArray);
EXPECT_THAT(dst,
ElementsAre(3, 4, 5, 6, 7, 8, 9, 10, 11, 9, 10, 11, 3, 4, 5));
}
TEST(ExtendTest, ExtendWithRanges) {
std::set<int> set = {1, 2, 3};
circular_deque<int> deque = {4, 5, 6};
{
std::vector<int> dst;
Extend(dst, Reversed(set));
EXPECT_THAT(dst, ElementsAre(3, 2, 1));
Extend(dst, Reversed(deque));
EXPECT_THAT(dst, ElementsAre(3, 2, 1, 6, 5, 4));
}
{
std::vector<int> dst;
Extend(dst, set, [](int val) { return -val; });
EXPECT_THAT(dst, ElementsAre(-1, -2, -3));
Extend(dst, deque, [](int val) { return val + 1; });
EXPECT_THAT(dst, ElementsAre(-1, -2, -3, 5, 6, 7));
}
{
std::vector<int> dst;
Extend(dst, Reversed(set), [](int val) { return val * val; });
EXPECT_THAT(dst, ElementsAre(9, 4, 1));
Extend(dst, Reversed(deque), [](int val) { return val - 1; });
EXPECT_THAT(dst, ElementsAre(9, 4, 1, 5, 4, 3));
}
}
} // namespace base
|