Skip to content

Commit d122c0d

Browse files
Abseil Teamcopybara-github
authored andcommitted
Add support for printing C++20 std::*_ordering types to gtest.
Adds feature test macro for C++20 <compare> header, a pretty-printer, and tests. Inexplicably, these types aren't enums, so can't be handled with a switch. PiperOrigin-RevId: 704783038 Change-Id: I29688989d18f43520fe610c12a447a20d2f98c95
1 parent 35d0c36 commit d122c0d

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

googletest/include/gtest/gtest-printers.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@
126126
#include <span> // NOLINT
127127
#endif // GTEST_INTERNAL_HAS_STD_SPAN
128128

129+
#if GTEST_INTERNAL_HAS_COMPARE_LIB
130+
#include <compare> // NOLINT
131+
#endif // GTEST_INTERNAL_HAS_COMPARE_LIB
132+
129133
namespace testing {
130134

131135
// Definitions in the internal* namespaces are subject to change without notice.
@@ -782,6 +786,41 @@ void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) {
782786
(PrintSmartPointer<T>)(ptr, os, 0);
783787
}
784788

789+
#if GTEST_INTERNAL_HAS_COMPARE_LIB
790+
template <typename T>
791+
void PrintOrderingHelper(T ordering, std::ostream* os) {
792+
if (ordering == T::less) {
793+
*os << "(less)";
794+
} else if (ordering == T::greater) {
795+
*os << "(greater)";
796+
} else if (ordering == T::equivalent) {
797+
*os << "(equivalent)";
798+
} else {
799+
*os << "(unknown ordering)";
800+
}
801+
}
802+
803+
inline void PrintTo(std::strong_ordering ordering, std::ostream* os) {
804+
if (ordering == std::strong_ordering::equal) {
805+
*os << "(equal)";
806+
} else {
807+
PrintOrderingHelper(ordering, os);
808+
}
809+
}
810+
811+
inline void PrintTo(std::partial_ordering ordering, std::ostream* os) {
812+
if (ordering == std::partial_ordering::unordered) {
813+
*os << "(unordered)";
814+
} else {
815+
PrintOrderingHelper(ordering, os);
816+
}
817+
}
818+
819+
inline void PrintTo(std::weak_ordering ordering, std::ostream* os) {
820+
PrintOrderingHelper(ordering, os);
821+
}
822+
#endif
823+
785824
// Helper function for printing a tuple. T must be instantiated with
786825
// a tuple type.
787826
template <typename T>

googletest/include/gtest/internal/gtest-port.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,4 +2533,12 @@ using Variant = ::std::variant<T...>;
25332533
#define GTEST_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL 1
25342534
#endif
25352535

2536+
#if (defined(__cpp_lib_three_way_comparison) || \
2537+
(GTEST_INTERNAL_HAS_INCLUDE(<compare>) && \
2538+
GTEST_INTERNAL_CPLUSPLUS_LANG >= 201907L))
2539+
#define GTEST_INTERNAL_HAS_COMPARE_LIB 1
2540+
#else
2541+
#define GTEST_INTERNAL_HAS_COMPARE_LIB 0
2542+
#endif
2543+
25362544
#endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_

googletest/test/googletest-printers-test.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@
6464
#include <span> // NOLINT
6565
#endif // GTEST_INTERNAL_HAS_STD_SPAN
6666

67+
#if GTEST_INTERNAL_HAS_COMPARE_LIB
68+
#include <compare> // NOLINT
69+
#endif // GTEST_INTERNAL_HAS_COMPARE_LIB
70+
6771
// Some user-defined types for testing the universal value printer.
6872

6973
// An anonymous enum type.
@@ -1970,6 +1974,26 @@ TEST(PrintOneofTest, Basic) {
19701974
PrintToString(Type(NonPrintable{})));
19711975
}
19721976
#endif // GTEST_INTERNAL_HAS_VARIANT
1977+
1978+
#if GTEST_INTERNAL_HAS_COMPARE_LIB
1979+
TEST(PrintOrderingTest, Basic) {
1980+
EXPECT_EQ("(less)", PrintToString(std::strong_ordering::less));
1981+
EXPECT_EQ("(greater)", PrintToString(std::strong_ordering::greater));
1982+
// equal == equivalent for strong_ordering.
1983+
EXPECT_EQ("(equal)", PrintToString(std::strong_ordering::equivalent));
1984+
EXPECT_EQ("(equal)", PrintToString(std::strong_ordering::equal));
1985+
1986+
EXPECT_EQ("(less)", PrintToString(std::weak_ordering::less));
1987+
EXPECT_EQ("(greater)", PrintToString(std::weak_ordering::greater));
1988+
EXPECT_EQ("(equivalent)", PrintToString(std::weak_ordering::equivalent));
1989+
1990+
EXPECT_EQ("(less)", PrintToString(std::partial_ordering::less));
1991+
EXPECT_EQ("(greater)", PrintToString(std::partial_ordering::greater));
1992+
EXPECT_EQ("(equivalent)", PrintToString(std::partial_ordering::equivalent));
1993+
EXPECT_EQ("(unordered)", PrintToString(std::partial_ordering::unordered));
1994+
}
1995+
#endif
1996+
19731997
namespace {
19741998
class string_ref;
19751999

0 commit comments

Comments
 (0)