diff options
Diffstat (limited to 'absl/types/variant_exception_safety_test.cc')
-rw-r--r-- | absl/types/variant_exception_safety_test.cc | 121 |
1 files changed, 55 insertions, 66 deletions
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); + })); } } |