libpayload: Add mock assert support for unit testing purposes

Some unit tests might require catching assert failures. This patch adds
an assert() variant depending on __TEST__ define passed to unit tests.

Change-Id: I7e4620400f27dbebc57c71bbf2bf9144ca65807f
Signed-off-by: Jakub Czapiga <jacz@semihalf.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/59495
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
This commit is contained in:
Jakub Czapiga 2021-11-18 10:53:34 +00:00 committed by Julius Werner
parent 162083072e
commit c69ea3285e
1 changed files with 13 additions and 0 deletions

View File

@ -29,6 +29,17 @@
#include <stdlib.h>
#include <stdio.h>
#ifdef __TEST__
/* CMocka function redefinition */
void mock_assert(const int result, const char *const expression, const char *const file,
const int line);
#define MOCK_ASSERT(result, expression) mock_assert((result), (expression), __FILE__, __LINE__)
#define assert(statement) MOCK_ASSERT(!!(statement), #statement)
#else
// assert's existence depends on NDEBUG state on _last_ inclusion of assert.h,
// so don't guard this against double-includes.
#ifdef NDEBUG
@ -43,3 +54,5 @@
abort(); \
}
#endif
#endif /* __TEST__ */