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 * @defgroup cmocka The CMocka API
58 * TODO Describe cmocka.
64 * Largest integral type. This type should be large enough to hold any
65 * pointer or integer supported by the compiler.
67 #ifndef LargestIntegralType
68 #define LargestIntegralType unsigned long long
69 #endif /* LargestIntegralType */
71 /* Printf format used to display LargestIntegralType. */
72 #ifndef LargestIntegralTypePrintfFormat
74 #define LargestIntegralTypePrintfFormat "%I64x"
76 #define LargestIntegralTypePrintfFormat "%llx"
78 #endif /* LargestIntegralTypePrintfFormat */
80 /* Perform an unsigned cast to LargestIntegralType. */
81 #define cast_to_largest_integral_type(value) \
82 ((LargestIntegralType)((size_t)(value)))
84 /* Smallest integral type capable of holding a pointer. */
89 /* WIN32 is an ILP32 platform */
90 typedef unsigned long uintptr_t;
92 /* what about 64-bit windows?
93 * what's the right preprocessor symbol?
94 typedef unsigned long long uintptr_t */
98 /* ILP32 and LP64 platforms */
99 #ifdef __WORDSIZE /* glibc */
100 # if __WORDSIZE == 64
101 typedef unsigned long int uintptr_t;
103 typedef unsigned int uintptr_t;
104 # endif /* __WORDSIZE == 64 */
105 #else /* __WORDSIZE */
107 # if defined(_LP64) || defined(_I32LPx)
108 typedef unsigned long int uintptr_t;
110 typedef unsigned int uintptr_t;
113 #endif /* __WORDSIZE */
116 #endif /* _UINTPTR_T */
118 /* Perform an unsigned cast to uintptr_t. */
119 #define cast_to_pointer_integral_type(value) \
122 /* Perform a cast of a pointer to uintmax_t */
123 #define cast_ptr_to_largest_integral_type(value) \
124 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
127 * @defgroup cmocka_mock Mock Objects
130 * Mock objects mock objects are simulated objects that mimic the behavior of
131 * real objects. Instead of calling the real objects, the tested object calls a
132 * mock object that merely asserts that the correct methods were called, with
133 * the expected parameters, in the correct order.
140 * @brief Retrieve a return value of the current function.
142 * @return The value which was stored to return by this function.
148 #define mock() _mock(__func__, __FILE__, __LINE__)
153 * @brief Store a value to be returned by mock() later.
155 * @param[in] #function The function which should return the given value.
157 * @param[in] value The value to be returned by mock().
160 * int return_integer(void)
162 * return (int)mock();
165 * static void test_integer_return(void **state)
167 * will_return(return_integer, 42);
169 * assert_int_equal(my_function_calling_return_integer(), 42);
174 * @see will_return_count()
176 void will_return(#function, void *value);
178 #define will_return(function, value) \
179 _will_return(#function, __FILE__, __LINE__, \
180 cast_to_largest_integral_type(value), 1)
185 * @brief Store a value to be returned by mock() later.
187 * @param[in] #function The function which should return the given value.
189 * @param[in] value The value to be returned by mock().
191 * @param[in] count The parameter returns the number of times the value should
192 * be returned by mock(). If count is set to -1 the value will
193 * always be returned.
197 void will_return_count(#function, void *value, int count);
199 #define will_return_count(function, value, count) \
200 _will_return(#function, __FILE__, __LINE__, \
201 cast_to_largest_integral_type(value), count)
207 * Add a custom parameter checking function. If the event parameter is NULL
208 * the event structure is allocated internally by this function. If event
209 * parameter is provided it must be allocated on the heap and doesn't need to
210 * be deallocated by the caller.
212 #define expect_check(function, parameter, check_function, check_data) \
213 _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
214 cast_to_largest_integral_type(check_data), NULL, 0)
217 * Add an event to check a parameter, using check_expected(), against a set of
218 * values. See will_return() for a description of the count parameter.
220 #define expect_in_set(function, parameter, value_array) \
221 expect_in_set_count(function, parameter, value_array, 1)
222 #define expect_in_set_count(function, parameter, value_array, count) \
223 _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
224 sizeof(value_array) / sizeof((value_array)[0]), count)
225 #define expect_not_in_set(function, parameter, value_array) \
226 expect_not_in_set_count(function, parameter, value_array, 1)
227 #define expect_not_in_set_count(function, parameter, value_array, count) \
228 _expect_not_in_set( \
229 #function, #parameter, __FILE__, __LINE__, value_array, \
230 sizeof(value_array) / sizeof((value_array)[0]), count)
234 * Add an event to check a parameter, using check_expected(), against a
235 * signed range. Where range is minimum <= value <= maximum.
236 * See will_return() for a description of the count parameter.
238 #define expect_in_range(function, parameter, minimum, maximum) \
239 expect_in_range_count(function, parameter, minimum, maximum, 1)
240 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
241 _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
245 * Add an event to check a parameter, using check_expected(), against a
246 * signed range. Where range is value < minimum or value > maximum.
247 * See will_return() for a description of the count parameter.
249 #define expect_not_in_range(function, parameter, minimum, maximum) \
250 expect_not_in_range_count(function, parameter, minimum, maximum, 1)
251 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
253 _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
254 minimum, maximum, count)
257 * Add an event to check whether a parameter, using check_expected(), is or
258 * isn't a value. See will_return() for a description of the count parameter.
260 #define expect_value(function, parameter, value) \
261 expect_value_count(function, parameter, value, 1)
262 #define expect_value_count(function, parameter, value, count) \
263 _expect_value(#function, #parameter, __FILE__, __LINE__, \
264 cast_to_largest_integral_type(value), count)
265 #define expect_not_value(function, parameter, value) \
266 expect_not_value_count(function, parameter, value, 1)
267 #define expect_not_value_count(function, parameter, value, count) \
268 _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
269 cast_to_largest_integral_type(value), count)
272 * Add an event to check whether a parameter, using check_expected(),
273 * is or isn't a string. See will_return() for a description of the count
276 #define expect_string(function, parameter, string) \
277 expect_string_count(function, parameter, string, 1)
278 #define expect_string_count(function, parameter, string, count) \
279 _expect_string(#function, #parameter, __FILE__, __LINE__, \
280 (const char*)(string), count)
281 #define expect_not_string(function, parameter, string) \
282 expect_not_string_count(function, parameter, string, 1)
283 #define expect_not_string_count(function, parameter, string, count) \
284 _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
285 (const char*)(string), count)
288 * Add an event to check whether a parameter, using check_expected() does or
289 * doesn't match an area of memory. See will_return() for a description of
290 * the count parameter.
292 #define expect_memory(function, parameter, memory, size) \
293 expect_memory_count(function, parameter, memory, size, 1)
294 #define expect_memory_count(function, parameter, memory, size, count) \
295 _expect_memory(#function, #parameter, __FILE__, __LINE__, \
296 (const void*)(memory), size, count)
297 #define expect_not_memory(function, parameter, memory, size) \
298 expect_not_memory_count(function, parameter, memory, size, 1)
299 #define expect_not_memory_count(function, parameter, memory, size, count) \
300 _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
301 (const void*)(memory), size, count)
305 * Add an event to allow any value for a parameter checked using
306 * check_expected(). See will_return() for a description of the count
309 #define expect_any(function, parameter) \
310 expect_any_count(function, parameter, 1)
311 #define expect_any_count(function, parameter, count) \
312 _expect_any(#function, #parameter, __FILE__, __LINE__, count)
315 * Determine whether a function parameter is correct. This ensures the next
316 * value queued by one of the expect_*() macros matches the specified variable.
318 #define check_expected(parameter) \
319 _check_expected(__func__, #parameter, __FILE__, __LINE__, \
320 cast_to_largest_integral_type(parameter))
322 /* Assert that the given expression is true. */
323 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
325 /* Assert that the given expression is false. */
326 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
329 /* Assert that the given pointer is non-NULL. */
330 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
332 /* Assert that the given pointer is NULL. */
333 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
336 /* Assert that the two given integers are equal, otherwise fail. */
337 #define assert_int_equal(a, b) \
338 _assert_int_equal(cast_to_largest_integral_type(a), \
339 cast_to_largest_integral_type(b), \
341 /* Assert that the two given integers are not equal, otherwise fail. */
342 #define assert_int_not_equal(a, b) \
343 _assert_int_not_equal(cast_to_largest_integral_type(a), \
344 cast_to_largest_integral_type(b), \
347 /* Assert that the two given strings are equal, otherwise fail. */
348 #define assert_string_equal(a, b) \
349 _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
351 /* Assert that the two given strings are not equal, otherwise fail. */
352 #define assert_string_not_equal(a, b) \
353 _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
356 /* Assert that the two given areas of memory are equal, otherwise fail. */
357 #define assert_memory_equal(a, b, size) \
358 _assert_memory_equal((const char*)(a), (const char*)(b), size, __FILE__, \
360 /* Assert that the two given areas of memory are not equal, otherwise fail. */
361 #define assert_memory_not_equal(a, b, size) \
362 _assert_memory_not_equal((const char*)(a), (const char*)(b), size, \
365 /* Assert that the specified value is >= minimum and <= maximum. */
366 #define assert_in_range(value, minimum, maximum) \
368 cast_to_largest_integral_type(value), \
369 cast_to_largest_integral_type(minimum), \
370 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
372 /* Assert that the specified value is < minumum or > maximum */
373 #define assert_not_in_range(value, minimum, maximum) \
374 _assert_not_in_range( \
375 cast_to_largest_integral_type(value), \
376 cast_to_largest_integral_type(minimum), \
377 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
379 /* Assert that the specified value is within a set. */
380 #define assert_in_set(value, values, number_of_values) \
381 _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
382 /* Assert that the specified value is not within a set. */
383 #define assert_not_in_set(value, values, number_of_values) \
384 _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
387 /* Forces the test to fail immediately and quit. */
388 #define fail() _fail(__FILE__, __LINE__)
390 /* Generic method to kick off testing */
391 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
393 /* Initializes a UnitTest structure. */
394 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
395 #define unit_test_setup(test, setup) \
396 { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
397 #define unit_test_teardown(test, teardown) \
398 { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
401 * Initialize an array of UnitTest structures with a setup function for a test
402 * and a teardown function. Either setup or teardown can be NULL.
404 #define unit_test_setup_teardown(test, setup, teardown) \
405 unit_test_setup(test, setup), \
407 unit_test_teardown(test, teardown)
410 * Run tests specified by an array of UnitTest structures. The following
411 * example illustrates this macro's use with the unit_test macro.
416 * int main(int argc, char* argv[]) {
417 * const UnitTest tests[] = {
421 * return run_tests(tests);
424 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
426 /* Dynamic allocators */
427 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
428 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
429 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
431 /* Redirect malloc, calloc and free to the unit test allocators. */
433 #define malloc test_malloc
434 #define calloc test_calloc
435 #define free test_free
436 #endif /* UNIT_TESTING */
439 * Ensure mock_assert() is called. If mock_assert() is called the assert
440 * expression string is returned.
443 * #define assert mock_assert
445 * void showmessage(const char *message) {
449 * int main(int argc, const char* argv[]) {
450 * expect_assert_failure(show_message(NULL));
451 * printf("succeeded\n");
455 #define expect_assert_failure(function_call) \
457 const int result = setjmp(global_expect_assert_env); \
458 global_expecting_assert = 1; \
460 print_message("Expected assertion %s occurred\n", \
461 global_last_failed_assert); \
462 global_expecting_assert = 0; \
465 global_expecting_assert = 0; \
466 print_error("Expected assert in %s\n", #function_call); \
467 _fail(__FILE__, __LINE__); \
471 /* Function prototype for setup, test and teardown functions. */
472 typedef void (*UnitTestFunction)(void **state);
474 /* Function that determines whether a function parameter value is correct. */
475 typedef int (*CheckParameterValue)(const LargestIntegralType value,
476 const LargestIntegralType check_value_data);
478 /* Type of the unit test function. */
479 typedef enum UnitTestFunctionType {
480 UNIT_TEST_FUNCTION_TYPE_TEST = 0,
481 UNIT_TEST_FUNCTION_TYPE_SETUP,
482 UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
483 } UnitTestFunctionType;
486 * Stores a unit test function with its name and type.
487 * NOTE: Every setup function must be paired with a teardown function. It's
488 * possible to specify NULL function pointers.
490 typedef struct UnitTest {
492 UnitTestFunction function;
493 UnitTestFunctionType function_type;
497 /* Location within some source code. */
498 typedef struct SourceLocation {
503 /* Event that's called to check a parameter value. */
504 typedef struct CheckParameterEvent {
505 SourceLocation location;
506 const char *parameter_name;
507 CheckParameterValue check_value;
508 LargestIntegralType check_value_data;
509 } CheckParameterEvent;
511 /* Used by expect_assert_failure() and mock_assert(). */
512 extern int global_expecting_assert;
513 extern jmp_buf global_expect_assert_env;
514 extern const char * global_last_failed_assert;
516 /* Retrieves a value for the given function, as set by "will_return". */
517 LargestIntegralType _mock(const char * const function, const char* const file,
521 const char* const function, const char* const parameter,
522 const char* const file, const int line,
523 const CheckParameterValue check_function,
524 const LargestIntegralType check_data, CheckParameterEvent * const event,
528 const char* const function, const char* const parameter,
529 const char* const file, const int line, const LargestIntegralType values[],
530 const size_t number_of_values, const int count);
531 void _expect_not_in_set(
532 const char* const function, const char* const parameter,
533 const char* const file, const int line, const LargestIntegralType values[],
534 const size_t number_of_values, const int count);
536 void _expect_in_range(
537 const char* const function, const char* const parameter,
538 const char* const file, const int line,
539 const LargestIntegralType minimum,
540 const LargestIntegralType maximum, const int count);
541 void _expect_not_in_range(
542 const char* const function, const char* const parameter,
543 const char* const file, const int line,
544 const LargestIntegralType minimum,
545 const LargestIntegralType maximum, const int count);
548 const char* const function, const char* const parameter,
549 const char* const file, const int line, const LargestIntegralType value,
551 void _expect_not_value(
552 const char* const function, const char* const parameter,
553 const char* const file, const int line, const LargestIntegralType value,
557 const char* const function, const char* const parameter,
558 const char* const file, const int line, const char* string,
560 void _expect_not_string(
561 const char* const function, const char* const parameter,
562 const char* const file, const int line, const char* string,
566 const char* const function, const char* const parameter,
567 const char* const file, const int line, const void* const memory,
568 const size_t size, const int count);
569 void _expect_not_memory(
570 const char* const function, const char* const parameter,
571 const char* const file, const int line, const void* const memory,
572 const size_t size, const int count);
575 const char* const function, const char* const parameter,
576 const char* const file, const int line, const int count);
578 void _check_expected(
579 const char * const function_name, const char * const parameter_name,
580 const char* file, const int line, const LargestIntegralType value);
583 * Can be used to replace assert in tested code so that in conjuction with
584 * check_assert() it's possible to determine whether an assert condition has
585 * failed without stopping a test.
587 void mock_assert(const int result, const char* const expression,
588 const char * const file, const int line);
590 void _will_return(const char * const function_name, const char * const file,
591 const int line, const LargestIntegralType value,
593 void _assert_true(const LargestIntegralType result,
594 const char* const expression,
595 const char * const file, const int line);
596 void _assert_int_equal(
597 const LargestIntegralType a, const LargestIntegralType b,
598 const char * const file, const int line);
599 void _assert_int_not_equal(
600 const LargestIntegralType a, const LargestIntegralType b,
601 const char * const file, const int line);
602 void _assert_string_equal(const char * const a, const char * const b,
603 const char * const file, const int line);
604 void _assert_string_not_equal(const char * const a, const char * const b,
605 const char *file, const int line);
606 void _assert_memory_equal(const void * const a, const void * const b,
607 const size_t size, const char* const file,
609 void _assert_memory_not_equal(const void * const a, const void * const b,
610 const size_t size, const char* const file,
612 void _assert_in_range(
613 const LargestIntegralType value, const LargestIntegralType minimum,
614 const LargestIntegralType maximum, const char* const file, const int line);
615 void _assert_not_in_range(
616 const LargestIntegralType value, const LargestIntegralType minimum,
617 const LargestIntegralType maximum, const char* const file, const int line);
619 const LargestIntegralType value, const LargestIntegralType values[],
620 const size_t number_of_values, const char* const file, const int line);
621 void _assert_not_in_set(
622 const LargestIntegralType value, const LargestIntegralType values[],
623 const size_t number_of_values, const char* const file, const int line);
625 void* _test_malloc(const size_t size, const char* file, const int line);
626 void* _test_calloc(const size_t number_of_elements, const size_t size,
627 const char* file, const int line);
628 void _test_free(void* const ptr, const char* file, const int line);
630 void _fail(const char * const file, const int line);
632 const char * const function_name, const UnitTestFunction Function,
633 void ** const volatile state, const UnitTestFunctionType function_type,
634 const void* const heap_check_point);
635 int _run_tests(const UnitTest * const tests, const size_t number_of_tests);
637 /* Standard output and error print methods. */
638 void print_message(const char* const format, ...) PRINTF_ATTRIBUTE(1, 2);
639 void print_error(const char* const format, ...) PRINTF_ATTRIBUTE(1, 2);
640 void vprint_message(const char* const format, va_list args) PRINTF_ATTRIBUTE(1, 0);
641 void vprint_error(const char* const format, va_list args) PRINTF_ATTRIBUTE(1, 0);
645 #endif /* CMOCKA_H_ */