Skip to content
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

First (far from done) implementation for printf style formatting for … #500

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
TEST_ASSERT_NOT_EQUAL_STRING_LEN and TEST_ASSERT_NOT_EQUAL_MEMORY
  • Loading branch information
alariois committed May 26, 2020
commit 327fdd043b6ff172f090287b5c2b61b90972680c
99 changes: 75 additions & 24 deletions src/unity.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ static const char PROGMEM UnityStrDelta[] = " Values Not Within
static const char PROGMEM UnityStrPointless[] = " You Asked Me To Compare Nothing, Which Was Pointless.";
static const char PROGMEM UnityStrNullPointerForExpected[] = " Expected pointer to be NULL";
static const char PROGMEM UnityStrNullPointerForActual[] = " Actual pointer was NULL";
static const char PROGMEM UnityStrBytesOfMemoryNotEqual[] = " bytes of memory not to be equal";
#ifndef UNITY_EXCLUDE_FLOAT
static const char PROGMEM UnityStrNot[] = "Not ";
static const char PROGMEM UnityStrInf[] = "Infinity";
Expand Down Expand Up @@ -636,7 +637,8 @@ static void UnityPrintExpectedAndActualStrings(const char* expected, const char*
/*-----------------------------------------------*/
static void UnityPrintExpectedAndActualStringsLen(const char* expected,
const char* actual,
const UNITY_UINT32 length)
const UNITY_UINT32 length,
UNITY_UINT not)
{
UnityPrint(UnityStrExpected);
if (expected != NULL)
Expand All @@ -649,7 +651,16 @@ static void UnityPrintExpectedAndActualStringsLen(const char* expected,
{
UnityPrint(UnityStrNull);
}
UnityPrint(UnityStrWas);

if(not)
{
UnityPrint(UnityStrNotEqual);
}
else
{
UnityPrint(UnityStrWas);
}

if (actual != NULL)
{
UNITY_OUTPUT_CHAR('\'');
Expand Down Expand Up @@ -1417,8 +1428,8 @@ void UnityAssertEqualStringLen(const char* expected,
const char* msg VA_ARGS_IF_ENABLED)
{
UNITY_UINT32 i;
UNITY_UINT32 not_equal = 0;

(void)not;
RETURN_IF_FAIL_OR_IGNORE;

/* if both pointers not null compare the strings */
Expand All @@ -1428,7 +1439,7 @@ void UnityAssertEqualStringLen(const char* expected,
{
if (expected[i] != actual[i])
{
Unity.CurrentTestFailed = 1;
not_equal = 1;
break;
}
}
Expand All @@ -1437,14 +1448,19 @@ void UnityAssertEqualStringLen(const char* expected,
{ /* handle case of one pointers being null (if both null, test should pass) */
if (expected != actual)
{
Unity.CurrentTestFailed = 1;
not_equal = 1;
}
}

if(not_equal != not)
{
Unity.CurrentTestFailed = 1;
}

if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrintExpectedAndActualStringsLen(expected, actual, length);
UnityPrintExpectedAndActualStringsLen(expected, actual, length, not);
UnityPrintMsgIfSpecifiedAndBail(msg);
}
}
Expand Down Expand Up @@ -1541,22 +1557,41 @@ void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected,
UNITY_PTR_ATTRIBUTE const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE const unsigned char*)actual;
UNITY_UINT32 elements = num_elements;
UNITY_UINT32 bytes;
(void)not;

RETURN_IF_FAIL_OR_IGNORE;

if ((elements == 0) || (length == 0))
{
UnityPrintPointlessAndBail(msg);
}

if (expected == actual)
if(not)
{
return; /* Both are NULL or same pointer */
}
if(expected == NULL && actual == NULL)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
UnityPrint(UnityStrNull);
UnityPrint(UnityStrNotEqual);
UnityPrint(UnityStrNull);
UnityPrintMsgIfSpecifiedAndBail(msg);
}

if (UnityIsOneArrayNull(expected, actual, lineNumber))
if(expected == NULL || actual == NULL)
{
return; /* One of the pointers is NULL so clearly they are not equal */
}
}
else
{
UnityPrintMsgIfSpecifiedAndBail(msg);
if (expected == actual)
{
return; /* Both are NULL or same pointer */
}
if (UnityIsOneArrayNull(expected, actual, lineNumber))
{
UnityPrintMsgIfSpecifiedAndBail(msg);
}
}

while (elements--)
Expand All @@ -1566,20 +1601,27 @@ void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected,
{
if (*ptr_exp != *ptr_act)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrMemory);
if (num_elements > 1)
if(not)
{
return;
}
else
{
UnityPrint(UnityStrElement);
UnityPrintNumberUnsigned(num_elements - elements - 1);
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrMemory);
if (num_elements > 1)
{
UnityPrint(UnityStrElement);
UnityPrintNumberUnsigned(num_elements - elements - 1);
}
UnityPrint(UnityStrByte);
UnityPrintNumberUnsigned(length - bytes - 1);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
UnityPrintMsgIfSpecifiedAndBail(msg);
}
UnityPrint(UnityStrByte);
UnityPrintNumberUnsigned(length - bytes - 1);
UnityPrint(UnityStrExpected);
UnityPrintNumberByStyle(*ptr_exp, UNITY_DISPLAY_STYLE_HEX8);
UnityPrint(UnityStrWas);
UnityPrintNumberByStyle(*ptr_act, UNITY_DISPLAY_STYLE_HEX8);
UnityPrintMsgIfSpecifiedAndBail(msg);
}
ptr_exp++;
ptr_act++;
Expand All @@ -1589,6 +1631,15 @@ void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected,
ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected;
}
}

