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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
|
// Copyright 2019 The Abseil Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "absl/container/inlined_vector.h"
#include "absl/base/config.h"
#if defined(ABSL_HAVE_EXCEPTIONS)
#include <array>
#include <initializer_list>
#include <iterator>
#include <memory>
#include <utility>
#include "gtest/gtest.h"
#include "absl/base/internal/exception_safety_testing.h"
namespace {
constexpr size_t kInlinedCapacity = 4;
constexpr size_t kLargeSize = kInlinedCapacity * 2;
constexpr size_t kSmallSize = kInlinedCapacity / 2;
using Thrower = testing::ThrowingValue<>;
using MovableThrower = testing::ThrowingValue<testing::TypeSpec::kNoThrowMove>;
using ThrowAlloc = testing::ThrowingAllocator<Thrower>;
using ThrowerVec = absl::InlinedVector<Thrower, kInlinedCapacity>;
using MovableThrowerVec = absl::InlinedVector<MovableThrower, kInlinedCapacity>;
using ThrowAllocThrowerVec =
absl::InlinedVector<Thrower, kInlinedCapacity, ThrowAlloc>;
using ThrowAllocMovableThrowerVec =
absl::InlinedVector<MovableThrower, kInlinedCapacity, ThrowAlloc>;
// In GCC, if an element of a `std::initializer_list` throws during construction
// the elements that were constructed before it are not destroyed. This causes
// incorrect exception safety test failures. Thus, `testing::nothrow_ctor` is
// required. See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66139
#define ABSL_INTERNAL_MAKE_INIT_LIST(T, N) \
(N > kInlinedCapacity \
? std::initializer_list<T>{T(0, testing::nothrow_ctor), \
T(1, testing::nothrow_ctor), \
T(2, testing::nothrow_ctor), \
T(3, testing::nothrow_ctor), \
T(4, testing::nothrow_ctor), \
T(5, testing::nothrow_ctor), \
T(6, testing::nothrow_ctor), \
T(7, testing::nothrow_ctor)} \
\
: std::initializer_list<T>{T(0, testing::nothrow_ctor), \
T(1, testing::nothrow_ctor)})
static_assert(kLargeSize == 8, "Must update ABSL_INTERNAL_MAKE_INIT_LIST(...)");
static_assert(kSmallSize == 2, "Must update ABSL_INTERNAL_MAKE_INIT_LIST(...)");
template <typename TheVecT, size_t... TheSizes>
class TestParams {
public:
using VecT = TheVecT;
constexpr static size_t GetSizeAt(size_t i) { return kSizes[1 + i]; }
private:
constexpr static size_t kSizes[1 + sizeof...(TheSizes)] = {1, TheSizes...};
};
using NoSizeTestParams =
::testing::Types<TestParams<ThrowerVec>, TestParams<MovableThrowerVec>,
TestParams<ThrowAllocThrowerVec>,
TestParams<ThrowAllocMovableThrowerVec>>;
using OneSizeTestParams =
::testing::Types<TestParams<ThrowerVec, kLargeSize>,
TestParams<ThrowerVec, kSmallSize>,
TestParams<MovableThrowerVec, kLargeSize>,
TestParams<MovableThrowerVec, kSmallSize>,
TestParams<ThrowAllocThrowerVec, kLargeSize>,
TestParams<ThrowAllocThrowerVec, kSmallSize>,
TestParams<ThrowAllocMovableThrowerVec, kLargeSize>,
TestParams<ThrowAllocMovableThrowerVec, kSmallSize>>;
using TwoSizeTestParams = ::testing::Types<
TestParams<ThrowerVec, kLargeSize, kLargeSize>,
TestParams<ThrowerVec, kLargeSize, kSmallSize>,
TestParams<ThrowerVec, kSmallSize, kLargeSize>,
TestParams<ThrowerVec, kSmallSize, kSmallSize>,
TestParams<MovableThrowerVec, kLargeSize, kLargeSize>,
TestParams<MovableThrowerVec, kLargeSize, kSmallSize>,
TestParams<MovableThrowerVec, kSmallSize, kLargeSize>,
TestParams<MovableThrowerVec, kSmallSize, kSmallSize>,
TestParams<ThrowAllocThrowerVec, kLargeSize, kLargeSize>,
TestParams<ThrowAllocThrowerVec, kLargeSize, kSmallSize>,
TestParams<ThrowAllocThrowerVec, kSmallSize, kLargeSize>,
TestParams<ThrowAllocThrowerVec, kSmallSize, kSmallSize>,
TestParams<ThrowAllocMovableThrowerVec, kLargeSize, kLargeSize>,
TestParams<ThrowAllocMovableThrowerVec, kLargeSize, kSmallSize>,
TestParams<ThrowAllocMovableThrowerVec, kSmallSize, kLargeSize>,
TestParams<ThrowAllocMovableThrowerVec, kSmallSize, kSmallSize>>;
template <typename>
struct NoSizeTest : ::testing::Test {};
TYPED_TEST_SUITE(NoSizeTest, NoSizeTestParams);
template <typename>
struct OneSizeTest : ::testing::Test {};
TYPED_TEST_SUITE(OneSizeTest, OneSizeTestParams);
template <typename>
struct TwoSizeTest : ::testing::Test {};
TYPED_TEST_SUITE(TwoSizeTest, TwoSizeTestParams);
template <typename VecT>
bool InlinedVectorInvariants(VecT* vec) {
if (*vec != *vec) return false;
if (vec->size() > vec->capacity()) return false;
if (vec->size() > vec->max_size()) return false;
if (vec->capacity() > vec->max_size()) return false;
if (vec->data() != std::addressof(vec->at(0))) return false;
if (vec->data() != vec->begin()) return false;
if (*vec->data() != *vec->begin()) return false;
if (vec->begin() > vec->end()) return false;
if ((vec->end() - vec->begin()) != vec->size()) return false;
if (std::distance(vec->begin(), vec->end()) != vec->size()) return false;
return true;
}
// Function that always returns false is correct, but refactoring is required
// for clarity. It's needed to express that, as a contract, certain operations
// should not throw at all. Execution of this function means an exception was
// thrown and thus the test should fail.
// TODO(johnsoncj): Add `testing::NoThrowGuarantee` to the framework
template <typename VecT>
bool NoThrowGuarantee(VecT* /* vec */) {
return false;
}
TYPED_TEST(NoSizeTest, DefaultConstructor) {
using VecT = typename TypeParam::VecT;
using allocator_type = typename VecT::allocator_type;
testing::TestThrowingCtor<VecT>();
testing::TestThrowingCtor<VecT>(allocator_type{});
}
TYPED_TEST(OneSizeTest, SizeConstructor) {
using VecT = typename TypeParam::VecT;
using allocator_type = typename VecT::allocator_type;
constexpr static auto size = TypeParam::GetSizeAt(0);
testing::TestThrowingCtor<VecT>(size);
testing::TestThrowingCtor<VecT>(size, allocator_type{});
}
TYPED_TEST(OneSizeTest, SizeRefConstructor) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
using allocator_type = typename VecT::allocator_type;
constexpr static auto size = TypeParam::GetSizeAt(0);
testing::TestThrowingCtor<VecT>(size, value_type{});
testing::TestThrowingCtor<VecT>(size, value_type{}, allocator_type{});
}
TYPED_TEST(OneSizeTest, InitializerListConstructor) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
using allocator_type = typename VecT::allocator_type;
constexpr static auto size = TypeParam::GetSizeAt(0);
testing::TestThrowingCtor<VecT>(
ABSL_INTERNAL_MAKE_INIT_LIST(value_type, size));
testing::TestThrowingCtor<VecT>(
ABSL_INTERNAL_MAKE_INIT_LIST(value_type, size), allocator_type{});
}
TYPED_TEST(OneSizeTest, RangeConstructor) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
using allocator_type = typename VecT::allocator_type;
constexpr static auto size = TypeParam::GetSizeAt(0);
std::array<value_type, size> arr{};
testing::TestThrowingCtor<VecT>(arr.begin(), arr.end());
testing::TestThrowingCtor<VecT>(arr.begin(), arr.end(), allocator_type{});
}
TYPED_TEST(OneSizeTest, CopyConstructor) {
using VecT = typename TypeParam::VecT;
using allocator_type = typename VecT::allocator_type;
constexpr static auto size = TypeParam::GetSizeAt(0);
VecT other_vec{size};
testing::TestThrowingCtor<VecT>(other_vec);
testing::TestThrowingCtor<VecT>(other_vec, allocator_type{});
}
TYPED_TEST(OneSizeTest, MoveConstructor) {
using VecT = typename TypeParam::VecT;
using allocator_type = typename VecT::allocator_type;
constexpr static auto size = TypeParam::GetSizeAt(0);
if (!absl::allocator_is_nothrow<allocator_type>::value) {
testing::TestThrowingCtor<VecT>(VecT{size});
testing::TestThrowingCtor<VecT>(VecT{size}, allocator_type{});
}
}
TYPED_TEST(TwoSizeTest, Assign) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
constexpr static auto from_size = TypeParam::GetSizeAt(0);
constexpr static auto to_size = TypeParam::GetSizeAt(1);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{from_size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
*vec = ABSL_INTERNAL_MAKE_INIT_LIST(value_type, to_size);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
VecT other_vec{to_size};
*vec = other_vec;
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
VecT other_vec{to_size};
*vec = std::move(other_vec);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
value_type val{};
vec->assign(to_size, val);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
vec->assign(ABSL_INTERNAL_MAKE_INIT_LIST(value_type, to_size));
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
std::array<value_type, to_size> arr{};
vec->assign(arr.begin(), arr.end());
}));
}
TYPED_TEST(TwoSizeTest, Resize) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
constexpr static auto from_size = TypeParam::GetSizeAt(0);
constexpr static auto to_size = TypeParam::GetSizeAt(1);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{from_size})
.WithContracts(InlinedVectorInvariants<VecT>,
testing::strong_guarantee);
EXPECT_TRUE(tester.Test([](VecT* vec) {
vec->resize(to_size); //
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
vec->resize(to_size, value_type{}); //
}));
}
TYPED_TEST(OneSizeTest, Insert) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
constexpr static auto from_size = TypeParam::GetSizeAt(0);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{from_size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
vec->insert(it, value_type{});
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
vec->insert(it, value_type{});
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->end();
vec->insert(it, value_type{});
}));
}
TYPED_TEST(TwoSizeTest, Insert) {
using VecT = typename TypeParam::VecT;
using value_type = typename VecT::value_type;
constexpr static auto from_size = TypeParam::GetSizeAt(0);
constexpr static auto count = TypeParam::GetSizeAt(1);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{from_size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
vec->insert(it, count, value_type{});
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
vec->insert(it, count, value_type{});
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->end();
vec->insert(it, count, value_type{});
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
vec->insert(it, ABSL_INTERNAL_MAKE_INIT_LIST(value_type, count));
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
vec->insert(it, ABSL_INTERNAL_MAKE_INIT_LIST(value_type, count));
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->end();
vec->insert(it, ABSL_INTERNAL_MAKE_INIT_LIST(value_type, count));
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
std::array<value_type, count> arr{};
vec->insert(it, arr.begin(), arr.end());
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
std::array<value_type, count> arr{};
vec->insert(it, arr.begin(), arr.end());
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->end();
std::array<value_type, count> arr{};
vec->insert(it, arr.begin(), arr.end());
}));
}
TYPED_TEST(OneSizeTest, EmplaceBack) {
using VecT = typename TypeParam::VecT;
constexpr static auto size = TypeParam::GetSizeAt(0);
// For testing calls to `emplace_back(...)` that reallocate.
VecT full_vec{size};
full_vec.resize(full_vec.capacity());
// For testing calls to `emplace_back(...)` that don't reallocate.
VecT nonfull_vec{size};
nonfull_vec.reserve(size + 1);
auto tester = testing::MakeExceptionSafetyTester().WithContracts(
InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.WithInitialValue(nonfull_vec).Test([](VecT* vec) {
vec->emplace_back();
}));
EXPECT_TRUE(tester.WithInitialValue(full_vec).Test(
[](VecT* vec) { vec->emplace_back(); }));
}
TYPED_TEST(OneSizeTest, PopBack) {
using VecT = typename TypeParam::VecT;
constexpr static auto size = TypeParam::GetSizeAt(0);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{size})
.WithContracts(NoThrowGuarantee<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
vec->pop_back(); //
}));
}
TYPED_TEST(OneSizeTest, Erase) {
using VecT = typename TypeParam::VecT;
constexpr static auto size = TypeParam::GetSizeAt(0);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
vec->erase(it);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
vec->erase(it);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() - 1);
vec->erase(it);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
vec->erase(it, it);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
vec->erase(it, it);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() - 1);
vec->erase(it, it);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin();
vec->erase(it, it + 1);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() / 2);
vec->erase(it, it + 1);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
auto it = vec->begin() + (vec->size() - 1);
vec->erase(it, it + 1);
}));
}
TYPED_TEST(OneSizeTest, Clear) {
using VecT = typename TypeParam::VecT;
constexpr static auto size = TypeParam::GetSizeAt(0);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{size})
.WithContracts(NoThrowGuarantee<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
vec->clear(); //
}));
}
TYPED_TEST(TwoSizeTest, Reserve) {
using VecT = typename TypeParam::VecT;
constexpr static auto from_size = TypeParam::GetSizeAt(0);
constexpr static auto to_capacity = TypeParam::GetSizeAt(1);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{from_size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) { vec->reserve(to_capacity); }));
}
TYPED_TEST(OneSizeTest, ShrinkToFit) {
using VecT = typename TypeParam::VecT;
constexpr static auto size = TypeParam::GetSizeAt(0);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
vec->shrink_to_fit(); //
}));
}
TYPED_TEST(TwoSizeTest, Swap) {
using VecT = typename TypeParam::VecT;
constexpr static auto from_size = TypeParam::GetSizeAt(0);
constexpr static auto to_size = TypeParam::GetSizeAt(1);
auto tester = testing::MakeExceptionSafetyTester()
.WithInitialValue(VecT{from_size})
.WithContracts(InlinedVectorInvariants<VecT>);
EXPECT_TRUE(tester.Test([](VecT* vec) {
VecT other_vec{to_size};
vec->swap(other_vec);
}));
EXPECT_TRUE(tester.Test([](VecT* vec) {
using std::swap;
VecT other_vec{to_size};
swap(*vec, other_vec);
}));
}
} // namespace
#endif // defined(ABSL_HAVE_EXCEPTIONS)
|