include: Add a group for mock objects.
[obnox/cmocka.git] / include / cmocka.h
1 /*
2  * Copyright 2008 Google Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #ifndef CMOCKA_H_
17 #define CMOCKA_H_
18
19 #ifdef _WIN32
20 #if _MSC_VER < 1500
21 #ifdef __cplusplus
22 extern "C" {
23 #endif   /* __cplusplus */
24 int __stdcall IsDebuggerPresent();
25 #ifdef __cplusplus
26 } /* extern "C" */
27 #endif   /* __cplusplus */
28 #endif  /* _MSC_VER < 1500 */
29 #endif  /* _WIN32 */
30
31 /*
32  * These headers or their equivalents should be included prior to including
33  * this header file.
34  *
35  * #include <stdarg.h>
36  * #include <stddef.h>
37  * #include <setjmp.h>
38  *
39  * This allows test applications to use custom definitions of C standard
40  * library functions and types.
41  */
42
43 /* For those who are used to __func__ from gcc. */
44 #ifndef __func__
45 #define __func__ __FUNCTION__
46 #endif
47
48 /* GCC have printf type attribute check.  */
49 #ifdef __GNUC__
50 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
51 #else
52 #define PRINTF_ATTRIBUTE(a,b)
53 #endif /* __GNUC__ */
54
55 /**
56  * @defgroup cmocka The CMocka API
57  *
58  * TODO Describe cmocka.
59  *
60  * @{
61  */
62
63 /*
64  * Largest integral type.  This type should be large enough to hold any
65  * pointer or integer supported by the compiler.
66  */
67 #ifndef LargestIntegralType
68 #define LargestIntegralType unsigned long long
69 #endif /* LargestIntegralType */
70
71 /* Printf format used to display LargestIntegralType. */
72 #ifndef LargestIntegralTypePrintfFormat
73 #ifdef _WIN32
74 #define LargestIntegralTypePrintfFormat "%I64x"
75 #else
76 #define LargestIntegralTypePrintfFormat "%llx"
77 #endif /* _WIN32 */
78 #endif /* LargestIntegralTypePrintfFormat */
79
80 /* Perform an unsigned cast to LargestIntegralType. */
81 #define cast_to_largest_integral_type(value) \
82     ((LargestIntegralType)((size_t)(value)))
83
84 /* Smallest integral type capable of holding a pointer. */
85 #ifndef _UINTPTR_T
86 #define _UINTPTR_T
87 #ifdef _WIN32
88
89 /* WIN32 is an ILP32 platform */
90 typedef unsigned long uintptr_t;
91
92 /* what about 64-bit windows?
93  * what's the right preprocessor symbol?
94 typedef unsigned long long uintptr_t */
95
96 #else /* _WIN32 */
97
98 /* ILP32 and LP64 platforms */
99 #ifdef __WORDSIZE /* glibc */
100 # if __WORDSIZE == 64
101 typedef unsigned long int uintptr_t;
102 # else
103 typedef unsigned int uintptr_t;
104 # endif /* __WORDSIZE == 64 */
105 #else /* __WORDSIZE */
106
107 # if defined(_LP64) || defined(_I32LPx)
108 typedef unsigned long int uintptr_t;
109 # else
110 typedef unsigned int uintptr_t;
111 # endif
112
113 #endif /* __WORDSIZE */
114
115 #endif /* _WIN32 */
116 #endif /* _UINTPTR_T */
117
118 /* Perform an unsigned cast to uintptr_t. */
119 #define cast_to_pointer_integral_type(value) \
120     ((uintptr_t)(value))
121
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))
125
126 /**
127  * @defgroup cmocka_mock Mock Objects
128  * @ingroup cmocka.
129  *
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.
134  *
135  * @{
136  */
137
138 #ifdef DOXYGEN
139 /**
140  * @brief Retrieve a return value of the current function.
141  *
142  * @return The value which was stored to return by this function.
143  *
144  * @see will_return()
145  */
146 void *mock(void);
147 #else
148 #define mock() _mock(__func__, __FILE__, __LINE__)
149 #endif
150
151 #ifdef DOXYGEN
152 /**
153  * @brief Store a value to be returned by mock() later.
154  *
155  * @param[in]  #function  The function which should return the given value.
156  *
157  * @param[in]  value The value to be returned by mock().
158  *
159  * @code
160  * int return_integer(void)
161  * {
162  *      return (int)mock();
163  * }
164  *
165  * static void test_integer_return(void **state)
166  * {
167  *      will_return(return_integer, 42);
168  *
169  *      assert_int_equal(my_function_calling_return_integer(), 42);
170  * }
171  * @endcode
172  *
173  * @see mock()
174  * @see will_return_count()
175  */
176 void will_return(#function, void *value);
177 #else
178 #define will_return(function, value) \
179     _will_return(#function, __FILE__, __LINE__, \
180                  cast_to_largest_integral_type(value), 1)
181 #endif
182
183 #ifdef DOXYGEN
184 /**
185  * @brief Store a value to be returned by mock() later.
186  *
187  * @param[in]  #function  The function which should return the given value.
188  *
189  * @param[in]  value The value to be returned by mock().
190  *
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.
194  *
195  * @see mock()
196  */
197 void will_return_count(#function, void *value, int count);
198 #else
199 #define will_return_count(function, value, count) \
200     _will_return(#function, __FILE__, __LINE__, \
201                  cast_to_largest_integral_type(value), count)
202 #endif
203
204 /** @} */
205
206 /*
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.
211  */
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)
215
216 /*
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.
219  */
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)
231
232
233 /*
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.
237  */
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, \
242                      maximum, count)
243
244 /*
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.
248  */
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, \
252                                   count) \
253     _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
254                          minimum, maximum, count)
255
256 /*
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.
259  */
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)
270
271 /*
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
274  * parameter.
275  */
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)
286
287 /*
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.
291  */
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)
302
303
304 /*
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
307  * parameter.
308  */
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)
313
314 /*
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.
317  */
318 #define check_expected(parameter) \
319     _check_expected(__func__, #parameter, __FILE__, __LINE__, \
320                     cast_to_largest_integral_type(parameter))
321
322 /* Assert that the given expression is true. */
323 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
324                                     __FILE__, __LINE__)
325 /* Assert that the given expression is false. */
326 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
327                                      __FILE__, __LINE__)
328
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, \
331 __FILE__, __LINE__)
332 /* Assert that the given pointer is NULL. */
333 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
334 __FILE__, __LINE__)
335
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), \
340                       __FILE__, __LINE__)
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), \
345                           __FILE__, __LINE__)
346
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__, \
350                          __LINE__)
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__, \
354                              __LINE__)
355
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__, \
359                          __LINE__)
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, \
363                              __FILE__, __LINE__)
364
365 /* Assert that the specified value is >= minimum and <= maximum. */
366 #define assert_in_range(value, minimum, maximum) \
367     _assert_in_range( \
368         cast_to_largest_integral_type(value), \
369         cast_to_largest_integral_type(minimum), \
370         cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
371
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__)
378
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__)
385
386
387 /* Forces the test to fail immediately and quit. */
388 #define fail() _fail(__FILE__, __LINE__)
389
390 /* Generic method to kick off testing */
391 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
392
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 }
399
400 /*
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.
403  */
404 #define unit_test_setup_teardown(test, setup, teardown) \
405     unit_test_setup(test, setup), \
406     unit_test(test), \
407     unit_test_teardown(test, teardown)
408
409 /*
410  * Run tests specified by an array of UnitTest structures.  The following
411  * example illustrates this macro's use with the unit_test macro.
412  *
413  * void Test0();
414  * void Test1();
415  *
416  * int main(int argc, char* argv[]) {
417  *     const UnitTest tests[] = {
418  *         unit_test(Test0);
419  *         unit_test(Test1);
420  *     };
421  *     return run_tests(tests);
422  * }
423  */
424 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
425
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__)
430
431 /* Redirect malloc, calloc and free to the unit test allocators. */
432 #if UNIT_TESTING
433 #define malloc test_malloc
434 #define calloc test_calloc
435 #define free test_free
436 #endif /* UNIT_TESTING */
437
438 /*
439  * Ensure mock_assert() is called.  If mock_assert() is called the assert
440  * expression string is returned.
441  * For example:
442  *
443  * #define assert mock_assert
444  *
445  * void showmessage(const char *message) {
446  *   assert(message);
447  * }
448  *
449  * int main(int argc, const char* argv[]) {
450  *   expect_assert_failure(show_message(NULL));
451  *   printf("succeeded\n");
452  *   return 0;
453  * }
454  */
455 #define expect_assert_failure(function_call) \
456   { \
457     const int result = setjmp(global_expect_assert_env); \
458     global_expecting_assert = 1; \
459     if (result) { \
460       print_message("Expected assertion %s occurred\n", \
461                     global_last_failed_assert); \
462       global_expecting_assert = 0; \
463     } else { \
464       function_call ; \
465       global_expecting_assert = 0; \
466       print_error("Expected assert in %s\n", #function_call); \
467       _fail(__FILE__, __LINE__); \
468     } \
469   }
470
471 /* Function prototype for setup, test and teardown functions. */
472 typedef void (*UnitTestFunction)(void **state);
473
474 /* Function that determines whether a function parameter value is correct. */
475 typedef int (*CheckParameterValue)(const LargestIntegralType value,
476                                    const LargestIntegralType check_value_data);
477
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;
484
485 /*
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.
489  */
490 typedef struct UnitTest {
491     const char* name;
492     UnitTestFunction function;
493     UnitTestFunctionType function_type;
494 } UnitTest;
495
496
497 /* Location within some source code. */
498 typedef struct SourceLocation {
499     const char* file;
500     int line;
501 } SourceLocation;
502
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;
510
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;
515
516 /* Retrieves a value for the given function, as set by "will_return". */
517 LargestIntegralType _mock(const char * const function, const char* const file,
518                           const int line);
519
520 void _expect_check(
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,
525     const int count);
526
527 void _expect_in_set(
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);
535
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);
546
547 void _expect_value(
548     const char* const function, const char* const parameter,
549     const char* const file, const int line, const LargestIntegralType value,
550     const int count);
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,
554     const int count);
555
556 void _expect_string(
557     const char* const function, const char* const parameter,
558     const char* const file, const int line, const char* string,
559     const int count);
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,
563     const int count);
564
565 void _expect_memory(
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);
573
574 void _expect_any(
575     const char* const function, const char* const parameter,
576     const char* const file, const int line, const int count);
577
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);
581
582 /*
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.
586  */
587 void mock_assert(const int result, const char* const expression,
588                  const char * const file, const int line);
589
590 void _will_return(const char * const function_name, const char * const file,
591                   const int line, const LargestIntegralType value,
592                   const int count);
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,
608                           const int line);
609 void _assert_memory_not_equal(const void * const a, const void * const b,
610                               const size_t size, const char* const file,
611                               const int line);
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);
618 void _assert_in_set(
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);
624
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);
629
630 void _fail(const char * const file, const int line);
631 int _run_test(
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);
636
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);
642
643 /** @} */
644
645 #endif /* CMOCKA_H_ */