1 #define UNIT_TESTING 1 2 3 #include <stdarg.h> 4 #include <stddef.h> 5 #include <setjmp.h> 6 #include <cmocka.h> 7 setup_fail(void ** state)8static int setup_fail(void **state) { 9 *state = NULL; 10 11 /* We need to fail in setup */ 12 return -1; 13 } 14 int_test_ignored(void ** state)15static void int_test_ignored(void **state) { 16 /* should not be called */ 17 assert_non_null(*state); 18 } 19 setup_ok(void ** state)20static int setup_ok(void **state) { 21 int *answer; 22 23 answer = malloc(sizeof(int)); 24 if (answer == NULL) { 25 return -1; 26 } 27 *answer = 42; 28 29 *state = answer; 30 31 return 0; 32 } 33 34 /* A test case that does check if an int is equal. */ int_test_success(void ** state)35static void int_test_success(void **state) { 36 int *answer = *state; 37 38 assert_int_equal(*answer, 42); 39 } 40 teardown(void ** state)41static int teardown(void **state) { 42 free(*state); 43 44 return 0; 45 } 46 main(void)47int main(void) { 48 const struct CMUnitTest tests[] = { 49 cmocka_unit_test_setup_teardown(int_test_ignored, setup_fail, teardown), 50 cmocka_unit_test_setup_teardown(int_test_success, setup_ok, teardown), 51 }; 52 53 return cmocka_run_group_tests(tests, NULL, NULL); 54 } 55