20 #include "gtest/gtest.h" 33 testing::AssertionResult AnyInvariants(
absl::any*
a) {
34 using testing::AssertionFailure;
35 using testing::AssertionSuccess;
38 if (a->
type() ==
typeid(void)) {
39 return AssertionFailure()
40 <<
"A non-empty any should not have type `void`";
43 if (a->
type() !=
typeid(void)) {
44 return AssertionFailure()
45 <<
"An empty any should have type void, but has type " 53 return AssertionFailure() <<
"A reset `any` should be valueless";
55 if (a->
type() !=
typeid(void)) {
56 return AssertionFailure() <<
"A reset `any` should have type() of `void`, " 57 "but instead has type " 62 static_cast<void>(unused);
63 return AssertionFailure()
64 <<
"A reset `any` should not be able to be any_cast";
67 return AssertionFailure()
68 <<
"Unexpected exception thrown from absl::any_cast";
70 return AssertionSuccess();
73 testing::AssertionResult AnyIsEmpty(
absl::any* a) {
75 return testing::AssertionSuccess();
77 return testing::AssertionFailure()
78 <<
"a should be empty, but instead has value " 82 TEST(AnyExceptionSafety, Ctors) {
84 testing::TestThrowingCtor<absl::any>(val);
87 testing::TestThrowingCtor<absl::any>(copy);
97 absl::in_place_type_t<ThrowingThrowerVec>(), {val},
ThrowingAlloc());
100 TEST(AnyExceptionSafety, Assignment) {
103 auto any_is_strong = [original](
absl::any* ap) {
104 return testing::AssertionResult(ap->has_value() &&
106 absl::any_cast<Thrower>(*ap));
110 .WithContracts(AnyInvariants, any_is_strong);
116 auto assign_any = [&any_val](
absl::any* ap) { *ap = any_val; };
117 auto assign_val = [&val](
absl::any* ap) { *ap = val; };
121 EXPECT_TRUE(any_strong_tester.Test(assign_any));
122 EXPECT_TRUE(any_strong_tester.Test(assign_val));
123 EXPECT_TRUE(any_strong_tester.Test(
move));
124 EXPECT_TRUE(any_strong_tester.Test(move_movable));
126 auto empty_any_is_strong = [](
absl::any* ap) {
127 return testing::AssertionResult{!ap->has_value()};
129 auto strong_empty_any_tester =
132 .WithContracts(AnyInvariants, empty_any_is_strong);
134 EXPECT_TRUE(strong_empty_any_tester.Test(assign_any));
135 EXPECT_TRUE(strong_empty_any_tester.Test(assign_val));
136 EXPECT_TRUE(strong_empty_any_tester.Test(
move));
140 #if !defined(ABSL_HAVE_STD_ANY) 141 TEST(AnyExceptionSafety, Emplace) {
146 .WithContracts(AnyInvariants, AnyIsEmpty);
149 auto emp_throwervec = [](
absl::any* ap) {
153 auto emp_movethrower = [](
absl::any* ap) {
157 EXPECT_TRUE(one_tester.Test(emp_thrower));
158 EXPECT_TRUE(one_tester.Test(emp_throwervec));
159 EXPECT_TRUE(one_tester.Test(emp_movethrower));
161 auto empty_tester = one_tester.WithInitialValue(
absl::any{});
163 EXPECT_TRUE(empty_tester.Test(emp_thrower));
164 EXPECT_TRUE(empty_tester.Test(emp_throwervec));
166 #endif // ABSL_HAVE_STD_ANY
std::vector< Thrower, ThrowingAlloc > ThrowingThrowerVec
bool has_value() const noexcept
void TestThrowingCtor(Args &&... args)
exceptions_internal::ExceptionSafetyTestBuilder MakeExceptionSafetyTester()
testing::ThrowingValue<> Thrower
void(*)(utility_internal::InPlaceTypeTag< T >) in_place_type_t
testing::ThrowingAllocator< Thrower > ThrowingAlloc
ValueType any_cast(const any &operand)
const std::type_info & type() const noexcept
exceptions_internal::NoThrowTag nothrow_ctor
TEST(Symbolize, Unimplemented)
ExceptionSafetyTestBuilder< DefaultFactory< T >, Operation, Contracts... > WithInitialValue(const T &t) const
std::initializer_list< Thrower > ThrowerList
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
std::vector< Thrower > ThrowerVec