fixed_array_exception_safety_test.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <initializer_list>
16 
18 
19 #include "gtest/gtest.h"
21 
22 namespace absl {
23 
24 namespace {
25 
26 constexpr size_t kInlined = 25;
27 constexpr size_t kSmallSize = kInlined / 2;
28 constexpr size_t kLargeSize = kInlined * 2;
29 
30 constexpr int kInitialValue = 5;
31 constexpr int kUpdatedValue = 10;
32 
34 
37 
39 using MoveFixedArr = absl::FixedArray<MoveThrower, kInlined>;
40 
41 TEST(FixedArrayExceptionSafety, CopyConstructor) {
42  auto small = FixedArr(kSmallSize);
43  TestThrowingCtor<FixedArr>(small);
44 
45  auto large = FixedArr(kLargeSize);
46  TestThrowingCtor<FixedArr>(large);
47 }
48 
49 TEST(FixedArrayExceptionSafety, MoveConstructor) {
50  TestThrowingCtor<FixedArr>(FixedArr(kSmallSize));
51  TestThrowingCtor<FixedArr>(FixedArr(kLargeSize));
52 
53  // TypeSpec::kNoThrowMove
54  TestThrowingCtor<MoveFixedArr>(MoveFixedArr(kSmallSize));
55  TestThrowingCtor<MoveFixedArr>(MoveFixedArr(kLargeSize));
56 }
57 
58 TEST(FixedArrayExceptionSafety, SizeConstructor) {
59  TestThrowingCtor<FixedArr>(kSmallSize);
60  TestThrowingCtor<FixedArr>(kLargeSize);
61 }
62 
63 TEST(FixedArrayExceptionSafety, SizeValueConstructor) {
64  TestThrowingCtor<FixedArr>(kSmallSize, Thrower());
65  TestThrowingCtor<FixedArr>(kLargeSize, Thrower());
66 }
67 
68 TEST(FixedArrayExceptionSafety, IteratorConstructor) {
69  auto small = FixedArr(kSmallSize);
70  TestThrowingCtor<FixedArr>(small.begin(), small.end());
71 
72  auto large = FixedArr(kLargeSize);
73  TestThrowingCtor<FixedArr>(large.begin(), large.end());
74 }
75 
76 TEST(FixedArrayExceptionSafety, InitListConstructor) {
77  constexpr int small_inlined = 3;
78  using SmallFixedArr = absl::FixedArray<Thrower, small_inlined>;
79 
80  TestThrowingCtor<SmallFixedArr>(std::initializer_list<Thrower>{});
81  // Test inlined allocation
82  TestThrowingCtor<SmallFixedArr>(
83  std::initializer_list<Thrower>{Thrower{}, Thrower{}});
84  // Test out of line allocation
85  TestThrowingCtor<SmallFixedArr>(std::initializer_list<Thrower>{
86  Thrower{}, Thrower{}, Thrower{}, Thrower{}, Thrower{}});
87 }
88 
89 testing::AssertionResult ReadMemory(FixedArr* fixed_arr) {
90  // Marked volatile to prevent optimization. Used for running asan tests.
91  volatile int sum = 0;
92  for (const auto& thrower : *fixed_arr) {
93  sum += thrower.Get();
94  }
95  return testing::AssertionSuccess() << "Values sum to [" << sum << "]";
96 }
97 
98 TEST(FixedArrayExceptionSafety, Fill) {
99  auto test_fill = testing::MakeExceptionSafetyTester()
100  .WithContracts(ReadMemory)
101  .WithOperation([&](FixedArr* fixed_arr_ptr) {
102  auto thrower =
103  Thrower(kUpdatedValue, testing::nothrow_ctor);
104  fixed_arr_ptr->fill(thrower);
105  });
106 
107  EXPECT_TRUE(
108  test_fill.WithInitialValue(FixedArr(kSmallSize, Thrower(kInitialValue)))
109  .Test());
110  EXPECT_TRUE(
111  test_fill.WithInitialValue(FixedArr(kLargeSize, Thrower(kInitialValue)))
112  .Test());
113 }
114 
115 } // namespace
116 
117 } // namespace absl
ExceptionSafetyTestBuilder< Factory, Operation, Contracts..., absl::decay_t< MoreContracts >... > WithContracts(const MoreContracts &...more_contracts) const
TEST(NotificationTest, SanityTest)
exceptions_internal::ExceptionSafetyTestBuilder MakeExceptionSafetyTester()
testing::ThrowingValue<> Thrower
Definition: algorithm.h:29
void TestThrowingCtor(Args &&...args)
exceptions_internal::NoThrowTag nothrow_ctor


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