optional_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 "absl/types/optional.h"
16 
17 #include "gtest/gtest.h"
19 
20 namespace absl {
21 
22 namespace {
23 
24 using ::testing::AssertionFailure;
25 using ::testing::AssertionResult;
26 using ::testing::AssertionSuccess;
28 
31 
33 using MoveOptional = absl::optional<MoveThrower>;
34 
35 constexpr int kInitialInteger = 5;
36 constexpr int kUpdatedInteger = 10;
37 
38 template <typename OptionalT>
39 bool ValueThrowsBadOptionalAccess(const OptionalT& optional) try {
40  return (static_cast<void>(optional.value()), false);
41 } catch (const absl::bad_optional_access&) {
42  return true;
43 }
44 
45 template <typename OptionalT>
46 AssertionResult OptionalInvariants(OptionalT* optional_ptr) {
47  // Check the current state post-throw for validity
48  auto& optional = *optional_ptr;
49 
50  if (optional.has_value() && ValueThrowsBadOptionalAccess(optional)) {
51  return AssertionFailure()
52  << "Optional with value should not throw bad_optional_access when "
53  "accessing the value.";
54  }
55  if (!optional.has_value() && !ValueThrowsBadOptionalAccess(optional)) {
56  return AssertionFailure()
57  << "Optional without a value should throw bad_optional_access when "
58  "accessing the value.";
59  }
60 
61  // Reset to a known state
62  optional.reset();
63 
64  // Confirm that the known post-reset state is valid
65  if (optional.has_value()) {
66  return AssertionFailure()
67  << "Optional should not contain a value after being reset.";
68  }
69  if (!ValueThrowsBadOptionalAccess(optional)) {
70  return AssertionFailure() << "Optional should throw bad_optional_access "
71  "when accessing the value after being reset.";
72  }
73 
74  return AssertionSuccess();
75 }
76 
77 template <typename OptionalT>
78 AssertionResult CheckDisengaged(OptionalT* optional_ptr) {
79  auto& optional = *optional_ptr;
80 
81  if (optional.has_value()) {
82  return AssertionFailure()
83  << "Expected optional to not contain a value but a value was found.";
84  }
85 
86  return AssertionSuccess();
87 }
88 
89 template <typename OptionalT>
90 AssertionResult CheckEngaged(OptionalT* optional_ptr) {
91  auto& optional = *optional_ptr;
92 
93  if (!optional.has_value()) {
94  return AssertionFailure()
95  << "Expected optional to contain a value but no value was found.";
96  }
97 
98  return AssertionSuccess();
99 }
100 
101 TEST(OptionalExceptionSafety, ThrowingConstructors) {
102  auto thrower_nonempty = Optional(Thrower(kInitialInteger));
103  testing::TestThrowingCtor<Optional>(thrower_nonempty);
104 
105  auto integer_nonempty = absl::optional<int>(kInitialInteger);
106  testing::TestThrowingCtor<Optional>(integer_nonempty);
107  testing::TestThrowingCtor<Optional>(std::move(integer_nonempty)); // NOLINT
108 
109  testing::TestThrowingCtor<Optional>(kInitialInteger);
110  using ThrowerVec = std::vector<Thrower, testing::ThrowingAllocator<Thrower>>;
111  testing::TestThrowingCtor<absl::optional<ThrowerVec>>(
112  absl::in_place,
113  std::initializer_list<Thrower>{Thrower(), Thrower(), Thrower()},
115 }
116 
117 TEST(OptionalExceptionSafety, NothrowConstructors) {
118  // This constructor is marked noexcept. If it throws, the program will
119  // terminate.
120  testing::TestThrowingCtor<MoveOptional>(MoveOptional(kUpdatedInteger));
121 }
122 
123 TEST(OptionalExceptionSafety, Emplace) {
124  // Test the basic guarantee plus test the result of optional::has_value()
125  // is false in all cases
126  auto disengaged_test = MakeExceptionSafetyTester().WithContracts(
127  OptionalInvariants<Optional>, CheckDisengaged<Optional>);
128  auto disengaged_test_empty = disengaged_test.WithInitialValue(Optional());
129  auto disengaged_test_nonempty =
130  disengaged_test.WithInitialValue(Optional(kInitialInteger));
131 
132  auto emplace_thrower_directly = [](Optional* optional_ptr) {
133  optional_ptr->emplace(kUpdatedInteger);
134  };
135  EXPECT_TRUE(disengaged_test_empty.Test(emplace_thrower_directly));
136  EXPECT_TRUE(disengaged_test_nonempty.Test(emplace_thrower_directly));
137 
138  auto emplace_thrower_copy = [](Optional* optional_ptr) {
139  auto thrower = Thrower(kUpdatedInteger, testing::nothrow_ctor);
140  optional_ptr->emplace(thrower);
141  };
142  EXPECT_TRUE(disengaged_test_empty.Test(emplace_thrower_copy));
143  EXPECT_TRUE(disengaged_test_nonempty.Test(emplace_thrower_copy));
144 }
145 
146 TEST(OptionalExceptionSafety, EverythingThrowsSwap) {
147  // Test the basic guarantee plus test the result of optional::has_value()
148  // remains the same
149  auto test =
150  MakeExceptionSafetyTester().WithContracts(OptionalInvariants<Optional>);
151  auto disengaged_test_empty = test.WithInitialValue(Optional())
152  .WithContracts(CheckDisengaged<Optional>);
153  auto engaged_test_nonempty = test.WithInitialValue(Optional(kInitialInteger))
154  .WithContracts(CheckEngaged<Optional>);
155 
156  auto swap_empty = [](Optional* optional_ptr) {
157  auto empty = Optional();
158  optional_ptr->swap(empty);
159  };
160  EXPECT_TRUE(engaged_test_nonempty.Test(swap_empty));
161 
162  auto swap_nonempty = [](Optional* optional_ptr) {
163  auto nonempty =
164  Optional(absl::in_place, kUpdatedInteger, testing::nothrow_ctor);
165  optional_ptr->swap(nonempty);
166  };
167  EXPECT_TRUE(disengaged_test_empty.Test(swap_nonempty));
168  EXPECT_TRUE(engaged_test_nonempty.Test(swap_nonempty));
169 }
170 
171 TEST(OptionalExceptionSafety, NoThrowMoveSwap) {
172  // Tests the nothrow guarantee for optional of T with non-throwing move
173  {
174  auto empty = MoveOptional();
175  auto nonempty = MoveOptional(kInitialInteger);
176  EXPECT_TRUE(testing::TestNothrowOp([&]() { nonempty.swap(empty); }));
177  }
178  {
179  auto nonempty = MoveOptional(kUpdatedInteger);
180  auto empty = MoveOptional();
181  EXPECT_TRUE(testing::TestNothrowOp([&]() { empty.swap(nonempty); }));
182  }
183  {
184  auto nonempty_from = MoveOptional(kUpdatedInteger);
185  auto nonempty_to = MoveOptional(kInitialInteger);
186  EXPECT_TRUE(
187  testing::TestNothrowOp([&]() { nonempty_to.swap(nonempty_from); }));
188  }
189 }
190 
191 TEST(OptionalExceptionSafety, CopyAssign) {
192  // Test the basic guarantee plus test the result of optional::has_value()
193  // remains the same
194  auto test =
195  MakeExceptionSafetyTester().WithContracts(OptionalInvariants<Optional>);
196  auto disengaged_test_empty = test.WithInitialValue(Optional())
197  .WithContracts(CheckDisengaged<Optional>);
198  auto engaged_test_nonempty = test.WithInitialValue(Optional(kInitialInteger))
199  .WithContracts(CheckEngaged<Optional>);
200 
201  auto copyassign_nonempty = [](Optional* optional_ptr) {
202  auto nonempty =
203  Optional(absl::in_place, kUpdatedInteger, testing::nothrow_ctor);
204  *optional_ptr = nonempty;
205  };
206  EXPECT_TRUE(disengaged_test_empty.Test(copyassign_nonempty));
207  EXPECT_TRUE(engaged_test_nonempty.Test(copyassign_nonempty));
208 
209  auto copyassign_thrower = [](Optional* optional_ptr) {
210  auto thrower = Thrower(kUpdatedInteger, testing::nothrow_ctor);
211  *optional_ptr = thrower;
212  };
213  EXPECT_TRUE(disengaged_test_empty.Test(copyassign_thrower));
214  EXPECT_TRUE(engaged_test_nonempty.Test(copyassign_thrower));
215 }
216 
217 TEST(OptionalExceptionSafety, MoveAssign) {
218  // Test the basic guarantee plus test the result of optional::has_value()
219  // remains the same
220  auto test =
221  MakeExceptionSafetyTester().WithContracts(OptionalInvariants<Optional>);
222  auto disengaged_test_empty = test.WithInitialValue(Optional())
223  .WithContracts(CheckDisengaged<Optional>);
224  auto engaged_test_nonempty = test.WithInitialValue(Optional(kInitialInteger))
225  .WithContracts(CheckEngaged<Optional>);
226 
227  auto moveassign_empty = [](Optional* optional_ptr) {
228  auto empty = Optional();
229  *optional_ptr = std::move(empty);
230  };
231  EXPECT_TRUE(engaged_test_nonempty.Test(moveassign_empty));
232 
233  auto moveassign_nonempty = [](Optional* optional_ptr) {
234  auto nonempty =
235  Optional(absl::in_place, kUpdatedInteger, testing::nothrow_ctor);
236  *optional_ptr = std::move(nonempty);
237  };
238  EXPECT_TRUE(disengaged_test_empty.Test(moveassign_nonempty));
239  EXPECT_TRUE(engaged_test_nonempty.Test(moveassign_nonempty));
240 
241  auto moveassign_thrower = [](Optional* optional_ptr) {
242  auto thrower = Thrower(kUpdatedInteger, testing::nothrow_ctor);
243  *optional_ptr = std::move(thrower);
244  };
245  EXPECT_TRUE(disengaged_test_empty.Test(moveassign_thrower));
246  EXPECT_TRUE(engaged_test_nonempty.Test(moveassign_thrower));
247 }
248 
249 TEST(OptionalExceptionSafety, NothrowMoveAssign) {
250  // Tests the nothrow guarantee for optional of T with non-throwing move
251  {
252  auto empty = MoveOptional();
253  auto nonempty = MoveOptional(kInitialInteger);
254  EXPECT_TRUE(testing::TestNothrowOp([&]() { nonempty = std::move(empty); }));
255  }
256  {
257  auto nonempty = MoveOptional(kInitialInteger);
258  auto empty = MoveOptional();
259  EXPECT_TRUE(testing::TestNothrowOp([&]() { empty = std::move(nonempty); }));
260  }
261  {
262  auto nonempty_from = MoveOptional(kUpdatedInteger);
263  auto nonempty_to = MoveOptional(kInitialInteger);
264  EXPECT_TRUE(testing::TestNothrowOp(
265  [&]() { nonempty_to = std::move(nonempty_from); }));
266  }
267  {
268  auto thrower = MoveThrower(kUpdatedInteger);
269  auto empty = MoveOptional();
270  EXPECT_TRUE(testing::TestNothrowOp([&]() { empty = std::move(thrower); }));
271  }
272  {
273  auto thrower = MoveThrower(kUpdatedInteger);
274  auto nonempty = MoveOptional(kInitialInteger);
275  EXPECT_TRUE(
276  testing::TestNothrowOp([&]() { nonempty = std::move(thrower); }));
277  }
278 }
279 
280 } // namespace
281 
282 } // 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
exceptions_internal::NoThrowTag nothrow_ctor
ABSL_ATTRIBUTE_REINITIALIZES void reset() noexcept
Definition: optional.h:336
testing::AssertionResult TestNothrowOp(const Operation &operation)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: utility.h:219
static bool Optional(bool)
Definition: demangle.cc:319
std::vector< Thrower > ThrowerVec


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