example: Add an exampele to test exception handling.
This commit is contained in:
parent
faef9724d6
commit
d81fb9099f
|
@ -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})
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue