example: Add an exampele to test exception handling.

This commit is contained in:
Andreas Schneider 2013-12-11 18:52:12 +01:00
parent faef9724d6
commit d81fb9099f
2 changed files with 40 additions and 0 deletions

View File

@ -20,6 +20,18 @@ target_link_libraries(fixture_test ${CMOCKA_SHARED_LIBRARY})
add_test(fixture_test ${CMAKE_CURRENT_BINARY_DIR}/fixture_test)
# segfault test
add_executable(segfault_test segfault_test.c)
target_link_libraries(segfault_test ${CMOCKA_SHARED_LIBRARY})
add_test(segfault_test ${CMAKE_CURRENT_BINARY_DIR}/segfault_test)
set_tests_properties(
segfault_test
PROPERTIES
PASS_REGULAR_EXPRESSION
"Test failed with exception: (Segmentation fault|11)"
)
add_executable(calculator_test calculator.c calculator_test.c)
target_link_libraries(calculator_test ${CMOCKA_SHARED_LIBRARY})

28
example/segfault_test.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>
struct test_segv {
int x;
int y;
};
static void test_segfault_recovery(void **state)
{
struct test_segv *s = NULL;
(void) state; /* unused */
s->x = 1;
}
int main(void) {
const UnitTest tests[] = {
unit_test(test_segfault_recovery),
};
return run_tests(tests);
}