Selling point is that it never touches the heap. Intended use case is cheaply returning a variable, bounded, and small number of things from a function. Specifically, there are situations where we'd like to return things like ArrayView<ArrayView<float>> where we currently have to allocate an array of ArrayView<float> for the outer ArrayView to point to, which is a bother; however, although the outer ArrayView is of variable size, that size is statically guaranteed to not exceed some small constant. After this CL, we'll be able to instead return BoundedInlineVector<ArrayView<float>, kSmallConstant> which is much more convenient. We already had the option of returning e.g. std::vector<ArrayView<float>> but that would bloat our binary with code to handle heap allocations in places we'd rather be lean and mean. https://godbolt.org/z/r-vcPj demonstrates that the overhead compared to a raw C array + a size is ~zero. Bug: webrtc:11391 Change-Id: Ifb6d937193052588be641aa62cc67ba0ec64ded6 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/168944 Commit-Queue: Karl Wiberg <kwiberg@webrtc.org> Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Reviewed-by: Per Åhgren <peah@webrtc.org> Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/master@{#30663}
119 lines
3.9 KiB
C++
119 lines
3.9 KiB
C++
/*
|
|
* Copyright 2020 The WebRTC Project Authors. All rights reserved.
|
|
*
|
|
* Use of this source code is governed by a BSD-style license
|
|
* that can be found in the LICENSE file in the root of the source
|
|
* tree. An additional intellectual property rights grant can be found
|
|
* in the file PATENTS. All contributing project authors may
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
*/
|
|
|
|
#include "rtc_base/bounded_inline_vector.h"
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include "test/gmock.h"
|
|
#include "test/gtest.h"
|
|
|
|
namespace webrtc {
|
|
namespace {
|
|
|
|
using SmallTrivial = BoundedInlineVector<int, 2>;
|
|
using LargeTrivial = BoundedInlineVector<int, 200>;
|
|
using NonTrivial = BoundedInlineVector<std::string, 2>;
|
|
static_assert(std::is_trivially_copyable<SmallTrivial>::value, "");
|
|
static_assert(!std::is_trivially_copyable<LargeTrivial>::value, "");
|
|
static_assert(std::is_trivially_destructible<LargeTrivial>::value, "");
|
|
static_assert(!std::is_trivially_copyable<NonTrivial>::value, "");
|
|
static_assert(!std::is_trivially_destructible<NonTrivial>::value, "");
|
|
|
|
template <typename T>
|
|
class BoundedInlineVectorTestAllTypes : public ::testing::Test {};
|
|
|
|
using AllTypes =
|
|
::testing::Types<int, // Scalar type.
|
|
std::pair<int, float>, // Trivial nonprimitive type.
|
|
std::unique_ptr<int>, // Move-only type.
|
|
std::string>; // Nontrivial copyable type.
|
|
TYPED_TEST_SUITE(BoundedInlineVectorTestAllTypes, AllTypes);
|
|
|
|
template <typename T>
|
|
class BoundedInlineVectorTestCopyableTypes : public ::testing::Test {};
|
|
|
|
using CopyableTypes = ::testing::Types<int, std::pair<int, float>, std::string>;
|
|
TYPED_TEST_SUITE(BoundedInlineVectorTestCopyableTypes, CopyableTypes);
|
|
|
|
TYPED_TEST(BoundedInlineVectorTestAllTypes, ConstructEmpty) {
|
|
BoundedInlineVector<TypeParam, 3> x;
|
|
EXPECT_EQ(x.size(), 0);
|
|
EXPECT_EQ(x.begin(), x.end());
|
|
static_assert(x.capacity() == 3, "");
|
|
}
|
|
|
|
TYPED_TEST(BoundedInlineVectorTestAllTypes, ConstructNonempty) {
|
|
BoundedInlineVector<TypeParam, 3> x = {TypeParam(), TypeParam()};
|
|
EXPECT_EQ(x.size(), 2);
|
|
static_assert(x.capacity() == 3, "");
|
|
}
|
|
|
|
TYPED_TEST(BoundedInlineVectorTestCopyableTypes, CopyConstruct) {
|
|
BoundedInlineVector<TypeParam, 3> x = {TypeParam(), TypeParam()};
|
|
BoundedInlineVector<TypeParam, 2> y = x;
|
|
EXPECT_EQ(y.size(), 2);
|
|
static_assert(x.capacity() == 3, "");
|
|
static_assert(y.capacity() == 2, "");
|
|
}
|
|
|
|
TYPED_TEST(BoundedInlineVectorTestCopyableTypes, CopyAssign) {
|
|
BoundedInlineVector<TypeParam, 3> x = {TypeParam(), TypeParam()};
|
|
BoundedInlineVector<TypeParam, 2> y;
|
|
EXPECT_EQ(y.size(), 0);
|
|
y = x;
|
|
EXPECT_EQ(y.size(), 2);
|
|
}
|
|
|
|
TYPED_TEST(BoundedInlineVectorTestAllTypes, MoveConstruct) {
|
|
BoundedInlineVector<TypeParam, 3> x = {TypeParam(), TypeParam()};
|
|
BoundedInlineVector<TypeParam, 2> y = std::move(x);
|
|
EXPECT_EQ(y.size(), 2);
|
|
static_assert(x.capacity() == 3, "");
|
|
static_assert(y.capacity() == 2, "");
|
|
}
|
|
|
|
TYPED_TEST(BoundedInlineVectorTestAllTypes, MoveAssign) {
|
|
BoundedInlineVector<TypeParam, 3> x = {TypeParam(), TypeParam()};
|
|
BoundedInlineVector<TypeParam, 2> y;
|
|
EXPECT_EQ(y.size(), 0);
|
|
y = std::move(x);
|
|
EXPECT_EQ(y.size(), 2);
|
|
}
|
|
|
|
TEST(BoundedInlineVectorTestOneType, Iteration) {
|
|
BoundedInlineVector<std::string, 4> sv{"one", "two", "three", "four"};
|
|
std::string cat;
|
|
for (const auto& s : sv) {
|
|
cat += s;
|
|
}
|
|
EXPECT_EQ(cat, "onetwothreefour");
|
|
}
|
|
|
|
TEST(BoundedInlineVectorTestOneType, Indexing) {
|
|
BoundedInlineVector<double, 1> x = {3.14};
|
|
EXPECT_EQ(x[0], 3.14);
|
|
}
|
|
|
|
template <typename T, int capacity, typename... Ts>
|
|
BoundedInlineVector<T, capacity> Returns(Ts... values) {
|
|
return {std::forward<Ts>(values)...};
|
|
}
|
|
|
|
TYPED_TEST(BoundedInlineVectorTestAllTypes, Return) {
|
|
EXPECT_EQ((Returns<TypeParam, 3>().size()), 0);
|
|
EXPECT_EQ((Returns<TypeParam, 3>(TypeParam(), TypeParam()).size()), 2);
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace webrtc
|