if(not)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);
UnityPrintNumberUnsigned(length);
UnityPrint(UnityStrBytesOfMemoryNotEqual);
UnityPrintMsgIfSpecifiedAndBail(msg);
}
}

/*-----------------------------------------------*/
Expand Down
8 changes: 7 additions & 1 deletion src/unity.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ void verifyTest(void);
#define TEST_ASSERT_HEX64_ARRAY_WITHIN(delta, expected, actual, num_elements) UNITY_TEST_ASSERT_HEX64_ARRAY_WITHIN((delta), (expected), (actual), num_elements, __LINE__, NULL)
#define TEST_ASSERT_CHAR_ARRAY_WITHIN(delta, expected, actual, num_elements) UNITY_TEST_ASSERT_CHAR_ARRAY_WITHIN((delta), (expected), (actual), num_elements, __LINE__, NULL)


/* Structs and Strings */
#define TEST_ASSERT_EQUAL_PTR(expected, actual) UNITY_TEST_ASSERT_EQUAL_PTR((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_STRING(expected, actual) UNITY_TEST_ASSERT_EQUAL_STRING((expected), (actual), __LINE__, NULL)
Expand All @@ -303,6 +302,9 @@ void verifyTest(void);

#define TEST_ASSERT_NOT_EQUAL_PTR(expected, actual) UNITY_TEST_ASSERT_NOT_EQUAL_PTR((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_NOT_EQUAL_STRING(expected, actual) UNITY_TEST_ASSERT_NOT_EQUAL_STRING((expected), (actual), __LINE__, NULL)
#define TEST_ASSERT_NOT_EQUAL_STRING_LEN(expected, actual, len) UNITY_TEST_ASSERT_NOT_EQUAL_STRING_LEN((expected), (actual), (len), __LINE__, NULL)
#define TEST_ASSERT_NOT_EQUAL_MEMORY(expected, actual, len) UNITY_TEST_ASSERT_NOT_EQUAL_MEMORY((expected), (actual), (len), __LINE__, NULL)

/* Arrays */
#define TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_INT_ARRAY((expected), (actual), (num_elements), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_INT8_ARRAY(expected, actual, num_elements) UNITY_TEST_ASSERT_EQUAL_INT8_ARRAY((expected), (actual), (num_elements), __LINE__, NULL)
Expand Down Expand Up @@ -742,6 +744,8 @@ void verifyTest(void);

#define TEST_ASSERT_NOT_EQUAL_PTR_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_NOT_EQUAL_PTR((expected), (actual), __LINE__, (message))
#define TEST_ASSERT_NOT_EQUAL_STRING_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_NOT_EQUAL_STRING((expected), (actual), __LINE__, (message))
#define TEST_ASSERT_NOT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message) UNITY_TEST_ASSERT_NOT_EQUAL_STRING_LEN((expected), (actual), (len), __LINE__, (message))
#define TEST_ASSERT_NOT_EQUAL_MEMORY_MESSAGE(expected, actual, len, message) UNITY_TEST_ASSERT_NOT_EQUAL_MEMORY((expected), (actual), (len), __LINE__, (message))
#else
#define TEST_ASSERT_EQUAL_PTR_MESSAGE(expected, actual, ...) UNITY_TEST_ASSERT_EQUAL_PTR((expected), (actual), __LINE__, __VA_ARGS__)
#define TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, ...) UNITY_TEST_ASSERT_EQUAL_STRING((expected), (actual), __LINE__, __VA_ARGS__)
Expand All @@ -750,6 +754,8 @@ void verifyTest(void);

