Skip to content

Commit 0963e20

Browse files
committed
Force moving double delta logic to local function
1 parent 7d2a927 commit 0963e20

File tree

1 file changed

+47
-16
lines changed

1 file changed

+47
-16
lines changed

src/unity.c

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -894,14 +894,13 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
894894
/*-----------------------------------------------*/
895895
#ifndef UNITY_EXCLUDE_FLOAT
896896
/* Wrap this define in a function with variable types as float or double */
897-
#define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta0, delta1, expected, actual, diff) \
897+
#define UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff) \
898898
if (isinf(expected) && isinf(actual) && (((expected) < 0) == ((actual) < 0))) return 1; \
899899
if (UNITY_NAN_CHECK) return 1; \
900900
(diff) = (actual) - (expected); \
901901
if ((diff) < 0) (diff) = -(diff); \
902-
if ((delta0) < 0) (delta0) = -(delta0); \
903-
if ((delta1) < 0) (delta1) = -(delta1); \
904-
return !(isnan(diff) || isinf(diff) || ((diff) > ((delta0) + (delta1))))
902+
if ((delta) < 0) (delta) = -(delta); \
903+
return !(isnan(diff) || isinf(diff) || ((diff) > (delta)))
905904
/* This first part of this condition will catch any NaN or Infinite values */
906905
#ifndef UNITY_NAN_NOT_EQUAL_NAN
907906
#define UNITY_NAN_CHECK isnan(expected) && isnan(actual)
@@ -923,10 +922,10 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
923922
#endif /* UNITY_EXCLUDE_FLOAT_PRINT */
924923

925924
/*-----------------------------------------------*/
926-
static int UnityFloatsWithin(UNITY_FLOAT delta0, UNITY_FLOAT delta1, UNITY_FLOAT expected, UNITY_FLOAT actual)
925+
static int UnityFloatsWithin(UNITY_FLOAT delta, UNITY_FLOAT expected, UNITY_FLOAT actual)
927926
{
928927
UNITY_FLOAT diff;
929-
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta0, delta1, expected, actual, diff);
928+
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
930929
}
931930

