throw_delegate_test.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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/throw_delegate.h"
  15. #include <functional>
  16. #include <new>
  17. #include <stdexcept>
  18. #include "absl/base/config.h"
  19. #include "gtest/gtest.h"
  20. namespace {
  21. using absl::base_internal::ThrowStdLogicError;
  22. using absl::base_internal::ThrowStdInvalidArgument;
  23. using absl::base_internal::ThrowStdDomainError;
  24. using absl::base_internal::ThrowStdLengthError;
  25. using absl::base_internal::ThrowStdOutOfRange;
  26. using absl::base_internal::ThrowStdRuntimeError;
  27. using absl::base_internal::ThrowStdRangeError;
  28. using absl::base_internal::ThrowStdOverflowError;
  29. using absl::base_internal::ThrowStdUnderflowError;
  30. using absl::base_internal::ThrowStdBadFunctionCall;
  31. using absl::base_internal::ThrowStdBadAlloc;
  32. constexpr const char* what_arg = "The quick brown fox jumps over the lazy dog";
  33. template <typename E>
  34. void ExpectThrowChar(void (*f)(const char*)) {
  35. #ifdef ABSL_HAVE_EXCEPTIONS
  36. try {
  37. f(what_arg);
  38. FAIL() << "Didn't throw";
  39. } catch (const E& e) {
  40. EXPECT_STREQ(e.what(), what_arg);
  41. }
  42. #else
  43. EXPECT_DEATH_IF_SUPPORTED(f(what_arg), what_arg);
  44. #endif
  45. }
  46. template <typename E>
  47. void ExpectThrowString(void (*f)(const std::string&)) {
  48. #ifdef ABSL_HAVE_EXCEPTIONS
  49. try {
  50. f(what_arg);
  51. FAIL() << "Didn't throw";
  52. } catch (const E& e) {
  53. EXPECT_STREQ(e.what(), what_arg);
  54. }
  55. #else
  56. EXPECT_DEATH_IF_SUPPORTED(f(what_arg), what_arg);
  57. #endif
  58. }
  59. template <typename E>
  60. void ExpectThrowNoWhat(void (*f)()) {
  61. #ifdef ABSL_HAVE_EXCEPTIONS
  62. try {
  63. f();
  64. FAIL() << "Didn't throw";
  65. } catch (const E& e) {
  66. }
  67. #else
  68. EXPECT_DEATH_IF_SUPPORTED(f(), "");
  69. #endif
  70. }
  71. TEST(ThrowHelper, Test) {
  72. // Not using EXPECT_THROW because we want to check the .what() message too.
  73. ExpectThrowChar<std::logic_error>(ThrowStdLogicError);
  74. ExpectThrowChar<std::invalid_argument>(ThrowStdInvalidArgument);
  75. ExpectThrowChar<std::domain_error>(ThrowStdDomainError);
  76. ExpectThrowChar<std::length_error>(ThrowStdLengthError);
  77. ExpectThrowChar<std::out_of_range>(ThrowStdOutOfRange);
  78. ExpectThrowChar<std::runtime_error>(ThrowStdRuntimeError);
  79. ExpectThrowChar<std::range_error>(ThrowStdRangeError);
  80. ExpectThrowChar<std::overflow_error>(ThrowStdOverflowError);
  81. ExpectThrowChar<std::underflow_error>(ThrowStdUnderflowError);
  82. ExpectThrowString<std::logic_error>(ThrowStdLogicError);
  83. ExpectThrowString<std::invalid_argument>(ThrowStdInvalidArgument);
  84. ExpectThrowString<std::domain_error>(ThrowStdDomainError);
  85. ExpectThrowString<std::length_error>(ThrowStdLengthError);
  86. ExpectThrowString<std::out_of_range>(ThrowStdOutOfRange);
  87. ExpectThrowString<std::runtime_error>(ThrowStdRuntimeError);
  88. ExpectThrowString<std::range_error>(ThrowStdRangeError);
  89. ExpectThrowString<std::overflow_error>(ThrowStdOverflowError);
  90. ExpectThrowString<std::underflow_error>(ThrowStdUnderflowError);
  91. ExpectThrowNoWhat<std::bad_function_call>(ThrowStdBadFunctionCall);
  92. ExpectThrowNoWhat<std::bad_alloc>(ThrowStdBadAlloc);
  93. }
  94. } // namespace