diff --git a/docs/UnityConfigurationGuide.md b/docs/UnityConfigurationGuide.md index 71305c3..9ac0cdd 100644 --- a/docs/UnityConfigurationGuide.md +++ b/docs/UnityConfigurationGuide.md @@ -121,6 +121,12 @@ _Example:_ #define UNITY_POINTER_WIDTH 64 // Set UNITY_POINTER_WIDTH to 64-bit ``` +#### `UNITY_COMPARE_PTRS_ON_ZERO_ARRAY` + +Define this to make all array assertions compare pointers instead of contents when a length of zero is specified. When not enabled, +defining a length of zero will always result in a failure and a message warning the user that they have tried to compare empty +arrays. + #### `UNITY_SUPPORT_64` Unity will automatically include 64-bit support if it auto-detects it, or if your `int`, `long`, or pointer widths are greater than 32-bits. diff --git a/src/unity.c b/src/unity.c index 398da66..951ed1c 100644 --- a/src/unity.c +++ b/src/unity.c @@ -796,7 +796,11 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected, if (num_elements == 0) { +#ifdef UNITY_COMPARE_PTRS_ON_ZERO_ARRAY + UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, lineNumber, msg); +#else UnityPrintPointlessAndBail(); +#endif } if (expected == actual) @@ -943,7 +947,11 @@ void UnityAssertEqualFloatArray(UNITY_PTR_ATTRIBUTE const UNITY_FLOAT* expected, if (elements == 0) { +#ifdef UNITY_COMPARE_PTRS_ON_ZERO_ARRAY + UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, lineNumber, msg); +#else UnityPrintPointlessAndBail(); +#endif } if (expected == actual) @@ -1084,7 +1092,11 @@ void UnityAssertEqualDoubleArray(UNITY_PTR_ATTRIBUTE const UNITY_DOUBLE* expecte if (elements == 0) { +#ifdef UNITY_COMPARE_PTRS_ON_ZERO_ARRAY + UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, lineNumber, msg); +#else UnityPrintPointlessAndBail(); +#endif } if (expected == actual) @@ -1265,7 +1277,11 @@ void UnityAssertNumbersArrayWithin(const UNITY_UINT delta, if (num_elements == 0) { +#ifdef UNITY_COMPARE_PTRS_ON_ZERO_ARRAY + UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, lineNumber, msg); +#else UnityPrintPointlessAndBail(); +#endif } if (expected == actual) @@ -1504,7 +1520,11 @@ void UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected, /* if no elements, it's an error */ if (num_elements == 0) { +#ifdef UNITY_COMPARE_PTRS_ON_ZERO_ARRAY + UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, lineNumber, msg); +#else UnityPrintPointlessAndBail(); +#endif } if ((const void*)expected == (const void*)actual) @@ -1581,7 +1601,15 @@ void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected, RETURN_IF_FAIL_OR_IGNORE; - if ((elements == 0) || (length == 0)) + if (elements == 0) + { +#ifdef UNITY_COMPARE_PTRS_ON_ZERO_ARRAY + UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, lineNumber, msg); +#else + UnityPrintPointlessAndBail(); +#endif + } + if (length == 0) { UnityPrintPointlessAndBail(); } diff --git a/src/unity_internals.h b/src/unity_internals.h index 819164e..8bc783b 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -214,6 +214,8 @@ #define UNITY_INTERNAL_PTR UNITY_PTR_ATTRIBUTE const void* #endif +/* optionally define UNITY_COMPARE_PTRS_ON_ZERO_ARRAY */ + /*------------------------------------------------------- * Float Support *-------------------------------------------------------*/ diff --git a/test/targets/clang_strict.yml b/test/targets/clang_strict.yml index 04d5680..f124e8f 100644 --- a/test/targets/clang_strict.yml +++ b/test/targets/clang_strict.yml @@ -69,3 +69,4 @@ colour: true - UNITY_INCLUDE_DOUBLE - UNITY_SUPPORT_TEST_CASES - UNITY_SUPPORT_64 + - UNITY_COMPARE_PTRS_ON_ZERO_ARRAY diff --git a/test/tests/test_unity_arrays.c b/test/tests/test_unity_arrays.c index 8d5bb47..5984883 100644 --- a/test/tests/test_unity_arrays.c +++ b/test/tests/test_unity_arrays.c @@ -2908,3 +2908,33 @@ void testNotEqualInt64Arrays(void) VERIFY_FAILS_END #endif } + +void testVerifyIntPassingPointerComparisonOnZeroLengthArray(void) +{ + int a[] = { 1 }; + +#ifndef UNITY_COMPARE_PTRS_ON_ZERO_ARRAY + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_INT_ARRAY(a, a, 0); + VERIFY_FAILS_END +#else + + TEST_ASSERT_EQUAL_INT_ARRAY(a, a, 0); +#endif +} + +void testVerifyIntFailingPointerComparisonOnZeroLengthArray(void) +{ + int a[] = { 1 }; + int b[] = { 1 }; + +#ifndef UNITY_COMPARE_PTRS_ON_ZERO_ARRAY + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_INT_ARRAY(a, b, 0); + VERIFY_FAILS_END +#else + EXPECT_ABORT_BEGIN + TEST_ASSERT_EQUAL_INT_ARRAY(a, b, 0); + VERIFY_FAILS_END +#endif +}