Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Augment Cuspatial Test Utility to Allow User Specified Abs Error #752

Merged
merged 3 commits into from
Nov 1, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions cpp/tests/utility/vector_equality.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ auto floating_eq(T val)
}
}

template <typename T>
auto floating_near(T val, T abs_error)
{
if constexpr (std::is_same_v<T, float>) {
return ::testing::FloatNear(val, abs_error);
} else {
return ::testing::FloatNear(val, abs_error);
}
}

MATCHER(vec_2d_matcher,
std::string(negation ? "are not" : "are") + " approximately equal vec_2d structs")
{
Expand All @@ -57,6 +67,22 @@ MATCHER(vec_2d_matcher,
return false;
}

MATCHER_P(vec_2d_near_matcher,
abs_error,
std::string(negation ? "are not" : "are") + " approximately equal vec_2d structs")
{
auto lhs = std::get<0>(arg);
auto rhs = std::get<1>(arg);

if (::testing::Matches(floating_near(rhs.x, abs_error))(lhs.x) &&
::testing::Matches(floating_near(rhs.y, abs_error))(lhs.y))
return true;

*result_listener << lhs << " != " << rhs;

return false;
}

MATCHER(float_matcher, std::string(negation ? "are not" : "are") + " approximately equal floats")
{
auto lhs = std::get<0>(arg);
Expand All @@ -69,6 +95,20 @@ MATCHER(float_matcher, std::string(negation ? "are not" : "are") + " approximate
return false;
}

MATCHER_P(float_near_matcher,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The float_matcher is also a "near" matcher, so perhaps float_near_matcher is not the right name. The only different is that we pass abs_error. GTEST ignores the abs error if it is zero. So why not just augment the existing matchers with default abs_error of zero to avoid all the additional code?

Copy link
Contributor Author

@isVoid isVoid Oct 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GTEST ignores the abs error if it is zero.

Hmm, it's not that simple.. Your assumption is that using FloatNear(abs_error==0) is the same as using FloatEq.
Technically they go through different code paths.

FLOAT_NEAR(abs_error==0) will go to this:
https://github.com/google/googletest/blob/3026483ae575e2de942db5e760cf95e973308dd5/googlemock/include/gmock/gmock-matchers.h#L1687

which will not use the ULP near equal check but completely resort to abs_err check.

FLOAT_EQ matcher will use this:
https://github.com/google/googletest/blob/3026483ae575e2de942db5e760cf95e973308dd5/googlemock/include/gmock/gmock-matchers.h#L1706

which will use the ULP near equal check.

While we can default abs_error to -1, the default value where gtest use internally to differentiate, I believe that's an implementation detail and it's in nature an invalid input to the matcher.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If just for naming, we can call it float_eq_by_ulp / float_eq_by_abs_error. As for the user facing API expect_vector_equivalent, I think we should keep it as is and use overloads for user to differentiate.

abs_error,
std::string(negation ? "are not" : "are") + " approximately equal floats")
{
auto lhs = std::get<0>(arg);
auto rhs = std::get<1>(arg);

if (::testing::Matches(floating_near(rhs, abs_error))(lhs)) return true;

*result_listener << std::setprecision(18) << lhs << " != " << rhs;

return false;
}

template <typename T, typename Vector>
thrust::host_vector<T> to_host(Vector const& dvec)
{
Expand Down Expand Up @@ -103,5 +143,28 @@ inline void expect_vector_equivalent(Vector1 const& lhs, Vector2 const& rhs)
}
}

template <typename Vector1, typename Vector2, typename T = typename Vector1::value_type>
inline void expect_vector_equivalent(Vector1 const& lhs, Vector2 const& rhs, T abs_error)
{
static_assert(std::is_same_v<T, typename Vector2::value_type>, "Value type mismatch.");
static_assert(!std::is_integral_v<T>, "Integral types cannot be compared with an error.");

if constexpr (cuspatial::is_vec_2d<T>()) {
EXPECT_THAT(to_host<T>(lhs),
::testing::Pointwise(vec_2d_near_matcher(abs_error), to_host<T>(rhs)));
} else if constexpr (std::is_floating_point_v<T>) {
EXPECT_THAT(to_host<T>(lhs),
::testing::Pointwise(float_near_matcher(abs_error), to_host<T>(rhs)));
} else {
EXPECT_EQ(lhs, rhs);
}
}

#define CUSPATIAL_EXPECT_VECTORS_EQUIVALENT(lhs, rhs, ...) \
do { \
SCOPED_TRACE(" <-- line of failure\n"); \
expect_vector_equivalent(lhs, rhs, ##__VA_ARGS__); \
} while (0)

} // namespace test
} // namespace cuspatial