about summary refs log tree commit diff
path: root/absl/types/variant_exception_safety_test.cc
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2018-06-08T15·14-0700
committerGennadiy Civil <misterg@google.com>2018-06-08T15·24-0400
commit92020a042c0cd46979db9f6f0cb32783dc07765e (patch)
tree5f4ebb5a8b0a8c4f0e5e2a061e63665b2071d92f /absl/types/variant_exception_safety_test.cc
parent7aacab8ae05d7115049923da9cfbf584dc1f8338 (diff)
- abacaab4b11a69dd4db627bd183571d7cabb8def Refinement to previous time.h edit (in this same github p... by Greg Falcon <gfalcon@google.com>
  - 64db19b773134c6c8004e3b23c9ca892efbf8bae Move SpinLock's adaptive spin count computation from a st... by Derek Mauro <dmauro@google.com>
  - 6f9533fb44a52485a7c2bbb9b4efc7bf8d6c359a Import of CCTZ from GitHub. by Abseil Team <absl-team@google.com>
  - a211d7255c986e8dd4ceada362c0d054a6a1969a Cleanup exception flags by Abseil Team <absl-team@google.com>
  - babdb29c590126fe9bba5229fe91034b5b5c358a Release time benchmarks. by Alex Strelnikov <strel@google.com>
  - 5803b32a3ff123d1fb57a0c471d199c818357c9f Release memutil microbenchmarks. by Alex Strelnikov <strel@google.com>
  - 5357d4890d30e80c53beb05af32500fb20e9402b Add parens around expansion of ABSL_PREDICT_{FALSE,TRUE} ... by Abseil Team <absl-team@google.com>
  - 32023f61a239a5f6b1c59e577bfe81b179bbcd2d Reformat build rule tag. by Alex Strelnikov <strel@google.com>
  - 833758ecf2b0cf7a42bbd50b5b127e416425c168 Release uint128 microbenchmarks. by Alex Strelnikov <strel@google.com>
  - c115a9bca1f944b90fdc78a56b2de176466b124f Disambiguate bitwise-not of size_type by Abseil Team <absl-team@google.com>
  - f6905f5b5f6e425792de646edafde440548d9346 Updates ConstructorTracker and TrackedObjects with 1) a m... by Abseil Team <absl-team@google.com>
  - 147c553bdd5d2db20a38f75c4d1ef973d6c709c5 Changes the absl::Duration factory functions to disallow ... by Greg Miller <jgm@google.com>
  - dba2b96d11b5264546b283ba452f2de1303b0f07 White space fix by Alex Strelnikov <strel@google.com>

GitOrigin-RevId: abacaab4b11a69dd4db627bd183571d7cabb8def
Change-Id: I6fa34f20d0b2f898e7b5475a603111413bb80a67
Diffstat (limited to 'absl/types/variant_exception_safety_test.cc')
-rw-r--r--absl/types/variant_exception_safety_test.cc121
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);
+                    }));
   }
 }