exception_safety_testing.cc 2.2 KB

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