diff --git a/docs/UnityAssertionsReference.md b/docs/UnityAssertionsReference.md index 90a3e5fd..81cd062f 100644 --- a/docs/UnityAssertionsReference.md +++ b/docs/UnityAssertionsReference.md @@ -420,19 +420,41 @@ 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. + #### `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. + +#### `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 up to the specified length. + #### `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. + ### Arrays `expected` and `actual` parameters are both arrays. `num_elements` specifies the diff --git a/src/unity.c b/src/unity.c index 9e8b3a15..fbebbf77 100644 --- a/src/unity.c +++ b/src/unity.c @@ -1849,6 +1849,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 7c749c2c..9e925fff 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 a66859ae..da235b9c 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -718,6 +718,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, @@ -733,6 +744,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, @@ -1085,6 +1102,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)