Adding possibility for setting relative & absolute

floating difference
This commit is contained in:
AJIOB
2022-11-28 17:47:08 +03:00
parent 0b92f9b960
commit 6567f07f47
2 changed files with 48 additions and 43 deletions

View File

@@ -894,13 +894,14 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
/*-----------------------------------------------*/
#ifndef UNITY_EXCLUDE_FLOAT
/* Wrap this define in a function with variable types as float or double */
#define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff) \
#define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta0, delta1, expected, actual, diff) \
if (isinf(expected) && isinf(actual) && (((expected) < 0) == ((actual) < 0))) return 1; \
if (UNITY_NAN_CHECK) return 1; \
(diff) = (actual) - (expected); \
if ((diff) < 0) (diff) = -(diff); \
if ((delta) < 0) (delta) = -(delta); \
return !(isnan(diff) || isinf(diff) || ((diff) > (delta)))
if ((delta0) < 0) (delta0) = -(delta0); \
if ((delta1) < 0) (delta1) = -(delta1); \
return !(isnan(diff) || isinf(diff) || ((diff) > ((delta0) + (delta1))))
/* This first part of this condition will catch any NaN or Infinite values */
#ifndef UNITY_NAN_NOT_EQUAL_NAN
#define UNITY_NAN_CHECK isnan(expected) && isnan(actual)
@@ -922,19 +923,20 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
#endif /* UNITY_EXCLUDE_FLOAT_PRINT */
/*-----------------------------------------------*/
static int UnityFloatsWithin(UNITY_FLOAT delta, UNITY_FLOAT expected, UNITY_FLOAT actual)
static int UnityFloatsWithin(UNITY_FLOAT delta0, UNITY_FLOAT delta1, UNITY_FLOAT expected, UNITY_FLOAT actual)
{
UNITY_FLOAT diff;
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta0, delta1, expected, actual, diff);
}
/*-----------------------------------------------*/
void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* actual,
const UNITY_UINT32 num_elements,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_FLAGS_T flags)
void UnityAssertWithinFloatArray(const UNITY_FLOAT delta,
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* actual,
const UNITY_UINT32 num_elements,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_FLAGS_T flags)
{
UNITY_UINT32 elements = num_elements;
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* ptr_expected = expected;
@@ -963,7 +965,7 @@ void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
while (elements--)
{
if (!UnityFloatsWithin(*ptr_expected * UNITY_FLOAT_PRECISION, *ptr_expected, *ptr_actual))
if (!UnityFloatsWithin(delta, *ptr_expected * UNITY_FLOAT_PRECISION, *ptr_expected, *ptr_actual))
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrElement);
@@ -990,7 +992,7 @@ void UnityAssertFloatsWithin(const UNITY_FLOAT delta,
RETURN_IF_FAIL_OR_IGNORE;
if (!UnityFloatsWithin(delta, expected, actual))
if (!UnityFloatsWithin(delta, (UNITY_FLOAT)0, expected, actual))
{
UnityTestResultsFailBegin(lineNumber);
UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT((UNITY_DOUBLE)expected, (UNITY_DOUBLE)actual);
@@ -1008,7 +1010,7 @@ void UnityAssertFloatsNotWithin(const UNITY_FLOAT delta,
{
RETURN_IF_FAIL_OR_IGNORE;
if (UnityFloatsWithin(delta, expected, actual))
if (UnityFloatsWithin(delta, (UNITY_FLOAT)0, expected, actual))
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
@@ -1037,7 +1039,7 @@ void UnityAssertGreaterOrLessFloat(const UNITY_FLOAT threshold,
if (!(actual < threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
if (!(actual > threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
if ((compare & UNITY_EQUAL_TO) && UnityFloatsWithin(threshold * UNITY_FLOAT_PRECISION, threshold, actual)) { failed = 0; }
if ((compare & UNITY_EQUAL_TO) && UnityFloatsWithin((UNITY_FLOAT)0, threshold * UNITY_FLOAT_PRECISION, threshold, actual)) { failed = 0; }
if (failed)
{
@@ -1121,19 +1123,20 @@ void UnityAssertFloatSpecial(const UNITY_FLOAT actual,
/*-----------------------------------------------*/
#ifndef UNITY_EXCLUDE_DOUBLE
static int UnityDoublesWithin(UNITY_DOUBLE delta, UNITY_DOUBLE expected, UNITY_DOUBLE actual)
static int UnityDoublesWithin(UNITY_DOUBLE delta0, UNITY_DOUBLE delta1, UNITY_DOUBLE expected, UNITY_DOUBLE actual)
{
UNITY_DOUBLE diff;
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta0, delta1, expected, actual, diff);
}
/*-----------------------------------------------*/
void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expected,
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* actual,
const UNITY_UINT32 num_elements,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_FLAGS_T flags)
void UnityAssertWithinDoubleArray(const UNITY_DOUBLE delta,
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expected,
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* actual,
const UNITY_UINT32 num_elements,
const char* msg,
const UNITY_LINE_TYPE lineNumber,
const UNITY_FLAGS_T flags)
{
UNITY_UINT32 elements = num_elements;
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* ptr_expected = expected;
@@ -1162,7 +1165,7 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expecte
while (elements--)
{
if (!UnityDoublesWithin(*ptr_expected * UNITY_DOUBLE_PRECISION, *ptr_expected, *ptr_actual))
if (!UnityDoublesWithin(delta, *ptr_expected * UNITY_DOUBLE_PRECISION, *ptr_expected, *ptr_actual))
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrElement);
@@ -1188,7 +1191,7 @@ void UnityAssertDoublesWithin(const UNITY_DOUBLE delta,
{
RETURN_IF_FAIL_OR_IGNORE;
if (!UnityDoublesWithin(delta, expected, actual))
if (!UnityDoublesWithin(delta, 0, expected, actual))
{
UnityTestResultsFailBegin(lineNumber);
UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual);
@@ -1206,7 +1209,7 @@ void UnityAssertDoublesNotWithin(const UNITY_DOUBLE delta,
{
RETURN_IF_FAIL_OR_IGNORE;
if (UnityDoublesWithin(delta, expected, actual))
if (UnityDoublesWithin(delta, 0, expected, actual))
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
@@ -1235,7 +1238,7 @@ void UnityAssertGreaterOrLessDouble(const UNITY_DOUBLE threshold,
if (!(actual < threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
if (!(actual > threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
if ((compare & UNITY_EQUAL_TO) && UnityFloatsWithin(threshold * UNITY_DOUBLE_PRECISION, threshold, actual)) { failed = 0; }
if ((compare & UNITY_EQUAL_TO) && UnityDoublesWithin((UNITY_FLOAT)0, threshold * UNITY_DOUBLE_PRECISION, threshold, actual)) { failed = 0; }
if (failed)
{