abseil-cpp/absl/base/throw_delegate_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/base/internal/throw_delegate.h"
16 
17 #include <functional>
18 #include <new>
19 #include <stdexcept>
20 
21 #include "absl/base/config.h"
22 #include "gtest/gtest.h"
23 
24 namespace {
25 
37 
38 constexpr const char* what_arg = "The quick brown fox jumps over the lazy dog";
39 
40 template <typename E>
41 void ExpectThrowChar(void (*f)(const char*)) {
42 #ifdef ABSL_HAVE_EXCEPTIONS
43  try {
44  f(what_arg);
45  FAIL() << "Didn't throw";
46  } catch (const E& e) {
47  EXPECT_STREQ(e.what(), what_arg);
48  }
49 #else
50  EXPECT_DEATH_IF_SUPPORTED(f(what_arg), what_arg);
51 #endif
52 }
53 
54 template <typename E>
55 void ExpectThrowString(void (*f)(const std::string&)) {
56 #ifdef ABSL_HAVE_EXCEPTIONS
57  try {
58  f(what_arg);
59  FAIL() << "Didn't throw";
60  } catch (const E& e) {
61  EXPECT_STREQ(e.what(), what_arg);
62  }
63 #else
64  EXPECT_DEATH_IF_SUPPORTED(f(what_arg), what_arg);
65 #endif
66 }
67 
68 template <typename E>
69 void ExpectThrowNoWhat(void (*f)()) {
70 #ifdef ABSL_HAVE_EXCEPTIONS
71  try {
72  f();
73  FAIL() << "Didn't throw";
74  } catch (const E& e) {
75  }
76 #else
78 #endif
79 }
80 
81 TEST(ThrowHelper, Test) {
82  // Not using EXPECT_THROW because we want to check the .what() message too.
83  ExpectThrowChar<std::logic_error>(ThrowStdLogicError);
84  ExpectThrowChar<std::invalid_argument>(ThrowStdInvalidArgument);
85  ExpectThrowChar<std::domain_error>(ThrowStdDomainError);
86  ExpectThrowChar<std::length_error>(ThrowStdLengthError);
87  ExpectThrowChar<std::out_of_range>(ThrowStdOutOfRange);
88  ExpectThrowChar<std::runtime_error>(ThrowStdRuntimeError);
89  ExpectThrowChar<std::range_error>(ThrowStdRangeError);
90  ExpectThrowChar<std::overflow_error>(ThrowStdOverflowError);
91  ExpectThrowChar<std::underflow_error>(ThrowStdUnderflowError);
92 
93  ExpectThrowString<std::logic_error>(ThrowStdLogicError);
94  ExpectThrowString<std::invalid_argument>(ThrowStdInvalidArgument);
95  ExpectThrowString<std::domain_error>(ThrowStdDomainError);
96  ExpectThrowString<std::length_error>(ThrowStdLengthError);
97  ExpectThrowString<std::out_of_range>(ThrowStdOutOfRange);
98  ExpectThrowString<std::runtime_error>(ThrowStdRuntimeError);
99  ExpectThrowString<std::range_error>(ThrowStdRangeError);
100  ExpectThrowString<std::overflow_error>(ThrowStdOverflowError);
101  ExpectThrowString<std::underflow_error>(ThrowStdUnderflowError);
102 
103  ExpectThrowNoWhat<std::bad_function_call>(ThrowStdBadFunctionCall);
104  ExpectThrowNoWhat<std::bad_alloc>(ThrowStdBadAlloc);
105 }
106 
107 } // namespace
absl::base_internal::ThrowStdRangeError
void ThrowStdRangeError(const std::string &what_arg)
Definition: abseil-cpp/absl/base/internal/throw_delegate.cc:143
absl::base_internal::ThrowStdLengthError
void ThrowStdLengthError(const std::string &what_arg)
Definition: abseil-cpp/absl/base/internal/throw_delegate.cc:92
absl::base_internal::ThrowStdDomainError
void ThrowStdDomainError(const std::string &what_arg)
Definition: abseil-cpp/absl/base/internal/throw_delegate.cc:75
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::base_internal::ThrowStdRuntimeError
void ThrowStdRuntimeError(const std::string &what_arg)
Definition: abseil-cpp/absl/base/internal/throw_delegate.cc:126
absl::base_internal::ThrowStdInvalidArgument
void ThrowStdInvalidArgument(const std::string &what_arg)
Definition: abseil-cpp/absl/base/internal/throw_delegate.cc:58
absl::base_internal::ThrowStdLogicError
void ThrowStdLogicError(const std::string &what_arg)
Definition: abseil-cpp/absl/base/internal/throw_delegate.cc:42
absl::base_internal::ThrowStdOutOfRange
void ThrowStdOutOfRange(const std::string &what_arg)
Definition: abseil-cpp/absl/base/internal/throw_delegate.cc:109
absl::FormatConversionChar::e
@ e
autogen_x86imm.f
f
Definition: autogen_x86imm.py:9
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
absl::base_internal::ThrowStdBadFunctionCall
void ThrowStdBadFunctionCall()
Definition: abseil-cpp/absl/base/internal/throw_delegate.cc:194
absl::base_internal::ThrowStdUnderflowError
void ThrowStdUnderflowError(const std::string &what_arg)
Definition: abseil-cpp/absl/base/internal/throw_delegate.cc:177
FAIL
@ FAIL
Definition: call_creds.cc:42
absl::base_internal::ThrowStdBadAlloc
void ThrowStdBadAlloc()
Definition: abseil-cpp/absl/base/internal/throw_delegate.cc:202
EXPECT_DEATH_IF_SUPPORTED
#define EXPECT_DEATH_IF_SUPPORTED(statement, regex)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest-death-test.h:335
EXPECT_STREQ
#define EXPECT_STREQ(s1, s2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2095
absl::base_internal::ThrowStdOverflowError
void ThrowStdOverflowError(const std::string &what_arg)
Definition: abseil-cpp/absl/base/internal/throw_delegate.cc:160
Test
Definition: hpack_parser_test.cc:43


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:36