Add assertion for checking empty null-terminated arrays. This is particularly useful for check c strings.

This commit is contained in:
mvandervoord
2020-03-16 15:04:40 -04:00
parent 5e9acef74f
commit bad429428d
4 changed files with 42 additions and 0 deletions

View File

@ -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)

View File

@ -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))

View File

@ -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)

View File

@ -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