#define TEST_ASSERT_NOT_EQUAL_PTR_MESSAGE(expected, actual, ...) UNITY_TEST_ASSERT_NOT_EQUAL_PTR((expected), (actual), __LINE__, __VA_ARGS__)
#define TEST_ASSERT_NOT_EQUAL_STRING_MESSAGE(expected, actual, ...) UNITY_TEST_ASSERT_NOT_EQUAL_STRING((expected), (actual), __LINE__, __VA_ARGS__)
#define TEST_ASSERT_NOT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, ...) UNITY_TEST_ASSERT_NOT_EQUAL_STRING_LEN((expected), (actual), (len), __LINE__, __VA_ARGS__)
#define TEST_ASSERT_NOT_EQUAL_MEMORY_MESSAGE(expected, actual, len, ...) UNITY_TEST_ASSERT_NOT_EQUAL_MEMORY((expected), (actual), (len), __LINE__, __VA_ARGS__)
#endif

/* Arrays */
Expand Down
4 changes: 4 additions & 0 deletions src/unity_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,8 @@ int UnityTestMatches(void);

#define UNITY_TEST_ASSERT_NOT_EQUAL_PTR(expected, actual, line, message) UnityAssertGreaterOrLessOrEqualNumber((UNITY_INT)(UNITY_PTR_TO_INT)(expected), (UNITY_INT)(UNITY_PTR_TO_INT)(actual), UNITY_NOT_EQUAL, (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_POINTER, (message))
#define UNITY_TEST_ASSERT_NOT_EQUAL_STRING(expected, actual, line, message) UnityAssertEqualString((const char*)(expected), (const char*)(actual), (UNITY_LINE_TYPE)(line), (UNITY_UINT)1, (message))
#define UNITY_TEST_ASSERT_NOT_EQUAL_STRING_LEN(expected, actual, len, line, message) UnityAssertEqualStringLen((const char*)(expected), (const char*)(actual), (UNITY_UINT32)(len), (UNITY_LINE_TYPE)(line), (UNITY_UINT)1, (message))
#define UNITY_TEST_ASSERT_NOT_EQUAL_MEMORY(expected, actual, len, line, message) UnityAssertEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(len), 1, (UNITY_LINE_TYPE)(line), UNITY_ARRAY_TO_ARRAY, (UNITY_UINT)1, (message))
#else
#define UNITY_TEST_ASSERT_EQUAL_PTR(expected, actual, line, ...) UnityAssertEqualNumber((UNITY_PTR_TO_INT)(expected), (UNITY_PTR_TO_INT)(actual), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_POINTER, __VA_ARGS__)
#define UNITY_TEST_ASSERT_EQUAL_STRING(expected, actual, line, ...) UnityAssertEqualString((const char*)(expected), (const char*)(actual), (UNITY_LINE_TYPE)(line), (UNITY_UINT)0, __VA_ARGS__)
Expand All @@ -1027,6 +1029,8 @@ int UnityTestMatches(void);

#define UNITY_TEST_ASSERT_NOT_EQUAL_PTR(expected, actual, line, ...) UnityAssertGreaterOrLessOrEqualNumber((UNITY_INT)(UNITY_PTR_TO_INT)(expected), (UNITY_INT)(UNITY_PTR_TO_INT)(actual), UNITY_NOT_EQUAL, (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_POINTER, __VA_ARGS__)
#define UNITY_TEST_ASSERT_NOT_EQUAL_STRING(expected, actual, line, ...) UnityAssertEqualString((const char*)(expected), (const char*)(actual), (UNITY_LINE_TYPE)(line), (UNITY_UINT)1, __VA_ARGS__)
#define UNITY_TEST_ASSERT_NOT_EQUAL_STRING_LEN(expected, actual, len, line, ...) UnityAssertEqualStringLen((const char*)(expected), (const char*)(actual), (UNITY_UINT32)(len), (UNITY_LINE_TYPE)(line), (UNITY_UINT)1, __VA_ARGS__)
#define UNITY_TEST_ASSERT_NOT_EQUAL_MEMORY(expected, actual, len, line, ...) UnityAssertEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(len), 1, (UNITY_LINE_TYPE)(line), UNITY_ARRAY_TO_ARRAY, (UNITY_UINT)1, __VA_ARGS__)
#endif

#ifndef UNITY_INCLUDE_PRINT_FORMATTED
Expand Down