15 #include "absl/base/config.h"
16 #include "absl/container/fixed_array.h"
18 #ifdef ABSL_HAVE_EXCEPTIONS
20 #include <initializer_list>
22 #include "gtest/gtest.h"
23 #include "absl/base/internal/exception_safety_testing.h"
31 constexpr
size_t kSmallSize =
kInlined / 2;
32 constexpr
size_t kLargeSize =
kInlined * 2;
34 constexpr
int kInitialValue = 5;
35 constexpr
int kUpdatedValue = 10;
37 using ::testing::TestThrowingCtor;
39 using Thrower = testing::ThrowingValue<testing::TypeSpec::kEverythingThrows>;
41 testing::ThrowingAllocator<Thrower, testing::AllocSpec::kEverythingThrows>;
42 using MoveThrower = testing::ThrowingValue<testing::TypeSpec::kNoThrowMove>;
43 using MoveThrowAlloc =
44 testing::ThrowingAllocator<MoveThrower,
45 testing::AllocSpec::kEverythingThrows>;
51 using MoveFixedArrWithAlloc =
54 TEST(FixedArrayExceptionSafety, CopyConstructor) {
55 auto small = FixedArr(kSmallSize);
56 TestThrowingCtor<FixedArr>(small);
58 auto large = FixedArr(kLargeSize);
59 TestThrowingCtor<FixedArr>(large);
62 TEST(FixedArrayExceptionSafety, CopyConstructorWithAlloc) {
63 auto small = FixedArrWithAlloc(kSmallSize);
64 TestThrowingCtor<FixedArrWithAlloc>(small);
66 auto large = FixedArrWithAlloc(kLargeSize);
67 TestThrowingCtor<FixedArrWithAlloc>(large);
70 TEST(FixedArrayExceptionSafety, MoveConstructor) {
71 TestThrowingCtor<FixedArr>(FixedArr(kSmallSize));
72 TestThrowingCtor<FixedArr>(FixedArr(kLargeSize));
75 TestThrowingCtor<MoveFixedArr>(MoveFixedArr(kSmallSize));
76 TestThrowingCtor<MoveFixedArr>(MoveFixedArr(kLargeSize));
79 TEST(FixedArrayExceptionSafety, MoveConstructorWithAlloc) {
80 TestThrowingCtor<FixedArrWithAlloc>(FixedArrWithAlloc(kSmallSize));
81 TestThrowingCtor<FixedArrWithAlloc>(FixedArrWithAlloc(kLargeSize));
84 TestThrowingCtor<MoveFixedArrWithAlloc>(MoveFixedArrWithAlloc(kSmallSize));
85 TestThrowingCtor<MoveFixedArrWithAlloc>(MoveFixedArrWithAlloc(kLargeSize));
88 TEST(FixedArrayExceptionSafety, SizeConstructor) {
89 TestThrowingCtor<FixedArr>(kSmallSize);
90 TestThrowingCtor<FixedArr>(kLargeSize);
93 TEST(FixedArrayExceptionSafety, SizeConstructorWithAlloc) {
94 TestThrowingCtor<FixedArrWithAlloc>(kSmallSize);
95 TestThrowingCtor<FixedArrWithAlloc>(kLargeSize);
98 TEST(FixedArrayExceptionSafety, SizeValueConstructor) {
99 TestThrowingCtor<FixedArr>(kSmallSize, Thrower());
100 TestThrowingCtor<FixedArr>(kLargeSize, Thrower());
103 TEST(FixedArrayExceptionSafety, SizeValueConstructorWithAlloc) {
104 TestThrowingCtor<FixedArrWithAlloc>(kSmallSize, Thrower());
105 TestThrowingCtor<FixedArrWithAlloc>(kLargeSize, Thrower());
108 TEST(FixedArrayExceptionSafety, IteratorConstructor) {
109 auto small = FixedArr(kSmallSize);
110 TestThrowingCtor<FixedArr>(small.begin(), small.end());
112 auto large = FixedArr(kLargeSize);
113 TestThrowingCtor<FixedArr>(
large.begin(),
large.end());
116 TEST(FixedArrayExceptionSafety, IteratorConstructorWithAlloc) {
117 auto small = FixedArrWithAlloc(kSmallSize);
118 TestThrowingCtor<FixedArrWithAlloc>(small.begin(), small.end());
120 auto large = FixedArrWithAlloc(kLargeSize);
121 TestThrowingCtor<FixedArrWithAlloc>(
large.begin(),
large.end());
124 TEST(FixedArrayExceptionSafety, InitListConstructor) {
125 constexpr
int small_inlined = 3;
128 TestThrowingCtor<SmallFixedArr>(std::initializer_list<Thrower>{});
130 TestThrowingCtor<SmallFixedArr>(
131 std::initializer_list<Thrower>{Thrower{}, Thrower{}});
133 TestThrowingCtor<SmallFixedArr>(std::initializer_list<Thrower>{
134 Thrower{}, Thrower{}, Thrower{}, Thrower{}, Thrower{}});
137 TEST(FixedArrayExceptionSafety, InitListConstructorWithAlloc) {
138 constexpr
int small_inlined = 3;
139 using SmallFixedArrWithAlloc =
142 TestThrowingCtor<SmallFixedArrWithAlloc>(std::initializer_list<Thrower>{});
144 TestThrowingCtor<SmallFixedArrWithAlloc>(
145 std::initializer_list<Thrower>{Thrower{}, Thrower{}});
147 TestThrowingCtor<SmallFixedArrWithAlloc>(std::initializer_list<Thrower>{
148 Thrower{}, Thrower{}, Thrower{}, Thrower{}, Thrower{}});
151 template <
typename FixedArrT>
154 for (
const auto& thrower : *fixed_arr) {
155 sum += thrower.Get();
160 TEST(FixedArrayExceptionSafety, Fill) {
161 auto test_fill = testing::MakeExceptionSafetyTester()
162 .WithContracts(ReadMemory<FixedArr>)
163 .WithOperation([&](FixedArr* fixed_arr_ptr) {
165 Thrower(kUpdatedValue, testing::nothrow_ctor);
166 fixed_arr_ptr->fill(thrower);
170 test_fill.WithInitialValue(FixedArr(kSmallSize, Thrower(kInitialValue)))
173 test_fill.WithInitialValue(FixedArr(kLargeSize, Thrower(kInitialValue)))
177 TEST(FixedArrayExceptionSafety, FillWithAlloc) {
178 auto test_fill = testing::MakeExceptionSafetyTester()
179 .WithContracts(ReadMemory<FixedArrWithAlloc>)
180 .WithOperation([&](FixedArrWithAlloc* fixed_arr_ptr) {
182 Thrower(kUpdatedValue, testing::nothrow_ctor);
183 fixed_arr_ptr->fill(thrower);
188 FixedArrWithAlloc(kSmallSize, Thrower(kInitialValue)))
192 FixedArrWithAlloc(kLargeSize, Thrower(kInitialValue)))
201 #endif // ABSL_HAVE_EXCEPTIONS