diff options
Diffstat (limited to 'absl/types/variant_exception_safety_test.cc')
-rw-r--r-- | absl/types/variant_exception_safety_test.cc | 108 |
1 files changed, 57 insertions, 51 deletions
diff --git a/absl/types/variant_exception_safety_test.cc b/absl/types/variant_exception_safety_test.cc index d7bc90d2..ff166051 100644 --- a/absl/types/variant_exception_safety_test.cc +++ b/absl/types/variant_exception_safety_test.cc @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + #include "absl/types/variant.h" #include <iostream> @@ -20,11 +21,14 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "absl/base/config.h" #include "absl/base/internal/exception_safety_testing.h" #include "absl/memory/memory.h" +// See comment in absl/base/config.h +#if !defined(ABSL_INTERNAL_MSVC_2017_DBG_MODE) namespace absl { -inline namespace lts_2018_06_20 { +inline namespace lts_2018_12_18 { namespace { using ::testing::MakeExceptionSafetyTester; @@ -54,13 +58,13 @@ void ToValuelessByException(ThrowingVariant& v) { // NOLINT try { v.emplace<Thrower>(); v.emplace<Thrower>(ExceptionOnConversion<Thrower>()); - } catch (ConversionException& /*e*/) { + } catch (const ConversionException&) { // This space intentionally left blank. } } // Check that variant is still in a usable state after an exception is thrown. -testing::AssertionResult CheckInvariants(ThrowingVariant* v) { +testing::AssertionResult VariantInvariants(ThrowingVariant* v) { using testing::AssertionFailure; using testing::AssertionSuccess; @@ -101,7 +105,7 @@ testing::AssertionResult CheckInvariants(ThrowingVariant* v) { auto unused = absl::get<Thrower>(*v); static_cast<void>(unused); return AssertionFailure() << "Variant should not contain Thrower"; - } catch (absl::bad_variant_access) { + } catch (const absl::bad_variant_access&) { } catch (...) { return AssertionFailure() << "Unexpected exception throw from absl::get"; } @@ -214,8 +218,8 @@ TEST(VariantExceptionSafetyTest, CopyAssign) { MakeExceptionSafetyTester() .WithInitialValue(WithThrower()) .WithOperation([&rhs](ThrowingVariant* lhs) { *lhs = rhs; }); - EXPECT_TRUE(tester.WithInvariants(CheckInvariants).Test()); - EXPECT_FALSE(tester.WithInvariants(strong_guarantee).Test()); + EXPECT_TRUE(tester.WithContracts(VariantInvariants).Test()); + EXPECT_FALSE(tester.WithContracts(strong_guarantee).Test()); } { const ThrowingVariant rhs(ExpectedThrowerVec()); @@ -223,8 +227,8 @@ TEST(VariantExceptionSafetyTest, CopyAssign) { MakeExceptionSafetyTester() .WithInitialValue(WithThrowerVec()) .WithOperation([&rhs](ThrowingVariant* lhs) { *lhs = rhs; }); - EXPECT_TRUE(tester.WithInvariants(CheckInvariants).Test()); - EXPECT_FALSE(tester.WithInvariants(strong_guarantee).Test()); + EXPECT_TRUE(tester.WithContracts(VariantInvariants).Test()); + EXPECT_FALSE(tester.WithContracts(strong_guarantee).Test()); } // libstdc++ std::variant has bugs on copy assignment regarding exception // safety. @@ -252,12 +256,12 @@ TEST(VariantExceptionSafetyTest, CopyAssign) { .WithInitialValue(WithCopyNoThrow()) .WithOperation([&rhs](ThrowingVariant* lhs) { *lhs = rhs; }); EXPECT_TRUE(tester - .WithInvariants(CheckInvariants, - [](ThrowingVariant* lhs) { - return lhs->valueless_by_exception(); - }) + .WithContracts(VariantInvariants, + [](ThrowingVariant* lhs) { + return lhs->valueless_by_exception(); + }) .Test()); - EXPECT_FALSE(tester.WithInvariants(strong_guarantee).Test()); + EXPECT_FALSE(tester.WithContracts(strong_guarantee).Test()); } #endif // !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__)) { @@ -269,7 +273,7 @@ TEST(VariantExceptionSafetyTest, CopyAssign) { const ThrowingVariant rhs(MoveNothrow{}); EXPECT_TRUE(MakeExceptionSafetyTester() .WithInitialValue(WithThrower()) - .WithInvariants(CheckInvariants, strong_guarantee) + .WithContracts(VariantInvariants, strong_guarantee) .Test([&rhs](ThrowingVariant* lhs) { *lhs = rhs; })); } } @@ -305,11 +309,11 @@ TEST(VariantExceptionSafetyTest, MoveAssign) { *lhs = std::move(copy); }); EXPECT_TRUE(tester - .WithInvariants( - CheckInvariants, + .WithContracts( + VariantInvariants, [&](ThrowingVariant* lhs) { return lhs->index() == j; }) .Test()); - EXPECT_FALSE(tester.WithInvariants(strong_guarantee).Test()); + EXPECT_FALSE(tester.WithContracts(strong_guarantee).Test()); } { // - otherwise (index() != j), equivalent to @@ -319,10 +323,10 @@ TEST(VariantExceptionSafetyTest, MoveAssign) { ThrowingVariant rhs(CopyNothrow{}); EXPECT_TRUE(MakeExceptionSafetyTester() .WithInitialValue(WithThrower()) - .WithInvariants(CheckInvariants, - [](ThrowingVariant* lhs) { - return lhs->valueless_by_exception(); - }) + .WithContracts(VariantInvariants, + [](ThrowingVariant* lhs) { + return lhs->valueless_by_exception(); + }) .Test([&](ThrowingVariant* lhs) { auto copy = rhs; *lhs = std::move(copy); @@ -348,12 +352,12 @@ TEST(VariantExceptionSafetyTest, ValueAssign) { .WithInitialValue(WithThrower()) .WithOperation([rhs](ThrowingVariant* lhs) { *lhs = rhs; }); EXPECT_TRUE(copy_tester - .WithInvariants(CheckInvariants, - [](ThrowingVariant* lhs) { - return !lhs->valueless_by_exception(); - }) + .WithContracts(VariantInvariants, + [](ThrowingVariant* lhs) { + return !lhs->valueless_by_exception(); + }) .Test()); - EXPECT_FALSE(copy_tester.WithInvariants(strong_guarantee).Test()); + EXPECT_FALSE(copy_tester.WithContracts(strong_guarantee).Test()); // move assign auto move_tester = MakeExceptionSafetyTester() .WithInitialValue(WithThrower()) @@ -362,13 +366,13 @@ TEST(VariantExceptionSafetyTest, ValueAssign) { *lhs = std::move(copy); }); EXPECT_TRUE(move_tester - .WithInvariants(CheckInvariants, - [](ThrowingVariant* lhs) { - return !lhs->valueless_by_exception(); - }) + .WithContracts(VariantInvariants, + [](ThrowingVariant* lhs) { + return !lhs->valueless_by_exception(); + }) .Test()); - EXPECT_FALSE(move_tester.WithInvariants(strong_guarantee).Test()); + EXPECT_FALSE(move_tester.WithContracts(strong_guarantee).Test()); } // Otherwise (*this holds something else), if is_nothrow_constructible_v<Tj, // T> || !is_nothrow_move_constructible_v<Tj> is true, equivalent to @@ -401,12 +405,12 @@ TEST(VariantExceptionSafetyTest, ValueAssign) { .WithInitialValue(WithCopyNoThrow()) .WithOperation([&rhs](ThrowingVariant* lhs) { *lhs = rhs; }); EXPECT_TRUE(copy_tester - .WithInvariants(CheckInvariants, - [](ThrowingVariant* lhs) { - return lhs->valueless_by_exception(); - }) + .WithContracts(VariantInvariants, + [](ThrowingVariant* lhs) { + return lhs->valueless_by_exception(); + }) .Test()); - EXPECT_FALSE(copy_tester.WithInvariants(strong_guarantee).Test()); + EXPECT_FALSE(copy_tester.WithContracts(strong_guarantee).Test()); // move auto move_tester = MakeExceptionSafetyTester() .WithInitialValue(WithCopyNoThrow()) @@ -414,12 +418,12 @@ TEST(VariantExceptionSafetyTest, ValueAssign) { *lhs = ExpectedThrower(testing::nothrow_ctor); }); EXPECT_TRUE(move_tester - .WithInvariants(CheckInvariants, - [](ThrowingVariant* lhs) { - return lhs->valueless_by_exception(); - }) + .WithContracts(VariantInvariants, + [](ThrowingVariant* lhs) { + return lhs->valueless_by_exception(); + }) .Test()); - EXPECT_FALSE(move_tester.WithInvariants(strong_guarantee).Test()); + EXPECT_FALSE(move_tester.WithContracts(strong_guarantee).Test()); } // Otherwise (if is_nothrow_constructible_v<Tj, T> == false && // is_nothrow_move_constructible<Tj> == true), @@ -433,7 +437,7 @@ TEST(VariantExceptionSafetyTest, ValueAssign) { MoveNothrow rhs; EXPECT_TRUE(MakeExceptionSafetyTester() .WithInitialValue(WithThrower()) - .WithInvariants(CheckInvariants, strong_guarantee) + .WithContracts(VariantInvariants, strong_guarantee) .Test([&rhs](ThrowingVariant* lhs) { *lhs = rhs; })); } #endif // !(defined(ABSL_HAVE_STD_VARIANT) && defined(__GLIBCXX__)) @@ -451,12 +455,12 @@ TEST(VariantExceptionSafetyTest, Emplace) { v->emplace<Thrower>(args); }); EXPECT_TRUE(tester - .WithInvariants(CheckInvariants, - [](ThrowingVariant* v) { - return v->valueless_by_exception(); - }) + .WithContracts(VariantInvariants, + [](ThrowingVariant* v) { + return v->valueless_by_exception(); + }) .Test()); - EXPECT_FALSE(tester.WithInvariants(strong_guarantee).Test()); + EXPECT_FALSE(tester.WithContracts(strong_guarantee).Test()); } } @@ -473,7 +477,7 @@ TEST(VariantExceptionSafetyTest, Swap) { ThrowingVariant rhs = ExpectedThrower(); EXPECT_TRUE(MakeExceptionSafetyTester() .WithInitialValue(WithThrower()) - .WithInvariants(CheckInvariants) + .WithContracts(VariantInvariants) .Test([&](ThrowingVariant* lhs) { auto copy = rhs; lhs->swap(copy); @@ -487,7 +491,7 @@ TEST(VariantExceptionSafetyTest, Swap) { ThrowingVariant rhs = ExpectedThrower(); EXPECT_TRUE(MakeExceptionSafetyTester() .WithInitialValue(WithCopyNoThrow()) - .WithInvariants(CheckInvariants) + .WithContracts(VariantInvariants) .Test([&](ThrowingVariant* lhs) { auto copy = rhs; lhs->swap(copy); @@ -497,7 +501,7 @@ TEST(VariantExceptionSafetyTest, Swap) { ThrowingVariant rhs = ExpectedThrower(); EXPECT_TRUE(MakeExceptionSafetyTester() .WithInitialValue(WithCopyNoThrow()) - .WithInvariants(CheckInvariants) + .WithContracts(VariantInvariants) .Test([&](ThrowingVariant* lhs) { auto copy = rhs; copy.swap(*lhs); @@ -506,5 +510,7 @@ TEST(VariantExceptionSafetyTest, Swap) { } } // namespace -} // inline namespace lts_2018_06_20 +} // inline namespace lts_2018_12_18 } // namespace absl + +#endif // !defined(ABSL_INTERNAL_MSVC_2017_DBG_MODE) |