Skip to content

DRY string comparison code #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: string-first-char-diff
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 13 additions & 94 deletions src/unity.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,38 +608,10 @@ static void UnityPrintFirstStringDifference(const char *expected, const char *ac
}

/*-----------------------------------------------*/
static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual, UNITY_UINT32 i_diff)
{
UnityPrint(UnityStrExpected);
if (expected != NULL)
{
UNITY_OUTPUT_CHAR('\'');
UnityPrint(expected);
UNITY_OUTPUT_CHAR('\'');
}
else
{
UnityPrint(UnityStrNull);
}
UnityPrint(UnityStrWas);
if (actual != NULL)
{
UNITY_OUTPUT_CHAR('\'');
UnityPrint(actual);
UNITY_OUTPUT_CHAR('\'');
}
else
{
UnityPrint(UnityStrNull);
}
UnityPrintFirstStringDifference(expected, actual, i_diff);
}

/*-----------------------------------------------*/
static void UnityPrintExpectedAndActualStringsLen(const char* expected,
const char* actual,
UNITY_UINT32 length,
UNITY_UINT32 i_diff)
static void UnityPrintExpectedAndActualStrings(const char* expected,
const char* actual,
UNITY_UINT32 length,
UNITY_UINT32 i_diff)
{
UnityPrint(UnityStrExpected);
if (expected != NULL)
Expand Down Expand Up @@ -1605,20 +1577,17 @@ void UnityAssertNumbersArrayWithin(const UNITY_UINT delta,
}

/*-----------------------------------------------*/
void UnityAssertEqualString(const char* expected,
const char* actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
static UNITY_UINT32 UnityEnsureStringsEqual(const char *expected,
const char *actual,
UNITY_UINT32 length)
{
UNITY_UINT32 i;
UNITY_UINT32 i_diff = 0;

RETURN_IF_FAIL_OR_IGNORE;

/* if both pointers not null compare the strings */
if (expected && actual)
{
for (i = 0; expected[i] || actual[i]; i++)
for (i = 0; (i < length) && (expected[i] || actual[i]); i++)
{
if (expected[i] != actual[i])
{
Expand All @@ -1635,14 +1604,7 @@ void UnityAssertEqualString(const char* expected,
Unity.CurrentTestFailed = 1;
}
}

if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrintExpectedAndActualStrings(expected, actual, i_diff);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
return i_diff;
}

/*-----------------------------------------------*/
Expand All @@ -1652,36 +1614,14 @@ void UnityAssertEqualStringLen(const char* expected,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
UNITY_UINT32 i;
UNITY_UINT32 i_diff = 0;

RETURN_IF_FAIL_OR_IGNORE;

/* if both pointers not null compare the strings */
if (expected && actual)
{
for (i = 0; (i < length) && (expected[i] || actual[i]); i++)
{
if (expected[i] != actual[i])
{
Unity.CurrentTestFailed = 1;
i_diff = i;
break;
}
}
}
else
{ /* fail if either null but not if both */
if (expected || actual)
{
Unity.CurrentTestFailed = 1;
}
}
UNITY_UINT32 i_diff = UnityEnsureStringsEqual(expected, actual, length);

if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrintExpectedAndActualStringsLen(expected, actual, length, i_diff);
UnityPrintExpectedAndActualStrings(expected, actual, length, i_diff);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
Expand All @@ -1695,8 +1635,6 @@ void UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected,
const UNITY_LINE_TYPE lineNumber,
const UNITY_FLAGS_T flags)
{
UNITY_UINT32 i = 0;
UNITY_UINT32 i_diff = 0;
UNITY_UINT32 j = 0;
const char* expd = NULL;
const char* act = NULL;
Expand Down Expand Up @@ -1736,26 +1674,7 @@ void UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected,
expd = ((const char* const*)expected)[j];
}

/* if both pointers not null compare the strings */
if (expd && act)
{
for (i = 0; expd[i] || act[i]; i++)
{
if (expd[i] != act[i])
{
Unity.CurrentTestFailed = 1;
i_diff = i;
break;
}
}
}
else
{ /* handle case of one pointers being null (if both null, test should pass) */
if (expd != act)
{
Unity.CurrentTestFailed = 1;
}
}
UNITY_UINT32 i_diff = UnityEnsureStringsEqual(expd, act, 0xFFFFFFFFU);

if (Unity.CurrentTestFailed)
{
Expand All @@ -1765,7 +1684,7 @@ void UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected,
UnityPrint(UnityStrElement);
UnityPrintNumberUnsigned(j);
}
UnityPrintExpectedAndActualStrings(expd, act, i_diff);
UnityPrintExpectedAndActualStrings(expd, act, 0xFFFFFFFFU, i_diff);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/unity_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ int UnityTestMatches(void);


#define UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, line, message) UnityAssertEqualNumber((UNITY_PTR_TO_INT)(expected), (UNITY_PTR_TO_INT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_POINTER)
#define UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, line, message) UnityAssertEqualString((const char*)(expected), (const char*)(actual), (message), (UNITY_LINE_TYPE)(line))
#define UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, line, message) UnityAssertEqualStringLen((const char*)(expected), (const char*)(actual), (UNITY_UINT32)0xFFFFFFFFU, (message), (UNITY_LINE_TYPE)(line))
#define UNITY_TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len, line, message) UnityAssertEqualStringLen((const char*)(expected), (const char*)(actual), (UNITY_UINT32)(len), (message), (UNITY_LINE_TYPE)(line))
#define UNITY_TEST_ASSERT_EQUAL_MEMORY(expected, actual, len, line, message) UnityAssertEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(len), 1, (message), (UNITY_LINE_TYPE)(line), UNITY_ARRAY_TO_ARRAY)

Expand Down