Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:00:51

0001 // Copyright 2017 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 // Utilities for testing exception-safety
0016 
0017 #ifndef ABSL_BASE_INTERNAL_EXCEPTION_SAFETY_TESTING_H_
0018 #define ABSL_BASE_INTERNAL_EXCEPTION_SAFETY_TESTING_H_
0019 
0020 #include "absl/base/config.h"
0021 
0022 #ifdef ABSL_HAVE_EXCEPTIONS
0023 
0024 #include <cstddef>
0025 #include <cstdint>
0026 #include <functional>
0027 #include <initializer_list>
0028 #include <iosfwd>
0029 #include <string>
0030 #include <tuple>
0031 #include <unordered_map>
0032 
0033 #include "gtest/gtest.h"
0034 #include "absl/base/internal/pretty_function.h"
0035 #include "absl/memory/memory.h"
0036 #include "absl/meta/type_traits.h"
0037 #include "absl/strings/string_view.h"
0038 #include "absl/strings/substitute.h"
0039 #include "absl/utility/utility.h"
0040 
0041 namespace testing {
0042 
0043 enum class TypeSpec;
0044 enum class AllocSpec;
0045 
0046 constexpr TypeSpec operator|(TypeSpec a, TypeSpec b) {
0047   using T = absl::underlying_type_t<TypeSpec>;
0048   return static_cast<TypeSpec>(static_cast<T>(a) | static_cast<T>(b));
0049 }
0050 
0051 constexpr TypeSpec operator&(TypeSpec a, TypeSpec b) {
0052   using T = absl::underlying_type_t<TypeSpec>;
0053   return static_cast<TypeSpec>(static_cast<T>(a) & static_cast<T>(b));
0054 }
0055 
0056 constexpr AllocSpec operator|(AllocSpec a, AllocSpec b) {
0057   using T = absl::underlying_type_t<AllocSpec>;
0058   return static_cast<AllocSpec>(static_cast<T>(a) | static_cast<T>(b));
0059 }
0060 
0061 constexpr AllocSpec operator&(AllocSpec a, AllocSpec b) {
0062   using T = absl::underlying_type_t<AllocSpec>;
0063   return static_cast<AllocSpec>(static_cast<T>(a) & static_cast<T>(b));
0064 }
0065 
0066 namespace exceptions_internal {
0067 
0068 std::string GetSpecString(TypeSpec);
0069 std::string GetSpecString(AllocSpec);
0070 
0071 struct NoThrowTag {};
0072 struct StrongGuaranteeTagType {};
0073 
0074 // A simple exception class.  We throw this so that test code can catch
0075 // exceptions specifically thrown by ThrowingValue.
0076 class TestException {
0077  public:
0078   explicit TestException(absl::string_view msg) : msg_(msg) {}
0079   virtual ~TestException() {}
0080   virtual const char* what() const noexcept { return msg_.c_str(); }
0081 
0082  private:
0083   std::string msg_;
0084 };
0085 
0086 // TestBadAllocException exists because allocation functions must throw an
0087 // exception which can be caught by a handler of std::bad_alloc.  We use a child
0088 // class of std::bad_alloc so we can customise the error message, and also
0089 // derive from TestException so we don't accidentally end up catching an actual
0090 // bad_alloc exception in TestExceptionSafety.
0091 class TestBadAllocException : public std::bad_alloc, public TestException {
0092  public:
0093   explicit TestBadAllocException(absl::string_view msg) : TestException(msg) {}
0094   using TestException::what;
0095 };
0096 
0097 extern int countdown;
0098 
0099 // Allows the countdown variable to be set manually (defaulting to the initial
0100 // value of 0)
0101 inline void SetCountdown(int i = 0) { countdown = i; }
0102 // Sets the countdown to the terminal value -1
0103 inline void UnsetCountdown() { SetCountdown(-1); }
0104 
0105 void MaybeThrow(absl::string_view msg, bool throw_bad_alloc = false);
0106 
0107 testing::AssertionResult FailureMessage(const TestException& e,
0108                                         int countdown) noexcept;
0109 
0110 struct TrackedAddress {
0111   bool is_alive;
0112   std::string description;
0113 };
0114 
0115 // Inspects the constructions and destructions of anything inheriting from
0116 // TrackedObject. This allows us to safely "leak" TrackedObjects, as
0117 // ConstructorTracker will destroy everything left over in its destructor.
0118 class ConstructorTracker {
0119  public:
0120   explicit ConstructorTracker(int count) : countdown_(count) {
0121     assert(current_tracker_instance_ == nullptr);
0122     current_tracker_instance_ = this;
0123   }
0124 
0125   ~ConstructorTracker() {
0126     assert(current_tracker_instance_ == this);
0127     current_tracker_instance_ = nullptr;
0128 
0129     for (auto& it : address_map_) {
0130       void* address = it.first;
0131       TrackedAddress& tracked_address = it.second;
0132       if (tracked_address.is_alive) {
0133         ADD_FAILURE() << ErrorMessage(address, tracked_address.description,
0134                                       countdown_, "Object was not destroyed.");
0135       }
0136     }
0137   }
0138 
0139   static void ObjectConstructed(void* address, std::string description) {
0140     if (!CurrentlyTracking()) return;
0141 
0142     TrackedAddress& tracked_address =
0143         current_tracker_instance_->address_map_[address];
0144     if (tracked_address.is_alive) {
0145       ADD_FAILURE() << ErrorMessage(
0146           address, tracked_address.description,
0147           current_tracker_instance_->countdown_,
0148           "Object was re-constructed. Current object was constructed by " +
0149               description);
0150     }
0151     tracked_address = {true, std::move(description)};
0152   }
0153 
0154   static void ObjectDestructed(void* address) {
0155     if (!CurrentlyTracking()) return;
0156 
0157     auto it = current_tracker_instance_->address_map_.find(address);
0158     // Not tracked. Ignore.
0159     if (it == current_tracker_instance_->address_map_.end()) return;
0160 
0161     TrackedAddress& tracked_address = it->second;
0162     if (!tracked_address.is_alive) {
0163       ADD_FAILURE() << ErrorMessage(address, tracked_address.description,
0164                                     current_tracker_instance_->countdown_,
0165                                     "Object was re-destroyed.");
0166     }
0167     tracked_address.is_alive = false;
0168   }
0169 
0170  private:
0171   static bool CurrentlyTracking() {
0172     return current_tracker_instance_ != nullptr;
0173   }
0174 
0175   static std::string ErrorMessage(void* address,
0176                                   const std::string& address_description,
0177                                   int countdown,
0178                                   const std::string& error_description) {
0179     return absl::Substitute(
0180         "With coundtown at $0:\n"
0181         "  $1\n"
0182         "  Object originally constructed by $2\n"
0183         "  Object address: $3\n",
0184         countdown, error_description, address_description, address);
0185   }
0186 
0187   std::unordered_map<void*, TrackedAddress> address_map_;
0188   int countdown_;
0189 
0190   static ConstructorTracker* current_tracker_instance_;
0191 };
0192 
0193 class TrackedObject {
0194  public:
0195   TrackedObject(const TrackedObject&) = delete;
0196   TrackedObject(TrackedObject&&) = delete;
0197 
0198  protected:
0199   explicit TrackedObject(std::string description) {
0200     ConstructorTracker::ObjectConstructed(this, std::move(description));
0201   }
0202 
0203   ~TrackedObject() noexcept { ConstructorTracker::ObjectDestructed(this); }
0204 };
0205 }  // namespace exceptions_internal
0206 
0207 extern exceptions_internal::NoThrowTag nothrow_ctor;
0208 
0209 extern exceptions_internal::StrongGuaranteeTagType strong_guarantee;
0210 
0211 // A test class which is convertible to bool.  The conversion can be
0212 // instrumented to throw at a controlled time.
0213 class ThrowingBool {
0214  public:
0215   ThrowingBool(bool b) noexcept : b_(b) {}  // NOLINT(runtime/explicit)
0216   operator bool() const {                   // NOLINT
0217     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0218     return b_;
0219   }
0220 
0221  private:
0222   bool b_;
0223 };
0224 
0225 /*
0226  * Configuration enum for the ThrowingValue type that defines behavior for the
0227  * lifetime of the instance. Use testing::nothrow_ctor to prevent the integer
0228  * constructor from throwing.
0229  *
0230  * kEverythingThrows: Every operation can throw an exception
0231  * kNoThrowCopy: Copy construction and copy assignment will not throw
0232  * kNoThrowMove: Move construction and move assignment will not throw
0233  * kNoThrowNew: Overloaded operators new and new[] will not throw
0234  */
0235 enum class TypeSpec {
0236   kEverythingThrows = 0,
0237   kNoThrowCopy = 1,
0238   kNoThrowMove = 1 << 1,
0239   kNoThrowNew = 1 << 2,
0240 };
0241 
0242 /*
0243  * A testing class instrumented to throw an exception at a controlled time.
0244  *
0245  * ThrowingValue implements a slightly relaxed version of the Regular concept --
0246  * that is it's a value type with the expected semantics.  It also implements
0247  * arithmetic operations.  It doesn't implement member and pointer operators
0248  * like operator-> or operator[].
0249  *
0250  * ThrowingValue can be instrumented to have certain operations be noexcept by
0251  * using compile-time bitfield template arguments.  That is, to make an
0252  * ThrowingValue which has noexcept move construction/assignment and noexcept
0253  * copy construction/assignment, use the following:
0254  *   ThrowingValue<testing::kNoThrowMove | testing::kNoThrowCopy> my_thrwr{val};
0255  */
0256 template <TypeSpec Spec = TypeSpec::kEverythingThrows>
0257 class ThrowingValue : private exceptions_internal::TrackedObject {
0258   static constexpr bool IsSpecified(TypeSpec spec) {
0259     return static_cast<bool>(Spec & spec);
0260   }
0261 
0262   static constexpr int kDefaultValue = 0;
0263   static constexpr int kBadValue = 938550620;
0264 
0265  public:
0266   ThrowingValue() : TrackedObject(GetInstanceString(kDefaultValue)) {
0267     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0268     dummy_ = kDefaultValue;
0269   }
0270 
0271   ThrowingValue(const ThrowingValue& other) noexcept(
0272       IsSpecified(TypeSpec::kNoThrowCopy))
0273       : TrackedObject(GetInstanceString(other.dummy_)) {
0274     if (!IsSpecified(TypeSpec::kNoThrowCopy)) {
0275       exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0276     }
0277     dummy_ = other.dummy_;
0278   }
0279 
0280   ThrowingValue(ThrowingValue&& other) noexcept(
0281       IsSpecified(TypeSpec::kNoThrowMove))
0282       : TrackedObject(GetInstanceString(other.dummy_)) {
0283     if (!IsSpecified(TypeSpec::kNoThrowMove)) {
0284       exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0285     }
0286     dummy_ = other.dummy_;
0287   }
0288 
0289   explicit ThrowingValue(int i) : TrackedObject(GetInstanceString(i)) {
0290     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0291     dummy_ = i;
0292   }
0293 
0294   ThrowingValue(int i, exceptions_internal::NoThrowTag) noexcept
0295       : TrackedObject(GetInstanceString(i)), dummy_(i) {}
0296 
0297   // absl expects nothrow destructors
0298   ~ThrowingValue() noexcept = default;
0299 
0300   ThrowingValue& operator=(const ThrowingValue& other) noexcept(
0301       IsSpecified(TypeSpec::kNoThrowCopy)) {
0302     dummy_ = kBadValue;
0303     if (!IsSpecified(TypeSpec::kNoThrowCopy)) {
0304       exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0305     }
0306     dummy_ = other.dummy_;
0307     return *this;
0308   }
0309 
0310   ThrowingValue& operator=(ThrowingValue&& other) noexcept(
0311       IsSpecified(TypeSpec::kNoThrowMove)) {
0312     dummy_ = kBadValue;
0313     if (!IsSpecified(TypeSpec::kNoThrowMove)) {
0314       exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0315     }
0316     dummy_ = other.dummy_;
0317     return *this;
0318   }
0319 
0320   // Arithmetic Operators
0321   ThrowingValue operator+(const ThrowingValue& other) const {
0322     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0323     return ThrowingValue(dummy_ + other.dummy_, nothrow_ctor);
0324   }
0325 
0326   ThrowingValue operator+() const {
0327     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0328     return ThrowingValue(dummy_, nothrow_ctor);
0329   }
0330 
0331   ThrowingValue operator-(const ThrowingValue& other) const {
0332     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0333     return ThrowingValue(dummy_ - other.dummy_, nothrow_ctor);
0334   }
0335 
0336   ThrowingValue operator-() const {
0337     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0338     return ThrowingValue(-dummy_, nothrow_ctor);
0339   }
0340 
0341   ThrowingValue& operator++() {
0342     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0343     ++dummy_;
0344     return *this;
0345   }
0346 
0347   ThrowingValue operator++(int) {
0348     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0349     auto out = ThrowingValue(dummy_, nothrow_ctor);
0350     ++dummy_;
0351     return out;
0352   }
0353 
0354   ThrowingValue& operator--() {
0355     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0356     --dummy_;
0357     return *this;
0358   }
0359 
0360   ThrowingValue operator--(int) {
0361     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0362     auto out = ThrowingValue(dummy_, nothrow_ctor);
0363     --dummy_;
0364     return out;
0365   }
0366 
0367   ThrowingValue operator*(const ThrowingValue& other) const {
0368     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0369     return ThrowingValue(dummy_ * other.dummy_, nothrow_ctor);
0370   }
0371 
0372   ThrowingValue operator/(const ThrowingValue& other) const {
0373     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0374     return ThrowingValue(dummy_ / other.dummy_, nothrow_ctor);
0375   }
0376 
0377   ThrowingValue operator%(const ThrowingValue& other) const {
0378     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0379     return ThrowingValue(dummy_ % other.dummy_, nothrow_ctor);
0380   }
0381 
0382   ThrowingValue operator<<(int shift) const {
0383     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0384     return ThrowingValue(dummy_ << shift, nothrow_ctor);
0385   }
0386 
0387   ThrowingValue operator>>(int shift) const {
0388     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0389     return ThrowingValue(dummy_ >> shift, nothrow_ctor);
0390   }
0391 
0392   // Comparison Operators
0393   // NOTE: We use `ThrowingBool` instead of `bool` because most STL
0394   // types/containers requires T to be convertible to bool.
0395   friend ThrowingBool operator==(const ThrowingValue& a,
0396                                  const ThrowingValue& b) {
0397     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0398     return a.dummy_ == b.dummy_;
0399   }
0400   friend ThrowingBool operator!=(const ThrowingValue& a,
0401                                  const ThrowingValue& b) {
0402     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0403     return a.dummy_ != b.dummy_;
0404   }
0405   friend ThrowingBool operator<(const ThrowingValue& a,
0406                                 const ThrowingValue& b) {
0407     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0408     return a.dummy_ < b.dummy_;
0409   }
0410   friend ThrowingBool operator<=(const ThrowingValue& a,
0411                                  const ThrowingValue& b) {
0412     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0413     return a.dummy_ <= b.dummy_;
0414   }
0415   friend ThrowingBool operator>(const ThrowingValue& a,
0416                                 const ThrowingValue& b) {
0417     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0418     return a.dummy_ > b.dummy_;
0419   }
0420   friend ThrowingBool operator>=(const ThrowingValue& a,
0421                                  const ThrowingValue& b) {
0422     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0423     return a.dummy_ >= b.dummy_;
0424   }
0425 
0426   // Logical Operators
0427   ThrowingBool operator!() const {
0428     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0429     return !dummy_;
0430   }
0431 
0432   ThrowingBool operator&&(const ThrowingValue& other) const {
0433     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0434     return dummy_ && other.dummy_;
0435   }
0436 
0437   ThrowingBool operator||(const ThrowingValue& other) const {
0438     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0439     return dummy_ || other.dummy_;
0440   }
0441 
0442   // Bitwise Logical Operators
0443   ThrowingValue operator~() const {
0444     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0445     return ThrowingValue(~dummy_, nothrow_ctor);
0446   }
0447 
0448   ThrowingValue operator&(const ThrowingValue& other) const {
0449     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0450     return ThrowingValue(dummy_ & other.dummy_, nothrow_ctor);
0451   }
0452 
0453   ThrowingValue operator|(const ThrowingValue& other) const {
0454     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0455     return ThrowingValue(dummy_ | other.dummy_, nothrow_ctor);
0456   }
0457 
0458   ThrowingValue operator^(const ThrowingValue& other) const {
0459     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0460     return ThrowingValue(dummy_ ^ other.dummy_, nothrow_ctor);
0461   }
0462 
0463   // Compound Assignment operators
0464   ThrowingValue& operator+=(const ThrowingValue& other) {
0465     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0466     dummy_ += other.dummy_;
0467     return *this;
0468   }
0469 
0470   ThrowingValue& operator-=(const ThrowingValue& other) {
0471     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0472     dummy_ -= other.dummy_;
0473     return *this;
0474   }
0475 
0476   ThrowingValue& operator*=(const ThrowingValue& other) {
0477     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0478     dummy_ *= other.dummy_;
0479     return *this;
0480   }
0481 
0482   ThrowingValue& operator/=(const ThrowingValue& other) {
0483     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0484     dummy_ /= other.dummy_;
0485     return *this;
0486   }
0487 
0488   ThrowingValue& operator%=(const ThrowingValue& other) {
0489     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0490     dummy_ %= other.dummy_;
0491     return *this;
0492   }
0493 
0494   ThrowingValue& operator&=(const ThrowingValue& other) {
0495     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0496     dummy_ &= other.dummy_;
0497     return *this;
0498   }
0499 
0500   ThrowingValue& operator|=(const ThrowingValue& other) {
0501     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0502     dummy_ |= other.dummy_;
0503     return *this;
0504   }
0505 
0506   ThrowingValue& operator^=(const ThrowingValue& other) {
0507     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0508     dummy_ ^= other.dummy_;
0509     return *this;
0510   }
0511 
0512   ThrowingValue& operator<<=(int shift) {
0513     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0514     dummy_ <<= shift;
0515     return *this;
0516   }
0517 
0518   ThrowingValue& operator>>=(int shift) {
0519     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0520     dummy_ >>= shift;
0521     return *this;
0522   }
0523 
0524   // Pointer operators
0525   void operator&() const = delete;  // NOLINT(runtime/operator)
0526 
0527   // Stream operators
0528   friend std::ostream& operator<<(std::ostream& os, const ThrowingValue& tv) {
0529     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0530     return os << GetInstanceString(tv.dummy_);
0531   }
0532 
0533   friend std::istream& operator>>(std::istream& is, const ThrowingValue&) {
0534     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0535     return is;
0536   }
0537 
0538   // Memory management operators
0539   static void* operator new(size_t s) noexcept(
0540       IsSpecified(TypeSpec::kNoThrowNew)) {
0541     if (!IsSpecified(TypeSpec::kNoThrowNew)) {
0542       exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION, true);
0543     }
0544     return ::operator new(s);
0545   }
0546 
0547   static void* operator new[](size_t s) noexcept(
0548       IsSpecified(TypeSpec::kNoThrowNew)) {
0549     if (!IsSpecified(TypeSpec::kNoThrowNew)) {
0550       exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION, true);
0551     }
0552     return ::operator new[](s);
0553   }
0554 
0555   template <typename... Args>
0556   static void* operator new(size_t s, Args&&... args) noexcept(
0557       IsSpecified(TypeSpec::kNoThrowNew)) {
0558     if (!IsSpecified(TypeSpec::kNoThrowNew)) {
0559       exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION, true);
0560     }
0561     return ::operator new(s, std::forward<Args>(args)...);
0562   }
0563 
0564   template <typename... Args>
0565   static void* operator new[](size_t s, Args&&... args) noexcept(
0566       IsSpecified(TypeSpec::kNoThrowNew)) {
0567     if (!IsSpecified(TypeSpec::kNoThrowNew)) {
0568       exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION, true);
0569     }
0570     return ::operator new[](s, std::forward<Args>(args)...);
0571   }
0572 
0573   // Abseil doesn't support throwing overloaded operator delete.  These are
0574   // provided so a throwing operator-new can clean up after itself.
0575   void operator delete(void* p) noexcept { ::operator delete(p); }
0576 
0577   template <typename... Args>
0578   void operator delete(void* p, Args&&... args) noexcept {
0579     ::operator delete(p, std::forward<Args>(args)...);
0580   }
0581 
0582   void operator delete[](void* p) noexcept { return ::operator delete[](p); }
0583 
0584   template <typename... Args>
0585   void operator delete[](void* p, Args&&... args) noexcept {
0586     return ::operator delete[](p, std::forward<Args>(args)...);
0587   }
0588 
0589   // Non-standard access to the actual contained value.  No need for this to
0590   // throw.
0591   int& Get() noexcept { return dummy_; }
0592   const int& Get() const noexcept { return dummy_; }
0593 
0594  private:
0595   static std::string GetInstanceString(int dummy) {
0596     return absl::StrCat("ThrowingValue<",
0597                         exceptions_internal::GetSpecString(Spec), ">(", dummy,
0598                         ")");
0599   }
0600 
0601   int dummy_;
0602 };
0603 // While not having to do with exceptions, explicitly delete comma operator, to
0604 // make sure we don't use it on user-supplied types.
0605 template <TypeSpec Spec, typename T>
0606 void operator,(const ThrowingValue<Spec>&, T&&) = delete;
0607 template <TypeSpec Spec, typename T>
0608 void operator,(T&&, const ThrowingValue<Spec>&) = delete;
0609 
0610 /*
0611  * Configuration enum for the ThrowingAllocator type that defines behavior for
0612  * the lifetime of the instance.
0613  *
0614  * kEverythingThrows: Calls to the member functions may throw
0615  * kNoThrowAllocate: Calls to the member functions will not throw
0616  */
0617 enum class AllocSpec {
0618   kEverythingThrows = 0,
0619   kNoThrowAllocate = 1,
0620 };
0621 
0622 /*
0623  * An allocator type which is instrumented to throw at a controlled time, or not
0624  * to throw, using AllocSpec. The supported settings are the default of every
0625  * function which is allowed to throw in a conforming allocator possibly
0626  * throwing, or nothing throws, in line with the ABSL_ALLOCATOR_THROWS
0627  * configuration macro.
0628  */
0629 template <typename T, AllocSpec Spec = AllocSpec::kEverythingThrows>
0630 class ThrowingAllocator : private exceptions_internal::TrackedObject {
0631   static constexpr bool IsSpecified(AllocSpec spec) {
0632     return static_cast<bool>(Spec & spec);
0633   }
0634 
0635  public:
0636   using pointer = T*;
0637   using const_pointer = const T*;
0638   using reference = T&;
0639   using const_reference = const T&;
0640   using void_pointer = void*;
0641   using const_void_pointer = const void*;
0642   using value_type = T;
0643   using size_type = size_t;
0644   using difference_type = ptrdiff_t;
0645 
0646   using is_nothrow =
0647       std::integral_constant<bool, Spec == AllocSpec::kNoThrowAllocate>;
0648   using propagate_on_container_copy_assignment = std::true_type;
0649   using propagate_on_container_move_assignment = std::true_type;
0650   using propagate_on_container_swap = std::true_type;
0651   using is_always_equal = std::false_type;
0652 
0653   ThrowingAllocator() : TrackedObject(GetInstanceString(next_id_)) {
0654     exceptions_internal::MaybeThrow(ABSL_PRETTY_FUNCTION);
0655     dummy_ = std::make_shared<const int>(next_id_++);
0656   }
0657 
0658   template <typename U>
0659   ThrowingAllocator(const ThrowingAllocator<U, Spec>& other) noexcept  // NOLINT
0660       : TrackedObject(GetInstanceString(*other.State())),
0661         dummy_(other.State()) {}
0662 
0663   // According to C++11 standard [17.6.3.5], Table 28, the move/copy ctors of
0664   // allocator shall not exit via an exception, thus they are marked noexcept.
0665   ThrowingAllocator(const ThrowingAllocator& other) noexcept
0666       : TrackedObject(GetInstanceString(*other.State())),
0667         dummy_(other.State()) {}
0668 
0669   template <typename U>
0670   ThrowingAllocator(ThrowingAllocator<U, Spec>&& other) noexcept  // NOLINT
0671       : TrackedObject(GetInstanceString(*other.State())),
0672         dummy_(std::move(other.State())) {}
0673 
0674   ThrowingAllocator(ThrowingAllocator&& other) noexcept
0675       : TrackedObject(GetInstanceString(*other.State())),
0676         dummy_(std::move(other.State())) {}
0677 
0678   ~ThrowingAllocator() noexcept = default;
0679 
0680   ThrowingAllocator& operator=(const ThrowingAllocator& other) noexcept {
0681     dummy_ = other.State();
0682     return *this;
0683   }
0684 
0685   template <typename U>
0686   ThrowingAllocator& operator=(
0687       const ThrowingAllocator<U, Spec>& other) noexcept {
0688     dummy_ = other.State();
0689     return *this;
0690   }
0691 
0692   template <typename U>
0693   ThrowingAllocator& operator=(ThrowingAllocator<U, Spec>&& other) noexcept {
0694     dummy_ = std::move(other.State());
0695     return *this;
0696   }
0697 
0698   template <typename U>
0699   struct rebind {
0700     using other = ThrowingAllocator<U, Spec>;
0701   };
0702 
0703   pointer allocate(size_type n) noexcept(
0704       IsSpecified(AllocSpec::kNoThrowAllocate)) {
0705     ReadStateAndMaybeThrow(ABSL_PRETTY_FUNCTION);
0706     return static_cast<pointer>(::operator new(n * sizeof(T)));
0707   }
0708 
0709   pointer allocate(size_type n, const_void_pointer) noexcept(
0710       IsSpecified(AllocSpec::kNoThrowAllocate)) {
0711     return allocate(n);
0712   }
0713 
0714   void deallocate(pointer ptr, size_type) noexcept {
0715     ReadState();
0716     ::operator delete(static_cast<void*>(ptr));
0717   }
0718 
0719   template <typename U, typename... Args>
0720   void construct(U* ptr, Args&&... args) noexcept(
0721       IsSpecified(AllocSpec::kNoThrowAllocate)) {
0722     ReadStateAndMaybeThrow(ABSL_PRETTY_FUNCTION);
0723     ::new (static_cast<void*>(ptr)) U(std::forward<Args>(args)...);
0724   }
0725 
0726   template <typename U>
0727   void destroy(U* p) noexcept {
0728     ReadState();
0729     p->~U();
0730   }
0731 
0732   size_type max_size() const noexcept {
0733     return (std::numeric_limits<difference_type>::max)() / sizeof(value_type);
0734   }
0735 
0736   ThrowingAllocator select_on_container_copy_construction() noexcept(
0737       IsSpecified(AllocSpec::kNoThrowAllocate)) {
0738     ReadStateAndMaybeThrow(ABSL_PRETTY_FUNCTION);
0739     return *this;
0740   }
0741 
0742   template <typename U>
0743   bool operator==(const ThrowingAllocator<U, Spec>& other) const noexcept {
0744     return dummy_ == other.dummy_;
0745   }
0746 
0747   template <typename U>
0748   bool operator!=(const ThrowingAllocator<U, Spec>& other) const noexcept {
0749     return dummy_ != other.dummy_;
0750   }
0751 
0752   template <typename, AllocSpec>
0753   friend class ThrowingAllocator;
0754 
0755  private:
0756   static std::string GetInstanceString(int dummy) {
0757     return absl::StrCat("ThrowingAllocator<",
0758                         exceptions_internal::GetSpecString(Spec), ">(", dummy,
0759                         ")");
0760   }
0761 
0762   const std::shared_ptr<const int>& State() const { return dummy_; }
0763   std::shared_ptr<const int>& State() { return dummy_; }
0764 
0765   void ReadState() {
0766     // we know that this will never be true, but the compiler doesn't, so this
0767     // should safely force a read of the value.
0768     if (*dummy_ < 0) std::abort();
0769   }
0770 
0771   void ReadStateAndMaybeThrow(absl::string_view msg) const {
0772     if (!IsSpecified(AllocSpec::kNoThrowAllocate)) {
0773       exceptions_internal::MaybeThrow(
0774           absl::Substitute("Allocator id $0 threw from $1", *dummy_, msg));
0775     }
0776   }
0777 
0778   static int next_id_;
0779   std::shared_ptr<const int> dummy_;
0780 };
0781 
0782 template <typename T, AllocSpec Spec>
0783 int ThrowingAllocator<T, Spec>::next_id_ = 0;
0784 
0785 // Tests for resource leaks by attempting to construct a T using args repeatedly
0786 // until successful, using the countdown method.  Side effects can then be
0787 // tested for resource leaks.
0788 template <typename T, typename... Args>
0789 void TestThrowingCtor(Args&&... args) {
0790   struct Cleanup {
0791     ~Cleanup() { exceptions_internal::UnsetCountdown(); }
0792   } c;
0793   for (int count = 0;; ++count) {
0794     exceptions_internal::ConstructorTracker ct(count);
0795     exceptions_internal::SetCountdown(count);
0796     try {
0797       T temp(std::forward<Args>(args)...);
0798       static_cast<void>(temp);
0799       break;
0800     } catch (const exceptions_internal::TestException&) {
0801     }
0802   }
0803 }
0804 
0805 // Tests the nothrow guarantee of the provided nullary operation. If the an
0806 // exception is thrown, the result will be AssertionFailure(). Otherwise, it
0807 // will be AssertionSuccess().
0808 template <typename Operation>
0809 testing::AssertionResult TestNothrowOp(const Operation& operation) {
0810   struct Cleanup {
0811     Cleanup() { exceptions_internal::SetCountdown(); }
0812     ~Cleanup() { exceptions_internal::UnsetCountdown(); }
0813   } c;
0814   try {
0815     operation();
0816     return testing::AssertionSuccess();
0817   } catch (const exceptions_internal::TestException&) {
0818     return testing::AssertionFailure()
0819            << "TestException thrown during call to operation() when nothrow "
0820               "guarantee was expected.";
0821   } catch (...) {
0822     return testing::AssertionFailure()
0823            << "Unknown exception thrown during call to operation() when "
0824               "nothrow guarantee was expected.";
0825   }
0826 }
0827 
0828 namespace exceptions_internal {
0829 
0830 // Dummy struct for ExceptionSafetyTestBuilder<> partial state.
0831 struct UninitializedT {};
0832 
0833 template <typename T>
0834 class DefaultFactory {
0835  public:
0836   explicit DefaultFactory(const T& t) : t_(t) {}
0837   std::unique_ptr<T> operator()() const { return absl::make_unique<T>(t_); }
0838 
0839  private:
0840   T t_;
0841 };
0842 
0843 template <size_t LazyContractsCount, typename LazyFactory,
0844           typename LazyOperation>
0845 using EnableIfTestable = typename absl::enable_if_t<
0846     LazyContractsCount != 0 &&
0847     !std::is_same<LazyFactory, UninitializedT>::value &&
0848     !std::is_same<LazyOperation, UninitializedT>::value>;
0849 
0850 template <typename Factory = UninitializedT,
0851           typename Operation = UninitializedT, typename... Contracts>
0852 class ExceptionSafetyTestBuilder;
0853 
0854 }  // namespace exceptions_internal
0855 
0856 /*
0857  * Constructs an empty ExceptionSafetyTestBuilder. All
0858  * ExceptionSafetyTestBuilder objects are immutable and all With[thing] mutation
0859  * methods return new instances of ExceptionSafetyTestBuilder.
0860  *
0861  * In order to test a T for exception safety, a factory for that T, a testable
0862  * operation, and at least one contract callback returning an assertion
0863  * result must be applied using the respective methods.
0864  */
0865 exceptions_internal::ExceptionSafetyTestBuilder<> MakeExceptionSafetyTester();
0866 
0867 namespace exceptions_internal {
0868 template <typename T>
0869 struct IsUniquePtr : std::false_type {};
0870 
0871 template <typename T, typename D>
0872 struct IsUniquePtr<std::unique_ptr<T, D>> : std::true_type {};
0873 
0874 template <typename Factory>
0875 struct FactoryPtrTypeHelper {
0876   using type = decltype(std::declval<const Factory&>()());
0877 
0878   static_assert(IsUniquePtr<type>::value, "Factories must return a unique_ptr");
0879 };
0880 
0881 template <typename Factory>
0882 using FactoryPtrType = typename FactoryPtrTypeHelper<Factory>::type;
0883 
0884 template <typename Factory>
0885 using FactoryElementType = typename FactoryPtrType<Factory>::element_type;
0886 
0887 template <typename T>
0888 class ExceptionSafetyTest {
0889   using Factory = std::function<std::unique_ptr<T>()>;
0890   using Operation = std::function<void(T*)>;
0891   using Contract = std::function<AssertionResult(T*)>;
0892 
0893  public:
0894   template <typename... Contracts>
0895   explicit ExceptionSafetyTest(const Factory& f, const Operation& op,
0896                                const Contracts&... contracts)
0897       : factory_(f), operation_(op), contracts_{WrapContract(contracts)...} {}
0898 
0899   AssertionResult Test() const {
0900     for (int count = 0;; ++count) {
0901       exceptions_internal::ConstructorTracker ct(count);
0902 
0903       for (const auto& contract : contracts_) {
0904         auto t_ptr = factory_();
0905         try {
0906           SetCountdown(count);
0907           operation_(t_ptr.get());
0908           // Unset for the case that the operation throws no exceptions, which
0909           // would leave the countdown set and break the *next* exception safety
0910           // test after this one.
0911           UnsetCountdown();
0912           return AssertionSuccess();
0913         } catch (const exceptions_internal::TestException& e) {
0914           if (!contract(t_ptr.get())) {
0915             return AssertionFailure() << e.what() << " failed contract check";
0916           }
0917         }
0918       }
0919     }
0920   }
0921 
0922  private:
0923   template <typename ContractFn>
0924   Contract WrapContract(const ContractFn& contract) {
0925     return [contract](T* t_ptr) { return AssertionResult(contract(t_ptr)); };
0926   }
0927 
0928   Contract WrapContract(StrongGuaranteeTagType) {
0929     return [this](T* t_ptr) { return AssertionResult(*factory_() == *t_ptr); };
0930   }
0931 
0932   Factory factory_;
0933   Operation operation_;
0934   std::vector<Contract> contracts_;
0935 };
0936 
0937 /*
0938  * Builds a tester object that tests if performing a operation on a T follows
0939  * exception safety guarantees. Verification is done via contract assertion
0940  * callbacks applied to T instances post-throw.
0941  *
0942  * Template parameters for ExceptionSafetyTestBuilder:
0943  *
0944  * - Factory: The factory object (passed in via tester.WithFactory(...) or
0945  *   tester.WithInitialValue(...)) must be invocable with the signature
0946  *   `std::unique_ptr<T> operator()() const` where T is the type being tested.
0947  *   It is used for reliably creating identical T instances to test on.
0948  *
0949  * - Operation: The operation object (passed in via tester.WithOperation(...)
0950  *   or tester.Test(...)) must be invocable with the signature
0951  *   `void operator()(T*) const` where T is the type being tested. It is used
0952  *   for performing steps on a T instance that may throw and that need to be
0953  *   checked for exception safety. Each call to the operation will receive a
0954  *   fresh T instance so it's free to modify and destroy the T instances as it
0955  *   pleases.
0956  *
0957  * - Contracts...: The contract assertion callback objects (passed in via
0958  *   tester.WithContracts(...)) must be invocable with the signature
0959  *   `testing::AssertionResult operator()(T*) const` where T is the type being
0960  *   tested. Contract assertion callbacks are provided T instances post-throw.
0961  *   They must return testing::AssertionSuccess when the type contracts of the
0962  *   provided T instance hold. If the type contracts of the T instance do not
0963  *   hold, they must return testing::AssertionFailure. Execution order of
0964  *   Contracts... is unspecified. They will each individually get a fresh T
0965  *   instance so they are free to modify and destroy the T instances as they
0966  *   please.
0967  */
0968 template <typename Factory, typename Operation, typename... Contracts>
0969 class ExceptionSafetyTestBuilder {
0970  public:
0971   /*
0972    * Returns a new ExceptionSafetyTestBuilder with an included T factory based
0973    * on the provided T instance. The existing factory will not be included in
0974    * the newly created tester instance. The created factory returns a new T
0975    * instance by copy-constructing the provided const T& t.
0976    *
0977    * Preconditions for tester.WithInitialValue(const T& t):
0978    *
0979    * - The const T& t object must be copy-constructible where T is the type
0980    *   being tested. For non-copy-constructible objects, use the method
0981    *   tester.WithFactory(...).
0982    */
0983   template <typename T>
0984   ExceptionSafetyTestBuilder<DefaultFactory<T>, Operation, Contracts...>
0985   WithInitialValue(const T& t) const {
0986     return WithFactory(DefaultFactory<T>(t));
0987   }
0988 
0989   /*
0990    * Returns a new ExceptionSafetyTestBuilder with the provided T factory
0991    * included. The existing factory will not be included in the newly-created
0992    * tester instance. This method is intended for use with types lacking a copy
0993    * constructor. Types that can be copy-constructed should instead use the
0994    * method tester.WithInitialValue(...).
0995    */
0996   template <typename NewFactory>
0997   ExceptionSafetyTestBuilder<absl::decay_t<NewFactory>, Operation, Contracts...>
0998   WithFactory(const NewFactory& new_factory) const {
0999     return {new_factory, operation_, contracts_};
1000   }
1001 
1002   /*
1003    * Returns a new ExceptionSafetyTestBuilder with the provided testable
1004    * operation included. The existing operation will not be included in the
1005    * newly created tester.
1006    */
1007   template <typename NewOperation>
1008   ExceptionSafetyTestBuilder<Factory, absl::decay_t<NewOperation>, Contracts...>
1009   WithOperation(const NewOperation& new_operation) const {
1010     return {factory_, new_operation, contracts_};
1011   }
1012 
1013   /*
1014    * Returns a new ExceptionSafetyTestBuilder with the provided MoreContracts...
1015    * combined with the Contracts... that were already included in the instance
1016    * on which the method was called. Contracts... cannot be removed or replaced
1017    * once added to an ExceptionSafetyTestBuilder instance. A fresh object must
1018    * be created in order to get an empty Contracts... list.
1019    *
1020    * In addition to passing in custom contract assertion callbacks, this method
1021    * accepts `testing::strong_guarantee` as an argument which checks T instances
1022    * post-throw against freshly created T instances via operator== to verify
1023    * that any state changes made during the execution of the operation were
1024    * properly rolled back.
1025    */
1026   template <typename... MoreContracts>
1027   ExceptionSafetyTestBuilder<Factory, Operation, Contracts...,
1028                              absl::decay_t<MoreContracts>...>
1029   WithContracts(const MoreContracts&... more_contracts) const {
1030     return {
1031         factory_, operation_,
1032         std::tuple_cat(contracts_, std::tuple<absl::decay_t<MoreContracts>...>(
1033                                        more_contracts...))};
1034   }
1035 
1036   /*
1037    * Returns a testing::AssertionResult that is the reduced result of the
1038    * exception safety algorithm. The algorithm short circuits and returns
1039    * AssertionFailure after the first contract callback returns an
1040    * AssertionFailure. Otherwise, if all contract callbacks return an
1041    * AssertionSuccess, the reduced result is AssertionSuccess.
1042    *
1043    * The passed-in testable operation will not be saved in a new tester instance
1044    * nor will it modify/replace the existing tester instance. This is useful
1045    * when each operation being tested is unique and does not need to be reused.
1046    *
1047    * Preconditions for tester.Test(const NewOperation& new_operation):
1048    *
1049    * - May only be called after at least one contract assertion callback and a
1050    *   factory or initial value have been provided.
1051    */
1052   template <
1053       typename NewOperation,
1054       typename = EnableIfTestable<sizeof...(Contracts), Factory, NewOperation>>
1055   testing::AssertionResult Test(const NewOperation& new_operation) const {
1056     return TestImpl(new_operation, absl::index_sequence_for<Contracts...>());
1057   }
1058 
1059   /*
1060    * Returns a testing::AssertionResult that is the reduced result of the
1061    * exception safety algorithm. The algorithm short circuits and returns
1062    * AssertionFailure after the first contract callback returns an
1063    * AssertionFailure. Otherwise, if all contract callbacks return an
1064    * AssertionSuccess, the reduced result is AssertionSuccess.
1065    *
1066    * Preconditions for tester.Test():
1067    *
1068    * - May only be called after at least one contract assertion callback, a
1069    *   factory or initial value and a testable operation have been provided.
1070    */
1071   template <
1072       typename LazyOperation = Operation,
1073       typename = EnableIfTestable<sizeof...(Contracts), Factory, LazyOperation>>
1074   testing::AssertionResult Test() const {
1075     return Test(operation_);
1076   }
1077 
1078  private:
1079   template <typename, typename, typename...>
1080   friend class ExceptionSafetyTestBuilder;
1081 
1082   friend ExceptionSafetyTestBuilder<> testing::MakeExceptionSafetyTester();
1083 
1084   ExceptionSafetyTestBuilder() {}
1085 
1086   ExceptionSafetyTestBuilder(const Factory& f, const Operation& o,
1087                              const std::tuple<Contracts...>& i)
1088       : factory_(f), operation_(o), contracts_(i) {}
1089 
1090   template <typename SelectedOperation, size_t... Indices>
1091   testing::AssertionResult TestImpl(SelectedOperation selected_operation,
1092                                     absl::index_sequence<Indices...>) const {
1093     return ExceptionSafetyTest<FactoryElementType<Factory>>(
1094                factory_, selected_operation, std::get<Indices>(contracts_)...)
1095         .Test();
1096   }
1097 
1098   Factory factory_;
1099   Operation operation_;
1100   std::tuple<Contracts...> contracts_;
1101 };
1102 
1103 }  // namespace exceptions_internal
1104 
1105 }  // namespace testing
1106 
1107 #endif  // ABSL_HAVE_EXCEPTIONS
1108 
1109 #endif  // ABSL_BASE_INTERNAL_EXCEPTION_SAFETY_TESTING_H_