Skip to content

Commit

Permalink
Print index and value of first character that differs in string compa…
Browse files Browse the repository at this point in the history
…risons
  • Loading branch information
jonathangjertsen committed Jan 15, 2024
1 parent b4f6557 commit 1ec1f45
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/unity.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static const char UNITY_PROGMEM UnityStrOrEqual[] = "or equal to
static const char UNITY_PROGMEM UnityStrNotEqual[] = " to be not equal to ";
static const char UNITY_PROGMEM UnityStrElement[] = " Element ";
static const char UNITY_PROGMEM UnityStrByte[] = " Byte ";
static const char UNITY_PROGMEM UnityStrCharacter[] = " Character ";
static const char UNITY_PROGMEM UnityStrMemory[] = " Memory Mismatch.";
static const char UNITY_PROGMEM UnityStrDelta[] = " Values Not Within Delta ";
static const char UNITY_PROGMEM UnityStrPointless[] = " You Asked Me To Compare Nothing, Which Was Pointless.";
Expand Down Expand Up @@ -593,7 +594,21 @@ static void UnityAddMsgIfSpecified(const char* msg)
}

/*-----------------------------------------------*/
static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual)
static void UnityPrintFirstStringDifference(const char *expected, const char *actual, UNITY_UINT32 i_diff)
{
if ((expected != NULL) && (actual != NULL))
{
UnityPrint(UnityStrCharacter);
UnityPrintNumberUnsigned(i_diff);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(expected[i_diff], UNITY_DISPLAY_STYLE_CHAR);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(actual[i_diff], UNITY_DISPLAY_STYLE_CHAR);
}
}

/*-----------------------------------------------*/
static void UnityPrintExpectedAndActualStrings(const char* expected, const char* actual, UNITY_UINT32 i_diff)
{
UnityPrint(UnityStrExpected);
if (expected != NULL)
Expand All @@ -617,12 +632,14 @@ static void UnityPrintExpectedAndActualStrings(const char* expected, const char*
{
UnityPrint(UnityStrNull);
}
UnityPrintFirstStringDifference(expected, actual, i_diff);
}

/*-----------------------------------------------*/
static void UnityPrintExpectedAndActualStringsLen(const char* expected,
const char* actual,
const UNITY_UINT32 length)
UNITY_UINT32 length,
UNITY_UINT32 i_diff)
{
UnityPrint(UnityStrExpected);
if (expected != NULL)
Expand All @@ -646,6 +663,7 @@ static void UnityPrintExpectedAndActualStringsLen(const char* expected,
{
UnityPrint(UnityStrNull);
}
UnityPrintFirstStringDifference(expected, actual, i_diff);
}

/*-----------------------------------------------
Expand Down Expand Up @@ -1593,6 +1611,7 @@ void UnityAssertEqualString(const char* expected,
const UNITY_LINE_TYPE lineNumber)
{
UNITY_UINT32 i;
UNITY_UINT32 i_diff = 0;

RETURN_IF_FAIL_OR_IGNORE;

Expand All @@ -1604,6 +1623,7 @@ void UnityAssertEqualString(const char* expected,
if (expected[i] != actual[i])
{
Unity.CurrentTestFailed = 1;
i_diff = i;
break;
}
}
Expand All @@ -1619,7 +1639,7 @@ void UnityAssertEqualString(const char* expected,
if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrintExpectedAndActualStrings(expected, actual);
UnityPrintExpectedAndActualStrings(expected, actual, i_diff);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
Expand All @@ -1633,6 +1653,7 @@ void UnityAssertEqualStringLen(const char* expected,
const UNITY_LINE_TYPE lineNumber)
{
UNITY_UINT32 i;
UNITY_UINT32 i_diff = 0;

RETURN_IF_FAIL_OR_IGNORE;

Expand All @@ -1644,6 +1665,7 @@ void UnityAssertEqualStringLen(const char* expected,
if (expected[i] != actual[i])
{
Unity.CurrentTestFailed = 1;
i_diff = i;
break;
}
}
Expand All @@ -1659,7 +1681,7 @@ void UnityAssertEqualStringLen(const char* expected,
if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrintExpectedAndActualStringsLen(expected, actual, length);
UnityPrintExpectedAndActualStringsLen(expected, actual, length, i_diff);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
Expand All @@ -1674,6 +1696,7 @@ void UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected,
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 @@ -1721,6 +1744,7 @@ void UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected,
if (expd[i] != act[i])
{
Unity.CurrentTestFailed = 1;
i_diff = i;
break;
}
}
Expand All @@ -1741,7 +1765,7 @@ void UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected,
UnityPrint(UnityStrElement);
UnityPrintNumberUnsigned(j);
}
UnityPrintExpectedAndActualStrings(expd, act);
UnityPrintExpectedAndActualStrings(expd, act, i_diff);
UnityAddMsgIfSpecified(msg);
UNITY_FAIL_AND_BAIL;
}
Expand Down

0 comments on commit 1ec1f45

Please sign in to comment.