/* * 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 #include #include #include "test/gmock.h" #include "test/gtest.h" namespace webrtc { namespace { using SmallTrivial = BoundedInlineVector; using LargeTrivial = BoundedInlineVector; using NonTrivial = BoundedInlineVector; static_assert(std::is_trivially_copyable::value, ""); static_assert(!std::is_trivially_copyable::value, ""); static_assert(std::is_trivially_destructible::value, ""); static_assert(!std::is_trivially_copyable::value, ""); static_assert(!std::is_trivially_destructible::value, ""); template class BoundedInlineVectorTestAllTypes : public ::testing::Test {}; using AllTypes = ::testing::Types, // Trivial nonprimitive type. std::unique_ptr, // Move-only type. std::string>; // Nontrivial copyable type. TYPED_TEST_SUITE(BoundedInlineVectorTestAllTypes, AllTypes); template class BoundedInlineVectorTestCopyableTypes : public ::testing::Test {}; using CopyableTypes = ::testing::Types, std::string>; TYPED_TEST_SUITE(BoundedInlineVectorTestCopyableTypes, CopyableTypes); TYPED_TEST(BoundedInlineVectorTestAllTypes, ConstructEmpty) { BoundedInlineVector x; EXPECT_EQ(x.size(), 0); EXPECT_EQ(x.begin(), x.end()); static_assert(x.capacity() == 3, ""); } TYPED_TEST(BoundedInlineVectorTestAllTypes, ConstructNonempty) { BoundedInlineVector x = {TypeParam(), TypeParam()}; EXPECT_EQ(x.size(), 2); static_assert(x.capacity() == 3, ""); } TYPED_TEST(BoundedInlineVectorTestCopyableTypes, CopyConstruct) { BoundedInlineVector x = {TypeParam(), TypeParam()}; BoundedInlineVector y = x; EXPECT_EQ(y.size(), 2); static_assert(x.capacity() == 3, ""); static_assert(y.capacity() == 2, ""); } TYPED_TEST(BoundedInlineVectorTestCopyableTypes, CopyAssign) { BoundedInlineVector x = {TypeParam(), TypeParam()}; BoundedInlineVector y; EXPECT_EQ(y.size(), 0); y = x; EXPECT_EQ(y.size(), 2); } TYPED_TEST(BoundedInlineVectorTestAllTypes, MoveConstruct) { BoundedInlineVector x = {TypeParam(), TypeParam()}; BoundedInlineVector y = std::move(x); EXPECT_EQ(y.size(), 2); static_assert(x.capacity() == 3, ""); static_assert(y.capacity() == 2, ""); } TYPED_TEST(BoundedInlineVectorTestAllTypes, MoveAssign) { BoundedInlineVector x = {TypeParam(), TypeParam()}; BoundedInlineVector y; EXPECT_EQ(y.size(), 0); y = std::move(x); EXPECT_EQ(y.size(), 2); } TEST(BoundedInlineVectorTestOneType, Iteration) { BoundedInlineVector sv{"one", "two", "three", "four"}; std::string cat; for (const auto& s : sv) { cat += s; } EXPECT_EQ(cat, "onetwothreefour"); } TEST(BoundedInlineVectorTestOneType, Indexing) { BoundedInlineVector x = {3.14}; EXPECT_EQ(x[0], 3.14); } template BoundedInlineVector Returns(Ts... values) { return {std::forward(values)...}; } TYPED_TEST(BoundedInlineVectorTestAllTypes, Return) { EXPECT_EQ((Returns().size()), 0); EXPECT_EQ((Returns(TypeParam(), TypeParam()).size()), 2); } TYPED_TEST(BoundedInlineVectorTestAllTypes, Resize) { BoundedInlineVector x; EXPECT_EQ(x.size(), 0); x.resize(17); EXPECT_EQ(x.size(), 17); // Test one arbitrary element, mostly to give MSan a chance to scream. But if // the type has a trivial default constructor we can't, because the element // won't be initialized. if (!std::is_trivially_default_constructible::value) { EXPECT_EQ(x[4], TypeParam()); } x.resize(2); EXPECT_EQ(x.size(), 2); } } // namespace } // namespace webrtc