xref: /aosp_15_r20/external/abseil-cpp/absl/base/throw_delegate_test.cc (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker 
15*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/throw_delegate.h"
16*9356374aSAndroid Build Coastguard Worker 
17*9356374aSAndroid Build Coastguard Worker #include <functional>
18*9356374aSAndroid Build Coastguard Worker #include <new>
19*9356374aSAndroid Build Coastguard Worker #include <stdexcept>
20*9356374aSAndroid Build Coastguard Worker 
21*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
22*9356374aSAndroid Build Coastguard Worker #include "gtest/gtest.h"
23*9356374aSAndroid Build Coastguard Worker 
24*9356374aSAndroid Build Coastguard Worker namespace {
25*9356374aSAndroid Build Coastguard Worker 
26*9356374aSAndroid Build Coastguard Worker using absl::base_internal::ThrowStdLogicError;
27*9356374aSAndroid Build Coastguard Worker using absl::base_internal::ThrowStdInvalidArgument;
28*9356374aSAndroid Build Coastguard Worker using absl::base_internal::ThrowStdDomainError;
29*9356374aSAndroid Build Coastguard Worker using absl::base_internal::ThrowStdLengthError;
30*9356374aSAndroid Build Coastguard Worker using absl::base_internal::ThrowStdOutOfRange;
31*9356374aSAndroid Build Coastguard Worker using absl::base_internal::ThrowStdRuntimeError;
32*9356374aSAndroid Build Coastguard Worker using absl::base_internal::ThrowStdRangeError;
33*9356374aSAndroid Build Coastguard Worker using absl::base_internal::ThrowStdOverflowError;
34*9356374aSAndroid Build Coastguard Worker using absl::base_internal::ThrowStdUnderflowError;
35*9356374aSAndroid Build Coastguard Worker using absl::base_internal::ThrowStdBadFunctionCall;
36*9356374aSAndroid Build Coastguard Worker using absl::base_internal::ThrowStdBadAlloc;
37*9356374aSAndroid Build Coastguard Worker 
38*9356374aSAndroid Build Coastguard Worker constexpr const char* what_arg = "The quick brown fox jumps over the lazy dog";
39*9356374aSAndroid Build Coastguard Worker 
40*9356374aSAndroid Build Coastguard Worker template <typename E>
ExpectThrowChar(void (* f)(const char *))41*9356374aSAndroid Build Coastguard Worker void ExpectThrowChar(void (*f)(const char*)) {
42*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
43*9356374aSAndroid Build Coastguard Worker   try {
44*9356374aSAndroid Build Coastguard Worker     f(what_arg);
45*9356374aSAndroid Build Coastguard Worker     FAIL() << "Didn't throw";
46*9356374aSAndroid Build Coastguard Worker   } catch (const E& e) {
47*9356374aSAndroid Build Coastguard Worker     EXPECT_STREQ(e.what(), what_arg);
48*9356374aSAndroid Build Coastguard Worker   }
49*9356374aSAndroid Build Coastguard Worker #else
50*9356374aSAndroid Build Coastguard Worker   EXPECT_DEATH_IF_SUPPORTED(f(what_arg), what_arg);
51*9356374aSAndroid Build Coastguard Worker #endif
52*9356374aSAndroid Build Coastguard Worker }
53*9356374aSAndroid Build Coastguard Worker 
54*9356374aSAndroid Build Coastguard Worker template <typename E>
ExpectThrowString(void (* f)(const std::string &))55*9356374aSAndroid Build Coastguard Worker void ExpectThrowString(void (*f)(const std::string&)) {
56*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
57*9356374aSAndroid Build Coastguard Worker   try {
58*9356374aSAndroid Build Coastguard Worker     f(what_arg);
59*9356374aSAndroid Build Coastguard Worker     FAIL() << "Didn't throw";
60*9356374aSAndroid Build Coastguard Worker   } catch (const E& e) {
61*9356374aSAndroid Build Coastguard Worker     EXPECT_STREQ(e.what(), what_arg);
62*9356374aSAndroid Build Coastguard Worker   }
63*9356374aSAndroid Build Coastguard Worker #else
64*9356374aSAndroid Build Coastguard Worker   EXPECT_DEATH_IF_SUPPORTED(f(what_arg), what_arg);
65*9356374aSAndroid Build Coastguard Worker #endif
66*9356374aSAndroid Build Coastguard Worker }
67*9356374aSAndroid Build Coastguard Worker 
68*9356374aSAndroid Build Coastguard Worker template <typename E>
ExpectThrowNoWhat(void (* f)())69*9356374aSAndroid Build Coastguard Worker void ExpectThrowNoWhat(void (*f)()) {
70*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
71*9356374aSAndroid Build Coastguard Worker   try {
72*9356374aSAndroid Build Coastguard Worker     f();
73*9356374aSAndroid Build Coastguard Worker     FAIL() << "Didn't throw";
74*9356374aSAndroid Build Coastguard Worker   } catch (const E& e) {
75*9356374aSAndroid Build Coastguard Worker   }
76*9356374aSAndroid Build Coastguard Worker #else
77*9356374aSAndroid Build Coastguard Worker   EXPECT_DEATH_IF_SUPPORTED(f(), "");
78*9356374aSAndroid Build Coastguard Worker #endif
79*9356374aSAndroid Build Coastguard Worker }
80*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdLogicErrorChar)81*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdLogicErrorChar) {
82*9356374aSAndroid Build Coastguard Worker   ExpectThrowChar<std::logic_error>(ThrowStdLogicError);
83*9356374aSAndroid Build Coastguard Worker }
84*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdInvalidArgumentChar)85*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdInvalidArgumentChar) {
86*9356374aSAndroid Build Coastguard Worker   ExpectThrowChar<std::invalid_argument>(ThrowStdInvalidArgument);
87*9356374aSAndroid Build Coastguard Worker }
88*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdDomainErrorChar)89*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdDomainErrorChar) {
90*9356374aSAndroid Build Coastguard Worker   ExpectThrowChar<std::domain_error>(ThrowStdDomainError);
91*9356374aSAndroid Build Coastguard Worker }
92*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdLengthErrorChar)93*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdLengthErrorChar) {
94*9356374aSAndroid Build Coastguard Worker   ExpectThrowChar<std::length_error>(ThrowStdLengthError);
95*9356374aSAndroid Build Coastguard Worker }
96*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdOutOfRangeChar)97*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdOutOfRangeChar) {
98*9356374aSAndroid Build Coastguard Worker   ExpectThrowChar<std::out_of_range>(ThrowStdOutOfRange);
99*9356374aSAndroid Build Coastguard Worker }
100*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdRuntimeErrorChar)101*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdRuntimeErrorChar) {
102*9356374aSAndroid Build Coastguard Worker   ExpectThrowChar<std::runtime_error>(ThrowStdRuntimeError);
103*9356374aSAndroid Build Coastguard Worker }
104*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdRangeErrorChar)105*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdRangeErrorChar) {
106*9356374aSAndroid Build Coastguard Worker   ExpectThrowChar<std::range_error>(ThrowStdRangeError);
107*9356374aSAndroid Build Coastguard Worker }
108*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdOverflowErrorChar)109*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdOverflowErrorChar) {
110*9356374aSAndroid Build Coastguard Worker   ExpectThrowChar<std::overflow_error>(ThrowStdOverflowError);
111*9356374aSAndroid Build Coastguard Worker }
112*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdUnderflowErrorChar)113*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdUnderflowErrorChar) {
114*9356374aSAndroid Build Coastguard Worker   ExpectThrowChar<std::underflow_error>(ThrowStdUnderflowError);
115*9356374aSAndroid Build Coastguard Worker }
116*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdLogicErrorString)117*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdLogicErrorString) {
118*9356374aSAndroid Build Coastguard Worker   ExpectThrowString<std::logic_error>(ThrowStdLogicError);
119*9356374aSAndroid Build Coastguard Worker }
120*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdInvalidArgumentString)121*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdInvalidArgumentString) {
122*9356374aSAndroid Build Coastguard Worker   ExpectThrowString<std::invalid_argument>(ThrowStdInvalidArgument);
123*9356374aSAndroid Build Coastguard Worker }
124*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdDomainErrorString)125*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdDomainErrorString) {
126*9356374aSAndroid Build Coastguard Worker   ExpectThrowString<std::domain_error>(ThrowStdDomainError);
127*9356374aSAndroid Build Coastguard Worker }
128*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdLengthErrorString)129*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdLengthErrorString) {
130*9356374aSAndroid Build Coastguard Worker   ExpectThrowString<std::length_error>(ThrowStdLengthError);
131*9356374aSAndroid Build Coastguard Worker }
132*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdOutOfRangeString)133*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdOutOfRangeString) {
134*9356374aSAndroid Build Coastguard Worker   ExpectThrowString<std::out_of_range>(ThrowStdOutOfRange);
135*9356374aSAndroid Build Coastguard Worker }
136*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdRuntimeErrorString)137*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdRuntimeErrorString) {
138*9356374aSAndroid Build Coastguard Worker   ExpectThrowString<std::runtime_error>(ThrowStdRuntimeError);
139*9356374aSAndroid Build Coastguard Worker }
140*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdRangeErrorString)141*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdRangeErrorString) {
142*9356374aSAndroid Build Coastguard Worker   ExpectThrowString<std::range_error>(ThrowStdRangeError);
143*9356374aSAndroid Build Coastguard Worker }
144*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdOverflowErrorString)145*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdOverflowErrorString) {
146*9356374aSAndroid Build Coastguard Worker   ExpectThrowString<std::overflow_error>(ThrowStdOverflowError);
147*9356374aSAndroid Build Coastguard Worker }
148*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdUnderflowErrorString)149*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdUnderflowErrorString) {
150*9356374aSAndroid Build Coastguard Worker   ExpectThrowString<std::underflow_error>(ThrowStdUnderflowError);
151*9356374aSAndroid Build Coastguard Worker }
152*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdBadFunctionCallNoWhat)153*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdBadFunctionCallNoWhat) {
154*9356374aSAndroid Build Coastguard Worker #ifdef ABSL_HAVE_EXCEPTIONS
155*9356374aSAndroid Build Coastguard Worker   try {
156*9356374aSAndroid Build Coastguard Worker     ThrowStdBadFunctionCall();
157*9356374aSAndroid Build Coastguard Worker     FAIL() << "Didn't throw";
158*9356374aSAndroid Build Coastguard Worker   } catch (const std::bad_function_call&) {
159*9356374aSAndroid Build Coastguard Worker   }
160*9356374aSAndroid Build Coastguard Worker #ifdef _LIBCPP_VERSION
161*9356374aSAndroid Build Coastguard Worker   catch (const std::exception&) {
162*9356374aSAndroid Build Coastguard Worker     // https://reviews.llvm.org/D92397 causes issues with the vtable for
163*9356374aSAndroid Build Coastguard Worker     // std::bad_function_call when using libc++ as a shared library.
164*9356374aSAndroid Build Coastguard Worker   }
165*9356374aSAndroid Build Coastguard Worker #endif
166*9356374aSAndroid Build Coastguard Worker #else
167*9356374aSAndroid Build Coastguard Worker   EXPECT_DEATH_IF_SUPPORTED(ThrowStdBadFunctionCall(), "");
168*9356374aSAndroid Build Coastguard Worker #endif
169*9356374aSAndroid Build Coastguard Worker }
170*9356374aSAndroid Build Coastguard Worker 
TEST(ThrowDelegate,ThrowStdBadAllocNoWhat)171*9356374aSAndroid Build Coastguard Worker TEST(ThrowDelegate, ThrowStdBadAllocNoWhat) {
172*9356374aSAndroid Build Coastguard Worker   ExpectThrowNoWhat<std::bad_alloc>(ThrowStdBadAlloc);
173*9356374aSAndroid Build Coastguard Worker }
174*9356374aSAndroid Build Coastguard Worker 
175*9356374aSAndroid Build Coastguard Worker }  // namespace
176