Zhanyong Wan 0bdccf4aa2 Add a DistanceFrom() matcher for general distance comparison.
We have a bunch of matchers for asserting that a value is near the target value, e.g.
`DoubleNear()` and `FloatNear()`. These matchers only work for specific types (`double` and `float`). They are not flexible enough to support other types that have the notion of a "distance" (e.g. N-dimensional points and vectors, which are commonly used in ML).

In this diff, we generalize the idea to a `DistanceFrom(target, get_distance, m)` matcher that works on arbitrary types that have the "distance" concept (the `get_distance` argument is optional and can be omitted for types that support `-`, and `std::abs()`). What it does:

1. compute the distance between the value and the target using `get_distance(value, target)`; if `get_distance` is omitted, compute the distance as `std::abs(value - target)`.
2. match the distance against matcher `m`; if the match succeeds, the `DistanceFrom()` match succeeds.

Examples:

```
  // 0.5's distance from 0.6 should be <= 0.2.
  EXPECT_THAT(0.5, DistanceFrom(0.6, Le(0.2)));

  Vector2D v1(3.0, 4.0), v2(3.2, 6.0);
  // v1's distance from v2, as computed by EuclideanDistance(v1, v2),
  // should be >= 1.0.
  EXPECT_THAT(v1, DistanceFrom(v2, EuclideanDistance, Ge(1.0)));
```

PiperOrigin-RevId: 734593292
Change-Id: Id6bb7074dc4aa4d8abd78b57ad2426637e590de5
2025-03-07 09:54:00 -08:00
..
BUILD.bazel Split gmock-matchers_test into 4 smaller test #3653 2022-03-23 09:08:04 -07:00
gmock_all_test.cc Split gmock-matchers_test into 4 smaller test #3653 2022-03-23 09:08:04 -07:00
gmock_ex_test.cc Add missing std includes 2023-04-20 14:29:09 -07:00
gmock_leak_test_.cc Use '=default' to define trivial constructor/destructors 2023-04-21 10:41:25 -07:00
gmock_leak_test.py Fix formatting in subset of Python files 2023-01-25 09:14:26 -08:00
gmock_link2_test.cc Running clang-format over all of GoogleTest 2022-03-15 13:42:11 -07:00
gmock_link_test.cc Running clang-format over all of GoogleTest 2022-03-15 13:42:11 -07:00
gmock_link_test.h Run clang-format. 2024-07-23 03:57:23 -07:00
gmock_output_test_.cc Merge branch 'main' into fixes_std_pair_diff 2023-05-01 14:27:38 -04:00
gmock_output_test_golden.txt Merge pull request #4146 from lygstate:fixes_std_pair_diff 2023-05-02 07:52:02 -07:00
gmock_output_test.py Fix formatting in subset of Python files 2023-01-25 09:14:26 -08:00
gmock_stress_test.cc Running clang-format over all of GoogleTest 2022-03-15 13:42:11 -07:00
gmock_test_utils.py Remove pylint disable comments that no longer trigger warnings 2023-01-25 12:35:34 -08:00
gmock_test.cc Use the empty method to check for emptiness 2023-04-21 08:42:42 -07:00
gmock-actions_test.cc Require C++17 2025-02-12 09:44:07 -08:00
gmock-cardinalities_test.cc Use '=default' to define trivial constructor/destructors 2023-04-21 10:41:25 -07:00
gmock-function-mocker_test.cc Automated Code Change 2024-10-04 20:54:14 -07:00
gmock-internal-utils_test.cc Add missing std includes 2023-04-20 14:29:09 -07:00
gmock-matchers_test.h Fix formatting of C++ files 2023-01-24 11:10:09 -08:00
gmock-matchers-arithmetic_test.cc Add a DistanceFrom() matcher for general distance comparison. 2025-03-07 09:54:00 -08:00
gmock-matchers-comparisons_test.cc Enable safe matcher casts from Matcher<const T&> to Matcher<T>. 2025-01-15 09:16:57 -08:00
gmock-matchers-containers_test.cc Use matcher's description in AnyOf when matcher has no explanation. 2024-09-16 14:49:27 -07:00
gmock-matchers-misc_test.cc Enable safe matcher casts from Matcher<const T&> to Matcher<T>. 2025-01-15 09:16:57 -08:00
gmock-more-actions_test.cc Add SaveArgByMove 2025-02-10 10:22:27 -08:00
gmock-nice-strict_test.cc Use '=default' to define trivial constructor/destructors 2023-04-21 10:41:25 -07:00
gmock-port_test.cc Running clang-format over all of GoogleTest 2022-03-15 13:42:11 -07:00
gmock-pp_test.cc Automated Code Change 2024-08-29 08:17:22 -07:00
gmock-pp-string_test.cc Running clang-format over all of GoogleTest 2022-03-15 13:42:11 -07:00
gmock-spec-builders_test.cc Require C++17 2025-02-12 09:44:07 -08:00