Skip to content

Commit 5a36b19

Browse files
authored
Merge pull request #644 from AJIOB/implement_array_within_check
Adding within API support for float & double arrays
2 parents d826f09 + a35af14 commit 5a36b19

File tree

6 files changed

+196
-33
lines changed

6 files changed

+196
-33
lines changed

docs/UnityAssertionsReference.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,10 +572,16 @@ code generation conventions for CMock.
572572
Asserts that the `actual` value is NOT "close enough to be considered equal" to the
573573
`expected` value.
574574
575+
#### `TEST_ASSERT_FLOAT_ARRAY_WITHIN (delta, expected, actual, num_elements)`
576+
577+
See Array assertion section for details. Note that individual array element
578+
uses user-provided delta plus default comparison delta for checking
579+
and is based on `TEST_ASSERT_FLOAT_WITHIN` comparison.
580+
575581
#### `TEST_ASSERT_EQUAL_FLOAT_ARRAY (expected, actual, num_elements)`
576582
577583
See Array assertion section for details. Note that individual array element
578-
float comparisons are executed using `TEST_ASSERT_EQUAL_FLOAT`.That is, user
584+
float comparisons are executed using `TEST_ASSERT_EQUAL_FLOAT`. That is, user
579585
specified delta comparison values requires a custom-implemented floating point
580586
array assertion.
581587
@@ -667,10 +673,16 @@ code generation conventions for CMock.
667673
Asserts that the `actual` value is NOT "close enough to be considered equal" to the
668674
`expected` value.
669675
676+
#### `TEST_ASSERT_DOUBLE_ARRAY_WITHIN (delta, expected, actual, num_elements)`
677+
678+
See Array assertion section for details. Note that individual array element
679+
uses user-provided delta plus default comparison delta for checking
680+
and is based on `TEST_ASSERT_DOUBLE_WITHIN` comparison.
681+
670682
#### `TEST_ASSERT_EQUAL_DOUBLE_ARRAY (expected, actual, num_elements)`
671683
672684
See Array assertion section for details. Note that individual array element
673-
double comparisons are executed using `TEST_ASSERT_EQUAL_DOUBLE`.That is, user
685+
double comparisons are executed using `TEST_ASSERT_EQUAL_DOUBLE`. That is, user
674686
specified delta comparison values requires a custom implemented double array
675687
assertion.
676688

src/unity.c

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -929,16 +929,19 @@ static int UnityFloatsWithin(UNITY_FLOAT delta, UNITY_FLOAT expected, UNITY_FLOA
929929
}
930930

931931
/*-----------------------------------------------*/
932-
void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
933-
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* actual,
934-
const UNITY_UINT32 num_elements,
935-
const char* msg,
936-
const UNITY_LINE_TYPE lineNumber,
937-
const UNITY_FLAGS_T flags)
932+
void UnityAssertWithinFloatArray(const UNITY_FLOAT delta,
933+
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
934+
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* actual,
935+
const UNITY_UINT32 num_elements,
936+
const char* msg,
937+
const UNITY_LINE_TYPE lineNumber,
938+
const UNITY_FLAGS_T flags)
938939
{
939940
UNITY_UINT32 elements = num_elements;
940941
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* ptr_expected = expected;
941942
UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* ptr_actual = actual;
943+
UNITY_FLOAT in_delta = delta;
944+
UNITY_FLOAT current_element_delta = delta;
942945

943946
RETURN_IF_FAIL_OR_IGNORE;
944947

@@ -951,6 +954,17 @@ void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
951954
#endif
952955
}
953956

957+
if (isinf(in_delta))
958+
{
959+
return; /* Arrays will be force equal with infinite delta */
960+
}
961+
962+
if (isnan(in_delta))
963+
{
964+
/* Delta must be correct number */
965+
UnityPrintPointlessAndBail();
966+
}
967+
954968
if (expected == actual)
955969
{
956970
return; /* Both are NULL or same pointer */
@@ -961,9 +975,23 @@ void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected,
961975
UNITY_FAIL_AND_BAIL;
962976
}
963977

