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

ZTEST: Add optional float/double comparison support #13569

Closed
microbuilder opened this issue Feb 20, 2019 · 3 comments
Closed

ZTEST: Add optional float/double comparison support #13569

microbuilder opened this issue Feb 20, 2019 · 3 comments
Labels
area: Test Framework Issues related not to a particular test, but to the framework instead Enhancement Changes/Updates/Additions to existing features

Comments

@microbuilder
Copy link
Member

Currently the zassert_* functions in the ztest API are based on integer values, which can be accurately compared (1==1, etc.).

There are no options for single or double precision floating point value comparisons, which are error-prone to compare againist fixed operands (x==0.5f), particularly with single-precision values.

When CONFIG_FLOAT is enabled, options should be included to compare single and double precision values for equality within a specific margin.

In previous projects, I've implemented the following helper functions to work with floating point values in unit tests ...

bool f_is_equal(float a, float b, float epsilon);
bool f_is_at_least(float a, float b);
bool f_is_less_than(float a, float b);
bool f_is_greater_than(float a, float b);
bool f_is_within(float a, float upper, float lower);

... which allows tests like the following code, with control over the degree of prevision involved in the comparison:

#define F_EPSILON5  1E-5
TEST_ASSERT(f_is_equal(xyy.x, 0.44422f, F_EPSILON5, "2900K -> xyY"));

I understand the allergy to float in an embedded context, but if CONFIG_FLOAT has been set to y and is being used in the application or library, shouldn't the option be available to test said floating point values in both single and double-precision widths, using 'safer' value comparisons?

I can continue to use my own code to test these, but it seems reasonable to at least consider adding these helpers to ztest and ztest_assert.h with an appropriate check for CONFIG_FLOAT?

@microbuilder microbuilder added the Enhancement Changes/Updates/Additions to existing features label Feb 20, 2019
@aescolar
Copy link
Member

@microbuilder : Would you consider preparing a PR with this enhancement?

@aescolar aescolar added the area: Test Framework Issues related not to a particular test, but to the framework instead label Feb 20, 2019
@microbuilder
Copy link
Member Author

Sure, I just wanted to check if there was strong resistance to including float support, but I'll put a PR together for this.

@microbuilder
Copy link
Member Author

Closing for lack of activity. Will reopen as PR when ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Test Framework Issues related not to a particular test, but to the framework instead Enhancement Changes/Updates/Additions to existing features
Projects
None yet
Development

No branches or pull requests

2 participants