Skip to content

Commit

Permalink
add float conversion tolerance for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Schmluk committed Sep 9, 2024
1 parent 27f2f79 commit fb660ba
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
8 changes: 7 additions & 1 deletion config_utilities/test/include/config_utilities/test/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@

namespace config::test {

bool expectEqual(const YAML::Node& a, const YAML::Node& b);
/**
* @brief Compare two YAML nodes for equality.
* @param a The first node.
* @param b The second node.
* @param epsilon The tolerance for floating point comparisons.
*/
bool expectEqual(const YAML::Node& a, const YAML::Node& b, double epsilon = 0);

class TestLogger : public internal::Logger {
public:
Expand Down
10 changes: 9 additions & 1 deletion config_utilities/test/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@

namespace config::test {

bool expectEqual(const YAML::Node& a, const YAML::Node& b) {
bool expectEqual(const YAML::Node& a, const YAML::Node& b, double epsilon) {
EXPECT_EQ(a.Type(), b.Type());
if (a.Type() != b.Type()) {
return false;
}
switch (a.Type()) {
case YAML::NodeType::Scalar:
if (epsilon > 0.0) {
// Attempt double conversion and comparison.
double a_val, b_val;
if (YAML::convert<double>::decode(a, a_val) && YAML::convert<double>::decode(b, b_val)) {
EXPECT_NEAR(a_val, b_val, epsilon);
return std::abs(a_val - b_val) <= epsilon;
}
}
EXPECT_EQ(a.Scalar(), b.Scalar());
return a.Scalar() == b.Scalar();
case YAML::NodeType::Sequence:
Expand Down
3 changes: 2 additions & 1 deletion config_utilities/test/tests/field_input_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ name: DefaultConfig
min: 0
lower_exclusive: true
)";
expectEqual(info, YAML::Load(expected));
// Epect near equal for floating point values.
expectEqual(info, YAML::Load(expected), 1e-4);
}

} // namespace config::test

0 comments on commit fb660ba

Please sign in to comment.