Skip to content
Merged
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
37 changes: 36 additions & 1 deletion docs/UnityAssertionsReference.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,19 +420,54 @@ of 7 - 13.

Asserts that the pointers point to the same memory location.

#### `TEST_ASSERT_NOT_EQUAL_PTR (expected, actual)`

Asserts that the pointers DO NOT point to the same memory location. Two NULL
pointers are considered equal and will cause this assertion to fail. A NULL
pointer and a non-NULL pointer are considered not equal and will pass.

#### `TEST_ASSERT_EQUAL_STRING (expected, actual)`

Asserts that the null terminated (`’\0’`)strings are identical. If strings are
Asserts that the null terminated (`’\0’`) strings are identical. If strings are
of different lengths or any portion of the strings before their terminators
differ, the assertion fails. Two NULL strings (i.e. zero length) are considered
equivalent.

#### `TEST_ASSERT_NOT_EQUAL_STRING (expected, actual)`

Asserts that the null terminated (`’\0’`) strings are NOT identical. The
assertion passes if the strings differ in length or content at any point before
their null terminators. A NULL pointer and a non-NULL string are considered not
equal and will pass. Two NULL pointers are considered equal and will fail.

#### `TEST_ASSERT_EQUAL_STRING_LEN (expected, actual, len)`

Asserts that the null terminated (`’\0’`) strings are identical up to the specified length.
It checks only the first `len` characters, not the entire string.

#### `TEST_ASSERT_NOT_EQUAL_STRING_LEN (expected, actual, len)`

Asserts that the null terminated (`’\0’`) strings are NOT identical within the
first `len` characters. The assertion passes if the strings differ at any
position within the specified length. A NULL pointer and a non-NULL string are
considered not equal and will pass. Two NULL pointers are considered equal and
will fail. Specifying a `len` of zero is considered pointless and will fail.

#### `TEST_ASSERT_EQUAL_MEMORY (expected, actual, len)`

Asserts that the contents of the memory specified by the `expected` and `actual`
pointers is identical. The size of the memory blocks in bytes is specified by
the `len` parameter.

#### `TEST_ASSERT_NOT_EQUAL_MEMORY (expected, actual, len)`

Asserts that the contents of the memory specified by the `expected` and `actual`
pointers is NOT identical within the first `len` bytes. The assertion passes if
any byte differs within that range. A NULL pointer and a non-NULL pointer are
considered not equal and will pass. Two NULL pointers (or the same pointer) are
considered equal and will fail. Specifying a `len` of zero is considered
pointless and will fail.

### Arrays

`expected` and `actual` parameters are both arrays. `num_elements` specifies the
Expand Down
173 changes: 173 additions & 0 deletions src/unity.c
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,179 @@ void UnityAssertEqualStringArray(UNITY_INTERNAL_PTR expected,
} while (++j < num_elements);
}

/*-----------------------------------------------*/
void UnityAssertNotEqualString(const char* expected,
const char* actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
UNITY_UINT32 i;

RETURN_IF_FAIL_OR_IGNORE;

/* If both pointers are null or same pointer, they are equal (FAIL) */
if (expected == actual)
{
Unity.CurrentTestFailed = 1;
}
/* If only one is null, they are not equal (PASS) */
else if ((expected == NULL) || (actual == NULL))
{
return;
}
else
{
for (i = 0; expected[i] || actual[i]; i++)
{
if (expected[i] != actual[i])
{
return; /* Strings are different (PASS) */
}
}

/* Strings are equal (FAIL) */
Unity.CurrentTestFailed = 1;
}

if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrExpected);

if (msg)
{
UnityAddMsgIfSpecified(msg);
}
else
{
UnityPrint(" Expected strings to be different");
}

UNITY_FAIL_AND_BAIL;
}
}

/*-----------------------------------------------*/
void UnityAssertNotEqualStringLen(const char* expected,
const char* actual,
const UNITY_UINT32 length,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
UNITY_UINT32 i;

RETURN_IF_FAIL_OR_IGNORE;

if (length == 0)
{
UnityPrintPointlessAndBail();
}

/* If both pointers are same (including both NULL), they are equal (FAIL) */
if (expected == actual)
{
Unity.CurrentTestFailed = 1;
}
/* If only one is null, they are not equal (PASS) */
else if ((expected == NULL) || (actual == NULL))
{
return;
}
else
{
for (i = 0; (i < length) && (expected[i] || actual[i]); i++)
{
if (expected[i] != actual[i])
{
return; /* Strings are different (PASS) */
}
}

/* Strings are equal within length (FAIL) */
Unity.CurrentTestFailed = 1;
}

if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrintExpectedAndActualStringsLen(expected, actual, length);

if (msg)
{
UnityAddMsgIfSpecified(msg);
}
else
{
UnityPrint(" Expected strings to be different");
}

UNITY_FAIL_AND_BAIL;
}
}

/*-----------------------------------------------*/
void UnityAssertNotEqualMemory(UNITY_INTERNAL_PTR expected,
UNITY_INTERNAL_PTR actual,
const UNITY_UINT32 length,
const char* msg,
const UNITY_LINE_TYPE lineNumber)
{
UNITY_PTR_ATTRIBUTE const unsigned char* ptr_exp = (UNITY_PTR_ATTRIBUTE const unsigned char*)expected;
UNITY_PTR_ATTRIBUTE const unsigned char* ptr_act = (UNITY_PTR_ATTRIBUTE const unsigned char*)actual;
UNITY_UINT32 bytes;

RETURN_IF_FAIL_OR_IGNORE;

if (length == 0)
{
UnityPrintPointlessAndBail();
}

if (expected == actual)
{
/* Both are NULL or same pointer (FAIL) */
Unity.CurrentTestFailed = 1;
}
else if (expected == NULL || actual == NULL)
{
/* One is NULL and other is not (PASS) */
return;
}
else
{
bytes = length;
while (bytes--)
{
if (*ptr_exp != *ptr_act)
{
return; /* Memory is different (PASS) */
}
ptr_exp++;
ptr_act++;
}

/* Memory is equal (PASS) */
Unity.CurrentTestFailed = 1;
}

if (Unity.CurrentTestFailed)
{
UnityTestResultsFailBegin(lineNumber);
UnityPrint(UnityStrMemory);

if (msg)
{
UnityAddMsgIfSpecified(msg);
}
else
{
UnityPrint(" Expected memory to be different");
}

UNITY_FAIL_AND_BAIL;
}
}

/*-----------------------------------------------*/
void UnityAssertEqualMemory(UNITY_INTERNAL_PTR expected,
UNITY_INTERNAL_PTR actual,
Expand Down
12 changes: 12 additions & 0 deletions src/unity.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ void verifyTest(void);
#define TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len) UNITY_TEST_ASSERT_EQUAL_STRING_LEN((expected), (actual), (len), __LINE__, NULL)
#define TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) UNITY_TEST_ASSERT_EQUAL_MEMORY((expected), (actual), (len), __LINE__, NULL)

/* Structs and Strings Not Equal To */
#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 @@ -585,6 +591,12 @@ void verifyTest(void);
#define TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message) UNITY_TEST_ASSERT_EQUAL_STRING_LEN((expected), (actual), (len), __LINE__, (message))
#define TEST_ASSERT_EQUAL_MEMORY_MESSAGE(expected, actual, len, message) UNITY_TEST_ASSERT_EQUAL_MEMORY((expected), (actual), (len), __LINE__, (message))

/* Structs and Strings Not Equal To */
#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))

/* Arrays */
#define TEST_ASSERT_EQUAL_INT_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT_ARRAY((expected), (actual), (num_elements), __LINE__, (message))
#define TEST_ASSERT_EQUAL_INT8_ARRAY_MESSAGE(expected, actual, num_elements, message) UNITY_TEST_ASSERT_EQUAL_INT8_ARRAY((expected), (actual), (num_elements), __LINE__, (message))
Expand Down
22 changes: 22 additions & 0 deletions src/unity_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,17 @@ void UnityAssertEqualStringLen(const char* expected,
const char* msg,
const UNITY_LINE_TYPE lineNumber);

void UnityAssertNotEqualString(const char* expected,
const char* actual,
const char* msg,
const UNITY_LINE_TYPE lineNumber);

void UnityAssertNotEqualStringLen(const char* expected,
const char* actual,
const UNITY_UINT32 length,
const char* msg,
const UNITY_LINE_TYPE lineNumber);

void UnityAssertEqualStringArray( UNITY_INTERNAL_PTR expected,
const char** actual,
const UNITY_UINT32 num_elements,
Expand All @@ -754,6 +765,12 @@ void UnityAssertEqualMemory( UNITY_INTERNAL_PTR expected,
const UNITY_LINE_TYPE lineNumber,
const UNITY_FLAGS_T flags);

void UnityAssertNotEqualMemory(UNITY_INTERNAL_PTR expected,
UNITY_INTERNAL_PTR actual,
const UNITY_UINT32 length,
const char* msg,
const UNITY_LINE_TYPE lineNumber);

void UnityAssertIntNumbersWithin(const UNITY_UINT delta,
const UNITY_INT expected,
const UNITY_INT actual,
Expand Down Expand Up @@ -1107,6 +1124,11 @@ int UnityTestMatches(void);
#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)

#define UNITY_TEST_ASSERT_NOT_EQUAL_PTR(expected, actual, line, message) UnityAssertIntGreaterOrLessOrEqualNumber((UNITY_PTR_TO_INT)(expected), (UNITY_PTR_TO_INT)(actual), UNITY_NOT_EQUAL, (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_POINTER)
#define UNITY_TEST_ASSERT_NOT_EQUAL_STRING(expected, actual, line, message) UnityAssertNotEqualString((const char*)(expected), (const char*)(actual), (message), (UNITY_LINE_TYPE)(line))
#define UNITY_TEST_ASSERT_NOT_EQUAL_STRING_LEN(expected, actual, len, line, message) UnityAssertNotEqualStringLen((const char*)(expected), (const char*)(actual), (UNITY_UINT32)(len), (message), (UNITY_LINE_TYPE)(line))
#define UNITY_TEST_ASSERT_NOT_EQUAL_MEMORY(expected, actual, len, line, message) UnityAssertNotEqualMemory((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(len), (message), (UNITY_LINE_TYPE)(line))

#define UNITY_TEST_ASSERT_EQUAL_INT_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT, UNITY_ARRAY_TO_ARRAY)
#define UNITY_TEST_ASSERT_EQUAL_INT8_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT8, UNITY_ARRAY_TO_ARRAY)
#define UNITY_TEST_ASSERT_EQUAL_INT16_ARRAY(expected, actual, num_elements, line, message) UnityAssertEqualIntArray((UNITY_INTERNAL_PTR)(expected), (UNITY_INTERNAL_PTR)(actual), (UNITY_UINT32)(num_elements), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT16, UNITY_ARRAY_TO_ARRAY)
Expand Down
34 changes: 34 additions & 0 deletions test/tests/test_unity_integers.c
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,40 @@ void testNotEqualPointers(void)
VERIFY_FAILS_END
}

void testNotEqualPtr(void)
{
int v0, v1;
int *p0, *p1;

v0 = 19467;
v1 = 18271;
p0 = &v0;
p1 = &v1;

TEST_ASSERT_NOT_EQUAL_PTR(p0, p1);
TEST_ASSERT_NOT_EQUAL_PTR(&v0, &v1);
TEST_ASSERT_NOT_EQUAL_PTR(p0, &v1);
TEST_ASSERT_NOT_EQUAL_PTR(NULL, &v0);
TEST_ASSERT_NOT_EQUAL_PTR(&v0, NULL);
}

void testNotNotEqualPointers(void)
{
int v0;
int *p0 = &v0;

EXPECT_ABORT_BEGIN
TEST_ASSERT_NOT_EQUAL_PTR(p0, p0);
VERIFY_FAILS_END
}

void testNotNotEqualPointers_BothNull(void)
{
EXPECT_ABORT_BEGIN
TEST_ASSERT_NOT_EQUAL_PTR(NULL, NULL);
VERIFY_FAILS_END
}

void testIntsWithinDelta(void)
{
TEST_ASSERT_INT_WITHIN(1, 5000, 5001);
Expand Down
29 changes: 29 additions & 0 deletions test/tests/test_unity_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,32 @@ void testNotEqualMemoryLengthZero(void)
TEST_ASSERT_EQUAL_MEMORY(NULL, NULL, 0);
VERIFY_FAILS_END
}

void testNotEqualMemory(void)
{
TEST_ASSERT_NOT_EQUAL_MEMORY("foo", "bar", 3);
TEST_ASSERT_NOT_EQUAL_MEMORY_MESSAGE("fool", "food", 4, "fool should not equal food");
TEST_ASSERT_NOT_EQUAL_MEMORY(NULL, "food", 4);
TEST_ASSERT_NOT_EQUAL_MEMORY("fool", NULL, 4);
}

void testNotNotEqualMemory1(void)
{
EXPECT_ABORT_BEGIN
TEST_ASSERT_NOT_EQUAL_MEMORY("foo", "foo", 3);
VERIFY_FAILS_END
}

void testNotNotEqualMemory2(void)
{
EXPECT_ABORT_BEGIN
TEST_ASSERT_NOT_EQUAL_MEMORY(NULL, NULL, 1);
VERIFY_FAILS_END
}

void testNotNotEqualMemoryLengthZero(void)
{
EXPECT_ABORT_BEGIN
TEST_ASSERT_NOT_EQUAL_MEMORY("foo", "bar", 0);
VERIFY_FAILS_END
}
Loading
Loading