diff options
author | Marek Gilbert <mcg@google.com> | 2018-04-15 16:10:47 -0700 |
---|---|---|
committer | Marek Gilbert <mcg@google.com> | 2018-04-15 16:45:50 -0700 |
commit | dfb5c04cf87fdff3c7fc16f9de3a2e8e1f4df265 (patch) | |
tree | d82fcc92ef0ba74169ae7d889a5e17c0541b262f /Firestore | |
parent | c1061b7362471ddb7cae26c430f8112b0b94bb83 (diff) |
Update abseil-cpp to a new upstream
Actually update to bf7fc9986e20f664958fc227547fd8d2fdcf863e
Change #754 didn't completely do this.
This makes the rest of the sources match optional, which was imported at
this change in #1083.
Also add:
absl/types/optional_test.cc
absl/types/CMakeLists.txt
absl/utility/CMakeLists.txt
Diffstat (limited to 'Firestore')
28 files changed, 2451 insertions, 282 deletions
diff --git a/Firestore/third_party/abseil-cpp/CMakeLists.txt b/Firestore/third_party/abseil-cpp/CMakeLists.txt index 6d3789e..6e715af 100644 --- a/Firestore/third_party/abseil-cpp/CMakeLists.txt +++ b/Firestore/third_party/abseil-cpp/CMakeLists.txt @@ -70,6 +70,7 @@ if(NOT ABSL_CCTZ_TARGET) endif() # commented: used only for standalone test +# Don't remove these or else CMake CI will break #add_subdirectory(cctz) #add_subdirectory(googletest) check_target(${ABSL_CCTZ_TARGET}) diff --git a/Firestore/third_party/abseil-cpp/absl/base/CMakeLists.txt b/Firestore/third_party/abseil-cpp/absl/base/CMakeLists.txt index 4b7b53a..ced81b6 100644 --- a/Firestore/third_party/abseil-cpp/absl/base/CMakeLists.txt +++ b/Firestore/third_party/abseil-cpp/absl/base/CMakeLists.txt @@ -32,6 +32,7 @@ list(APPEND BASE_PUBLIC_HEADERS list(APPEND BASE_INTERNAL_HEADERS "internal/atomic_hook.h" "internal/cycleclock.h" + "internal/direct_mmap.h" "internal/endian.h" "internal/exception_testing.h" "internal/exception_safety_testing.h" diff --git a/Firestore/third_party/abseil-cpp/absl/base/attributes.h b/Firestore/third_party/abseil-cpp/absl/base/attributes.h index 4e1fc8b..a4ec7e7 100644 --- a/Firestore/third_party/abseil-cpp/absl/base/attributes.h +++ b/Firestore/third_party/abseil-cpp/absl/base/attributes.h @@ -527,17 +527,34 @@ #define ABSL_ATTRIBUTE_PACKED #endif +// ABSL_ATTRIBUTE_FUNC_ALIGN +// +// Tells the compiler to align the function start at least to certain +// alignment boundary +#if ABSL_HAVE_ATTRIBUTE(aligned) || (defined(__GNUC__) && !defined(__clang__)) +#define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) __attribute__((aligned(bytes))) +#else +#define ABSL_ATTRIBUTE_FUNC_ALIGN(bytes) +#endif + // ABSL_CONST_INIT // // A variable declaration annotated with the `ABSL_CONST_INIT` attribute will // not compile (on supported platforms) unless the variable has a constant // initializer. This is useful for variables with static and thread storage // duration, because it guarantees that they will not suffer from the so-called -// "static init order fiasco". +// "static init order fiasco". Prefer to put this attribute on the most visible +// declaration of the variable, if there's more than one, because code that +// accesses the variable can then use the attribute for optimization. // // Example: // -// ABSL_CONST_INIT static MyType my_var = MakeMyType(...); +// class MyClass { +// public: +// ABSL_CONST_INIT static MyType my_var; +// }; +// +// MyType MyClass::my_var = MakeMyType(...); // // Note that this attribute is redundant if the variable is declared constexpr. #if ABSL_HAVE_CPP_ATTRIBUTE(clang::require_constant_initialization) diff --git a/Firestore/third_party/abseil-cpp/absl/base/config.h b/Firestore/third_party/abseil-cpp/absl/base/config.h index 6703d0e..500bc8c 100644 --- a/Firestore/third_party/abseil-cpp/absl/base/config.h +++ b/Firestore/third_party/abseil-cpp/absl/base/config.h @@ -138,12 +138,16 @@ // supported. #ifdef ABSL_HAVE_THREAD_LOCAL #error ABSL_HAVE_THREAD_LOCAL cannot be directly set -#elif (!defined(__apple_build_version__) || \ - (__apple_build_version__ >= 8000042)) && \ - !(defined(__APPLE__) && TARGET_OS_IPHONE && \ - __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0) +#elif defined(__APPLE__) // Notes: Xcode's clang did not support `thread_local` until version -// 8, and even then not for all iOS < 9.0. +// 8, and even then not for all iOS < 9.0. Also, Xcode 9.3 started disallowing +// `thread_local` for 32-bit iOS simulator targeting iOS 9.x. +// `__has_feature` is only supported by Clang so it has be inside +// `defined(__APPLE__)` check. +#if __has_feature(cxx_thread_local) +#define ABSL_HAVE_THREAD_LOCAL 1 +#endif +#else // !defined(__APPLE__) #define ABSL_HAVE_THREAD_LOCAL 1 #endif @@ -176,16 +180,22 @@ // Checks whether the __int128 compiler extension for a 128-bit integral type is // supported. // -// Notes: __SIZEOF_INT128__ is defined by Clang and GCC when __int128 is -// supported, except on ppc64 and aarch64 where __int128 exists but has exhibits -// a sporadic compiler crashing bug. Nvidia's nvcc also defines __GNUC__ and -// __SIZEOF_INT128__ but not all versions actually support __int128. +// Note: __SIZEOF_INT128__ is defined by Clang and GCC when __int128 is +// supported, but we avoid using it in certain cases: +// * On Clang: +// * Building using Clang for Windows, where the Clang runtime library has +// 128-bit support only on LP64 architectures, but Windows is LLP64. +// * Building for aarch64, where __int128 exists but has exhibits a sporadic +// compiler crashing bug. +// * On Nvidia's nvcc: +// * nvcc also defines __GNUC__ and __SIZEOF_INT128__, but not all versions +// actually support __int128. #ifdef ABSL_HAVE_INTRINSIC_INT128 #error ABSL_HAVE_INTRINSIC_INT128 cannot be directly set #elif defined(__SIZEOF_INT128__) -#if (defined(__clang__) && !defined(__aarch64__)) || \ - (defined(__CUDACC__) && __CUDACC_VER_MAJOR__ >= 9) || \ - (!defined(__clang__) && !defined(__CUDACC__) && defined(__GNUC__)) +#if (defined(__clang__) && !defined(_WIN32) && !defined(__aarch64__)) || \ + (defined(__CUDACC__) && __CUDACC_VER_MAJOR__ >= 9) || \ + (defined(__GNUC__) && !defined(__clang__) && !defined(__CUDACC__)) #define ABSL_HAVE_INTRINSIC_INT128 1 #elif defined(__CUDACC__) // __CUDACC_VER__ is a full version number before CUDA 9, and is defined to a @@ -244,6 +254,7 @@ // Windows _WIN32 // NaCL __native_client__ // AsmJS __asmjs__ +// WebAssembly __wasm__ // Fuchsia __Fuchsia__ // // Note that since Android defines both __ANDROID__ and __linux__, one @@ -257,7 +268,7 @@ #error ABSL_HAVE_MMAP cannot be directly set #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \ defined(__ros__) || defined(__native_client__) || defined(__asmjs__) || \ - defined(__Fuchsia__) + defined(__wasm__) || defined(__Fuchsia__) #define ABSL_HAVE_MMAP 1 #endif diff --git a/Firestore/third_party/abseil-cpp/absl/base/internal/raw_logging.cc b/Firestore/third_party/abseil-cpp/absl/base/internal/raw_logging.cc index 86e34d4..1ce1388 100644 --- a/Firestore/third_party/abseil-cpp/absl/base/internal/raw_logging.cc +++ b/Firestore/third_party/abseil-cpp/absl/base/internal/raw_logging.cc @@ -20,6 +20,7 @@ #include <cstdlib> #include <cstring> +#include "absl/base/attributes.h" #include "absl/base/config.h" #include "absl/base/internal/atomic_hook.h" #include "absl/base/log_severity.h" @@ -35,7 +36,7 @@ // This preprocessor token is also defined in raw_io.cc. If you need to copy // this, consider moving both to config.h instead. #if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \ - defined(__Fuchsia__) + defined(__Fuchsia__) || defined(__native_client__) #include <unistd.h> @@ -81,6 +82,8 @@ static const char kTruncated[] = " ... (message truncated)\n"; // consumed bytes, and return whether the message fit without truncation. If // truncation occurred, if possible leave room in the buffer for the message // kTruncated[]. +inline static bool VADoRawLog(char** buf, int* size, const char* format, + va_list ap) ABSL_PRINTF_ATTRIBUTE(3, 0); inline static bool VADoRawLog(char** buf, int* size, const char* format, va_list ap) { int n = vsnprintf(*buf, *size, format, ap); @@ -101,12 +104,6 @@ inline static bool VADoRawLog(char** buf, int* size, static constexpr int kLogBufSize = 3000; -namespace absl { -namespace raw_logging_internal { -void SafeWriteToStderr(const char *s, size_t len); -} // namespace raw_logging_internal -} // namespace absl - namespace { // CAVEAT: vsnprintf called from *DoRawLog below has some (exotic) code paths @@ -129,6 +126,8 @@ bool DoRawLog(char** buf, int* size, const char* format, ...) { } void RawLogVA(absl::LogSeverity severity, const char* file, int line, + const char* format, va_list ap) ABSL_PRINTF_ATTRIBUTE(4, 0); +void RawLogVA(absl::LogSeverity severity, const char* file, int line, const char* format, va_list ap) { char buffer[kLogBufSize]; char* buf = buffer; @@ -183,12 +182,6 @@ void RawLogVA(absl::LogSeverity severity, const char* file, int line, namespace absl { namespace raw_logging_internal { - -// Writes the provided buffer directly to stderr, in a safe, low-level manner. -// -// In POSIX this means calling write(), which is async-signal safe and does -// not malloc. If the platform supports the SYS_write syscall, we invoke that -// directly to side-step any libc interception. void SafeWriteToStderr(const char *s, size_t len) { #if defined(ABSL_HAVE_SYSCALL_WRITE) syscall(SYS_write, STDERR_FILENO, s, len); @@ -204,6 +197,8 @@ void SafeWriteToStderr(const char *s, size_t len) { } void RawLog(absl::LogSeverity severity, const char* file, int line, + const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5); +void RawLog(absl::LogSeverity severity, const char* file, int line, const char* format, ...) { va_list ap; va_start(ap, format); diff --git a/Firestore/third_party/abseil-cpp/absl/base/internal/raw_logging.h b/Firestore/third_party/abseil-cpp/absl/base/internal/raw_logging.h index 1b2a44b..a2b7207 100644 --- a/Firestore/third_party/abseil-cpp/absl/base/internal/raw_logging.h +++ b/Firestore/third_party/abseil-cpp/absl/base/internal/raw_logging.h @@ -74,6 +74,13 @@ namespace raw_logging_internal { void RawLog(absl::LogSeverity severity, const char* file, int line, const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5); +// Writes the provided buffer directly to stderr, in a safe, low-level manner. +// +// In POSIX this means calling write(), which is async-signal safe and does +// not malloc. If the platform supports the SYS_write syscall, we invoke that +// directly to side-step any libc interception. +void SafeWriteToStderr(const char *s, size_t len); + // compile-time function to get the "base" filename, that is, the part of // a filename after the last "/" or "\" path separator. The search starts at // the end of the std::string; the second parameter is the length of the std::string. diff --git a/Firestore/third_party/abseil-cpp/absl/base/log_severity.h b/Firestore/third_party/abseil-cpp/absl/base/log_severity.h index e146bcb..e2931c3 100644 --- a/Firestore/third_party/abseil-cpp/absl/base/log_severity.h +++ b/Firestore/third_party/abseil-cpp/absl/base/log_severity.h @@ -22,6 +22,9 @@ namespace absl { +// Four severity levels are defined. Logging APIs should terminate the program +// when a message is logged at severity `kFatal`; the other levels have no +// special semantics. enum class LogSeverity : int { kInfo = 0, kWarning = 1, @@ -36,6 +39,8 @@ constexpr std::array<absl::LogSeverity, 4> LogSeverities() { absl::LogSeverity::kError, absl::LogSeverity::kFatal}}; } +// Returns the all-caps std::string representation (e.g. "INFO") of the specified +// severity level if it is one of the normal levels and "UNKNOWN" otherwise. constexpr const char* LogSeverityName(absl::LogSeverity s) { return s == absl::LogSeverity::kInfo ? "INFO" @@ -46,7 +51,8 @@ constexpr const char* LogSeverityName(absl::LogSeverity s) { : s == absl::LogSeverity::kFatal ? "FATAL" : "UNKNOWN"; } -// Note that out-of-range severities normalize to kInfo or kError, never kFatal. +// Values less than `kInfo` normalize to `kInfo`; values greater than `kFatal` +// normalize to `kError` (**NOT** `kFatal`). constexpr absl::LogSeverity NormalizeLogSeverity(absl::LogSeverity s) { return s < absl::LogSeverity::kInfo ? absl::LogSeverity::kInfo diff --git a/Firestore/third_party/abseil-cpp/absl/base/macros.h b/Firestore/third_party/abseil-cpp/absl/base/macros.h index 5ae0f05..114a7be 100644 --- a/Firestore/third_party/abseil-cpp/absl/base/macros.h +++ b/Firestore/third_party/abseil-cpp/absl/base/macros.h @@ -46,7 +46,7 @@ namespace absl { namespace macros_internal { template <typename T, size_t N> -char (&ArraySizeHelper(T (&array)[N]))[N]; +auto ArraySizeHelper(const T (&array)[N]) -> char (&)[N]; } // namespace macros_internal } // namespace absl #define ABSL_ARRAYSIZE(array) \ diff --git a/Firestore/third_party/abseil-cpp/absl/base/policy_checks.h b/Firestore/third_party/abseil-cpp/absl/base/policy_checks.h index 17c05c1..d634dac 100644 --- a/Firestore/third_party/abseil-cpp/absl/base/policy_checks.h +++ b/Firestore/third_party/abseil-cpp/absl/base/policy_checks.h @@ -46,15 +46,15 @@ // We support MSVC++ 14.0 update 2 and later. // This minimum will go up. -#if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 190023918 -#error "This package requires Visual Studio 2015 Update 2 or higher" +#if defined(_MSC_FULL_VER) && _MSC_FULL_VER < 190023918 && !defined(__clang__) +#error "This package requires Visual Studio 2015 Update 2 or higher." #endif // We support gcc 4.7 and later. // This minimum will go up. #if defined(__GNUC__) && !defined(__clang__) #if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7) -#error "This package requires gcc 4.7 or higher" +#error "This package requires gcc 4.7 or higher." #endif #endif @@ -62,7 +62,7 @@ // This corresponds to Apple Xcode version 4.5. // This minimum will go up. #if defined(__apple_build_version__) && __apple_build_version__ < 4211165 -#error "This package requires __apple_build_version__ of 4211165 or higher" +#error "This package requires __apple_build_version__ of 4211165 or higher." #endif // ----------------------------------------------------------------------------- @@ -96,4 +96,26 @@ #error "STLPort is not supported." #endif +// ----------------------------------------------------------------------------- +// `char` Size Check +// ----------------------------------------------------------------------------- + +// Abseil currently assumes CHAR_BIT == 8. If you would like to use Abseil on a +// platform where this is not the case, please provide us with the details about +// your platform so we can consider relaxing this requirement. +#if CHAR_BIT != 8 +#error "Abseil assumes CHAR_BIT == 8." +#endif + +// ----------------------------------------------------------------------------- +// `int` Size Check +// ----------------------------------------------------------------------------- + +// Abseil currently assumes that an int is 4 bytes. If you would like to use +// Abseil on a platform where this is not the case, please provide us with the +// details about your platform so we can consider relaxing this requirement. +#if INT_MAX < 2147483647 +#error "Abseil assumes that int is at least 4 bytes. " +#endif + #endif // ABSL_BASE_POLICY_CHECKS_H_ diff --git a/Firestore/third_party/abseil-cpp/absl/meta/type_traits.h b/Firestore/third_party/abseil-cpp/absl/meta/type_traits.h index f36a59a..ac5d8e1 100644 --- a/Firestore/third_party/abseil-cpp/absl/meta/type_traits.h +++ b/Firestore/third_party/abseil-cpp/absl/meta/type_traits.h @@ -150,6 +150,7 @@ struct is_trivially_destructible : std::integral_constant<bool, __has_trivial_destructor(T) && std::is_destructible<T>::value> { #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_DESTRUCTIBLE + private: static constexpr bool compliant = std::is_trivially_destructible<T>::value == is_trivially_destructible::value; static_assert(compliant || std::is_trivially_destructible<T>::value, @@ -199,6 +200,7 @@ struct is_trivially_default_constructible std::is_default_constructible<T>::value && is_trivially_destructible<T>::value> { #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE + private: static constexpr bool compliant = std::is_trivially_default_constructible<T>::value == is_trivially_default_constructible::value; @@ -230,6 +232,7 @@ struct is_trivially_copy_constructible std::is_copy_constructible<T>::value && is_trivially_destructible<T>::value> { #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_CONSTRUCTIBLE + private: static constexpr bool compliant = std::is_trivially_copy_constructible<T>::value == is_trivially_copy_constructible::value; @@ -262,6 +265,7 @@ struct is_trivially_copy_assignable : std::integral_constant<bool, __has_trivial_assign(T) && std::is_copy_assignable<T>::value> { #ifdef ABSL_HAVE_STD_IS_TRIVIALLY_ASSIGNABLE + private: static constexpr bool compliant = std::is_trivially_copy_assignable<T>::value == is_trivially_copy_assignable::value; diff --git a/Firestore/third_party/abseil-cpp/absl/numeric/int128.cc b/Firestore/third_party/abseil-cpp/absl/numeric/int128.cc index 00bf7f4..3688e5e 100644 --- a/Firestore/third_party/abseil-cpp/absl/numeric/int128.cc +++ b/Firestore/third_party/abseil-cpp/absl/numeric/int128.cc @@ -20,6 +20,7 @@ #include <iostream> // NOLINT(readability/streams) #include <sstream> #include <string> +#include <type_traits> namespace absl { @@ -104,11 +105,15 @@ void DivModImpl(uint128 dividend, uint128 divisor, uint128* quotient_ret, } template <typename T> -uint128 Initialize128FromFloat(T v) { +uint128 MakeUint128FromFloat(T v) { + static_assert(std::is_floating_point<T>::value, ""); + // Rounding behavior is towards zero, same as for built-in types. // Undefined behavior if v is NaN or cannot fit into uint128. - assert(!std::isnan(v) && v > -1 && v < std::ldexp(static_cast<T>(1), 128)); + assert(std::isfinite(v) && v > -1 && + (std::numeric_limits<T>::max_exponent <= 128 || + v < std::ldexp(static_cast<T>(1), 128))); if (v >= std::ldexp(static_cast<T>(1), 64)) { uint64_t hi = static_cast<uint64_t>(std::ldexp(v, -64)); @@ -120,28 +125,36 @@ uint128 Initialize128FromFloat(T v) { } } // namespace -uint128::uint128(float v) : uint128(Initialize128FromFloat(v)) {} -uint128::uint128(double v) : uint128(Initialize128FromFloat(v)) {} -uint128::uint128(long double v) : uint128(Initialize128FromFloat(v)) {} +uint128::uint128(float v) : uint128(MakeUint128FromFloat(v)) {} +uint128::uint128(double v) : uint128(MakeUint128FromFloat(v)) {} +uint128::uint128(long double v) : uint128(MakeUint128FromFloat(v)) {} -uint128& uint128::operator/=(uint128 other) { +uint128 operator/(uint128 lhs, uint128 rhs) { +#if defined(ABSL_HAVE_INTRINSIC_INT128) + return static_cast<unsigned __int128>(lhs) / + static_cast<unsigned __int128>(rhs); +#else // ABSL_HAVE_INTRINSIC_INT128 uint128 quotient = 0; uint128 remainder = 0; - DivModImpl(*this, other, "ient, &remainder); - *this = quotient; - return *this; + DivModImpl(lhs, rhs, "ient, &remainder); + return quotient; +#endif // ABSL_HAVE_INTRINSIC_INT128 } -uint128& uint128::operator%=(uint128 other) { +uint128 operator%(uint128 lhs, uint128 rhs) { +#if defined(ABSL_HAVE_INTRINSIC_INT128) + return static_cast<unsigned __int128>(lhs) % + static_cast<unsigned __int128>(rhs); +#else // ABSL_HAVE_INTRINSIC_INT128 uint128 quotient = 0; uint128 remainder = 0; - DivModImpl(*this, other, "ient, &remainder); - *this = remainder; - return *this; + DivModImpl(lhs, rhs, "ient, &remainder); + return remainder; +#endif // ABSL_HAVE_INTRINSIC_INT128 } -std::ostream& operator<<(std::ostream& o, uint128 b) { - std::ios_base::fmtflags flags = o.flags(); +namespace { +std::string Uint128ToFormattedString(uint128 v, std::ios_base::fmtflags flags) { // Select a divisor which is the largest power of the base < 2^64. uint128 div; int div_base_log; @@ -160,14 +173,14 @@ std::ostream& operator<<(std::ostream& o, uint128 b) { break; } - // Now piece together the uint128 representation from three chunks of - // the original value, each less than "div" and therefore representable - // as a uint64_t. + // Now piece together the uint128 representation from three chunks of the + // original value, each less than "div" and therefore representable as a + // uint64_t. std::ostringstream os; std::ios_base::fmtflags copy_mask = std::ios::basefield | std::ios::showbase | std::ios::uppercase; os.setf(flags & copy_mask, copy_mask); - uint128 high = b; + uint128 high = v; uint128 low; DivModImpl(high, div, &high, &low); uint128 mid; @@ -182,25 +195,31 @@ std::ostream& operator<<(std::ostream& o, uint128 b) { os << std::noshowbase << std::setfill('0') << std::setw(div_base_log); } os << Uint128Low64(low); - std::string rep = os.str(); + return os.str(); +} + +} // namespace + +std::ostream& operator<<(std::ostream& os, uint128 v) { + std::ios_base::fmtflags flags = os.flags(); + std::string rep = Uint128ToFormattedString(v, flags); // Add the requisite padding. - std::streamsize width = o.width(0); + std::streamsize width = os.width(0); if (static_cast<size_t>(width) > rep.size()) { std::ios::fmtflags adjustfield = flags & std::ios::adjustfield; if (adjustfield == std::ios::left) { - rep.append(width - rep.size(), o.fill()); + rep.append(width - rep.size(), os.fill()); } else if (adjustfield == std::ios::internal && (flags & std::ios::showbase) && - (flags & std::ios::basefield) == std::ios::hex && b != 0) { - rep.insert(2, width - rep.size(), o.fill()); + (flags & std::ios::basefield) == std::ios::hex && v != 0) { + rep.insert(2, width - rep.size(), os.fill()); } else { - rep.insert(0, width - rep.size(), o.fill()); + rep.insert(0, width - rep.size(), os.fill()); } } - // Stream the final representation in a single "<<" call. - return o << rep; + return os << rep; } } // namespace absl diff --git a/Firestore/third_party/abseil-cpp/absl/numeric/int128.h b/Firestore/third_party/abseil-cpp/absl/numeric/int128.h index d87cbbd..bc7dbb4 100644 --- a/Firestore/third_party/abseil-cpp/absl/numeric/int128.h +++ b/Firestore/third_party/abseil-cpp/absl/numeric/int128.h @@ -17,9 +17,10 @@ // File: int128.h // ----------------------------------------------------------------------------- // -// This header file defines 128-bit integer types. Currently, this file defines -// `uint128`, an unsigned 128-bit integer; a signed 128-bit integer is -// forthcoming. +// This header file defines 128-bit integer types. +// +// Currently, this file defines `uint128`, an unsigned 128-bit integer; a signed +// 128-bit integer is forthcoming. #ifndef ABSL_NUMERIC_INT128_H_ #define ABSL_NUMERIC_INT128_H_ @@ -37,14 +38,15 @@ namespace absl { + // uint128 // // An unsigned 128-bit integer type. The API is meant to mimic an intrinsic type // as closely as is practical, including exhibiting undefined behavior in // analogous cases (e.g. division by zero). This type is intended to be a // drop-in replacement once C++ supports an intrinsic `uint128_t` type; when -// that occurs, existing uses of `uint128` will continue to work using that new -// type. +// that occurs, existing well-behaved uses of `uint128` will continue to work +// using that new type. // // Note: code written with this type will continue to compile once `uint128_t` // is introduced, provided the replacement helper functions @@ -62,23 +64,30 @@ namespace absl { // However, a `uint128` differs from intrinsic integral types in the following // ways: // -// * Errors on implicit conversions that does not preserve value (such as +// * Errors on implicit conversions that do not preserve value (such as // loss of precision when converting to float values). // * Requires explicit construction from and conversion to floating point // types. // * Conversion to integral types requires an explicit static_cast() to // mimic use of the `-Wnarrowing` compiler flag. +// * The alignment requirement of `uint128` may differ from that of an +// intrinsic 128-bit integer type depending on platform and build +// configuration. // // Example: // -// float y = absl::kuint128max; // Error. uint128 cannot be implicitly -// // converted to float. +// float y = absl::Uint128Max(); // Error. uint128 cannot be implicitly +// // converted to float. // // absl::uint128 v; -// absl::uint64_t i = v; // Error -// absl::uint64_t i = static_cast<uint64_t>(v); // OK +// uint64_t i = v; // Error +// uint64_t i = static_cast<uint64_t>(v); // OK // -class alignas(16) uint128 { +class +#if defined(ABSL_HAVE_INTRINSIC_INT128) + alignas(unsigned __int128) +#endif // ABSL_HAVE_INTRINSIC_INT128 + uint128 { public: uint128() = default; @@ -175,10 +184,15 @@ class alignas(16) uint128 { // Example: // // absl::uint128 big = absl::MakeUint128(1, 0); - friend constexpr uint128 MakeUint128(uint64_t top, uint64_t bottom); + friend constexpr uint128 MakeUint128(uint64_t high, uint64_t low); + + // Uint128Max() + // + // Returns the highest value for a 128-bit unsigned integer. + friend constexpr uint128 Uint128Max(); private: - constexpr uint128(uint64_t top, uint64_t bottom); + constexpr uint128(uint64_t high, uint64_t low); // TODO(strel) Update implementation to use __int128 once all users of // uint128 are fixed to not depend on alignof(uint128) == 8. Also add @@ -195,10 +209,13 @@ class alignas(16) uint128 { #endif // byte order }; +// Prefer to use the constexpr `Uint128Max()`. +// +// TODO(absl-team) deprecate kuint128max once migration tool is released. extern const uint128 kuint128max; // allow uint128 to be logged -extern std::ostream& operator<<(std::ostream& o, uint128 b); +std::ostream& operator<<(std::ostream& os, uint128 v); // TODO(strel) add operator>>(std::istream&, uint128) @@ -208,8 +225,13 @@ extern std::ostream& operator<<(std::ostream& o, uint128 b); // Implementation details follow // -------------------------------------------------------------------------- -constexpr uint128 MakeUint128(uint64_t top, uint64_t bottom) { - return uint128(top, bottom); +constexpr uint128 MakeUint128(uint64_t high, uint64_t low) { + return uint128(high, low); +} + +constexpr uint128 Uint128Max() { + return uint128(std::numeric_limits<uint64_t>::max(), + std::numeric_limits<uint64_t>::max()); } // Assignment from integer types. @@ -249,34 +271,49 @@ inline uint128& uint128::operator=(unsigned __int128 v) { } #endif // ABSL_HAVE_INTRINSIC_INT128 -// Shift and arithmetic operators. +// Arithmetic operators. -inline uint128 operator<<(uint128 lhs, int amount) { - return uint128(lhs) <<= amount; +uint128 operator<<(uint128 lhs, int amount); +uint128 operator>>(uint128 lhs, int amount); +uint128 operator+(uint128 lhs, uint128 rhs); +uint128 operator-(uint128 lhs, uint128 rhs); +uint128 operator*(uint128 lhs, uint128 rhs); +uint128 operator/(uint128 lhs, uint128 rhs); +uint128 operator%(uint128 lhs, uint128 rhs); + +inline uint128& uint128::operator<<=(int amount) { + *this = *this << amount; + return *this; } -inline uint128 operator>>(uint128 lhs, int amount) { - return uint128(lhs) >>= amount; +inline uint128& uint128::operator>>=(int amount) { + *this = *this >> amount; + return *this; } -inline uint128 operator+(uint128 lhs, uint128 rhs) { - return uint128(lhs) += rhs; +inline uint128& uint128::operator+=(uint128 other) { + *this = *this + other; + return *this; } -inline uint128 operator-(uint128 lhs, uint128 rhs) { - return uint128(lhs) -= rhs; +inline uint128& uint128::operator-=(uint128 other) { + *this = *this - other; + return *this; } -inline uint128 operator*(uint128 lhs, uint128 rhs) { - return uint128(lhs) *= rhs; +inline uint128& uint128::operator*=(uint128 other) { + *this = *this * other; + return *this; } -inline uint128 operator/(uint128 lhs, uint128 rhs) { - return uint128(lhs) /= rhs; +inline uint128& uint128::operator/=(uint128 other) { + *this = *this / other; + return *this; } -inline uint128 operator%(uint128 lhs, uint128 rhs) { - return uint128(lhs) %= rhs; +inline uint128& uint128::operator%=(uint128 other) { + *this = *this % other; + return *this; } constexpr uint64_t Uint128Low64(uint128 v) { return v.lo_; } @@ -287,56 +324,62 @@ constexpr uint64_t Uint128High64(uint128 v) { return v.hi_; } #if defined(ABSL_IS_LITTLE_ENDIAN) -constexpr uint128::uint128(uint64_t top, uint64_t bottom) - : lo_(bottom), hi_(top) {} +constexpr uint128::uint128(uint64_t high, uint64_t low) + : lo_{low}, hi_{high} {} constexpr uint128::uint128(int v) - : lo_(v), hi_(v < 0 ? std::numeric_limits<uint64_t>::max() : 0) {} + : lo_{static_cast<uint64_t>(v)}, + hi_{v < 0 ? std::numeric_limits<uint64_t>::max() : 0} {} constexpr uint128::uint128(long v) // NOLINT(runtime/int) - : lo_(v), hi_(v < 0 ? std::numeric_limits<uint64_t>::max() : 0) {} + : lo_{static_cast<uint64_t>(v)}, + hi_{v < 0 ? std::numeric_limits<uint64_t>::max() : 0} {} constexpr uint128::uint128(long long v) // NOLINT(runtime/int) - : lo_(v), hi_(v < 0 ? std::numeric_limits<uint64_t>::max() : 0) {} + : lo_{static_cast<uint64_t>(v)}, + hi_{v < 0 ? std::numeric_limits<uint64_t>::max() : 0} {} -constexpr uint128::uint128(unsigned int v) : lo_(v), hi_(0) {} +constexpr uint128::uint128(unsigned int v) : lo_{v}, hi_{0} {} // NOLINTNEXTLINE(runtime/int) -constexpr uint128::uint128(unsigned long v) : lo_(v), hi_(0) {} +constexpr uint128::uint128(unsigned long v) : lo_{v}, hi_{0} {} // NOLINTNEXTLINE(runtime/int) -constexpr uint128::uint128(unsigned long long v) : lo_(v), hi_(0) {} +constexpr uint128::uint128(unsigned long long v) : lo_{v}, hi_{0} {} #ifdef ABSL_HAVE_INTRINSIC_INT128 constexpr uint128::uint128(__int128 v) - : lo_(static_cast<uint64_t>(v & ~uint64_t{0})), - hi_(static_cast<uint64_t>(static_cast<unsigned __int128>(v) >> 64)) {} + : lo_{static_cast<uint64_t>(v & ~uint64_t{0})}, + hi_{static_cast<uint64_t>(static_cast<unsigned __int128>(v) >> 64)} {} constexpr uint128::uint128(unsigned __int128 v) - : lo_(static_cast<uint64_t>(v & ~uint64_t{0})), - hi_(static_cast<uint64_t>(v >> 64)) {} + : lo_{static_cast<uint64_t>(v & ~uint64_t{0})}, + hi_{static_cast<uint64_t>(v >> 64)} {} #endif // ABSL_HAVE_INTRINSIC_INT128 #elif defined(ABSL_IS_BIG_ENDIAN) -constexpr uint128::uint128(uint64_t top, uint64_t bottom) - : hi_(top), lo_(bottom) {} +constexpr uint128::uint128(uint64_t high, uint64_t low) + : hi_{high}, lo_{low} {} constexpr uint128::uint128(int v) - : hi_(v < 0 ? std::numeric_limits<uint64_t>::max() : 0), lo_(v) {} + : hi_{v < 0 ? std::numeric_limits<uint64_t>::max() : 0}, + lo_{static_cast<uint64_t>(v)} {} constexpr uint128::uint128(long v) // NOLINT(runtime/int) - : hi_(v < 0 ? std::numeric_limits<uint64_t>::max() : 0), lo_(v) {} + : hi_{v < 0 ? std::numeric_limits<uint64_t>::max() : 0}, + lo_{static_cast<uint64_t>(v)} {} constexpr uint128::uint128(long long v) // NOLINT(runtime/int) - : hi_(v < 0 ? std::numeric_limits<uint64_t>::max() : 0), lo_(v) {} + : hi_{v < 0 ? std::numeric_limits<uint64_t>::max() : 0}, + lo_{static_cast<uint64_t>(v)} {} -constexpr uint128::uint128(unsigned int v) : hi_(0), lo_(v) {} +constexpr uint128::uint128(unsigned int v) : hi_{0}, lo_{v} {} // NOLINTNEXTLINE(runtime/int) -constexpr uint128::uint128(unsigned long v) : hi_(0), lo_(v) {} +constexpr uint128::uint128(unsigned long v) : hi_{0}, lo_{v} {} // NOLINTNEXTLINE(runtime/int) -constexpr uint128::uint128(unsigned long long v) : hi_(0), lo_(v) {} +constexpr uint128::uint128(unsigned long long v) : hi_{0}, lo_{v} {} #ifdef ABSL_HAVE_INTRINSIC_INT128 constexpr uint128::uint128(__int128 v) - : hi_(static_cast<uint64_t>(static_cast<unsigned __int128>(v) >> 64)), - lo_(static_cast<uint64_t>(v & ~uint64_t{0})) {} + : hi_{static_cast<uint64_t>(static_cast<unsigned __int128>(v) >> 64)}, + lo_{static_cast<uint64_t>(v & ~uint64_t{0})} {} constexpr uint128::uint128(unsigned __int128 v) - : hi_(static_cast<uint64_t>(v >> 64)), - lo_(static_cast<uint64_t>(v & ~uint64_t{0})) {} + : hi_{static_cast<uint64_t>(v >> 64)}, + lo_{static_cast<uint64_t>(v & ~uint64_t{0})} {} #endif // ABSL_HAVE_INTRINSIC_INT128 #else // byte order @@ -460,13 +503,10 @@ inline bool operator>=(uint128 lhs, uint128 rhs) { // Unary operators. inline uint128 operator-(uint128 val) { - const uint64_t hi_flip = ~Uint128High64(val); - const uint64_t lo_flip = ~Uint128Low64(val); - const uint64_t lo_add = lo_flip + 1; - if (lo_add < lo_flip) { - return MakeUint128(hi_flip + 1, lo_add); - } - return MakeUint128(hi_flip, lo_add); + uint64_t hi = ~Uint128High64(val); + uint64_t lo = ~Uint128Low64(val) + 1; + if (lo == 0) ++hi; // carry + return MakeUint128(hi, lo); } inline bool operator!(uint128 val) { @@ -512,88 +552,72 @@ inline uint128& uint128::operator^=(uint128 other) { return *this; } -// Shift and arithmetic assign operators. - -inline uint128& uint128::operator<<=(int amount) { - // Shifts of >= 128 are undefined. - assert(amount < 128); +// Arithmetic operators. +inline uint128 operator<<(uint128 lhs, int amount) { // uint64_t shifts of >= 64 are undefined, so we will need some // special-casing. if (amount < 64) { if (amount != 0) { - hi_ = (hi_ << amount) | (lo_ >> (64 - amount)); - lo_ = lo_ << amount; + return MakeUint128( + (Uint128High64(lhs) << amount) | (Uint128Low64(lhs) >> (64 - amount)), + Uint128Low64(lhs) << amount); } - } else { - hi_ = lo_ << (amount - 64); - lo_ = 0; + return lhs; } - return *this; + return MakeUint128(Uint128Low64(lhs) << (amount - 64), 0); } -inline uint128& uint128::operator>>=(int amount) { - // Shifts of >= 128 are undefined. - assert(amount < 128); - +inline uint128 operator>>(uint128 lhs, int amount) { // uint64_t shifts of >= 64 are undefined, so we will need some // special-casing. if (amount < 64) { if (amount != 0) { - lo_ = (lo_ >> amount) | (hi_ << (64 - amount)); - hi_ = hi_ >> amount; + return MakeUint128(Uint128High64(lhs) >> amount, + (Uint128Low64(lhs) >> amount) | + (Uint128High64(lhs) << (64 - amount))); } - } else { - lo_ = hi_ >> (amount - 64); - hi_ = 0; + return lhs; } - return *this; + return MakeUint128(0, Uint128High64(lhs) >> (amount - 64)); } -inline uint128& uint128::operator+=(uint128 other) { - hi_ += other.hi_; - uint64_t lolo = lo_ + other.lo_; - if (lolo < lo_) - ++hi_; - lo_ = lolo; - return *this; +inline uint128 operator+(uint128 lhs, uint128 rhs) { + uint128 result = MakeUint128(Uint128High64(lhs) + Uint128High64(rhs), + Uint128Low64(lhs) + Uint128Low64(rhs)); + if (Uint128Low64(result) < Uint128Low64(lhs)) { // check for carry + return MakeUint128(Uint128High64(result) + 1, Uint128Low64(result)); + } + return result; } -inline uint128& uint128::operator-=(uint128 other) { - hi_ -= other.hi_; - if (other.lo_ > lo_) --hi_; - lo_ -= other.lo_; - return *this; +inline uint128 operator-(uint128 lhs, uint128 rhs) { + uint128 result = MakeUint128(Uint128High64(lhs) - Uint128High64(rhs), + Uint128Low64(lhs) - Uint128Low64(rhs)); + if (Uint128Low64(lhs) < Uint128Low64(rhs)) { // check for carry + return MakeUint128(Uint128High64(result) - 1, Uint128Low64(result)); + } + return result; } -inline uint128& uint128::operator*=(uint128 other) { +inline uint128 operator*(uint128 lhs, uint128 rhs) { #if defined(ABSL_HAVE_INTRINSIC_INT128) // TODO(strel) Remove once alignment issues are resolved and unsigned __int128 // can be used for uint128 storage. - *this = static_cast<unsigned __int128>(*this) * - static_cast<unsigned __int128>(other); - return *this; + return static_cast<unsigned __int128>(lhs) * + static_cast<unsigned __int128>(rhs); #else // ABSL_HAVE_INTRINSIC128 - uint64_t a96 = hi_ >> 32; - uint64_t a64 = hi_ & 0xffffffff; - uint64_t a32 = lo_ >> 32; - uint64_t a00 = lo_ & 0xffffffff; - uint64_t b96 = other.hi_ >> 32; - uint64_t b64 = other.hi_ & 0xffffffff; - uint64_t b32 = other.lo_ >> 32; - uint64_t b00 = other.lo_ & 0xffffffff; - // multiply [a96 .. a00] x [b96 .. b00] - // terms higher than c96 disappear off the high side - // terms c96 and c64 are safe to ignore carry bit - uint64_t c96 = a96 * b00 + a64 * b32 + a32 * b64 + a00 * b96; - uint64_t c64 = a64 * b00 + a32 * b32 + a00 * b64; - this->hi_ = (c96 << 32) + c64; - this->lo_ = 0; - // add terms after this one at a time to capture carry - *this += uint128(a32 * b00) << 32; - *this += uint128(a00 * b32) << 32; - *this += a00 * b00; - return *this; + uint64_t a32 = Uint128Low64(lhs) >> 32; + uint64_t a00 = Uint128Low64(lhs) & 0xffffffff; + uint64_t b32 = Uint128Low64(rhs) >> 32; + uint64_t b00 = Uint128Low64(rhs) & 0xffffffff; + uint128 result = + MakeUint128(Uint128High64(lhs) * Uint128Low64(rhs) + + Uint128Low64(lhs) * Uint128High64(rhs) + a32 * b32, + a00 * b00); + result += uint128(a32 * b00) << 32; + result += uint128(a00 * b32) << 32; + return result; #endif // ABSL_HAVE_INTRINSIC128 } diff --git a/Firestore/third_party/abseil-cpp/absl/numeric/int128_have_intrinsic.inc b/Firestore/third_party/abseil-cpp/absl/numeric/int128_have_intrinsic.inc index 49bde07..ee2a093 100644 --- a/Firestore/third_party/abseil-cpp/absl/numeric/int128_have_intrinsic.inc +++ b/Firestore/third_party/abseil-cpp/absl/numeric/int128_have_intrinsic.inc @@ -1,3 +1,18 @@ -// This file will contain :int128 implementation details that depend on internal -// representation when ABSL_HAVE_INTRINSIC_INT128 is defined. This file will be +// +// Copyright 2017 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. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This file contains :int128 implementation details that depend on internal +// representation when ABSL_HAVE_INTRINSIC_INT128 is defined. This file is // included by int128.h. diff --git a/Firestore/third_party/abseil-cpp/absl/numeric/int128_no_intrinsic.inc b/Firestore/third_party/abseil-cpp/absl/numeric/int128_no_intrinsic.inc index 2dbff2b..0d0b3cf 100644 --- a/Firestore/third_party/abseil-cpp/absl/numeric/int128_no_intrinsic.inc +++ b/Firestore/third_party/abseil-cpp/absl/numeric/int128_no_intrinsic.inc @@ -1,3 +1,18 @@ -// This file will contain :int128 implementation details that depend on internal +// +// Copyright 2017 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. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// This file contains :int128 implementation details that depend on internal // representation when ABSL_HAVE_INTRINSIC_INT128 is *not* defined. This file -// will be included by int128.h. +// is included by int128.h. diff --git a/Firestore/third_party/abseil-cpp/absl/numeric/int128_test.cc b/Firestore/third_party/abseil-cpp/absl/numeric/int128_test.cc index d674cb1..79bcca9 100644 --- a/Firestore/third_party/abseil-cpp/absl/numeric/int128_test.cc +++ b/Firestore/third_party/abseil-cpp/absl/numeric/int128_test.cc @@ -110,7 +110,7 @@ TEST(Uint128, AllTests) { absl::uint128 big = absl::MakeUint128(2000, 2); absl::uint128 big_minus_one = absl::MakeUint128(2000, 1); absl::uint128 bigger = absl::MakeUint128(2001, 1); - absl::uint128 biggest = absl::kuint128max; + absl::uint128 biggest = absl::Uint128Max(); absl::uint128 high_low = absl::MakeUint128(1, 0); absl::uint128 low_high = absl::MakeUint128(0, std::numeric_limits<uint64_t>::max()); @@ -227,8 +227,10 @@ TEST(Uint128, AllTests) { EXPECT_EQ(big, -(-big)); EXPECT_EQ(two, -((-one) - 1)); - EXPECT_EQ(absl::kuint128max, -one); + EXPECT_EQ(absl::Uint128Max(), -one); EXPECT_EQ(zero, -zero); + + EXPECT_EQ(absl::Uint128Max(), absl::kuint128max); } TEST(Uint128, ConversionTests) { diff --git a/Firestore/third_party/abseil-cpp/absl/strings/escaping_test.cc b/Firestore/third_party/abseil-cpp/absl/strings/escaping_test.cc index e87f101..982989b 100644 --- a/Firestore/third_party/abseil-cpp/absl/strings/escaping_test.cc +++ b/Firestore/third_party/abseil-cpp/absl/strings/escaping_test.cc @@ -22,6 +22,7 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "absl/container/fixed_array.h" #include "absl/strings/str_cat.h" #include "absl/strings/internal/escaping_test_common.inc" diff --git a/Firestore/third_party/abseil-cpp/absl/strings/internal/stl_type_traits.h b/Firestore/third_party/abseil-cpp/absl/strings/internal/stl_type_traits.h index 8c3d877..04c4a53 100644 --- a/Firestore/third_party/abseil-cpp/absl/strings/internal/stl_type_traits.h +++ b/Firestore/third_party/abseil-cpp/absl/strings/internal/stl_type_traits.h @@ -79,31 +79,48 @@ struct IsSTLContainer template <typename C, template <typename...> class T, typename = void> struct IsBaseOfSpecializationImpl : std::false_type {}; -// IsBaseOfSpecializationImpl must have three partial specializations, -// because we must only compare templates that take the same number of -// arguments. Otherwise, for example, std::vector can be compared with std::map, -// and fail to compile because of too few or too many template arguments. -// -// We must also SFINAE on the existence of an allocator_type. Otherwise, we may -// try to compare, for example, a std::pair<std::string, std::string> with a -// std::vector<std::string, std:std::string>. This would fail to compile, because -// of expected properties of the type passed in as the allocator. -template <template <typename, typename> class U, - template <typename, typename> class T, typename... Args> +// IsBaseOfSpecializationImpl needs multiple partial specializations to SFINAE +// on the existence of container dependent types and plug them into the STL +// template. +template <typename C, template <typename, typename> class T> +struct IsBaseOfSpecializationImpl< + C, T, absl::void_t<typename C::value_type, typename C::allocator_type>> + : std::is_base_of<C, + T<typename C::value_type, typename C::allocator_type>> {}; +template <typename C, template <typename, typename, typename> class T> struct IsBaseOfSpecializationImpl< - U<Args...>, T, absl::void_t<typename U<Args...>::allocator_type>> - : std::is_base_of<U<Args...>, T<Args...>> {}; -template <template <typename, typename, typename> class U, - template <typename, typename, typename> class T, typename... Args> + C, T, + absl::void_t<typename C::key_type, typename C::key_compare, + typename C::allocator_type>> + : std::is_base_of<C, T<typename C::key_type, typename C::key_compare, + typename C::allocator_type>> {}; +template <typename C, template <typename, typename, typename, typename> class T> +struct IsBaseOfSpecializationImpl< + C, T, + absl::void_t<typename C::key_type, typename C::mapped_type, + typename C::key_compare, typename C::allocator_type>> + : std::is_base_of<C, + T<typename C::key_type, typename C::mapped_type, + typename C::key_compare, typename C::allocator_type>> { +}; +template <typename C, template <typename, typename, typename, typename> class T> struct IsBaseOfSpecializationImpl< - U<Args...>, T, absl::void_t<typename U<Args...>::allocator_type>> - : std::is_base_of<U<Args...>, T<Args...>> {}; -template <template <typename, typename, typename, typename> class U, - template <typename, typename, typename, typename> class T, - typename... Args> + C, T, + absl::void_t<typename C::key_type, typename C::hasher, + typename C::key_equal, typename C::allocator_type>> + : std::is_base_of<C, T<typename C::key_type, typename C::hasher, + typename C::key_equal, typename C::allocator_type>> { +}; +template <typename C, + template <typename, typename, typename, typename, typename> class T> struct IsBaseOfSpecializationImpl< - U<Args...>, T, absl::void_t<typename U<Args...>::allocator_type>> - : std::is_base_of<U<Args...>, T<Args...>> {}; + C, T, + absl::void_t<typename C::key_type, typename C::mapped_type, + typename C::hasher, typename C::key_equal, + typename C::allocator_type>> + : std::is_base_of<C, T<typename C::key_type, typename C::mapped_type, + typename C::hasher, typename C::key_equal, + typename C::allocator_type>> {}; template <typename C, template <typename...> class T> using IsBaseOfSpecialization = IsBaseOfSpecializationImpl<absl::decay_t<C>, T>; @@ -140,31 +157,47 @@ struct IsBaseOfSTLContainer template <typename C, template <typename...> class T, typename = void> struct IsConvertibleToSpecializationImpl : std::false_type {}; -// IsConvertibleToSpecializationImpl must have three partial specializations, -// because we must only compare templates that take the same number of -// arguments. Otherwise, for example, std::vector can be compared with std::map, -// and fail to compile because of too few or too many template arguments. -// -// We must also SFINAE on the existence of an allocator_type. Otherwise, we may -// try to compare, for example, a std::pair<std::string, std::string> with a -// std::vector<std::string, std:std::string>. This would fail to compile, because -// of expected properties of the type passed in as the allocator. -template <template <typename, typename> class U, - template <typename, typename> class T, typename... Args> +// IsConvertibleToSpecializationImpl needs multiple partial specializations to +// SFINAE on the existence of container dependent types and plug them into the +// STL template. +template <typename C, template <typename, typename> class T> +struct IsConvertibleToSpecializationImpl< + C, T, absl::void_t<typename C::value_type, typename C::allocator_type>> + : std::is_convertible< + C, T<typename C::value_type, typename C::allocator_type>> {}; +template <typename C, template <typename, typename, typename> class T> +struct IsConvertibleToSpecializationImpl< + C, T, + absl::void_t<typename C::key_type, typename C::key_compare, + typename C::allocator_type>> + : std::is_convertible<C, T<typename C::key_type, typename C::key_compare, + typename C::allocator_type>> {}; +template <typename C, template <typename, typename, typename, typename> class T> struct IsConvertibleToSpecializationImpl< - U<Args...>, T, absl::void_t<typename U<Args...>::allocator_type>> - : std::is_convertible<U<Args...>, T<Args...>> {}; -template <template <typename, typename, typename> class U, - template <typename, typename, typename> class T, typename... Args> + C, T, + absl::void_t<typename C::key_type, typename C::mapped_type, + typename C::key_compare, typename C::allocator_type>> + : std::is_convertible< + C, T<typename C::key_type, typename C::mapped_type, + typename C::key_compare, typename C::allocator_type>> {}; +template <typename C, template <typename, typename, typename, typename> class T> struct IsConvertibleToSpecializationImpl< - U<Args...>, T, absl::void_t<typename U<Args...>::allocator_type>> - : std::is_convertible<U<Args...>, T<Args...>> {}; -template <template <typename, typename, typename, typename> class U, - template <typename, typename, typename, typename> class T, - typename... Args> + C, T, + absl::void_t<typename C::key_type, typename C::hasher, + typename C::key_equal, typename C::allocator_type>> + : std::is_convertible< + C, T<typename C::key_type, typename C::hasher, typename C::key_equal, + typename C::allocator_type>> {}; +template <typename C, + template <typename, typename, typename, typename, typename> class T> struct IsConvertibleToSpecializationImpl< - U<Args...>, T, absl::void_t<typename U<Args...>::allocator_type>> - : std::is_convertible<U<Args...>, T<Args...>> {}; + C, T, + absl::void_t<typename C::key_type, typename C::mapped_type, + typename C::hasher, typename C::key_equal, + typename C::allocator_type>> + : std::is_convertible<C, T<typename C::key_type, typename C::mapped_type, + typename C::hasher, typename C::key_equal, + typename C::allocator_type>> {}; template <typename C, template <typename...> class T> using IsConvertibleToSpecialization = IsConvertibleToSpecializationImpl<absl::decay_t<C>, T>; diff --git a/Firestore/third_party/abseil-cpp/absl/strings/internal/utf8.h b/Firestore/third_party/abseil-cpp/absl/strings/internal/utf8.h index 5bd82e8..d2c3c0b 100644 --- a/Firestore/third_party/abseil-cpp/absl/strings/internal/utf8.h +++ b/Firestore/third_party/abseil-cpp/absl/strings/internal/utf8.h @@ -14,10 +14,6 @@ // // UTF8 utilities, implemented to reduce dependencies. // -// If you need Unicode specific processing (for example being aware of -// Unicode character boundaries, or knowledge of Unicode casing rules, -// or various forms of equivalence and normalization), take a look at -// files in i18n/utf8. #ifndef ABSL_STRINGS_INTERNAL_UTF8_H_ #define ABSL_STRINGS_INTERNAL_UTF8_H_ diff --git a/Firestore/third_party/abseil-cpp/absl/strings/str_cat.cc b/Firestore/third_party/abseil-cpp/absl/strings/str_cat.cc index 99eb289..3fe8c95 100644 --- a/Firestore/third_party/abseil-cpp/absl/strings/str_cat.cc +++ b/Firestore/third_party/abseil-cpp/absl/strings/str_cat.cc @@ -45,6 +45,37 @@ AlphaNum::AlphaNum(Hex hex) { piece_ = absl::string_view(beg, end - beg); } +AlphaNum::AlphaNum(Dec dec) { + assert(dec.width <= numbers_internal::kFastToBufferSize); + char* const end = &digits_[numbers_internal::kFastToBufferSize]; + char* const minfill = end - dec.width; + char* writer = end; + uint64_t value = dec.value; + bool neg = dec.neg; + while (value > 9) { + *--writer = '0' + (value % 10); + value /= 10; + } + *--writer = '0' + value; + if (neg) *--writer = '-'; + + ptrdiff_t fillers = writer - minfill; + if (fillers > 0) { + // Tricky: if the fill character is ' ', then it's <fill><+/-><digits> + // But...: if the fill character is '0', then it's <+/-><fill><digits> + bool add_sign_again = false; + if (neg && dec.fill == '0') { // If filling with '0', + ++writer; // ignore the sign we just added + add_sign_again = true; // and re-add the sign later. + } + writer -= fillers; + std::fill_n(writer, fillers, dec.fill); + if (add_sign_again) *--writer = '-'; + } + + piece_ = absl::string_view(writer, end - writer); +} + // ---------------------------------------------------------------------- // StrCat() // This merges the given strings or integers, with no delimiter. This diff --git a/Firestore/third_party/abseil-cpp/absl/strings/str_cat.h b/Firestore/third_party/abseil-cpp/absl/strings/str_cat.h index 1cf7b11..e38369c 100644 --- a/Firestore/third_party/abseil-cpp/absl/strings/str_cat.h +++ b/Firestore/third_party/abseil-cpp/absl/strings/str_cat.h @@ -76,10 +76,10 @@ struct AlphaNumBuffer { } // namespace strings_internal -// Enum that specifies the number of significant digits to return in a `Hex` -// conversion and fill character to use. A `kZeroPad2` value, for example, would -// produce hexadecimal strings such as "0A","0F" and 'kSpacePad5' value would -// produce hexadecimal strings such as " A"," F". +// Enum that specifies the number of significant digits to return in a `Hex` or +// `Dec` conversion and fill character to use. A `kZeroPad2` value, for example, +// would produce hexadecimal strings such as "0A","0F" and a 'kSpacePad5' value +// would produce hexadecimal strings such as " A"," F". enum PadSpec { kNoPad = 1, kZeroPad2, @@ -127,21 +127,32 @@ struct Hex { char fill; template <typename Int> - explicit Hex(Int v, PadSpec spec = absl::kNoPad, - typename std::enable_if<sizeof(Int) == 1>::type* = nullptr) + explicit Hex( + Int v, PadSpec spec = absl::kNoPad, + typename std::enable_if<sizeof(Int) == 1 && + !std::is_pointer<Int>::value>::type* = nullptr) : Hex(spec, static_cast<uint8_t>(v)) {} template <typename Int> - explicit Hex(Int v, PadSpec spec = absl::kNoPad, - typename std::enable_if<sizeof(Int) == 2>::type* = nullptr) + explicit Hex( + Int v, PadSpec spec = absl::kNoPad, + typename std::enable_if<sizeof(Int) == 2 && + !std::is_pointer<Int>::value>::type* = nullptr) : Hex(spec, static_cast<uint16_t>(v)) {} template <typename Int> - explicit Hex(Int v, PadSpec spec = absl::kNoPad, - typename std::enable_if<sizeof(Int) == 4>::type* = nullptr) + explicit Hex( + Int v, PadSpec spec = absl::kNoPad, + typename std::enable_if<sizeof(Int) == 4 && + !std::is_pointer<Int>::value>::type* = nullptr) : Hex(spec, static_cast<uint32_t>(v)) {} template <typename Int> - explicit Hex(Int v, PadSpec spec = absl::kNoPad, - typename std::enable_if<sizeof(Int) == 8>::type* = nullptr) + explicit Hex( + Int v, PadSpec spec = absl::kNoPad, + typename std::enable_if<sizeof(Int) == 8 && + !std::is_pointer<Int>::value>::type* = nullptr) : Hex(spec, static_cast<uint64_t>(v)) {} + template <typename Pointee> + explicit Hex(Pointee* v, PadSpec spec = absl::kNoPad) + : Hex(spec, reinterpret_cast<uintptr_t>(v)) {} private: Hex(PadSpec spec, uint64_t v) @@ -154,6 +165,32 @@ struct Hex { }; // ----------------------------------------------------------------------------- +// Dec +// ----------------------------------------------------------------------------- +// +// `Dec` stores a set of decimal std::string conversion parameters for use +// within `AlphaNum` std::string conversions. Dec is slower than the default +// integer conversion, so use it only if you need padding. +struct Dec { + uint64_t value; + uint8_t width; + char fill; + bool neg; + + template <typename Int> + explicit Dec(Int v, PadSpec spec = absl::kNoPad, + typename std::enable_if<(sizeof(Int) <= 8)>::type* = nullptr) + : value(v >= 0 ? static_cast<uint64_t>(v) + : uint64_t{0} - static_cast<uint64_t>(v)), + width(spec == absl::kNoPad + ? 1 + : spec >= absl::kSpacePad2 ? spec - absl::kSpacePad2 + 2 + : spec - absl::kZeroPad2 + 2), + fill(spec >= absl::kSpacePad2 ? ' ' : '0'), + neg(v < 0) {} +}; + +// ----------------------------------------------------------------------------- // AlphaNum // ----------------------------------------------------------------------------- // @@ -191,6 +228,7 @@ class AlphaNum { : piece_(digits_, numbers_internal::SixDigitsToBuffer(f, digits_)) {} AlphaNum(Hex hex); // NOLINT(runtime/explicit) + AlphaNum(Dec dec); // NOLINT(runtime/explicit) template <size_t size> AlphaNum( // NOLINT(runtime/explicit) diff --git a/Firestore/third_party/abseil-cpp/absl/strings/str_cat_test.cc b/Firestore/third_party/abseil-cpp/absl/strings/str_cat_test.cc index dd063b0..58ab743 100644 --- a/Firestore/third_party/abseil-cpp/absl/strings/str_cat_test.cc +++ b/Firestore/third_party/abseil-cpp/absl/strings/str_cat_test.cc @@ -234,7 +234,7 @@ TEST(StrCat, CustomAllocator) { TEST(StrCat, MaxArgs) { std::string result; - // Test 10 up to 26 arguments, the current maximum + // Test 10 up to 26 arguments, the old maximum result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a"); EXPECT_EQ(result, "123456789a"); result = absl::StrCat(1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b"); @@ -431,28 +431,97 @@ void CheckHex(IntType v, const char* nopad_format, const char* zeropad_format, } } -void CheckHex64(uint64_t v) { - unsigned long long llv = v; // NOLINT(runtime/int) +template <typename IntType> +void CheckDec(IntType v, const char* nopad_format, const char* zeropad_format, + const char* spacepad_format) { + char expected[256]; + + std::string actual = absl::StrCat(absl::Dec(v, absl::kNoPad)); + snprintf(expected, sizeof(expected), nopad_format, v); + EXPECT_EQ(expected, actual) << " decimal value " << v; + + for (int spec = absl::kZeroPad2; spec <= absl::kZeroPad16; ++spec) { + std::string actual = + absl::StrCat(absl::Dec(v, static_cast<absl::PadSpec>(spec))); + snprintf(expected, sizeof(expected), zeropad_format, + spec - absl::kZeroPad2 + 2, v); + EXPECT_EQ(expected, actual) + << " decimal value " << v << " format '" << zeropad_format + << "' digits " << (spec - absl::kZeroPad2 + 2); + } - CheckHex(llv, "%llx", "%0*llx", "%*llx"); + for (int spec = absl::kSpacePad2; spec <= absl::kSpacePad16; ++spec) { + std::string actual = + absl::StrCat(absl::Dec(v, static_cast<absl::PadSpec>(spec))); + snprintf(expected, sizeof(expected), spacepad_format, + spec - absl::kSpacePad2 + 2, v); + EXPECT_EQ(expected, actual) + << " decimal value " << v << " format '" << spacepad_format + << "' digits " << (spec - absl::kSpacePad2 + 2); + } +} + +void CheckHexDec64(uint64_t v) { + unsigned long long ullv = v; // NOLINT(runtime/int) + + CheckHex(ullv, "%llx", "%0*llx", "%*llx"); + CheckDec(ullv, "%llu", "%0*llu", "%*llu"); + + long long llv = static_cast<long long>(ullv); // NOLINT(runtime/int) + CheckDec(llv, "%lld", "%0*lld", "%*lld"); + + if (sizeof(v) == sizeof(&v)) { + auto uintptr = static_cast<uintptr_t>(v); + void* ptr = reinterpret_cast<void*>(uintptr); + CheckHex(ptr, "%llx", "%0*llx", "%*llx"); + } +} + +void CheckHexDec32(uint32_t uv) { + CheckHex(uv, "%x", "%0*x", "%*x"); + CheckDec(uv, "%u", "%0*u", "%*u"); + int32_t v = static_cast<int32_t>(uv); + CheckDec(v, "%d", "%0*d", "%*d"); + + if (sizeof(v) == sizeof(&v)) { + auto uintptr = static_cast<uintptr_t>(v); + void* ptr = reinterpret_cast<void*>(uintptr); + CheckHex(ptr, "%x", "%0*x", "%*x"); + } } -template <typename Int32Type> -void CheckHex32(Int32Type v) { - CheckHex(v, "%x", "%0*x", "%*x"); +void CheckAll(uint64_t v) { + CheckHexDec64(v); + CheckHexDec32(static_cast<uint32_t>(v)); } void TestFastPrints() { - // Test min int to make sure that works + // Test all small ints; there aren't many and they're common. for (int i = 0; i < 10000; i++) { - CheckHex64(i); - CheckHex32(static_cast<uint32_t>(i)); - CheckHex32(i); - CheckHex32(-i); + CheckAll(i); } - CheckHex64(uint64_t{0x123456789abcdef0}); - CheckHex32(0x12345678U); + CheckAll(std::numeric_limits<uint64_t>::max()); + CheckAll(std::numeric_limits<uint64_t>::max() - 1); + CheckAll(std::numeric_limits<int64_t>::min()); + CheckAll(std::numeric_limits<int64_t>::min() + 1); + CheckAll(std::numeric_limits<uint32_t>::max()); + CheckAll(std::numeric_limits<uint32_t>::max() - 1); + CheckAll(std::numeric_limits<int32_t>::min()); + CheckAll(std::numeric_limits<int32_t>::min() + 1); + CheckAll(999999999); // fits in 32 bits + CheckAll(1000000000); // fits in 32 bits + CheckAll(9999999999); // doesn't fit in 32 bits + CheckAll(10000000000); // doesn't fit in 32 bits + CheckAll(999999999999999999); // fits in signed 64-bit + CheckAll(9999999999999999999u); // fits in unsigned 64-bit, but not signed. + CheckAll(1000000000000000000); // fits in signed 64-bit + CheckAll(10000000000000000000u); // fits in unsigned 64-bit, but not signed. + + CheckAll(999999999876543210); // check all decimal digits, signed + CheckAll(9999999999876543210u); // check all decimal digits, unsigned. + CheckAll(0x123456789abcdef0); // check all hex digits + CheckAll(0x12345678); int8_t minus_one_8bit = -1; EXPECT_EQ("ff", absl::StrCat(absl::Hex(minus_one_8bit))); diff --git a/Firestore/third_party/abseil-cpp/absl/strings/str_split.h b/Firestore/third_party/abseil-cpp/absl/strings/str_split.h index 5b3d6a8..1f089b9 100644 --- a/Firestore/third_party/abseil-cpp/absl/strings/str_split.h +++ b/Firestore/third_party/abseil-cpp/absl/strings/str_split.h @@ -208,7 +208,7 @@ class ByAnyChar { // using absl::ByLength; // std::vector<std::string> v = absl::StrSplit("12345", ByLength(2)); // -// // v[0] == "12", v[1] == "35", v[2] == "5" +// // v[0] == "12", v[1] == "34", v[2] == "5" class ByLength { public: explicit ByLength(ptrdiff_t length); @@ -402,7 +402,7 @@ struct SkipWhitespace { // // std::vector<std::string> v = absl::StrSplit(" a , ,,b,", // ',', SkipWhitespace()); -// // v[0] == "a", v[1] == "b" +// // v[0] == " a ", v[1] == "b" // // See above for more information on predicates. // diff --git a/Firestore/third_party/abseil-cpp/absl/strings/str_split_test.cc b/Firestore/third_party/abseil-cpp/absl/strings/str_split_test.cc index 500f3cb..c172a76 100644 --- a/Firestore/third_party/abseil-cpp/absl/strings/str_split_test.cc +++ b/Firestore/third_party/abseil-cpp/absl/strings/str_split_test.cc @@ -154,8 +154,8 @@ TEST(Split, APIExamples) { { // Uses the SkipWhitespace predicate. using absl::SkipWhitespace; - std::vector<std::string> v = absl::StrSplit("a, ,,b,", ',', SkipWhitespace()); - EXPECT_THAT(v, ElementsAre("a", "b")); + std::vector<std::string> v = absl::StrSplit(" a , ,,b,", ',', SkipWhitespace()); + EXPECT_THAT(v, ElementsAre(" a ", "b")); } { @@ -241,10 +241,10 @@ TEST(SplitIterator, Basics) { EXPECT_NE(it, end); EXPECT_EQ("a", *it); // tests dereference - ++it; // tests preincrement + ++it; // tests preincrement EXPECT_NE(it, end); EXPECT_EQ("b", std::string(it->data(), it->size())); // tests dereference as ptr - it++; // tests postincrement + it++; // tests postincrement EXPECT_EQ(it, end); } @@ -265,10 +265,10 @@ TEST(SplitIterator, Predicate) { EXPECT_NE(it, end); EXPECT_EQ("a", *it); // tests dereference - ++it; // tests preincrement -- "b" should be skipped here. + ++it; // tests preincrement -- "b" should be skipped here. EXPECT_NE(it, end); EXPECT_EQ("c", std::string(it->data(), it->size())); // tests dereference as ptr - it++; // tests postincrement + it++; // tests postincrement EXPECT_EQ(it, end); } @@ -278,13 +278,13 @@ TEST(SplitIterator, EdgeCases) { std::string in; std::vector<std::string> expect; } specs[] = { - {"", {""}}, - {"foo", {"foo"}}, - {",", {"", ""}}, - {",foo", {"", "foo"}}, - {"foo,", {"foo", ""}}, - {",foo,", {"", "foo", ""}}, - {"foo,bar", {"foo", "bar"}}, + {"", {""}}, + {"foo", {"foo"}}, + {",", {"", ""}}, + {",foo", {"", "foo"}}, + {"foo,", {"foo", ""}}, + {",foo,", {"", "foo", ""}}, + {"foo,bar", {"foo", "bar"}}, }; for (const auto& spec : specs) { @@ -621,23 +621,28 @@ TEST(Split, StringDelimiter) { TEST(Split, UTF8) { // Tests splitting utf8 strings and utf8 delimiters. + std::string utf8_string = u8"\u03BA\u1F79\u03C3\u03BC\u03B5"; { // A utf8 input std::string with an ascii delimiter. - std::vector<absl::string_view> v = absl::StrSplit("a,κόσμε", ','); - EXPECT_THAT(v, ElementsAre("a", "κόσμε")); + std::string to_split = "a," + utf8_string; + std::vector<absl::string_view> v = absl::StrSplit(to_split, ','); + EXPECT_THAT(v, ElementsAre("a", utf8_string)); } { // A utf8 input std::string and a utf8 delimiter. - std::vector<absl::string_view> v = absl::StrSplit("a,κόσμε,b", ",κόσμε,"); + std::string to_split = "a," + utf8_string + ",b"; + std::string unicode_delimiter = "," + utf8_string + ","; + std::vector<absl::string_view> v = + absl::StrSplit(to_split, unicode_delimiter); EXPECT_THAT(v, ElementsAre("a", "b")); } { // A utf8 input std::string and ByAnyChar with ascii chars. std::vector<absl::string_view> v = - absl::StrSplit("Foo hällo th丞re", absl::ByAnyChar(" \t")); - EXPECT_THAT(v, ElementsAre("Foo", "hällo", "th丞re")); + absl::StrSplit(u8"Foo h\u00E4llo th\u4E1Ere", absl::ByAnyChar(" \t")); + EXPECT_THAT(v, ElementsAre("Foo", u8"h\u00E4llo", u8"th\u4E1Ere")); } } diff --git a/Firestore/third_party/abseil-cpp/absl/strings/strip_test.cc b/Firestore/third_party/abseil-cpp/absl/strings/strip_test.cc index ff0e7f1..205c160 100644 --- a/Firestore/third_party/abseil-cpp/absl/strings/strip_test.cc +++ b/Firestore/third_party/abseil-cpp/absl/strings/strip_test.cc @@ -178,4 +178,24 @@ TEST(String, StripLeadingAsciiWhitespace) { EXPECT_EQ(absl::string_view(), absl::StripLeadingAsciiWhitespace(orig)); } +TEST(Strip, StripAsciiWhitespace) { + std::string test2 = "\t \f\r\n\vfoo \t\f\r\v\n"; + absl::StripAsciiWhitespace(&test2); + EXPECT_EQ(test2, "foo"); + std::string test3 = "bar"; + absl::StripAsciiWhitespace(&test3); + EXPECT_EQ(test3, "bar"); + std::string test4 = "\t \f\r\n\vfoo"; + absl::StripAsciiWhitespace(&test4); + EXPECT_EQ(test4, "foo"); + std::string test5 = "foo \t\f\r\v\n"; + absl::StripAsciiWhitespace(&test5); + EXPECT_EQ(test5, "foo"); + absl::string_view test6("\t \f\r\n\vfoo \t\f\r\v\n"); + test6 = absl::StripAsciiWhitespace(test6); + EXPECT_EQ(test6, "foo"); + test6 = absl::StripAsciiWhitespace(test6); + EXPECT_EQ(test6, "foo"); // already stripped +} + } // namespace diff --git a/Firestore/third_party/abseil-cpp/absl/strings/substitute_test.cc b/Firestore/third_party/abseil-cpp/absl/strings/substitute_test.cc index a6d7d7b..7c9af6b 100644 --- a/Firestore/third_party/abseil-cpp/absl/strings/substitute_test.cc +++ b/Firestore/third_party/abseil-cpp/absl/strings/substitute_test.cc @@ -46,8 +46,14 @@ TEST(SubstituteTest, Substitute) { // Pointer. const int* int_p = reinterpret_cast<const int*>(0x12345); std::string str = absl::Substitute("$0", int_p); - EXPECT_EQ(absl::StrCat("0x", absl::Hex(reinterpret_cast<intptr_t>(int_p))), - str); + EXPECT_EQ(absl::StrCat("0x", absl::Hex(int_p)), str); + + // Volatile Pointer. + // Like C++ streamed I/O, such pointers implicitly become bool + volatile int vol = 237; + volatile int *volatile volptr = &vol; + str = absl::Substitute("$0", volptr); + EXPECT_EQ("true", str); // null is special. StrCat prints 0x0. Substitute prints NULL. const uint64_t* null_p = nullptr; diff --git a/Firestore/third_party/abseil-cpp/absl/types/CMakeLists.txt b/Firestore/third_party/abseil-cpp/absl/types/CMakeLists.txt new file mode 100644 index 0000000..fd71f38 --- /dev/null +++ b/Firestore/third_party/abseil-cpp/absl/types/CMakeLists.txt @@ -0,0 +1,188 @@ +# +# Copyright 2017 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +list(APPEND TYPES_PUBLIC_HEADERS + "any.h" + "bad_any_cast.h" + "bad_optional_access.h" + "optional.h" + "span.h" +) + + +# any library +absl_header_library( + TARGET + absl_any + PUBLIC_LIBRARIES + absl::utility + PRIVATE_COMPILE_FLAGS + ${ABSL_EXCEPTIONS_FLAG} + EXPORT_NAME + any +) + +# span library +absl_header_library( + TARGET + absl_span + PUBLIC_LIBRARIES + absl::utility + EXPORT_NAME + span +) + + +# bad_any_cast library +list(APPEND BAD_ANY_CAST_SRC + "bad_any_cast.cc" + ${TYPES_PUBLIC_HEADERS} +) + +absl_library( + TARGET + absl_bad_any_cast + SOURCES + ${BAD_ANY_CAST_SRC} + PUBLIC_LIBRARIES + absl::base absl::any + EXPORT_NAME + bad_any_cast +) + + +# optional library +list(APPEND OPTIONAL_SRC + "optional.cc" +) + +absl_library( + TARGET + absl_optional + SOURCES + ${OPTIONAL_SRC} + PUBLIC_LIBRARIES + absl::base + EXPORT_NAME + optional +) + + +set(BAD_OPTIONAL_ACCESS_SRC "bad_optional_access.cc") +set(BAD_OPTIONAL_ACCESS_LIBRARIES absl::base) + +absl_library( + TARGET + absl_bad_optional_access + SOURCES + ${BAD_OPTIONAL_ACCESS_SRC} + PUBLIC_LIBRARIES + ${BAD_OPTIONAL_ACCESS_PUBLIC_LIBRARIES} + EXPORT_NAME + bad_optional_access +) + + + +# +## TESTS +# + + +# test any_test +set(ANY_TEST_SRC "any_test.cc") +set(ANY_TEST_PUBLIC_LIBRARIES absl::base absl::throw_delegate absl::any absl::bad_any_cast test_instance_tracker_lib) + +absl_test( + TARGET + any_test + SOURCES + ${ANY_TEST_SRC} + PUBLIC_LIBRARIES + ${ANY_TEST_PUBLIC_LIBRARIES} + PRIVATE_COMPILE_FLAGS + ${ABSL_EXCEPTIONS_FLAG} +) + + +# test any_test_noexceptions +absl_test( + TARGET + any_test_noexceptions + SOURCES + ${ANY_TEST_SRC} + PUBLIC_LIBRARIES + ${ANY_TEST_PUBLIC_LIBRARIES} +) + +# test any_exception_safety_test +set(ANY_EXCEPTION_SAFETY_TEST_SRC "any_exception_safety_test.cc") +set(ANY_EXCEPTION_SAFETY_TEST_PUBLIC_LIBRARIES absl::any absl::base absl::base_internal_exception_safety_testing) + +absl_test( + TARGET + any_exception_safety_test + SOURCES + ${ANY_EXCEPTION_SAFETY_TEST_SRC} + PUBLIC_LIBRARIES + ${ANY_EXCEPTION_SAFETY_TEST_PUBLIC_LIBRARIES} + PRIVATE_COMPILE_FLAGS + ${ABSL_EXCEPTIONS_FLAG} +) + + +# test span_test +set(SPAN_TEST_SRC "span_test.cc") +set(SPAN_TEST_PUBLIC_LIBRARIES absl::base absl::strings absl::throw_delegate absl::span test_instance_tracker_lib) + +absl_test( + TARGET + span_test + SOURCES + ${SPAN_TEST_SRC} + PUBLIC_LIBRARIES + ${SPAN_TEST_PUBLIC_LIBRARIES} + PRIVATE_COMPILE_FLAGS + ${ABSL_EXCEPTIONS_FLAG} +) + + +# test span_test_noexceptions +absl_test( + TARGET + span_test_noexceptions + SOURCES + ${SPAN_TEST_SRC} + PUBLIC_LIBRARIES + ${SPAN_TEST_PUBLIC_LIBRARIES} +) + + + +# test optional_test +set(OPTIONAL_TEST_SRC "optional_test.cc") +set(OPTIONAL_TEST_PUBLIC_LIBRARIES absl::base absl::throw_delegate absl::optional absl_bad_optional_access) + +absl_test( + TARGET + optional_test + SOURCES + ${OPTIONAL_TEST_SRC} + PUBLIC_LIBRARIES + ${OPTIONAL_TEST_PUBLIC_LIBRARIES} +) + + diff --git a/Firestore/third_party/abseil-cpp/absl/types/optional_test.cc b/Firestore/third_party/abseil-cpp/absl/types/optional_test.cc new file mode 100644 index 0000000..5eedfcf --- /dev/null +++ b/Firestore/third_party/abseil-cpp/absl/types/optional_test.cc @@ -0,0 +1,1598 @@ +// Copyright 2017 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. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "absl/types/optional.h" + +#include <string> +#include <type_traits> +#include <utility> + +#include "gtest/gtest.h" +#include "absl/base/config.h" +#include "absl/base/internal/raw_logging.h" +#include "absl/meta/type_traits.h" +#include "absl/strings/string_view.h" + +struct Hashable {}; + +namespace std { +template <> +struct hash<Hashable> { + size_t operator()(const Hashable&) { return 0; } +}; +} // namespace std + +struct NonHashable {}; + +namespace { + +std::string TypeQuals(std::string&) { return "&"; } +std::string TypeQuals(std::string&&) { return "&&"; } +std::string TypeQuals(const std::string&) { return "c&"; } +std::string TypeQuals(const std::string&&) { return "c&&"; } + +struct StructorListener { + int construct0 = 0; + int construct1 = 0; + int construct2 = 0; + int listinit = 0; + int copy = 0; + int move = 0; + int copy_assign = 0; + int move_assign = 0; + int destruct = 0; + int volatile_copy = 0; + int volatile_move = 0; + int volatile_copy_assign = 0; + int volatile_move_assign = 0; +}; + +// Suppress MSVC warnings. +// 4521: multiple copy constructors specified +// 4522: multiple assignment operators specified +// We wrote multiple of them to test that the correct overloads are selected. +#ifdef _MSC_VER +#pragma warning( push ) +#pragma warning( disable : 4521) +#pragma warning( disable : 4522) +#endif +struct Listenable { + static StructorListener* listener; + + Listenable() { ++listener->construct0; } + explicit Listenable(int /*unused*/) { ++listener->construct1; } + Listenable(int /*unused*/, int /*unused*/) { ++listener->construct2; } + Listenable(std::initializer_list<int> /*unused*/) { ++listener->listinit; } + Listenable(const Listenable& /*unused*/) { ++listener->copy; } + Listenable(const volatile Listenable& /*unused*/) { + ++listener->volatile_copy; + } + Listenable(volatile Listenable&& /*unused*/) { ++listener->volatile_move; } + Listenable(Listenable&& /*unused*/) { ++listener->move; } + Listenable& operator=(const Listenable& /*unused*/) { + ++listener->copy_assign; + return *this; + } + Listenable& operator=(Listenable&& /*unused*/) { + ++listener->move_assign; + return *this; + } + // use void return type instead of volatile T& to work around GCC warning + // when the assignment's returned reference is ignored. + void operator=(const volatile Listenable& /*unused*/) volatile { + ++listener->volatile_copy_assign; + } + void operator=(volatile Listenable&& /*unused*/) volatile { + ++listener->volatile_move_assign; + } + ~Listenable() { ++listener->destruct; } +}; +#ifdef _MSC_VER +#pragma warning( pop ) +#endif + +StructorListener* Listenable::listener = nullptr; + +// ABSL_HAVE_NO_CONSTEXPR_INITIALIZER_LIST is defined to 1 when the standard +// library implementation doesn't marked initializer_list's default constructor +// constexpr. The C++11 standard doesn't specify constexpr on it, but C++14 +// added it. However, libstdc++ 4.7 marked it constexpr. +#if defined(_LIBCPP_VERSION) && \ + (_LIBCPP_STD_VER <= 11 || defined(_LIBCPP_HAS_NO_CXX14_CONSTEXPR)) +#define ABSL_HAVE_NO_CONSTEXPR_INITIALIZER_LIST 1 +#endif + +struct ConstexprType { + enum CtorTypes { + kCtorDefault, + kCtorInt, + kCtorInitializerList, + kCtorConstChar + }; + constexpr ConstexprType() : x(kCtorDefault) {} + constexpr explicit ConstexprType(int i) : x(kCtorInt) {} +#ifndef ABSL_HAVE_NO_CONSTEXPR_INITIALIZER_LIST + constexpr ConstexprType(std::initializer_list<int> il) + : x(kCtorInitializerList) {} +#endif + constexpr ConstexprType(const char*) // NOLINT(runtime/explicit) + : x(kCtorConstChar) {} + int x; +}; + +struct Copyable { + Copyable() {} + Copyable(const Copyable&) {} + Copyable& operator=(const Copyable&) { return *this; } +}; + +struct MoveableThrow { + MoveableThrow() {} + MoveableThrow(MoveableThrow&&) {} + MoveableThrow& operator=(MoveableThrow&&) { return *this; } +}; + +struct MoveableNoThrow { + MoveableNoThrow() {} + MoveableNoThrow(MoveableNoThrow&&) noexcept {} + MoveableNoThrow& operator=(MoveableNoThrow&&) noexcept { return *this; } +}; + +struct NonMovable { + NonMovable() {} + NonMovable(const NonMovable&) = delete; + NonMovable& operator=(const NonMovable&) = delete; + NonMovable(NonMovable&&) = delete; + NonMovable& operator=(NonMovable&&) = delete; +}; + +TEST(optionalTest, DefaultConstructor) { + absl::optional<int> empty; + EXPECT_FALSE(empty); + constexpr absl::optional<int> cempty; + static_assert(!cempty.has_value(), ""); + EXPECT_TRUE( + std::is_nothrow_default_constructible<absl::optional<int>>::value); +} + +TEST(optionalTest, nulloptConstructor) { + absl::optional<int> empty(absl::nullopt); + EXPECT_FALSE(empty); + +#ifdef ABSL_HAVE_STD_OPTIONAL + constexpr absl::optional<int> cempty{absl::nullopt}; +#else + // Creating a temporary absl::nullopt_t object instead of using absl::nullopt + // because absl::nullopt cannot be constexpr and have external linkage at the + // same time. + constexpr absl::optional<int> cempty{absl::nullopt_t(absl::nullopt_t::init)}; +#endif + static_assert(!cempty.has_value(), ""); + EXPECT_TRUE((std::is_nothrow_constructible<absl::optional<int>, + absl::nullopt_t>::value)); +} + +TEST(optionalTest, CopyConstructor) { + { + absl::optional<int> empty, opt42 = 42; + absl::optional<int> empty_copy(empty); + EXPECT_FALSE(empty_copy); + absl::optional<int> opt42_copy(opt42); + EXPECT_TRUE(opt42_copy); + EXPECT_EQ(42, *opt42_copy); + } + { + absl::optional<const int> empty, opt42 = 42; + absl::optional<const int> empty_copy(empty); + EXPECT_FALSE(empty_copy); + absl::optional<const int> opt42_copy(opt42); + EXPECT_TRUE(opt42_copy); + EXPECT_EQ(42, *opt42_copy); + } + { + absl::optional<volatile int> empty, opt42 = 42; + absl::optional<volatile int> empty_copy(empty); + EXPECT_FALSE(empty_copy); + absl::optional<volatile int> opt42_copy(opt42); + EXPECT_TRUE(opt42_copy); + EXPECT_EQ(42, *opt42_copy); + } + // test copyablility + EXPECT_TRUE(std::is_copy_constructible<absl::optional<int>>::value); + EXPECT_TRUE(std::is_copy_constructible<absl::optional<Copyable>>::value); + EXPECT_FALSE( + std::is_copy_constructible<absl::optional<MoveableThrow>>::value); + EXPECT_FALSE( + std::is_copy_constructible<absl::optional<MoveableNoThrow>>::value); + EXPECT_FALSE(std::is_copy_constructible<absl::optional<NonMovable>>::value); + + EXPECT_FALSE( + absl::is_trivially_copy_constructible<absl::optional<Copyable>>::value); +#if defined(ABSL_HAVE_STD_OPTIONAL) && defined(__GLIBCXX__) + // libstdc++ std::optional implementation (as of 7.2) has a bug: when T is + // trivially copyable, optional<T> is not trivially copyable (due to one of + // its base class is unconditionally nontrivial). +#define ABSL_GLIBCXX_OPTIONAL_TRIVIALITY_BUG 1 +#endif +#ifndef ABSL_GLIBCXX_OPTIONAL_TRIVIALITY_BUG + EXPECT_TRUE( + absl::is_trivially_copy_constructible<absl::optional<int>>::value); + EXPECT_TRUE( + absl::is_trivially_copy_constructible<absl::optional<const int>>::value); +#ifndef _MSC_VER + // See defect report "Trivial copy/move constructor for class with volatile + // member" at + // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#2094 + // A class with non-static data member of volatile-qualified type should still + // have a trivial copy constructor if the data member is trivial. + // Also a cv-qualified scalar type should be trivially copyable. + EXPECT_TRUE(absl::is_trivially_copy_constructible< + absl::optional<volatile int>>::value); +#endif // _MSC_VER +#endif // ABSL_GLIBCXX_OPTIONAL_TRIVIALITY_BUG + + // constexpr copy constructor for trivially copyable types + { + constexpr absl::optional<int> o1; + constexpr absl::optional<int> o2 = o1; + static_assert(!o2, ""); + } + { + constexpr absl::optional<int> o1 = 42; + constexpr absl::optional<int> o2 = o1; + static_assert(o2, ""); + static_assert(*o2 == 42, ""); + } + { + struct TrivialCopyable { + constexpr TrivialCopyable() : x(0) {} + constexpr explicit TrivialCopyable(int i) : x(i) {} + int x; + }; + constexpr absl::optional<TrivialCopyable> o1(42); + constexpr absl::optional<TrivialCopyable> o2 = o1; + static_assert(o2, ""); + static_assert(o2->x == 42, ""); +#ifndef ABSL_GLIBCXX_OPTIONAL_TRIVIALITY_BUG + EXPECT_TRUE(absl::is_trivially_copy_constructible< + absl::optional<TrivialCopyable>>::value); + EXPECT_TRUE(absl::is_trivially_copy_constructible< + absl::optional<const TrivialCopyable>>::value); +#endif + // When testing with VS 2017 15.3, there seems to be a bug in MSVC + // std::optional when T is volatile-qualified. So skipping this test. + // Bug report: + // https://connect.microsoft.com/VisualStudio/feedback/details/3142534 +#if defined(ABSL_HAVE_STD_OPTIONAL) && defined(_MSC_VER) && _MSC_VER >= 1911 +#define ABSL_MSVC_OPTIONAL_VOLATILE_COPY_BUG 1 +#endif +#ifndef ABSL_MSVC_OPTIONAL_VOLATILE_COPY_BUG + EXPECT_FALSE(std::is_copy_constructible< + absl::optional<volatile TrivialCopyable>>::value); +#endif + } +} + +TEST(optionalTest, MoveConstructor) { + absl::optional<int> empty, opt42 = 42; + absl::optional<int> empty_move(std::move(empty)); + EXPECT_FALSE(empty_move); + absl::optional<int> opt42_move(std::move(opt42)); + EXPECT_TRUE(opt42_move); + EXPECT_EQ(42, opt42_move); + // test movability + EXPECT_TRUE(std::is_move_constructible<absl::optional<int>>::value); + EXPECT_TRUE(std::is_move_constructible<absl::optional<Copyable>>::value); + EXPECT_TRUE(std::is_move_constructible<absl::optional<MoveableThrow>>::value); + EXPECT_TRUE( + std::is_move_constructible<absl::optional<MoveableNoThrow>>::value); + EXPECT_FALSE(std::is_move_constructible<absl::optional<NonMovable>>::value); + // test noexcept + EXPECT_TRUE(std::is_nothrow_move_constructible<absl::optional<int>>::value); +#ifndef ABSL_HAVE_STD_OPTIONAL + EXPECT_EQ( + absl::default_allocator_is_nothrow::value, + std::is_nothrow_move_constructible<absl::optional<MoveableThrow>>::value); +#endif + EXPECT_TRUE(std::is_nothrow_move_constructible< + absl::optional<MoveableNoThrow>>::value); +} + +TEST(optionalTest, Destructor) { + struct Trivial {}; + + struct NonTrivial { + NonTrivial(const NonTrivial&) {} + NonTrivial& operator=(const NonTrivial&) { return *this; } + ~NonTrivial() {} + }; + + EXPECT_TRUE(std::is_trivially_destructible<absl::optional<int>>::value); + EXPECT_TRUE(std::is_trivially_destructible<absl::optional<Trivial>>::value); + EXPECT_FALSE( + std::is_trivially_destructible<absl::optional<NonTrivial>>::value); +} + +TEST(optionalTest, InPlaceConstructor) { + constexpr absl::optional<ConstexprType> opt0{absl::in_place_t()}; + static_assert(opt0, ""); + static_assert(opt0->x == ConstexprType::kCtorDefault, ""); + constexpr absl::optional<ConstexprType> opt1{absl::in_place_t(), 1}; + static_assert(opt1, ""); + static_assert(opt1->x == ConstexprType::kCtorInt, ""); +#ifndef ABSL_HAVE_NO_CONSTEXPR_INITIALIZER_LIST + constexpr absl::optional<ConstexprType> opt2{absl::in_place_t(), {1, 2}}; + static_assert(opt2, ""); + static_assert(opt2->x == ConstexprType::kCtorInitializerList, ""); +#endif + + // TODO(absl-team): uncomment these when std::is_constructible<T, Args&&...> + // SFINAE is added to optional::optional(absl::in_place_t, Args&&...). + // struct I { + // I(absl::in_place_t); + // }; + + // EXPECT_FALSE((std::is_constructible<absl::optional<I>, + // absl::in_place_t>::value)); + // EXPECT_FALSE((std::is_constructible<absl::optional<I>, const + // absl::in_place_t&>::value)); +} + +// template<U=T> optional(U&&); +TEST(optionalTest, ValueConstructor) { + constexpr absl::optional<int> opt0(0); + static_assert(opt0, ""); + static_assert(*opt0 == 0, ""); + EXPECT_TRUE((std::is_convertible<int, absl::optional<int>>::value)); + // Copy initialization ( = "abc") won't work due to optional(optional&&) + // is not constexpr. Use list initialization instead. This invokes + // absl::optional<ConstexprType>::absl::optional<U>(U&&), with U = const char + // (&) [4], which direct-initializes the ConstexprType value held by the + // optional via ConstexprType::ConstexprType(const char*). + constexpr absl::optional<ConstexprType> opt1 = {"abc"}; + static_assert(opt1, ""); + static_assert(ConstexprType::kCtorConstChar == opt1->x, ""); + EXPECT_TRUE( + (std::is_convertible<const char*, absl::optional<ConstexprType>>::value)); + // direct initialization + constexpr absl::optional<ConstexprType> opt2{2}; + static_assert(opt2, ""); + static_assert(ConstexprType::kCtorInt == opt2->x, ""); + EXPECT_FALSE( + (std::is_convertible<int, absl::optional<ConstexprType>>::value)); + + // this invokes absl::optional<int>::optional(int&&) + // NOTE: this has different behavior than assignment, e.g. + // "opt3 = {};" clears the optional rather than setting the value to 0 + // According to C++17 standard N4659 [over.ics.list] 16.3.3.1.5, (9.2)- "if + // the initializer list has no elements, the implicit conversion is the + // identity conversion", so `optional(int&&)` should be a better match than + // `optional(optional&&)` which is a user-defined conversion. + // Note: GCC 7 has a bug with this overload selection when compiled with + // `-std=c++17`. +#if defined(__GNUC__) && !defined(__clang__) && __GNUC__ == 7 && \ + __cplusplus == 201703L +#define ABSL_GCC7_OVER_ICS_LIST_BUG 1 +#endif +#ifndef ABSL_GCC7_OVER_ICS_LIST_BUG + constexpr absl::optional<int> opt3({}); + static_assert(opt3, ""); + static_assert(*opt3 == 0, ""); +#endif + + // this invokes the move constructor with a default constructed optional + // because non-template function is a better match than template function. + absl::optional<ConstexprType> opt4({}); + EXPECT_FALSE(opt4); +} + +struct Implicit {}; + +struct Explicit {}; + +struct Convert { + Convert(const Implicit&) // NOLINT(runtime/explicit) + : implicit(true), move(false) {} + Convert(Implicit&&) // NOLINT(runtime/explicit) + : implicit(true), move(true) {} + explicit Convert(const Explicit&) : implicit(false), move(false) {} + explicit Convert(Explicit&&) : implicit(false), move(true) {} + + bool implicit; + bool move; +}; + +struct ConvertFromOptional { + ConvertFromOptional(const Implicit&) // NOLINT(runtime/explicit) + : implicit(true), move(false), from_optional(false) {} + ConvertFromOptional(Implicit&&) // NOLINT(runtime/explicit) + : implicit(true), move(true), from_optional(false) {} + ConvertFromOptional( + const absl::optional<Implicit>&) // NOLINT(runtime/explicit) + : implicit(true), move(false), from_optional(true) {} + ConvertFromOptional(absl::optional<Implicit>&&) // NOLINT(runtime/explicit) + : implicit(true), move(true), from_optional(true) {} + explicit ConvertFromOptional(const Explicit&) + : implicit(false), move(false), from_optional(false) {} + explicit ConvertFromOptional(Explicit&&) + : implicit(false), move(true), from_optional(false) {} + explicit ConvertFromOptional(const absl::optional<Explicit>&) + : implicit(false), move(false), from_optional(true) {} + explicit ConvertFromOptional(absl::optional<Explicit>&&) + : implicit(false), move(true), from_optional(true) {} + + bool implicit; + bool move; + bool from_optional; +}; + +TEST(optionalTest, ConvertingConstructor) { + absl::optional<Implicit> i_empty; + absl::optional<Implicit> i(absl::in_place); + absl::optional<Explicit> e_empty; + absl::optional<Explicit> e(absl::in_place); + { + // implicitly constructing absl::optional<Convert> from + // absl::optional<Implicit> + absl::optional<Convert> empty = i_empty; + EXPECT_FALSE(empty); + absl::optional<Convert> opt_copy = i; + EXPECT_TRUE(opt_copy); + EXPECT_TRUE(opt_copy->implicit); + EXPECT_FALSE(opt_copy->move); + absl::optional<Convert> opt_move = absl::optional<Implicit>(absl::in_place); + EXPECT_TRUE(opt_move); + EXPECT_TRUE(opt_move->implicit); + EXPECT_TRUE(opt_move->move); + } + { + // explicitly constructing absl::optional<Convert> from + // absl::optional<Explicit> + absl::optional<Convert> empty(e_empty); + EXPECT_FALSE(empty); + absl::optional<Convert> opt_copy(e); + EXPECT_TRUE(opt_copy); + EXPECT_FALSE(opt_copy->implicit); + EXPECT_FALSE(opt_copy->move); + EXPECT_FALSE((std::is_convertible<const absl::optional<Explicit>&, + absl::optional<Convert>>::value)); + absl::optional<Convert> opt_move{absl::optional<Explicit>(absl::in_place)}; + EXPECT_TRUE(opt_move); + EXPECT_FALSE(opt_move->implicit); + EXPECT_TRUE(opt_move->move); + EXPECT_FALSE((std::is_convertible<absl::optional<Explicit>&&, + absl::optional<Convert>>::value)); + } + { + // implicitly constructing absl::optional<ConvertFromOptional> from + // absl::optional<Implicit> via + // ConvertFromOptional(absl::optional<Implicit>&&) check that + // ConvertFromOptional(Implicit&&) is NOT called + static_assert( + std::is_convertible<absl::optional<Implicit>, + absl::optional<ConvertFromOptional>>::value, + ""); + absl::optional<ConvertFromOptional> opt0 = i_empty; + EXPECT_TRUE(opt0); + EXPECT_TRUE(opt0->implicit); + EXPECT_FALSE(opt0->move); + EXPECT_TRUE(opt0->from_optional); + absl::optional<ConvertFromOptional> opt1 = absl::optional<Implicit>(); + EXPECT_TRUE(opt1); + EXPECT_TRUE(opt1->implicit); + EXPECT_TRUE(opt1->move); + EXPECT_TRUE(opt1->from_optional); + } + { + // implicitly constructing absl::optional<ConvertFromOptional> from + // absl::optional<Explicit> via + // ConvertFromOptional(absl::optional<Explicit>&&) check that + // ConvertFromOptional(Explicit&&) is NOT called + absl::optional<ConvertFromOptional> opt0(e_empty); + EXPECT_TRUE(opt0); + EXPECT_FALSE(opt0->implicit); + EXPECT_FALSE(opt0->move); + EXPECT_TRUE(opt0->from_optional); + EXPECT_FALSE( + (std::is_convertible<const absl::optional<Explicit>&, + absl::optional<ConvertFromOptional>>::value)); + absl::optional<ConvertFromOptional> opt1{absl::optional<Explicit>()}; + EXPECT_TRUE(opt1); + EXPECT_FALSE(opt1->implicit); + EXPECT_TRUE(opt1->move); + EXPECT_TRUE(opt1->from_optional); + EXPECT_FALSE( + (std::is_convertible<absl::optional<Explicit>&&, + absl::optional<ConvertFromOptional>>::value)); + } +} + +TEST(optionalTest, StructorBasic) { + StructorListener listener; + Listenable::listener = &listener; + { + absl::optional<Listenable> empty; + EXPECT_FALSE(empty); + absl::optional<Listenable> opt0(absl::in_place); + EXPECT_TRUE(opt0); + absl::optional<Listenable> opt1(absl::in_place, 1); + EXPECT_TRUE(opt1); + absl::optional<Listenable> opt2(absl::in_place, 1, 2); + EXPECT_TRUE(opt2); + } + EXPECT_EQ(1, listener.construct0); + EXPECT_EQ(1, listener.construct1); + EXPECT_EQ(1, listener.construct2); + EXPECT_EQ(3, listener.destruct); +} + +TEST(optionalTest, CopyMoveStructor) { + StructorListener listener; + Listenable::listener = &listener; + absl::optional<Listenable> original(absl::in_place); + EXPECT_EQ(1, listener.construct0); + EXPECT_EQ(0, listener.copy); + EXPECT_EQ(0, listener.move); + absl::optional<Listenable> copy(original); + EXPECT_EQ(1, listener.construct0); + EXPECT_EQ(1, listener.copy); + EXPECT_EQ(0, listener.move); + absl::optional<Listenable> move(std::move(original)); + EXPECT_EQ(1, listener.construct0); + EXPECT_EQ(1, listener.copy); + EXPECT_EQ(1, listener.move); +} + +TEST(optionalTest, ListInit) { + StructorListener listener; + Listenable::listener = &listener; + absl::optional<Listenable> listinit1(absl::in_place, {1}); + absl::optional<Listenable> listinit2(absl::in_place, {1, 2}); + EXPECT_EQ(2, listener.listinit); +} + +TEST(optionalTest, AssignFromNullopt) { + absl::optional<int> opt(1); + opt = absl::nullopt; + EXPECT_FALSE(opt); + + StructorListener listener; + Listenable::listener = &listener; + absl::optional<Listenable> opt1(absl::in_place); + opt1 = absl::nullopt; + EXPECT_FALSE(opt1); + EXPECT_EQ(1, listener.construct0); + EXPECT_EQ(1, listener.destruct); + + EXPECT_TRUE(( + std::is_nothrow_assignable<absl::optional<int>, absl::nullopt_t>::value)); + EXPECT_TRUE((std::is_nothrow_assignable<absl::optional<Listenable>, + absl::nullopt_t>::value)); +} + +TEST(optionalTest, CopyAssignment) { + const absl::optional<int> empty, opt1 = 1, opt2 = 2; + absl::optional<int> empty_to_opt1, opt1_to_opt2, opt2_to_empty; + + EXPECT_FALSE(empty_to_opt1); + empty_to_opt1 = empty; + EXPECT_FALSE(empty_to_opt1); + empty_to_opt1 = opt1; + EXPECT_TRUE(empty_to_opt1); + EXPECT_EQ(1, empty_to_opt1.value()); + + EXPECT_FALSE(opt1_to_opt2); + opt1_to_opt2 = opt1; + EXPECT_TRUE(opt1_to_opt2); + EXPECT_EQ(1, opt1_to_opt2.value()); + opt1_to_opt2 = opt2; + EXPECT_TRUE(opt1_to_opt2); + EXPECT_EQ(2, opt1_to_opt2.value()); + + EXPECT_FALSE(opt2_to_empty); + opt2_to_empty = opt2; + EXPECT_TRUE(opt2_to_empty); + EXPECT_EQ(2, opt2_to_empty.value()); + opt2_to_empty = empty; + EXPECT_FALSE(opt2_to_empty); + + EXPECT_FALSE(std::is_copy_assignable<absl::optional<const int>>::value); + EXPECT_TRUE(std::is_copy_assignable<absl::optional<Copyable>>::value); + EXPECT_FALSE(std::is_copy_assignable<absl::optional<MoveableThrow>>::value); + EXPECT_FALSE(std::is_copy_assignable<absl::optional<MoveableNoThrow>>::value); + EXPECT_FALSE(std::is_copy_assignable<absl::optional<NonMovable>>::value); + + EXPECT_TRUE(absl::is_trivially_copy_assignable<int>::value); + EXPECT_TRUE(absl::is_trivially_copy_assignable<volatile int>::value); + + struct Trivial { + int i; + }; + struct NonTrivial { + NonTrivial& operator=(const NonTrivial&) { return *this; } + int i; + }; + + EXPECT_TRUE(absl::is_trivially_copy_assignable<Trivial>::value); + EXPECT_FALSE(std::is_copy_assignable<const Trivial>::value); + EXPECT_FALSE(std::is_copy_assignable<volatile Trivial>::value); + EXPECT_TRUE(std::is_copy_assignable<NonTrivial>::value); + EXPECT_FALSE(absl::is_trivially_copy_assignable<NonTrivial>::value); + + // std::optional doesn't support volatile nontrivial types. +#ifndef ABSL_HAVE_STD_OPTIONAL + { + StructorListener listener; + Listenable::listener = &listener; + + absl::optional<volatile Listenable> empty, set(absl::in_place); + EXPECT_EQ(1, listener.construct0); + absl::optional<volatile Listenable> empty_to_empty, empty_to_set, + set_to_empty(absl::in_place), set_to_set(absl::in_place); + EXPECT_EQ(3, listener.construct0); + empty_to_empty = empty; // no effect + empty_to_set = set; // copy construct + set_to_empty = empty; // destruct + set_to_set = set; // copy assign + EXPECT_EQ(1, listener.volatile_copy); + EXPECT_EQ(0, listener.volatile_move); + EXPECT_EQ(1, listener.destruct); + EXPECT_EQ(1, listener.volatile_copy_assign); + } +#endif // ABSL_HAVE_STD_OPTIONAL +} + +TEST(optionalTest, MoveAssignment) { + { + StructorListener listener; + Listenable::listener = &listener; + + absl::optional<Listenable> empty1, empty2, set1(absl::in_place), + set2(absl::in_place); + EXPECT_EQ(2, listener.construct0); + absl::optional<Listenable> empty_to_empty, empty_to_set, + set_to_empty(absl::in_place), set_to_set(absl::in_place); + EXPECT_EQ(4, listener.construct0); + empty_to_empty = std::move(empty1); + empty_to_set = std::move(set1); + set_to_empty = std::move(empty2); + set_to_set = std::move(set2); + EXPECT_EQ(0, listener.copy); + EXPECT_EQ(1, listener.move); + EXPECT_EQ(1, listener.destruct); + EXPECT_EQ(1, listener.move_assign); + } + // std::optional doesn't support volatile nontrivial types. +#ifndef ABSL_HAVE_STD_OPTIONAL + { + StructorListener listener; + Listenable::listener = &listener; + + absl::optional<volatile Listenable> empty1, empty2, set1(absl::in_place), + set2(absl::in_place); + EXPECT_EQ(2, listener.construct0); + absl::optional<volatile Listenable> empty_to_empty, empty_to_set, + set_to_empty(absl::in_place), set_to_set(absl::in_place); + EXPECT_EQ(4, listener.construct0); + empty_to_empty = std::move(empty1); // no effect + empty_to_set = std::move(set1); // move construct + set_to_empty = std::move(empty2); // destruct + set_to_set = std::move(set2); // move assign + EXPECT_EQ(0, listener.volatile_copy); + EXPECT_EQ(1, listener.volatile_move); + EXPECT_EQ(1, listener.destruct); + EXPECT_EQ(1, listener.volatile_move_assign); + } +#endif // ABSL_HAVE_STD_OPTIONAL + EXPECT_FALSE(std::is_move_assignable<absl::optional<const int>>::value); + EXPECT_TRUE(std::is_move_assignable<absl::optional<Copyable>>::value); + EXPECT_TRUE(std::is_move_assignable<absl::optional<MoveableThrow>>::value); + EXPECT_TRUE(std::is_move_assignable<absl::optional<MoveableNoThrow>>::value); + EXPECT_FALSE(std::is_move_assignable<absl::optional<NonMovable>>::value); + + EXPECT_FALSE( + std::is_nothrow_move_assignable<absl::optional<MoveableThrow>>::value); + EXPECT_TRUE( + std::is_nothrow_move_assignable<absl::optional<MoveableNoThrow>>::value); +} + +struct NoConvertToOptional { + // disable implicit conversion from const NoConvertToOptional& + // to absl::optional<NoConvertToOptional>. + NoConvertToOptional(const NoConvertToOptional&) = delete; +}; + +struct CopyConvert { + CopyConvert(const NoConvertToOptional&); + CopyConvert& operator=(const CopyConvert&) = delete; + CopyConvert& operator=(const NoConvertToOptional&); +}; + +struct CopyConvertFromOptional { + CopyConvertFromOptional(const NoConvertToOptional&); + CopyConvertFromOptional(const absl::optional<NoConvertToOptional>&); + CopyConvertFromOptional& operator=(const CopyConvertFromOptional&) = delete; + CopyConvertFromOptional& operator=(const NoConvertToOptional&); + CopyConvertFromOptional& operator=( + const absl::optional<NoConvertToOptional>&); +}; + +struct MoveConvert { + MoveConvert(NoConvertToOptional&&); + MoveConvert& operator=(const MoveConvert&) = delete; + MoveConvert& operator=(NoConvertToOptional&&); +}; + +struct MoveConvertFromOptional { + MoveConvertFromOptional(NoConvertToOptional&&); + MoveConvertFromOptional(absl::optional<NoConvertToOptional>&&); + MoveConvertFromOptional& operator=(const MoveConvertFromOptional&) = delete; + MoveConvertFromOptional& operator=(NoConvertToOptional&&); + MoveConvertFromOptional& operator=(absl::optional<NoConvertToOptional>&&); +}; + +// template <typename U = T> absl::optional<T>& operator=(U&& v); +TEST(optionalTest, ValueAssignment) { + absl::optional<int> opt; + EXPECT_FALSE(opt); + opt = 42; + EXPECT_TRUE(opt); + EXPECT_EQ(42, opt.value()); + opt = absl::nullopt; + EXPECT_FALSE(opt); + opt = 42; + EXPECT_TRUE(opt); + EXPECT_EQ(42, opt.value()); + opt = 43; + EXPECT_TRUE(opt); + EXPECT_EQ(43, opt.value()); + opt = {}; // this should clear optional + EXPECT_FALSE(opt); + + opt = {44}; + EXPECT_TRUE(opt); + EXPECT_EQ(44, opt.value()); + + // U = const NoConvertToOptional& + EXPECT_TRUE((std::is_assignable<absl::optional<CopyConvert>&, + const NoConvertToOptional&>::value)); + // U = const absl::optional<NoConvertToOptional>& + EXPECT_TRUE((std::is_assignable<absl::optional<CopyConvertFromOptional>&, + const NoConvertToOptional&>::value)); + // U = const NoConvertToOptional& triggers SFINAE because + // std::is_constructible_v<MoveConvert, const NoConvertToOptional&> is false + EXPECT_FALSE((std::is_assignable<absl::optional<MoveConvert>&, + const NoConvertToOptional&>::value)); + // U = NoConvertToOptional + EXPECT_TRUE((std::is_assignable<absl::optional<MoveConvert>&, + NoConvertToOptional&&>::value)); + // U = const NoConvertToOptional& triggers SFINAE because + // std::is_constructible_v<MoveConvertFromOptional, const + // NoConvertToOptional&> is false + EXPECT_FALSE((std::is_assignable<absl::optional<MoveConvertFromOptional>&, + const NoConvertToOptional&>::value)); + // U = NoConvertToOptional + EXPECT_TRUE((std::is_assignable<absl::optional<MoveConvertFromOptional>&, + NoConvertToOptional&&>::value)); + // U = const absl::optional<NoConvertToOptional>& + EXPECT_TRUE( + (std::is_assignable<absl::optional<CopyConvertFromOptional>&, + const absl::optional<NoConvertToOptional>&>::value)); + // U = absl::optional<NoConvertToOptional> + EXPECT_TRUE( + (std::is_assignable<absl::optional<MoveConvertFromOptional>&, + absl::optional<NoConvertToOptional>&&>::value)); +} + +// template <typename U> absl::optional<T>& operator=(const absl::optional<U>& +// rhs); template <typename U> absl::optional<T>& operator=(absl::optional<U>&& +// rhs); +TEST(optionalTest, ConvertingAssignment) { + absl::optional<int> opt_i; + absl::optional<char> opt_c('c'); + opt_i = opt_c; + EXPECT_TRUE(opt_i); + EXPECT_EQ(*opt_c, *opt_i); + opt_i = absl::optional<char>(); + EXPECT_FALSE(opt_i); + opt_i = absl::optional<char>('d'); + EXPECT_TRUE(opt_i); + EXPECT_EQ('d', *opt_i); + + absl::optional<std::string> opt_str; + absl::optional<const char*> opt_cstr("abc"); + opt_str = opt_cstr; + EXPECT_TRUE(opt_str); + EXPECT_EQ(std::string("abc"), *opt_str); + opt_str = absl::optional<const char*>(); + EXPECT_FALSE(opt_str); + opt_str = absl::optional<const char*>("def"); + EXPECT_TRUE(opt_str); + EXPECT_EQ(std::string("def"), *opt_str); + + // operator=(const absl::optional<U>&) with U = NoConvertToOptional + EXPECT_TRUE( + (std::is_assignable<absl::optional<CopyConvert>, + const absl::optional<NoConvertToOptional>&>::value)); + // operator=(const absl::optional<U>&) with U = NoConvertToOptional + // triggers SFINAE because + // std::is_constructible_v<MoveConvert, const NoConvertToOptional&> is false + EXPECT_FALSE( + (std::is_assignable<absl::optional<MoveConvert>&, + const absl::optional<NoConvertToOptional>&>::value)); + // operator=(absl::optional<U>&&) with U = NoConvertToOptional + EXPECT_TRUE( + (std::is_assignable<absl::optional<MoveConvert>&, + absl::optional<NoConvertToOptional>&&>::value)); + // operator=(const absl::optional<U>&) with U = NoConvertToOptional triggers + // SFINAE because std::is_constructible_v<MoveConvertFromOptional, const + // NoConvertToOptional&> is false. operator=(U&&) with U = const + // absl::optional<NoConverToOptional>& triggers SFINAE because + // std::is_constructible<MoveConvertFromOptional, + // absl::optional<NoConvertToOptional>&&> is true. + EXPECT_FALSE( + (std::is_assignable<absl::optional<MoveConvertFromOptional>&, + const absl::optional<NoConvertToOptional>&>::value)); +} + +TEST(optionalTest, ResetAndHasValue) { + StructorListener listener; + Listenable::listener = &listener; + absl::optional<Listenable> opt; + EXPECT_FALSE(opt); + EXPECT_FALSE(opt.has_value()); + opt.emplace(); + EXPECT_TRUE(opt); + EXPECT_TRUE(opt.has_value()); + opt.reset(); + EXPECT_FALSE(opt); + EXPECT_FALSE(opt.has_value()); + EXPECT_EQ(1, listener.destruct); + opt.reset(); + EXPECT_FALSE(opt); + EXPECT_FALSE(opt.has_value()); + + constexpr absl::optional<int> empty; + static_assert(!empty.has_value(), ""); + constexpr absl::optional<int> nonempty(1); + static_assert(nonempty.has_value(), ""); +} + +TEST(optionalTest, Emplace) { + StructorListener listener; + Listenable::listener = &listener; + absl::optional<Listenable> opt; + EXPECT_FALSE(opt); + opt.emplace(1); + EXPECT_TRUE(opt); + opt.emplace(1, 2); + EXPECT_EQ(1, listener.construct1); + EXPECT_EQ(1, listener.construct2); + EXPECT_EQ(1, listener.destruct); + + absl::optional<std::string> o; + EXPECT_TRUE((std::is_same<std::string&, decltype(o.emplace("abc"))>::value)); + std::string& ref = o.emplace("abc"); + EXPECT_EQ(&ref, &o.value()); +} + +TEST(optionalTest, ListEmplace) { + StructorListener listener; + Listenable::listener = &listener; + absl::optional<Listenable> opt; + EXPECT_FALSE(opt); + opt.emplace({1}); + EXPECT_TRUE(opt); + opt.emplace({1, 2}); + EXPECT_EQ(2, listener.listinit); + EXPECT_EQ(1, listener.destruct); + + absl::optional<Listenable> o; + EXPECT_TRUE((std::is_same<Listenable&, decltype(o.emplace({1}))>::value)); + Listenable& ref = o.emplace({1}); + EXPECT_EQ(&ref, &o.value()); +} + +TEST(optionalTest, Swap) { + absl::optional<int> opt_empty, opt1 = 1, opt2 = 2; + EXPECT_FALSE(opt_empty); + EXPECT_TRUE(opt1); + EXPECT_EQ(1, opt1.value()); + EXPECT_TRUE(opt2); + EXPECT_EQ(2, opt2.value()); + swap(opt_empty, opt1); + EXPECT_FALSE(opt1); + EXPECT_TRUE(opt_empty); + EXPECT_EQ(1, opt_empty.value()); + EXPECT_TRUE(opt2); + EXPECT_EQ(2, opt2.value()); + swap(opt_empty, opt1); + EXPECT_FALSE(opt_empty); + EXPECT_TRUE(opt1); + EXPECT_EQ(1, opt1.value()); + EXPECT_TRUE(opt2); + EXPECT_EQ(2, opt2.value()); + swap(opt1, opt2); + EXPECT_FALSE(opt_empty); + EXPECT_TRUE(opt1); + EXPECT_EQ(2, opt1.value()); + EXPECT_TRUE(opt2); + EXPECT_EQ(1, opt2.value()); + + EXPECT_TRUE(noexcept(opt1.swap(opt2))); + EXPECT_TRUE(noexcept(swap(opt1, opt2))); +} + +TEST(optionalTest, PointerStuff) { + absl::optional<std::string> opt(absl::in_place, "foo"); + EXPECT_EQ("foo", *opt); + const auto& opt_const = opt; + EXPECT_EQ("foo", *opt_const); + EXPECT_EQ(opt->size(), 3); + EXPECT_EQ(opt_const->size(), 3); + + constexpr absl::optional<ConstexprType> opt1(1); + static_assert(opt1->x == ConstexprType::kCtorInt, ""); +} + +// gcc has a bug pre 4.9.1 where it doesn't do correct overload resolution +// when overloads are const-qualified and *this is an raluve. +// Skip that test to make the build green again when using the old compiler. +// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59296 is fixed in 4.9.1. +#if defined(__GNUC__) && !defined(__clang__) +#define GCC_VERSION (__GNUC__ * 10000 \ + + __GNUC_MINOR__ * 100 \ + + __GNUC_PATCHLEVEL__) +#if GCC_VERSION < 40901 +#define ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG +#endif +#endif + +// MSVC has a bug with "cv-qualifiers in class construction", fixed in 2017. See +// https://docs.microsoft.com/en-us/cpp/cpp-conformance-improvements-2017#bug-fixes +// The compiler some incorrectly ingores the cv-qualifier when generating a +// class object via a constructor call. For example: +// +// class optional { +// constexpr T&& value() &&; +// constexpr const T&& value() const &&; +// } +// +// using COI = const absl::optional<int>; +// static_assert(2 == COI(2).value(), ""); // const && +// +// This should invoke the "const &&" overload but since it ignores the const +// qualifier it finds the "&&" overload the best candidate. +#if defined(_MSC_VER) && _MSC_VER < 1910 +#define ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG +#endif + +TEST(optionalTest, Value) { + using O = absl::optional<std::string>; + using CO = const absl::optional<std::string>; + using OC = absl::optional<const std::string>; + O lvalue(absl::in_place, "lvalue"); + CO clvalue(absl::in_place, "clvalue"); + OC lvalue_c(absl::in_place, "lvalue_c"); + EXPECT_EQ("lvalue", lvalue.value()); + EXPECT_EQ("clvalue", clvalue.value()); + EXPECT_EQ("lvalue_c", lvalue_c.value()); + EXPECT_EQ("xvalue", O(absl::in_place, "xvalue").value()); + EXPECT_EQ("xvalue_c", OC(absl::in_place, "xvalue_c").value()); +#ifndef ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG + EXPECT_EQ("cxvalue", CO(absl::in_place, "cxvalue").value()); +#endif + EXPECT_EQ("&", TypeQuals(lvalue.value())); + EXPECT_EQ("c&", TypeQuals(clvalue.value())); + EXPECT_EQ("c&", TypeQuals(lvalue_c.value())); + EXPECT_EQ("&&", TypeQuals(O(absl::in_place, "xvalue").value())); +#if !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG) && \ + !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG) + EXPECT_EQ("c&&", TypeQuals(CO(absl::in_place, "cxvalue").value())); +#endif + EXPECT_EQ("c&&", TypeQuals(OC(absl::in_place, "xvalue_c").value())); + + // test on volatile type + using OV = absl::optional<volatile int>; + OV lvalue_v(absl::in_place, 42); + EXPECT_EQ(42, lvalue_v.value()); + EXPECT_EQ(42, OV(42).value()); + EXPECT_TRUE((std::is_same<volatile int&, decltype(lvalue_v.value())>::value)); + EXPECT_TRUE((std::is_same<volatile int&&, decltype(OV(42).value())>::value)); + + // test exception throw on value() + absl::optional<int> empty; +#ifdef ABSL_HAVE_EXCEPTIONS + EXPECT_THROW(empty.value(), absl::bad_optional_access); +#else + EXPECT_DEATH(empty.value(), "Bad optional access"); +#endif + + // test constexpr value() + constexpr absl::optional<int> o1(1); + static_assert(1 == o1.value(), ""); // const & +#if !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG) && \ + !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG) + using COI = const absl::optional<int>; + static_assert(2 == COI(2).value(), ""); // const && +#endif +} + +TEST(optionalTest, DerefOperator) { + using O = absl::optional<std::string>; + using CO = const absl::optional<std::string>; + using OC = absl::optional<const std::string>; + O lvalue(absl::in_place, "lvalue"); + CO clvalue(absl::in_place, "clvalue"); + OC lvalue_c(absl::in_place, "lvalue_c"); + EXPECT_EQ("lvalue", *lvalue); + EXPECT_EQ("clvalue", *clvalue); + EXPECT_EQ("lvalue_c", *lvalue_c); + EXPECT_EQ("xvalue", *O(absl::in_place, "xvalue")); + EXPECT_EQ("xvalue_c", *OC(absl::in_place, "xvalue_c")); +#ifndef ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG + EXPECT_EQ("cxvalue", *CO(absl::in_place, "cxvalue")); +#endif + EXPECT_EQ("&", TypeQuals(*lvalue)); + EXPECT_EQ("c&", TypeQuals(*clvalue)); + EXPECT_EQ("&&", TypeQuals(*O(absl::in_place, "xvalue"))); +#if !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG) && \ + !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG) + EXPECT_EQ("c&&", TypeQuals(*CO(absl::in_place, "cxvalue"))); +#endif + EXPECT_EQ("c&&", TypeQuals(*OC(absl::in_place, "xvalue_c"))); + + // test on volatile type + using OV = absl::optional<volatile int>; + OV lvalue_v(absl::in_place, 42); + EXPECT_EQ(42, *lvalue_v); + EXPECT_EQ(42, *OV(42)); + EXPECT_TRUE((std::is_same<volatile int&, decltype(*lvalue_v)>::value)); + EXPECT_TRUE((std::is_same<volatile int&&, decltype(*OV(42))>::value)); + + constexpr absl::optional<int> opt1(1); + static_assert(*opt1 == 1, ""); +#if !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG) && \ + !defined(ABSL_SKIP_OVERLOAD_TEST_DUE_TO_GCC_BUG) + using COI = const absl::optional<int>; + static_assert(*COI(2) == 2, ""); +#endif +} + +TEST(optionalTest, ValueOr) { + absl::optional<double> opt_empty, opt_set = 1.2; + EXPECT_EQ(42.0, opt_empty.value_or(42)); + EXPECT_EQ(1.2, opt_set.value_or(42)); + EXPECT_EQ(42.0, absl::optional<double>().value_or(42)); + EXPECT_EQ(1.2, absl::optional<double>(1.2).value_or(42)); + + constexpr absl::optional<double> copt_empty, copt_set = {1.2}; + static_assert(42.0 == copt_empty.value_or(42), ""); + static_assert(1.2 == copt_set.value_or(42), ""); +#ifndef ABSL_SKIP_OVERLOAD_TEST_DUE_TO_MSVC_BUG + using COD = const absl::optional<double>; + static_assert(42.0 == COD().value_or(42), ""); + static_assert(1.2 == COD(1.2).value_or(42), ""); +#endif +} + +// make_optional cannot be constexpr until C++17 +TEST(optionalTest, make_optional) { + auto opt_int = absl::make_optional(42); + EXPECT_TRUE((std::is_same<decltype(opt_int), absl::optional<int>>::value)); + EXPECT_EQ(42, opt_int); + + StructorListener listener; + Listenable::listener = &listener; + + absl::optional<Listenable> opt0 = absl::make_optional<Listenable>(); + EXPECT_EQ(1, listener.construct0); + absl::optional<Listenable> opt1 = absl::make_optional<Listenable>(1); + EXPECT_EQ(1, listener.construct1); + absl::optional<Listenable> opt2 = absl::make_optional<Listenable>(1, 2); + EXPECT_EQ(1, listener.construct2); + absl::optional<Listenable> opt3 = absl::make_optional<Listenable>({1}); + absl::optional<Listenable> opt4 = absl::make_optional<Listenable>({1, 2}); + EXPECT_EQ(2, listener.listinit); + + // Constexpr tests on trivially copyable types + // optional<T> has trivial copy/move ctors when T is trivially copyable. + // For nontrivial types with constexpr constructors, we need copy elision in + // C++17 for make_optional to be constexpr. + { + constexpr absl::optional<int> c_opt = absl::make_optional(42); + static_assert(c_opt.value() == 42, ""); + } + { + struct TrivialCopyable { + constexpr TrivialCopyable() : x(0) {} + constexpr explicit TrivialCopyable(int i) : x(i) {} + int x; + }; + + constexpr TrivialCopyable v; + constexpr absl::optional<TrivialCopyable> c_opt0 = absl::make_optional(v); + static_assert(c_opt0->x == 0, ""); + constexpr absl::optional<TrivialCopyable> c_opt1 = + absl::make_optional<TrivialCopyable>(); + static_assert(c_opt1->x == 0, ""); + constexpr absl::optional<TrivialCopyable> c_opt2 = + absl::make_optional<TrivialCopyable>(42); + static_assert(c_opt2->x == 42, ""); + } +} + +template <typename T, typename U> +void optionalTest_Comparisons_EXPECT_LESS(T x, U y) { + EXPECT_FALSE(x == y); + EXPECT_TRUE(x != y); + EXPECT_TRUE(x < y); + EXPECT_FALSE(x > y); + EXPECT_TRUE(x <= y); + EXPECT_FALSE(x >= y); +} + +template <typename T, typename U> +void optionalTest_Comparisons_EXPECT_SAME(T x, U y) { + EXPECT_TRUE(x == y); + EXPECT_FALSE(x != y); + EXPECT_FALSE(x < y); + EXPECT_FALSE(x > y); + EXPECT_TRUE(x <= y); + EXPECT_TRUE(x >= y); +} + +template <typename T, typename U> +void optionalTest_Comparisons_EXPECT_GREATER(T x, U y) { + EXPECT_FALSE(x == y); + EXPECT_TRUE(x != y); + EXPECT_FALSE(x < y); + EXPECT_TRUE(x > y); + EXPECT_FALSE(x <= y); + EXPECT_TRUE(x >= y); +} + + +template <typename T, typename U, typename V> +void TestComparisons() { + absl::optional<T> ae, a2{2}, a4{4}; + absl::optional<U> be, b2{2}, b4{4}; + V v3 = 3; + + // LHS: absl::nullopt, ae, a2, v3, a4 + // RHS: absl::nullopt, be, b2, v3, b4 + + // optionalTest_Comparisons_EXPECT_NOT_TO_WORK(absl::nullopt,absl::nullopt); + optionalTest_Comparisons_EXPECT_SAME(absl::nullopt, be); + optionalTest_Comparisons_EXPECT_LESS(absl::nullopt, b2); + // optionalTest_Comparisons_EXPECT_NOT_TO_WORK(absl::nullopt,v3); + optionalTest_Comparisons_EXPECT_LESS(absl::nullopt, b4); + + optionalTest_Comparisons_EXPECT_SAME(ae, absl::nullopt); + optionalTest_Comparisons_EXPECT_SAME(ae, be); + optionalTest_Comparisons_EXPECT_LESS(ae, b2); + optionalTest_Comparisons_EXPECT_LESS(ae, v3); + optionalTest_Comparisons_EXPECT_LESS(ae, b4); + + optionalTest_Comparisons_EXPECT_GREATER(a2, absl::nullopt); + optionalTest_Comparisons_EXPECT_GREATER(a2, be); + optionalTest_Comparisons_EXPECT_SAME(a2, b2); + optionalTest_Comparisons_EXPECT_LESS(a2, v3); + optionalTest_Comparisons_EXPECT_LESS(a2, b4); + + // optionalTest_Comparisons_EXPECT_NOT_TO_WORK(v3,absl::nullopt); + optionalTest_Comparisons_EXPECT_GREATER(v3, be); + optionalTest_Comparisons_EXPECT_GREATER(v3, b2); + optionalTest_Comparisons_EXPECT_SAME(v3, v3); + optionalTest_Comparisons_EXPECT_LESS(v3, b4); + + optionalTest_Comparisons_EXPECT_GREATER(a4, absl::nullopt); + optionalTest_Comparisons_EXPECT_GREATER(a4, be); + optionalTest_Comparisons_EXPECT_GREATER(a4, b2); + optionalTest_Comparisons_EXPECT_GREATER(a4, v3); + optionalTest_Comparisons_EXPECT_SAME(a4, b4); +} + +struct Int1 { + Int1() = default; + Int1(int i) : i(i) {} // NOLINT(runtime/explicit) + int i; +}; + +struct Int2 { + Int2() = default; + Int2(int i) : i(i) {} // NOLINT(runtime/explicit) + int i; +}; + +// comparison between Int1 and Int2 +constexpr bool operator==(const Int1& lhs, const Int2& rhs) { + return lhs.i == rhs.i; +} +constexpr bool operator!=(const Int1& lhs, const Int2& rhs) { + return !(lhs == rhs); +} +constexpr bool operator<(const Int1& lhs, const Int2& rhs) { + return lhs.i < rhs.i; +} +constexpr bool operator<=(const Int1& lhs, const Int2& rhs) { + return lhs < rhs || lhs == rhs; +} +constexpr bool operator>(const Int1& lhs, const Int2& rhs) { + return !(lhs <= rhs); +} +constexpr bool operator>=(const Int1& lhs, const Int2& rhs) { + return !(lhs < rhs); +} + +TEST(optionalTest, Comparisons) { + TestComparisons<int, int, int>(); + TestComparisons<const int, int, int>(); + TestComparisons<Int1, int, int>(); + TestComparisons<int, Int2, int>(); + TestComparisons<Int1, Int2, int>(); + + // compare absl::optional<std::string> with const char* + absl::optional<std::string> opt_str = "abc"; + const char* cstr = "abc"; + EXPECT_TRUE(opt_str == cstr); + // compare absl::optional<std::string> with absl::optional<const char*> + absl::optional<const char*> opt_cstr = cstr; + EXPECT_TRUE(opt_str == opt_cstr); + // compare absl::optional<std::string> with absl::optional<absl::string_view> + absl::optional<absl::string_view> e1; + absl::optional<std::string> e2; + EXPECT_TRUE(e1 == e2); +} + + +TEST(optionalTest, SwapRegression) { + StructorListener listener; + Listenable::listener = &listener; + + { + absl::optional<Listenable> a; + absl::optional<Listenable> b(absl::in_place); + a.swap(b); + } + + EXPECT_EQ(1, listener.construct0); + EXPECT_EQ(1, listener.move); + EXPECT_EQ(2, listener.destruct); + + { + absl::optional<Listenable> a(absl::in_place); + absl::optional<Listenable> b; + a.swap(b); + } + + EXPECT_EQ(2, listener.construct0); + EXPECT_EQ(2, listener.move); + EXPECT_EQ(4, listener.destruct); +} + +TEST(optionalTest, BigStringLeakCheck) { + constexpr size_t n = 1 << 16; + + using OS = absl::optional<std::string>; + + OS a; + OS b = absl::nullopt; + OS c = std::string(n, 'c'); + std::string sd(n, 'd'); + OS d = sd; + OS e(absl::in_place, n, 'e'); + OS f; + f.emplace(n, 'f'); + + OS ca(a); + OS cb(b); + OS cc(c); + OS cd(d); + OS ce(e); + + OS oa; + OS ob = absl::nullopt; + OS oc = std::string(n, 'c'); + std::string sod(n, 'd'); + OS od = sod; + OS oe(absl::in_place, n, 'e'); + OS of; + of.emplace(n, 'f'); + + OS ma(std::move(oa)); + OS mb(std::move(ob)); + OS mc(std::move(oc)); + OS md(std::move(od)); + OS me(std::move(oe)); + OS mf(std::move(of)); + + OS aa1; + OS ab1 = absl::nullopt; + OS ac1 = std::string(n, 'c'); + std::string sad1(n, 'd'); + OS ad1 = sad1; + OS ae1(absl::in_place, n, 'e'); + OS af1; + af1.emplace(n, 'f'); + + OS aa2; + OS ab2 = absl::nullopt; + OS ac2 = std::string(n, 'c'); + std::string sad2(n, 'd'); + OS ad2 = sad2; + OS ae2(absl::in_place, n, 'e'); + OS af2; + af2.emplace(n, 'f'); + + aa1 = af2; + ab1 = ae2; + ac1 = ad2; + ad1 = ac2; + ae1 = ab2; + af1 = aa2; + + OS aa3; + OS ab3 = absl::nullopt; + OS ac3 = std::string(n, 'c'); + std::string sad3(n, 'd'); + OS ad3 = sad3; + OS ae3(absl::in_place, n, 'e'); + OS af3; + af3.emplace(n, 'f'); + + aa3 = absl::nullopt; + ab3 = absl::nullopt; + ac3 = absl::nullopt; + ad3 = absl::nullopt; + ae3 = absl::nullopt; + af3 = absl::nullopt; + + OS aa4; + OS ab4 = absl::nullopt; + OS ac4 = std::string(n, 'c'); + std::string sad4(n, 'd'); + OS ad4 = sad4; + OS ae4(absl::in_place, n, 'e'); + OS af4; + af4.emplace(n, 'f'); + + aa4 = OS(absl::in_place, n, 'a'); + ab4 = OS(absl::in_place, n, 'b'); + ac4 = OS(absl::in_place, n, 'c'); + ad4 = OS(absl::in_place, n, 'd'); + ae4 = OS(absl::in_place, n, 'e'); + af4 = OS(absl::in_place, n, 'f'); + + OS aa5; + OS ab5 = absl::nullopt; + OS ac5 = std::string(n, 'c'); + std::string sad5(n, 'd'); + OS ad5 = sad5; + OS ae5(absl::in_place, n, 'e'); + OS af5; + af5.emplace(n, 'f'); + + std::string saa5(n, 'a'); + std::string sab5(n, 'a'); + std::string sac5(n, 'a'); + std::string sad52(n, 'a'); + std::string sae5(n, 'a'); + std::string saf5(n, 'a'); + + aa5 = saa5; + ab5 = sab5; + ac5 = sac5; + ad5 = sad52; + ae5 = sae5; + af5 = saf5; + + OS aa6; + OS ab6 = absl::nullopt; + OS ac6 = std::string(n, 'c'); + std::string sad6(n, 'd'); + OS ad6 = sad6; + OS ae6(absl::in_place, n, 'e'); + OS af6; + af6.emplace(n, 'f'); + + aa6 = std::string(n, 'a'); + ab6 = std::string(n, 'b'); + ac6 = std::string(n, 'c'); + ad6 = std::string(n, 'd'); + ae6 = std::string(n, 'e'); + af6 = std::string(n, 'f'); + + OS aa7; + OS ab7 = absl::nullopt; + OS ac7 = std::string(n, 'c'); + std::string sad7(n, 'd'); + OS ad7 = sad7; + OS ae7(absl::in_place, n, 'e'); + OS af7; + af7.emplace(n, 'f'); + + aa7.emplace(n, 'A'); + ab7.emplace(n, 'B'); + ac7.emplace(n, 'C'); + ad7.emplace(n, 'D'); + ae7.emplace(n, 'E'); + af7.emplace(n, 'F'); +} + +TEST(optionalTest, MoveAssignRegression) { + StructorListener listener; + Listenable::listener = &listener; + + { + absl::optional<Listenable> a; + Listenable b; + a = std::move(b); + } + + EXPECT_EQ(1, listener.construct0); + EXPECT_EQ(1, listener.move); + EXPECT_EQ(2, listener.destruct); +} + +TEST(optionalTest, ValueType) { + EXPECT_TRUE((std::is_same<absl::optional<int>::value_type, int>::value)); + EXPECT_TRUE( + (std::is_same<absl::optional<std::string>::value_type, std::string>::value)); + EXPECT_FALSE( + (std::is_same<absl::optional<int>::value_type, absl::nullopt_t>::value)); +} + +template <typename T> +struct is_hash_enabled_for { + template <typename U, typename = decltype(std::hash<U>()(std::declval<U>()))> + static std::true_type test(int); + + template <typename U> + static std::false_type test(...); + + static constexpr bool value = decltype(test<T>(0))::value; +}; + +TEST(optionalTest, Hash) { + std::hash<absl::optional<int>> hash; + std::set<size_t> hashcodes; + hashcodes.insert(hash(absl::nullopt)); + for (int i = 0; i < 100; ++i) { + hashcodes.insert(hash(i)); + } + EXPECT_GT(hashcodes.size(), 90); + + static_assert(is_hash_enabled_for<absl::optional<int>>::value, ""); + static_assert(is_hash_enabled_for<absl::optional<Hashable>>::value, ""); + +#if defined(_MSC_VER) || (defined(_LIBCPP_VERSION) && \ + _LIBCPP_VERSION < 4000 && _LIBCPP_STD_VER > 11) + // For MSVC and libc++ (< 4.0 and c++14), std::hash primary template has a + // static_assert to catch any user-defined type that doesn't provide a hash + // specialization. So instantiating std::hash<absl::optional<T>> will result + // in a hard error which is not SFINAE friendly. +#define ABSL_STD_HASH_NOT_SFINAE_FRIENDLY 1 +#endif + +#ifndef ABSL_STD_HASH_NOT_SFINAE_FRIENDLY + static_assert(!is_hash_enabled_for<absl::optional<NonHashable>>::value, ""); +#endif + + // libstdc++ std::optional is missing remove_const_t, i.e. it's using + // std::hash<T> rather than std::hash<std::remove_const_t<T>>. + // Reference: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82262 +#ifndef __GLIBCXX__ + static_assert(is_hash_enabled_for<absl::optional<const int>>::value, ""); + static_assert(is_hash_enabled_for<absl::optional<const Hashable>>::value, ""); + std::hash<absl::optional<const int>> c_hash; + for (int i = 0; i < 100; ++i) { + EXPECT_EQ(hash(i), c_hash(i)); + } +#endif +} + +struct MoveMeNoThrow { + MoveMeNoThrow() : x(0) {} + [[noreturn]] MoveMeNoThrow(const MoveMeNoThrow& other) : x(other.x) { + ABSL_RAW_LOG(FATAL, "Should not be called."); + abort(); + } + MoveMeNoThrow(MoveMeNoThrow&& other) noexcept : x(other.x) {} + int x; +}; + +struct MoveMeThrow { + MoveMeThrow() : x(0) {} + MoveMeThrow(const MoveMeThrow& other) : x(other.x) {} + MoveMeThrow(MoveMeThrow&& other) : x(other.x) {} + int x; +}; + +TEST(optionalTest, NoExcept) { + static_assert( + std::is_nothrow_move_constructible<absl::optional<MoveMeNoThrow>>::value, + ""); +#ifndef ABSL_HAVE_STD_OPTIONAL + static_assert(absl::default_allocator_is_nothrow::value == + std::is_nothrow_move_constructible< + absl::optional<MoveMeThrow>>::value, + ""); +#endif + std::vector<absl::optional<MoveMeNoThrow>> v; + for (int i = 0; i < 10; ++i) v.emplace_back(); +} + +struct AnyLike { + AnyLike(AnyLike&&) = default; + AnyLike(const AnyLike&) = default; + + template <typename ValueType, + typename T = typename std::decay<ValueType>::type, + typename std::enable_if< + !absl::disjunction< + std::is_same<AnyLike, T>, + absl::negation<std::is_copy_constructible<T>>>::value, + int>::type = 0> + AnyLike(ValueType&&) {} // NOLINT(runtime/explicit) + + AnyLike& operator=(AnyLike&&) = default; + AnyLike& operator=(const AnyLike&) = default; + + template <typename ValueType, + typename T = typename std::decay<ValueType>::type> + typename std::enable_if< + absl::conjunction<absl::negation<std::is_same<AnyLike, T>>, + std::is_copy_constructible<T>>::value, + AnyLike&>::type + operator=(ValueType&& /* rhs */) { + return *this; + } +}; + +TEST(optionalTest, ConstructionConstraints) { + EXPECT_TRUE((std::is_constructible<AnyLike, absl::optional<AnyLike>>::value)); + + EXPECT_TRUE( + (std::is_constructible<AnyLike, const absl::optional<AnyLike>&>::value)); + + EXPECT_TRUE((std::is_constructible<absl::optional<AnyLike>, AnyLike>::value)); + EXPECT_TRUE( + (std::is_constructible<absl::optional<AnyLike>, const AnyLike&>::value)); + + EXPECT_TRUE((std::is_convertible<absl::optional<AnyLike>, AnyLike>::value)); + + EXPECT_TRUE( + (std::is_convertible<const absl::optional<AnyLike>&, AnyLike>::value)); + + EXPECT_TRUE((std::is_convertible<AnyLike, absl::optional<AnyLike>>::value)); + EXPECT_TRUE( + (std::is_convertible<const AnyLike&, absl::optional<AnyLike>>::value)); + + EXPECT_TRUE(std::is_move_constructible<absl::optional<AnyLike>>::value); + EXPECT_TRUE(std::is_copy_constructible<absl::optional<AnyLike>>::value); +} + +TEST(optionalTest, AssignmentConstraints) { + EXPECT_TRUE((std::is_assignable<AnyLike&, absl::optional<AnyLike>>::value)); + EXPECT_TRUE( + (std::is_assignable<AnyLike&, const absl::optional<AnyLike>&>::value)); + EXPECT_TRUE((std::is_assignable<absl::optional<AnyLike>&, AnyLike>::value)); + EXPECT_TRUE( + (std::is_assignable<absl::optional<AnyLike>&, const AnyLike&>::value)); + EXPECT_TRUE(std::is_move_assignable<absl::optional<AnyLike>>::value); + EXPECT_TRUE(std::is_copy_assignable<absl::optional<AnyLike>>::value); +} + +} // namespace diff --git a/Firestore/third_party/abseil-cpp/absl/utility/CMakeLists.txt b/Firestore/third_party/abseil-cpp/absl/utility/CMakeLists.txt new file mode 100644 index 0000000..df21b85 --- /dev/null +++ b/Firestore/third_party/abseil-cpp/absl/utility/CMakeLists.txt @@ -0,0 +1,45 @@ +# +# Copyright 2017 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. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + + +list(APPEND UTILITY_PUBLIC_HEADERS + "utility.h" +) + +absl_header_library( + TARGET + absl_utility + EXPORT_NAME + utility +) + + +# +## TESTS +# + +# test utility_test +set(UTILITY_TEST_SRC "utility_test.cc") +set(UTILITY_TEST_PUBLIC_LIBRARIES absl::utility) + +absl_test( + TARGET + utility_test + SOURCES + ${UTILITY_TEST_SRC} + PUBLIC_LIBRARIES + ${UTILITY_TEST_PUBLIC_LIBRARIES} +) |