26 #ifdef HAVE_INTTYPES_H 57 #ifdef CMOCKA_PLATFORM_INCLUDE 58 # include "cmocka_platform.h" 65 #define MALLOC_GUARD_SIZE 16 67 #define MALLOC_GUARD_PATTERN 0xEF 69 #define MALLOC_ALLOC_PATTERN 0xBA 70 #define MALLOC_FREE_PATTERN 0xCD 72 #define MALLOC_ALIGNMENT sizeof(size_t) 75 #define SOURCE_LOCATION_FORMAT "%s:%u" 77 #if defined(HAVE_GCC_THREAD_LOCAL_STORAGE) 78 # define CMOCKA_THREAD __thread 79 #elif defined(HAVE_MSVC_THREAD_LOCAL_STORAGE) 80 # define CMOCKA_THREAD __declspec(thread) 82 # define CMOCKA_THREAD 85 #ifdef HAVE_CLOCK_GETTIME_REALTIME 86 #define CMOCKA_CLOCK_GETTIME(clock_id, ts) clock_gettime((clock_id), (ts)) 88 #define CMOCKA_CLOCK_GETTIME(clock_id, ts) 94 #ifdef HAVE_SIGLONGJMP 95 # define cm_jmp_buf sigjmp_buf 96 # define cm_setjmp(env) sigsetjmp(env, 1) 97 # define cm_longjmp(env, val) siglongjmp(env, val) 99 # define cm_jmp_buf jmp_buf 100 # define cm_setjmp(env) setjmp(env) 101 # define cm_longjmp(env, val) longjmp(env, val) 109 #define declare_initialize_value_pointer_pointer(name, ptr) \ 110 ValuePointer name ; \ 112 name.x.pointer = (void*)(ptr) 118 #define declare_initialize_value_pointer_value(name, val) \ 119 ValuePointer name ; \ 123 #define cast_largest_integral_type_to_pointer( \ 124 pointer_type, largest_integral_type) \ 125 ((pointer_type)((ValuePointer*)&(largest_integral_type))->x.pointer) 131 #if defined(WORDS_BIGENDIAN) && (WORDS_SIZEOF_VOID_P == 4) 132 unsigned int padding;
184 const char *
function;
218 void *
const cleanup_value_data);
221 void *
const cleanup_value_data);
229 void *
const cleanup_value_data);
232 ListNode *
const symbol_map_head,
const char *
const symbol_names[],
233 const size_t number_of_symbol_names,
const void*
value,
const int count);
235 ListNode *
const symbol_map_head,
const char *
const symbol_names[],
236 const size_t number_of_symbol_names,
void **output);
239 const void *
value,
void *cleanup_value_data);
241 const size_t number_of_symbol_names);
244 const char *
const error_message);
247 const ListNode *
const map_head,
const char *
const error_message,
248 const size_t number_of_symbol_names);
306 static const int exception_signals[] = {
319 typedef void (*SignalFunction)(
int signal);
320 static SignalFunction default_signal_functions[
334 #define EXCEPTION_CODE_INFO(exception_code) {exception_code, #exception_code} 380 const char *abort_test = getenv(
"CMOCKA_TEST_ABORT");
382 if (abort_test != NULL && abort_test[0] ==
'1') {
385 }
else if (global_running_test) {
387 }
else if (quit_application) {
392 void _skip(
const char *
const file,
const int line)
395 global_skip_test = 1;
402 location->
file = NULL;
410 return location->
file && location->
line;
419 location->
file = file;
420 location->
line = line;
437 int error_occurred = 0;
441 &global_function_result_map_head,
442 "%s() has remaining non-returned values.\n", 1)) {
448 &global_function_parameter_map_head,
449 "%s parameter still has values that haven't been checked.\n", 2)) {
455 "%s function was expected to be called but was not not.\n")) {
458 if (error_occurred) {
492 const int refcount) {
506 new_node->
next = head;
509 head->
prev = new_node;
517 void *
const cleanup_value_data) {
522 cleanup_value(node->
value, cleanup_value_data);
531 void *
const cleanup_value_data) {
545 void *
const cleanup_value_data) {
557 return head->
next == head;
569 for (current = head->
next; current != head; current = current->
next) {
570 if (equal_func(current->
value, value)) {
585 target_node = head->
next;
586 *output = target_node;
593 (void)cleanup_value_data;
601 void *cleanup_value_data) {
618 (
const char*)symbol);
626 const char *
const symbol_names[],
627 const size_t number_of_symbol_names,
628 const void*
value,
const int refcount) {
629 const char* symbol_name;
635 symbol_name = symbol_names[0];
643 target_node =
list_add_value(symbol_map_head, new_symbol_map_value,
648 if (number_of_symbol_names == 1) {
653 &symbol_names[1], number_of_symbol_names - 1, value,
666 ListNode *
const head,
const char *
const symbol_names[],
667 const size_t number_of_symbol_names,
void **output) {
668 const char* symbol_name;
674 symbol_name = symbol_names[0];
679 int return_value = 0;
686 if (number_of_symbol_names == 1) {
688 return_value =
list_first(child_list, &value_node);
690 *output = (
void*) value_node->
value;
691 return_value = value_node->
refcount;
692 if (value_node->
refcount - 1 == 0) {
694 }
else if (value_node->
refcount > -2) {
699 child_list, &symbol_names[1], number_of_symbol_names - 1,
724 for (current = map_head->
next, next = current->
next;
726 current = next, next = current->
next) {
739 const size_t number_of_symbol_names) {
743 current = map_head->
next;
744 while (current != map_head) {
752 if (number_of_symbol_names == 1) {
760 number_of_symbol_names - 1);
772 const char *
const error_message)
775 int leftover_count = 0;
778 for (child_node = head->
next; child_node != head;
779 child_node = child_node->
next, ++leftover_count) {
784 ": note: remaining item was declared here\n",
788 return leftover_count;
796 const ListNode *
const map_head,
const char *
const error_message,
797 const size_t number_of_symbol_names) {
799 int symbols_with_leftover_values = 0;
803 for (current = map_head->
next; current != map_head;
804 current = current->
next) {
812 if (number_of_symbol_names == 1) {
816 for (child_node = child_list->
next; child_node != child_list;
817 child_node = child_node->
next) {
821 ": note: remaining item was declared here\n",
827 number_of_symbol_names - 1);
829 symbols_with_leftover_values ++;
832 return symbols_with_leftover_values;
841 &
function, 1, &result);
845 global_last_mock_value_location = symbol->
location;
852 "to mock function %s\n", file, line,
function);
855 ": note: Previously returned mock value was declared here\n",
856 global_last_mock_value_location.file,
857 global_last_mock_value_location.line);
859 cm_print_error(
"There were no previously returned mock values for " 869 const char *
const file,
877 rc =
list_first(&global_call_ordering_head, &value_node);
878 first_value_node = value_node;
883 cmp = strcmp(expected_call->
function,
function);
890 value_node = value_node->
next;
893 cmp = strcmp(expected_call->
function,
function);
896 value_node != first_value_node->
prev) {
897 value_node = value_node->
next;
898 if (value_node == NULL) {
902 if (expected_call == NULL) {
905 cmp = strcmp(expected_call->
function,
function);
908 if (value_node == first_value_node->
prev) {
910 ": error: No expected mock calls matching " 911 "called() invocation in %s",
925 ": error: Expected call to %s but received called() " 934 ": error: No mock calls expected but called() was " 943 void _will_return(
const char *
const function_name,
const char *
const file,
952 return_value, count);
963 const char*
const function,
const char*
const parameter,
964 const char*
const file,
const int line,
970 const char* symbols[] = {
function, parameter};
986 const char *
const function_name,
987 const char *
const file,
1000 ordering->
function = function_name;
1009 const int equal = left == right;
1022 const int not_equal = left != right;
1041 int succeeded = invert;
1045 const size_t size_of_set = check_integer_set->
size_of_set;
1047 for (i = 0; i < size_of_set; i++) {
1048 if (
set[i] == value) {
1051 succeeded = !succeeded;
1059 invert ?
"" :
"not ");
1060 for (i = 0; i < size_of_set; i++) {
1077 if (value >= range_min && value <= range_max) {
1081 value, range_min, range_max);
1094 if (value < range_min || value > range_max) {
1098 value, range_min, range_max);
1109 const char *
const left,
const char *
const right) {
1110 if (strcmp(left, right) == 0) {
1124 const char *
const left,
const char *
const right) {
1125 if (strcmp(left, right) != 0) {
1138 const size_t size) {
1139 int differences = 0;
1141 for (i = 0; i < size; i++) {
1142 const char l = a[i];
1143 const char r = b[i];
1152 differences, (
void *)a, (
void *)b);
1165 const char*
const a,
const char*
const b,
const size_t size) {
1168 for (i = 0; i < size; i++) {
1169 const char l = a[i];
1170 const char r = b[i];
1177 same, (
void *)a, (
void *)b);
1189 check_value_data), 0);
1198 check_value_data), 1);
1205 const char*
const function,
const char*
const parameter,
1206 const char*
const file,
const int line,
1211 (
sizeof(values[0]) * number_of_values));
1213 check_integer_set + 1);
1217 memcpy(
set, values, number_of_values *
sizeof(values[0]));
1218 check_integer_set->
set =
set;
1219 check_integer_set->
size_of_set = number_of_values;
1221 function, parameter, file, line, check_function,
1222 check_data.value, &check_integer_set->
event, count);
1228 const char*
const function,
const char*
const parameter,
1229 const char*
const file,
const int line,
1232 expect_set(
function, parameter, file, line, values, number_of_values,
1239 const char*
const function,
const char*
const parameter,
1240 const char*
const file,
const int line,
1243 expect_set(
function, parameter, file, line, values, number_of_values,
1256 check_integer_range->
maximum);
1268 value, check_integer_range->
minimum, check_integer_range->
maximum);
1275 const char*
const function,
const char*
const parameter,
1276 const char*
const file,
const int line,
1282 check_integer_range->
minimum = minimum;
1283 check_integer_range->
maximum = maximum;
1284 _expect_check(
function, parameter, file, line, check_function,
1285 check_data.value, &check_integer_range->
event, count);
1291 const char*
const function,
const char*
const parameter,
1292 const char*
const file,
const int line,
1295 expect_range(
function, parameter, file, line, minimum, maximum,
1302 const char*
const function,
const char*
const parameter,
1303 const char*
const file,
const int line,
1306 expect_range(
function, parameter, file, line, minimum, maximum,
1321 const char*
const function,
const char*
const parameter,
1322 const char*
const file,
const int line,
1339 const char*
const function,
const char*
const parameter,
1340 const char*
const file,
const int line,
1358 const char*
const function,
const char*
const parameter,
1359 const char*
const file,
const int line,
const char*
string,
1364 string_pointer.value, NULL, count);
1380 const char*
const function,
const char*
const parameter,
1381 const char*
const file,
const int line,
const char*
string,
1386 string_pointer.value, NULL, count);
1405 const char*
const function,
const char*
const parameter,
1406 const char*
const file,
const int line,
1407 const void *
const memory,
const size_t size,
1411 void *
const mem = (
void*)(check_data + 1);
1415 memcpy(mem, memory, size);
1416 check_data->
memory = mem;
1417 check_data->
size = size;
1418 _expect_check(
function, parameter, file, line, check_function,
1419 check_data_pointer.value, &check_data->
event, count);
1425 const char*
const function,
const char*
const parameter,
1426 const char*
const file,
const int line,
const void*
const memory,
1427 const size_t size,
const int count) {
1442 (
const char*)check->
memory,
1449 const char*
const function,
const char*
const parameter,
1450 const char*
const file,
const int line,
const void*
const memory,
1451 const size_t size,
const int count) {
1461 (void)check_value_data;
1468 const char*
const function,
const char*
const parameter,
1469 const char*
const file,
const int line,
const int count) {
1476 const char *
const function_name,
const char *
const parameter_name,
1479 const char* symbols[] = {function_name, parameter_name};
1481 symbols, 2, &result);
1484 int check_succeeded;
1485 global_last_parameter_location = check->
location;
1490 if (!check_succeeded) {
1492 ": error: Check of parameter %s, function %s failed\n" 1494 ": note: Expected parameter declared here\n",
1496 parameter_name, function_name,
1497 global_last_parameter_location.file,
1498 global_last_parameter_location.line);
1503 "to check parameter %s of function %s\n", file, line,
1504 parameter_name, function_name);
1507 ": note: Previously declared parameter value was declared here\n",
1508 global_last_parameter_location.file,
1509 global_last_parameter_location.line);
1511 cm_print_error(
"There were no previously declared parameter values " 1512 "for this test.\n");
1521 const char*
const file,
const int line) {
1523 if (global_expecting_assert) {
1525 longjmp(global_expect_assert_env, result);
1535 const char *
const expression,
1536 const char *
const file,
const int line) {
1546 const char *
const expression,
1547 const char *
const file,
1561 valmax = 2147483647;
1565 if (rlen >
sizeof(valmax)) {
1566 valmax = 2147483647;
1568 valmax = 9223372036854775807L;
1573 if (result > valmax - 1) {
1576 expression, error, strerror((
int)error));
1586 const char *
const file,
const int line) {
1595 const char *
const file,
const int line) {
1603 const char *
const file,
const int line) {
1611 const char *file,
const int line) {
1619 const size_t size,
const char*
const file,
1628 const size_t size,
const char*
const file,
1657 const size_t number_of_values,
const char*
const file,
1660 check_integer_set.
set = values;
1669 const size_t number_of_values,
const char*
const file,
1672 check_integer_set.
set = values;
1683 if (!global_allocated_blocks.value) {
1685 global_allocated_blocks.value = (
void*)1;
1694 #define malloc test_malloc 1701 #define free test_free 1708 #define realloc test_realloc 1724 len =
vsnprintf(buffer,
sizeof(buffer), format, args);
1750 if (((
size_t)len) <
sizeof(buffer)) {
1773 char*
const block = (
char*)
malloc(allocate_size);
1786 sizeof(*block_info)));
1789 block_info->
size = size;
1790 block_info->
block = block;
1795 #define malloc test_malloc 1799 const char* file,
const int line) {
1800 void*
const ptr =
_test_malloc(number_of_elements * size, file, line);
1802 memset(ptr, 0, number_of_elements * size);
1810 void _test_free(
void*
const ptr,
const char* file,
const int line) {
1821 sizeof(*block_info)));
1825 block + block_info->
size};
1828 char *
const guard = guards[i];
1833 ": error: Guard block of %p size=%lu is corrupt\n" 1836 ptr, (
unsigned long)block_info->
size,
1850 #define free test_free 1860 size_t block_size = size;
1873 sizeof(*block_info)));
1880 if (block_info->
size < size) {
1881 block_size = block_info->
size;
1884 memcpy(
new, ptr, block_size);
1891 #define realloc test_realloc 1904 int allocated_blocks = 0;
1908 for (node = check_point->
next; node != head; node = node->
next) {
1913 if (!allocated_blocks) {
1920 allocated_blocks ++;
1922 return allocated_blocks;
1932 node = check_point->
next;
1935 while (node != head) {
1945 const char *
const test_name) {
1947 if (allocated_blocks) {
1956 void _fail(
const char *
const file,
const int line) {
1972 static void exception_handler(
int sig) {
1973 const char *sig_strerror =
"";
1975 #ifdef HAVE_STRSIGNAL 1976 sig_strerror = strsignal(sig);
1987 EXCEPTION_RECORD *
const exception_record =
1988 exception_pointers->ExceptionRecord;
1989 const DWORD code = exception_record->ExceptionCode;
1991 for (i = 0; i <
ARRAY_SIZE(exception_codes); i++) {
1993 if (code == code_info->
code) {
1994 static int shown_debug_message = 0;
1997 exception_record->ExceptionAddress);
1998 if (!shown_debug_message) {
2001 "To debug in Visual Studio...\n" 2002 "1. Select menu item File->Open Project\n" 2003 "2. Change 'Files of type' to 'Executable Files'\n" 2004 "3. Open this executable.\n" 2005 "4. Select menu item Debug->Start\n" 2007 "Alternatively, set the environment variable \n" 2008 "UNIT_TESTING_DEBUG to 1 and rebuild this executable, \n" 2009 "then click 'Debug' in the popup dialog box.\n" 2011 shown_debug_message = 1;
2014 return EXCEPTION_EXECUTE_HANDLER;
2017 return EXCEPTION_CONTINUE_SEARCH;
2024 va_start(args, format);
2036 vsnprintf(buffer,
sizeof(buffer), format, args);
2037 printf(
"%s", buffer);
2040 OutputDebugString(buffer);
2047 vsnprintf(buffer,
sizeof(buffer), format, args);
2048 fprintf(stderr,
"%s", buffer);
2051 OutputDebugString(buffer);
2058 va_start(args, format);
2066 va_start(args, format);
2077 env = getenv(
"CMOCKA_MESSAGE_OUTPUT");
2081 }
else if (
strcasecmp(env,
"SUBUNIT") == 0) {
2102 size_t total_executed,
2103 size_t total_failed,
2104 size_t total_errors,
2105 size_t total_skipped,
2106 double total_runtime,
2110 int file_opened = 0;
2114 env = getenv(
"CMOCKA_XML_FILE");
2117 snprintf(buf,
sizeof(buf),
"%s", env);
2119 fp = fopen(buf,
"r");
2121 fp = fopen(buf,
"w");
2133 fprintf(fp,
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
2134 fprintf(fp,
"<testsuites>\n");
2135 fprintf(fp,
" <testsuite name=\"%s\" time=\"%.3f\" " 2136 "tests=\"%u\" failures=\"%u\" errors=\"%u\" skipped=\"%u\" >\n",
2138 total_runtime * 1000,
2139 (
unsigned)total_executed,
2140 (
unsigned)total_failed,
2141 (
unsigned)total_errors,
2142 (
unsigned)total_skipped);
2144 for (i = 0; i < total_executed; i++) {
2147 fprintf(fp,
" <testcase name=\"%s\" time=\"%.3f\" >\n",
2150 switch (cmtest->
status) {
2154 fprintf(fp,
" <failure><![CDATA[%s]]></failure>\n",
2157 fprintf(fp,
" <failure message=\"Unknown error\" />\n");
2161 fprintf(fp,
" <skipped/>\n");
2169 fprintf(fp,
" </testcase>\n");
2172 fprintf(fp,
" </testsuite>\n");
2173 fprintf(fp,
"</testsuites>\n");
2183 (
unsigned)num_tests);
2187 size_t total_passed,
2188 size_t total_failed,
2189 size_t total_errors,
2190 size_t total_skipped,
2195 print_message(
"[==========] %u test(s) run.\n", (
unsigned)total_executed);
2197 (
unsigned)(total_passed));
2199 if (total_skipped) {
2200 print_error(
"[ SKIPPED ] %"PRIdS " test(s), listed below:\n", total_skipped);
2201 for (i = 0; i < total_executed; i++) {
2208 print_error(
"\n %u SKIPPED TEST(S)\n", (
unsigned)(total_skipped));
2212 print_error(
"[ FAILED ] %"PRIdS " test(s), listed below:\n", total_failed);
2213 for (i = 0; i < total_executed; i++) {
2221 (
unsigned)(total_failed + total_errors));
2226 const char *test_name,
2237 if (error_message != NULL) {
2246 if (error_message != NULL) {
2260 size_t total_executed,
2261 size_t total_passed,
2262 size_t total_skipped)
2264 const char *
status =
"not ok";
2265 if (total_passed + total_skipped == total_executed) {
2272 uint32_t test_number,
2273 const char *test_name,
2280 print_message(
"\tok %u - %s\n", (
unsigned)test_number, test_name);
2283 print_message(
"\tnot ok %u - %s\n", (
unsigned)test_number, test_name);
2284 if (error_message != NULL) {
2288 msg = strdup(error_message);
2294 while (p[0] !=
'\0') {
2297 p = strchr(q,
'\n');
2313 print_message(
"\tnot ok %u # SKIP %s\n", (
unsigned)test_number, test_name);
2317 (
unsigned)test_number, test_name, error_message);
2323 const char *test_name,
2335 if (error_message != NULL) {
2343 print_message(
"error: %s [ %s ]\n", test_name, error_message);
2369 size_t total_executed,
2370 size_t total_passed,
2371 size_t total_failed,
2372 size_t total_errors,
2373 size_t total_skipped,
2374 double total_runtime,
2409 const char *test_name,
2424 cmprintf_tap(type, test_number, test_name, error_message);
2440 #ifdef HAVE_STRUCT_TIMESPEC 2441 static struct timespec cm_tspecdiff(struct timespec time1,
2442 struct timespec time0)
2444 struct timespec ret;
2448 if (time0.tv_nsec > time1.tv_nsec) {
2449 xsec = (int) ((time0.tv_nsec - time1.tv_nsec) / (1E9 + 1));
2450 time0.tv_nsec -= (
long int) (1E9 * xsec);
2451 time0.tv_sec += xsec;
2454 if ((time1.tv_nsec - time0.tv_nsec) > 1E9) {
2455 xsec = (int) ((time1.tv_nsec - time0.tv_nsec) / 1E9);
2456 time0.tv_nsec += (
long int) (1E9 * xsec);
2457 time0.tv_sec -= xsec;
2460 ret.tv_sec = time1.tv_sec - time0.tv_sec;
2461 ret.tv_nsec = time1.tv_nsec - time0.tv_nsec;
2463 if (time1.tv_sec < time0.tv_sec) {
2467 ret.tv_sec = ret.tv_sec * sign;
2472 static double cm_secdiff(
struct timespec clock1,
struct timespec clock0)
2475 struct timespec diff;
2477 diff = cm_tspecdiff(clock1, clock0);
2480 ret += (double) diff.tv_nsec / (
double) 1E9;
2493 void **
const volatile state,
2494 const void *
const heap_check_point)
2497 (heap_check_point != NULL ?
2499 int handle_exceptions = 1;
2500 void *current_state = NULL;
2509 #ifdef UNIT_TESTING_DEBUG 2510 handle_exceptions = 0;
2514 if (handle_exceptions) {
2517 for (i = 0; i <
ARRAY_SIZE(exception_signals); i++) {
2518 default_signal_functions[i] = signal(
2519 exception_signals[i], exception_handler);
2522 previous_exception_filter = SetUnhandledExceptionFilter(
2530 global_running_test = 1;
2532 if (
cm_setjmp(global_run_test_env) == 0) {
2533 if (test_func != NULL) {
2534 test_func(state != NULL ? state : ¤t_state);
2538 }
else if (setup_func != NULL) {
2539 rc = setup_func(state != NULL ? state : ¤t_state);
2545 }
else if (teardown_func != NULL) {
2546 rc = teardown_func(state != NULL ? state : ¤t_state);
2553 global_running_test = 0;
2556 global_running_test = 0;
2561 if (handle_exceptions) {
2564 for (i = 0; i <
ARRAY_SIZE(exception_signals); i++) {
2565 signal(exception_signals[i], default_signal_functions[i]);
2568 if (previous_exception_filter) {
2569 SetUnhandledExceptionFilter(previous_exception_filter);
2570 previous_exception_filter = NULL;
2582 const void *
const heap_check_point)
2586 if (setup_func != NULL) {
2607 #ifdef HAVE_STRUCT_TIMESPEC 2608 struct timespec start = {
2612 struct timespec finish = {
2637 #ifdef HAVE_STRUCT_TIMESPEC 2651 if (global_skip_test) {
2653 global_skip_test = 0;
2663 #ifdef HAVE_STRUCT_TIMESPEC 2665 test_state->
runtime = cm_secdiff(finish, start);
2690 const size_t num_tests,
2696 void *group_state = NULL;
2697 size_t total_tests = 0;
2698 size_t total_failed = 0;
2699 size_t total_passed = 0;
2700 size_t total_executed = 0;
2701 size_t total_errors = 0;
2702 size_t total_skipped = 0;
2703 double total_runtime = 0;
2711 if (cm_tests == NULL) {
2716 for (i = 0; i < num_tests; i++) {
2717 if (tests[i].name != NULL &&
2718 (tests[i].test_func != NULL
2719 || tests[i].setup_func != NULL
2720 || tests[i].teardown_func != NULL)) {
2735 if (group_setup != NULL) {
2745 for (i = 0; i < total_tests; i++) {
2747 size_t test_number = i + 1;
2751 if (group_state != NULL) {
2752 cmtest->
state = group_state;
2759 total_runtime += cmtest->
runtime;
2761 switch (cmtest->
status) {
2787 "Internal cmocka error");
2795 "Could not run the test - check test fixtures");
2806 group_name,
"[ FAILED ] GROUP SETUP");
2811 if (group_teardown != NULL) {
2824 group_name,
"[ FAILED ] GROUP TEARDOWN");
2837 for (i = 0; i < total_tests; i++) {
2843 return total_failed + total_errors;
2853 const void*
const heap_check_point) {
2857 void *current_state = NULL;
2858 volatile int rc = 1;
2859 int handle_exceptions = 1;
2863 #ifdef UNIT_TESTING_DEBUG 2864 handle_exceptions = 0;
2869 if (handle_exceptions) {
2872 for (i = 0; i <
ARRAY_SIZE(exception_signals); i++) {
2873 default_signal_functions[i] = signal(
2874 exception_signals[i], exception_handler);
2877 previous_exception_filter = SetUnhandledExceptionFilter(
2886 global_running_test = 1;
2887 if (
cm_setjmp(global_run_test_env) == 0) {
2888 Function(state ? state : ¤t_state);
2897 global_running_test = 0;
2904 global_running_test = 0;
2909 if (handle_exceptions) {
2912 for (i = 0; i <
ARRAY_SIZE(exception_signals); i++) {
2913 signal(exception_signals[i], default_signal_functions[i]);
2916 if (previous_exception_filter) {
2917 SetUnhandledExceptionFilter(previous_exception_filter);
2918 previous_exception_filter = NULL;
2929 int run_next_test = 1;
2931 int previous_test_failed = 0;
2933 int previous_setup_failed = 0;
2937 size_t current_test = 0;
2939 size_t tests_executed = 0;
2941 size_t total_failed = 0;
2945 size_t teardowns = 0;
2954 long number_of_test_states = 0;
2956 const char** failed_names = (
const char**)
malloc(number_of_tests *
2957 sizeof(*failed_names));
2958 void **current_state = NULL;
2961 for (i = 0; i < number_of_tests; i++) {
2974 number_of_tests - setups - teardowns);
2979 while (current_test < number_of_tests) {
2980 const ListNode *test_check_point = NULL;
2989 if (! previous_setup_failed) {
2995 current_TestState = &test_states[number_of_test_states++];
2997 test_check_point = current_TestState->
check_point;
2998 current_state = ¤t_TestState->
state;
2999 *current_state = NULL;
3006 current_TestState = &test_states[--number_of_test_states];
3007 test_check_point = current_TestState->
check_point;
3008 current_state = ¤t_TestState->
state;
3011 print_error(
"Invalid unit test function type %d\n",
3017 if (run_next_test) {
3021 failed_names[total_failed] = test->
name;
3026 previous_test_failed = failed;
3027 total_failed += failed;
3037 previous_setup_failed = 1;
3039 previous_test_failed = 0;
3044 if (failed && !previous_test_failed) {
3058 print_error(
"[ PASSED ] %"PRIdS " test(s).\n", tests_executed - total_failed);
3060 if (total_failed > 0) {
3061 print_error(
"[ FAILED ] %"PRIdS " test(s), listed below:\n", total_failed);
3062 for (i = 0; i < total_failed; i++) {
3069 if (number_of_test_states != 0) {
3071 "teardown %"PRIdS " functions\n", setups, teardowns);
3072 total_failed = (size_t)-1;
3076 free((
void*)failed_names);
3079 return (
int)total_failed;
3085 const char *setup_name;
3086 size_t num_setups = 0;
3088 const char *teardown_name;
3089 size_t num_teardowns = 0;
3090 size_t current_test = 0;
3094 size_t tests_executed = 0;
3096 size_t total_failed = 0;
3099 const char** failed_names = (
const char**)
malloc(number_of_tests *
3100 sizeof(*failed_names));
3101 void **current_state = NULL;
3105 for (i = 0; i < number_of_tests; i++) {
3109 if (setup == NULL) {
3111 setup_name = test->
name;
3114 print_error(
"[ ERROR ] More than one group setup function detected\n");
3120 if (teardown == NULL) {
3122 teardown_name = test->
name;
3125 print_error(
"[ ERROR ] More than one group teardown function detected\n");
3132 number_of_tests - num_setups - num_teardowns);
3134 if (setup != NULL) {
3138 current_state = &group_state.
state;
3139 *current_state = NULL;
3146 failed_names[total_failed] = setup_name;
3149 total_failed += failed;
3153 while (current_test < number_of_tests) {
3170 print_error(
"Invalid unit test function type %d\n",
3184 failed_names[total_failed] = test->
name;
3187 total_failed += failed;
3192 if (teardown != NULL) {
3201 failed_names[total_failed] = teardown_name;
3204 total_failed += failed;
3209 print_error(
"[ PASSED ] %"PRIdS " test(s).\n", tests_executed - total_failed);
3212 print_error(
"[ FAILED ] %"PRIdS " test(s), listed below:\n", total_failed);
3213 for (i = 0; i < total_failed; i++) {
3220 free((
void*)failed_names);
3223 return (
int)total_failed;
static enum cm_message_output cm_get_output(void)
static void cmprintf_tap(enum cm_printf_type type, uint32_t test_number, const char *test_name, const char *error_message)
void cm_print_error(const char *const format,...) CMOCKA_PRINTF_ATTRIBUTE(1
struct CheckMemoryData CheckMemoryData
void _assert_memory_equal(const void *const a, const void *const b, const size_t size, const char *const file, const int line)
#define assert_non_null(c)
static void cmprintf_subunit(enum cm_printf_type type, const char *test_name, const char *error_message)
CheckParameterValue check_value
LargestIntegralType maximum
#define SOURCE_LOCATION_FORMAT
static void vcm_free_error(char *err_msg)
static void initialize_source_location(SourceLocation *const location)
void(* CleanupListValue)(const void *value, void *cleanup_value_data)
static void cmprintf_group_finish_xml(const char *group_name, size_t total_executed, size_t total_failed, size_t total_errors, size_t total_skipped, double total_runtime, struct CMUnitTestState *cm_tests)
void print_message(const char *const format,...)
static int string_not_equal_display_error(const char *const left, const char *const right)
static void cmprintf_group_finish_standard(size_t total_executed, size_t total_passed, size_t total_failed, size_t total_errors, size_t total_skipped, struct CMUnitTestState *cm_tests)
static CMOCKA_THREAD int global_running_test
ListNode symbol_values_list_head
static void expect_set(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType values[], const size_t number_of_values, const CheckParameterValue check_function, const int count)
CMFixtureFunction setup_func
void _expect_not_memory(const char *const function, const char *const parameter, const char *const file, const int line, const void *const memory, const size_t size, const int count)
static ListNode * list_add_value(ListNode *const head, const void *value, const int count)
static void cmprintf(enum cm_printf_type type, size_t test_number, const char *test_name, const char *error_message)
static int check_not_memory(const LargestIntegralType value, const LargestIntegralType check_value_data)
static int source_location_is_set(const SourceLocation *const location)
struct CheckIntegerSet CheckIntegerSet
void(* CMUnitTestFunction)(void **state)
static int global_skip_test
static void cmprintf_group_start(const size_t num_tests)
struct CheckIntegerRange CheckIntegerRange
static LONG WINAPI exception_filter(EXCEPTION_POINTERS *exception_pointers)
static int cm_error_message_enabled
static void cmprintf_group_finish(const char *group_name, size_t total_executed, size_t total_passed, size_t total_failed, size_t total_errors, size_t total_skipped, double total_runtime, struct CMUnitTestState *cm_tests)
static void exit_test(const int quit_application)
static CMOCKA_THREAD ListNode global_call_ordering_head
void _assert_int_equal(const LargestIntegralType a, const LargestIntegralType b, const char *const file, const int line)
int(* CheckParameterValue)(const LargestIntegralType value, const LargestIntegralType check_value_data)
static int memory_equal_display_error(const char *const a, const char *const b, const size_t size)
struct MallocBlockInfo MallocBlockInfo
static int teardown(void **state)
#define MALLOC_GUARD_SIZE
static int check_memory(const LargestIntegralType value, const LargestIntegralType check_value_data)
LargestIntegralType minimum
static void * libc_malloc(size_t size)
int _run_test(const char *const function_name, const UnitTestFunction Function, void **const volatile state, const UnitTestFunctionType function_type, const void *const heap_check_point)
static int setup(void **state)
void _expect_not_string(const char *const function, const char *const parameter, const char *const file, const int line, const char *string, const int count)
static void free_value(const void *value, void *cleanup_value_data)
void _assert_memory_not_equal(const void *const a, const void *const b, const size_t size, const char *const file, const int line)
void mock_assert(const int result, const char *const expression, const char *const file, const int line)
Function to replace assert(3) in tested code.
CheckParameterEvent event
static int list_find(ListNode *const head, const void *value, const EqualityFunction equal_func, ListNode **output)
const ListNode * check_point
#define cm_longjmp(env, val)
static void set_source_location(SourceLocation *const location, const char *const file, const int line)
LargestIntegralType value
static void cmprintf_standard(enum cm_printf_type type, const char *test_name, const char *error_message)
static void list_remove_free(ListNode *const node, const CleanupListValue cleanup_value, void *const cleanup_value_data)
static void initialize_testing(const char *test_name)
LargestIntegralType check_value_data
void _expect_any(const char *const function, const char *const parameter, const char *const file, const int line, const int count)
static int check_not_value(const LargestIntegralType value, const LargestIntegralType check_value_data)
CMUnitTestFunction test_func
static void remove_always_return_values(ListNode *const map_head, const size_t number_of_symbol_names)
static int integer_not_in_range_display_error(const LargestIntegralType value, const LargestIntegralType range_min, const LargestIntegralType range_max)
#define CMOCKA_PRINTF_ATTRIBUTE(a, b)
void _expect_string(const char *const function, const char *const parameter, const char *const file, const int line, const char *string, const int count)
#define discard_const_p(type, ptr)
Type-safe version of discard_const.
void _will_return(const char *const function_name, const char *const file, const int line, const LargestIntegralType value, const int count)
static int check_for_leftover_values(const ListNode *const map_head, const char *const error_message, const size_t number_of_symbol_names)
const char * error_message
static int check_not_in_range(const LargestIntegralType value, const LargestIntegralType check_value_data)
static int group_teardown(void **state)
static void cmprintf_group_finish_tap(const char *group_name, size_t total_executed, size_t total_passed, size_t total_skipped)
static void cmprintf_group_start_tap(const size_t num_tests)
int _run_group_tests(const UnitTest *const tests, const size_t number_of_tests)
void _expect_memory(const char *const function, const char *const parameter, const char *const file, const int line, const void *const memory, const size_t size, const int count)
void _expect_not_in_range(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType minimum, const LargestIntegralType maximum, const int count)
struct FuncOrderingValue FuncOrderingValue
void _test_free(void *const ptr, const char *file, const int line)
static int display_allocated_blocks(const ListNode *const check_point)
static void libc_free(void *ptr)
static int check_any(const LargestIntegralType value, const LargestIntegralType check_value_data)
static int values_equal_display_error(const LargestIntegralType left, const LargestIntegralType right)
static const ExceptionCodeInfo exception_codes[]
static CMOCKA_THREAD ListNode global_function_result_map_head
#define EXCEPTION_CODE_INFO(exception_code)
void _fail(const char *const file, const int line)
static ListNode * list_initialize(ListNode *const node)
const LargestIntegralType * set
int _run_tests(const UnitTest *const tests, const size_t number_of_tests)
UnitTestFunction function
static void teardown_testing(const char *test_name)
const struct CMUnitTest * test
#define cm_jmp_buf
POSIX has sigsetjmp/siglongjmp, while Windows only has setjmp/longjmp.
static LPTOP_LEVEL_EXCEPTION_FILTER previous_exception_filter
static CMOCKA_THREAD SourceLocation global_last_call_ordering_location
static void * libc_realloc(void *ptr, size_t size)
struct SymbolMapValue SymbolMapValue
union ValuePointer ValuePointer
void * _test_malloc(const size_t size, const char *file, const int line)
struct ValuePointer::@15 x
#define MALLOC_ALLOC_PATTERN
static int string_equal_display_error(const char *const left, const char *const right)
int global_expecting_assert
#define LargestIntegralTypePrintfFormat
static int cmocka_run_one_test_or_fixture(const char *function_name, CMUnitTestFunction test_func, CMFixtureFunction setup_func, CMFixtureFunction teardown_func, void **const volatile state, const void *const heap_check_point)
static int check_not_string(const LargestIntegralType value, const LargestIntegralType check_value_data)
static int memory_not_equal_display_error(const char *const a, const char *const b, const size_t size)
static ListNode * list_remove(ListNode *const node, const CleanupListValue cleanup_value, void *const cleanup_value_data)
void _assert_in_range(const LargestIntegralType value, const LargestIntegralType minimum, const LargestIntegralType maximum, const char *const file, const int line)
jmp_buf global_expect_assert_env
WINBASEAPI BOOL WINAPI IsDebuggerPresent(VOID)
static int check_in_range(const LargestIntegralType value, const LargestIntegralType check_value_data)
static CMOCKA_THREAD SourceLocation global_last_mock_value_location
#define discard_const(ptr)
This is a hack to fix warnings.
struct TestState TestState
struct SymbolValue SymbolValue
void _assert_return_code(const LargestIntegralType result, size_t rlen, const LargestIntegralType error, const char *const expression, const char *const file, const int line)
static int value_in_set_display_error(const LargestIntegralType value, const CheckIntegerSet *const check_integer_set, const int invert)
void _assert_not_in_set(const LargestIntegralType value, const LargestIntegralType values[], const size_t number_of_values, const char *const file, const int line)
void _function_called(const char *const function, const char *const file, const int line)
static void free_allocated_blocks(const ListNode *const check_point)
void _expect_check(const char *const function, const char *const parameter, const char *const file, const int line, const CheckParameterValue check_function, const LargestIntegralType check_data, CheckParameterEvent *const event, const int count)
void _expect_in_range(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType minimum, const LargestIntegralType maximum, const int count)
#define CMOCKA_CLOCK_GETTIME(clock_id, ts)
static void vcm_print_error(const char *const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1
static int values_not_equal_display_error(const LargestIntegralType left, const LargestIntegralType right)
void _assert_string_equal(const char *const a, const char *const b, const char *const file, const int line)
static int check_string(const LargestIntegralType value, const LargestIntegralType check_value_data)
int(* CMFixtureFunction)(void **state)
static int check_in_set(const LargestIntegralType value, const LargestIntegralType check_value_data)
static const ListNode * check_point_allocated_blocks()
static ListNode * get_allocated_blocks_list()
#define cast_ptr_to_largest_integral_type(value)
#define LargestIntegralType
#define declare_initialize_value_pointer_pointer(name, ptr)
static void expect_range(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType minimum, const LargestIntegralType maximum, const CheckParameterValue check_function, const int count)
static ListNode * list_add(ListNode *const head, ListNode *new_node)
static void fail_if_leftover_values(const char *test_name)
void _expect_in_set(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType values[], const size_t number_of_values, const int count)
static void fail_if_blocks_allocated(const ListNode *const check_point, const char *const test_name)
static int symbol_names_match(const void *map_value, const void *symbol)
const char * global_last_failed_assert
LargestIntegralType _mock(const char *const function, const char *const file, const int line)
void _expect_function_call(const char *const function_name, const char *const file, const int line, const int count)
void print_error(const char *const format,...)
static int check_value(const LargestIntegralType value, const LargestIntegralType check_value_data)
void _assert_not_in_range(const LargestIntegralType value, const LargestIntegralType minimum, const LargestIntegralType maximum, const char *const file, const int line)
LargestIntegralType value
CheckParameterEvent event
static enum cm_message_output global_msg_output
CMFixtureFunction teardown_func
Wrapper structure for dynamically allocated memory.
struct ExceptionCodeInfo ExceptionCodeInfo
void vprint_message(const char *const format, va_list args)
void _skip(const char *const file, const int line)
void cmocka_set_message_output(enum cm_message_output output)
Function to set the output format for a test.
const char * parameter_name
static ListNode * list_free(ListNode *const head, const CleanupListValue cleanup_value, void *const cleanup_value_data)
static CMOCKA_THREAD SourceLocation global_last_parameter_location
static int list_empty(const ListNode *const head)
void * _test_calloc(const size_t number_of_elements, const size_t size, const char *file, const int line)
enum CMUnitTestStatus status
static int check_for_leftover_values_list(const ListNode *head, const char *const error_message)
static CMOCKA_THREAD char * cm_error_message
static int cmocka_run_one_tests(struct CMUnitTestState *test_state)
void _expect_not_in_set(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType values[], const size_t number_of_values, const int count)
static void expect_memory_setup(const char *const function, const char *const parameter, const char *const file, const int line, const void *const memory, const size_t size, const CheckParameterValue check_function, const int count)
void _check_expected(const char *const function_name, const char *const parameter_name, const char *file, const int line, const LargestIntegralType value)
void _expect_not_value(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType value, const int count)
static void cmprintf_group_start_standard(const size_t num_tests)
void _assert_string_not_equal(const char *const a, const char *const b, const char *file, const int line)
static int get_symbol_value(ListNode *const symbol_map_head, const char *const symbol_names[], const size_t number_of_symbol_names, void **output)
#define ARRAY_SIZE(a)
Get the size of an array.
static int check_not_in_set(const LargestIntegralType value, const LargestIntegralType check_value_data)
#define cast_largest_integral_type_to_pointer(pointer_type, largest_integral_type)
void _assert_true(const LargestIntegralType result, const char *const expression, const char *const file, const int line)
int(* EqualityFunction)(const void *left, const void *right)
void _expect_value(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType value, const int count)
void static CMOCKA_THREAD cm_jmp_buf global_run_test_env
UnitTestFunctionType function_type
static int list_first(ListNode *const head, ListNode **output)
void(* UnitTestFunction)(void **state)
static SERVICE_STATUS status
void vprint_error(const char *const format, va_list args)
void * _test_realloc(void *ptr, const size_t size, const char *file, const int line)
static int group_setup(void **state)
void _assert_in_set(const LargestIntegralType value, const LargestIntegralType values[], const size_t number_of_values, const char *const file, const int line)
void _assert_int_not_equal(const LargestIntegralType a, const LargestIntegralType b, const char *const file, const int line)
static void add_symbol_value(ListNode *const symbol_map_head, const char *const symbol_names[], const size_t number_of_symbol_names, const void *value, const int count)
#define MALLOC_GUARD_PATTERN
static int integer_in_range_display_error(const LargestIntegralType value, const LargestIntegralType range_min, const LargestIntegralType range_max)
static int cmocka_run_group_fixture(const char *function_name, CMFixtureFunction setup_func, CMFixtureFunction teardown_func, void **state, const void *const heap_check_point)
static void remove_always_return_values_from_list(ListNode *const map_head)
Taverse a list of nodes and remove first symbol value in list that has a refcount < -1 (i...
#define MALLOC_FREE_PATTERN
static void free_symbol_map_value(const void *value, void *cleanup_value_data)
int _cmocka_run_group_tests(const char *group_name, const struct CMUnitTest *const tests, const size_t num_tests, CMFixtureFunction group_setup, CMFixtureFunction group_teardown)
CheckParameterEvent event
static CMOCKA_THREAD ListNode global_function_parameter_map_head
static CMOCKA_THREAD ListNode global_allocated_blocks
const ListNode * check_point