Export of internal Abseil changes.
-- eca34da4ccb7bb6a580f1364dff9ca053418fa3b by Abseil Team <absl-team@google.com>: Internal change. PiperOrigin-RevId: 214305433 -- 35393bdd21a87c4286f945fd34dda93afc4e0cd6 by Abseil Team <absl-team@google.com>: Move some implementation details of string_view around to facilitate compiling on NVCC. Abseil does not officially support NVCC as a reminder. PiperOrigin-RevId: 214184876 -- 61846cab9ab9476a4676ecade7173f68978cd038 by Jorg Brown <jorg@google.com>: Move the initialization values for constants back to their declaration. PiperOrigin-RevId: 214135927 -- 7ac7df6c5f78f2faf419268c04618b936cb26065 by Abseil Team <absl-team@google.com>: Performance improvements on format parser. PiperOrigin-RevId: 214032366 -- 90b4c0cf20e9feaa257a7ece40adaf7db40a60a7 by Xiaoyi Zhang <zhangxy@google.com>: Add static_assert check to absl::visit to make sure all overloads of the visitor return the same type, as required by the C++ standard. PiperOrigin-RevId: 213677001 -- 787995342101b4c181291cde9ecea3048536e4bd by Abseil Team <absl-team@google.com>: Update comment to indicate finite durations are less than InfiniteDuration. PiperOrigin-RevId: 213660328 -- d78f0dce7cc31218807e96d93b9e8513b6c80b24 by Jon Cohen <cohenjon@google.com>: s/invariant/contract in the exceptions safety testing framework. This is a better term as these can be type invariants or function post conditions. They also are very similar ground as to what is covered by c++20 Contracts (and could even be replaced by them. PiperOrigin-RevId: 213631019 -- 0b3ff1a640de9a7391a6c233568802cf86245b0e by Abseil Team <absl-team@google.com>: Add noinline attribute for GetStackTrace/GetStackFrames/... so the skipped frames will not change because of inlining difference. PiperOrigin-RevId: 213009637 GitOrigin-RevId: eca34da4ccb7bb6a580f1364dff9ca053418fa3b Change-Id: Iff1022fd24e440fcbdf3c4ab2a915ca8954daa31
This commit is contained in:
parent
8ff1374008
commit
e01d95528e
15 changed files with 251 additions and 223 deletions
|
@ -5,11 +5,11 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
|
|||
http_archive(
|
||||
name = "bazel_toolchains",
|
||||
urls = [
|
||||
"https://mirror.bazel.build/github.com/bazelbuild/bazel-toolchains/archive/287b64e0a211fb7c23b74695f8d5f5205b61f4eb.tar.gz",
|
||||
"https://github.com/bazelbuild/bazel-toolchains/archive/287b64e0a211fb7c23b74695f8d5f5205b61f4eb.tar.gz",
|
||||
"https://mirror.bazel.build/github.com/bazelbuild/bazel-toolchains/archive/bc09b995c137df042bb80a395b73d7ce6f26afbe.tar.gz",
|
||||
"https://github.com/bazelbuild/bazel-toolchains/archive/bc09b995c137df042bb80a395b73d7ce6f26afbe.tar.gz",
|
||||
],
|
||||
strip_prefix = "bazel-toolchains-287b64e0a211fb7c23b74695f8d5f5205b61f4eb",
|
||||
sha256 = "aca8ac6afd7745027ee4a43032b51a725a61a75a30f02cc58681ee87e4dcdf4b",
|
||||
strip_prefix = "bazel-toolchains-bc09b995c137df042bb80a395b73d7ce6f26afbe",
|
||||
sha256 = "4329663fe6c523425ad4d3c989a8ac026b04e1acedeceb56aa4b190fa7f3973c",
|
||||
)
|
||||
|
||||
# GoogleTest/GoogleMock framework. Used by most unit-tests.
|
||||
|
|
|
@ -548,21 +548,21 @@ TEST(ExceptionSafetyTesterTest, IncompleteTypesAreNotTestable) {
|
|||
// Test that providing operation and inveriants still does not allow for the
|
||||
// the invocation of .Test() and .Test(op) because it lacks a factory
|
||||
auto without_fac =
|
||||
testing::MakeExceptionSafetyTester().WithOperation(op).WithInvariants(
|
||||
testing::MakeExceptionSafetyTester().WithOperation(op).WithContracts(
|
||||
inv, testing::strong_guarantee);
|
||||
EXPECT_FALSE(HasNullaryTest(without_fac));
|
||||
EXPECT_FALSE(HasUnaryTest(without_fac));
|
||||
|
||||
// Test that providing invariants and factory allows the invocation of
|
||||
// Test that providing contracts and factory allows the invocation of
|
||||
// .Test(op) but does not allow for .Test() because it lacks an operation
|
||||
auto without_op = testing::MakeExceptionSafetyTester()
|
||||
.WithInvariants(inv, testing::strong_guarantee)
|
||||
.WithContracts(inv, testing::strong_guarantee)
|
||||
.WithFactory(fac);
|
||||
EXPECT_FALSE(HasNullaryTest(without_op));
|
||||
EXPECT_TRUE(HasUnaryTest(without_op));
|
||||
|
||||
// Test that providing operation and factory still does not allow for the
|
||||
// the invocation of .Test() and .Test(op) because it lacks invariants
|
||||
// the invocation of .Test() and .Test(op) because it lacks contracts
|
||||
auto without_inv =
|
||||
testing::MakeExceptionSafetyTester().WithOperation(op).WithFactory(fac);
|
||||
EXPECT_FALSE(HasNullaryTest(without_inv));
|
||||
|
@ -577,7 +577,7 @@ std::unique_ptr<ExampleStruct> ExampleFunctionFactory() {
|
|||
|
||||
void ExampleFunctionOperation(ExampleStruct*) {}
|
||||
|
||||
testing::AssertionResult ExampleFunctionInvariant(ExampleStruct*) {
|
||||
testing::AssertionResult ExampleFunctionContract(ExampleStruct*) {
|
||||
return testing::AssertionSuccess();
|
||||
}
|
||||
|
||||
|
@ -593,16 +593,16 @@ struct {
|
|||
|
||||
struct {
|
||||
testing::AssertionResult operator()(ExampleStruct* example_struct) const {
|
||||
return ExampleFunctionInvariant(example_struct);
|
||||
return ExampleFunctionContract(example_struct);
|
||||
}
|
||||
} example_struct_invariant;
|
||||
} example_struct_contract;
|
||||
|
||||
auto example_lambda_factory = []() { return ExampleFunctionFactory(); };
|
||||
|
||||
auto example_lambda_operation = [](ExampleStruct*) {};
|
||||
|
||||
auto example_lambda_invariant = [](ExampleStruct* example_struct) {
|
||||
return ExampleFunctionInvariant(example_struct);
|
||||
auto example_lambda_contract = [](ExampleStruct* example_struct) {
|
||||
return ExampleFunctionContract(example_struct);
|
||||
};
|
||||
|
||||
// Testing that function references, pointers, structs with operator() and
|
||||
|
@ -612,28 +612,28 @@ TEST(ExceptionSafetyTesterTest, MixedFunctionTypes) {
|
|||
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
||||
.WithFactory(ExampleFunctionFactory)
|
||||
.WithOperation(ExampleFunctionOperation)
|
||||
.WithInvariants(ExampleFunctionInvariant)
|
||||
.WithContracts(ExampleFunctionContract)
|
||||
.Test());
|
||||
|
||||
// function pointer
|
||||
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
||||
.WithFactory(&ExampleFunctionFactory)
|
||||
.WithOperation(&ExampleFunctionOperation)
|
||||
.WithInvariants(&ExampleFunctionInvariant)
|
||||
.WithContracts(&ExampleFunctionContract)
|
||||
.Test());
|
||||
|
||||
// struct
|
||||
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
||||
.WithFactory(example_struct_factory)
|
||||
.WithOperation(example_struct_operation)
|
||||
.WithInvariants(example_struct_invariant)
|
||||
.WithContracts(example_struct_contract)
|
||||
.Test());
|
||||
|
||||
// lambda
|
||||
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
||||
.WithFactory(example_lambda_factory)
|
||||
.WithOperation(example_lambda_operation)
|
||||
.WithInvariants(example_lambda_invariant)
|
||||
.WithContracts(example_lambda_contract)
|
||||
.Test());
|
||||
}
|
||||
|
||||
|
@ -658,9 +658,9 @@ struct {
|
|||
} invoker;
|
||||
|
||||
auto tester =
|
||||
testing::MakeExceptionSafetyTester().WithOperation(invoker).WithInvariants(
|
||||
testing::MakeExceptionSafetyTester().WithOperation(invoker).WithContracts(
|
||||
CheckNonNegativeInvariants);
|
||||
auto strong_tester = tester.WithInvariants(testing::strong_guarantee);
|
||||
auto strong_tester = tester.WithContracts(testing::strong_guarantee);
|
||||
|
||||
struct FailsBasicGuarantee : public NonNegative {
|
||||
void operator()() {
|
||||
|
@ -690,7 +690,7 @@ TEST(ExceptionCheckTest, StrongGuaranteeFailure) {
|
|||
EXPECT_FALSE(strong_tester.WithInitialValue(FollowsBasicGuarantee{}).Test());
|
||||
}
|
||||
|
||||
struct BasicGuaranteeWithExtraInvariants : public NonNegative {
|
||||
struct BasicGuaranteeWithExtraContracts : public NonNegative {
|
||||
// After operator(), i is incremented. If operator() throws, i is set to 9999
|
||||
void operator()() {
|
||||
int old_i = i;
|
||||
|
@ -701,21 +701,21 @@ struct BasicGuaranteeWithExtraInvariants : public NonNegative {
|
|||
|
||||
static constexpr int kExceptionSentinel = 9999;
|
||||
};
|
||||
constexpr int BasicGuaranteeWithExtraInvariants::kExceptionSentinel;
|
||||
constexpr int BasicGuaranteeWithExtraContracts::kExceptionSentinel;
|
||||
|
||||
TEST(ExceptionCheckTest, BasicGuaranteeWithExtraInvariants) {
|
||||
TEST(ExceptionCheckTest, BasicGuaranteeWithExtraContracts) {
|
||||
auto tester_with_val =
|
||||
tester.WithInitialValue(BasicGuaranteeWithExtraInvariants{});
|
||||
tester.WithInitialValue(BasicGuaranteeWithExtraContracts{});
|
||||
EXPECT_TRUE(tester_with_val.Test());
|
||||
EXPECT_TRUE(
|
||||
tester_with_val
|
||||
.WithInvariants([](BasicGuaranteeWithExtraInvariants* o) {
|
||||
if (o->i == BasicGuaranteeWithExtraInvariants::kExceptionSentinel) {
|
||||
.WithContracts([](BasicGuaranteeWithExtraContracts* o) {
|
||||
if (o->i == BasicGuaranteeWithExtraContracts::kExceptionSentinel) {
|
||||
return testing::AssertionSuccess();
|
||||
}
|
||||
return testing::AssertionFailure()
|
||||
<< "i should be "
|
||||
<< BasicGuaranteeWithExtraInvariants::kExceptionSentinel
|
||||
<< BasicGuaranteeWithExtraContracts::kExceptionSentinel
|
||||
<< ", but is " << o->i;
|
||||
})
|
||||
.Test());
|
||||
|
@ -740,7 +740,7 @@ struct HasReset : public NonNegative {
|
|||
void reset() { i = 0; }
|
||||
};
|
||||
|
||||
testing::AssertionResult CheckHasResetInvariants(HasReset* h) {
|
||||
testing::AssertionResult CheckHasResetContracts(HasReset* h) {
|
||||
h->reset();
|
||||
return testing::AssertionResult(h->i == 0);
|
||||
}
|
||||
|
@ -759,14 +759,14 @@ TEST(ExceptionCheckTest, ModifyingChecker) {
|
|||
};
|
||||
|
||||
EXPECT_FALSE(tester.WithInitialValue(FollowsBasicGuarantee{})
|
||||
.WithInvariants(set_to_1000, is_1000)
|
||||
.WithContracts(set_to_1000, is_1000)
|
||||
.Test());
|
||||
EXPECT_TRUE(strong_tester.WithInitialValue(FollowsStrongGuarantee{})
|
||||
.WithInvariants(increment)
|
||||
.WithContracts(increment)
|
||||
.Test());
|
||||
EXPECT_TRUE(testing::MakeExceptionSafetyTester()
|
||||
.WithInitialValue(HasReset{})
|
||||
.WithInvariants(CheckHasResetInvariants)
|
||||
.WithContracts(CheckHasResetContracts)
|
||||
.Test(invoker));
|
||||
}
|
||||
|
||||
|
@ -799,7 +799,7 @@ TEST(ExceptionCheckTest, NonEqualityComparable) {
|
|||
return testing::AssertionResult(nec->i == NonEqualityComparable().i);
|
||||
};
|
||||
auto strong_nec_tester = tester.WithInitialValue(NonEqualityComparable{})
|
||||
.WithInvariants(nec_is_strong);
|
||||
.WithContracts(nec_is_strong);
|
||||
|
||||
EXPECT_TRUE(strong_nec_tester.Test());
|
||||
EXPECT_FALSE(strong_nec_tester.Test(
|
||||
|
@ -833,14 +833,14 @@ struct {
|
|||
testing::AssertionResult operator()(ExhaustivenessTester<T>*) const {
|
||||
return testing::AssertionSuccess();
|
||||
}
|
||||
} CheckExhaustivenessTesterInvariants;
|
||||
} CheckExhaustivenessTesterContracts;
|
||||
|
||||
template <typename T>
|
||||
unsigned char ExhaustivenessTester<T>::successes = 0;
|
||||
|
||||
TEST(ExceptionCheckTest, Exhaustiveness) {
|
||||
auto exhaust_tester = testing::MakeExceptionSafetyTester()
|
||||
.WithInvariants(CheckExhaustivenessTesterInvariants)
|
||||
.WithContracts(CheckExhaustivenessTesterContracts)
|
||||
.WithOperation(invoker);
|
||||
|
||||
EXPECT_TRUE(
|
||||
|
@ -849,7 +849,7 @@ TEST(ExceptionCheckTest, Exhaustiveness) {
|
|||
|
||||
EXPECT_TRUE(
|
||||
exhaust_tester.WithInitialValue(ExhaustivenessTester<ThrowingValue<>>{})
|
||||
.WithInvariants(testing::strong_guarantee)
|
||||
.WithContracts(testing::strong_guarantee)
|
||||
.Test());
|
||||
EXPECT_EQ(ExhaustivenessTester<ThrowingValue<>>::successes, 0xF);
|
||||
}
|
||||
|
|
|
@ -191,19 +191,19 @@ class TrackedObject {
|
|||
~TrackedObject() noexcept { ConstructorTracker::ObjectDestructed(this); }
|
||||
};
|
||||
|
||||
template <typename Factory, typename Operation, typename Invariant>
|
||||
absl::optional<testing::AssertionResult> TestSingleInvariantAtCountdownImpl(
|
||||
template <typename Factory, typename Operation, typename Contract>
|
||||
absl::optional<testing::AssertionResult> TestSingleContractAtCountdownImpl(
|
||||
const Factory& factory, const Operation& operation, int count,
|
||||
const Invariant& invariant) {
|
||||
const Contract& contract) {
|
||||
auto t_ptr = factory();
|
||||
absl::optional<testing::AssertionResult> current_res;
|
||||
SetCountdown(count);
|
||||
try {
|
||||
operation(t_ptr.get());
|
||||
} catch (const exceptions_internal::TestException& e) {
|
||||
current_res.emplace(invariant(t_ptr.get()));
|
||||
current_res.emplace(contract(t_ptr.get()));
|
||||
if (!current_res.value()) {
|
||||
*current_res << e.what() << " failed invariant check";
|
||||
*current_res << e.what() << " failed contract check";
|
||||
}
|
||||
}
|
||||
UnsetCountdown();
|
||||
|
@ -211,22 +211,22 @@ absl::optional<testing::AssertionResult> TestSingleInvariantAtCountdownImpl(
|
|||
}
|
||||
|
||||
template <typename Factory, typename Operation>
|
||||
absl::optional<testing::AssertionResult> TestSingleInvariantAtCountdownImpl(
|
||||
absl::optional<testing::AssertionResult> TestSingleContractAtCountdownImpl(
|
||||
const Factory& factory, const Operation& operation, int count,
|
||||
StrongGuaranteeTagType) {
|
||||
using TPtr = typename decltype(factory())::pointer;
|
||||
auto t_is_strong = [&](TPtr t) { return *t == *factory(); };
|
||||
return TestSingleInvariantAtCountdownImpl(factory, operation, count,
|
||||
t_is_strong);
|
||||
return TestSingleContractAtCountdownImpl(factory, operation, count,
|
||||
t_is_strong);
|
||||
}
|
||||
|
||||
template <typename Factory, typename Operation, typename Invariant>
|
||||
int TestSingleInvariantAtCountdown(
|
||||
template <typename Factory, typename Operation, typename Contract>
|
||||
int TestSingleContractAtCountdown(
|
||||
const Factory& factory, const Operation& operation, int count,
|
||||
const Invariant& invariant,
|
||||
const Contract& contract,
|
||||
absl::optional<testing::AssertionResult>* reduced_res) {
|
||||
// If reduced_res is empty, it means the current call to
|
||||
// TestSingleInvariantAtCountdown(...) is the first test being run so we do
|
||||
// TestSingleContractAtCountdown(...) is the first test being run so we do
|
||||
// want to run it. Alternatively, if it's not empty (meaning a previous test
|
||||
// has run) we want to check if it passed. If the previous test did pass, we
|
||||
// want to contine running tests so we do want to run the current one. If it
|
||||
|
@ -234,22 +234,22 @@ int TestSingleInvariantAtCountdown(
|
|||
// output. If that's the case, we do not run the current test and instead we
|
||||
// simply return.
|
||||
if (!reduced_res->has_value() || reduced_res->value()) {
|
||||
*reduced_res = TestSingleInvariantAtCountdownImpl(factory, operation, count,
|
||||
invariant);
|
||||
*reduced_res =
|
||||
TestSingleContractAtCountdownImpl(factory, operation, count, contract);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename Factory, typename Operation, typename... Invariants>
|
||||
inline absl::optional<testing::AssertionResult> TestAllInvariantsAtCountdown(
|
||||
template <typename Factory, typename Operation, typename... Contracts>
|
||||
inline absl::optional<testing::AssertionResult> TestAllContractsAtCountdown(
|
||||
const Factory& factory, const Operation& operation, int count,
|
||||
const Invariants&... invariants) {
|
||||
const Contracts&... contracts) {
|
||||
absl::optional<testing::AssertionResult> reduced_res;
|
||||
|
||||
// Run each checker, short circuiting after the first failure
|
||||
int dummy[] = {
|
||||
0, (TestSingleInvariantAtCountdown(factory, operation, count, invariants,
|
||||
&reduced_res))...};
|
||||
0, (TestSingleContractAtCountdown(factory, operation, count, contracts,
|
||||
&reduced_res))...};
|
||||
static_cast<void>(dummy);
|
||||
return reduced_res;
|
||||
}
|
||||
|
@ -884,15 +884,15 @@ class DefaultFactory {
|
|||
T t_;
|
||||
};
|
||||
|
||||
template <size_t LazyInvariantsCount, typename LazyFactory,
|
||||
template <size_t LazyContractsCount, typename LazyFactory,
|
||||
typename LazyOperation>
|
||||
using EnableIfTestable = typename absl::enable_if_t<
|
||||
LazyInvariantsCount != 0 &&
|
||||
LazyContractsCount != 0 &&
|
||||
!std::is_same<LazyFactory, UninitializedT>::value &&
|
||||
!std::is_same<LazyOperation, UninitializedT>::value>;
|
||||
|
||||
template <typename Factory = UninitializedT,
|
||||
typename Operation = UninitializedT, typename... Invariants>
|
||||
typename Operation = UninitializedT, typename... Contracts>
|
||||
class ExceptionSafetyTester;
|
||||
|
||||
} // namespace exceptions_internal
|
||||
|
@ -903,7 +903,7 @@ namespace exceptions_internal {
|
|||
|
||||
/*
|
||||
* Builds a tester object that tests if performing a operation on a T follows
|
||||
* exception safety guarantees. Verification is done via invariant assertion
|
||||
* exception safety guarantees. Verification is done via contract assertion
|
||||
* callbacks applied to T instances post-throw.
|
||||
*
|
||||
* Template parameters for ExceptionSafetyTester:
|
||||
|
@ -921,18 +921,18 @@ namespace exceptions_internal {
|
|||
* fresh T instance so it's free to modify and destroy the T instances as it
|
||||
* pleases.
|
||||
*
|
||||
* - Invariants...: The invariant assertion callback objects (passed in via
|
||||
* tester.WithInvariants(...)) must be invocable with the signature
|
||||
* - Contracts...: The contract assertion callback objects (passed in via
|
||||
* tester.WithContracts(...)) must be invocable with the signature
|
||||
* `testing::AssertionResult operator()(T*) const` where T is the type being
|
||||
* tested. Invariant assertion callbacks are provided T instances post-throw.
|
||||
* They must return testing::AssertionSuccess when the type invariants of the
|
||||
* provided T instance hold. If the type invariants of the T instance do not
|
||||
* tested. Contract assertion callbacks are provided T instances post-throw.
|
||||
* They must return testing::AssertionSuccess when the type contracts of the
|
||||
* provided T instance hold. If the type contracts of the T instance do not
|
||||
* hold, they must return testing::AssertionFailure. Execution order of
|
||||
* Invariants... is unspecified. They will each individually get a fresh T
|
||||
* Contracts... is unspecified. They will each individually get a fresh T
|
||||
* instance so they are free to modify and destroy the T instances as they
|
||||
* please.
|
||||
*/
|
||||
template <typename Factory, typename Operation, typename... Invariants>
|
||||
template <typename Factory, typename Operation, typename... Contracts>
|
||||
class ExceptionSafetyTester {
|
||||
public:
|
||||
/*
|
||||
|
@ -948,7 +948,7 @@ class ExceptionSafetyTester {
|
|||
* tester.WithFactory(...).
|
||||
*/
|
||||
template <typename T>
|
||||
ExceptionSafetyTester<DefaultFactory<T>, Operation, Invariants...>
|
||||
ExceptionSafetyTester<DefaultFactory<T>, Operation, Contracts...>
|
||||
WithInitialValue(const T& t) const {
|
||||
return WithFactory(DefaultFactory<T>(t));
|
||||
}
|
||||
|
@ -961,9 +961,9 @@ class ExceptionSafetyTester {
|
|||
* method tester.WithInitialValue(...).
|
||||
*/
|
||||
template <typename NewFactory>
|
||||
ExceptionSafetyTester<absl::decay_t<NewFactory>, Operation, Invariants...>
|
||||
ExceptionSafetyTester<absl::decay_t<NewFactory>, Operation, Contracts...>
|
||||
WithFactory(const NewFactory& new_factory) const {
|
||||
return {new_factory, operation_, invariants_};
|
||||
return {new_factory, operation_, contracts_};
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -972,39 +972,39 @@ class ExceptionSafetyTester {
|
|||
* tester.
|
||||
*/
|
||||
template <typename NewOperation>
|
||||
ExceptionSafetyTester<Factory, absl::decay_t<NewOperation>, Invariants...>
|
||||
ExceptionSafetyTester<Factory, absl::decay_t<NewOperation>, Contracts...>
|
||||
WithOperation(const NewOperation& new_operation) const {
|
||||
return {factory_, new_operation, invariants_};
|
||||
return {factory_, new_operation, contracts_};
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns a new ExceptionSafetyTester with the provided MoreInvariants...
|
||||
* combined with the Invariants... that were already included in the instance
|
||||
* on which the method was called. Invariants... cannot be removed or replaced
|
||||
* Returns a new ExceptionSafetyTester with the provided MoreContracts...
|
||||
* combined with the Contracts... that were already included in the instance
|
||||
* on which the method was called. Contracts... cannot be removed or replaced
|
||||
* once added to an ExceptionSafetyTester instance. A fresh object must be
|
||||
* created in order to get an empty Invariants... list.
|
||||
* created in order to get an empty Contracts... list.
|
||||
*
|
||||
* In addition to passing in custom invariant assertion callbacks, this method
|
||||
* In addition to passing in custom contract assertion callbacks, this method
|
||||
* accepts `testing::strong_guarantee` as an argument which checks T instances
|
||||
* post-throw against freshly created T instances via operator== to verify
|
||||
* that any state changes made during the execution of the operation were
|
||||
* properly rolled back.
|
||||
*/
|
||||
template <typename... MoreInvariants>
|
||||
ExceptionSafetyTester<Factory, Operation, Invariants...,
|
||||
absl::decay_t<MoreInvariants>...>
|
||||
WithInvariants(const MoreInvariants&... more_invariants) const {
|
||||
return {factory_, operation_,
|
||||
std::tuple_cat(invariants_,
|
||||
std::tuple<absl::decay_t<MoreInvariants>...>(
|
||||
more_invariants...))};
|
||||
template <typename... MoreContracts>
|
||||
ExceptionSafetyTester<Factory, Operation, Contracts...,
|
||||
absl::decay_t<MoreContracts>...>
|
||||
WithContracts(const MoreContracts&... more_contracts) const {
|
||||
return {
|
||||
factory_, operation_,
|
||||
std::tuple_cat(contracts_, std::tuple<absl::decay_t<MoreContracts>...>(
|
||||
more_contracts...))};
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns a testing::AssertionResult that is the reduced result of the
|
||||
* exception safety algorithm. The algorithm short circuits and returns
|
||||
* AssertionFailure after the first invariant callback returns an
|
||||
* AssertionFailure. Otherwise, if all invariant callbacks return an
|
||||
* AssertionFailure after the first contract callback returns an
|
||||
* AssertionFailure. Otherwise, if all contract callbacks return an
|
||||
* AssertionSuccess, the reduced result is AssertionSuccess.
|
||||
*
|
||||
* The passed-in testable operation will not be saved in a new tester instance
|
||||
|
@ -1013,33 +1013,33 @@ class ExceptionSafetyTester {
|
|||
*
|
||||
* Preconditions for tester.Test(const NewOperation& new_operation):
|
||||
*
|
||||
* - May only be called after at least one invariant assertion callback and a
|
||||
* - May only be called after at least one contract assertion callback and a
|
||||
* factory or initial value have been provided.
|
||||
*/
|
||||
template <
|
||||
typename NewOperation,
|
||||
typename = EnableIfTestable<sizeof...(Invariants), Factory, NewOperation>>
|
||||
typename = EnableIfTestable<sizeof...(Contracts), Factory, NewOperation>>
|
||||
testing::AssertionResult Test(const NewOperation& new_operation) const {
|
||||
return TestImpl(new_operation, absl::index_sequence_for<Invariants...>());
|
||||
return TestImpl(new_operation, absl::index_sequence_for<Contracts...>());
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns a testing::AssertionResult that is the reduced result of the
|
||||
* exception safety algorithm. The algorithm short circuits and returns
|
||||
* AssertionFailure after the first invariant callback returns an
|
||||
* AssertionFailure. Otherwise, if all invariant callbacks return an
|
||||
* AssertionFailure after the first contract callback returns an
|
||||
* AssertionFailure. Otherwise, if all contract callbacks return an
|
||||
* AssertionSuccess, the reduced result is AssertionSuccess.
|
||||
*
|
||||
* Preconditions for tester.Test():
|
||||
*
|
||||
* - May only be called after at least one invariant assertion callback, a
|
||||
* - May only be called after at least one contract assertion callback, a
|
||||
* factory or initial value and a testable operation have been provided.
|
||||
*/
|
||||
template <typename LazyOperation = Operation,
|
||||
typename =
|
||||
EnableIfTestable<sizeof...(Invariants), Factory, LazyOperation>>
|
||||
template <
|
||||
typename LazyOperation = Operation,
|
||||
typename = EnableIfTestable<sizeof...(Contracts), Factory, LazyOperation>>
|
||||
testing::AssertionResult Test() const {
|
||||
return TestImpl(operation_, absl::index_sequence_for<Invariants...>());
|
||||
return TestImpl(operation_, absl::index_sequence_for<Contracts...>());
|
||||
}
|
||||
|
||||
private:
|
||||
|
@ -1051,8 +1051,8 @@ class ExceptionSafetyTester {
|
|||
ExceptionSafetyTester() {}
|
||||
|
||||
ExceptionSafetyTester(const Factory& f, const Operation& o,
|
||||
const std::tuple<Invariants...>& i)
|
||||
: factory_(f), operation_(o), invariants_(i) {}
|
||||
const std::tuple<Contracts...>& i)
|
||||
: factory_(f), operation_(o), contracts_(i) {}
|
||||
|
||||
template <typename SelectedOperation, size_t... Indices>
|
||||
testing::AssertionResult TestImpl(const SelectedOperation& selected_operation,
|
||||
|
@ -1064,28 +1064,28 @@ class ExceptionSafetyTester {
|
|||
|
||||
// Run the full exception safety test algorithm for the current countdown
|
||||
auto reduced_res =
|
||||
TestAllInvariantsAtCountdown(factory_, selected_operation, count,
|
||||
std::get<Indices>(invariants_)...);
|
||||
// If there is no value in the optional, no invariants were run because no
|
||||
TestAllContractsAtCountdown(factory_, selected_operation, count,
|
||||
std::get<Indices>(contracts_)...);
|
||||
// If there is no value in the optional, no contracts were run because no
|
||||
// exception was thrown. This means that the test is complete and the loop
|
||||
// can exit successfully.
|
||||
if (!reduced_res.has_value()) {
|
||||
return testing::AssertionSuccess();
|
||||
}
|
||||
// If the optional is not empty and the value is falsy, an invariant check
|
||||
// If the optional is not empty and the value is falsy, an contract check
|
||||
// failed so the test must exit to propegate the failure.
|
||||
if (!reduced_res.value()) {
|
||||
return reduced_res.value();
|
||||
}
|
||||
// If the optional is not empty and the value is not falsy, it means
|
||||
// exceptions were thrown but the invariants passed so the test must
|
||||
// exceptions were thrown but the contracts passed so the test must
|
||||
// continue to run.
|
||||
}
|
||||
}
|
||||
|
||||
Factory factory_;
|
||||
Operation operation_;
|
||||
std::tuple<Invariants...> invariants_;
|
||||
std::tuple<Contracts...> contracts_;
|
||||
};
|
||||
|
||||
} // namespace exceptions_internal
|
||||
|
@ -1096,7 +1096,7 @@ class ExceptionSafetyTester {
|
|||
* instances of ExceptionSafetyTester.
|
||||
*
|
||||
* In order to test a T for exception safety, a factory for that T, a testable
|
||||
* operation, and at least one invariant callback returning an assertion
|
||||
* operation, and at least one contract callback returning an assertion
|
||||
* result must be applied using the respective methods.
|
||||
*/
|
||||
inline exceptions_internal::ExceptionSafetyTester<>
|
||||
|
|
|
@ -97,7 +97,7 @@ testing::AssertionResult ReadMemory(FixedArr* fixed_arr) {
|
|||
|
||||
TEST(FixedArrayExceptionSafety, Fill) {
|
||||
auto test_fill = testing::MakeExceptionSafetyTester()
|
||||
.WithInvariants(ReadMemory)
|
||||
.WithContracts(ReadMemory)
|
||||
.WithOperation([&](FixedArr* fixed_arr_ptr) {
|
||||
auto thrower =
|
||||
Thrower(kUpdatedValue, testing::nothrow_ctor);
|
||||
|
|
|
@ -80,11 +80,13 @@ ABSL_ATTRIBUTE_ALWAYS_INLINE inline int Unwind(void** result, int* sizes,
|
|||
|
||||
} // anonymous namespace
|
||||
|
||||
ABSL_ATTRIBUTE_NOINLINE
|
||||
int GetStackFrames(void** result, int* sizes, int max_depth, int skip_count) {
|
||||
return Unwind<true, false>(result, sizes, max_depth, skip_count, nullptr,
|
||||
nullptr);
|
||||
}
|
||||
|
||||
ABSL_ATTRIBUTE_NOINLINE
|
||||
int GetStackFramesWithContext(void** result, int* sizes, int max_depth,
|
||||
int skip_count, const void* uc,
|
||||
int* min_dropped_frames) {
|
||||
|
@ -92,11 +94,13 @@ int GetStackFramesWithContext(void** result, int* sizes, int max_depth,
|
|||
min_dropped_frames);
|
||||
}
|
||||
|
||||
ABSL_ATTRIBUTE_NOINLINE
|
||||
int GetStackTrace(void** result, int max_depth, int skip_count) {
|
||||
return Unwind<false, false>(result, nullptr, max_depth, skip_count, nullptr,
|
||||
nullptr);
|
||||
}
|
||||
|
||||
ABSL_ATTRIBUTE_NOINLINE
|
||||
int GetStackTraceWithContext(void** result, int max_depth, int skip_count,
|
||||
const void* uc, int* min_dropped_frames) {
|
||||
return Unwind<false, true>(result, nullptr, max_depth, skip_count, uc,
|
||||
|
|
|
@ -32,7 +32,7 @@ TEST(MakeUnique, CheckForLeaks) {
|
|||
.WithInitialValue(Thrower(kValue))
|
||||
// Ensures make_unique does not modify the input. The real
|
||||
// test, though, is ConstructorTracker checking for leaks.
|
||||
.WithInvariants(testing::strong_guarantee);
|
||||
.WithContracts(testing::strong_guarantee);
|
||||
|
||||
EXPECT_TRUE(tester.Test([](Thrower* thrower) {
|
||||
static_cast<void>(absl::make_unique<Thrower>(*thrower));
|
||||
|
|
|
@ -84,16 +84,24 @@ bool ConsumeConversion(string_view *src, UnboundConversion *conv,
|
|||
const char *pos = src->data();
|
||||
const char *const end = pos + src->size();
|
||||
char c;
|
||||
// Read the next char into `c` and update `pos`. Reads '\0' if at end.
|
||||
const auto get_char = [&] { c = pos == end ? '\0' : *pos++; };
|
||||
// Read the next char into `c` and update `pos`. Returns false if there are
|
||||
// no more chars to read.
|
||||
#define ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR() \
|
||||
do { \
|
||||
if (ABSL_PREDICT_FALSE(pos == end)) return false; \
|
||||
c = *pos++; \
|
||||
} while (0)
|
||||
|
||||
const auto parse_digits = [&] {
|
||||
int digits = c - '0';
|
||||
// We do not want to overflow `digits` so we consume at most digits10-1
|
||||
// We do not want to overflow `digits` so we consume at most digits10
|
||||
// digits. If there are more digits the parsing will fail later on when the
|
||||
// digit doesn't match the expected characters.
|
||||
int num_digits = std::numeric_limits<int>::digits10 - 2;
|
||||
for (get_char(); num_digits && std::isdigit(c); get_char()) {
|
||||
int num_digits = std::numeric_limits<int>::digits10;
|
||||
for (;;) {
|
||||
if (ABSL_PREDICT_FALSE(pos == end || !num_digits)) break;
|
||||
c = *pos++;
|
||||
if (!std::isdigit(c)) break;
|
||||
--num_digits;
|
||||
digits = 10 * digits + c - '0';
|
||||
}
|
||||
|
@ -101,14 +109,14 @@ bool ConsumeConversion(string_view *src, UnboundConversion *conv,
|
|||
};
|
||||
|
||||
if (is_positional) {
|
||||
get_char();
|
||||
if (c < '1' || c > '9') return false;
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
if (ABSL_PREDICT_FALSE(c < '1' || c > '9')) return false;
|
||||
conv->arg_position = parse_digits();
|
||||
assert(conv->arg_position > 0);
|
||||
if (c != '$') return false;
|
||||
if (ABSL_PREDICT_FALSE(c != '$')) return false;
|
||||
}
|
||||
|
||||
get_char();
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
|
||||
// We should start with the basic flag on.
|
||||
assert(conv->flags.basic);
|
||||
|
@ -119,32 +127,39 @@ bool ConsumeConversion(string_view *src, UnboundConversion *conv,
|
|||
if (c < 'A') {
|
||||
conv->flags.basic = false;
|
||||
|
||||
for (; c <= '0'; get_char()) {
|
||||
for (; c <= '0';) {
|
||||
// FIXME: We might be able to speed this up reusing the kIds lookup table
|
||||
// from above.
|
||||
// It might require changing Flags to be a plain integer where we can |= a
|
||||
// value.
|
||||
switch (c) {
|
||||
case '-':
|
||||
conv->flags.left = true;
|
||||
continue;
|
||||
break;
|
||||
case '+':
|
||||
conv->flags.show_pos = true;
|
||||
continue;
|
||||
break;
|
||||
case ' ':
|
||||
conv->flags.sign_col = true;
|
||||
continue;
|
||||
break;
|
||||
case '#':
|
||||
conv->flags.alt = true;
|
||||
continue;
|
||||
break;
|
||||
case '0':
|
||||
conv->flags.zero = true;
|
||||
continue;
|
||||
break;
|
||||
default:
|
||||
goto flags_done;
|
||||
}
|
||||
break;
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
}
|
||||
flags_done:
|
||||
|
||||
if (c <= '9') {
|
||||
if (c >= '0') {
|
||||
int maybe_width = parse_digits();
|
||||
if (!is_positional && c == '$') {
|
||||
if (*next_arg != 0) return false;
|
||||
if (ABSL_PREDICT_FALSE(*next_arg != 0)) return false;
|
||||
// Positional conversion.
|
||||
*next_arg = -1;
|
||||
conv->flags = Flags();
|
||||
|
@ -153,12 +168,12 @@ bool ConsumeConversion(string_view *src, UnboundConversion *conv,
|
|||
}
|
||||
conv->width.set_value(maybe_width);
|
||||
} else if (c == '*') {
|
||||
get_char();
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
if (is_positional) {
|
||||
if (c < '1' || c > '9') return false;
|
||||
if (ABSL_PREDICT_FALSE(c < '1' || c > '9')) return false;
|
||||
conv->width.set_from_arg(parse_digits());
|
||||
if (c != '$') return false;
|
||||
get_char();
|
||||
if (ABSL_PREDICT_FALSE(c != '$')) return false;
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
} else {
|
||||
conv->width.set_from_arg(++*next_arg);
|
||||
}
|
||||
|
@ -166,16 +181,16 @@ bool ConsumeConversion(string_view *src, UnboundConversion *conv,
|
|||
}
|
||||
|
||||
if (c == '.') {
|
||||
get_char();
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
if (std::isdigit(c)) {
|
||||
conv->precision.set_value(parse_digits());
|
||||
} else if (c == '*') {
|
||||
get_char();
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
if (is_positional) {
|
||||
if (c < '1' || c > '9') return false;
|
||||
if (ABSL_PREDICT_FALSE(c < '1' || c > '9')) return false;
|
||||
conv->precision.set_from_arg(parse_digits());
|
||||
if (c != '$') return false;
|
||||
get_char();
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
} else {
|
||||
conv->precision.set_from_arg(++*next_arg);
|
||||
}
|
||||
|
@ -188,23 +203,23 @@ bool ConsumeConversion(string_view *src, UnboundConversion *conv,
|
|||
std::int8_t id = kIds[static_cast<unsigned char>(c)];
|
||||
|
||||
if (id < 0) {
|
||||
if (id == none) return false;
|
||||
if (ABSL_PREDICT_FALSE(id == none)) return false;
|
||||
|
||||
// It is a length modifier.
|
||||
using str_format_internal::LengthMod;
|
||||
LengthMod length_mod = LengthMod::FromId(static_cast<LM>(~id));
|
||||
get_char();
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
if (c == 'h' && length_mod.id() == LengthMod::h) {
|
||||
conv->length_mod = LengthMod::FromId(LengthMod::hh);
|
||||
get_char();
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
} else if (c == 'l' && length_mod.id() == LengthMod::l) {
|
||||
conv->length_mod = LengthMod::FromId(LengthMod::ll);
|
||||
get_char();
|
||||
ABSL_FORMAT_PARSER_INTERNAL_GET_CHAR();
|
||||
} else {
|
||||
conv->length_mod = length_mod;
|
||||
}
|
||||
id = kIds[static_cast<unsigned char>(c)];
|
||||
if (id < 0) return false;
|
||||
if (ABSL_PREDICT_FALSE(id < 0)) return false;
|
||||
}
|
||||
|
||||
assert(CheckFastPathSetting(*conv));
|
||||
|
|
|
@ -84,9 +84,9 @@ class ConsumeUnboundConversionTest : public ::testing::Test {
|
|||
TEST_F(ConsumeUnboundConversionTest, ConsumeSpecification) {
|
||||
struct Expectation {
|
||||
int line;
|
||||
const char *src;
|
||||
const char *out;
|
||||
const char *src_post;
|
||||
string_view src;
|
||||
string_view out;
|
||||
string_view src_post;
|
||||
};
|
||||
const Expectation kExpect[] = {
|
||||
{__LINE__, "", "", "" },
|
||||
|
@ -236,6 +236,16 @@ TEST_F(ConsumeUnboundConversionTest, WidthAndPrecision) {
|
|||
EXPECT_EQ(9, o.precision.get_from_arg());
|
||||
|
||||
EXPECT_FALSE(Run(".*0$d")) << "no arg 0";
|
||||
|
||||
// Large values
|
||||
EXPECT_TRUE(Run("999999999.999999999d"));
|
||||
EXPECT_FALSE(o.width.is_from_arg());
|
||||
EXPECT_EQ(999999999, o.width.value());
|
||||
EXPECT_FALSE(o.precision.is_from_arg());
|
||||
EXPECT_EQ(999999999, o.precision.value());
|
||||
|
||||
EXPECT_FALSE(Run("1000000000.999999999d"));
|
||||
EXPECT_FALSE(Run("999999999.1000000000d"));
|
||||
}
|
||||
|
||||
TEST_F(ConsumeUnboundConversionTest, Flags) {
|
||||
|
|
|
@ -173,8 +173,19 @@ class string_view {
|
|||
// Implicit constructor of a `string_view` from nul-terminated `str`. When
|
||||
// accepting possibly null strings, use `absl::NullSafeStringView(str)`
|
||||
// instead (see below).
|
||||
#if ABSL_HAVE_BUILTIN(__builtin_strlen) || \
|
||||
(defined(__GNUC__) && !defined(__clang__))
|
||||
// GCC has __builtin_strlen according to
|
||||
// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Other-Builtins.html, but
|
||||
// ABSL_HAVE_BUILTIN doesn't detect that, so we use the extra checks above.
|
||||
// __builtin_strlen is constexpr.
|
||||
constexpr string_view(const char* str) // NOLINT(runtime/explicit)
|
||||
: ptr_(str), length_(CheckLengthInternal(StrLenInternal(str))) {}
|
||||
: ptr_(str),
|
||||
length_(CheckLengthInternal(str ? __builtin_strlen(str) : 0)) {}
|
||||
#else
|
||||
constexpr string_view(const char* str) // NOLINT(runtime/explicit)
|
||||
: ptr_(str), length_(CheckLengthInternal(str ? strlen(str) : 0)) {}
|
||||
#endif
|
||||
|
||||
// Implicit constructor of a `string_view` from a `const char*` and length.
|
||||
constexpr string_view(const char* data, size_type len)
|
||||
|
@ -481,22 +492,6 @@ class string_view {
|
|||
static constexpr size_type kMaxSize =
|
||||
std::numeric_limits<difference_type>::max();
|
||||
|
||||
// check whether __builtin_strlen is provided by the compiler.
|
||||
// GCC doesn't have __has_builtin()
|
||||
// (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66970),
|
||||
// but has __builtin_strlen according to
|
||||
// https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/Other-Builtins.html.
|
||||
#if ABSL_HAVE_BUILTIN(__builtin_strlen) || \
|
||||
(defined(__GNUC__) && !defined(__clang__))
|
||||
static constexpr size_type StrLenInternal(const char* str) {
|
||||
return str ? __builtin_strlen(str) : 0;
|
||||
}
|
||||
#else
|
||||
static constexpr size_type StrLenInternal(const char* str) {
|
||||
return str ? strlen(str) : 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static constexpr size_type CheckLengthInternal(size_type len) {
|
||||
return ABSL_ASSERT(len <= kMaxSize), len;
|
||||
}
|
||||
|
|
|
@ -204,8 +204,7 @@ class NodeSet {
|
|||
}
|
||||
|
||||
private:
|
||||
static const int32_t kEmpty;
|
||||
static const int32_t kDel;
|
||||
enum : int32_t { kEmpty = -1, kDel = -2 };
|
||||
Vec<int32_t> table_;
|
||||
uint32_t occupied_; // Count of non-empty slots (includes deleted slots)
|
||||
|
||||
|
@ -255,9 +254,6 @@ class NodeSet {
|
|||
NodeSet& operator=(const NodeSet&) = delete;
|
||||
};
|
||||
|
||||
const int32_t NodeSet::kEmpty = -1;
|
||||
const int32_t NodeSet::kDel = -2;
|
||||
|
||||
// We encode a node index and a node version in GraphId. The version
|
||||
// number is incremented when the GraphId is freed which automatically
|
||||
// invalidates all copies of the GraphId.
|
||||
|
|
|
@ -322,6 +322,9 @@ Duration Ceil(Duration d, Duration unit);
|
|||
// 0 == d / inf
|
||||
// INT64_MAX == inf / d
|
||||
//
|
||||
// d < inf
|
||||
// -inf < d
|
||||
//
|
||||
// // Division by zero returns infinity, or INT64_MIN/MAX where appropriate.
|
||||
// inf == d / 0
|
||||
// INT64_MAX == d / absl::ZeroDuration()
|
||||
|
|
|
@ -107,7 +107,7 @@ TEST(AnyExceptionSafety, Assignment) {
|
|||
};
|
||||
auto any_strong_tester = testing::MakeExceptionSafetyTester()
|
||||
.WithInitialValue(original)
|
||||
.WithInvariants(AnyInvariants, any_is_strong);
|
||||
.WithContracts(AnyInvariants, any_is_strong);
|
||||
|
||||
Thrower val(2);
|
||||
absl::any any_val(val);
|
||||
|
@ -129,7 +129,7 @@ TEST(AnyExceptionSafety, Assignment) {
|
|||
auto strong_empty_any_tester =
|
||||
testing::MakeExceptionSafetyTester()
|
||||
.WithInitialValue(absl::any{})
|
||||
.WithInvariants(AnyInvariants, empty_any_is_strong);
|
||||
.WithContracts(AnyInvariants, empty_any_is_strong);
|
||||
|
||||
EXPECT_TRUE(strong_empty_any_tester.Test(assign_any));
|
||||
EXPECT_TRUE(strong_empty_any_tester.Test(assign_val));
|
||||
|
@ -142,7 +142,7 @@ TEST(AnyExceptionSafety, Emplace) {
|
|||
absl::any{absl::in_place_type_t<Thrower>(), 1, testing::nothrow_ctor};
|
||||
auto one_tester = testing::MakeExceptionSafetyTester()
|
||||
.WithInitialValue(initial_val)
|
||||
.WithInvariants(AnyInvariants, AnyIsEmpty);
|
||||
.WithContracts(AnyInvariants, AnyIsEmpty);
|
||||
|
||||
auto emp_thrower = [](absl::any* ap) { ap->emplace<Thrower>(2); };
|
||||
auto emp_throwervec = [](absl::any* ap) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
// Copyright 2017 The Abseil Authors.
|
||||
// Copyright 2018 The Abseil Authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
|
@ -910,6 +910,11 @@ struct PerformVisitation {
|
|||
template <std::size_t... TupIs, std::size_t... Is>
|
||||
constexpr ReturnType Run(std::false_type /*has_valueless*/,
|
||||
index_sequence<TupIs...>, SizeT<Is>...) const {
|
||||
static_assert(
|
||||
std::is_same<ReturnType,
|
||||
absl::result_of_t<Op(VariantAccessResult<
|
||||
Is, QualifiedVariants>...)>>::value,
|
||||
"All visitation overloads must have the same return type.");
|
||||
return absl::base_internal::Invoke(
|
||||
absl::forward<Op>(op),
|
||||
VariantCoreAccess::Access<Is>(
|
||||
|
|
|
@ -43,7 +43,7 @@ bool ValueThrowsBadOptionalAccess(const OptionalT& optional) try {
|
|||
}
|
||||
|
||||
template <typename OptionalT>
|
||||
AssertionResult CheckInvariants(OptionalT* optional_ptr) {
|
||||
AssertionResult OptionalInvariants(OptionalT* optional_ptr) {
|
||||
// Check the current state post-throw for validity
|
||||
auto& optional = *optional_ptr;
|
||||
|
||||
|
@ -123,8 +123,8 @@ TEST(OptionalExceptionSafety, NothrowConstructors) {
|
|||
TEST(OptionalExceptionSafety, Emplace) {
|
||||
// Test the basic guarantee plus test the result of optional::has_value()
|
||||
// is false in all cases
|
||||
auto disengaged_test = MakeExceptionSafetyTester().WithInvariants(
|
||||
CheckInvariants<Optional>, CheckDisengaged<Optional>);
|
||||
auto disengaged_test = MakeExceptionSafetyTester().WithContracts(
|
||||
OptionalInvariants<Optional>, CheckDisengaged<Optional>);
|
||||
auto disengaged_test_empty = disengaged_test.WithInitialValue(Optional());
|
||||
auto disengaged_test_nonempty =
|
||||
disengaged_test.WithInitialValue(Optional(kInitialInteger));
|
||||
|
@ -147,11 +147,11 @@ TEST(OptionalExceptionSafety, EverythingThrowsSwap) {
|
|||
// Test the basic guarantee plus test the result of optional::has_value()
|
||||
// remains the same
|
||||
auto test =
|
||||
MakeExceptionSafetyTester().WithInvariants(CheckInvariants<Optional>);
|
||||
MakeExceptionSafetyTester().WithContracts(OptionalInvariants<Optional>);
|
||||
auto disengaged_test_empty = test.WithInitialValue(Optional())
|
||||
.WithInvariants(CheckDisengaged<Optional>);
|
||||
.WithContracts(CheckDisengaged<Optional>);
|
||||
auto engaged_test_nonempty = test.WithInitialValue(Optional(kInitialInteger))
|
||||
.WithInvariants(CheckEngaged<Optional>);
|
||||
.WithContracts(CheckEngaged<Optional>);
|
||||
|
||||
auto swap_empty = [](Optional* optional_ptr) {
|
||||
auto empty = Optional();
|
||||
|
@ -192,11 +192,11 @@ TEST(OptionalExceptionSafety, CopyAssign) {
|
|||
// Test the basic guarantee plus test the result of optional::has_value()
|
||||
// remains the same
|
||||
auto test =
|
||||
MakeExceptionSafetyTester().WithInvariants(CheckInvariants<Optional>);
|
||||
MakeExceptionSafetyTester().WithContracts(OptionalInvariants<Optional>);
|
||||
auto disengaged_test_empty = test.WithInitialValue(Optional())
|
||||
.WithInvariants(CheckDisengaged<Optional>);
|
||||
.WithContracts(CheckDisengaged<Optional>);
|
||||
auto engaged_test_nonempty = test.WithInitialValue(Optional(kInitialInteger))
|
||||
.WithInvariants(CheckEngaged<Optional>);
|
||||
.WithContracts(CheckEngaged<Optional>);
|
||||
|
||||
auto copyassign_nonempty = [](Optional* optional_ptr) {
|
||||
auto nonempty =
|
||||
|
@ -218,11 +218,11 @@ TEST(OptionalExceptionSafety, MoveAssign) {
|
|||
// Test the basic guarantee plus test the result of optional::has_value()
|
||||
// remains the same
|
||||
auto test =
|
||||
MakeExceptionSafetyTester().WithInvariants(CheckInvariants<Optional>);
|
||||
MakeExceptionSafetyTester().WithContracts(OptionalInvariants<Optional>);
|
||||
auto disengaged_test_empty = test.WithInitialValue(Optional())
|
||||
.WithInvariants(CheckDisengaged<Optional>);
|
||||
.WithContracts(CheckDisengaged<Optional>);
|
||||
auto engaged_test_nonempty = test.WithInitialValue(Optional(kInitialInteger))
|
||||
.WithInvariants(CheckEngaged<Optional>);
|
||||
.WithContracts(CheckEngaged<Optional>);
|
||||
|
||||
auto moveassign_empty = [](Optional* optional_ptr) {
|
||||
auto empty = Optional();
|
||||
|
|
|
@ -59,7 +59,7 @@ void ToValuelessByException(ThrowingVariant& v) { // NOLINT
|
|||
}
|
||||
|
||||
// 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;
|
||||
|
||||
|
@ -213,8 +213,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());
|
||||
|
@ -222,8 +222,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.
|
||||
|
@ -251,12 +251,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__))
|
||||
{
|
||||
|
@ -268,7 +268,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; }));
|
||||
}
|
||||
}
|
||||
|
@ -304,11 +304,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
|
||||
|
@ -318,10 +318,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);
|
||||
|
@ -347,12 +347,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())
|
||||
|
@ -361,13 +361,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
|
||||
|
@ -400,12 +400,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())
|
||||
|
@ -413,12 +413,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),
|
||||
|
@ -432,7 +432,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__))
|
||||
|
@ -450,12 +450,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());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -472,7 +472,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);
|
||||
|
@ -486,7 +486,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);
|
||||
|
@ -496,7 +496,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);
|
||||
|
|
Loading…
Reference in a new issue