diff --git a/docs/UnityAssertionsReference.md b/docs/UnityAssertionsReference.md index a395d61..1ef06b3 100644 --- a/docs/UnityAssertionsReference.md +++ b/docs/UnityAssertionsReference.md @@ -236,6 +236,15 @@ conditional statements. ##### `TEST_ASSERT_NOT_NULL (pointer)` +Verify if a pointer is or is not NULL. + +##### `TEST_ASSERT_EMPTY (pointer)` + +##### `TEST_ASSERT_NOT_EMPTY (pointer)` + +Verify if the first element dereferenced from a pointer is or is not zero. This +is particularly useful for checking for empty (or non-empty) null-terminated +C strings, but can be just as easily used for other null-terminated arrays. ### Signed and Unsigned Integers (of all sizes) diff --git a/src/unity.h b/src/unity.h index 7c23e8d..db5815c 100644 --- a/src/unity.h +++ b/src/unity.h @@ -128,6 +128,8 @@ void verifyTest(void); #define TEST_ASSERT_FALSE(condition) UNITY_TEST_ASSERT( !(condition), __LINE__, " Expected FALSE Was TRUE") #define TEST_ASSERT_NULL(pointer) UNITY_TEST_ASSERT_NULL( (pointer), __LINE__, " Expected NULL") #define TEST_ASSERT_NOT_NULL(pointer) UNITY_TEST_ASSERT_NOT_NULL((pointer), __LINE__, " Expected Non-NULL") +#define TEST_ASSERT_EMPTY(pointer) UNITY_TEST_ASSERT_EMPTY( (pointer), __LINE__, " Expected Empty") +#define TEST_ASSERT_NOT_EMPTY(pointer) UNITY_TEST_ASSERT_NOT_EMPTY((pointer), __LINE__, " Expected Non-Empty") /* Integers (of all sizes) */ #define TEST_ASSERT_EQUAL_INT(expected, actual) UNITY_TEST_ASSERT_EQUAL_INT((expected), (actual), __LINE__, NULL) @@ -376,6 +378,8 @@ void verifyTest(void); #define TEST_ASSERT_FALSE_MESSAGE(condition, message) UNITY_TEST_ASSERT( !(condition), __LINE__, (message)) #define TEST_ASSERT_NULL_MESSAGE(pointer, message) UNITY_TEST_ASSERT_NULL( (pointer), __LINE__, (message)) #define TEST_ASSERT_NOT_NULL_MESSAGE(pointer, message) UNITY_TEST_ASSERT_NOT_NULL((pointer), __LINE__, (message)) +#define TEST_ASSERT_EMPTY_MESSAGE(pointer, message) UNITY_TEST_ASSERT_EMPTY( (pointer), __LINE__, (message)) +#define TEST_ASSERT_NOT_EMPTY_MESSAGE(pointer, message) UNITY_TEST_ASSERT_NOT_EMPTY((pointer), __LINE__, (message)) /* Integers (of all sizes) */ #define TEST_ASSERT_EQUAL_INT_MESSAGE(expected, actual, message) UNITY_TEST_ASSERT_EQUAL_INT((expected), (actual), __LINE__, (message)) diff --git a/src/unity_internals.h b/src/unity_internals.h index 6fccf8b..993e094 100644 --- a/src/unity_internals.h +++ b/src/unity_internals.h @@ -761,6 +761,8 @@ int UnityTestMatches(void); #define UNITY_TEST_ASSERT(condition, line, message) if (condition) {} else {UNITY_TEST_FAIL((UNITY_LINE_TYPE)(line), (message));} #define UNITY_TEST_ASSERT_NULL(pointer, line, message) UNITY_TEST_ASSERT(((pointer) == NULL), (UNITY_LINE_TYPE)(line), (message)) #define UNITY_TEST_ASSERT_NOT_NULL(pointer, line, message) UNITY_TEST_ASSERT(((pointer) != NULL), (UNITY_LINE_TYPE)(line), (message)) +#define UNITY_TEST_ASSERT_EMPTY(pointer, line, message) UNITY_TEST_ASSERT(((pointer[0]) == 0), (UNITY_LINE_TYPE)(line), (message)) +#define UNITY_TEST_ASSERT_NOT_EMPTY(pointer, line, message) UNITY_TEST_ASSERT(((pointer[0]) != 0), (UNITY_LINE_TYPE)(line), (message)) #define UNITY_TEST_ASSERT_EQUAL_INT(expected, actual, line, message) UnityAssertEqualNumber((UNITY_INT)(expected), (UNITY_INT)(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT) #define UNITY_TEST_ASSERT_EQUAL_INT8(expected, actual, line, message) UnityAssertEqualNumber((UNITY_INT)(UNITY_INT8 )(expected), (UNITY_INT)(UNITY_INT8 )(actual), (message), (UNITY_LINE_TYPE)(line), UNITY_DISPLAY_STYLE_INT8) diff --git a/test/tests/testunity.c b/test/tests/testunity.c index 0875446..eef84e2 100644 --- a/test/tests/testunity.c +++ b/test/tests/testunity.c @@ -258,6 +258,33 @@ void testNotNullShouldFailIfNULL(void) VERIFY_FAILS_END } +void testIsEmpty(void) +{ + const char* ptr1 = "\0"; + const char* ptr2 = "hello"; + + TEST_ASSERT_EMPTY(ptr1); + TEST_ASSERT_NOT_EMPTY(ptr2); +} + +void testIsEmptyShouldFailIfNot(void) +{ + const char* ptr1 = "hello"; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_EMPTY(ptr1); + VERIFY_FAILS_END +} + +void testNotEmptyShouldFailIfEmpty(void) +{ + const char* ptr1 = "\0"; + + EXPECT_ABORT_BEGIN + TEST_ASSERT_NOT_EMPTY(ptr1); + VERIFY_FAILS_END +} + void testIgnore(void) { EXPECT_ABORT_BEGIN