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/base/internal/exception_safety_testing.h"
16
17 #ifdef ABSL_HAVE_EXCEPTIONS
18
19 #include "gtest/gtest.h"
20 #include "absl/meta/type_traits.h"
21
22 namespace testing {
23
24 exceptions_internal::NoThrowTag nothrow_ctor;
25
26 exceptions_internal::StrongGuaranteeTagType strong_guarantee;
27
MakeExceptionSafetyTester()28 exceptions_internal::ExceptionSafetyTestBuilder<> MakeExceptionSafetyTester() {
29 return {};
30 }
31
32 namespace exceptions_internal {
33
34 int countdown = -1;
35
36 ConstructorTracker* ConstructorTracker::current_tracker_instance_ = nullptr;
37
MaybeThrow(absl::string_view msg,bool throw_bad_alloc)38 void MaybeThrow(absl::string_view msg, bool throw_bad_alloc) {
39 if (countdown-- == 0) {
40 if (throw_bad_alloc) throw TestBadAllocException(msg);
41 throw TestException(msg);
42 }
43 }
44
FailureMessage(const TestException & e,int countdown)45 testing::AssertionResult FailureMessage(const TestException& e,
46 int countdown) noexcept {
47 return testing::AssertionFailure() << "Exception thrown from " << e.what();
48 }
49
GetSpecString(TypeSpec spec)50 std::string GetSpecString(TypeSpec spec) {
51 std::string out;
52 absl::string_view sep;
53 const auto append = [&](absl::string_view s) {
54 absl::StrAppend(&out, sep, s);
55 sep = " | ";
56 };
57 if (static_cast<bool>(TypeSpec::kNoThrowCopy & spec)) {
58 append("kNoThrowCopy");
59 }
60 if (static_cast<bool>(TypeSpec::kNoThrowMove & spec)) {
61 append("kNoThrowMove");
62 }
63 if (static_cast<bool>(TypeSpec::kNoThrowNew & spec)) {
64 append("kNoThrowNew");
65 }
66 return out;
67 }
68
GetSpecString(AllocSpec spec)69 std::string GetSpecString(AllocSpec spec) {
70 return static_cast<bool>(AllocSpec::kNoThrowAllocate & spec)
71 ? "kNoThrowAllocate"
72 : "";
73 }
74
75 } // namespace exceptions_internal
76
77 } // namespace testing
78
79 #endif // ABSL_HAVE_EXCEPTIONS
80