932931
/*-----------------------------------------------*/
@@ -941,6 +940,8 @@ void UnityAssertWithinFloatArray(const UNITY_FLOAT delta,
941940
UNITY_UINT32 elements = num_elements;
942941
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* ptr_expected = expected;
943942
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* ptr_actual = actual;
943+
UNITY_FLOAT in_delta = delta;
944+
UNITY_FLOAT current_element_delta = delta;
944945

945946
RETURN_IF_FAIL_OR_IGNORE;
946947

@@ -963,9 +964,23 @@ void UnityAssertWithinFloatArray(const UNITY_FLOAT delta,
963964
UNITY_FAIL_AND_BAIL;
964965
}
965966

967+
/* fix delta sign if need */
968+
if (in_delta < 0)
969+
{
970+
in_delta = -in_delta;
971+
}
972+
966973
while (elements--)
967974
{
968-
if (!UnityFloatsWithin(delta, *ptr_expected * UNITY_FLOAT_PRECISION, *ptr_expected, *ptr_actual))
975+
current_element_delta = *ptr_expected * UNITY_FLOAT_PRECISION;
976+
977+
if (current_element_delta < 0)
978+
{
979+
/* fix delta sign for correct calculations */
980+
current_element_delta = -current_element_delta;
981+
}
982+
983+
if (!UnityFloatsWithin(in_delta + current_element_delta, *ptr_expected, *ptr_actual))
969984
{
970985
UnityTestResultsFailBegin(lineNumber);
971986
UnityPrint(UnityStrElement);
@@ -992,7 +1007,7 @@ void UnityAssertFloatsWithin(const UNITY_FLOAT delta,
9921007
RETURN_IF_FAIL_OR_IGNORE;
9931008

9941009

995-
if (!UnityFloatsWithin(delta, (UNITY_FLOAT)0, expected, actual))
1010+
if (!UnityFloatsWithin(delta, expected, actual))
9961011
{
9971012
UnityTestResultsFailBegin(lineNumber);
9981013
UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT((UNITY_DOUBLE)expected, (UNITY_DOUBLE)actual);
@@ -1010,7 +1025,7 @@ void UnityAssertFloatsNotWithin(const UNITY_FLOAT delta,
10101025
{
10111026
RETURN_IF_FAIL_OR_IGNORE;
10121027

1013-
if (UnityFloatsWithin(delta, (UNITY_FLOAT)0, expected, actual))
1028+
if (UnityFloatsWithin(delta, expected, actual))
10141029
{
10151030
UnityTestResultsFailBegin(lineNumber);
10161031
UnityPrint(UnityStrExpected);
@@ -1039,7 +1054,7 @@ void UnityAssertGreaterOrLessFloat(const UNITY_FLOAT threshold,
10391054
if (!(actual < threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
10401055
if (!(actual > threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
10411056

1042-
if ((compare & UNITY_EQUAL_TO) && UnityFloatsWithin((UNITY_FLOAT)0, threshold * UNITY_FLOAT_PRECISION, threshold, actual)) { failed = 0; }
1057+
if ((compare & UNITY_EQUAL_TO) && UnityFloatsWithin(threshold * UNITY_FLOAT_PRECISION, threshold, actual)) { failed = 0; }
10431058

10441059
if (failed)
10451060
{
@@ -1123,10 +1138,10 @@ void UnityAssertFloatSpecial(const UNITY_FLOAT actual,
11231138

11241139
/*-----------------------------------------------*/
11251140
#ifndef UNITY_EXCLUDE_DOUBLE
1126-
static int UnityDoublesWithin(UNITY_DOUBLE delta0, UNITY_DOUBLE delta1, UNITY_DOUBLE expected, UNITY_DOUBLE actual)
1141+
static int UnityDoublesWithin(UNITY_DOUBLE delta, UNITY_DOUBLE expected, UNITY_DOUBLE actual)
11271142
{
11281143
UNITY_DOUBLE diff;
1129-
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta0, delta1, expected, actual, diff);
1144+
UNITY_FLOAT_OR_DOUBLE_WITHIN(delta, expected, actual, diff);
11301145
}
11311146

11321147
/*-----------------------------------------------*/
@@ -1141,6 +1156,8 @@ void UnityAssertWithinDoubleArray(const UNITY_DOUBLE delta,
11411156
UNITY_UINT32 elements = num_elements;
11421157
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* ptr_expected = expected;
11431158
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* ptr_actual = actual;
1159+
UNITY_DOUBLE in_delta = delta;
1160+
UNITY_DOUBLE current_element_delta = delta;
11441161

11451162
RETURN_IF_FAIL_OR_IGNORE;
11461163

@@ -1163,9 +1180,23 @@ void UnityAssertWithinDoubleArray(const UNITY_DOUBLE delta,
11631180
UNITY_FAIL_AND_BAIL;
11641181
}
11651182

1183+
/* fix delta sign if need */
1184+
if (in_delta < 0)
1185+
{
1186+
in_delta = -in_delta;
1187+
}
1188+
11661189
while (elements--)
11671190
{
1168-
if (!UnityDoublesWithin(delta, *ptr_expected * UNITY_DOUBLE_PRECISION, *ptr_expected, *ptr_actual))
1191+
current_element_delta = *ptr_expected * UNITY_DOUBLE_PRECISION;
1192+
1193+
if (current_element_delta < 0)
1194+
{
1195+
/* fix delta sign for correct calculations */
1196+
current_element_delta = -current_element_delta;
1197+
}
1198+
1199+
if (!UnityDoublesWithin(in_delta + current_element_delta, *ptr_expected, *ptr_actual))
11691200
{
11701201
UnityTestResultsFailBegin(lineNumber);
11711202
UnityPrint(UnityStrElement);
@@ -1191,7 +1222,7 @@ void UnityAssertDoublesWithin(const UNITY_DOUBLE delta,
11911222
{
11921223
RETURN_IF_FAIL_OR_IGNORE;
11931224

1194-
if (!UnityDoublesWithin(delta, (UNITY_DOUBLE)0, expected, actual))
1225+
if (!UnityDoublesWithin(delta, expected, actual))
11951226
{
11961227
UnityTestResultsFailBegin(lineNumber);
11971228
UNITY_PRINT_EXPECTED_AND_ACTUAL_FLOAT(expected, actual);
@@ -1209,7 +1240,7 @@ void UnityAssertDoublesNotWithin(const UNITY_DOUBLE delta,
12091240
{
12101241
RETURN_IF_FAIL_OR_IGNORE;
12111242

1212-
if (UnityDoublesWithin(delta, (UNITY_DOUBLE)0, expected, actual))
1243+
if (UnityDoublesWithin(delta, expected, actual))
12131244
{
12141245
UnityTestResultsFailBegin(lineNumber);
12151246
UnityPrint(UnityStrExpected);
@@ -1238,7 +1269,7 @@ void UnityAssertGreaterOrLessDouble(const UNITY_DOUBLE threshold,
12381269
if (!(actual < threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
12391270
if (!(actual > threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
12401271

1241-
if ((compare & UNITY_EQUAL_TO) && UnityDoublesWithin((UNITY_DOUBLE)0, threshold * UNITY_DOUBLE_PRECISION, threshold, actual)) { failed = 0; }
1272+
if ((compare & UNITY_EQUAL_TO) && UnityDoublesWithin(threshold * UNITY_DOUBLE_PRECISION, threshold, actual)) { failed = 0; }
12421273

12431274
if (failed)
12441275
{

0 commit comments

Comments
 (0)