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.
23 #endif /* __cplusplus */
24 int __stdcall IsDebuggerPresent();
27 #endif /* __cplusplus */
28 #endif /* _MSC_VER < 1500 */
32 * These headers or their equivalents should be included prior to including
39 * This allows test applications to use custom definitions of C standard
40 * library functions and types.
43 /* For those who are used to __func__ from gcc. */
45 #define __func__ __FUNCTION__
48 /* GCC have printf type attribute check. */
50 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
52 #define PRINTF_ATTRIBUTE(a,b)
56 * Largest integral type. This type should be large enough to hold any
57 * pointer or integer supported by the compiler.
59 #ifndef LargestIntegralType
60 #define LargestIntegralType unsigned long long
61 #endif /* LargestIntegralType */
63 /* Printf format used to display LargestIntegralType. */
64 #ifndef LargestIntegralTypePrintfFormat
66 #define LargestIntegralTypePrintfFormat "%I64x"
68 #define LargestIntegralTypePrintfFormat "%llx"
70 #endif /* LargestIntegralTypePrintfFormat */
72 /* Perform an unsigned cast to LargestIntegralType. */
73 #define cast_to_largest_integral_type(value) \
74 ((LargestIntegralType)((unsigned)(value)))
76 /* Smallest integral type capable of holding a pointer. */
81 /* WIN32 is an ILP32 platform */
82 typedef unsigned long uintptr_t;
86 /* what about 64-bit windows?
87 * what's the right preprocessor symbol?
88 typedef unsigned long long uintptr_t */
90 /* ILP32 and LP64 platforms */
91 typedef unsigned long uintptr_t;
94 #endif /* _UINTPTR_T */
96 /* Perform an unsigned cast to uintptr_t. */
97 #define cast_to_pointer_integral_type(value) \
100 /* Perform a cast of a pointer to uintmax_t */
101 #define cast_ptr_to_largest_integral_type(value) \
102 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
104 /* Retrieves a return value for the current function. */
105 #define mock() _mock(__func__, __FILE__, __LINE__)
108 * Stores a value to be returned by the specified function later.
109 * The count parameter returns the number of times the value should be returned
110 * by mock(). If count is set to -1 the value will always be returned.
112 #define will_return(function, value) \
113 _will_return(#function, __FILE__, __LINE__, \
114 cast_to_largest_integral_type(value), 1)
115 #define will_return_count(function, value, count) \
116 _will_return(#function, __FILE__, __LINE__, \
117 cast_to_largest_integral_type(value), count)
120 * Add a custom parameter checking function. If the event parameter is NULL
121 * the event structure is allocated internally by this function. If event
122 * parameter is provided it must be allocated on the heap and doesn't need to
123 * be deallocated by the caller.
125 #define expect_check(function, parameter, check_function, check_data) \
126 _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
127 cast_to_largest_integral_type(check_data), NULL, 0)
130 * Add an event to check a parameter, using check_expected(), against a set of
131 * values. See will_return() for a description of the count parameter.
133 #define expect_in_set(function, parameter, value_array) \
134 expect_in_set_count(function, parameter, value_array, 1)
135 #define expect_in_set_count(function, parameter, value_array, count) \
136 _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
137 sizeof(value_array) / sizeof((value_array)[0]), count)
138 #define expect_not_in_set(function, parameter, value_array) \
139 expect_not_in_set_count(function, parameter, value_array, 1)
140 #define expect_not_in_set_count(function, parameter, value_array, count) \
141 _expect_not_in_set( \
142 #function, #parameter, __FILE__, __LINE__, value_array, \
143 sizeof(value_array) / sizeof((value_array)[0]), count)
147 * Add an event to check a parameter, using check_expected(), against a
148 * signed range. Where range is minimum <= value <= maximum.
149 * See will_return() for a description of the count parameter.
151 #define expect_in_range(function, parameter, minimum, maximum) \
152 expect_in_range_count(function, parameter, minimum, maximum, 1)
153 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
154 _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
158 * Add an event to check a parameter, using check_expected(), against a
159 * signed range. Where range is value < minimum or value > maximum.
160 * See will_return() for a description of the count parameter.
162 #define expect_not_in_range(function, parameter, minimum, maximum) \
163 expect_not_in_range_count(function, parameter, minimum, maximum, 1)
164 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
166 _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
167 minimum, maximum, count)
170 * Add an event to check whether a parameter, using check_expected(), is or
171 * isn't a value. See will_return() for a description of the count parameter.
173 #define expect_value(function, parameter, value) \
174 expect_value_count(function, parameter, value, 1)
175 #define expect_value_count(function, parameter, value, count) \
176 _expect_value(#function, #parameter, __FILE__, __LINE__, \
177 cast_to_largest_integral_type(value), count)
178 #define expect_not_value(function, parameter, value) \
179 expect_not_value_count(function, parameter, value, 1)
180 #define expect_not_value_count(function, parameter, value, count) \
181 _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
182 cast_to_largest_integral_type(value), count)
185 * Add an event to check whether a parameter, using check_expected(),
186 * is or isn't a string. See will_return() for a description of the count
189 #define expect_string(function, parameter, string) \
190 expect_string_count(function, parameter, string, 1)
191 #define expect_string_count(function, parameter, string, count) \
192 _expect_string(#function, #parameter, __FILE__, __LINE__, \
193 (const char*)(string), count)
194 #define expect_not_string(function, parameter, string) \
195 expect_not_string_count(function, parameter, string, 1)
196 #define expect_not_string_count(function, parameter, string, count) \
197 _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
198 (const char*)(string), count)
201 * Add an event to check whether a parameter, using check_expected() does or
202 * doesn't match an area of memory. See will_return() for a description of
203 * the count parameter.
205 #define expect_memory(function, parameter, memory, size) \
206 expect_memory_count(function, parameter, memory, size, 1)
207 #define expect_memory_count(function, parameter, memory, size, count) \
208 _expect_memory(#function, #parameter, __FILE__, __LINE__, \
209 (const void*)(memory), size, count)
210 #define expect_not_memory(function, parameter, memory, size) \
211 expect_not_memory_count(function, parameter, memory, size, 1)
212 #define expect_not_memory_count(function, parameter, memory, size, count) \
213 _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
214 (const void*)(memory), size, count)
218 * Add an event to allow any value for a parameter checked using
219 * check_expected(). See will_return() for a description of the count
222 #define expect_any(function, parameter) \
223 expect_any_count(function, parameter, 1)
224 #define expect_any_count(function, parameter, count) \
225 _expect_any(#function, #parameter, __FILE__, __LINE__, count)
228 * Determine whether a function parameter is correct. This ensures the next
229 * value queued by one of the expect_*() macros matches the specified variable.
231 #define check_expected(parameter) \
232 _check_expected(__func__, #parameter, __FILE__, __LINE__, \
233 cast_to_largest_integral_type(parameter))
235 /* Assert that the given expression is true. */
236 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
238 /* Assert that the given expression is false. */
239 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
242 /* Assert that the given pointer is non-NULL. */
243 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
245 /* Assert that the given pointer is NULL. */
246 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
249 /* Assert that the two given integers are equal, otherwise fail. */
250 #define assert_int_equal(a, b) \
251 _assert_int_equal(cast_to_largest_integral_type(a), \
252 cast_to_largest_integral_type(b), \
254 /* Assert that the two given integers are not equal, otherwise fail. */
255 #define assert_int_not_equal(a, b) \
256 _assert_int_not_equal(cast_to_largest_integral_type(a), \
257 cast_to_largest_integral_type(b), \
260 /* Assert that the two given strings are equal, otherwise fail. */
261 #define assert_string_equal(a, b) \
262 _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
264 /* Assert that the two given strings are not equal, otherwise fail. */
265 #define assert_string_not_equal(a, b) \
266 _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
269 /* Assert that the two given areas of memory are equal, otherwise fail. */
270 #define assert_memory_equal(a, b, size) \
271 _assert_memory_equal((const char*)(a), (const char*)(b), size, __FILE__, \
273 /* Assert that the two given areas of memory are not equal, otherwise fail. */
274 #define assert_memory_not_equal(a, b, size) \
275 _assert_memory_not_equal((const char*)(a), (const char*)(b), size, \
278 /* Assert that the specified value is >= minimum and <= maximum. */
279 #define assert_in_range(value, minimum, maximum) \
281 cast_to_largest_integral_type(value), \
282 cast_to_largest_integral_type(minimum), \
283 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
285 /* Assert that the specified value is < minumum or > maximum */
286 #define assert_not_in_range(value, minimum, maximum) \
287 _assert_not_in_range( \
288 cast_to_largest_integral_type(value), \
289 cast_to_largest_integral_type(minimum), \
290 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
292 /* Assert that the specified value is within a set. */
293 #define assert_in_set(value, values, number_of_values) \
294 _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
295 /* Assert that the specified value is not within a set. */
296 #define assert_not_in_set(value, values, number_of_values) \
297 _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
300 /* Forces the test to fail immediately and quit. */
301 #define fail() _fail(__FILE__, __LINE__)
303 /* Generic method to kick off testing */
304 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
306 /* Initializes a UnitTest structure. */
307 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
308 #define unit_test_setup(test, setup) \
309 { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
310 #define unit_test_teardown(test, teardown) \
311 { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
314 * Initialize an array of UnitTest structures with a setup function for a test
315 * and a teardown function. Either setup or teardown can be NULL.
317 #define unit_test_setup_teardown(test, setup, teardown) \
318 unit_test_setup(test, setup), \
320 unit_test_teardown(test, teardown)
323 * Run tests specified by an array of UnitTest structures. The following
324 * example illustrates this macro's use with the unit_test macro.
329 * int main(int argc, char* argv[]) {
330 * const UnitTest tests[] = {
334 * return run_tests(tests);
337 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
339 /* Dynamic allocators */
340 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
341 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
342 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
344 /* Redirect malloc, calloc and free to the unit test allocators. */
346 #define malloc test_malloc
347 #define calloc test_calloc
348 #define free test_free
349 #endif /* UNIT_TESTING */
352 * Ensure mock_assert() is called. If mock_assert() is called the assert
353 * expression string is returned.
356 * #define assert mock_assert
358 * void showmessage(const char *message) {
362 * int main(int argc, const char* argv[]) {
363 * expect_assert_failure(show_message(NULL));
364 * printf("succeeded\n");
368 #define expect_assert_failure(function_call) \
370 const int result = setjmp(global_expect_assert_env); \
371 global_expecting_assert = 1; \
373 print_message("Expected assertion %s occurred\n", \
374 global_last_failed_assert); \
375 global_expecting_assert = 0; \
378 global_expecting_assert = 0; \
379 print_error("Expected assert in %s\n", #function_call); \
380 _fail(__FILE__, __LINE__); \
384 /* Function prototype for setup, test and teardown functions. */
385 typedef void (*UnitTestFunction)(void **state);
387 /* Function that determines whether a function parameter value is correct. */
388 typedef int (*CheckParameterValue)(const LargestIntegralType value,
389 const LargestIntegralType check_value_data);
391 /* Type of the unit test function. */
392 typedef enum UnitTestFunctionType {
393 UNIT_TEST_FUNCTION_TYPE_TEST = 0,
394 UNIT_TEST_FUNCTION_TYPE_SETUP,
395 UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
396 } UnitTestFunctionType;
399 * Stores a unit test function with its name and type.
400 * NOTE: Every setup function must be paired with a teardown function. It's
401 * possible to specify NULL function pointers.
403 typedef struct UnitTest {
405 UnitTestFunction function;
406 UnitTestFunctionType function_type;
410 /* Location within some source code. */
411 typedef struct SourceLocation {
416 /* Event that's called to check a parameter value. */
417 typedef struct CheckParameterEvent {
418 SourceLocation location;
419 const char *parameter_name;
420 CheckParameterValue check_value;
421 LargestIntegralType check_value_data;
422 } CheckParameterEvent;
424 /* Used by expect_assert_failure() and mock_assert(). */
425 extern int global_expecting_assert;
426 extern jmp_buf global_expect_assert_env;
427 extern const char * global_last_failed_assert;
429 /* Retrieves a value for the given function, as set by "will_return". */
430 LargestIntegralType _mock(const char * const function, const char* const file,
434 const char* const function, const char* const parameter,
435 const char* const file, const int line,
436 const CheckParameterValue check_function,
437 const LargestIntegralType check_data, CheckParameterEvent * const event,
441 const char* const function, const char* const parameter,
442 const char* const file, const int line, const LargestIntegralType values[],
443 const size_t number_of_values, const int count);
444 void _expect_not_in_set(
445 const char* const function, const char* const parameter,
446 const char* const file, const int line, const LargestIntegralType values[],
447 const size_t number_of_values, const int count);
449 void _expect_in_range(
450 const char* const function, const char* const parameter,
451 const char* const file, const int line,
452 const LargestIntegralType minimum,
453 const LargestIntegralType maximum, const int count);
454 void _expect_not_in_range(
455 const char* const function, const char* const parameter,
456 const char* const file, const int line,
457 const LargestIntegralType minimum,
458 const LargestIntegralType maximum, const int count);
461 const char* const function, const char* const parameter,
462 const char* const file, const int line, const LargestIntegralType value,
464 void _expect_not_value(
465 const char* const function, const char* const parameter,
466 const char* const file, const int line, const LargestIntegralType value,
470 const char* const function, const char* const parameter,
471 const char* const file, const int line, const char* string,
473 void _expect_not_string(
474 const char* const function, const char* const parameter,
475 const char* const file, const int line, const char* string,
479 const char* const function, const char* const parameter,
480 const char* const file, const int line, const void* const memory,
481 const size_t size, const int count);
482 void _expect_not_memory(
483 const char* const function, const char* const parameter,
484 const char* const file, const int line, const void* const memory,
485 const size_t size, const int count);
488 const char* const function, const char* const parameter,
489 const char* const file, const int line, const int count);
491 void _check_expected(
492 const char * const function_name, const char * const parameter_name,
493 const char* file, const int line, const LargestIntegralType value);
496 * Can be used to replace assert in tested code so that in conjuction with
497 * check_assert() it's possible to determine whether an assert condition has
498 * failed without stopping a test.
500 void mock_assert(const int result, const char* const expression,
501 const char * const file, const int line);
503 void _will_return(const char * const function_name, const char * const file,
504 const int line, const LargestIntegralType value,
506 void _assert_true(const LargestIntegralType result,
507 const char* const expression,
508 const char * const file, const int line);
509 void _assert_int_equal(
510 const LargestIntegralType a, const LargestIntegralType b,
511 const char * const file, const int line);
512 void _assert_int_not_equal(
513 const LargestIntegralType a, const LargestIntegralType b,
514 const char * const file, const int line);
515 void _assert_string_equal(const char * const a, const char * const b,
516 const char * const file, const int line);
517 void _assert_string_not_equal(const char * const a, const char * const b,
518 const char *file, const int line);
519 void _assert_memory_equal(const void * const a, const void * const b,
520 const size_t size, const char* const file,
522 void _assert_memory_not_equal(const void * const a, const void * const b,
523 const size_t size, const char* const file,
525 void _assert_in_range(
526 const LargestIntegralType value, const LargestIntegralType minimum,
527 const LargestIntegralType maximum, const char* const file, const int line);
528 void _assert_not_in_range(
529 const LargestIntegralType value, const LargestIntegralType minimum,
530 const LargestIntegralType maximum, const char* const file, const int line);
532 const LargestIntegralType value, const LargestIntegralType values[],
533 const size_t number_of_values, const char* const file, const int line);
534 void _assert_not_in_set(
535 const LargestIntegralType value, const LargestIntegralType values[],
536 const size_t number_of_values, const char* const file, const int line);
538 void* _test_malloc(const size_t size, const char* file, const int line);
539 void* _test_calloc(const size_t number_of_elements, const size_t size,
540 const char* file, const int line);
541 void _test_free(void* const ptr, const char* file, const int line);
543 void _fail(const char * const file, const int line);
545 const char * const function_name, const UnitTestFunction Function,
546 void ** const volatile state, const UnitTestFunctionType function_type,
547 const void* const heap_check_point);
548 int _run_tests(const UnitTest * const tests, const size_t number_of_tests);
550 /* Standard output and error print methods. */
551 void print_message(const char* const format, ...) PRINTF_ATTRIBUTE(1, 2);
552 void print_error(const char* const format, ...) PRINTF_ATTRIBUTE(1, 2);
553 void vprint_message(const char* const format, va_list args);
554 void vprint_error(const char* const format, va_list args);
556 #endif /* CMOCKA_H_ */