978+
/* fix delta sign if need */
979+
if (in_delta < 0)
980+
{
981+
in_delta = -in_delta;
982+
}
983+
964984
while (elements--)
965985
{
966-
if (!UnityFloatsWithin(*ptr_expected * UNITY_FLOAT_PRECISION, *ptr_expected, *ptr_actual))
986+
current_element_delta = *ptr_expected * UNITY_FLOAT_PRECISION;
987+
988+
if (current_element_delta < 0)
989+
{
990+
/* fix delta sign for correct calculations */
991+
current_element_delta = -current_element_delta;
992+
}
993+
994+
if (!UnityFloatsWithin(in_delta + current_element_delta, *ptr_expected, *ptr_actual))
967995
{
968996
UnityTestResultsFailBegin(lineNumber);
969997
UnityPrint(UnityStrElement);
@@ -1128,16 +1156,19 @@ static int UnityDoublesWithin(UNITY_DOUBLE delta, UNITY_DOUBLE expected, UNITY_D
11281156
}
11291157

11301158
/*-----------------------------------------------*/
1131-
void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expected,
1132-
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* actual,
1133-
const UNITY_UINT32 num_elements,
1134-
const char* msg,
1135-
const UNITY_LINE_TYPE lineNumber,
1136-
const UNITY_FLAGS_T flags)
1159+
void UnityAssertWithinDoubleArray(const UNITY_DOUBLE delta,
1160+
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expected,
1161+
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* actual,
1162+
const UNITY_UINT32 num_elements,
1163+
const char* msg,
1164+
const UNITY_LINE_TYPE lineNumber,
1165+
const UNITY_FLAGS_T flags)
11371166
{
11381167
UNITY_UINT32 elements = num_elements;
11391168
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* ptr_expected = expected;
11401169
UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* ptr_actual = actual;
1170+
UNITY_DOUBLE in_delta = delta;
1171+
UNITY_DOUBLE current_element_delta = delta;
11411172

11421173
RETURN_IF_FAIL_OR_IGNORE;
11431174

@@ -1150,6 +1181,17 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expecte
11501181
#endif
11511182
}
11521183

1184+
if (isinf(in_delta))
1185+
{
1186+
return; /* Arrays will be force equal with infinite delta */
1187+
}
1188+
1189+
if (isnan(in_delta))
1190+
{
1191+
/* Delta must be correct number */
1192+
UnityPrintPointlessAndBail();
1193+
}
1194+
11531195
if (expected == actual)
11541196
{
11551197
return; /* Both are NULL or same pointer */
@@ -1160,9 +1202,23 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expecte
11601202
UNITY_FAIL_AND_BAIL;
11611203
}
11621204

1205+
/* fix delta sign if need */
1206+
if (in_delta < 0)
1207+
{
1208+
in_delta = -in_delta;
1209+
}
1210+
11631211
while (elements--)
11641212
{
1165-
if (!UnityDoublesWithin(*ptr_expected * UNITY_DOUBLE_PRECISION, *ptr_expected, *ptr_actual))
1213+
current_element_delta = *ptr_expected * UNITY_DOUBLE_PRECISION;
1214+
1215+
if (current_element_delta < 0)
1216+
{
1217+
/* fix delta sign for correct calculations */
1218+
current_element_delta = -current_element_delta;
1219+
}
1220+
1221+
if (!UnityDoublesWithin(in_delta + current_element_delta, *ptr_expected, *ptr_actual))
11661222
{
11671223
UnityTestResultsFailBegin(lineNumber);
11681224
UnityPrint(UnityStrElement);
@@ -1235,7 +1291,7 @@ void UnityAssertGreaterOrLessDouble(const UNITY_DOUBLE threshold,
12351291
if (!(actual < threshold) && (compare & UNITY_SMALLER_THAN)) { failed = 1; }
12361292
if (!(actual > threshold) && (compare & UNITY_GREATER_THAN)) { failed = 1; }
12371293

1238-
if ((compare & UNITY_EQUAL_TO) && UnityFloatsWithin(threshold * UNITY_DOUBLE_PRECISION, threshold, actual)) { failed = 0; }
1294+
if ((compare & UNITY_EQUAL_TO) && UnityDoublesWithin(threshold * UNITY_DOUBLE_PRECISION, threshold, actual)) { failed = 0; }
12391295

12401296
if (failed)
12411297
{

src/unity.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ void verifyTest(void);
340340
#define TEST_ASSERT_FLOAT_NOT_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_FLOAT_NOT_WITHIN((delta), (expected), (actual), __LINE__, NULL)
341341
#define TEST_ASSERT_EQUAL_FLOAT(expected, actual) UNITY_TEST_ASSERT_EQUAL_FLOAT((expected), (actual), __LINE__, NULL)
342342
#define TEST_ASSERT_NOT_EQUAL_FLOAT(expected, actual) UNITY_TEST_ASSERT_NOT_EQUAL_FLOAT((expected), (actual), __LINE__, NULL)
343+
#define TEST_ASSERT_FLOAT_ARRAY_WITHIN(delta, expected, actual, num_elements) UNITY_TEST_ASSERT_FLOAT_ARRAY_WITHIN((delta), (expected), (actual), (num_elements), __LINE__, NULL)
343344
#define TEST_ASSERT_EQUAL_FLOAT_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_FLOAT_ARRAY((expected), (actual), (num_elements), __LINE__, NULL)
344345
#define TEST_ASSERT_EACH_EQUAL_FLOAT(expected, actual, num_elements) UNITY_TEST_ASSERT_EACH_EQUAL_FLOAT((expected), (actual), (num_elements), __LINE__, NULL)
345346
#define TEST_ASSERT_GREATER_THAN_FLOAT(threshold, actual) UNITY_TEST_ASSERT_GREATER_THAN_FLOAT((threshold), (actual), __LINE__, NULL)
@@ -360,6 +361,7 @@ void verifyTest(void);
360361
#define TEST_ASSERT_DOUBLE_NOT_WITHIN(delta, expected, actual) UNITY_TEST_ASSERT_DOUBLE_NOT_WITHIN((delta), (expected), (actual), __LINE__, NULL)
361362
#define TEST_ASSERT_EQUAL_DOUBLE(expected, actual) UNITY_TEST_ASSERT_EQUAL_DOUBLE((expected), (actual), __LINE__, NULL)
362363
#define TEST_ASSERT_NOT_EQUAL_DOUBLE(expected, actual) UNITY_TEST_ASSERT_NOT_EQUAL_DOUBLE((expected), (actual), __LINE__, NULL)
364+
#define TEST_ASSERT_DOUBLE_ARRAY_WITHIN(delta, expected, actual, num_elements) UNITY_TEST_ASSERT_DOUBLE_ARRAY_WITHIN((delta), (expected), (actual), (num_elements), __LINE__, NULL)
363365
#define TEST_ASSERT_EQUAL_DOUBLE_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_DOUBLE_ARRAY((expected), (actual), (num_elements), __LINE__, NULL)
364366
#define TEST_ASSERT_EACH_EQUAL_DOUBLE(expected, actual, num_elements) UNITY_TEST_ASSERT_EACH_EQUAL_DOUBLE((expected), (actual), (num_elements), __LINE__, NULL)
365367
#define TEST_ASSERT_GREATER_THAN_DOUBLE(threshold, actual) UNITY_TEST_ASSERT_GREATER_THAN_DOUBLE((threshold), (actual), __LINE__, NULL)
@@ -620,6 +622,7 @@ void verifyTest(void);
620622
#define TEST_ASSERT_FLOAT_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_FLOAT_WITHIN((delta), (expected), (actual), __LINE__, (message))
621623
#define TEST_ASSERT_EQUAL_FLOAT_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_FLOAT((expected), (actual), __LINE__, (message))
622624
#define TEST_ASSERT_NOT_EQUAL_FLOAT_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_NOT_EQUAL_FLOAT((expected), (actual), __LINE__, (message))
625+
#define TEST_ASSERT_FLOAT_ARRAY_WITHIN_MESSAGE(delta, expected, actual, num_elements, message) UNITY_TEST_ASSERT_FLOAT_ARRAY_WITHIN((delta), (expected), (actual), (num_elements), __LINE__, (message))
623626
#define TEST_ASSERT_EQUAL_FLOAT_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_FLOAT_ARRAY((expected), (actual), (num_elements), __LINE__, (message))
624627
#define TEST_ASSERT_EACH_EQUAL_FLOAT_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EACH_EQUAL_FLOAT((expected), (actual), (num_elements), __LINE__, (message))
625628
#define TEST_ASSERT_GREATER_THAN_FLOAT_MESSAGE(threshold, actual, message) UNITY_TEST_ASSERT_GREATER_THAN_FLOAT((threshold), (actual), __LINE__, (message))
@@ -639,6 +642,7 @@ void verifyTest(void);
639642
#define TEST_ASSERT_DOUBLE_WITHIN_MESSAGE(delta, expected, actual, message) UNITY_TEST_ASSERT_DOUBLE_WITHIN((delta), (expected), (actual), __LINE__, (message))
640643
#define TEST_ASSERT_EQUAL_DOUBLE_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_DOUBLE((expected), (actual), __LINE__, (message))
641644
#define TEST_ASSERT_NOT_EQUAL_DOUBLE_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_NOT_EQUAL_DOUBLE((expected), (actual), __LINE__, (message))
645+
#define TEST_ASSERT_DOUBLE_ARRAY_WITHIN_MESSAGE(delta, expected, actual, num_elements, message) UNITY_TEST_ASSERT_DOUBLE_ARRAY_WITHIN((delta), (expected), (actual), (num_elements), __LINE__, (message))
642646
#define TEST_ASSERT_EQUAL_DOUBLE_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_DOUBLE_ARRAY((expected), (actual), (num_elements), __LINE__, (message))
643647
#define TEST_ASSERT_EACH_EQUAL_DOUBLE_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EACH_EQUAL_DOUBLE((expected), (actual), (num_elements), __LINE__, (message))
644648
#define TEST_ASSERT_GREATER_THAN_DOUBLE_MESSAGE(threshold, actual, message) UNITY_TEST_ASSERT_GREATER_THAN_DOUBLE((threshold), (actual), __LINE__, (message))

0 commit comments

Comments
 (0)