| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -104,15 +104,19 @@ | |||
| 104 | 104 | #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ | |
| 105 | 105 | #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_ | |
| 106 | 106 | ||
| 107 | + #include <any> | ||
| 107 | 108 | #include <functional> | |
| 108 | 109 | #include <memory> | |
| 110 | + #include <optional> | ||
| 109 | 111 | #include <ostream> // NOLINT | |
| 110 | 112 | #include <sstream> | |
| 111 | 113 | #include <string> | |
| 114 | + #include <string_view> | ||
| 112 | 115 | #include <tuple> | |
| 113 | 116 | #include <type_traits> | |
| 114 | 117 | #include <typeinfo> | |
| 115 | 118 | #include <utility> | |
| 119 | + #include <variant> | ||
| 116 | 120 | #include <vector> | |
| 117 | 121 | ||
| 118 | 122 | #ifdef GTEST_HAS_ABSL | |
@@ -245,8 +249,8 @@ struct StreamPrinter { | |||
| 245 | 249 | // ADL (possibly involving implicit conversions). | |
| 246 | 250 | // (Use SFINAE via return type, because it seems GCC < 12 doesn't handle name | |
| 247 | 251 | // lookup properly when we do it in the template parameter list.) | |
| 248 | - static auto PrintValue(const T& value, | ||
| 249 | - ::std::ostream* os) -> decltype((void)(*os << value)) { | ||
| 252 | + static auto PrintValue(const T& value, ::std::ostream* os) | ||
| 253 | + -> decltype((void)(*os << value)) { | ||
| 250 | 254 | // Call streaming operator found by ADL, possibly with implicit conversions | |
| 251 | 255 | // of the arguments. | |
| 252 | 256 | *os << value; | |
@@ -521,11 +525,15 @@ GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os); | |||
| 521 | 525 | ||
| 522 | 526 | GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os); | |
| 523 | 527 | inline void PrintTo(char16_t c, ::std::ostream* os) { | |
| 524 | - PrintTo(ImplicitCast_<char32_t>(c), os); | ||
| 528 | + // TODO(b/418738869): Incorrect for values not representing valid codepoints. | ||
| 529 | + // Also see https://github.com/google/googletest/issues/4762. | ||
| 530 | + PrintTo(static_cast<char32_t>(c), os); | ||
| 525 | 531 | } | |
| 526 | 532 | #ifdef __cpp_lib_char8_t | |
| 527 | 533 | inline void PrintTo(char8_t c, ::std::ostream* os) { | |
| 528 | - PrintTo(ImplicitCast_<char32_t>(c), os); | ||
| 534 | + // TODO(b/418738869): Incorrect for values not representing valid codepoints. | ||
| 535 | + // Also see https://github.com/google/googletest/issues/4762. | ||
| 536 | + PrintTo(static_cast<char32_t>(c), os); | ||
| 529 | 537 | } | |
| 530 | 538 | #endif | |
| 531 | 539 | ||
@@ -695,44 +703,63 @@ void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) { | |||
| 695 | 703 | } | |
| 696 | 704 | } | |
| 697 | 705 | ||
| 698 | - // Overloads for ::std::string. | ||
| 699 | - GTEST_API_ void PrintStringTo(const ::std::string& s, ::std::ostream* os); | ||
| 706 | + // Overloads for ::std::string and ::std::string_view | ||
| 707 | + GTEST_API_ void PrintStringTo(::std::string_view s, ::std::ostream* os); | ||
| 700 | 708 | inline void PrintTo(const ::std::string& s, ::std::ostream* os) { | |
| 701 | 709 | PrintStringTo(s, os); | |
| 702 | 710 | } | |
| 711 | + inline void PrintTo(::std::string_view s, ::std::ostream* os) { | ||
| 712 | + PrintStringTo(s, os); | ||
| 713 | + } | ||
| 703 | 714 | ||
| 704 | - // Overloads for ::std::u8string | ||
| 715 | + // Overloads for ::std::u8string and ::std::u8string_view | ||
| 705 | 716 | #ifdef __cpp_lib_char8_t | |
| 706 | - GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os); | ||
| 717 | + GTEST_API_ void PrintU8StringTo(::std::u8string_view s, ::std::ostream* os); | ||
| 707 | 718 | inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) { | |
| 708 | 719 | PrintU8StringTo(s, os); | |
| 709 | 720 | } | |
| 721 | + inline void PrintTo(::std::u8string_view s, ::std::ostream* os) { | ||
| 722 | + PrintU8StringTo(s, os); | ||
| 723 | + } | ||
| 710 | 724 | #endif | |
| 711 | 725 | ||
| 712 | - // Overloads for ::std::u16string | ||
| 713 | - GTEST_API_ void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os); | ||
| 726 | + // Overloads for ::std::u16string and ::std::u16string_view | ||
| 727 | + GTEST_API_ void PrintU16StringTo(::std::u16string_view s, ::std::ostream* os); | ||
| 714 | 728 | inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) { | |
| 715 | 729 | PrintU16StringTo(s, os); | |
| 716 | 730 | } | |
| 731 | + inline void PrintTo(::std::u16string_view s, ::std::ostream* os) { | ||
| 732 | + PrintU16StringTo(s, os); | ||
| 733 | + } | ||
| 717 | 734 | ||
| 718 | - // Overloads for ::std::u32string | ||
| 719 | - GTEST_API_ void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os); | ||
| 735 | + // Overloads for ::std::u32string and ::std::u32string_view | ||
| 736 | + GTEST_API_ void PrintU32StringTo(::std::u32string_view s, ::std::ostream* os); | ||
| 720 | 737 | inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) { | |
| 721 | 738 | PrintU32StringTo(s, os); | |
| 722 | 739 | } | |
| 740 | + inline void PrintTo(::std::u32string_view s, ::std::ostream* os) { | ||
| 741 | + PrintU32StringTo(s, os); | ||
| 742 | + } | ||
| 723 | 743 | ||
| 724 | - // Overloads for ::std::wstring. | ||
| 744 | + // Overloads for ::std::wstring and ::std::wstring_view | ||
| 725 | 745 | #if GTEST_HAS_STD_WSTRING | |
| 726 | - GTEST_API_ void PrintWideStringTo(const ::std::wstring& s, ::std::ostream* os); | ||
| 746 | + GTEST_API_ void PrintWideStringTo(::std::wstring_view s, ::std::ostream* os); | ||
| 727 | 747 | inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) { | |
| 728 | 748 | PrintWideStringTo(s, os); | |
| 729 | 749 | } | |
| 750 | + inline void PrintTo(::std::wstring_view s, ::std::ostream* os) { | ||
| 751 | + PrintWideStringTo(s, os); | ||
| 752 | + } | ||
| 730 | 753 | #endif // GTEST_HAS_STD_WSTRING | |
| 731 | 754 | ||
| 732 | 755 | #if GTEST_INTERNAL_HAS_STRING_VIEW | |
| 733 | - // Overload for internal::StringView. | ||
| 756 | + // Overload for internal::StringView. Needed for build configurations where | ||
| 757 | + // internal::StringView is an alias for absl::string_view, but absl::string_view | ||
| 758 | + // is a distinct type from std::string_view. | ||
| 759 | + template <int&... ExplicitArgumentBarrier, typename T = internal::StringView, | ||
| 760 | + std::enable_if_t<!std::is_same_v<T, ::std::string_view>, int> = 0> | ||
| 734 | 761 | inline void PrintTo(internal::StringView sp, ::std::ostream* os) { | |
| 735 | - PrintTo(::std::string(sp), os); | ||
| 762 | + PrintStringTo(sp, os); | ||
| 736 | 763 | } | |
| 737 | 764 | #endif // GTEST_INTERNAL_HAS_STRING_VIEW | |
| 738 | 765 | ||
@@ -890,14 +917,11 @@ class UniversalPrinter { | |||
| 890 | 917 | template <typename T> | |
| 891 | 918 | class UniversalPrinter<const T> : public UniversalPrinter<T> {}; | |
| 892 | 919 | ||
| 893 | - #if GTEST_INTERNAL_HAS_ANY | ||
| 894 | - | ||
| 895 | - // Printer for std::any / absl::any | ||
| 896 | - | ||
| 920 | + // Printer for std::any | ||
| 897 | 921 | template <> | |
| 898 | - class UniversalPrinter<Any> { | ||
| 922 | + class UniversalPrinter<std::any> { | ||
| 899 | 923 | public: | |
| 900 | - static void Print(const Any& value, ::std::ostream* os) { | ||
| 924 | + static void Print(const std::any& value, ::std::ostream* os) { | ||
| 901 | 925 | if (value.has_value()) { | |
| 902 | 926 | *os << "value of type " << GetTypeName(value); | |
| 903 | 927 | } else { | |
@@ -906,7 +930,7 @@ class UniversalPrinter<Any> { | |||
| 906 | 930 | } | |
| 907 | 931 | ||
| 908 | 932 | private: | |
| 909 | - static std::string GetTypeName(const Any& value) { | ||
| 933 | + static std::string GetTypeName(const std::any& value) { | ||
| 910 | 934 | #if GTEST_HAS_RTTI | |
| 911 | 935 | return internal::GetTypeName(value.type()); | |
| 912 | 936 | #else | |
@@ -916,16 +940,11 @@ class UniversalPrinter<Any> { | |||
| 916 | 940 | } | |
| 917 | 941 | }; | |
| 918 | 942 | ||
| 919 | - #endif // GTEST_INTERNAL_HAS_ANY | ||
| 920 | - | ||
| 921 | - #if GTEST_INTERNAL_HAS_OPTIONAL | ||
| 922 | - | ||
| 923 | - // Printer for std::optional / absl::optional | ||
| 924 | - | ||
| 943 | + // Printer for std::optional | ||
| 925 | 944 | template <typename T> | |
| 926 | - class UniversalPrinter<Optional<T>> { | ||
| 945 | + class UniversalPrinter<std::optional<T>> { | ||
| 927 | 946 | public: | |
| 928 | - static void Print(const Optional<T>& value, ::std::ostream* os) { | ||
| 947 | + static void Print(const std::optional<T>& value, ::std::ostream* os) { | ||
| 929 | 948 | *os << '('; | |
| 930 | 949 | if (!value) { | |
| 931 | 950 | *os << "nullopt"; | |
@@ -937,29 +956,18 @@ class UniversalPrinter<Optional<T>> { | |||
| 937 | 956 | }; | |
| 938 | 957 | ||
| 939 | 958 | template <> | |
| 940 | - class UniversalPrinter<decltype(Nullopt())> { | ||
| 959 | + class UniversalPrinter<std::nullopt_t> { | ||
| 941 | 960 | public: | |
| 942 | - static void Print(decltype(Nullopt()), ::std::ostream* os) { | ||
| 943 | - *os << "(nullopt)"; | ||
| 944 | - } | ||
| 961 | + static void Print(std::nullopt_t, ::std::ostream* os) { *os << "(nullopt)"; } | ||
| 945 | 962 | }; | |
| 946 | 963 | ||
| 947 | - #endif // GTEST_INTERNAL_HAS_OPTIONAL | ||
| 948 | - | ||
| 949 | - #if GTEST_INTERNAL_HAS_VARIANT | ||
| 950 | - | ||
| 951 | - // Printer for std::variant / absl::variant | ||
| 952 | - | ||
| 964 | + // Printer for std::variant | ||
| 953 | 965 | template <typename... T> | |
| 954 | - class UniversalPrinter<Variant<T...>> { | ||
| 966 | + class UniversalPrinter<std::variant<T...>> { | ||
| 955 | 967 | public: | |
| 956 | - static void Print(const Variant<T...>& value, ::std::ostream* os) { | ||
| 968 | + static void Print(const std::variant<T...>& value, ::std::ostream* os) { | ||
| 957 | 969 | *os << '('; | |
| 958 | - #ifdef GTEST_HAS_ABSL | ||
| 959 | - absl::visit(Visitor{os, value.index()}, value); | ||
| 960 | - #else | ||
| 961 | 970 | std::visit(Visitor{os, value.index()}, value); | |
| 962 | - #endif // GTEST_HAS_ABSL | ||
| 963 | 971 | *os << ')'; | |
| 964 | 972 | } | |
| 965 | 973 | ||
@@ -976,8 +984,6 @@ class UniversalPrinter<Variant<T...>> { | |||
| 976 | 984 | }; | |
| 977 | 985 | }; | |
| 978 | 986 | ||
| 979 | - #endif // GTEST_INTERNAL_HAS_VARIANT | ||
| 980 | - | ||
| 981 | 987 | // UniversalPrintArray(begin, len, os) prints an array of 'len' | |
| 982 | 988 | // elements, starting at address 'begin'. | |
| 983 | 989 | template <typename T> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1693,7 +1693,7 @@ class WithParamInterface { | |||
| 1693 | 1693 | ||
| 1694 | 1694 | // The current parameter value. Is also available in the test fixture's | |
| 1695 | 1695 | // constructor. | |
| 1696 | - static const ParamType& GetParam() { | ||
| 1696 | + [[nodiscard]] static const ParamType& GetParam() { | ||
| 1697 | 1697 | GTEST_CHECK_(parameter_ != nullptr) | |
| 1698 | 1698 | << "GetParam() can only be called inside a value-parameterized test " | |
| 1699 | 1699 | << "-- did you intend to write TEST_P instead of TEST_F?"; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -290,17 +290,17 @@ class FloatingPoint { | |||
| 290 | 290 | // around may change its bits, although the new value is guaranteed | |
| 291 | 291 | // to be also a NAN. Therefore, don't expect this constructor to | |
| 292 | 292 | // preserve the bits in x when x is a NAN. | |
| 293 | - explicit FloatingPoint(const RawType& x) { u_.value_ = x; } | ||
| 293 | + explicit FloatingPoint(RawType x) { memcpy(&bits_, &x, sizeof(x)); } | ||
| 294 | 294 | ||
| 295 | 295 | // Static methods | |
| 296 | 296 | ||
| 297 | 297 | // Reinterprets a bit pattern as a floating-point number. | |
| 298 | 298 | // | |
| 299 | 299 | // This function is needed to test the AlmostEquals() method. | |
| 300 | - static RawType ReinterpretBits(const Bits bits) { | ||
| 301 | - FloatingPoint fp(0); | ||
| 302 | - fp.u_.bits_ = bits; | ||
| 303 | - return fp.u_.value_; | ||
| 300 | + static RawType ReinterpretBits(Bits bits) { | ||
| 301 | + RawType fp; | ||
| 302 | + memcpy(&fp, &bits, sizeof(fp)); | ||
| 303 | + return fp; | ||
| 304 | 304 | } | |
| 305 | 305 | ||
| 306 | 306 | // Returns the floating-point number that represent positive infinity. | |
@@ -309,16 +309,16 @@ class FloatingPoint { | |||
| 309 | 309 | // Non-static methods | |
| 310 | 310 | ||
| 311 | 311 | // Returns the bits that represents this number. | |
| 312 | - const Bits& bits() const { return u_.bits_; } | ||
| 312 | + const Bits& bits() const { return bits_; } | ||
| 313 | 313 | ||
| 314 | 314 | // Returns the exponent bits of this number. | |
| 315 | - Bits exponent_bits() const { return kExponentBitMask & u_.bits_; } | ||
| 315 | + Bits exponent_bits() const { return kExponentBitMask & bits_; } | ||
| 316 | 316 | ||
| 317 | 317 | // Returns the fraction bits of this number. | |
| 318 | - Bits fraction_bits() const { return kFractionBitMask & u_.bits_; } | ||
| 318 | + Bits fraction_bits() const { return kFractionBitMask & bits_; } | ||
| 319 | 319 | ||
| 320 | 320 | // Returns the sign bit of this number. | |
| 321 | - Bits sign_bit() const { return kSignBitMask & u_.bits_; } | ||
| 321 | + Bits sign_bit() const { return kSignBitMask & bits_; } | ||
| 322 | 322 | ||
| 323 | 323 | // Returns true if and only if this is NAN (not a number). | |
| 324 | 324 | bool is_nan() const { | |
@@ -332,23 +332,16 @@ class FloatingPoint { | |||
| 332 | 332 | // | |
| 333 | 333 | // - returns false if either number is (or both are) NAN. | |
| 334 | 334 | // - treats really large numbers as almost equal to infinity. | |
| 335 | - // - thinks +0.0 and -0.0 are 0 DLP's apart. | ||
| 335 | + // - thinks +0.0 and -0.0 are 0 ULP's apart. | ||
| 336 | 336 | bool AlmostEquals(const FloatingPoint& rhs) const { | |
| 337 | 337 | // The IEEE standard says that any comparison operation involving | |
| 338 | 338 | // a NAN must return false. | |
| 339 | 339 | if (is_nan() || rhs.is_nan()) return false; | |
| 340 | 340 | ||
| 341 | - return DistanceBetweenSignAndMagnitudeNumbers(u_.bits_, rhs.u_.bits_) <= | ||
| 342 | - kMaxUlps; | ||
| 341 | + return DistanceBetweenSignAndMagnitudeNumbers(bits_, rhs.bits_) <= kMaxUlps; | ||
| 343 | 342 | } | |
| 344 | 343 | ||
| 345 | 344 | private: | |
| 346 | - // The data type used to store the actual floating-point number. | ||
| 347 | - union FloatingPointUnion { | ||
| 348 | - RawType value_; // The raw floating-point number. | ||
| 349 | - Bits bits_; // The bits that represent the number. | ||
| 350 | - }; | ||
| 351 | - | ||
| 352 | 345 | // Converts an integer from the sign-and-magnitude representation to | |
| 353 | 346 | // the biased representation. More precisely, let N be 2 to the | |
| 354 | 347 | // power of (kBitCount - 1), an integer x is represented by the | |
@@ -364,7 +357,7 @@ class FloatingPoint { | |||
| 364 | 357 | // | |
| 365 | 358 | // Read https://en.wikipedia.org/wiki/Signed_number_representations | |
| 366 | 359 | // for more details on signed number representations. | |
| 367 | - static Bits SignAndMagnitudeToBiased(const Bits& sam) { | ||
| 360 | + static Bits SignAndMagnitudeToBiased(Bits sam) { | ||
| 368 | 361 | if (kSignBitMask & sam) { | |
| 369 | 362 | // sam represents a negative number. | |
| 370 | 363 | return ~sam + 1; | |
@@ -376,14 +369,13 @@ class FloatingPoint { | |||
| 376 | 369 | ||
| 377 | 370 | // Given two numbers in the sign-and-magnitude representation, | |
| 378 | 371 | // returns the distance between them as an unsigned number. | |
| 379 | - static Bits DistanceBetweenSignAndMagnitudeNumbers(const Bits& sam1, | ||
| 380 | - const Bits& sam2) { | ||
| 372 | + static Bits DistanceBetweenSignAndMagnitudeNumbers(Bits sam1, Bits sam2) { | ||
| 381 | 373 | const Bits biased1 = SignAndMagnitudeToBiased(sam1); | |
| 382 | 374 | const Bits biased2 = SignAndMagnitudeToBiased(sam2); | |
| 383 | 375 | return (biased1 >= biased2) ? (biased1 - biased2) : (biased2 - biased1); | |
| 384 | 376 | } | |
| 385 | 377 | ||
| 386 | - FloatingPointUnion u_; | ||
| 378 | + Bits bits_; // The bits that represent the number. | ||
| 387 | 379 | }; | |
| 388 | 380 | ||
| 389 | 381 | // Typedefs the instances of the FloatingPoint template class that we | |
| Back | FazBrowse Home | New Git URL |
0 commit comments