Skip to content

Commit

Permalink
Add assertion for checking empty null-terminated arrays. This is part…
Browse files Browse the repository at this point in the history
…icularly useful for check c strings.
  • Loading branch information
mvandervoord committed Mar 16, 2020
1 parent 5e9acef commit bad4294
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/UnityAssertionsReference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions src/unity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 2 additions & 0 deletions src/unity_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 27 additions & 0 deletions test/tests/testunity.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bad4294

Please sign in to comment.