fixed_array_exception_safety_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include <initializer_list>
00016 
00017 #include "absl/container/fixed_array.h"
00018 
00019 #include "gtest/gtest.h"
00020 #include "absl/base/internal/exception_safety_testing.h"
00021 
00022 namespace absl {
00023 
00024 namespace {
00025 
00026 constexpr size_t kInlined = 25;
00027 constexpr size_t kSmallSize = kInlined / 2;
00028 constexpr size_t kLargeSize = kInlined * 2;
00029 
00030 constexpr int kInitialValue = 5;
00031 constexpr int kUpdatedValue = 10;
00032 
00033 using ::testing::TestThrowingCtor;
00034 
00035 using Thrower = testing::ThrowingValue<testing::TypeSpec::kEverythingThrows>;
00036 using FixedArr = absl::FixedArray<Thrower, kInlined>;
00037 
00038 using MoveThrower = testing::ThrowingValue<testing::TypeSpec::kNoThrowMove>;
00039 using MoveFixedArr = absl::FixedArray<MoveThrower, kInlined>;
00040 
00041 TEST(FixedArrayExceptionSafety, CopyConstructor) {
00042   auto small = FixedArr(kSmallSize);
00043   TestThrowingCtor<FixedArr>(small);
00044 
00045   auto large = FixedArr(kLargeSize);
00046   TestThrowingCtor<FixedArr>(large);
00047 }
00048 
00049 TEST(FixedArrayExceptionSafety, MoveConstructor) {
00050   TestThrowingCtor<FixedArr>(FixedArr(kSmallSize));
00051   TestThrowingCtor<FixedArr>(FixedArr(kLargeSize));
00052 
00053   // TypeSpec::kNoThrowMove
00054   TestThrowingCtor<MoveFixedArr>(MoveFixedArr(kSmallSize));
00055   TestThrowingCtor<MoveFixedArr>(MoveFixedArr(kLargeSize));
00056 }
00057 
00058 TEST(FixedArrayExceptionSafety, SizeConstructor) {
00059   TestThrowingCtor<FixedArr>(kSmallSize);
00060   TestThrowingCtor<FixedArr>(kLargeSize);
00061 }
00062 
00063 TEST(FixedArrayExceptionSafety, SizeValueConstructor) {
00064   TestThrowingCtor<FixedArr>(kSmallSize, Thrower());
00065   TestThrowingCtor<FixedArr>(kLargeSize, Thrower());
00066 }
00067 
00068 TEST(FixedArrayExceptionSafety, IteratorConstructor) {
00069   auto small = FixedArr(kSmallSize);
00070   TestThrowingCtor<FixedArr>(small.begin(), small.end());
00071 
00072   auto large = FixedArr(kLargeSize);
00073   TestThrowingCtor<FixedArr>(large.begin(), large.end());
00074 }
00075 
00076 TEST(FixedArrayExceptionSafety, InitListConstructor) {
00077   constexpr int small_inlined = 3;
00078   using SmallFixedArr = absl::FixedArray<Thrower, small_inlined>;
00079 
00080   TestThrowingCtor<SmallFixedArr>(std::initializer_list<Thrower>{});
00081   // Test inlined allocation
00082   TestThrowingCtor<SmallFixedArr>(
00083       std::initializer_list<Thrower>{Thrower{}, Thrower{}});
00084   // Test out of line allocation
00085   TestThrowingCtor<SmallFixedArr>(std::initializer_list<Thrower>{
00086       Thrower{}, Thrower{}, Thrower{}, Thrower{}, Thrower{}});
00087 }
00088 
00089 testing::AssertionResult ReadMemory(FixedArr* fixed_arr) {
00090   // Marked volatile to prevent optimization. Used for running asan tests.
00091   volatile int sum = 0;
00092   for (const auto& thrower : *fixed_arr) {
00093     sum += thrower.Get();
00094   }
00095   return testing::AssertionSuccess() << "Values sum to [" << sum << "]";
00096 }
00097 
00098 TEST(FixedArrayExceptionSafety, Fill) {
00099   auto test_fill = testing::MakeExceptionSafetyTester()
00100                        .WithContracts(ReadMemory)
00101                        .WithOperation([&](FixedArr* fixed_arr_ptr) {
00102                          auto thrower =
00103                              Thrower(kUpdatedValue, testing::nothrow_ctor);
00104                          fixed_arr_ptr->fill(thrower);
00105                        });
00106 
00107   EXPECT_TRUE(
00108       test_fill.WithInitialValue(FixedArr(kSmallSize, Thrower(kInitialValue)))
00109           .Test());
00110   EXPECT_TRUE(
00111       test_fill.WithInitialValue(FixedArr(kLargeSize, Thrower(kInitialValue)))
00112           .Test());
00113 }
00114 
00115 }  // namespace
00116 
00117 }  // namespace absl


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:14