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 #define inline __inline
29 # endif /* __cplusplus */
30 int __stdcall IsDebuggerPresent();
33 # endif /* __cplusplus */
34 # endif /* _MSC_VER < 1500 */
35 # endif /* _MSC_VER */
39 * These headers or their equivalents should be included prior to including
46 * This allows test applications to use custom definitions of C standard
47 * library functions and types.
50 /* For those who are used to __func__ from gcc. */
52 #define __func__ __FUNCTION__
55 /* GCC have printf type attribute check. */
57 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) \
58 __attribute__ ((__format__ (__printf__, a, b)))
60 #define CMOCKA_PRINTF_ATTRIBUTE(a,b)
64 * @defgroup cmocka The CMocka API
66 * TODO Describe cmocka.
72 * Largest integral type. This type should be large enough to hold any
73 * pointer or integer supported by the compiler.
75 #ifndef LargestIntegralType
76 #define LargestIntegralType unsigned long long
77 #endif /* LargestIntegralType */
79 /* Printf format used to display LargestIntegralType. */
80 #ifndef LargestIntegralTypePrintfFormat
82 #define LargestIntegralTypePrintfFormat "0x%I64x"
84 #define LargestIntegralTypePrintfFormat "%#llx"
86 #endif /* LargestIntegralTypePrintfFormat */
88 /* Perform an unsigned cast to LargestIntegralType. */
89 #define cast_to_largest_integral_type(value) \
90 ((LargestIntegralType)((size_t)(value)))
92 /* Smallest integral type capable of holding a pointer. */
93 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED)
95 /* WIN32 is an ILP32 platform */
96 typedef unsigned int uintptr_t;
97 # elif defined(_WIN64)
98 typedef unsigned long int uintptr_t
101 /* ILP32 and LP64 platforms */
102 # ifdef __WORDSIZE /* glibc */
103 # if __WORDSIZE == 64
104 typedef unsigned long int uintptr_t;
106 typedef unsigned int uintptr_t;
107 # endif /* __WORDSIZE == 64 */
108 # else /* __WORDSIZE */
109 # if defined(_LP64) || defined(_I32LPx)
110 typedef unsigned long int uintptr_t;
112 typedef unsigned int uintptr_t;
114 # endif /* __WORDSIZE */
118 # define _UINTPTR_T_DEFINED
119 #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */
121 /* Perform an unsigned cast to uintptr_t. */
122 #define cast_to_pointer_integral_type(value) \
125 /* Perform a cast of a pointer to uintmax_t */
126 #define cast_ptr_to_largest_integral_type(value) \
127 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
130 * @defgroup cmocka_mock Mock Objects
133 * Mock objects mock objects are simulated objects that mimic the behavior of
134 * real objects. Instead of calling the real objects, the tested object calls a
135 * mock object that merely asserts that the correct methods were called, with
136 * the expected parameters, in the correct order.
139 * <li><strong>will_return(function, value)</strong> - The will_return() macro
140 * pushes a value onto a stack of mock values. This macro is intended to be
141 * used by the unit test itself, while programming the behaviour of the mocked
144 * <li><strong>mock()</strong> - the mock macro pops a value from a stack of
145 * test values. The user of the mock() macro is the mocked object that uses it
146 * to learn how it should behave.</li>
149 * Because the will_return() and mock() are intended to be used in pairs, the
150 * cmocka library would fail the test if there are more values pushed onto the
151 * stack using will_return() than consumed with mock() and vice-versa.
153 * The following unit test stub illustrates how would a unit test instruct the
154 * mock object to return a particular value:
157 * will_return(chef_cook, "hotdog");
158 * will_return(chef_cook, 0);
161 * Now the mock object can check if the parameter it received is the parameter
162 * which is expected by the test driver. This can be done the following way:
165 * int chef_cook(const char *order, char **dish_out)
167 * check_expected(order);
171 * For a complete example please at a look
172 * <a href="http://git.cryptomilk.org/projects/cmocka.git/tree/example/chef_wrap/waiter_test_wrap.c">here</a>.
179 * @brief Retrieve a return value of the current function.
181 * @return The value which was stored to return by this function.
187 #define mock() _mock(__func__, __FILE__, __LINE__)
192 * @brief Retrieve a typed return value of the current function.
194 * The value would be casted to type internally to avoid having the
195 * caller to do the cast manually.
197 * @param[in] #type The expected type of the return value
199 * @return The value which was stored to return by this function.
204 * param = mock_type(int);
209 * @see mock_ptr_type()
211 void *mock_type(#type);
213 #define mock_type(type) ((type) mock())
218 * @brief Retrieve a typed return value of the current function.
220 * The value would be casted to type internally to avoid having the
221 * caller to do the cast manually but also casted to uintptr_t to make
222 * sure the result has a valid size to be used as a pointer.
224 * @param[in] #type The expected type of the return value
226 * @return The value which was stored to return by this function.
231 * param = mock_ptr_type(char *);
238 void *mock_ptr_type(#type);
240 #define mock_ptr_type(type) ((type) (uintptr_t) mock())
246 * @brief Store a value to be returned by mock() later.
248 * @param[in] #function The function which should return the given value.
250 * @param[in] value The value to be returned by mock().
253 * int return_integer(void)
255 * return (int)mock();
258 * static void test_integer_return(void **state)
260 * will_return(return_integer, 42);
262 * assert_int_equal(my_function_calling_return_integer(), 42);
267 * @see will_return_count()
269 void will_return(#function, void *value);
271 #define will_return(function, value) \
272 _will_return(#function, __FILE__, __LINE__, \
273 cast_to_largest_integral_type(value), 1)
278 * @brief Store a value to be returned by mock() later.
280 * @param[in] #function The function which should return the given value.
282 * @param[in] value The value to be returned by mock().
284 * @param[in] count The parameter returns the number of times the value should
285 * be returned by mock(). If count is set to -1 the value will
286 * always be returned.
290 void will_return_count(#function, void *value, int count);
292 #define will_return_count(function, value, count) \
293 _will_return(#function, __FILE__, __LINE__, \
294 cast_to_largest_integral_type(value), count)
299 * @brief Store a value that will be always returned by mock().
301 * @param[in] #function The function which should return the given value.
303 * @param[in] value The value to be returned by mock().
305 * This is equivalent to:
307 * will_return_count(function, value, -1);
310 * @see will_return_count()
313 void will_return_always(#function, void *value);
315 #define will_return_always(function, value) \
316 will_return_count(function, (value), -1)
322 * @defgroup cmocka_param Checking Parameters
325 * Functionality to store expected values for mock function parameters.
327 * In addition to storing the return values of mock functions, cmocka provides
328 * functionality to store expected values for mock function parameters using
329 * the expect_*() functions provided. A mock function parameter can then be
330 * validated using the check_expected() macro.
332 * Successive calls to expect_*() macros for a parameter queues values to check
333 * the specified parameter. check_expected() checks a function parameter
334 * against the next value queued using expect_*(), if the parameter check fails
335 * a test failure is signalled. In addition if check_expected() is called and
336 * no more parameter values are queued a test failure occurs.
338 * The following test stub illustrates how to do this. First is the the function
339 * we call in the test driver:
342 * static void test_driver(void **state)
344 * expect_string(chef_cook, order, "hotdog");
348 * Now the chef_cook function can check if the parameter we got passed is the
349 * parameter which is expected by the test driver. This can be done the
353 * int chef_cook(const char *order, char **dish_out)
355 * check_expected(order);
359 * For a complete example please at a look at
360 * <a href="http://git.cryptomilk.org/projects/cmocka.git/tree/example/chef_wrap/waiter_test_wrap.c">here</a>
366 * Add a custom parameter checking function. If the event parameter is NULL
367 * the event structure is allocated internally by this function. If event
368 * parameter is provided it must be allocated on the heap and doesn't need to
369 * be deallocated by the caller.
373 * @brief Add a custom parameter checking function.
375 * If the event parameter is NULL the event structure is allocated internally
376 * by this function. If the parameter is provided it must be allocated on the
377 * heap and doesn't need to be deallocated by the caller.
379 * @param[in] #function The function to add a custom parameter checking
382 * @param[in] #parameter The parameters passed to the function.
384 * @param[in] #check_function The check function to call.
386 * @param[in] check_data The data to pass to the check function.
388 void expect_check(#function, #parameter, #check_function, const void *check_data);
390 #define expect_check(function, parameter, check_function, check_data) \
391 _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
392 cast_to_largest_integral_type(check_data), NULL, 1)
397 * @brief Add an event to check if the parameter value is part of the provided
400 * The event is triggered by calling check_expected() in the mocked function.
402 * @param[in] #function The function to add the check for.
404 * @param[in] #parameter The name of the parameter passed to the function.
406 * @param[in] value_array[] The array to check for the value.
408 * @see check_expected().
410 void expect_in_set(#function, #parameter, uintmax_t value_array[]);
412 #define expect_in_set(function, parameter, value_array) \
413 expect_in_set_count(function, parameter, value_array, 1)
418 * @brief Add an event to check if the parameter value is part of the provided
421 * The event is triggered by calling check_expected() in the mocked function.
423 * @param[in] #function The function to add the check for.
425 * @param[in] #parameter The name of the parameter passed to the function.
427 * @param[in] value_array[] The array to check for the value.
429 * @param[in] count The count parameter returns the number of times the value
430 * should be returned by check_expected(). If count is set
431 * to -1 the value will always be returned.
433 * @see check_expected().
435 void expect_in_set_count(#function, #parameter, uintmax_t value_array[], size_t count);
437 #define expect_in_set_count(function, parameter, value_array, count) \
438 _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
439 sizeof(value_array) / sizeof((value_array)[0]), count)
444 * @brief Add an event to check if the parameter value is not part of the
447 * The event is triggered by calling check_expected() in the mocked function.
449 * @param[in] #function The function to add the check for.
451 * @param[in] #parameter The name of the parameter passed to the function.
453 * @param[in] value_array[] The array to check for the value.
455 * @see check_expected().
457 void expect_not_in_set(#function, #parameter, uintmax_t value_array[]);
459 #define expect_not_in_set(function, parameter, value_array) \
460 expect_not_in_set_count(function, parameter, value_array, 1)
465 * @brief Add an event to check if the parameter value is not part of the
468 * The event is triggered by calling check_expected() in the mocked function.
470 * @param[in] #function The function to add the check for.
472 * @param[in] #parameter The name of the parameter passed to the function.
474 * @param[in] value_array[] The array to check for the value.
476 * @param[in] count The count parameter returns the number of times the value
477 * should be returned by check_expected(). If count is set
478 * to -1 the value will always be returned.
480 * @see check_expected().
482 void expect_not_in_set_count(#function, #parameter, uintmax_t value_array[], size_t count);
484 #define expect_not_in_set_count(function, parameter, value_array, count) \
485 _expect_not_in_set( \
486 #function, #parameter, __FILE__, __LINE__, value_array, \
487 sizeof(value_array) / sizeof((value_array)[0]), count)
493 * @brief Add an event to check a parameter is inside a numerical range.
494 * The check would succeed if minimum <= value <= maximum.
496 * The event is triggered by calling check_expected() in the mocked function.
498 * @param[in] #function The function to add the check for.
500 * @param[in] #parameter The name of the parameter passed to the function.
502 * @param[in] minimum The lower boundary of the interval to check against.
504 * @param[in] maximum The upper boundary of the interval to check against.
506 * @see check_expected().
508 void expect_in_range(#function, #parameter, uintmax_t minimum, uintmax_t maximum);
510 #define expect_in_range(function, parameter, minimum, maximum) \
511 expect_in_range_count(function, parameter, minimum, maximum, 1)
516 * @brief Add an event to repeatedly check a parameter is inside a
517 * numerical range. The check would succeed if minimum <= value <= maximum.
519 * The event is triggered by calling check_expected() in the mocked function.
521 * @param[in] #function The function to add the check for.
523 * @param[in] #parameter The name of the parameter passed to the function.
525 * @param[in] minimum The lower boundary of the interval to check against.
527 * @param[in] maximum The upper boundary of the interval to check against.
529 * @param[in] count The count parameter returns the number of times the value
530 * should be returned by check_expected(). If count is set
531 * to -1 the value will always be returned.
533 * @see check_expected().
535 void expect_in_range_count(#function, #parameter, uintmax_t minimum, uintmax_t maximum, size_t count);
537 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
538 _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
544 * @brief Add an event to check a parameter is outside a numerical range.
545 * The check would succeed if minimum > value > maximum.
547 * The event is triggered by calling check_expected() in the mocked function.
549 * @param[in] #function The function to add the check for.
551 * @param[in] #parameter The name of the parameter passed to the function.
553 * @param[in] minimum The lower boundary of the interval to check against.
555 * @param[in] maximum The upper boundary of the interval to check against.
557 * @see check_expected().
559 void expect_not_in_range(#function, #parameter, uintmax_t minimum, uintmax_t maximum);
561 #define expect_not_in_range(function, parameter, minimum, maximum) \
562 expect_not_in_range_count(function, parameter, minimum, maximum, 1)
567 * @brief Add an event to repeatedly check a parameter is outside a
568 * numerical range. The check would succeed if minimum > value > maximum.
570 * The event is triggered by calling check_expected() in the mocked function.
572 * @param[in] #function The function to add the check for.
574 * @param[in] #parameter The name of the parameter passed to the function.
576 * @param[in] minimum The lower boundary of the interval to check against.
578 * @param[in] maximum The upper boundary of the interval to check against.
580 * @param[in] count The count parameter returns the number of times the value
581 * should be returned by check_expected(). If count is set
582 * to -1 the value will always be returned.
584 * @see check_expected().
586 void expect_not_in_range_count(#function, #parameter, uintmax_t minimum, uintmax_t maximum, size_t count);
588 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
590 _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
591 minimum, maximum, count)
596 * @brief Add an event to check if a parameter is the given value.
598 * The event is triggered by calling check_expected() in the mocked function.
600 * @param[in] #function The function to add the check for.
602 * @param[in] #parameter The name of the parameter passed to the function.
604 * @param[in] value The value to check.
606 * @see check_expected().
608 void expect_value(#function, #parameter, uintmax_t value);
610 #define expect_value(function, parameter, value) \
611 expect_value_count(function, parameter, value, 1)
616 * @brief Add an event to repeatedly check if a parameter is the given value.
618 * The event is triggered by calling check_expected() in the mocked function.
620 * @param[in] #function The function to add the check for.
622 * @param[in] #parameter The name of the parameter passed to the function.
624 * @param[in] value The value to check.
626 * @param[in] count The count parameter returns the number of times the value
627 * should be returned by check_expected(). If count is set
628 * to -1 the value will always be returned.
630 * @see check_expected().
632 void expect_value_count(#function, #parameter, uintmax_t value, size_t count);
634 #define expect_value_count(function, parameter, value, count) \
635 _expect_value(#function, #parameter, __FILE__, __LINE__, \
636 cast_to_largest_integral_type(value), count)
641 * @brief Add an event to check if a parameter isn't the given value.
643 * The event is triggered by calling check_expected() in the mocked function.
645 * @param[in] #function The function to add the check for.
647 * @param[in] #parameter The name of the parameter passed to the function.
649 * @param[in] value The value to check.
651 * @see check_expected().
653 void expect_not_value(#function, #parameter, uintmax_t value);
655 #define expect_not_value(function, parameter, value) \
656 expect_not_value_count(function, parameter, value, 1)
661 * @brief Add an event to repeatedly check if a parameter isn't the given value.
663 * The event is triggered by calling check_expected() in the mocked function.
665 * @param[in] #function The function to add the check for.
667 * @param[in] #parameter The name of the parameter passed to the function.
669 * @param[in] value The value to check.
671 * @param[in] count The count parameter returns the number of times the value
672 * should be returned by check_expected(). If count is set
673 * to -1 the value will always be returned.
675 * @see check_expected().
677 void expect_not_value_count(#function, #parameter, uintmax_t value, size_t count);
679 #define expect_not_value_count(function, parameter, value, count) \
680 _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
681 cast_to_largest_integral_type(value), count)
686 * @brief Add an event to check if the parameter value is equal to the
689 * The event is triggered by calling check_expected() in the mocked function.
691 * @param[in] #function The function to add the check for.
693 * @param[in] #parameter The name of the parameter passed to the function.
695 * @param[in] string The string value to compare.
697 * @see check_expected().
699 void expect_string(#function, #parameter, const char *string);
701 #define expect_string(function, parameter, string) \
702 expect_string_count(function, parameter, string, 1)
707 * @brief Add an event to check if the parameter value is equal to the
710 * The event is triggered by calling check_expected() in the mocked function.
712 * @param[in] #function The function to add the check for.
714 * @param[in] #parameter The name of the parameter passed to the function.
716 * @param[in] string The string value to compare.
718 * @param[in] count The count parameter returns the number of times the value
719 * should be returned by check_expected(). If count is set
720 * to -1 the value will always be returned.
722 * @see check_expected().
724 void expect_string_count(#function, #parameter, const char *string, size_t count);
726 #define expect_string_count(function, parameter, string, count) \
727 _expect_string(#function, #parameter, __FILE__, __LINE__, \
728 (const char*)(string), count)
733 * @brief Add an event to check if the parameter value isn't equal to the
736 * The event is triggered by calling check_expected() in the mocked function.
738 * @param[in] #function The function to add the check for.
740 * @param[in] #parameter The name of the parameter passed to the function.
742 * @param[in] string The string value to compare.
744 * @see check_expected().
746 void expect_not_string(#function, #parameter, const char *string);
748 #define expect_not_string(function, parameter, string) \
749 expect_not_string_count(function, parameter, string, 1)
754 * @brief Add an event to check if the parameter value isn't equal to the
757 * The event is triggered by calling check_expected() in the mocked function.
759 * @param[in] #function The function to add the check for.
761 * @param[in] #parameter The name of the parameter passed to the function.
763 * @param[in] string The string value to compare.
765 * @param[in] count The count parameter returns the number of times the value
766 * should be returned by check_expected(). If count is set
767 * to -1 the value will always be returned.
769 * @see check_expected().
771 void expect_not_string_count(#function, #parameter, const char *string, size_t count);
773 #define expect_not_string_count(function, parameter, string, count) \
774 _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
775 (const char*)(string), count)
780 * @brief Add an event to check if the parameter does match an area of memory.
782 * The event is triggered by calling check_expected() in the mocked function.
784 * @param[in] #function The function to add the check for.
786 * @param[in] #parameter The name of the parameter passed to the function.
788 * @param[in] memory The memory to compare.
790 * @param[in] size The size of the memory to compare.
792 * @see check_expected().
794 void expect_memory(#function, #parameter, void *memory, size_t size);
796 #define expect_memory(function, parameter, memory, size) \
797 expect_memory_count(function, parameter, memory, size, 1)
802 * @brief Add an event to repeatedly check if the parameter does match an area
805 * The event is triggered by calling check_expected() in the mocked function.
807 * @param[in] #function The function to add the check for.
809 * @param[in] #parameter The name of the parameter passed to the function.
811 * @param[in] memory The memory to compare.
813 * @param[in] size The size of the memory to compare.
815 * @param[in] count The count parameter returns the number of times the value
816 * should be returned by check_expected(). If count is set
817 * to -1 the value will always be returned.
819 * @see check_expected().
821 void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
823 #define expect_memory_count(function, parameter, memory, size, count) \
824 _expect_memory(#function, #parameter, __FILE__, __LINE__, \
825 (const void*)(memory), size, count)
830 * @brief Add an event to check if the parameter doesn't match an area of
833 * The event is triggered by calling check_expected() in the mocked function.
835 * @param[in] #function The function to add the check for.
837 * @param[in] #parameter The name of the parameter passed to the function.
839 * @param[in] memory The memory to compare.
841 * @param[in] size The size of the memory to compare.
843 * @see check_expected().
845 void expect_not_memory(#function, #parameter, void *memory, size_t size);
847 #define expect_not_memory(function, parameter, memory, size) \
848 expect_not_memory_count(function, parameter, memory, size, 1)
853 * @brief Add an event to repeatedly check if the parameter doesn't match an
856 * The event is triggered by calling check_expected() in the mocked function.
858 * @param[in] #function The function to add the check for.
860 * @param[in] #parameter The name of the parameter passed to the function.
862 * @param[in] memory The memory to compare.
864 * @param[in] size The size of the memory to compare.
866 * @param[in] count The count parameter returns the number of times the value
867 * should be returned by check_expected(). If count is set
868 * to -1 the value will always be returned.
870 * @see check_expected().
872 void expect_not_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
874 #define expect_not_memory_count(function, parameter, memory, size, count) \
875 _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
876 (const void*)(memory), size, count)
882 * @brief Add an event to check if a parameter (of any value) has been passed.
884 * The event is triggered by calling check_expected() in the mocked function.
886 * @param[in] #function The function to add the check for.
888 * @param[in] #parameter The name of the parameter passed to the function.
890 * @see check_expected().
892 void expect_any(#function, #parameter);
894 #define expect_any(function, parameter) \
895 expect_any_count(function, parameter, 1)
900 * @brief Add an event to repeatedly check if a parameter (of any value) has
903 * The event is triggered by calling check_expected() in the mocked function.
905 * @param[in] #function The function to add the check for.
907 * @param[in] #parameter The name of the parameter passed to the function.
909 * @param[in] count The count parameter returns the number of times the value
910 * should be returned by check_expected(). If count is set
911 * to -1 the value will always be returned.
913 * @see check_expected().
915 void expect_any_count(#function, #parameter, size_t count);
917 #define expect_any_count(function, parameter, count) \
918 _expect_any(#function, #parameter, __FILE__, __LINE__, count)
923 * @brief Determine whether a function parameter is correct.
925 * This ensures the next value queued by one of the expect_*() macros matches
926 * the specified variable.
928 * This function needs to be called in the mock object.
930 * @param[in] #parameter The parameter to check.
932 void check_expected(#parameter);
934 #define check_expected(parameter) \
935 _check_expected(__func__, #parameter, __FILE__, __LINE__, \
936 cast_to_largest_integral_type(parameter))
942 * @defgroup cmocka_asserts Assert Macros
945 * This is a set of useful assert macros like the standard C libary's
948 * On an assertion failure a cmocka assert macro will write the failure to the
949 * standard error stream and signal a test failure. Due to limitations of the C
950 * language the general C standard library assert() and cmocka's assert_true()
951 * and assert_false() macros can only display the expression that caused the
952 * assert failure. cmocka's type specific assert macros, assert_{type}_equal()
953 * and assert_{type}_not_equal(), display the data that caused the assertion
954 * failure which increases data visibility aiding debugging of failing test
962 * @brief Assert that the given expression is true.
964 * The function prints an error message to standard error and terminates the
965 * test by calling fail() if expression is false (i.e., compares equal to
968 * @param[in] expression The expression to evaluate.
970 * @see assert_int_equal()
971 * @see assert_string_equal()
973 void assert_true(scalar expression);
975 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
981 * @brief Assert that the given expression is false.
983 * The function prints an error message to standard error and terminates the
984 * test by calling fail() if expression is true.
986 * @param[in] expression The expression to evaluate.
988 * @see assert_int_equal()
989 * @see assert_string_equal()
991 void assert_false(scalar expression);
993 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
999 * @brief Assert if the return_code is smaller than 0.
1001 * The function prints an error message to standard error and terminates the
1002 * test by calling fail() if the return code is smaller than 0. If the function
1003 * you check sets an errno if it fails you can pass it to the function and
1004 * it will be printed as part of the error message.
1006 * @param[in] rc The return code to evaluate.
1008 * @param[in] error Pass errno here or 0.
1010 void assert_return_code(int rc, int error);
1012 #define assert_return_code(rc, error) \
1013 _assert_return_code(cast_to_largest_integral_type(rc), \
1015 cast_to_largest_integral_type(error), \
1016 #rc, __FILE__, __LINE__)
1021 * @brief Assert that the given pointer is non-NULL.
1023 * The function prints an error message to standard error and terminates the
1024 * test by calling fail() if the pointer is non-NULL.
1026 * @param[in] pointer The pointer to evaluate.
1028 * @see assert_null()
1030 void assert_non_null(void *pointer);
1032 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
1038 * @brief Assert that the given pointer is NULL.
1040 * The function prints an error message to standard error and terminates the
1041 * test by calling fail() if the pointer is non-NULL.
1043 * @param[in] pointer The pointer to evaluate.
1045 * @see assert_non_null()
1047 void assert_null(void *pointer);
1049 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
1055 * @brief Assert that the two given integers are equal.
1057 * The function prints an error message to standard error and terminates the
1058 * test by calling fail() if the integers are not equal.
1060 * @param[in] a The first integer to compare.
1062 * @param[in] b The integer to compare against the first one.
1064 void assert_int_equal(int a, int b);
1066 #define assert_int_equal(a, b) \
1067 _assert_int_equal(cast_to_largest_integral_type(a), \
1068 cast_to_largest_integral_type(b), \
1074 * @brief Assert that the two given integers are not equal.
1076 * The function prints an error message to standard error and terminates the
1077 * test by calling fail() if the integers are equal.
1079 * @param[in] a The first integer to compare.
1081 * @param[in] b The integer to compare against the first one.
1083 * @see assert_int_equal()
1085 void assert_int_not_equal(int a, int b);
1087 #define assert_int_not_equal(a, b) \
1088 _assert_int_not_equal(cast_to_largest_integral_type(a), \
1089 cast_to_largest_integral_type(b), \
1095 * @brief Assert that the two given strings are equal.
1097 * The function prints an error message to standard error and terminates the
1098 * test by calling fail() if the strings are not equal.
1100 * @param[in] a The string to check.
1102 * @param[in] b The other string to compare.
1104 void assert_string_equal(const char *a, const char *b);
1106 #define assert_string_equal(a, b) \
1107 _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
1113 * @brief Assert that the two given strings are not equal.
1115 * The function prints an error message to standard error and terminates the
1116 * test by calling fail() if the strings are equal.
1118 * @param[in] a The string to check.
1120 * @param[in] b The other string to compare.
1122 void assert_string_not_equal(const char *a, const char *b);
1124 #define assert_string_not_equal(a, b) \
1125 _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
1131 * @brief Assert that the two given areas of memory are equal, otherwise fail.
1133 * The function prints an error message to standard error and terminates the
1134 * test by calling fail() if the memory is not equal.
1136 * @param[in] a The first memory area to compare
1137 * (interpreted as unsigned char).
1139 * @param[in] b The second memory area to compare
1140 * (interpreted as unsigned char).
1142 * @param[in] size The first n bytes of the memory areas to compare.
1144 void assert_memory_equal(const void *a, const void *b, size_t size);
1146 #define assert_memory_equal(a, b, size) \
1147 _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \
1153 * @brief Assert that the two given areas of memory are not equal.
1155 * The function prints an error message to standard error and terminates the
1156 * test by calling fail() if the memory is equal.
1158 * @param[in] a The first memory area to compare
1159 * (interpreted as unsigned char).
1161 * @param[in] b The second memory area to compare
1162 * (interpreted as unsigned char).
1164 * @param[in] size The first n bytes of the memory areas to compare.
1166 void assert_memory_not_equal(const void *a, const void *b, size_t size);
1168 #define assert_memory_not_equal(a, b, size) \
1169 _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
1175 * @brief Assert that the specified value is bigger than the minimum and
1176 * smaller than the maximum.
1178 * The function prints an error message to standard error and terminates the
1179 * test by calling fail() if value is not in range.
1181 * @param[in] value The value to check.
1183 * @param[in] minimum The minimum value allowed.
1185 * @param[in] maximum The maximum value allowed.
1187 void assert_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum);
1189 #define assert_in_range(value, minimum, maximum) \
1191 cast_to_largest_integral_type(value), \
1192 cast_to_largest_integral_type(minimum), \
1193 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1198 * @brief Assert that the specified value is smaller than the minimum and
1199 * bigger than the maximum.
1201 * The function prints an error message to standard error and terminates the
1202 * test by calling fail() if value is in range.
1204 * @param[in] value The value to check.
1206 * @param[in] minimum The minimum value to compare.
1208 * @param[in] maximum The maximum value to compare.
1210 void assert_not_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum);
1212 #define assert_not_in_range(value, minimum, maximum) \
1213 _assert_not_in_range( \
1214 cast_to_largest_integral_type(value), \
1215 cast_to_largest_integral_type(minimum), \
1216 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1221 * @brief Assert that the specified value is within a set.
1223 * The function prints an error message to standard error and terminates the
1224 * test by calling fail() if value is not within a set.
1226 * @param[in] value The value to look up
1228 * @param[in] values[] The array to check for the value.
1230 * @param[in] count The size of the values array.
1232 void assert_in_set(uintmax_t value, uintmax_t values[], size_t count);
1234 #define assert_in_set(value, values, number_of_values) \
1235 _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
1240 * @brief Assert that the specified value is not within a set.
1242 * The function prints an error message to standard error and terminates the
1243 * test by calling fail() if value is within a set.
1245 * @param[in] value The value to look up
1247 * @param[in] values[] The array to check for the value.
1249 * @param[in] count The size of the values array.
1251 void assert_not_in_set(uintmax_t value, uintmax_t values[], size_t count);
1253 #define assert_not_in_set(value, values, number_of_values) \
1254 _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
1260 * @defgroup cmocka_exec Running Tests
1263 * This is the way tests are executed with CMocka.
1265 * The following example illustrates this macro's use with the unit_test macro.
1268 * void Test0(void **state);
1269 * void Test1(void **state);
1273 * const UnitTest tests[] = {
1278 * return run_tests(tests);
1287 * @brief Forces the test to fail immediately and quit.
1291 #define fail() _fail(__FILE__, __LINE__)
1296 * @brief Forces the test to fail immediately and quit, printing the reason.
1298 void fail_msg(const char *msg, ...);
1300 #define fail_msg(msg, ...) do { \
1301 print_error("ERROR: " msg "\n", ##__VA_ARGS__); \
1308 * @brief Generic method to run a single test.
1310 * @param[in] #function The function to test.
1312 * @return 0 on success, 1 if an error occured.
1315 * // A test case that does nothing and succeeds.
1316 * void null_test_success(void **state) {
1320 * return run_test(null_test_success);
1324 int run_test(#function);
1326 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
1329 static inline void _unit_test_dummy(void **state) {
1333 /** Initializes a UnitTest structure. */
1334 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
1336 #define _unit_test_setup(test, setup) \
1337 { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
1339 /** Initializes a UnitTest structure with a setup function. */
1340 #define unit_test_setup(test, setup) \
1341 _unit_test_setup(test, setup), \
1343 _unit_test_teardown(test, _unit_test_dummy)
1345 #define _unit_test_teardown(test, teardown) \
1346 { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
1348 /** Initializes a UnitTest structure with a teardown function. */
1349 #define unit_test_teardown(test, teardown) \
1350 _unit_test_setup(test, _unit_test_dummy), \
1352 _unit_test_teardown(test, teardown)
1354 #define group_test_setup(setup) \
1355 { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }
1357 #define group_test_teardown(teardown) \
1358 { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }
1361 * Initialize an array of UnitTest structures with a setup function for a test
1362 * and a teardown function. Either setup or teardown can be NULL.
1364 #define unit_test_setup_teardown(test, setup, teardown) \
1365 _unit_test_setup(test, setup), \
1367 _unit_test_teardown(test, teardown)
1371 * @brief Run tests specified by an array of UnitTest structures.
1373 * @param[in] tests[] The array of unit tests to execute.
1375 * @return 0 on success, 1 if an error occured.
1378 * static void setup(void **state) {
1379 * int *answer = malloc(sizeof(int));
1380 * assert_non_null(answer);
1387 * static void teardown(void **state) {
1391 * static void null_test_success(void **state) {
1395 * static void int_test_success(void **state) {
1396 * int *answer = *state;
1397 * assert_int_equal(*answer, 42);
1401 * const UnitTest tests[] = {
1402 * unit_test(null_test_success),
1403 * unit_test_setup_teardown(int_test_success, setup, teardown),
1406 * return run_tests(tests);
1411 * @see unit_test_setup
1412 * @see unit_test_teardown
1413 * @see unit_test_setup_teardown
1415 int run_tests(const UnitTest tests[]);
1417 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
1420 #define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof(tests)[0])
1425 * @defgroup cmocka_alloc Dynamic Memory Allocation
1428 * Memory leaks, buffer overflows and underflows can be checked using cmocka.
1430 * To test for memory leaks, buffer overflows and underflows a module being
1431 * tested by cmocka should replace calls to malloc(), calloc() and free() to
1432 * test_malloc(), test_calloc() and test_free() respectively. Each time a block
1433 * is deallocated using test_free() it is checked for corruption, if a corrupt
1434 * block is found a test failure is signalled. All blocks allocated using the
1435 * test_*() allocation functions are tracked by the cmocka library. When a test
1436 * completes if any allocated blocks (memory leaks) remain they are reported
1437 * and a test failure is signalled.
1439 * For simplicity cmocka currently executes all tests in one process. Therefore
1440 * all test cases in a test application share a single address space which
1441 * means memory corruption from a single test case could potentially cause the
1442 * test application to exit prematurely.
1449 * @brief Test function overriding malloc.
1451 * @param[in] size The bytes which should be allocated.
1453 * @return A pointer to the allocated memory or NULL on error.
1456 * #ifdef UNIT_TESTING
1457 * extern void* _test_malloc(const size_t size, const char* file, const int line);
1459 * #define malloc(size) _test_malloc(size, __FILE__, __LINE__)
1462 * void leak_memory() {
1463 * int * const temporary = (int*)malloc(sizeof(int));
1470 void *test_malloc(size_t size);
1472 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
1477 * @brief Test function overriding calloc.
1479 * The memory is set to zero.
1481 * @param[in] nmemb The number of elements for an array to be allocated.
1483 * @param[in] size The size in bytes of each array element to allocate.
1485 * @return A pointer to the allocated memory, NULL on error.
1489 void *test_calloc(size_t nmemb, size_t size);
1491 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
1496 * @brief Test function overriding free(3).
1498 * @param[in] ptr The pointer to the memory space to free.
1502 void test_free(void *ptr);
1504 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
1507 /* Redirect malloc, calloc and free to the unit test allocators. */
1509 #define malloc test_malloc
1510 #define calloc test_calloc
1511 #define free test_free
1512 #endif /* UNIT_TESTING */
1518 * @defgroup cmocka_mock_assert Standard Assertions
1521 * How to handle assert(3) of the standard C library.
1523 * Runtime assert macros like the standard C library's assert() should be
1524 * redefined in modules being tested to use cmocka's mock_assert() function.
1525 * Normally mock_assert() signals a test failure. If a function is called using
1526 * the expect_assert_failure() macro, any calls to mock_assert() within the
1527 * function will result in the execution of the test. If no calls to
1528 * mock_assert() occur during the function called via expect_assert_failure() a
1529 * test failure is signalled.
1535 * @brief Function to replace assert(3) in tested code.
1537 * In conjuction with check_assert() it's possible to determine whether an
1538 * assert condition has failed without stopping a test.
1540 * @param[in] result The expression to assert.
1542 * @param[in] expression The expression as string.
1544 * @param[in] file The file mock_assert() is called.
1546 * @param[in] line The line mock_assert() is called.
1549 * #ifdef UNIT_TESTING
1550 * extern void mock_assert(const int result, const char* const expression,
1551 * const char * const file, const int line);
1554 * #define assert(expression) \
1555 * mock_assert((int)(expression), #expression, __FILE__, __LINE__);
1558 * void increment_value(int * const value) {
1565 * @see expect_assert_failure
1567 void mock_assert(const int result, const char* const expression,
1568 const char * const file, const int line);
1572 * @brief Ensure that mock_assert() is called.
1574 * If mock_assert() is called the assert expression string is returned.
1576 * @param[in] fn_call The function will will call mock_assert().
1579 * #define assert mock_assert
1581 * void showmessage(const char *message) {
1585 * int main(int argc, const char* argv[]) {
1586 * expect_assert_failure(show_message(NULL));
1587 * printf("succeeded\n");
1593 void expect_assert_failure(function fn_call);
1595 #define expect_assert_failure(function_call) \
1597 const int result = setjmp(global_expect_assert_env); \
1598 global_expecting_assert = 1; \
1600 print_message("Expected assertion %s occurred\n", \
1601 global_last_failed_assert); \
1602 global_expecting_assert = 0; \
1605 global_expecting_assert = 0; \
1606 print_error("Expected assert in %s\n", #function_call); \
1607 _fail(__FILE__, __LINE__); \
1614 /* Function prototype for setup, test and teardown functions. */
1615 typedef void (*UnitTestFunction)(void **state);
1617 /* Function that determines whether a function parameter value is correct. */
1618 typedef int (*CheckParameterValue)(const LargestIntegralType value,
1619 const LargestIntegralType check_value_data);
1621 /* Type of the unit test function. */
1622 typedef enum UnitTestFunctionType {
1623 UNIT_TEST_FUNCTION_TYPE_TEST = 0,
1624 UNIT_TEST_FUNCTION_TYPE_SETUP,
1625 UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
1626 UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP,
1627 UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN,
1628 } UnitTestFunctionType;
1631 * Stores a unit test function with its name and type.
1632 * NOTE: Every setup function must be paired with a teardown function. It's
1633 * possible to specify NULL function pointers.
1635 typedef struct UnitTest {
1637 UnitTestFunction function;
1638 UnitTestFunctionType function_type;
1641 typedef struct GroupTest {
1642 UnitTestFunction setup;
1643 UnitTestFunction teardown;
1644 const UnitTest *tests;
1645 const size_t number_of_tests;
1648 /* Location within some source code. */
1649 typedef struct SourceLocation {
1654 /* Event that's called to check a parameter value. */
1655 typedef struct CheckParameterEvent {
1656 SourceLocation location;
1657 const char *parameter_name;
1658 CheckParameterValue check_value;
1659 LargestIntegralType check_value_data;
1660 } CheckParameterEvent;
1662 /* Used by expect_assert_failure() and mock_assert(). */
1663 extern int global_expecting_assert;
1664 extern jmp_buf global_expect_assert_env;
1665 extern const char * global_last_failed_assert;
1667 /* Retrieves a value for the given function, as set by "will_return". */
1668 LargestIntegralType _mock(const char * const function, const char* const file,
1672 const char* const function, const char* const parameter,
1673 const char* const file, const int line,
1674 const CheckParameterValue check_function,
1675 const LargestIntegralType check_data, CheckParameterEvent * const event,
1678 void _expect_in_set(
1679 const char* const function, const char* const parameter,
1680 const char* const file, const int line, const LargestIntegralType values[],
1681 const size_t number_of_values, const int count);
1682 void _expect_not_in_set(
1683 const char* const function, const char* const parameter,
1684 const char* const file, const int line, const LargestIntegralType values[],
1685 const size_t number_of_values, const int count);
1687 void _expect_in_range(
1688 const char* const function, const char* const parameter,
1689 const char* const file, const int line,
1690 const LargestIntegralType minimum,
1691 const LargestIntegralType maximum, const int count);
1692 void _expect_not_in_range(
1693 const char* const function, const char* const parameter,
1694 const char* const file, const int line,
1695 const LargestIntegralType minimum,
1696 const LargestIntegralType maximum, const int count);
1699 const char* const function, const char* const parameter,
1700 const char* const file, const int line, const LargestIntegralType value,
1702 void _expect_not_value(
1703 const char* const function, const char* const parameter,
1704 const char* const file, const int line, const LargestIntegralType value,
1707 void _expect_string(
1708 const char* const function, const char* const parameter,
1709 const char* const file, const int line, const char* string,
1711 void _expect_not_string(
1712 const char* const function, const char* const parameter,
1713 const char* const file, const int line, const char* string,
1716 void _expect_memory(
1717 const char* const function, const char* const parameter,
1718 const char* const file, const int line, const void* const memory,
1719 const size_t size, const int count);
1720 void _expect_not_memory(
1721 const char* const function, const char* const parameter,
1722 const char* const file, const int line, const void* const memory,
1723 const size_t size, const int count);
1726 const char* const function, const char* const parameter,
1727 const char* const file, const int line, const int count);
1729 void _check_expected(
1730 const char * const function_name, const char * const parameter_name,
1731 const char* file, const int line, const LargestIntegralType value);
1733 void _will_return(const char * const function_name, const char * const file,
1734 const int line, const LargestIntegralType value,
1736 void _assert_true(const LargestIntegralType result,
1737 const char* const expression,
1738 const char * const file, const int line);
1739 void _assert_return_code(const LargestIntegralType result,
1741 const LargestIntegralType error,
1742 const char * const expression,
1743 const char * const file,
1745 void _assert_int_equal(
1746 const LargestIntegralType a, const LargestIntegralType b,
1747 const char * const file, const int line);
1748 void _assert_int_not_equal(
1749 const LargestIntegralType a, const LargestIntegralType b,
1750 const char * const file, const int line);
1751 void _assert_string_equal(const char * const a, const char * const b,
1752 const char * const file, const int line);
1753 void _assert_string_not_equal(const char * const a, const char * const b,
1754 const char *file, const int line);
1755 void _assert_memory_equal(const void * const a, const void * const b,
1756 const size_t size, const char* const file,
1758 void _assert_memory_not_equal(const void * const a, const void * const b,
1759 const size_t size, const char* const file,
1761 void _assert_in_range(
1762 const LargestIntegralType value, const LargestIntegralType minimum,
1763 const LargestIntegralType maximum, const char* const file, const int line);
1764 void _assert_not_in_range(
1765 const LargestIntegralType value, const LargestIntegralType minimum,
1766 const LargestIntegralType maximum, const char* const file, const int line);
1767 void _assert_in_set(
1768 const LargestIntegralType value, const LargestIntegralType values[],
1769 const size_t number_of_values, const char* const file, const int line);
1770 void _assert_not_in_set(
1771 const LargestIntegralType value, const LargestIntegralType values[],
1772 const size_t number_of_values, const char* const file, const int line);
1774 void* _test_malloc(const size_t size, const char* file, const int line);
1775 void* _test_calloc(const size_t number_of_elements, const size_t size,
1776 const char* file, const int line);
1777 void _test_free(void* const ptr, const char* file, const int line);
1779 void _fail(const char * const file, const int line);
1781 const char * const function_name, const UnitTestFunction Function,
1782 void ** const volatile state, const UnitTestFunctionType function_type,
1783 const void* const heap_check_point);
1784 int _run_tests(const UnitTest * const tests, const size_t number_of_tests);
1785 int _run_group_tests(const UnitTest * const tests,
1786 const size_t number_of_tests);
1788 /* Standard output and error print methods. */
1789 void print_message(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
1790 void print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
1791 void vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
1792 void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
1796 #endif /* CMOCKA_H_ */