mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2026-01-23 00:15:58 +01:00
Support option to specify array length of zero to force pointer comparison.
This commit is contained in:
@@ -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.
|
||||
|
||||
30
src/unity.c
30
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();
|
||||
}
|
||||
|
||||
@@ -214,6 +214,8 @@
|
||||
#define UNITY_INTERNAL_PTR UNITY_PTR_ATTRIBUTE const void*
|
||||
#endif
|
||||
|
||||
/* optionally define UNITY_COMPARE_PTRS_ON_ZERO_ARRAY */
|
||||
|
||||
/*-------------------------------------------------------
|
||||
* Float Support
|
||||
*-------------------------------------------------------*/
|
||||
|
||||
@@ -69,3 +69,4 @@ colour: true
|
||||
- UNITY_INCLUDE_DOUBLE
|
||||
- UNITY_SUPPORT_TEST_CASES
|
||||
- UNITY_SUPPORT_64
|
||||
- UNITY_COMPARE_PTRS_ON_ZERO_ARRAY
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user