diff options
Diffstat (limited to 'googlemock')
-rw-r--r-- | googlemock/docs/cheat_sheet.md | 1 | ||||
-rw-r--r-- | googlemock/include/gmock/gmock-matchers.h | 28 | ||||
-rw-r--r-- | googlemock/include/gmock/internal/gmock-internal-utils.h | 92 | ||||
-rw-r--r-- | googlemock/include/gmock/internal/gmock-pp.h | 108 | ||||
-rw-r--r-- | googlemock/test/gmock-generated-matchers_test.cc | 53 | ||||
-rw-r--r-- | googlemock/test/gmock-matchers_test.cc | 146 | ||||
-rw-r--r-- | googlemock/test/gmock-pp_test.cc | 10 |
7 files changed, 249 insertions, 189 deletions
diff --git a/googlemock/docs/cheat_sheet.md b/googlemock/docs/cheat_sheet.md index 8ec5ea0..3236e6a 100644 --- a/googlemock/docs/cheat_sheet.md +++ b/googlemock/docs/cheat_sheet.md @@ -287,6 +287,7 @@ is not changed afterwards, or the meaning of your matcher will be changed. | `FloatEq(a_float)` | `argument` is a `float` value approximately equal to `a_float`, treating two NaNs as unequal. | | `NanSensitiveDoubleEq(a_double)` | `argument` is a `double` value approximately equal to `a_double`, treating two NaNs as equal. | | `NanSensitiveFloatEq(a_float)` | `argument` is a `float` value approximately equal to `a_float`, treating two NaNs as equal. | +| `IsNan()` | `argument` is any floating-point type with a NaN value. | <!-- mdformat on --> The above matchers use ULP-based comparison (the same as used in googletest). diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index 4428ec1..bb047da 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -42,8 +42,8 @@ #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ #define GMOCK_INCLUDE_GMOCK_GMOCK_MATCHERS_H_ -#include <math.h> #include <algorithm> +#include <cmath> #include <initializer_list> #include <iterator> #include <limits> @@ -54,6 +54,7 @@ #include <type_traits> #include <utility> #include <vector> + #include "gmock/internal/gmock-internal-utils.h" #include "gmock/internal/gmock-port.h" #include "gtest/gtest.h" @@ -141,7 +142,7 @@ class MatcherCastImpl { template <bool Ignore> static Matcher<T> CastImpl(const M& polymorphic_matcher_or_value, std::true_type /* convertible_to_matcher */, - bool_constant<Ignore>) { + std::integral_constant<bool, Ignore>) { // M is implicitly convertible to Matcher<T>, which means that either // M is a polymorphic matcher or Matcher<T> has an implicit constructor // from M. In both cases using the implicit conversion will produce a @@ -1349,6 +1350,22 @@ MakePredicateFormatterFromMatcher(M matcher) { return PredicateFormatterFromMatcher<M>(std::move(matcher)); } +// Implements the polymorphic IsNan() matcher, which matches any floating type +// value that is Nan. +class IsNanMatcher { + public: + template <typename FloatType> + bool MatchAndExplain(const FloatType& f, + MatchResultListener* /* listener */) const { + return (::std::isnan)(f); + } + + void DescribeTo(::std::ostream* os) const { *os << "is NaN"; } + void DescribeNegationTo(::std::ostream* os) const { + *os << "isn't NaN"; + } +}; + // Implements the polymorphic floating point equality matcher, which matches // two float values using ULP-based approximation or, optionally, a // user-specified epsilon. The template is meant to be instantiated with @@ -1409,7 +1426,7 @@ class FloatingEqMatcher { } const FloatType diff = value - expected_; - if (fabs(diff) <= max_abs_error_) { + if (::std::fabs(diff) <= max_abs_error_) { return true; } @@ -3626,6 +3643,11 @@ inline internal::RefMatcher<T&> Ref(T& x) { // NOLINT return internal::RefMatcher<T&>(x); } +// Creates a polymorphic matcher that matches any NaN floating point. +inline PolymorphicMatcher<internal::IsNanMatcher> IsNan() { + return MakePolymorphicMatcher(internal::IsNanMatcher()); +} + // Creates a matcher that matches any double argument approximately // equal to rhs, where two NANs are considered unequal. inline internal::FloatingEqMatcher<double> DoubleEq(double rhs) { diff --git a/googlemock/include/gmock/internal/gmock-internal-utils.h b/googlemock/include/gmock/internal/gmock-internal-utils.h index 584afa9..5fd169e 100644 --- a/googlemock/include/gmock/internal/gmock-internal-utils.h +++ b/googlemock/include/gmock/internal/gmock-internal-utils.h @@ -157,9 +157,6 @@ GMOCK_DECLARE_KIND_(long double, kFloatingPoint); static_cast< ::testing::internal::TypeKind>( \ ::testing::internal::KindOf<type>::value) -// Evaluates to true if and only if integer type T is signed. -#define GMOCK_IS_SIGNED_(T) (static_cast<T>(-1) < 0) - // LosslessArithmeticConvertibleImpl<kFromKind, From, kToKind, To>::value // is true if and only if arithmetic type From can be losslessly converted to // arithmetic type To. @@ -170,65 +167,30 @@ GMOCK_DECLARE_KIND_(long double, kFloatingPoint); // From, and kToKind is the kind of To; the value is // implementation-defined when the above pre-condition is violated. template <TypeKind kFromKind, typename From, TypeKind kToKind, typename To> -struct LosslessArithmeticConvertibleImpl : public std::false_type {}; - -// Converting bool to bool is lossless. -template <> -struct LosslessArithmeticConvertibleImpl<kBool, bool, kBool, bool> - : public std::true_type {}; - -// Converting bool to any integer type is lossless. -template <typename To> -struct LosslessArithmeticConvertibleImpl<kBool, bool, kInteger, To> - : public std::true_type {}; - -// Converting bool to any floating-point type is lossless. -template <typename To> -struct LosslessArithmeticConvertibleImpl<kBool, bool, kFloatingPoint, To> - : public std::true_type {}; - -// Converting an integer to bool is lossy. -template <typename From> -struct LosslessArithmeticConvertibleImpl<kInteger, From, kBool, bool> - : public std::false_type {}; - -// Converting an integer to another non-bool integer is lossless -// if and only if the target type's range encloses the source type's range. -template <typename From, typename To> -struct LosslessArithmeticConvertibleImpl<kInteger, From, kInteger, To> - : public bool_constant< - // When converting from a smaller size to a larger size, we are - // fine as long as we are not converting from signed to unsigned. - ((sizeof(From) < sizeof(To)) && - (!GMOCK_IS_SIGNED_(From) || GMOCK_IS_SIGNED_(To))) || - // When converting between the same size, the signedness must match. - ((sizeof(From) == sizeof(To)) && - (GMOCK_IS_SIGNED_(From) == GMOCK_IS_SIGNED_(To)))> {}; // NOLINT - -#undef GMOCK_IS_SIGNED_ - -// Converting an integer to a floating-point type may be lossy, since -// the format of a floating-point number is implementation-defined. -template <typename From, typename To> -struct LosslessArithmeticConvertibleImpl<kInteger, From, kFloatingPoint, To> - : public std::false_type {}; - -// Converting a floating-point to bool is lossy. -template <typename From> -struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kBool, bool> - : public std::false_type {}; - -// Converting a floating-point to an integer is lossy. -template <typename From, typename To> -struct LosslessArithmeticConvertibleImpl<kFloatingPoint, From, kInteger, To> - : public std::false_type {}; - -// Converting a floating-point to another floating-point is lossless -// if and only if the target type is at least as big as the source type. -template <typename From, typename To> -struct LosslessArithmeticConvertibleImpl< - kFloatingPoint, From, kFloatingPoint, To> - : public bool_constant<sizeof(From) <= sizeof(To)> {}; // NOLINT +using LosslessArithmeticConvertibleImpl = std::integral_constant< + bool, + // clang-format off + // Converting from bool is always lossless + (kFromKind == kBool) ? true + // Converting between any other type kinds will be lossy if the type + // kinds are not the same. + : (kFromKind != kToKind) ? false + : (kFromKind == kInteger && + // Converting between integers of different widths is allowed so long + // as the conversion does not go from signed to unsigned. + (((sizeof(From) < sizeof(To)) && + !(std::is_signed<From>::value && !std::is_signed<To>::value)) || + // Converting between integers of the same width only requires the + // two types to have the same signedness. + ((sizeof(From) == sizeof(To)) && + (std::is_signed<From>::value == std::is_signed<To>::value))) + ) ? true + // Floating point conversions are lossless if and only if `To` is at least + // as wide as `From`. + : (kFromKind == kFloatingPoint && (sizeof(From) <= sizeof(To))) ? true + : false + // clang-format on + >; // LosslessArithmeticConvertible<From, To>::value is true if and only if // arithmetic type From can be losslessly converted to arithmetic type To. @@ -238,9 +200,9 @@ struct LosslessArithmeticConvertibleImpl< // reference) built-in arithmetic types; the value is // implementation-defined when the above pre-condition is violated. template <typename From, typename To> -struct LosslessArithmeticConvertible - : public LosslessArithmeticConvertibleImpl< - GMOCK_KIND_OF_(From), From, GMOCK_KIND_OF_(To), To> {}; // NOLINT +using LosslessArithmeticConvertible = + LosslessArithmeticConvertibleImpl<GMOCK_KIND_OF_(From), From, + GMOCK_KIND_OF_(To), To>; // This interface knows how to report a Google Mock failure (either // non-fatal or fatal). diff --git a/googlemock/include/gmock/internal/gmock-pp.h b/googlemock/include/gmock/internal/gmock-pp.h index 1ab80e1..c3759f6 100644 --- a/googlemock/include/gmock/internal/gmock-pp.h +++ b/googlemock/include/gmock/internal/gmock-pp.h @@ -1,19 +1,6 @@ #ifndef THIRD_PARTY_GOOGLETEST_GOOGLEMOCK_INCLUDE_GMOCK_PP_H_ #define THIRD_PARTY_GOOGLETEST_GOOGLEMOCK_INCLUDE_GMOCK_PP_H_ -#undef GMOCK_PP_INTERNAL_USE_MSVC -#if defined(__clang__) -#define GMOCK_PP_INTERNAL_USE_MSVC 0 -#elif defined(_MSC_VER) -// TODO(iserna): Also verify tradional versus comformant preprocessor. -static_assert( - _MSC_VER >= 1900, - "MSVC version not supported. There is support for MSVC 14.0 and above."); -#define GMOCK_PP_INTERNAL_USE_MSVC 1 -#else -#define GMOCK_PP_INTERNAL_USE_MSVC 0 -#endif - // Expands and concatenates the arguments. Constructed macros reevaluate. #define GMOCK_PP_CAT(_1, _2) GMOCK_PP_INTERNAL_CAT(_1, _2) @@ -29,10 +16,6 @@ static_assert( // Returns the only argument. #define GMOCK_PP_IDENTITY(_1) _1 -// MSVC preprocessor collapses __VA_ARGS__ in a single argument, we use a -// CAT-like directive to force correct evaluation. Each macro has its own. -#if GMOCK_PP_INTERNAL_USE_MSVC - // Evaluates to the number of arguments after expansion. // // #define PAIR x, y @@ -43,45 +26,27 @@ static_assert( // GMOCK_PP_NARG(PAIR) => 2 // // Requires: the number of arguments after expansion is at most 15. -#define GMOCK_PP_NARG(...) \ - GMOCK_PP_INTERNAL_NARG_CAT( \ - GMOCK_PP_INTERNAL_INTERNAL_16TH(__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, \ - 8, 7, 6, 5, 4, 3, 2, 1), ) +#define GMOCK_PP_NARG(...) \ + GMOCK_PP_INTERNAL_16TH( \ + (__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)) // Returns 1 if the expansion of arguments has an unprotected comma. Otherwise // returns 0. Requires no more than 15 unprotected commas. -#define GMOCK_PP_HAS_COMMA(...) \ - GMOCK_PP_INTERNAL_HAS_COMMA_CAT( \ - GMOCK_PP_INTERNAL_INTERNAL_16TH(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ - 1, 1, 1, 1, 1, 0), ) +#define GMOCK_PP_HAS_COMMA(...) \ + GMOCK_PP_INTERNAL_16TH( \ + (__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0)) + // Returns the first argument. -#define GMOCK_PP_HEAD(...) \ - GMOCK_PP_INTERNAL_HEAD_CAT(GMOCK_PP_INTERNAL_HEAD(__VA_ARGS__), ) +#define GMOCK_PP_HEAD(...) GMOCK_PP_INTERNAL_HEAD((__VA_ARGS__)) // Returns the tail. A variadic list of all arguments minus the first. Requires // at least one argument. -#define GMOCK_PP_TAIL(...) \ - GMOCK_PP_INTERNAL_TAIL_CAT(GMOCK_PP_INTERNAL_TAIL(__VA_ARGS__), ) +#define GMOCK_PP_TAIL(...) GMOCK_PP_INTERNAL_TAIL((__VA_ARGS__)) // Calls CAT(_Macro, NARG(__VA_ARGS__))(__VA_ARGS__) #define GMOCK_PP_VARIADIC_CALL(_Macro, ...) \ - GMOCK_PP_INTERNAL_VARIADIC_CALL_CAT( \ - GMOCK_PP_CAT(_Macro, GMOCK_PP_NARG(__VA_ARGS__))(__VA_ARGS__), ) - -#else // GMOCK_PP_INTERNAL_USE_MSVC - -#define GMOCK_PP_NARG(...) \ - GMOCK_PP_INTERNAL_INTERNAL_16TH(__VA_ARGS__, 15, 14, 13, 12, 11, 10, 9, 8, \ - 7, 6, 5, 4, 3, 2, 1) -#define GMOCK_PP_HAS_COMMA(...) \ - GMOCK_PP_INTERNAL_INTERNAL_16TH(__VA_ARGS__, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \ - 1, 1, 1, 1, 0) -#define GMOCK_PP_HEAD(...) GMOCK_PP_INTERNAL_HEAD(__VA_ARGS__) -#define GMOCK_PP_TAIL(...) GMOCK_PP_INTERNAL_TAIL(__VA_ARGS__) -#define GMOCK_PP_VARIADIC_CALL(_Macro, ...) \ - GMOCK_PP_CAT(_Macro, GMOCK_PP_NARG(__VA_ARGS__))(__VA_ARGS__) - -#endif // GMOCK_PP_INTERNAL_USE_MSVC + GMOCK_PP_IDENTITY( \ + GMOCK_PP_CAT(_Macro, GMOCK_PP_NARG(__VA_ARGS__))(__VA_ARGS__)) // If the arguments after expansion have no tokens, evaluates to `1`. Otherwise // evaluates to `0`. @@ -139,10 +104,9 @@ static_assert( // Expands to 1 if the first argument starts with something in parentheses, // otherwise to 0. -#define GMOCK_PP_IS_BEGIN_PARENS(...) \ - GMOCK_PP_INTERNAL_ALTERNATE_HEAD( \ - GMOCK_PP_CAT(GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_R_, \ - GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_C __VA_ARGS__)) +#define GMOCK_PP_IS_BEGIN_PARENS(...) \ + GMOCK_PP_HEAD(GMOCK_PP_CAT(GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_R_, \ + GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_C __VA_ARGS__)) // Expands to 1 is there is only one argument and it is enclosed in parentheses. #define GMOCK_PP_IS_ENCLOSED_PARENS(...) \ @@ -179,10 +143,6 @@ static_assert( #define GMOCK_PP_INTENRAL_EMPTY_TUPLE (, , , , , , , , , , , , , , , ) #define GMOCK_PP_INTERNAL_CAT(_1, _2) _1##_2 #define GMOCK_PP_INTERNAL_STRINGIZE(...) #__VA_ARGS__ -#define GMOCK_PP_INTERNAL_INTERNAL_16TH(_1, _2, _3, _4, _5, _6, _7, _8, _9, \ - _10, _11, _12, _13, _14, _15, _16, \ - ...) \ - _16 #define GMOCK_PP_INTERNAL_CAT_5(_1, _2, _3, _4, _5) _1##_2##_3##_4##_5 #define GMOCK_PP_INTERNAL_IS_EMPTY(_1, _2, _3, _4) \ GMOCK_PP_HAS_COMMA(GMOCK_PP_INTERNAL_CAT_5(GMOCK_PP_INTERNAL_IS_EMPTY_CASE_, \ @@ -190,30 +150,24 @@ static_assert( #define GMOCK_PP_INTERNAL_IS_EMPTY_CASE_0001 , #define GMOCK_PP_INTERNAL_IF_1(_Then, _Else) _Then #define GMOCK_PP_INTERNAL_IF_0(_Then, _Else) _Else -#define GMOCK_PP_INTERNAL_HEAD(_1, ...) _1 -#define GMOCK_PP_INTERNAL_TAIL(_1, ...) __VA_ARGS__ -#if GMOCK_PP_INTERNAL_USE_MSVC -#define GMOCK_PP_INTERNAL_NARG_CAT(_1, _2) GMOCK_PP_INTERNAL_NARG_CAT_I(_1, _2) -#define GMOCK_PP_INTERNAL_HEAD_CAT(_1, _2) GMOCK_PP_INTERNAL_HEAD_CAT_I(_1, _2) -#define GMOCK_PP_INTERNAL_HAS_COMMA_CAT(_1, _2) \ - GMOCK_PP_INTERNAL_HAS_COMMA_CAT_I(_1, _2) -#define GMOCK_PP_INTERNAL_TAIL_CAT(_1, _2) GMOCK_PP_INTERNAL_TAIL_CAT_I(_1, _2) -#define GMOCK_PP_INTERNAL_VARIADIC_CALL_CAT(_1, _2) \ - GMOCK_PP_INTERNAL_VARIADIC_CALL_CAT_I(_1, _2) -#define GMOCK_PP_INTERNAL_NARG_CAT_I(_1, _2) _1##_2 -#define GMOCK_PP_INTERNAL_HEAD_CAT_I(_1, _2) _1##_2 -#define GMOCK_PP_INTERNAL_HAS_COMMA_CAT_I(_1, _2) _1##_2 -#define GMOCK_PP_INTERNAL_TAIL_CAT_I(_1, _2) _1##_2 -#define GMOCK_PP_INTERNAL_VARIADIC_CALL_CAT_I(_1, _2) _1##_2 -#define GMOCK_PP_INTERNAL_ALTERNATE_HEAD(...) \ - GMOCK_PP_INTERNAL_ALTERNATE_HEAD_CAT(GMOCK_PP_HEAD(__VA_ARGS__), ) -#define GMOCK_PP_INTERNAL_ALTERNATE_HEAD_CAT(_1, _2) \ - GMOCK_PP_INTERNAL_ALTERNATE_HEAD_CAT_I(_1, _2) -#define GMOCK_PP_INTERNAL_ALTERNATE_HEAD_CAT_I(_1, _2) _1##_2 -#else // GMOCK_PP_INTERNAL_USE_MSVC -#define GMOCK_PP_INTERNAL_ALTERNATE_HEAD(...) GMOCK_PP_HEAD(__VA_ARGS__) -#endif // GMOCK_PP_INTERNAL_USE_MSVC +// Because of MSVC treating a token with a comma in it as a single token when +// passed to another macro, we need to force it to evaluate it as multiple +// tokens. We do that by using a "IDENTITY(MACRO PARENTHESIZED_ARGS)" macro. We +// define one per possible macro that relies on this behavior. Note "_Args" must +// be parenthesized. +#define GMOCK_PP_INTERNAL_INTERNAL_16TH(_1, _2, _3, _4, _5, _6, _7, _8, _9, \ + _10, _11, _12, _13, _14, _15, _16, \ + ...) \ + _16 +#define GMOCK_PP_INTERNAL_16TH(_Args) \ + GMOCK_PP_IDENTITY(GMOCK_PP_INTERNAL_INTERNAL_16TH _Args) +#define GMOCK_PP_INTERNAL_INTERNAL_HEAD(_1, ...) _1 +#define GMOCK_PP_INTERNAL_HEAD(_Args) \ + GMOCK_PP_IDENTITY(GMOCK_PP_INTERNAL_INTERNAL_HEAD _Args) +#define GMOCK_PP_INTERNAL_INTERNAL_TAIL(_1, ...) __VA_ARGS__ +#define GMOCK_PP_INTERNAL_TAIL(_Args) \ + GMOCK_PP_IDENTITY(GMOCK_PP_INTERNAL_INTERNAL_TAIL _Args) #define GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_C(...) 1 _ #define GMOCK_PP_INTERNAL_IBP_IS_VARIADIC_R_1 1, diff --git a/googlemock/test/gmock-generated-matchers_test.cc b/googlemock/test/gmock-generated-matchers_test.cc index 6c4b300..f3f49a6 100644 --- a/googlemock/test/gmock-generated-matchers_test.cc +++ b/googlemock/test/gmock-generated-matchers_test.cc @@ -41,6 +41,8 @@ #include "gmock/gmock-generated-matchers.h" +#include <array> +#include <iterator> #include <list> #include <map> #include <memory> @@ -51,8 +53,8 @@ #include <vector> #include "gmock/gmock.h" -#include "gtest/gtest.h" #include "gtest/gtest-spi.h" +#include "gtest/gtest.h" namespace { @@ -195,7 +197,7 @@ TEST(ElementsAreTest, ExplainsNonTrivialMatch) { ElementsAre(GreaterThan(1), 0, GreaterThan(2)); const int a[] = { 10, 0, 100 }; - vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); + vector<int> test_vector(std::begin(a), std::end(a)); EXPECT_EQ("whose element #0 matches, which is 9 more than 1,\n" "and whose element #2 matches, which is 98 more than 2", Explain(m, test_vector)); @@ -280,7 +282,7 @@ TEST(ElementsAreTest, MatchesThreeElementsMixedMatchers) { TEST(ElementsAreTest, MatchesTenElementVector) { const int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; - vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); + vector<int> test_vector(std::begin(a), std::end(a)); EXPECT_THAT(test_vector, // The element list can contain values and/or matchers @@ -317,13 +319,10 @@ TEST(ElementsAreTest, DoesNotMatchWrongOrder) { } TEST(ElementsAreTest, WorksForNestedContainer) { - const char* strings[] = { - "Hi", - "world" - }; + constexpr std::array<const char*, 2> strings = {{"Hi", "world"}}; vector<list<char> > nested; - for (size_t i = 0; i < GTEST_ARRAY_SIZE_(strings); i++) { + for (size_t i = 0; i < strings.size(); i++) { nested.push_back(list<char>(strings[i], strings[i] + strlen(strings[i]))); } @@ -335,7 +334,7 @@ TEST(ElementsAreTest, WorksForNestedContainer) { TEST(ElementsAreTest, WorksWithByRefElementMatchers) { int a[] = { 0, 1, 2 }; - vector<int> v(a, a + GTEST_ARRAY_SIZE_(a)); + vector<int> v(std::begin(a), std::end(a)); EXPECT_THAT(v, ElementsAre(Ref(v[0]), Ref(v[1]), Ref(v[2]))); EXPECT_THAT(v, Not(ElementsAre(Ref(v[0]), Ref(v[1]), Ref(a[2])))); @@ -343,7 +342,7 @@ TEST(ElementsAreTest, WorksWithByRefElementMatchers) { TEST(ElementsAreTest, WorksWithContainerPointerUsingPointee) { int a[] = { 0, 1, 2 }; - vector<int> v(a, a + GTEST_ARRAY_SIZE_(a)); + vector<int> v(std::begin(a), std::end(a)); EXPECT_THAT(&v, Pointee(ElementsAre(0, 1, _))); EXPECT_THAT(&v, Not(Pointee(ElementsAre(0, _, 3)))); @@ -440,7 +439,7 @@ TEST(ElementsAreTest, MakesCopyOfArguments) { TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) { const int a[] = { 1, 2, 3 }; - vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); + vector<int> test_vector(std::begin(a), std::end(a)); EXPECT_THAT(test_vector, ElementsAreArray(a)); test_vector[2] = 0; @@ -448,20 +447,20 @@ TEST(ElementsAreArrayTest, CanBeCreatedWithValueArray) { } TEST(ElementsAreArrayTest, CanBeCreatedWithArraySize) { - const char* a[] = { "one", "two", "three" }; + std::array<const char*, 3> a = {{"one", "two", "three"}}; - vector<std::string> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); - EXPECT_THAT(test_vector, ElementsAreArray(a, GTEST_ARRAY_SIZE_(a))); + vector<std::string> test_vector(std::begin(a), std::end(a)); + EXPECT_THAT(test_vector, ElementsAreArray(a.data(), a.size())); - const char** p = a; + const char** p = a.data(); test_vector[0] = "1"; - EXPECT_THAT(test_vector, Not(ElementsAreArray(p, GTEST_ARRAY_SIZE_(a)))); + EXPECT_THAT(test_vector, Not(ElementsAreArray(p, a.size()))); } TEST(ElementsAreArrayTest, CanBeCreatedWithoutArraySize) { const char* a[] = { "one", "two", "three" }; - vector<std::string> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); + vector<std::string> test_vector(std::begin(a), std::end(a)); EXPECT_THAT(test_vector, ElementsAreArray(a)); test_vector[0] = "1"; @@ -484,8 +483,8 @@ TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherArray) { TEST(ElementsAreArrayTest, CanBeCreatedWithVector) { const int a[] = { 1, 2, 3 }; - vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); - const vector<int> expected(a, a + GTEST_ARRAY_SIZE_(a)); + vector<int> test_vector(std::begin(a), std::end(a)); + const vector<int> expected(std::begin(a), std::end(a)); EXPECT_THAT(test_vector, ElementsAreArray(expected)); test_vector.push_back(4); EXPECT_THAT(test_vector, Not(ElementsAreArray(expected))); @@ -530,9 +529,9 @@ TEST(ElementsAreArrayTest, TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) { const int a[] = { 1, 2, 3 }; const Matcher<int> kMatchers[] = { Eq(1), Eq(2), Eq(3) }; - vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); - const vector<Matcher<int> > expected( - kMatchers, kMatchers + GTEST_ARRAY_SIZE_(kMatchers)); + vector<int> test_vector(std::begin(a), std::end(a)); + const vector<Matcher<int>> expected(std::begin(kMatchers), + std::end(kMatchers)); EXPECT_THAT(test_vector, ElementsAreArray(expected)); test_vector.push_back(4); EXPECT_THAT(test_vector, Not(ElementsAreArray(expected))); @@ -540,11 +539,11 @@ TEST(ElementsAreArrayTest, CanBeCreatedWithMatcherVector) { TEST(ElementsAreArrayTest, CanBeCreatedWithIteratorRange) { const int a[] = { 1, 2, 3 }; - const vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); - const vector<int> expected(a, a + GTEST_ARRAY_SIZE_(a)); + const vector<int> test_vector(std::begin(a), std::end(a)); + const vector<int> expected(std::begin(a), std::end(a)); EXPECT_THAT(test_vector, ElementsAreArray(expected.begin(), expected.end())); // Pointers are iterators, too. - EXPECT_THAT(test_vector, ElementsAreArray(a, a + GTEST_ARRAY_SIZE_(a))); + EXPECT_THAT(test_vector, ElementsAreArray(std::begin(a), std::end(a))); // The empty range of NULL pointers should also be okay. int* const null_int = nullptr; EXPECT_THAT(test_vector, Not(ElementsAreArray(null_int, null_int))); @@ -564,8 +563,8 @@ TEST(ElementsAreArrayTest, WorksWithNativeArray) { TEST(ElementsAreArrayTest, SourceLifeSpan) { const int a[] = { 1, 2, 3 }; - vector<int> test_vector(a, a + GTEST_ARRAY_SIZE_(a)); - vector<int> expect(a, a + GTEST_ARRAY_SIZE_(a)); + vector<int> test_vector(std::begin(a), std::end(a)); + vector<int> expect(std::begin(a), std::end(a)); ElementsAreArrayMatcher<int> matcher_maker = ElementsAreArray(expect.begin(), expect.end()); EXPECT_THAT(test_vector, matcher_maker); diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc index 0373526..bc49cb6 100644 --- a/googlemock/test/gmock-matchers_test.cc +++ b/googlemock/test/gmock-matchers_test.cc @@ -41,10 +41,11 @@ #endif #include "gmock/gmock-matchers.h" -#include "gmock/gmock-more-matchers.h" #include <string.h> #include <time.h> + +#include <array> #include <deque> #include <forward_list> #include <functional> @@ -60,9 +61,11 @@ #include <type_traits> #include <utility> #include <vector> + +#include "gmock/gmock-more-matchers.h" #include "gmock/gmock.h" -#include "gtest/gtest.h" #include "gtest/gtest-spi.h" +#include "gtest/gtest.h" namespace testing { namespace gmock_matchers_test { @@ -2054,6 +2057,114 @@ TEST(PairMatchBaseTest, WorksWithMoveOnly) { EXPECT_TRUE(matcher.Matches(pointers)); } +// Tests that IsNan() matches a NaN, with float. +TEST(IsNan, FloatMatchesNan) { + float quiet_nan = std::numeric_limits<float>::quiet_NaN(); + float other_nan = std::nanf("1"); + float real_value = 1.0f; + + Matcher<float> m = IsNan(); + EXPECT_TRUE(m.Matches(quiet_nan)); + EXPECT_TRUE(m.Matches(other_nan)); + EXPECT_FALSE(m.Matches(real_value)); + + Matcher<float&> m_ref = IsNan(); + EXPECT_TRUE(m_ref.Matches(quiet_nan)); + EXPECT_TRUE(m_ref.Matches(other_nan)); + EXPECT_FALSE(m_ref.Matches(real_value)); + + Matcher<const float&> m_cref = IsNan(); + EXPECT_TRUE(m_cref.Matches(quiet_nan)); + EXPECT_TRUE(m_cref.Matches(other_nan)); + EXPECT_FALSE(m_cref.Matches(real_value)); +} + +// Tests that IsNan() matches a NaN, with double. +TEST(IsNan, DoubleMatchesNan) { + double quiet_nan = std::numeric_limits<double>::quiet_NaN(); + double other_nan = std::nan("1"); + double real_value = 1.0; + + Matcher<double> m = IsNan(); + EXPECT_TRUE(m.Matches(quiet_nan)); + EXPECT_TRUE(m.Matches(other_nan)); + EXPECT_FALSE(m.Matches(real_value)); + + Matcher<double&> m_ref = IsNan(); + EXPECT_TRUE(m_ref.Matches(quiet_nan)); + EXPECT_TRUE(m_ref.Matches(other_nan)); + EXPECT_FALSE(m_ref.Matches(real_value)); + + Matcher<const double&> m_cref = IsNan(); + EXPECT_TRUE(m_cref.Matches(quiet_nan)); + EXPECT_TRUE(m_cref.Matches(other_nan)); + EXPECT_FALSE(m_cref.Matches(real_value)); +} + +// Tests that IsNan() matches a NaN, with long double. +TEST(IsNan, LongDoubleMatchesNan) { + long double quiet_nan = std::numeric_limits<long double>::quiet_NaN(); + long double other_nan = std::nan("1"); + long double real_value = 1.0; + + Matcher<long double> m = IsNan(); + EXPECT_TRUE(m.Matches(quiet_nan)); + EXPECT_TRUE(m.Matches(other_nan)); + EXPECT_FALSE(m.Matches(real_value)); + + Matcher<long double&> m_ref = IsNan(); + EXPECT_TRUE(m_ref.Matches(quiet_nan)); + EXPECT_TRUE(m_ref.Matches(other_nan)); + EXPECT_FALSE(m_ref.Matches(real_value)); + + Matcher<const long double&> m_cref = IsNan(); + EXPECT_TRUE(m_cref.Matches(quiet_nan)); + EXPECT_TRUE(m_cref.Matches(other_nan)); + EXPECT_FALSE(m_cref.Matches(real_value)); +} + +// Tests that IsNan() works with Not. +TEST(IsNan, NotMatchesNan) { + Matcher<float> mf = Not(IsNan()); + EXPECT_FALSE(mf.Matches(std::numeric_limits<float>::quiet_NaN())); + EXPECT_FALSE(mf.Matches(std::nanf("1"))); + EXPECT_TRUE(mf.Matches(1.0)); + + Matcher<double> md = Not(IsNan()); + EXPECT_FALSE(md.Matches(std::numeric_limits<double>::quiet_NaN())); + EXPECT_FALSE(md.Matches(std::nan("1"))); + EXPECT_TRUE(md.Matches(1.0)); + + Matcher<long double> mld = Not(IsNan()); + EXPECT_FALSE(mld.Matches(std::numeric_limits<long double>::quiet_NaN())); + EXPECT_FALSE(mld.Matches(std::nanl("1"))); + EXPECT_TRUE(mld.Matches(1.0)); +} + +// Tests that IsNan() can describe itself. +TEST(IsNan, CanDescribeSelf) { + Matcher<float> mf = IsNan(); + EXPECT_EQ("is NaN", Describe(mf)); + + Matcher<double> md = IsNan(); + EXPECT_EQ("is NaN", Describe(md)); + + Matcher<long double> mld = IsNan(); + EXPECT_EQ("is NaN", Describe(mld)); +} + +// Tests that IsNan() can describe itself with Not. +TEST(IsNan, CanDescribeSelfWithNot) { + Matcher<float> mf = Not(IsNan()); + EXPECT_EQ("isn't NaN", Describe(mf)); + + Matcher<double> md = Not(IsNan()); + EXPECT_EQ("isn't NaN", Describe(md)); + + Matcher<long double> mld = Not(IsNan()); + EXPECT_EQ("isn't NaN", Describe(mld)); +} + // Tests that FloatEq() matches a 2-tuple where // FloatEq(first field) matches the second field. TEST(FloatEq2Test, MatchesEqualArguments) { @@ -5093,14 +5204,14 @@ TEST(WhenSortedTest, WorksForStreamlike) { // Streamlike 'container' provides only minimal iterator support. // Its iterators are tagged with input_iterator_tag. const int a[5] = {2, 1, 4, 5, 3}; - Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); + Streamlike<int> s(std::begin(a), std::end(a)); EXPECT_THAT(s, WhenSorted(ElementsAre(1, 2, 3, 4, 5))); EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3)))); } TEST(WhenSortedTest, WorksForVectorConstRefMatcherOnStreamlike) { const int a[] = {2, 1, 4, 5, 3}; - Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); + Streamlike<int> s(std::begin(a), std::end(a)); Matcher<const std::vector<int>&> vector_match = ElementsAre(1, 2, 3, 4, 5); EXPECT_THAT(s, WhenSorted(vector_match)); EXPECT_THAT(s, Not(WhenSorted(ElementsAre(2, 1, 4, 5, 3)))); @@ -5145,7 +5256,7 @@ TEST(IsSupersetOfTest, WorksForEmpty) { TEST(IsSupersetOfTest, WorksForStreamlike) { const int a[5] = {1, 2, 3, 4, 5}; - Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); + Streamlike<int> s(std::begin(a), std::end(a)); vector<int> expected; expected.push_back(1); @@ -5273,7 +5384,7 @@ TEST(IsSubsetOfTest, WorksForEmpty) { TEST(IsSubsetOfTest, WorksForStreamlike) { const int a[5] = {1, 2}; - Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); + Streamlike<int> s(std::begin(a), std::end(a)); vector<int> expected; expected.push_back(1); @@ -5367,14 +5478,14 @@ TEST(IsSubsetOfTest, WorksWithMoveOnly) { TEST(ElemensAreStreamTest, WorksForStreamlike) { const int a[5] = {1, 2, 3, 4, 5}; - Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); + Streamlike<int> s(std::begin(a), std::end(a)); EXPECT_THAT(s, ElementsAre(1, 2, 3, 4, 5)); EXPECT_THAT(s, Not(ElementsAre(2, 1, 4, 5, 3))); } TEST(ElemensAreArrayStreamTest, WorksForStreamlike) { const int a[5] = {1, 2, 3, 4, 5}; - Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); + Streamlike<int> s(std::begin(a), std::end(a)); vector<int> expected; expected.push_back(1); @@ -5421,7 +5532,7 @@ TEST(ElementsAreTest, TakesStlContainer) { TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) { const int a[] = {0, 1, 2, 3, 4}; - std::vector<int> s(a, a + GTEST_ARRAY_SIZE_(a)); + std::vector<int> s(std::begin(a), std::end(a)); do { StringMatchResultListener listener; EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(a), @@ -5432,8 +5543,8 @@ TEST(UnorderedElementsAreArrayTest, SucceedsWhenExpected) { TEST(UnorderedElementsAreArrayTest, VectorBool) { const bool a[] = {0, 1, 0, 1, 1}; const bool b[] = {1, 0, 1, 1, 0}; - std::vector<bool> expected(a, a + GTEST_ARRAY_SIZE_(a)); - std::vector<bool> actual(b, b + GTEST_ARRAY_SIZE_(b)); + std::vector<bool> expected(std::begin(a), std::end(a)); + std::vector<bool> actual(std::begin(b), std::end(b)); StringMatchResultListener listener; EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAreArray(expected), actual, &listener)) << listener.str(); @@ -5444,7 +5555,7 @@ TEST(UnorderedElementsAreArrayTest, WorksForStreamlike) { // Its iterators are tagged with input_iterator_tag, and it has no // size() or empty() methods. const int a[5] = {2, 1, 4, 5, 3}; - Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); + Streamlike<int> s(std::begin(a), std::end(a)); ::std::vector<int> expected; expected.push_back(1); @@ -5527,7 +5638,7 @@ TEST_F(UnorderedElementsAreTest, WorksWithUncopyable) { TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) { const int a[] = {1, 2, 3}; - std::vector<int> s(a, a + GTEST_ARRAY_SIZE_(a)); + std::vector<int> s(std::begin(a), std::end(a)); do { StringMatchResultListener listener; EXPECT_TRUE(ExplainMatchResult(UnorderedElementsAre(1, 2, 3), @@ -5537,7 +5648,7 @@ TEST_F(UnorderedElementsAreTest, SucceedsWhenExpected) { TEST_F(UnorderedElementsAreTest, FailsWhenAnElementMatchesNoMatcher) { const int a[] = {1, 2, 3}; - std::vector<int> s(a, a + GTEST_ARRAY_SIZE_(a)); + std::vector<int> s(std::begin(a), std::end(a)); std::vector<Matcher<int> > mv; mv.push_back(1); mv.push_back(2); @@ -5553,7 +5664,7 @@ TEST_F(UnorderedElementsAreTest, WorksForStreamlike) { // Its iterators are tagged with input_iterator_tag, and it has no // size() or empty() methods. const int a[5] = {2, 1, 4, 5, 3}; - Streamlike<int> s(a, a + GTEST_ARRAY_SIZE_(a)); + Streamlike<int> s(std::begin(a), std::end(a)); EXPECT_THAT(s, UnorderedElementsAre(1, 2, 3, 4, 5)); EXPECT_THAT(s, Not(UnorderedElementsAre(2, 2, 3, 4, 5))); @@ -5869,8 +5980,9 @@ TEST_F(BipartiteNonSquareTest, SimpleBacktracking) { // :.......: // 0 1 2 MatchMatrix g(4, 3); - static const size_t kEdges[][2] = {{0, 2}, {1, 1}, {2, 1}, {3, 0}}; - for (size_t i = 0; i < GTEST_ARRAY_SIZE_(kEdges); ++i) { + constexpr std::array<std::array<size_t, 2>, 4> kEdges = { + {{{0, 2}}, {{1, 1}}, {{2, 1}}, {{3, 0}}}}; + for (size_t i = 0; i < kEdges.size(); ++i) { g.SetEdge(kEdges[i][0], kEdges[i][1], true); } EXPECT_THAT(FindBacktrackingMaxBPM(g), diff --git a/googlemock/test/gmock-pp_test.cc b/googlemock/test/gmock-pp_test.cc index 7387d39..5d1566e 100644 --- a/googlemock/test/gmock-pp_test.cc +++ b/googlemock/test/gmock-pp_test.cc @@ -1,5 +1,10 @@ #include "gmock/internal/gmock-pp.h" +// Used to test MSVC treating __VA_ARGS__ with a comma in it as one value +#define GMOCK_TEST_REPLACE_comma_WITH_COMMA_I_comma , +#define GMOCK_TEST_REPLACE_comma_WITH_COMMA(x) \ + GMOCK_PP_CAT(GMOCK_TEST_REPLACE_comma_WITH_COMMA_I_, x) + // Static assertions. namespace testing { namespace internal { @@ -17,6 +22,11 @@ static_assert(GMOCK_PP_NARG(x, y, z, w) == 4, ""); static_assert(!GMOCK_PP_HAS_COMMA(), ""); static_assert(GMOCK_PP_HAS_COMMA(b, ), ""); static_assert(!GMOCK_PP_HAS_COMMA((, )), ""); +static_assert(GMOCK_PP_HAS_COMMA(GMOCK_TEST_REPLACE_comma_WITH_COMMA(comma)), + ""); +static_assert( + GMOCK_PP_HAS_COMMA(GMOCK_TEST_REPLACE_comma_WITH_COMMA(comma(unrelated))), + ""); static_assert(!GMOCK_PP_IS_EMPTY(, ), ""); static_assert(!GMOCK_PP_IS_EMPTY(a), ""); static_assert(!GMOCK_PP_IS_EMPTY(()), ""); |