mirror of
https://github.com/ThrowTheSwitch/Unity.git
synced 2025-06-27 13:02:47 +08:00
Refactor of IntArray function with switch inside loop, remove repeated code
This commit is contained in:
132
src/unity.c
132
src/unity.c
@ -580,12 +580,13 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
|
|||||||
const UNITY_DISPLAY_STYLE_T style)
|
const UNITY_DISPLAY_STYLE_T style)
|
||||||
{
|
{
|
||||||
UNITY_UINT32 elements = num_elements;
|
UNITY_UINT32 elements = num_elements;
|
||||||
UNITY_INTERNAL_PTR ptr_exp = (UNITY_INTERNAL_PTR)expected;
|
const unsigned int length = style & 0xF;
|
||||||
UNITY_INTERNAL_PTR ptr_act = (UNITY_INTERNAL_PTR)actual;
|
UNITY_INT expect_val = 0;
|
||||||
|
UNITY_INT actual_val = 0;
|
||||||
|
|
||||||
UNITY_SKIP_EXECUTION;
|
UNITY_SKIP_EXECUTION;
|
||||||
|
|
||||||
if (elements == 0)
|
if (num_elements == 0)
|
||||||
{
|
{
|
||||||
UnityPrintPointlessAndBail();
|
UnityPrintPointlessAndBail();
|
||||||
}
|
}
|
||||||
@ -593,95 +594,44 @@ void UnityAssertEqualIntArray(UNITY_INTERNAL_PTR expected,
|
|||||||
if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1)
|
if (UnityCheckArraysForNull((UNITY_INTERNAL_PTR)expected, (UNITY_INTERNAL_PTR)actual, lineNumber, msg) == 1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* If style is UNITY_DISPLAY_STYLE_INT, we'll fall into the default case rather than the INT16 or INT32 (etc) case
|
while (elements--)
|
||||||
* as UNITY_DISPLAY_STYLE_INT includes a flag for UNITY_DISPLAY_RANGE_AUTO, which the width-specific
|
|
||||||
* variants do not. Therefore remove this flag. */
|
|
||||||
switch(style & (UNITY_DISPLAY_STYLE_T)(~UNITY_DISPLAY_RANGE_AUTO))
|
|
||||||
{
|
{
|
||||||
case UNITY_DISPLAY_STYLE_HEX8:
|
switch (length)
|
||||||
case UNITY_DISPLAY_STYLE_INT8:
|
{
|
||||||
case UNITY_DISPLAY_STYLE_UINT8:
|
case 1:
|
||||||
while (elements--)
|
expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)expected;
|
||||||
{
|
actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)actual;
|
||||||
if (*(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)ptr_act)
|
break;
|
||||||
{
|
case 2:
|
||||||
UnityTestResultsFailBegin(lineNumber);
|
expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)expected;
|
||||||
UnityPrint(UnityStrElement);
|
actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)actual;
|
||||||
UnityPrintNumberUnsigned(num_elements - elements - 1);
|
break;
|
||||||
UnityPrint(UnityStrExpected);
|
default: /* length 4 bytes */
|
||||||
UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)ptr_exp, style);
|
expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)expected;
|
||||||
UnityPrint(UnityStrWas);
|
actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)actual;
|
||||||
UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const UNITY_INT8*)ptr_act, style);
|
break;
|
||||||
UnityAddMsgIfSpecified(msg);
|
|
||||||
UNITY_FAIL_AND_BAIL;
|
|
||||||
}
|
|
||||||
ptr_exp = (UNITY_INTERNAL_PTR)((UNITY_PTR)ptr_exp + 1);
|
|
||||||
ptr_act = (UNITY_INTERNAL_PTR)((UNITY_PTR)ptr_act + 1);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case UNITY_DISPLAY_STYLE_HEX16:
|
|
||||||
case UNITY_DISPLAY_STYLE_INT16:
|
|
||||||
case UNITY_DISPLAY_STYLE_UINT16:
|
|
||||||
while (elements--)
|
|
||||||
{
|
|
||||||
if (*(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)ptr_act)
|
|
||||||
{
|
|
||||||
UnityTestResultsFailBegin(lineNumber);
|
|
||||||
UnityPrint(UnityStrElement);
|
|
||||||
UnityPrintNumberUnsigned(num_elements - elements - 1);
|
|
||||||
UnityPrint(UnityStrExpected);
|
|
||||||
UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)ptr_exp, style);
|
|
||||||
UnityPrint(UnityStrWas);
|
|
||||||
UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const UNITY_INT16*)ptr_act, style);
|
|
||||||
UnityAddMsgIfSpecified(msg);
|
|
||||||
UNITY_FAIL_AND_BAIL;
|
|
||||||
}
|
|
||||||
ptr_exp = (UNITY_INTERNAL_PTR)((UNITY_PTR)ptr_exp + 2);
|
|
||||||
ptr_act = (UNITY_INTERNAL_PTR)((UNITY_PTR)ptr_act + 2);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
#ifdef UNITY_SUPPORT_64
|
#ifdef UNITY_SUPPORT_64
|
||||||
case UNITY_DISPLAY_STYLE_HEX64:
|
case 8:
|
||||||
case UNITY_DISPLAY_STYLE_INT64:
|
expect_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)expected;
|
||||||
case UNITY_DISPLAY_STYLE_UINT64:
|
actual_val = *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)actual;
|
||||||
while (elements--)
|
break;
|
||||||
{
|
|
||||||
if (*(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)ptr_act)
|
|
||||||
{
|
|
||||||
UnityTestResultsFailBegin(lineNumber);
|
|
||||||
UnityPrint(UnityStrElement);
|
|
||||||
UnityPrintNumberUnsigned(num_elements - elements - 1);
|
|
||||||
UnityPrint(UnityStrExpected);
|
|
||||||
UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)ptr_exp, style);
|
|
||||||
UnityPrint(UnityStrWas);
|
|
||||||
UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const UNITY_INT64*)ptr_act, style);
|
|
||||||
UnityAddMsgIfSpecified(msg);
|
|
||||||
UNITY_FAIL_AND_BAIL;
|
|
||||||
}
|
|
||||||
ptr_exp = (UNITY_INTERNAL_PTR)((UNITY_PTR)ptr_exp + 8);
|
|
||||||
ptr_act = (UNITY_INTERNAL_PTR)((UNITY_PTR)ptr_act + 8);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
#endif
|
#endif
|
||||||
default:
|
}
|
||||||
while (elements--)
|
|
||||||
{
|
if (expect_val != actual_val)
|
||||||
if (*(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)ptr_exp != *(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)ptr_act)
|
{
|
||||||
{
|
UnityTestResultsFailBegin(lineNumber);
|
||||||
UnityTestResultsFailBegin(lineNumber);
|
UnityPrint(UnityStrElement);
|
||||||
UnityPrint(UnityStrElement);
|
UnityPrintNumberUnsigned(num_elements - elements - 1);
|
||||||
UnityPrintNumberUnsigned(num_elements - elements - 1);
|
UnityPrint(UnityStrExpected);
|
||||||
UnityPrint(UnityStrExpected);
|
UnityPrintNumberByStyle(expect_val, style);
|
||||||
UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)ptr_exp, style);
|
UnityPrint(UnityStrWas);
|
||||||
UnityPrint(UnityStrWas);
|
UnityPrintNumberByStyle(actual_val, style);
|
||||||
UnityPrintNumberByStyle(*(UNITY_PTR_ATTRIBUTE const UNITY_INT32*)ptr_act, style);
|
UnityAddMsgIfSpecified(msg);
|
||||||
UnityAddMsgIfSpecified(msg);
|
UNITY_FAIL_AND_BAIL;
|
||||||
UNITY_FAIL_AND_BAIL;
|
}
|
||||||
}
|
expected = length + (const char*)expected;
|
||||||
ptr_exp = (UNITY_INTERNAL_PTR)((UNITY_PTR)ptr_exp + 4);
|
actual = length + (const char*)actual;
|
||||||
ptr_act = (UNITY_INTERNAL_PTR)((UNITY_PTR)ptr_act + 4);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1194,8 +1144,8 @@ void UnityAssertEqualMemory( UNITY_INTERNAL_PTR expected,
|
|||||||
UnityAddMsgIfSpecified(msg);
|
UnityAddMsgIfSpecified(msg);
|
||||||
UNITY_FAIL_AND_BAIL;
|
UNITY_FAIL_AND_BAIL;
|
||||||
}
|
}
|
||||||
ptr_exp = (UNITY_INTERNAL_PTR)((UNITY_PTR)ptr_exp + 1);
|
ptr_exp++;
|
||||||
ptr_act = (UNITY_INTERNAL_PTR)((UNITY_PTR)ptr_act + 1);
|
ptr_act++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -334,11 +334,10 @@ typedef void (*UnityTestFunction)(void);
|
|||||||
#define UNITY_DISPLAY_RANGE_INT (0x10)
|
#define UNITY_DISPLAY_RANGE_INT (0x10)
|
||||||
#define UNITY_DISPLAY_RANGE_UINT (0x20)
|
#define UNITY_DISPLAY_RANGE_UINT (0x20)
|
||||||
#define UNITY_DISPLAY_RANGE_HEX (0x40)
|
#define UNITY_DISPLAY_RANGE_HEX (0x40)
|
||||||
#define UNITY_DISPLAY_RANGE_AUTO (0x80)
|
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
UNITY_DISPLAY_STYLE_INT = sizeof(int)+ UNITY_DISPLAY_RANGE_INT + UNITY_DISPLAY_RANGE_AUTO,
|
UNITY_DISPLAY_STYLE_INT = sizeof(int)+ UNITY_DISPLAY_RANGE_INT,
|
||||||
UNITY_DISPLAY_STYLE_INT8 = 1 + UNITY_DISPLAY_RANGE_INT,
|
UNITY_DISPLAY_STYLE_INT8 = 1 + UNITY_DISPLAY_RANGE_INT,
|
||||||
UNITY_DISPLAY_STYLE_INT16 = 2 + UNITY_DISPLAY_RANGE_INT,
|
UNITY_DISPLAY_STYLE_INT16 = 2 + UNITY_DISPLAY_RANGE_INT,
|
||||||
UNITY_DISPLAY_STYLE_INT32 = 4 + UNITY_DISPLAY_RANGE_INT,
|
UNITY_DISPLAY_STYLE_INT32 = 4 + UNITY_DISPLAY_RANGE_INT,
|
||||||
@ -346,7 +345,7 @@ UNITY_DISPLAY_STYLE_INT = sizeof(int)+ UNITY_DISPLAY_RANGE_INT + UNITY_DISPLAY_R
|
|||||||
UNITY_DISPLAY_STYLE_INT64 = 8 + UNITY_DISPLAY_RANGE_INT,
|
UNITY_DISPLAY_STYLE_INT64 = 8 + UNITY_DISPLAY_RANGE_INT,
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
UNITY_DISPLAY_STYLE_UINT = sizeof(unsigned) + UNITY_DISPLAY_RANGE_UINT + UNITY_DISPLAY_RANGE_AUTO,
|
UNITY_DISPLAY_STYLE_UINT = sizeof(unsigned) + UNITY_DISPLAY_RANGE_UINT,
|
||||||
UNITY_DISPLAY_STYLE_UINT8 = 1 + UNITY_DISPLAY_RANGE_UINT,
|
UNITY_DISPLAY_STYLE_UINT8 = 1 + UNITY_DISPLAY_RANGE_UINT,
|
||||||
UNITY_DISPLAY_STYLE_UINT16 = 2 + UNITY_DISPLAY_RANGE_UINT,
|
UNITY_DISPLAY_STYLE_UINT16 = 2 + UNITY_DISPLAY_RANGE_UINT,
|
||||||
UNITY_DISPLAY_STYLE_UINT32 = 4 + UNITY_DISPLAY_RANGE_UINT,
|
UNITY_DISPLAY_STYLE_UINT32 = 4 + UNITY_DISPLAY_RANGE_UINT,
|
||||||
|
Reference in New Issue
Block a user