2 * Copyright 2008 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
37 #define vsnprintf _vsnprintf
40 /* Backwards compatibility with headers shipped with Visual Studio 2005 and
43 WINBASEAPI BOOL WINAPI IsDebuggerPresent(VOID);
46 // Size of guard bytes around dynamically allocated blocks.
47 #define MALLOC_GUARD_SIZE 16
48 // Pattern used to initialize guard blocks.
49 #define MALLOC_GUARD_PATTERN 0xEF
50 // Pattern used to initialize memory allocated with test_malloc().
51 #define MALLOC_ALLOC_PATTERN 0xBA
52 #define MALLOC_FREE_PATTERN 0xCD
53 // Alignment of allocated blocks. NOTE: This must be base2.
54 #define MALLOC_ALIGNMENT sizeof(size_t)
56 // Printf formatting for source code locations.
57 #define SOURCE_LOCATION_FORMAT "%s:%d"
59 // Calculates the number of elements in an array.
60 #define ARRAY_LENGTH(x) (sizeof(x) / sizeof((x)[0]))
62 // Declare and initialize the pointer member of ValuePointer variable name
64 #define declare_initialize_value_pointer_pointer(name, ptr) \
67 name.pointer = (void*)(ptr)
69 // Declare and initialize the value member of ValuePointer variable name
71 #define declare_initialize_value_pointer_value(name, val) \
75 // Cast a LargestIntegralType to pointer_type via a ValuePointer.
76 #define cast_largest_integral_type_to_pointer( \
77 pointer_type, largest_integral_type) \
78 ((pointer_type)((ValuePointer*)&(largest_integral_type))->pointer)
80 // Used to cast LargetIntegralType to void* and vice versa.
81 typedef union ValuePointer {
82 LargestIntegralType value;
86 // Doubly linked list node.
87 typedef struct ListNode {
90 struct ListNode *next;
91 struct ListNode *prev;
94 // Debug information for malloc().
95 typedef struct MallocBlockInfo {
96 void* block; // Address of the block returned by malloc().
97 size_t allocated_size; // Total size of the allocated block.
98 size_t size; // Request block size.
99 SourceLocation location; // Where the block was allocated.
100 ListNode node; // Node within list of all allocated blocks.
103 // State of each test.
104 typedef struct TestState {
105 const ListNode *check_point; // Check point of the test if there's a
107 void *state; // State associated with the test.
110 // Determines whether two values are the same.
111 typedef int (*EqualityFunction)(const void *left, const void *right);
113 // Value of a symbol and the place it was declared.
114 typedef struct SymbolValue {
115 SourceLocation location;
116 LargestIntegralType value;
119 /* Contains a list of values for a symbol.
120 * NOTE: Each structure referenced by symbol_values_list_head must have a
121 * SourceLocation as its' first member.
123 typedef struct SymbolMapValue {
124 const char *symbol_name;
125 ListNode symbol_values_list_head;
128 // Used by list_free() to deallocate values referenced by list nodes.
129 typedef void (*CleanupListValue)(const void *value, void *cleanup_value_data);
131 // Structure used to check the range of integer types.
132 typedef struct CheckIntegerRange {
133 CheckParameterEvent event;
134 LargestIntegralType minimum;
135 LargestIntegralType maximum;
138 // Structure used to check whether an integer value is in a set.
139 typedef struct CheckIntegerSet {
140 CheckParameterEvent event;
141 const LargestIntegralType *set;
145 /* Used to check whether a parameter matches the area of memory referenced by
147 typedef struct CheckMemoryData {
148 CheckParameterEvent event;
153 static ListNode* list_initialize(ListNode * const node);
154 static ListNode* list_add(ListNode * const head, ListNode *new_node);
155 static ListNode* list_add_value(ListNode * const head, const void *value,
157 static ListNode* list_remove(
158 ListNode * const node, const CleanupListValue cleanup_value,
159 void * const cleanup_value_data);
160 static void list_remove_free(
161 ListNode * const node, const CleanupListValue cleanup_value,
162 void * const cleanup_value_data);
163 static int list_empty(const ListNode * const head);
164 static int list_find(
165 ListNode * const head, const void *value,
166 const EqualityFunction equal_func, ListNode **output);
167 static int list_first(ListNode * const head, ListNode **output);
168 static ListNode* list_free(
169 ListNode * const head, const CleanupListValue cleanup_value,
170 void * const cleanup_value_data);
172 static void add_symbol_value(
173 ListNode * const symbol_map_head, const char * const symbol_names[],
174 const size_t number_of_symbol_names, const void* value, const int count);
175 static int get_symbol_value(
176 ListNode * const symbol_map_head, const char * const symbol_names[],
177 const size_t number_of_symbol_names, void **output);
178 static void free_value(const void *value, void *cleanup_value_data);
179 static void free_symbol_map_value(
180 const void *value, void *cleanup_value_data);
181 static void remove_always_return_values(ListNode * const map_head,
182 const size_t number_of_symbol_names);
183 static int check_for_leftover_values(
184 const ListNode * const map_head, const char * const error_message,
185 const size_t number_of_symbol_names);
186 // This must be called at the beginning of a test to initialize some data
188 static void initialize_testing(const char *test_name);
189 // This must be called at the end of a test to free() allocated structures.
190 static void teardown_testing(const char *test_name);
193 // Keeps track of the calling context returned by setenv() so that the fail()
194 // method can jump out of a test.
195 static jmp_buf global_run_test_env;
196 static int global_running_test = 0;
198 // Keeps track of the calling context returned by setenv() so that
199 // mock_assert() can optionally jump back to expect_assert_failure().
200 jmp_buf global_expect_assert_env;
201 int global_expecting_assert = 0;
203 // Keeps a map of the values that functions will have to return to provide
204 // mocked interfaces.
205 static ListNode global_function_result_map_head;
206 // Location of the last mock value returned was declared.
207 static SourceLocation global_last_mock_value_location;
209 /* Keeps a map of the values that functions expect as parameters to their
210 * mocked interfaces. */
211 static ListNode global_function_parameter_map_head;
212 // Location of last parameter value checked was declared.
213 static SourceLocation global_last_parameter_location;
215 // List of all currently allocated blocks.
216 static ListNode global_allocated_blocks;
219 // Signals caught by exception_handler().
220 static const int exception_signals[] = {
228 // Default signal functions that should be restored after a test is complete.
229 typedef void (*SignalFunction)(int signal);
230 static SignalFunction default_signal_functions[
231 ARRAY_LENGTH(exception_signals)];
235 // The default exception filter.
236 static LPTOP_LEVEL_EXCEPTION_FILTER previous_exception_filter;
239 typedef struct ExceptionCodeInfo {
241 const char* description;
244 #define EXCEPTION_CODE_INFO(exception_code) {exception_code, #exception_code}
246 static const ExceptionCodeInfo exception_codes[] = {
247 EXCEPTION_CODE_INFO(EXCEPTION_ACCESS_VIOLATION),
248 EXCEPTION_CODE_INFO(EXCEPTION_ARRAY_BOUNDS_EXCEEDED),
249 EXCEPTION_CODE_INFO(EXCEPTION_DATATYPE_MISALIGNMENT),
250 EXCEPTION_CODE_INFO(EXCEPTION_FLT_DENORMAL_OPERAND),
251 EXCEPTION_CODE_INFO(EXCEPTION_FLT_DIVIDE_BY_ZERO),
252 EXCEPTION_CODE_INFO(EXCEPTION_FLT_INEXACT_RESULT),
253 EXCEPTION_CODE_INFO(EXCEPTION_FLT_INVALID_OPERATION),
254 EXCEPTION_CODE_INFO(EXCEPTION_FLT_OVERFLOW),
255 EXCEPTION_CODE_INFO(EXCEPTION_FLT_STACK_CHECK),
256 EXCEPTION_CODE_INFO(EXCEPTION_FLT_UNDERFLOW),
257 EXCEPTION_CODE_INFO(EXCEPTION_GUARD_PAGE),
258 EXCEPTION_CODE_INFO(EXCEPTION_ILLEGAL_INSTRUCTION),
259 EXCEPTION_CODE_INFO(EXCEPTION_INT_DIVIDE_BY_ZERO),
260 EXCEPTION_CODE_INFO(EXCEPTION_INT_OVERFLOW),
261 EXCEPTION_CODE_INFO(EXCEPTION_INVALID_DISPOSITION),
262 EXCEPTION_CODE_INFO(EXCEPTION_INVALID_HANDLE),
263 EXCEPTION_CODE_INFO(EXCEPTION_IN_PAGE_ERROR),
264 EXCEPTION_CODE_INFO(EXCEPTION_NONCONTINUABLE_EXCEPTION),
265 EXCEPTION_CODE_INFO(EXCEPTION_PRIV_INSTRUCTION),
266 EXCEPTION_CODE_INFO(EXCEPTION_STACK_OVERFLOW),
271 // Exit the currently executing test.
272 static void exit_test(const int quit_application) {
273 if (global_running_test) {
274 longjmp(global_run_test_env, 1);
275 } else if (quit_application) {
281 // Initialize a SourceLocation structure.
282 static void initialize_source_location(SourceLocation * const location) {
283 assert_non_null(location);
284 location->file = NULL;
289 // Determine whether a source location is currently set.
290 static int source_location_is_set(const SourceLocation * const location) {
291 assert_non_null(location);
292 return location->file && location->line;
296 // Set a source location.
297 static void set_source_location(
298 SourceLocation * const location, const char * const file,
300 assert_non_null(location);
301 location->file = file;
302 location->line = line;
306 // Create function results and expected parameter lists.
307 void initialize_testing(const char *test_name) {
309 list_initialize(&global_function_result_map_head);
310 initialize_source_location(&global_last_mock_value_location);
311 list_initialize(&global_function_parameter_map_head);
312 initialize_source_location(&global_last_parameter_location);
316 void fail_if_leftover_values(const char *test_name) {
317 int error_occurred = 0;
319 remove_always_return_values(&global_function_result_map_head, 1);
320 if (check_for_leftover_values(
321 &global_function_result_map_head,
322 "%s() has remaining non-returned values.\n", 1)) {
326 remove_always_return_values(&global_function_parameter_map_head, 2);
327 if (check_for_leftover_values(
328 &global_function_parameter_map_head,
329 "%s parameter still has values that haven't been checked.\n", 2)) {
332 if (error_occurred) {
338 void teardown_testing(const char *test_name) {
340 list_free(&global_function_result_map_head, free_symbol_map_value,
342 initialize_source_location(&global_last_mock_value_location);
343 list_free(&global_function_parameter_map_head, free_symbol_map_value,
345 initialize_source_location(&global_last_parameter_location);
348 // Initialize a list node.
349 static ListNode* list_initialize(ListNode * const node) {
358 /* Adds a value at the tail of a given list.
359 * The node referencing the value is allocated from the heap. */
360 static ListNode* list_add_value(ListNode * const head, const void *value,
361 const int refcount) {
362 ListNode * const new_node = (ListNode*)malloc(sizeof(ListNode));
363 assert_non_null(head);
364 assert_non_null(value);
365 new_node->value = value;
366 new_node->refcount = refcount;
367 return list_add(head, new_node);
371 // Add new_node to the end of the list.
372 static ListNode* list_add(ListNode * const head, ListNode *new_node) {
373 assert_non_null(head);
374 assert_non_null(new_node);
375 new_node->next = head;
376 new_node->prev = head->prev;
377 head->prev->next = new_node;
378 head->prev = new_node;
383 // Remove a node from a list.
384 static ListNode* list_remove(
385 ListNode * const node, const CleanupListValue cleanup_value,
386 void * const cleanup_value_data) {
387 assert_non_null(node);
388 node->prev->next = node->next;
389 node->next->prev = node->prev;
391 cleanup_value(node->value, cleanup_value_data);
397 /* Remove a list node from a list and free the node. */
398 static void list_remove_free(
399 ListNode * const node, const CleanupListValue cleanup_value,
400 void * const cleanup_value_data) {
401 assert_non_null(node);
402 free(list_remove(node, cleanup_value, cleanup_value_data));
406 /* Frees memory kept by a linked list
407 * The cleanup_value function is called for every "value" field of nodes in the
408 * list, except for the head. In addition to each list value,
409 * cleanup_value_data is passed to each call to cleanup_value. The head
410 * of the list is not deallocated.
412 static ListNode* list_free(
413 ListNode * const head, const CleanupListValue cleanup_value,
414 void * const cleanup_value_data) {
415 assert_non_null(head);
416 while (!list_empty(head)) {
417 list_remove_free(head->next, cleanup_value, cleanup_value_data);
423 // Determine whether a list is empty.
424 static int list_empty(const ListNode * const head) {
425 assert_non_null(head);
426 return head->next == head;
430 /* Find a value in the list using the equal_func to compare each node with the
433 static int list_find(ListNode * const head, const void *value,
434 const EqualityFunction equal_func, ListNode **output) {
436 assert_non_null(head);
437 for (current = head->next; current != head; current = current->next) {
438 if (equal_func(current->value, value)) {
446 // Returns the first node of a list
447 static int list_first(ListNode * const head, ListNode **output) {
448 ListNode *target_node;
449 assert_non_null(head);
450 if (list_empty(head)) {
453 target_node = head->next;
454 *output = target_node;
459 // Deallocate a value referenced by a list.
460 static void free_value(const void *value, void *cleanup_value_data) {
461 (void)cleanup_value_data;
462 assert_non_null(value);
467 // Releases memory associated to a symbol_map_value.
468 static void free_symbol_map_value(const void *value,
469 void *cleanup_value_data) {
470 SymbolMapValue * const map_value = (SymbolMapValue*)value;
471 const LargestIntegralType children = cast_ptr_to_largest_integral_type(cleanup_value_data);
472 assert_non_null(value);
473 list_free(&map_value->symbol_values_list_head,
474 children ? free_symbol_map_value : free_value,
475 (void *) ((uintptr_t)children - 1));
480 /* Determine whether a symbol name referenced by a symbol_map_value
481 * matches the specified function name. */
482 static int symbol_names_match(const void *map_value, const void *symbol) {
483 return !strcmp(((SymbolMapValue*)map_value)->symbol_name,
484 (const char*)symbol);
488 /* Adds a value to the queue of values associated with the given
489 * hierarchy of symbols. It's assumed value is allocated from the heap.
491 static void add_symbol_value(ListNode * const symbol_map_head,
492 const char * const symbol_names[],
493 const size_t number_of_symbol_names,
494 const void* value, const int refcount) {
495 const char* symbol_name;
496 ListNode *target_node;
497 SymbolMapValue *target_map_value;
498 assert_non_null(symbol_map_head);
499 assert_non_null(symbol_names);
500 assert_true(number_of_symbol_names);
501 symbol_name = symbol_names[0];
503 if (!list_find(symbol_map_head, symbol_name, symbol_names_match,
505 SymbolMapValue * const new_symbol_map_value =
506 (SymbolMapValue*)malloc(sizeof(*new_symbol_map_value));
507 new_symbol_map_value->symbol_name = symbol_name;
508 list_initialize(&new_symbol_map_value->symbol_values_list_head);
509 target_node = list_add_value(symbol_map_head, new_symbol_map_value,
513 target_map_value = (SymbolMapValue*)target_node->value;
514 if (number_of_symbol_names == 1) {
515 list_add_value(&target_map_value->symbol_values_list_head,
518 add_symbol_value(&target_map_value->symbol_values_list_head,
519 &symbol_names[1], number_of_symbol_names - 1, value,
525 /* Gets the next value associated with the given hierarchy of symbols.
526 * The value is returned as an output parameter with the function returning the
527 * node's old refcount value if a value is found, 0 otherwise.
528 * This means that a return value of 1 indicates the node was just removed from
531 static int get_symbol_value(
532 ListNode * const head, const char * const symbol_names[],
533 const size_t number_of_symbol_names, void **output) {
534 const char* symbol_name;
535 ListNode *target_node;
536 assert_non_null(head);
537 assert_non_null(symbol_names);
538 assert_true(number_of_symbol_names);
539 assert_non_null(output);
540 symbol_name = symbol_names[0];
542 if (list_find(head, symbol_name, symbol_names_match, &target_node)) {
543 SymbolMapValue *map_value;
544 ListNode *child_list;
545 int return_value = 0;
546 assert_non_null(target_node);
547 assert_non_null(target_node->value);
549 map_value = (SymbolMapValue*)target_node->value;
550 child_list = &map_value->symbol_values_list_head;
552 if (number_of_symbol_names == 1) {
553 ListNode *value_node = NULL;
554 return_value = list_first(child_list, &value_node);
555 assert_true(return_value);
556 *output = (void*) value_node->value;
557 return_value = value_node->refcount;
558 if (--value_node->refcount == 0) {
559 list_remove_free(value_node, NULL, NULL);
562 return_value = get_symbol_value(
563 child_list, &symbol_names[1], number_of_symbol_names - 1,
566 if (list_empty(child_list)) {
567 list_remove_free(target_node, free_symbol_map_value, (void*)0);
571 print_error("No entries for symbol %s.\n", symbol_name);
577 /* Traverse down a tree of symbol values and remove the first symbol value
578 * in each branch that has a refcount < -1 (i.e should always be returned
579 * and has been returned at least once).
581 static void remove_always_return_values(ListNode * const map_head,
582 const size_t number_of_symbol_names) {
584 assert_non_null(map_head);
585 assert_true(number_of_symbol_names);
586 current = map_head->next;
587 while (current != map_head) {
588 SymbolMapValue * const value = (SymbolMapValue*)current->value;
589 ListNode * const next = current->next;
590 ListNode *child_list;
591 assert_non_null(value);
592 child_list = &value->symbol_values_list_head;
594 if (!list_empty(child_list)) {
595 if (number_of_symbol_names == 1) {
596 ListNode * const child_node = child_list->next;
597 // If this item has been returned more than once, free it.
598 if (child_node->refcount < -1) {
599 list_remove_free(child_node, free_value, NULL);
602 remove_always_return_values(child_list,
603 number_of_symbol_names - 1);
607 if (list_empty(child_list)) {
608 list_remove_free(current, free_value, NULL);
614 /* Checks if there are any leftover values set up by the test that were never
615 * retrieved through execution, and fail the test if that is the case.
617 static int check_for_leftover_values(
618 const ListNode * const map_head, const char * const error_message,
619 const size_t number_of_symbol_names) {
620 const ListNode *current;
621 int symbols_with_leftover_values = 0;
622 assert_non_null(map_head);
623 assert_true(number_of_symbol_names);
625 for (current = map_head->next; current != map_head;
626 current = current->next) {
627 const SymbolMapValue * const value =
628 (SymbolMapValue*)current->value;
629 const ListNode *child_list;
630 assert_non_null(value);
631 child_list = &value->symbol_values_list_head;
633 if (!list_empty(child_list)) {
634 if (number_of_symbol_names == 1) {
635 const ListNode *child_node;
636 print_error(error_message, value->symbol_name);
637 print_error(" Remaining item(s) declared at...\n");
639 for (child_node = child_list->next; child_node != child_list;
640 child_node = child_node->next) {
641 const SourceLocation * const location =
642 (const SourceLocation*)child_node->value;
643 print_error(" " SOURCE_LOCATION_FORMAT "\n",
644 location->file, location->line);
647 print_error("%s.", value->symbol_name);
648 check_for_leftover_values(child_list, error_message,
649 number_of_symbol_names - 1);
651 symbols_with_leftover_values ++;
654 return symbols_with_leftover_values;
658 // Get the next return value for the specified mock function.
659 LargestIntegralType _mock(const char * const function, const char* const file,
662 const int rc = get_symbol_value(&global_function_result_map_head,
663 &function, 1, &result);
665 SymbolValue * const symbol = (SymbolValue*)result;
666 const LargestIntegralType value = symbol->value;
667 global_last_mock_value_location = symbol->location;
673 print_error("ERROR: " SOURCE_LOCATION_FORMAT " - Could not get value "
674 "to mock function %s\n", file, line, function);
675 if (source_location_is_set(&global_last_mock_value_location)) {
676 print_error("Previously returned mock value was declared at "
677 SOURCE_LOCATION_FORMAT "\n",
678 global_last_mock_value_location.file,
679 global_last_mock_value_location.line);
681 print_error("There were no previously returned mock values for "
690 // Add a return value for the specified mock function name.
691 void _will_return(const char * const function_name, const char * const file,
692 const int line, const LargestIntegralType value,
694 SymbolValue * const return_value =
695 (SymbolValue*)malloc(sizeof(*return_value));
696 assert_true(count > 0 || count == -1);
697 return_value->value = value;
698 set_source_location(&return_value->location, file, line);
699 add_symbol_value(&global_function_result_map_head, &function_name, 1,
700 return_value, count);
704 /* Add a custom parameter checking function. If the event parameter is NULL
705 * the event structure is allocated internally by this function. If event
706 * parameter is provided it must be allocated on the heap and doesn't need to
707 * be deallocated by the caller.
710 const char* const function, const char* const parameter,
711 const char* const file, const int line,
712 const CheckParameterValue check_function,
713 const LargestIntegralType check_data,
714 CheckParameterEvent * const event, const int count) {
715 CheckParameterEvent * const check =
716 event ? event : (CheckParameterEvent*)malloc(sizeof(*check));
717 const char* symbols[] = {function, parameter};
718 check->parameter_name = parameter;
719 check->check_value = check_function;
720 check->check_value_data = check_data;
721 set_source_location(&check->location, file, line);
722 add_symbol_value(&global_function_parameter_map_head, symbols, 2, check,
727 /* Returns 1 if the specified values are equal. If the values are not equal
728 * an error is displayed and 0 is returned. */
729 static int values_equal_display_error(const LargestIntegralType left,
730 const LargestIntegralType right) {
731 const int equal = left == right;
733 print_error(LargestIntegralTypePrintfFormat " != "
734 LargestIntegralTypePrintfFormat "\n", left, right);
739 /* Returns 1 if the specified values are not equal. If the values are equal
740 * an error is displayed and 0 is returned. */
741 static int values_not_equal_display_error(const LargestIntegralType left,
742 const LargestIntegralType right) {
743 const int not_equal = left != right;
745 print_error(LargestIntegralTypePrintfFormat " == "
746 LargestIntegralTypePrintfFormat "\n", left, right);
752 /* Determine whether value is contained within check_integer_set.
753 * If invert is 0 and the value is in the set 1 is returned, otherwise 0 is
754 * returned and an error is displayed. If invert is 1 and the value is not
755 * in the set 1 is returned, otherwise 0 is returned and an error is
757 static int value_in_set_display_error(
758 const LargestIntegralType value,
759 const CheckIntegerSet * const check_integer_set, const int invert) {
760 int succeeded = invert;
761 assert_non_null(check_integer_set);
763 const LargestIntegralType * const set = check_integer_set->set;
764 const size_t size_of_set = check_integer_set->size_of_set;
766 for (i = 0; i < size_of_set; i++) {
767 if (set[i] == value) {
768 // If invert = 0 and item is found, succeeded = 1.
769 // If invert = 1 and item is found, succeeded = 0.
770 succeeded = !succeeded;
777 print_error("%d is %sin the set (", value, invert ? "" : "not ");
778 for (i = 0; i < size_of_set; i++) {
779 print_error("%d, ", set[i]);
787 /* Determine whether a value is within the specified range. If the value is
788 * within the specified range 1 is returned. If the value isn't within the
789 * specified range an error is displayed and 0 is returned. */
790 static int integer_in_range_display_error(
791 const LargestIntegralType value, const LargestIntegralType range_min,
792 const LargestIntegralType range_max) {
793 if (value >= range_min && value <= range_max) {
796 print_error("%d is not within the range %d-%d\n", value, range_min,
802 /* Determine whether a value is within the specified range. If the value
803 * is not within the range 1 is returned. If the value is within the
804 * specified range an error is displayed and zero is returned. */
805 static int integer_not_in_range_display_error(
806 const LargestIntegralType value, const LargestIntegralType range_min,
807 const LargestIntegralType range_max) {
808 if (value < range_min || value > range_max) {
811 print_error("%d is within the range %d-%d\n", value, range_min,
817 /* Determine whether the specified strings are equal. If the strings are equal
818 * 1 is returned. If they're not equal an error is displayed and 0 is
820 static int string_equal_display_error(
821 const char * const left, const char * const right) {
822 if (strcmp(left, right) == 0) {
825 print_error("\"%s\" != \"%s\"\n", left, right);
830 /* Determine whether the specified strings are equal. If the strings are not
831 * equal 1 is returned. If they're not equal an error is displayed and 0 is
833 static int string_not_equal_display_error(
834 const char * const left, const char * const right) {
835 if (strcmp(left, right) != 0) {
838 print_error("\"%s\" == \"%s\"\n", left, right);
843 /* Determine whether the specified areas of memory are equal. If they're equal
844 * 1 is returned otherwise an error is displayed and 0 is returned. */
845 static int memory_equal_display_error(const char* const a, const char* const b,
849 for (i = 0; i < size; i++) {
853 print_error("difference at offset %d 0x%02x 0x%02x\n", i, l, r);
858 print_error("%d bytes of 0x%08x and 0x%08x differ\n", differences,
866 /* Determine whether the specified areas of memory are not equal. If they're
867 * not equal 1 is returned otherwise an error is displayed and 0 is
869 static int memory_not_equal_display_error(
870 const char* const a, const char* const b, const size_t size) {
873 for (i = 0; i < size; i++) {
881 print_error("%u bytes of 0x%08x and 0x%08x the same\n", same,
889 // CheckParameterValue callback to check whether a value is within a set.
890 static int check_in_set(const LargestIntegralType value,
891 const LargestIntegralType check_value_data) {
892 return value_in_set_display_error(value,
893 cast_largest_integral_type_to_pointer(CheckIntegerSet*,
894 check_value_data), 0);
898 // CheckParameterValue callback to check whether a value isn't within a set.
899 static int check_not_in_set(const LargestIntegralType value,
900 const LargestIntegralType check_value_data) {
901 return value_in_set_display_error(value,
902 cast_largest_integral_type_to_pointer(CheckIntegerSet*,
903 check_value_data), 1);
907 /* Create the callback data for check_in_set() or check_not_in_set() and
908 * register a check event. */
909 static void expect_set(
910 const char* const function, const char* const parameter,
911 const char* const file, const int line,
912 const LargestIntegralType values[], const size_t number_of_values,
913 const CheckParameterValue check_function, const int count) {
914 CheckIntegerSet * const check_integer_set =
915 (CheckIntegerSet*)malloc(sizeof(*check_integer_set) +
916 (sizeof(values[0]) * number_of_values));
917 LargestIntegralType * const set = (LargestIntegralType*)(
918 check_integer_set + 1);
919 declare_initialize_value_pointer_pointer(check_data, check_integer_set);
920 assert_non_null(values);
921 assert_true(number_of_values);
922 memcpy(set, values, number_of_values * sizeof(values[0]));
923 check_integer_set->set = set;
925 function, parameter, file, line, check_function,
926 check_data.value, &check_integer_set->event, count);
930 // Add an event to check whether a value is in a set.
932 const char* const function, const char* const parameter,
933 const char* const file, const int line,
934 const LargestIntegralType values[], const size_t number_of_values,
936 expect_set(function, parameter, file, line, values, number_of_values,
937 check_in_set, count);
941 // Add an event to check whether a value isn't in a set.
942 void _expect_not_in_set(
943 const char* const function, const char* const parameter,
944 const char* const file, const int line,
945 const LargestIntegralType values[], const size_t number_of_values,
947 expect_set(function, parameter, file, line, values, number_of_values,
948 check_not_in_set, count);
952 // CheckParameterValue callback to check whether a value is within a range.
953 static int check_in_range(const LargestIntegralType value,
954 const LargestIntegralType check_value_data) {
955 CheckIntegerRange * const check_integer_range =
956 cast_largest_integral_type_to_pointer(CheckIntegerRange*,
958 assert_non_null(check_integer_range);
959 return integer_in_range_display_error(value, check_integer_range->minimum,
960 check_integer_range->maximum);
964 // CheckParameterValue callback to check whether a value is not within a range.
965 static int check_not_in_range(const LargestIntegralType value,
966 const LargestIntegralType check_value_data) {
967 CheckIntegerRange * const check_integer_range =
968 cast_largest_integral_type_to_pointer(CheckIntegerRange*,
970 assert_non_null(check_integer_range);
971 return integer_not_in_range_display_error(
972 value, check_integer_range->minimum, check_integer_range->maximum);
976 /* Create the callback data for check_in_range() or check_not_in_range() and
977 * register a check event. */
978 static void expect_range(
979 const char* const function, const char* const parameter,
980 const char* const file, const int line,
981 const LargestIntegralType minimum, const LargestIntegralType maximum,
982 const CheckParameterValue check_function, const int count) {
983 CheckIntegerRange * const check_integer_range =
984 (CheckIntegerRange*)malloc(sizeof(*check_integer_range));
985 declare_initialize_value_pointer_pointer(check_data, check_integer_range);
986 check_integer_range->minimum = minimum;
987 check_integer_range->maximum = maximum;
988 _expect_check(function, parameter, file, line, check_function,
989 check_data.value, &check_integer_range->event, count);
993 // Add an event to determine whether a parameter is within a range.
994 void _expect_in_range(
995 const char* const function, const char* const parameter,
996 const char* const file, const int line,
997 const LargestIntegralType minimum, const LargestIntegralType maximum,
999 expect_range(function, parameter, file, line, minimum, maximum,
1000 check_in_range, count);
1004 // Add an event to determine whether a parameter is not within a range.
1005 void _expect_not_in_range(
1006 const char* const function, const char* const parameter,
1007 const char* const file, const int line,
1008 const LargestIntegralType minimum, const LargestIntegralType maximum,
1010 expect_range(function, parameter, file, line, minimum, maximum,
1011 check_not_in_range, count);
1015 /* CheckParameterValue callback to check whether a value is equal to an
1016 * expected value. */
1017 static int check_value(const LargestIntegralType value,
1018 const LargestIntegralType check_value_data) {
1019 return values_equal_display_error(value, check_value_data);
1023 // Add an event to check a parameter equals an expected value.
1025 const char* const function, const char* const parameter,
1026 const char* const file, const int line,
1027 const LargestIntegralType value, const int count) {
1028 _expect_check(function, parameter, file, line, check_value, value, NULL,
1033 /* CheckParameterValue callback to check whether a value is not equal to an
1034 * expected value. */
1035 static int check_not_value(const LargestIntegralType value,
1036 const LargestIntegralType check_value_data) {
1037 return values_not_equal_display_error(value, check_value_data);
1041 // Add an event to check a parameter is not equal to an expected value.
1042 void _expect_not_value(
1043 const char* const function, const char* const parameter,
1044 const char* const file, const int line,
1045 const LargestIntegralType value, const int count) {
1046 _expect_check(function, parameter, file, line, check_not_value, value,
1051 // CheckParameterValue callback to check whether a parameter equals a string.
1052 static int check_string(const LargestIntegralType value,
1053 const LargestIntegralType check_value_data) {
1054 return string_equal_display_error(
1055 cast_largest_integral_type_to_pointer(char*, value),
1056 cast_largest_integral_type_to_pointer(char*, check_value_data));
1060 // Add an event to check whether a parameter is equal to a string.
1061 void _expect_string(
1062 const char* const function, const char* const parameter,
1063 const char* const file, const int line, const char* string,
1065 declare_initialize_value_pointer_pointer(string_pointer, (char*)string);
1066 _expect_check(function, parameter, file, line, check_string,
1067 string_pointer.value, NULL, count);
1071 /* CheckParameterValue callback to check whether a parameter is not equals to
1073 static int check_not_string(const LargestIntegralType value,
1074 const LargestIntegralType check_value_data) {
1075 return string_not_equal_display_error(
1076 cast_largest_integral_type_to_pointer(char*, value),
1077 cast_largest_integral_type_to_pointer(char*, check_value_data));
1081 // Add an event to check whether a parameter is not equal to a string.
1082 void _expect_not_string(
1083 const char* const function, const char* const parameter,
1084 const char* const file, const int line, const char* string,
1086 declare_initialize_value_pointer_pointer(string_pointer, (char*)string);
1087 _expect_check(function, parameter, file, line, check_not_string,
1088 string_pointer.value, NULL, count);
1091 /* CheckParameterValue callback to check whether a parameter equals an area of
1093 static int check_memory(const LargestIntegralType value,
1094 const LargestIntegralType check_value_data) {
1095 CheckMemoryData * const check = cast_largest_integral_type_to_pointer(
1096 CheckMemoryData*, check_value_data);
1097 assert_non_null(check);
1098 return memory_equal_display_error(
1099 cast_largest_integral_type_to_pointer(const char*, value),
1100 (const char*)check->memory, check->size);
1104 /* Create the callback data for check_memory() or check_not_memory() and
1105 * register a check event. */
1106 static void expect_memory_setup(
1107 const char* const function, const char* const parameter,
1108 const char* const file, const int line,
1109 const void * const memory, const size_t size,
1110 const CheckParameterValue check_function, const int count) {
1111 CheckMemoryData * const check_data =
1112 (CheckMemoryData*)malloc(sizeof(*check_data) + size);
1113 void * const mem = (void*)(check_data + 1);
1114 declare_initialize_value_pointer_pointer(check_data_pointer, check_data);
1115 assert_non_null(memory);
1117 memcpy(mem, memory, size);
1118 check_data->memory = mem;
1119 check_data->size = size;
1120 _expect_check(function, parameter, file, line, check_function,
1121 check_data_pointer.value, &check_data->event, count);
1125 // Add an event to check whether a parameter matches an area of memory.
1126 void _expect_memory(
1127 const char* const function, const char* const parameter,
1128 const char* const file, const int line, const void* const memory,
1129 const size_t size, const int count) {
1130 expect_memory_setup(function, parameter, file, line, memory, size,
1131 check_memory, count);
1135 /* CheckParameterValue callback to check whether a parameter is not equal to
1136 * an area of memory. */
1137 static int check_not_memory(const LargestIntegralType value,
1138 const LargestIntegralType check_value_data) {
1139 CheckMemoryData * const check = cast_largest_integral_type_to_pointer(
1140 CheckMemoryData*, check_value_data);
1141 assert_non_null(check);
1142 return memory_not_equal_display_error(
1143 cast_largest_integral_type_to_pointer(const char*, value),
1144 (const char*)check->memory,
1149 // Add an event to check whether a parameter doesn't match an area of memory.
1150 void _expect_not_memory(
1151 const char* const function, const char* const parameter,
1152 const char* const file, const int line, const void* const memory,
1153 const size_t size, const int count) {
1154 expect_memory_setup(function, parameter, file, line, memory, size,
1155 check_not_memory, count);
1159 // CheckParameterValue callback that always returns 1.
1160 static int check_any(const LargestIntegralType value,
1161 const LargestIntegralType check_value_data) {
1163 (void)check_value_data;
1168 // Add an event to allow any value for a parameter.
1170 const char* const function, const char* const parameter,
1171 const char* const file, const int line, const int count) {
1172 _expect_check(function, parameter, file, line, check_any, 0, NULL,
1177 void _check_expected(
1178 const char * const function_name, const char * const parameter_name,
1179 const char* file, const int line, const LargestIntegralType value) {
1181 const char* symbols[] = {function_name, parameter_name};
1182 const int rc = get_symbol_value(&global_function_parameter_map_head,
1183 symbols, 2, &result);
1185 CheckParameterEvent * const check = (CheckParameterEvent*)result;
1186 int check_succeeded;
1187 global_last_parameter_location = check->location;
1188 check_succeeded = check->check_value(value, check->check_value_data);
1192 if (!check_succeeded) {
1193 print_error("ERROR: Check of parameter %s, function %s failed\n"
1194 "Expected parameter declared at "
1195 SOURCE_LOCATION_FORMAT "\n",
1196 parameter_name, function_name,
1197 global_last_parameter_location.file,
1198 global_last_parameter_location.line);
1202 print_error("ERROR: " SOURCE_LOCATION_FORMAT " - Could not get value "
1203 "to check parameter %s of function %s\n", file, line,
1204 parameter_name, function_name);
1205 if (source_location_is_set(&global_last_parameter_location)) {
1206 print_error("Previously declared parameter value was declared at "
1207 SOURCE_LOCATION_FORMAT "\n",
1208 global_last_parameter_location.file,
1209 global_last_parameter_location.line);
1211 print_error("There were no previously declared parameter values "
1212 "for this test.\n");
1219 // Replacement for assert.
1220 void mock_assert(const int result, const char* const expression,
1221 const char* const file, const int line) {
1223 if (global_expecting_assert) {
1224 longjmp(global_expect_assert_env, (int)expression);
1226 print_error("ASSERT: %s\n", expression);
1233 void _assert_true(const LargestIntegralType result,
1234 const char * const expression,
1235 const char * const file, const int line) {
1237 print_error("%s\n", expression);
1242 void _assert_int_equal(
1243 const LargestIntegralType a, const LargestIntegralType b,
1244 const char * const file, const int line) {
1245 if (!values_equal_display_error(a, b)) {
1251 void _assert_int_not_equal(
1252 const LargestIntegralType a, const LargestIntegralType b,
1253 const char * const file, const int line) {
1254 if (!values_not_equal_display_error(a, b)) {
1260 void _assert_string_equal(const char * const a, const char * const b,
1261 const char * const file, const int line) {
1262 if (!string_equal_display_error(a, b)) {
1268 void _assert_string_not_equal(const char * const a, const char * const b,
1269 const char *file, const int line) {
1270 if (!string_not_equal_display_error(a, b)) {
1276 void _assert_memory_equal(const void * const a, const void * const b,
1277 const size_t size, const char* const file,
1279 if (!memory_equal_display_error((const char*)a, (const char*)b, size)) {
1285 void _assert_memory_not_equal(const void * const a, const void * const b,
1286 const size_t size, const char* const file,
1288 if (!memory_not_equal_display_error((const char*)a, (const char*)b,
1295 void _assert_in_range(
1296 const LargestIntegralType value, const LargestIntegralType minimum,
1297 const LargestIntegralType maximum, const char* const file,
1299 if (!integer_in_range_display_error(value, minimum, maximum)) {
1304 void _assert_not_in_range(
1305 const LargestIntegralType value, const LargestIntegralType minimum,
1306 const LargestIntegralType maximum, const char* const file,
1308 if (!integer_not_in_range_display_error(value, minimum, maximum)) {
1313 void _assert_in_set(const LargestIntegralType value,
1314 const LargestIntegralType values[],
1315 const size_t number_of_values, const char* const file,
1317 CheckIntegerSet check_integer_set;
1318 check_integer_set.set = values;
1319 check_integer_set.size_of_set = number_of_values;
1320 if (!value_in_set_display_error(value, &check_integer_set, 0)) {
1325 void _assert_not_in_set(const LargestIntegralType value,
1326 const LargestIntegralType values[],
1327 const size_t number_of_values, const char* const file,
1329 CheckIntegerSet check_integer_set;
1330 check_integer_set.set = values;
1331 check_integer_set.size_of_set = number_of_values;
1332 if (!value_in_set_display_error(value, &check_integer_set, 1)) {
1338 // Get the list of allocated blocks.
1339 static ListNode* get_allocated_blocks_list() {
1340 // If it initialized, initialize the list of allocated blocks.
1341 if (!global_allocated_blocks.value) {
1342 list_initialize(&global_allocated_blocks);
1343 global_allocated_blocks.value = (void*)1;
1345 return &global_allocated_blocks;
1348 // Use the real malloc in this function.
1350 void* _test_malloc(const size_t size, const char* file, const int line) {
1352 MallocBlockInfo *block_info;
1353 ListNode * const block_list = get_allocated_blocks_list();
1354 const size_t allocate_size = size + (MALLOC_GUARD_SIZE * 2) +
1355 sizeof(*block_info) + MALLOC_ALIGNMENT;
1356 char* const block = (char*)malloc(allocate_size);
1357 assert_non_null(block);
1359 // Calculate the returned address.
1360 ptr = (char*)(((size_t)block + MALLOC_GUARD_SIZE + sizeof(*block_info) +
1361 MALLOC_ALIGNMENT) & ~(MALLOC_ALIGNMENT - 1));
1363 // Initialize the guard blocks.
1364 memset(ptr - MALLOC_GUARD_SIZE, MALLOC_GUARD_PATTERN, MALLOC_GUARD_SIZE);
1365 memset(ptr + size, MALLOC_GUARD_PATTERN, MALLOC_GUARD_SIZE);
1366 memset(ptr, MALLOC_ALLOC_PATTERN, size);
1368 block_info = (MallocBlockInfo*)(ptr - (MALLOC_GUARD_SIZE +
1369 sizeof(*block_info)));
1370 set_source_location(&block_info->location, file, line);
1371 block_info->allocated_size = allocate_size;
1372 block_info->size = size;
1373 block_info->block = block;
1374 block_info->node.value = block_info;
1375 list_add(block_list, &block_info->node);
1378 #define malloc test_malloc
1381 void* _test_calloc(const size_t number_of_elements, const size_t size,
1382 const char* file, const int line) {
1383 void* const ptr = _test_malloc(number_of_elements * size, file, line);
1385 memset(ptr, 0, number_of_elements * size);
1391 // Use the real free in this function.
1393 void _test_free(void* const ptr, const char* file, const int line) {
1395 char *block = (char*)ptr;
1396 MallocBlockInfo *block_info;
1397 _assert_true(cast_ptr_to_largest_integral_type(ptr), "ptr", file, line);
1398 block_info = (MallocBlockInfo*)(block - (MALLOC_GUARD_SIZE +
1399 sizeof(*block_info)));
1400 // Check the guard blocks.
1402 char *guards[2] = {block - MALLOC_GUARD_SIZE,
1403 block + block_info->size};
1404 for (i = 0; i < ARRAY_LENGTH(guards); i++) {
1406 char * const guard = guards[i];
1407 for (j = 0; j < MALLOC_GUARD_SIZE; j++) {
1408 const char diff = guard[j] - MALLOC_GUARD_PATTERN;
1411 "Guard block of 0x%08x size=%d allocated by "
1412 SOURCE_LOCATION_FORMAT " at 0x%08x is corrupt\n",
1413 (size_t)ptr, block_info->size,
1414 block_info->location.file, block_info->location.line,
1421 list_remove(&block_info->node, NULL, NULL);
1423 block = (char*)block_info->block;
1424 memset(block, MALLOC_FREE_PATTERN, block_info->allocated_size);
1427 #define free test_free
1430 // Crudely checkpoint the current heap state.
1431 static const ListNode* check_point_allocated_blocks() {
1432 return get_allocated_blocks_list()->prev;
1436 /* Display the blocks allocated after the specified check point. This
1437 * function returns the number of blocks displayed. */
1438 static int display_allocated_blocks(const ListNode * const check_point) {
1439 const ListNode * const head = get_allocated_blocks_list();
1440 const ListNode *node;
1441 int allocated_blocks = 0;
1442 assert_non_null(check_point);
1443 assert_non_null(check_point->next);
1445 for (node = check_point->next; node != head; node = node->next) {
1446 const MallocBlockInfo * const block_info =
1447 (const MallocBlockInfo*)node->value;
1448 assert_non_null(block_info);
1450 if (!allocated_blocks) {
1451 print_error("Blocks allocated...\n");
1453 print_error(" 0x%08x : " SOURCE_LOCATION_FORMAT "\n",
1454 block_info->block, block_info->location.file,
1455 block_info->location.line);
1456 allocated_blocks ++;
1458 return allocated_blocks;
1462 // Free all blocks allocated after the specified check point.
1463 static void free_allocated_blocks(const ListNode * const check_point) {
1464 const ListNode * const head = get_allocated_blocks_list();
1465 const ListNode *node;
1466 assert_non_null(check_point);
1468 node = check_point->next;
1469 assert_non_null(node);
1471 while (node != head) {
1472 MallocBlockInfo * const block_info = (MallocBlockInfo*)node->value;
1474 free((char*)block_info + sizeof(*block_info) + MALLOC_GUARD_SIZE);
1479 // Fail if any any blocks are allocated after the specified check point.
1480 static void fail_if_blocks_allocated(const ListNode * const check_point,
1481 const char * const test_name) {
1482 const int allocated_blocks = display_allocated_blocks(check_point);
1483 if (allocated_blocks) {
1484 free_allocated_blocks(check_point);
1485 print_error("ERROR: %s leaked %d block(s)\n", test_name,
1492 void _fail(const char * const file, const int line) {
1493 print_error("ERROR: " SOURCE_LOCATION_FORMAT " Failure!\n", file, line);
1499 static void exception_handler(int sig) {
1501 print_error("%d\n", sig);
1503 print_error("%s\n", strsignal(sig));
1510 static LONG WINAPI exception_filter(EXCEPTION_POINTERS *exception_pointers) {
1511 EXCEPTION_RECORD * const exception_record =
1512 exception_pointers->ExceptionRecord;
1513 const DWORD code = exception_record->ExceptionCode;
1515 for (i = 0; i < ARRAY_LENGTH(exception_codes); i++) {
1516 const ExceptionCodeInfo * const code_info = &exception_codes[i];
1517 if (code == code_info->code) {
1518 static int shown_debug_message = 0;
1520 print_error("%s occurred at 0x%08x.\n", code_info->description,
1521 exception_record->ExceptionAddress);
1522 if (!shown_debug_message) {
1525 "To debug in Visual Studio...\n"
1526 "1. Select menu item File->Open Project\n"
1527 "2. Change 'Files of type' to 'Executable Files'\n"
1528 "3. Open this executable.\n"
1529 "4. Select menu item Debug->Start\n"
1531 "Alternatively, set the environment variable \n"
1532 "UNIT_TESTING_DEBUG to 1 and rebuild this executable, \n"
1533 "then click 'Debug' in the popup dialog box.\n"
1535 shown_debug_message = 1;
1538 return EXCEPTION_EXECUTE_HANDLER;
1541 return EXCEPTION_CONTINUE_SEARCH;
1546 // Standard output and error print methods.
1547 void vprint_message(const char* const format, va_list args) {
1549 vsnprintf(buffer, sizeof(buffer), format, args);
1550 printf("%s", buffer);
1553 OutputDebugString(buffer);
1558 void vprint_error(const char* const format, va_list args) {
1560 vsnprintf(buffer, sizeof(buffer), format, args);
1561 fprintf(stderr, "%s", buffer);
1564 OutputDebugString(buffer);
1569 void print_message(const char* const format, ...) {
1571 va_start(args, format);
1572 vprint_message(format, args);
1577 void print_error(const char* const format, ...) {
1579 va_start(args, format);
1580 vprint_error(format, args);
1586 const char * const function_name, const UnitTestFunction Function,
1587 void ** const volatile state, const UnitTestFunctionType function_type,
1588 const void* const heap_check_point) {
1589 const ListNode * const volatile check_point = (const ListNode*)
1591 heap_check_point : check_point_allocated_blocks());
1592 void *current_state = NULL;
1593 volatile int rc = 1;
1594 int handle_exceptions = 1;
1596 handle_exceptions = !IsDebuggerPresent();
1598 #if UNIT_TESTING_DEBUG
1599 handle_exceptions = 0;
1600 #endif // UNIT_TESTING_DEBUG
1602 if (handle_exceptions) {
1605 for (i = 0; i < ARRAY_LENGTH(exception_signals); i++) {
1606 default_signal_functions[i] = signal(
1607 exception_signals[i], exception_handler);
1610 previous_exception_filter = SetUnhandledExceptionFilter(
1615 if (function_type == UNIT_TEST_FUNCTION_TYPE_TEST) {
1616 print_message("[ RUN ] %s\n", function_name);
1618 initialize_testing(function_name);
1619 global_running_test = 1;
1620 if (setjmp(global_run_test_env) == 0) {
1621 Function(state ? state : ¤t_state);
1622 fail_if_leftover_values(function_name);
1624 /* If this is a setup function then ignore any allocated blocks
1625 * only ensure they're deallocated on tear down. */
1626 if (function_type != UNIT_TEST_FUNCTION_TYPE_SETUP) {
1627 fail_if_blocks_allocated(check_point, function_name);
1630 global_running_test = 0;
1632 if (function_type == UNIT_TEST_FUNCTION_TYPE_TEST) {
1633 print_message("[ OK ] %s\n", function_name);
1637 global_running_test = 0;
1638 print_message("[ FAILED ] %s\n", function_name);
1640 teardown_testing(function_name);
1642 if (handle_exceptions) {
1645 for (i = 0; i < ARRAY_LENGTH(exception_signals); i++) {
1646 signal(exception_signals[i], default_signal_functions[i]);
1649 if (previous_exception_filter) {
1650 SetUnhandledExceptionFilter(previous_exception_filter);
1651 previous_exception_filter = NULL;
1660 int _run_tests(const UnitTest * const tests, const size_t number_of_tests) {
1661 // Whether to execute the next test.
1662 int run_next_test = 1;
1663 // Whether the previous test failed.
1664 int previous_test_failed = 0;
1665 // Check point of the heap state.
1666 const ListNode * const check_point = check_point_allocated_blocks();
1667 // Current test being executed.
1668 size_t current_test = 0;
1669 // Number of tests executed.
1670 size_t tests_executed = 0;
1671 // Number of failed tests.
1672 size_t total_failed = 0;
1673 // Number of setup functions.
1675 // Number of teardown functions.
1676 size_t teardowns = 0;
1677 /* A stack of test states. A state is pushed on the stack
1678 * when a test setup occurs and popped on tear down. */
1679 TestState* test_states =
1680 (TestState*)malloc(number_of_tests * sizeof(*test_states));
1681 size_t number_of_test_states = 0;
1682 // Names of the tests that failed.
1683 const char** failed_names = (const char**)malloc(number_of_tests *
1684 sizeof(*failed_names));
1685 void **current_state = NULL;
1687 print_message("[==========] Running %d test(s).\n", number_of_tests);
1689 // Make sure LargestIntegralType is at least the size of a pointer.
1690 assert_true(sizeof(LargestIntegralType) >= sizeof(void*));
1692 while (current_test < number_of_tests) {
1693 const ListNode *test_check_point = NULL;
1694 TestState *current_TestState;
1695 const UnitTest * const test = &tests[current_test++];
1696 if (!test->function) {
1700 switch (test->function_type) {
1701 case UNIT_TEST_FUNCTION_TYPE_TEST:
1704 case UNIT_TEST_FUNCTION_TYPE_SETUP: {
1705 // Checkpoint the heap before the setup.
1706 current_TestState = &test_states[number_of_test_states++];
1707 current_TestState->check_point = check_point_allocated_blocks();
1708 test_check_point = current_TestState->check_point;
1709 current_state = ¤t_TestState->state;
1710 *current_state = NULL;
1715 case UNIT_TEST_FUNCTION_TYPE_TEARDOWN:
1716 // Check the heap based on the last setup checkpoint.
1717 assert_true(number_of_test_states);
1718 current_TestState = &test_states[--number_of_test_states];
1719 test_check_point = current_TestState->check_point;
1720 current_state = ¤t_TestState->state;
1724 print_error("Invalid unit test function type %d\n",
1725 test->function_type);
1730 if (run_next_test) {
1731 int failed = _run_test(test->name, test->function, current_state,
1732 test->function_type, test_check_point);
1734 failed_names[total_failed] = test->name;
1737 switch (test->function_type) {
1738 case UNIT_TEST_FUNCTION_TYPE_TEST:
1739 previous_test_failed = failed;
1740 total_failed += failed;
1744 case UNIT_TEST_FUNCTION_TYPE_SETUP:
1748 // Skip forward until the next test or setup function.
1751 previous_test_failed = 0;
1754 case UNIT_TEST_FUNCTION_TYPE_TEARDOWN:
1755 // If this test failed.
1756 if (failed && !previous_test_failed) {
1762 assert_null("BUG: shouldn't be here!");
1769 print_message("[==========] %d test(s) run.\n", tests_executed);
1770 print_error("[ PASSED ] %d test(s).\n", tests_executed - total_failed);
1774 print_error("[ FAILED ] %d test(s), listed below:\n", total_failed);
1775 for (i = 0; i < total_failed; i++) {
1776 print_error("[ FAILED ] %s\n", failed_names[i]);
1779 print_error("\n %d FAILED TEST(S)\n", total_failed);
1782 if (number_of_test_states) {
1783 print_error("[ ERROR ] Mismatched number of setup %d and "
1784 "teardown %d functions\n", setups, teardowns);
1785 total_failed = (size_t)-1;
1789 free((void*)failed_names);
1791 fail_if_blocks_allocated(check_point, "run_tests");
1792 return (int)total_failed;