-
Notifications
You must be signed in to change notification settings - Fork 34
Support for comparing floats for equality in unit test ==> AssertEqualFloat(expect, actual, abs_delta) ? #244
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
Comments
Hi Rob- thanks for contributing this. At the moment, the assertions are really just some thin wrappers around comparisions I think this will involve some extra macro work; what I have there now was borrowed from work done by @wmacevoy and I'm hoping they can provide a recommendation as to how to proceed here. |
Something like this could work?
|
I think you mean |
From requirements point of view:
AlmostEqual can of course also be used by
Additional - I just realized - I might need
|
Had a good case to test float (almost) equality with my complex class. I put the following Macro at top of .....\Complex\test\unit_test_001.cpp #define assertEqualFloat(arg1, arg2, arg3) assertOp("assertEqualFloat", "expected", fabs(arg1 - arg2), compareLessOrEqual, "<=", "actual", arg3) so that works pretty well. |
Got the assertEqualINF and assertEqualNAN working, not needed yet but quite handy. #define assertEqualFloat(arg1, arg2, arg3) assertOp("assertEqualFloat", "expected", fabs(arg1 - arg2), compareLessOrEqual, "<=", "actual", arg3)
#define assertEqualINF(arg) assertOp("assertEqualINF", "expected", INFINITY, compareEqual, "==", "actual", arg)
#define assertEqualNAN(arg) assertOp("assertEqualNAN", "expected", true, compareEqual, "==", "actual", isnan(arg))
unittest(test_new_operator)
{
assertEqualINF(exp(800));
assertEqualINF(0.0/0.0);
assertEqualINF(42);
assertEqualNAN(INFINITY - INFINITY);
assertEqualNAN(0.0/0.0);
assertEqualNAN(42);
} OUTPUT: (the fail is expected)
|
Maybe the inverted assertNot_INF and assertNot_NAN are also (even more) interesting. |
I should also add docs that say how to use Have you tried a case where the assertion fails? I'm curious what the error message should like since it involves both the expected value and a delta |
@ianfixes |
I put them here https://github.com./Arduino-CI/arduino_ci/blob/master/REFERENCE.md#assertions Note that these changes haven't been released under a new version of the gem (nor of the GitHub action) yet. |
Some of my libraries use float math and errors add up when doing a lot of float math.
e.g https://github.com./RobTillaart/AverageAngle/runs/1543777648?check_suite_focus=true
To test for equality for a float output I typically use the following working construct
however I would prefer a oneliner
output should be something like one of the three
Maybe it should be called
AssertAlmostEqual(...)
Thanks,
The text was updated successfully, but these errors were encountered: