diff --git a/docs/UnityAssertionsReference.md b/docs/UnityAssertionsReference.md index 90a3e5fd7..4243a3efe 100644 --- a/docs/UnityAssertionsReference.md +++ b/docs/UnityAssertionsReference.md @@ -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 diff --git a/src/unity.c b/src/unity.c index 84d6729bf..144d9a084 100644 --- a/src/unity.c +++ b/src/unity.c @@ -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, diff --git a/src/unity.h b/src/unity.h index 8f1b8d4da..911a82972 100644 --- a/src/unity.h +++ b/src/unity.h @@ -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) @@ -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)) diff --git a/src/unity_internals.h b/src/unity_internals.h index ec162a174..57450a9fc 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -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, @@ -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, @@ -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) diff --git a/test/tests/test_unity_integers.c b/test/tests/test_unity_integers.c index 495d87da3..5fc42e3e3 100644 --- a/test/tests/test_unity_integers.c +++ b/test/tests/test_unity_integers.c @@ -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); diff --git a/test/tests/test_unity_memory.c b/test/tests/test_unity_memory.c index 7ee48d2bc..b1067ca00 100644 --- a/test/tests/test_unity_memory.c +++ b/test/tests/test_unity_memory.c @@ -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 +} diff --git a/test/tests/test_unity_strings.c b/test/tests/test_unity_strings.c index bb38ecbbd..798f6c672 100644 --- a/test/tests/test_unity_strings.c +++ b/test/tests/test_unity_strings.c @@ -173,6 +173,70 @@ void testNotEqualString_ActualStringIsLonger(void) VERIFY_FAILS_END } +void testNotEqualStrings(void) +{ + TEST_ASSERT_NOT_EQUAL_STRING("foo", "bar"); + TEST_ASSERT_NOT_EQUAL_STRING_MESSAGE("foo", "bar", "foo should not equal bar"); + TEST_ASSERT_NOT_EQUAL_STRING("foo", ""); + TEST_ASSERT_NOT_EQUAL_STRING("", "bar"); + TEST_ASSERT_NOT_EQUAL_STRING("foo", "foo2"); + TEST_ASSERT_NOT_EQUAL_STRING("foo2", "foo"); + TEST_ASSERT_NOT_EQUAL_STRING(NULL, "bar"); + TEST_ASSERT_NOT_EQUAL_STRING("foo", NULL); +} + +void testNotNotEqualString1(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_NOT_EQUAL_STRING("foo", "foo"); + VERIFY_FAILS_END +} + +void testNotNotEqualString2(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_NOT_EQUAL_STRING(NULL, NULL); + VERIFY_FAILS_END +} + +void testNotEqualStringsLen(void) +{ + TEST_ASSERT_NOT_EQUAL_STRING_LEN("foobar", "foobaz", 6); + TEST_ASSERT_NOT_EQUAL_STRING_LEN_MESSAGE("foo", "bar", 3, "foo should not equal bar"); + TEST_ASSERT_NOT_EQUAL_STRING_LEN("foo", "", 3); + TEST_ASSERT_NOT_EQUAL_STRING_LEN("", "bar", 3); + TEST_ASSERT_NOT_EQUAL_STRING_LEN(NULL, "bar", 3); + TEST_ASSERT_NOT_EQUAL_STRING_LEN("foo", NULL, 3); +} + +void testNotNotEqualStringLen1(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_NOT_EQUAL_STRING_LEN("foobar", "foobaz", 5); + VERIFY_FAILS_END +} + +void testNotNotEqualStringLen2(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_NOT_EQUAL_STRING_LEN("foo", "foo", 3); + VERIFY_FAILS_END +} + +void testNotNotEqualStringLen3(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_NOT_EQUAL_STRING_LEN(NULL, NULL, 3); + VERIFY_FAILS_END +} + +void testNotNotEqualStringLenZero(void) +{ + EXPECT_ABORT_BEGIN + TEST_ASSERT_NOT_EQUAL_STRING_LEN("foo", "bar", 0); + VERIFY_FAILS_END +} + void testEqualStringArrays(void) { const char *testStrings[] = { "foo", "boo", "woo", "moo" };