diff options
Diffstat (limited to 'absl/types')
-rw-r--r-- | absl/types/BUILD.bazel | 15 | ||||
-rw-r--r-- | absl/types/optional_exception_safety_test.cc | 84 | ||||
-rw-r--r-- | absl/types/span.h | 2 | ||||
-rw-r--r-- | absl/types/variant_exception_safety_test.cc | 121 |
4 files changed, 109 insertions, 113 deletions
diff --git a/absl/types/BUILD.bazel b/absl/types/BUILD.bazel index c26c68d0b35f..1fc4c098e9bd 100644 --- a/absl/types/BUILD.bazel +++ b/absl/types/BUILD.bazel @@ -28,7 +28,7 @@ licenses(["notice"]) # Apache 2.0 cc_library( name = "any", hdrs = ["any.h"], - copts = ABSL_DEFAULT_COPTS + ABSL_EXCEPTIONS_FLAG, + copts = ABSL_DEFAULT_COPTS, deps = [ ":bad_any_cast", "//absl/base:config", @@ -40,9 +40,19 @@ cc_library( cc_library( name = "bad_any_cast", - srcs = ["bad_any_cast.cc"], hdrs = ["bad_any_cast.h"], + copts = ABSL_DEFAULT_COPTS, + deps = [":bad_any_cast_impl"], +) + +cc_library( + name = "bad_any_cast_impl", + srcs = [ + "bad_any_cast.cc", + "bad_any_cast.h", + ], copts = ABSL_EXCEPTIONS_FLAG + ABSL_DEFAULT_COPTS, + visibility = ["//visibility:private"], deps = [ "//absl/base", "//absl/base:config", @@ -206,7 +216,6 @@ cc_test( ], ) - cc_library( name = "variant", srcs = ["internal/variant.h"], diff --git a/absl/types/optional_exception_safety_test.cc b/absl/types/optional_exception_safety_test.cc index 7f6348e1b8e1..d2ef04b8d1b0 100644 --- a/absl/types/optional_exception_safety_test.cc +++ b/absl/types/optional_exception_safety_test.cc @@ -170,25 +170,22 @@ TEST(OptionalExceptionSafety, EverythingThrowsSwap) { TEST(OptionalExceptionSafety, NoThrowMoveSwap) { // Tests the nothrow guarantee for optional of T with non-throwing move - auto nothrow_test = - MakeExceptionSafetyTester().WithInvariants(testing::nothrow_guarantee); - auto nothrow_test_empty = nothrow_test.WithInitialValue(MoveOptional()); - auto nothrow_test_nonempty = - nothrow_test.WithInitialValue(MoveOptional(kInitialInteger)); - - auto swap_empty = [](MoveOptional* optional_ptr) { + { auto empty = MoveOptional(); - optional_ptr->swap(empty); - }; - EXPECT_TRUE(nothrow_test_nonempty.Test(swap_empty)); - - auto swap_nonempty = [](MoveOptional* optional_ptr) { - auto nonempty = - MoveOptional(absl::in_place, kUpdatedInteger, testing::nothrow_ctor); - optional_ptr->swap(nonempty); - }; - EXPECT_TRUE(nothrow_test_empty.Test(swap_nonempty)); - EXPECT_TRUE(nothrow_test_nonempty.Test(swap_nonempty)); + auto nonempty = MoveOptional(kInitialInteger); + EXPECT_TRUE(testing::TestNothrowOp([&]() { nonempty.swap(empty); })); + } + { + auto nonempty = MoveOptional(kUpdatedInteger); + auto empty = MoveOptional(); + EXPECT_TRUE(testing::TestNothrowOp([&]() { empty.swap(nonempty); })); + } + { + auto nonempty_from = MoveOptional(kUpdatedInteger); + auto nonempty_to = MoveOptional(kInitialInteger); + EXPECT_TRUE( + testing::TestNothrowOp([&]() { nonempty_to.swap(nonempty_from); })); + } } TEST(OptionalExceptionSafety, CopyAssign) { @@ -251,32 +248,33 @@ TEST(OptionalExceptionSafety, MoveAssign) { TEST(OptionalExceptionSafety, NothrowMoveAssign) { // Tests the nothrow guarantee for optional of T with non-throwing move - auto nothrow_test = - MakeExceptionSafetyTester().WithInvariants(testing::nothrow_guarantee); - auto nothrow_test_empty = nothrow_test.WithInitialValue(MoveOptional()); - auto nothrow_test_nonempty = - nothrow_test.WithInitialValue(MoveOptional(kInitialInteger)); - - auto moveassign_empty = [](MoveOptional* optional_ptr) { + { auto empty = MoveOptional(); - *optional_ptr = std::move(empty); - }; - EXPECT_TRUE(nothrow_test_nonempty.Test(moveassign_empty)); - - auto moveassign_nonempty = [](MoveOptional* optional_ptr) { - auto nonempty = - MoveOptional(absl::in_place, kUpdatedInteger, testing::nothrow_ctor); - *optional_ptr = std::move(nonempty); - }; - EXPECT_TRUE(nothrow_test_empty.Test(moveassign_nonempty)); - EXPECT_TRUE(nothrow_test_nonempty.Test(moveassign_nonempty)); - - auto moveassign_thrower = [](MoveOptional* optional_ptr) { - auto thrower = MoveThrower(kUpdatedInteger, testing::nothrow_ctor); - *optional_ptr = std::move(thrower); - }; - EXPECT_TRUE(nothrow_test_empty.Test(moveassign_thrower)); - EXPECT_TRUE(nothrow_test_nonempty.Test(moveassign_thrower)); + auto nonempty = MoveOptional(kInitialInteger); + EXPECT_TRUE(testing::TestNothrowOp([&]() { nonempty = std::move(empty); })); + } + { + auto nonempty = MoveOptional(kInitialInteger); + auto empty = MoveOptional(); + EXPECT_TRUE(testing::TestNothrowOp([&]() { empty = std::move(nonempty); })); + } + { + auto nonempty_from = MoveOptional(kUpdatedInteger); + auto nonempty_to = MoveOptional(kInitialInteger); + EXPECT_TRUE(testing::TestNothrowOp( + [&]() { nonempty_to = std::move(nonempty_from); })); + } + { + auto thrower = MoveThrower(kUpdatedInteger); + auto empty = MoveOptional(); + EXPECT_TRUE(testing::TestNothrowOp([&]() { empty = std::move(thrower); })); + } + { + auto thrower = MoveThrower(kUpdatedInteger); + auto nonempty = MoveOptional(kInitialInteger); + EXPECT_TRUE( + testing::TestNothrowOp([&]() { nonempty = std::move(thrower); })); + } } } // namespace diff --git a/absl/types/span.h b/absl/types/span.h index f781353c2347..76be819ecca2 100644 --- a/absl/types/span.h +++ b/absl/types/span.h @@ -279,7 +279,7 @@ class Span { using size_type = size_t; using difference_type = ptrdiff_t; - static const size_type npos = ~size_type{0}; + static const size_type npos = ~(size_type(0)); constexpr Span() noexcept : Span(nullptr, 0) {} constexpr Span(pointer array, size_type length) noexcept diff --git a/absl/types/variant_exception_safety_test.cc b/absl/types/variant_exception_safety_test.cc index 377e4afac572..27c0b96ca6f3 100644 --- a/absl/types/variant_exception_safety_test.cc +++ b/absl/types/variant_exception_safety_test.cc @@ -27,8 +27,8 @@ namespace absl { namespace { using ::testing::MakeExceptionSafetyTester; -using ::testing::nothrow_guarantee; using ::testing::strong_guarantee; +using ::testing::TestNothrowOp; using ::testing::TestThrowingCtor; using Thrower = testing::ThrowingValue<>; @@ -120,7 +120,11 @@ testing::AssertionResult CheckInvariants(ThrowingVariant* v) { return AssertionSuccess(); } -Thrower ExpectedThrower() { return Thrower(42); } +template <typename... Args> +Thrower ExpectedThrower(Args&&... args) { + return Thrower(42, args...); +} + ThrowerVec ExpectedThrowerVec() { return {Thrower(100), Thrower(200)}; } ThrowingVariant ValuelessByException() { ThrowingVariant v; @@ -193,18 +197,14 @@ TEST(VariantExceptionSafetyTest, CopyAssign) { { // - neither *this nor rhs holds a value const ThrowingVariant rhs = ValuelessByException(); - EXPECT_TRUE(MakeExceptionSafetyTester() - .WithInitialValue(ValuelessByException()) - .WithInvariants(nothrow_guarantee) - .Test([&rhs](ThrowingVariant* lhs) { *lhs = rhs; })); + ThrowingVariant lhs = ValuelessByException(); + EXPECT_TRUE(TestNothrowOp([&]() { lhs = rhs; })); } { // - *this holds a value but rhs does not const ThrowingVariant rhs = ValuelessByException(); - EXPECT_TRUE(MakeExceptionSafetyTester() - .WithInitialValue(WithThrower()) - .WithInvariants(nothrow_guarantee) - .Test([&rhs](ThrowingVariant* lhs) { *lhs = rhs; })); + ThrowingVariant lhs = WithThrower(); + EXPECT_TRUE(TestNothrowOp([&]() { lhs = rhs; })); } // - index() == j { @@ -237,10 +237,8 @@ TEST(VariantExceptionSafetyTest, CopyAssign) { // should not throw because emplace() invokes Tj's copy ctor // which should not throw. const ThrowingVariant rhs(CopyNothrow{}); - EXPECT_TRUE(MakeExceptionSafetyTester() - .WithInitialValue(WithThrower()) - .WithInvariants(nothrow_guarantee) - .Test([&rhs](ThrowingVariant* lhs) { *lhs = rhs; })); + ThrowingVariant lhs = WithThrower(); + EXPECT_TRUE(TestNothrowOp([&]() { lhs = rhs; })); } { // is_nothrow_copy_constructible<Tj> == false && @@ -281,23 +279,14 @@ TEST(VariantExceptionSafetyTest, MoveAssign) { { // - neither *this nor rhs holds a value ThrowingVariant rhs = ValuelessByException(); - - EXPECT_TRUE(MakeExceptionSafetyTester() - .WithInitialValue(ValuelessByException()) - .WithInvariants(nothrow_guarantee) - .Test([rhs](ThrowingVariant* lhs) mutable { - *lhs = std::move(rhs); - })); + ThrowingVariant lhs = ValuelessByException(); + EXPECT_TRUE(TestNothrowOp([&]() { lhs = std::move(rhs); })); } { // - *this holds a value but rhs does not ThrowingVariant rhs = ValuelessByException(); - EXPECT_TRUE(MakeExceptionSafetyTester() - .WithInitialValue(WithThrower()) - .WithInvariants(nothrow_guarantee) - .Test([rhs](ThrowingVariant* lhs) mutable { - *lhs = std::move(rhs); - })); + ThrowingVariant lhs = WithThrower(); + EXPECT_TRUE(TestNothrowOp([&]() { lhs = std::move(rhs); })); } { // - index() == j @@ -310,13 +299,14 @@ TEST(VariantExceptionSafetyTest, MoveAssign) { // Since Thrower's move assignment has basic guarantee, so should variant's. auto tester = MakeExceptionSafetyTester() .WithInitialValue(WithThrower()) - .WithOperation([rhs](ThrowingVariant* lhs) mutable { - *lhs = std::move(rhs); + .WithOperation([&](ThrowingVariant* lhs) { + auto copy = rhs; + *lhs = std::move(copy); }); EXPECT_TRUE(tester .WithInvariants( CheckInvariants, - [j](ThrowingVariant* lhs) { return lhs->index() == j; }) + [&](ThrowingVariant* lhs) { return lhs->index() == j; }) .Test()); EXPECT_FALSE(tester.WithInvariants(strong_guarantee).Test()); } @@ -332,8 +322,9 @@ TEST(VariantExceptionSafetyTest, MoveAssign) { [](ThrowingVariant* lhs) { return lhs->valueless_by_exception(); }) - .Test([rhs](ThrowingVariant* lhs) mutable { - *lhs = std::move(rhs); + .Test([&](ThrowingVariant* lhs) { + auto copy = rhs; + *lhs = std::move(copy); })); } } @@ -365,8 +356,9 @@ TEST(VariantExceptionSafetyTest, ValueAssign) { // move assign auto move_tester = MakeExceptionSafetyTester() .WithInitialValue(WithThrower()) - .WithOperation([rhs](ThrowingVariant* lhs) mutable { - *lhs = std::move(rhs); + .WithOperation([&](ThrowingVariant* lhs) { + auto copy = rhs; + *lhs = std::move(copy); }); EXPECT_TRUE(move_tester .WithInvariants(CheckInvariants, @@ -388,19 +380,13 @@ TEST(VariantExceptionSafetyTest, ValueAssign) { // invokes the copy/move constructor and it should not throw. { const CopyNothrow rhs; - EXPECT_TRUE(MakeExceptionSafetyTester() - .WithInitialValue(WithThrower()) - .WithInvariants(nothrow_guarantee) - .Test([&rhs](ThrowingVariant* lhs) { *lhs = rhs; })); + ThrowingVariant lhs = WithThrower(); + EXPECT_TRUE(TestNothrowOp([&]() { lhs = rhs; })); } { MoveNothrow rhs; - EXPECT_TRUE(MakeExceptionSafetyTester() - .WithInitialValue(WithThrower()) - .WithInvariants(nothrow_guarantee) - .Test([rhs](ThrowingVariant* lhs) mutable { - *lhs = std::move(rhs); - })); + ThrowingVariant lhs = WithThrower(); + EXPECT_TRUE(TestNothrowOp([&]() { lhs = std::move(rhs); })); } // if is_nothrow_constructible_v<Tj, T> == false && // is_nothrow_move_constructible<Tj> == false @@ -423,8 +409,8 @@ TEST(VariantExceptionSafetyTest, ValueAssign) { // move auto move_tester = MakeExceptionSafetyTester() .WithInitialValue(WithCopyNoThrow()) - .WithOperation([rhs](ThrowingVariant* lhs) mutable { - *lhs = std::move(rhs); + .WithOperation([](ThrowingVariant* lhs) { + *lhs = ExpectedThrower(testing::nothrow_ctor); }); EXPECT_TRUE(move_tester .WithInvariants(CheckInvariants, @@ -477,21 +463,20 @@ TEST(VariantExceptionSafetyTest, Swap) { // if both are valueless_by_exception(), no effect { ThrowingVariant rhs = ValuelessByException(); - EXPECT_TRUE( - MakeExceptionSafetyTester() - .WithInitialValue(ValuelessByException()) - .WithInvariants(nothrow_guarantee) - .Test([rhs](ThrowingVariant* lhs) mutable { lhs->swap(rhs); })); + ThrowingVariant lhs = ValuelessByException(); + EXPECT_TRUE(TestNothrowOp([&]() { lhs.swap(rhs); })); } // if index() == rhs.index(), calls swap(get<i>(*this), get<i>(rhs)) // where i is index(). { ThrowingVariant rhs = ExpectedThrower(); - EXPECT_TRUE( - MakeExceptionSafetyTester() - .WithInitialValue(WithThrower()) - .WithInvariants(CheckInvariants) - .Test([rhs](ThrowingVariant* lhs) mutable { lhs->swap(rhs); })); + EXPECT_TRUE(MakeExceptionSafetyTester() + .WithInitialValue(WithThrower()) + .WithInvariants(CheckInvariants) + .Test([&](ThrowingVariant* lhs) { + auto copy = rhs; + lhs->swap(copy); + })); } // Otherwise, exchanges the value of rhs and *this. The exception safety // involves variant in moved-from state which is not specified in the @@ -499,19 +484,23 @@ TEST(VariantExceptionSafetyTest, Swap) { // overall strong guarantee. So, we are only checking basic guarantee here. { ThrowingVariant rhs = ExpectedThrower(); - EXPECT_TRUE( - MakeExceptionSafetyTester() - .WithInitialValue(WithCopyNoThrow()) - .WithInvariants(CheckInvariants) - .Test([rhs](ThrowingVariant* lhs) mutable { lhs->swap(rhs); })); + EXPECT_TRUE(MakeExceptionSafetyTester() + .WithInitialValue(WithCopyNoThrow()) + .WithInvariants(CheckInvariants) + .Test([&](ThrowingVariant* lhs) { + auto copy = rhs; + lhs->swap(copy); + })); } { ThrowingVariant rhs = ExpectedThrower(); - EXPECT_TRUE( - MakeExceptionSafetyTester() - .WithInitialValue(WithCopyNoThrow()) - .WithInvariants(CheckInvariants) - .Test([rhs](ThrowingVariant* lhs) mutable { rhs.swap(*lhs); })); + EXPECT_TRUE(MakeExceptionSafetyTester() + .WithInitialValue(WithCopyNoThrow()) + .WithInvariants(CheckInvariants) + .Test([&](ThrowingVariant* lhs) { + auto copy = rhs; + copy.swap(*lhs); + })); } } |