ae6802d00fdd875926b56c5a33df9db53d17bcbe
[platform/upstream/cmocka.git] / src / 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  * Largest integral type.  This type should be large enough to hold any
57  * pointer or integer supported by the compiler.
58  */
59 #ifndef LargestIntegralType
60 #define LargestIntegralType unsigned long long
61 #endif /* LargestIntegralType */
62
63 /* Printf format used to display LargestIntegralType. */
64 #ifndef LargestIntegralTypePrintfFormat
65 #ifdef _WIN32
66 #define LargestIntegralTypePrintfFormat "%I64x"
67 #else
68 #define LargestIntegralTypePrintfFormat "%llx"
69 #endif /* _WIN32 */
70 #endif /* LargestIntegralTypePrintfFormat */
71
72 /* Perform an unsigned cast to LargestIntegralType. */
73 #define cast_to_largest_integral_type(value) \
74     ((LargestIntegralType)((unsigned)(size_t)(value)))
75
76 /* Smallest integral type capable of holding a pointer. */
77 #ifndef _UINTPTR_T
78 #define _UINTPTR_T
79 #ifdef _WIN32
80
81 /* WIN32 is an ILP32 platform */
82 typedef unsigned long uintptr_t;
83
84 #else /* _WIN32 */
85
86 /* what about 64-bit windows?
87  * what's the right preprocessor symbol?
88 typedef unsigned long long uintptr_t */
89
90 /* ILP32 and LP64 platforms */
91 typedef unsigned long uintptr_t;
92
93 #endif /* _WIN32 */
94 #endif /* _UINTPTR_T */
95
96 /* Perform an unsigned cast to uintptr_t. */
97 #define cast_to_pointer_integral_type(value) \
98     ((uintptr_t)(value))
99
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))
103
104 /* Retrieves a return value for the current function. */
105 #define mock() _mock(__func__, __FILE__, __LINE__)
106
107 /*
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.
111  */
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)
118
119 /*
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.
124  */
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)
128
129 /*
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.
132  */
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)
144
145
146 /*
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.
150  */
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, \
155                      maximum, count)
156
157 /*
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.
161  */
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, \
165                                   count) \
166     _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
167                          minimum, maximum, count)
168
169 /*
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.
172  */
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)
183
184 /*
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
187  * parameter.
188  */
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)
199
200 /*
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.
204  */
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)
215
216
217 /*
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
220  * parameter.
221  */
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)
226
227 /*
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.
230  */
231 #define check_expected(parameter) \
232     _check_expected(__func__, #parameter, __FILE__, __LINE__, \
233                     cast_to_largest_integral_type(parameter))
234
235 /* Assert that the given expression is true. */
236 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
237                                     __FILE__, __LINE__)
238 /* Assert that the given expression is false. */
239 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
240                                      __FILE__, __LINE__)
241
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, \
244 __FILE__, __LINE__)
245 /* Assert that the given pointer is NULL. */
246 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
247 __FILE__, __LINE__)
248
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), \
253                       __FILE__, __LINE__)
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), \
258                           __FILE__, __LINE__)
259
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__, \
263                          __LINE__)
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__, \
267                              __LINE__)
268
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__, \
272                          __LINE__)
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, \
276                              __FILE__, __LINE__)
277
278 /* Assert that the specified value is >= minimum and <= maximum. */
279 #define assert_in_range(value, minimum, maximum) \
280     _assert_in_range( \
281         cast_to_largest_integral_type(value), \
282         cast_to_largest_integral_type(minimum), \
283         cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
284
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__)
291
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__)
298
299
300 /* Forces the test to fail immediately and quit. */
301 #define fail() _fail(__FILE__, __LINE__)
302
303 /* Generic method to kick off testing */
304 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
305
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 }
312
313 /*
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.
316  */
317 #define unit_test_setup_teardown(test, setup, teardown) \
318     unit_test_setup(test, setup), \
319     unit_test(test), \
320     unit_test_teardown(test, teardown)
321
322 /*
323  * Run tests specified by an array of UnitTest structures.  The following
324  * example illustrates this macro's use with the unit_test macro.
325  *
326  * void Test0();
327  * void Test1();
328  *
329  * int main(int argc, char* argv[]) {
330  *     const UnitTest tests[] = {
331  *         unit_test(Test0);
332  *         unit_test(Test1);
333  *     };
334  *     return run_tests(tests);
335  * }
336  */
337 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
338
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__)
343
344 /* Redirect malloc, calloc and free to the unit test allocators. */
345 #if UNIT_TESTING
346 #define malloc test_malloc
347 #define calloc test_calloc
348 #define free test_free
349 #endif /* UNIT_TESTING */
350
351 /*
352  * Ensure mock_assert() is called.  If mock_assert() is called the assert
353  * expression string is returned.
354  * For example:
355  *
356  * #define assert mock_assert
357  *
358  * void showmessage(const char *message) {
359  *   assert(message);
360  * }
361  *
362  * int main(int argc, const char* argv[]) {
363  *   expect_assert_failure(show_message(NULL));
364  *   printf("succeeded\n");
365  *   return 0;
366  * }
367  */
368 #define expect_assert_failure(function_call) \
369   { \
370     const int result = setjmp(global_expect_assert_env); \
371     global_expecting_assert = 1; \
372     if (result) { \
373       print_message("Expected assertion %s occurred\n", \
374                     global_last_failed_assert); \
375       global_expecting_assert = 0; \
376     } else { \
377       function_call ; \
378       global_expecting_assert = 0; \
379       print_error("Expected assert in %s\n", #function_call); \
380       _fail(__FILE__, __LINE__); \
381     } \
382   }
383
384 /* Function prototype for setup, test and teardown functions. */
385 typedef void (*UnitTestFunction)(void **state);
386
387 /* Function that determines whether a function parameter value is correct. */
388 typedef int (*CheckParameterValue)(const LargestIntegralType value,
389                                    const LargestIntegralType check_value_data);
390
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;
397
398 /*
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.
402  */
403 typedef struct UnitTest {
404     const char* name;
405     UnitTestFunction function;
406     UnitTestFunctionType function_type;
407 } UnitTest;
408
409
410 /* Location within some source code. */
411 typedef struct SourceLocation {
412     const char* file;
413     int line;
414 } SourceLocation;
415
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;
423
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;
428
429 /* Retrieves a value for the given function, as set by "will_return". */
430 LargestIntegralType _mock(const char * const function, const char* const file,
431                           const int line);
432
433 void _expect_check(
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,
438     const int count);
439
440 void _expect_in_set(
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);
448
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);
459
460 void _expect_value(
461     const char* const function, const char* const parameter,
462     const char* const file, const int line, const LargestIntegralType value,
463     const int count);
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,
467     const int count);
468
469 void _expect_string(
470     const char* const function, const char* const parameter,
471     const char* const file, const int line, const char* string,
472     const int count);
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,
476     const int count);
477
478 void _expect_memory(
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);
486
487 void _expect_any(
488     const char* const function, const char* const parameter,
489     const char* const file, const int line, const int count);
490
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);
494
495 /*
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.
499  */
500 void mock_assert(const int result, const char* const expression,
501                  const char * const file, const int line);
502
503 void _will_return(const char * const function_name, const char * const file,
504                   const int line, const LargestIntegralType value,
505                   const int count);
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,
521                           const int line);
522 void _assert_memory_not_equal(const void * const a, const void * const b,
523                               const size_t size, const char* const file,
524                               const int line);
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);
531 void _assert_in_set(
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);
537
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);
542
543 void _fail(const char * const file, const int line);
544 int _run_test(
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);
549
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) PRINTF_ATTRIBUTE(1, 0);
554 void vprint_error(const char* const format, va_list args) PRINTF_ATTRIBUTE(1, 0);
555
556 #endif /* CMOCKA_H_ */