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 * @defgroup cmocka The CMocka API
41 * These headers or their equivalents should be included prior to including
48 * This allows test applications to use custom definitions of C standard
49 * library functions and types.
54 /* For those who are used to __func__ from gcc. */
56 #define __func__ __FUNCTION__
60 * Largest integral type. This type should be large enough to hold any
61 * pointer or integer supported by the compiler.
63 #ifndef LargestIntegralType
65 # define LargestIntegralType unsigned long int
67 # define LargestIntegralType unsigned long long int
69 #endif /* LargestIntegralType */
71 /* Printf format used to display LargestIntegralType. */
72 #ifndef LargestIntegralTypePrintfFormat
74 # define LargestIntegralTypePrintfFormat "0x%I64x"
77 # define LargestIntegralTypePrintfFormat "%#lx"
79 # define LargestIntegralTypePrintfFormat "%#llx"
82 #endif /* LargestIntegralTypePrintfFormat */
84 /* Perform an unsigned cast to LargestIntegralType. */
85 #define cast_to_largest_integral_type(value) \
86 ((LargestIntegralType)(value))
88 /* Smallest integral type capable of holding a pointer. */
89 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED)
91 /* WIN32 is an ILP32 platform */
92 typedef unsigned int uintptr_t;
93 # elif defined(_WIN64)
94 typedef unsigned long int uintptr_t
97 /* ILP32 and LP64 platforms */
98 # ifdef __WORDSIZE /* glibc */
100 typedef unsigned long int uintptr_t;
102 typedef unsigned int uintptr_t;
103 # endif /* __WORDSIZE == 64 */
104 # else /* __WORDSIZE */
105 # if defined(_LP64) || defined(_I32LPx)
106 typedef unsigned long int uintptr_t;
108 typedef unsigned int uintptr_t;
110 # endif /* __WORDSIZE */
114 # define _UINTPTR_T_DEFINED
115 #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */
117 /* Perform an unsigned cast to uintptr_t. */
118 #define cast_to_pointer_integral_type(value) \
119 ((uintptr_t)((size_t)(value)))
121 /* Perform a cast of a pointer to uintmax_t */
122 #define cast_ptr_to_largest_integral_type(value) \
123 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
125 /* GCC have printf type attribute check. */
127 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) \
128 __attribute__ ((__format__ (__printf__, a, b)))
130 #define CMOCKA_PRINTF_ATTRIBUTE(a,b)
131 #endif /* __GNUC__ */
133 #if defined(__GNUC__)
134 #define CMOCKA_DEPRECATED __attribute__ ((deprecated))
135 #elif defined(_MSC_VER)
136 #define CMOCKA_DEPRECATED __declspec(deprecated)
138 #define CMOCKA_DEPRECATED
142 * @defgroup cmocka_mock Mock Objects
145 * Mock objects mock objects are simulated objects that mimic the behavior of
146 * real objects. Instead of calling the real objects, the tested object calls a
147 * mock object that merely asserts that the correct methods were called, with
148 * the expected parameters, in the correct order.
151 * <li><strong>will_return(function, value)</strong> - The will_return() macro
152 * pushes a value onto a stack of mock values. This macro is intended to be
153 * used by the unit test itself, while programming the behaviour of the mocked
156 * <li><strong>mock()</strong> - the mock macro pops a value from a stack of
157 * test values. The user of the mock() macro is the mocked object that uses it
158 * to learn how it should behave.</li>
161 * Because the will_return() and mock() are intended to be used in pairs, the
162 * cmocka library would fail the test if there are more values pushed onto the
163 * stack using will_return() than consumed with mock() and vice-versa.
165 * The following unit test stub illustrates how would a unit test instruct the
166 * mock object to return a particular value:
169 * will_return(chef_cook, "hotdog");
170 * will_return(chef_cook, 0);
173 * Now the mock object can check if the parameter it received is the parameter
174 * which is expected by the test driver. This can be done the following way:
177 * int chef_cook(const char *order, char **dish_out)
179 * check_expected(order);
183 * For a complete example please at a look
184 * <a href="http://git.cryptomilk.org/projects/cmocka.git/tree/example/chef_wrap/waiter_test_wrap.c">here</a>.
191 * @brief Retrieve a return value of the current function.
193 * @return The value which was stored to return by this function.
199 #define mock() _mock(__func__, __FILE__, __LINE__)
204 * @brief Retrieve a typed return value of the current function.
206 * The value would be casted to type internally to avoid having the
207 * caller to do the cast manually.
209 * @param[in] #type The expected type of the return value
211 * @return The value which was stored to return by this function.
216 * param = mock_type(int);
221 * @see mock_ptr_type()
223 void *mock_type(#type);
225 #define mock_type(type) ((type) mock())
230 * @brief Retrieve a typed return value of the current function.
232 * The value would be casted to type internally to avoid having the
233 * caller to do the cast manually but also casted to uintptr_t to make
234 * sure the result has a valid size to be used as a pointer.
236 * @param[in] #type The expected type of the return value
238 * @return The value which was stored to return by this function.
243 * param = mock_ptr_type(char *);
250 void *mock_ptr_type(#type);
252 #define mock_ptr_type(type) ((type) (uintptr_t) mock())
258 * @brief Store a value to be returned by mock() later.
260 * @param[in] #function The function which should return the given value.
262 * @param[in] value The value to be returned by mock().
265 * int return_integer(void)
267 * return (int)mock();
270 * static void test_integer_return(void **state)
272 * will_return(return_integer, 42);
274 * assert_int_equal(my_function_calling_return_integer(), 42);
279 * @see will_return_count()
281 void will_return(#function, void *value);
283 #define will_return(function, value) \
284 _will_return(#function, __FILE__, __LINE__, \
285 cast_to_largest_integral_type(value), 1)
290 * @brief Store a value to be returned by mock() later.
292 * @param[in] #function The function which should return the given value.
294 * @param[in] value The value to be returned by mock().
296 * @param[in] count The parameter returns the number of times the value should
297 * be returned by mock(). If count is set to -1 the value will
298 * always be returned.
302 void will_return_count(#function, void *value, int count);
304 #define will_return_count(function, value, count) \
305 _will_return(#function, __FILE__, __LINE__, \
306 cast_to_largest_integral_type(value), count)
311 * @brief Store a value that will be always returned by mock().
313 * @param[in] #function The function which should return the given value.
315 * @param[in] value The value to be returned by mock().
317 * This is equivalent to:
319 * will_return_count(function, value, -1);
322 * @see will_return_count()
325 void will_return_always(#function, void *value);
327 #define will_return_always(function, value) \
328 will_return_count(function, (value), -1)
334 * @defgroup cmocka_param Checking Parameters
337 * Functionality to store expected values for mock function parameters.
339 * In addition to storing the return values of mock functions, cmocka provides
340 * functionality to store expected values for mock function parameters using
341 * the expect_*() functions provided. A mock function parameter can then be
342 * validated using the check_expected() macro.
344 * Successive calls to expect_*() macros for a parameter queues values to check
345 * the specified parameter. check_expected() checks a function parameter
346 * against the next value queued using expect_*(), if the parameter check fails
347 * a test failure is signalled. In addition if check_expected() is called and
348 * no more parameter values are queued a test failure occurs.
350 * The following test stub illustrates how to do this. First is the the function
351 * we call in the test driver:
354 * static void test_driver(void **state)
356 * expect_string(chef_cook, order, "hotdog");
360 * Now the chef_cook function can check if the parameter we got passed is the
361 * parameter which is expected by the test driver. This can be done the
365 * int chef_cook(const char *order, char **dish_out)
367 * check_expected(order);
371 * For a complete example please at a look at
372 * <a href="http://git.cryptomilk.org/projects/cmocka.git/tree/example/chef_wrap/waiter_test_wrap.c">here</a>
378 * Add a custom parameter checking function. If the event parameter is NULL
379 * the event structure is allocated internally by this function. If event
380 * parameter is provided it must be allocated on the heap and doesn't need to
381 * be deallocated by the caller.
385 * @brief Add a custom parameter checking function.
387 * If the event parameter is NULL the event structure is allocated internally
388 * by this function. If the parameter is provided it must be allocated on the
389 * heap and doesn't need to be deallocated by the caller.
391 * @param[in] #function The function to add a custom parameter checking
394 * @param[in] #parameter The parameters passed to the function.
396 * @param[in] #check_function The check function to call.
398 * @param[in] check_data The data to pass to the check function.
400 void expect_check(#function, #parameter, #check_function, const void *check_data);
402 #define expect_check(function, parameter, check_function, check_data) \
403 _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
404 cast_to_largest_integral_type(check_data), NULL, 1)
409 * @brief Add an event to check if the parameter value is part of the provided
412 * The event is triggered by calling check_expected() in the mocked function.
414 * @param[in] #function The function to add the check for.
416 * @param[in] #parameter The name of the parameter passed to the function.
418 * @param[in] value_array[] The array to check for the value.
420 * @see check_expected().
422 void expect_in_set(#function, #parameter, uintmax_t value_array[]);
424 #define expect_in_set(function, parameter, value_array) \
425 expect_in_set_count(function, parameter, value_array, 1)
430 * @brief Add an event to check if the parameter value is part of the provided
433 * The event is triggered by calling check_expected() in the mocked function.
435 * @param[in] #function The function to add the check for.
437 * @param[in] #parameter The name of the parameter passed to the function.
439 * @param[in] value_array[] The array to check for the value.
441 * @param[in] count The count parameter returns the number of times the value
442 * should be returned by check_expected(). If count is set
443 * to -1 the value will always be returned.
445 * @see check_expected().
447 void expect_in_set_count(#function, #parameter, uintmax_t value_array[], size_t count);
449 #define expect_in_set_count(function, parameter, value_array, count) \
450 _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
451 sizeof(value_array) / sizeof((value_array)[0]), count)
456 * @brief Add an event to check if the parameter value is not part of the
459 * The event is triggered by calling check_expected() in the mocked function.
461 * @param[in] #function The function to add the check for.
463 * @param[in] #parameter The name of the parameter passed to the function.
465 * @param[in] value_array[] The array to check for the value.
467 * @see check_expected().
469 void expect_not_in_set(#function, #parameter, uintmax_t value_array[]);
471 #define expect_not_in_set(function, parameter, value_array) \
472 expect_not_in_set_count(function, parameter, value_array, 1)
477 * @brief Add an event to check if the parameter value is not part of the
480 * The event is triggered by calling check_expected() in the mocked function.
482 * @param[in] #function The function to add the check for.
484 * @param[in] #parameter The name of the parameter passed to the function.
486 * @param[in] value_array[] The array to check for the value.
488 * @param[in] count The count parameter returns the number of times the value
489 * should be returned by check_expected(). If count is set
490 * to -1 the value will always be returned.
492 * @see check_expected().
494 void expect_not_in_set_count(#function, #parameter, uintmax_t value_array[], size_t count);
496 #define expect_not_in_set_count(function, parameter, value_array, count) \
497 _expect_not_in_set( \
498 #function, #parameter, __FILE__, __LINE__, value_array, \
499 sizeof(value_array) / sizeof((value_array)[0]), count)
505 * @brief Add an event to check a parameter is inside a numerical range.
506 * The check would succeed if minimum <= value <= maximum.
508 * The event is triggered by calling check_expected() in the mocked function.
510 * @param[in] #function The function to add the check for.
512 * @param[in] #parameter The name of the parameter passed to the function.
514 * @param[in] minimum The lower boundary of the interval to check against.
516 * @param[in] maximum The upper boundary of the interval to check against.
518 * @see check_expected().
520 void expect_in_range(#function, #parameter, uintmax_t minimum, uintmax_t maximum);
522 #define expect_in_range(function, parameter, minimum, maximum) \
523 expect_in_range_count(function, parameter, minimum, maximum, 1)
528 * @brief Add an event to repeatedly check a parameter is inside a
529 * numerical range. The check would succeed if minimum <= value <= maximum.
531 * The event is triggered by calling check_expected() in the mocked function.
533 * @param[in] #function The function to add the check for.
535 * @param[in] #parameter The name of the parameter passed to the function.
537 * @param[in] minimum The lower boundary of the interval to check against.
539 * @param[in] maximum The upper boundary of the interval to check against.
541 * @param[in] count The count parameter returns the number of times the value
542 * should be returned by check_expected(). If count is set
543 * to -1 the value will always be returned.
545 * @see check_expected().
547 void expect_in_range_count(#function, #parameter, uintmax_t minimum, uintmax_t maximum, size_t count);
549 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
550 _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
556 * @brief Add an event to check a parameter is outside a numerical range.
557 * The check would succeed if minimum > value > maximum.
559 * The event is triggered by calling check_expected() in the mocked function.
561 * @param[in] #function The function to add the check for.
563 * @param[in] #parameter The name of the parameter passed to the function.
565 * @param[in] minimum The lower boundary of the interval to check against.
567 * @param[in] maximum The upper boundary of the interval to check against.
569 * @see check_expected().
571 void expect_not_in_range(#function, #parameter, uintmax_t minimum, uintmax_t maximum);
573 #define expect_not_in_range(function, parameter, minimum, maximum) \
574 expect_not_in_range_count(function, parameter, minimum, maximum, 1)
579 * @brief Add an event to repeatedly check a parameter is outside a
580 * numerical range. The check would succeed if minimum > value > maximum.
582 * The event is triggered by calling check_expected() in the mocked function.
584 * @param[in] #function The function to add the check for.
586 * @param[in] #parameter The name of the parameter passed to the function.
588 * @param[in] minimum The lower boundary of the interval to check against.
590 * @param[in] maximum The upper boundary of the interval to check against.
592 * @param[in] count The count parameter returns the number of times the value
593 * should be returned by check_expected(). If count is set
594 * to -1 the value will always be returned.
596 * @see check_expected().
598 void expect_not_in_range_count(#function, #parameter, uintmax_t minimum, uintmax_t maximum, size_t count);
600 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
602 _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
603 minimum, maximum, count)
608 * @brief Add an event to check if a parameter is the given value.
610 * The event is triggered by calling check_expected() in the mocked function.
612 * @param[in] #function The function to add the check for.
614 * @param[in] #parameter The name of the parameter passed to the function.
616 * @param[in] value The value to check.
618 * @see check_expected().
620 void expect_value(#function, #parameter, uintmax_t value);
622 #define expect_value(function, parameter, value) \
623 expect_value_count(function, parameter, value, 1)
628 * @brief Add an event to repeatedly check if a parameter is the given value.
630 * The event is triggered by calling check_expected() in the mocked function.
632 * @param[in] #function The function to add the check for.
634 * @param[in] #parameter The name of the parameter passed to the function.
636 * @param[in] value The value to check.
638 * @param[in] count The count parameter returns the number of times the value
639 * should be returned by check_expected(). If count is set
640 * to -1 the value will always be returned.
642 * @see check_expected().
644 void expect_value_count(#function, #parameter, uintmax_t value, size_t count);
646 #define expect_value_count(function, parameter, value, count) \
647 _expect_value(#function, #parameter, __FILE__, __LINE__, \
648 cast_to_largest_integral_type(value), count)
653 * @brief Add an event to check if a parameter isn't the given value.
655 * The event is triggered by calling check_expected() in the mocked function.
657 * @param[in] #function The function to add the check for.
659 * @param[in] #parameter The name of the parameter passed to the function.
661 * @param[in] value The value to check.
663 * @see check_expected().
665 void expect_not_value(#function, #parameter, uintmax_t value);
667 #define expect_not_value(function, parameter, value) \
668 expect_not_value_count(function, parameter, value, 1)
673 * @brief Add an event to repeatedly check if a parameter isn't the given value.
675 * The event is triggered by calling check_expected() in the mocked function.
677 * @param[in] #function The function to add the check for.
679 * @param[in] #parameter The name of the parameter passed to the function.
681 * @param[in] value The value to check.
683 * @param[in] count The count parameter returns the number of times the value
684 * should be returned by check_expected(). If count is set
685 * to -1 the value will always be returned.
687 * @see check_expected().
689 void expect_not_value_count(#function, #parameter, uintmax_t value, size_t count);
691 #define expect_not_value_count(function, parameter, value, count) \
692 _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
693 cast_to_largest_integral_type(value), count)
698 * @brief Add an event to check if the parameter value is equal to the
701 * The event is triggered by calling check_expected() in the mocked function.
703 * @param[in] #function The function to add the check for.
705 * @param[in] #parameter The name of the parameter passed to the function.
707 * @param[in] string The string value to compare.
709 * @see check_expected().
711 void expect_string(#function, #parameter, const char *string);
713 #define expect_string(function, parameter, string) \
714 expect_string_count(function, parameter, string, 1)
719 * @brief Add an event to check if the parameter value is equal to the
722 * The event is triggered by calling check_expected() in the mocked function.
724 * @param[in] #function The function to add the check for.
726 * @param[in] #parameter The name of the parameter passed to the function.
728 * @param[in] string The string value to compare.
730 * @param[in] count The count parameter returns the number of times the value
731 * should be returned by check_expected(). If count is set
732 * to -1 the value will always be returned.
734 * @see check_expected().
736 void expect_string_count(#function, #parameter, const char *string, size_t count);
738 #define expect_string_count(function, parameter, string, count) \
739 _expect_string(#function, #parameter, __FILE__, __LINE__, \
740 (const char*)(string), count)
745 * @brief Add an event to check if the parameter value isn't equal to the
748 * The event is triggered by calling check_expected() in the mocked function.
750 * @param[in] #function The function to add the check for.
752 * @param[in] #parameter The name of the parameter passed to the function.
754 * @param[in] string The string value to compare.
756 * @see check_expected().
758 void expect_not_string(#function, #parameter, const char *string);
760 #define expect_not_string(function, parameter, string) \
761 expect_not_string_count(function, parameter, string, 1)
766 * @brief Add an event to check if the parameter value isn't equal to the
769 * The event is triggered by calling check_expected() in the mocked function.
771 * @param[in] #function The function to add the check for.
773 * @param[in] #parameter The name of the parameter passed to the function.
775 * @param[in] string The string value to compare.
777 * @param[in] count The count parameter returns the number of times the value
778 * should be returned by check_expected(). If count is set
779 * to -1 the value will always be returned.
781 * @see check_expected().
783 void expect_not_string_count(#function, #parameter, const char *string, size_t count);
785 #define expect_not_string_count(function, parameter, string, count) \
786 _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
787 (const char*)(string), count)
792 * @brief Add an event to check if the parameter does match an area of memory.
794 * The event is triggered by calling check_expected() in the mocked function.
796 * @param[in] #function The function to add the check for.
798 * @param[in] #parameter The name of the parameter passed to the function.
800 * @param[in] memory The memory to compare.
802 * @param[in] size The size of the memory to compare.
804 * @see check_expected().
806 void expect_memory(#function, #parameter, void *memory, size_t size);
808 #define expect_memory(function, parameter, memory, size) \
809 expect_memory_count(function, parameter, memory, size, 1)
814 * @brief Add an event to repeatedly check if the parameter does match an area
817 * The event is triggered by calling check_expected() in the mocked function.
819 * @param[in] #function The function to add the check for.
821 * @param[in] #parameter The name of the parameter passed to the function.
823 * @param[in] memory The memory to compare.
825 * @param[in] size The size of the memory to compare.
827 * @param[in] count The count parameter returns the number of times the value
828 * should be returned by check_expected(). If count is set
829 * to -1 the value will always be returned.
831 * @see check_expected().
833 void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
835 #define expect_memory_count(function, parameter, memory, size, count) \
836 _expect_memory(#function, #parameter, __FILE__, __LINE__, \
837 (const void*)(memory), size, count)
842 * @brief Add an event to check if the parameter doesn't match an area of
845 * The event is triggered by calling check_expected() in the mocked function.
847 * @param[in] #function The function to add the check for.
849 * @param[in] #parameter The name of the parameter passed to the function.
851 * @param[in] memory The memory to compare.
853 * @param[in] size The size of the memory to compare.
855 * @see check_expected().
857 void expect_not_memory(#function, #parameter, void *memory, size_t size);
859 #define expect_not_memory(function, parameter, memory, size) \
860 expect_not_memory_count(function, parameter, memory, size, 1)
865 * @brief Add an event to repeatedly check if the parameter doesn't match an
868 * The event is triggered by calling check_expected() in the mocked function.
870 * @param[in] #function The function to add the check for.
872 * @param[in] #parameter The name of the parameter passed to the function.
874 * @param[in] memory The memory to compare.
876 * @param[in] size The size of the memory to compare.
878 * @param[in] count The count parameter returns the number of times the value
879 * should be returned by check_expected(). If count is set
880 * to -1 the value will always be returned.
882 * @see check_expected().
884 void expect_not_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
886 #define expect_not_memory_count(function, parameter, memory, size, count) \
887 _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
888 (const void*)(memory), size, count)
894 * @brief Add an event to check if a parameter (of any value) has been passed.
896 * The event is triggered by calling check_expected() in the mocked function.
898 * @param[in] #function The function to add the check for.
900 * @param[in] #parameter The name of the parameter passed to the function.
902 * @see check_expected().
904 void expect_any(#function, #parameter);
906 #define expect_any(function, parameter) \
907 expect_any_count(function, parameter, 1)
912 * @brief Add an event to repeatedly check if a parameter (of any value) has
915 * The event is triggered by calling check_expected() in the mocked function.
917 * @param[in] #function The function to add the check for.
919 * @param[in] #parameter The name of the parameter passed to the function.
921 * @param[in] count The count parameter returns the number of times the value
922 * should be returned by check_expected(). If count is set
923 * to -1 the value will always be returned.
925 * @see check_expected().
927 void expect_any_count(#function, #parameter, size_t count);
929 #define expect_any_count(function, parameter, count) \
930 _expect_any(#function, #parameter, __FILE__, __LINE__, count)
935 * @brief Determine whether a function parameter is correct.
937 * This ensures the next value queued by one of the expect_*() macros matches
938 * the specified variable.
940 * This function needs to be called in the mock object.
942 * @param[in] #parameter The parameter to check.
944 void check_expected(#parameter);
946 #define check_expected(parameter) \
947 _check_expected(__func__, #parameter, __FILE__, __LINE__, \
948 cast_to_largest_integral_type(parameter))
953 * @brief Determine whether a function parameter is correct.
955 * This ensures the next value queued by one of the expect_*() macros matches
956 * the specified variable.
958 * This function needs to be called in the mock object.
960 * @param[in] #parameter The pointer to check.
962 void check_expected_ptr(#parameter);
964 #define check_expected_ptr(parameter) \
965 _check_expected(__func__, #parameter, __FILE__, __LINE__, \
966 cast_ptr_to_largest_integral_type(parameter))
972 * @defgroup cmocka_asserts Assert Macros
975 * This is a set of useful assert macros like the standard C libary's
978 * On an assertion failure a cmocka assert macro will write the failure to the
979 * standard error stream and signal a test failure. Due to limitations of the C
980 * language the general C standard library assert() and cmocka's assert_true()
981 * and assert_false() macros can only display the expression that caused the
982 * assert failure. cmocka's type specific assert macros, assert_{type}_equal()
983 * and assert_{type}_not_equal(), display the data that caused the assertion
984 * failure which increases data visibility aiding debugging of failing test
992 * @brief Assert that the given expression is true.
994 * The function prints an error message to standard error and terminates the
995 * test by calling fail() if expression is false (i.e., compares equal to
998 * @param[in] expression The expression to evaluate.
1000 * @see assert_int_equal()
1001 * @see assert_string_equal()
1003 void assert_true(scalar expression);
1005 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
1011 * @brief Assert that the given expression is false.
1013 * The function prints an error message to standard error and terminates the
1014 * test by calling fail() if expression is true.
1016 * @param[in] expression The expression to evaluate.
1018 * @see assert_int_equal()
1019 * @see assert_string_equal()
1021 void assert_false(scalar expression);
1023 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
1029 * @brief Assert that the return_code is greater than or equal to 0.
1031 * The function prints an error message to standard error and terminates the
1032 * test by calling fail() if the return code is smaller than 0. If the function
1033 * you check sets an errno if it fails you can pass it to the function and
1034 * it will be printed as part of the error message.
1036 * @param[in] rc The return code to evaluate.
1038 * @param[in] error Pass errno here or 0.
1040 void assert_return_code(int rc, int error);
1042 #define assert_return_code(rc, error) \
1043 _assert_return_code(cast_to_largest_integral_type(rc), \
1045 cast_to_largest_integral_type(error), \
1046 #rc, __FILE__, __LINE__)
1051 * @brief Assert that the given pointer is non-NULL.
1053 * The function prints an error message to standard error and terminates the
1054 * test by calling fail() if the pointer is non-NULL.
1056 * @param[in] pointer The pointer to evaluate.
1058 * @see assert_null()
1060 void assert_non_null(void *pointer);
1062 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
1068 * @brief Assert that the given pointer is NULL.
1070 * The function prints an error message to standard error and terminates the
1071 * test by calling fail() if the pointer is non-NULL.
1073 * @param[in] pointer The pointer to evaluate.
1075 * @see assert_non_null()
1077 void assert_null(void *pointer);
1079 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
1085 * @brief Assert that the two given integers are equal.
1087 * The function prints an error message to standard error and terminates the
1088 * test by calling fail() if the integers are not equal.
1090 * @param[in] a The first integer to compare.
1092 * @param[in] b The integer to compare against the first one.
1094 void assert_int_equal(int a, int b);
1096 #define assert_int_equal(a, b) \
1097 _assert_int_equal(cast_to_largest_integral_type(a), \
1098 cast_to_largest_integral_type(b), \
1104 * @brief Assert that the two given integers are not equal.
1106 * The function prints an error message to standard error and terminates the
1107 * test by calling fail() if the integers are equal.
1109 * @param[in] a The first integer to compare.
1111 * @param[in] b The integer to compare against the first one.
1113 * @see assert_int_equal()
1115 void assert_int_not_equal(int a, int b);
1117 #define assert_int_not_equal(a, b) \
1118 _assert_int_not_equal(cast_to_largest_integral_type(a), \
1119 cast_to_largest_integral_type(b), \
1125 * @brief Assert that the two given strings are equal.
1127 * The function prints an error message to standard error and terminates the
1128 * test by calling fail() if the strings are not equal.
1130 * @param[in] a The string to check.
1132 * @param[in] b The other string to compare.
1134 void assert_string_equal(const char *a, const char *b);
1136 #define assert_string_equal(a, b) \
1137 _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
1143 * @brief Assert that the two given strings are not equal.
1145 * The function prints an error message to standard error and terminates the
1146 * test by calling fail() if the strings are equal.
1148 * @param[in] a The string to check.
1150 * @param[in] b The other string to compare.
1152 void assert_string_not_equal(const char *a, const char *b);
1154 #define assert_string_not_equal(a, b) \
1155 _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
1161 * @brief Assert that the two given areas of memory are equal, otherwise fail.
1163 * The function prints an error message to standard error and terminates the
1164 * test by calling fail() if the memory is not equal.
1166 * @param[in] a The first memory area to compare
1167 * (interpreted as unsigned char).
1169 * @param[in] b The second memory area to compare
1170 * (interpreted as unsigned char).
1172 * @param[in] size The first n bytes of the memory areas to compare.
1174 void assert_memory_equal(const void *a, const void *b, size_t size);
1176 #define assert_memory_equal(a, b, size) \
1177 _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \
1183 * @brief Assert that the two given areas of memory are not equal.
1185 * The function prints an error message to standard error and terminates the
1186 * test by calling fail() if the memory is equal.
1188 * @param[in] a The first memory area to compare
1189 * (interpreted as unsigned char).
1191 * @param[in] b The second memory area to compare
1192 * (interpreted as unsigned char).
1194 * @param[in] size The first n bytes of the memory areas to compare.
1196 void assert_memory_not_equal(const void *a, const void *b, size_t size);
1198 #define assert_memory_not_equal(a, b, size) \
1199 _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
1205 * @brief Assert that the specified value is not smaller than the minimum
1206 * and and not greater than the maximum.
1208 * The function prints an error message to standard error and terminates the
1209 * test by calling fail() if value is not in range.
1211 * @param[in] value The value to check.
1213 * @param[in] minimum The minimum value allowed.
1215 * @param[in] maximum The maximum value allowed.
1217 void assert_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum);
1219 #define assert_in_range(value, minimum, maximum) \
1221 cast_to_largest_integral_type(value), \
1222 cast_to_largest_integral_type(minimum), \
1223 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1228 * @brief Assert that the specified value is smaller than the minimum or
1229 * greater than the maximum.
1231 * The function prints an error message to standard error and terminates the
1232 * test by calling fail() if value is in range.
1234 * @param[in] value The value to check.
1236 * @param[in] minimum The minimum value to compare.
1238 * @param[in] maximum The maximum value to compare.
1240 void assert_not_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum);
1242 #define assert_not_in_range(value, minimum, maximum) \
1243 _assert_not_in_range( \
1244 cast_to_largest_integral_type(value), \
1245 cast_to_largest_integral_type(minimum), \
1246 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1251 * @brief Assert that the specified value is within a set.
1253 * The function prints an error message to standard error and terminates the
1254 * test by calling fail() if value is not within a set.
1256 * @param[in] value The value to look up
1258 * @param[in] values[] The array to check for the value.
1260 * @param[in] count The size of the values array.
1262 void assert_in_set(uintmax_t value, uintmax_t values[], size_t count);
1264 #define assert_in_set(value, values, number_of_values) \
1265 _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
1270 * @brief Assert that the specified value is not within a set.
1272 * The function prints an error message to standard error and terminates the
1273 * test by calling fail() if value is within a set.
1275 * @param[in] value The value to look up
1277 * @param[in] values[] The array to check for the value.
1279 * @param[in] count The size of the values array.
1281 void assert_not_in_set(uintmax_t value, uintmax_t values[], size_t count);
1283 #define assert_not_in_set(value, values, number_of_values) \
1284 _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
1290 * @defgroup cmocka_exec Running Tests
1293 * This is the way tests are executed with CMocka.
1295 * The following example illustrates this macro's use with the unit_test macro.
1298 * void Test0(void **state);
1299 * void Test1(void **state);
1303 * const struct CMUnitTest tests[] = {
1304 * cmocka_unit_test(Test0),
1305 * cmocka_unit_test(Test1),
1308 * return cmocka_run_group_tests(tests, NULL, NULL);
1317 * @brief Forces the test to fail immediately and quit.
1321 #define fail() _fail(__FILE__, __LINE__)
1326 * @brief Forces the test to not be executed, but marked as skipped
1330 #define skip() _skip(__FILE__, __LINE__)
1335 * @brief Forces the test to fail immediately and quit, printing the reason.
1338 * fail_msg("This is some error message for test");
1344 * char *error_msg = "This is some error message for test";
1345 * fail_msg("%s", error_msg);
1348 void fail_msg(const char *msg, ...);
1350 #define fail_msg(msg, ...) do { \
1351 print_error("ERROR: " msg "\n", ##__VA_ARGS__); \
1358 * @brief Generic method to run a single test.
1360 * @deprecated This function was deprecated in favor of cmocka_run_group_tests
1362 * @param[in] #function The function to test.
1364 * @return 0 on success, 1 if an error occured.
1367 * // A test case that does nothing and succeeds.
1368 * void null_test_success(void **state) {
1372 * return run_test(null_test_success);
1376 int run_test(#function);
1378 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
1381 static inline void _unit_test_dummy(void **state) {
1385 /** Initializes a UnitTest structure.
1387 * @deprecated This function was deprecated in favor of cmocka_unit_test
1389 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
1391 #define _unit_test_setup(test, setup) \
1392 { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
1394 /** Initializes a UnitTest structure with a setup function.
1396 * @deprecated This function was deprecated in favor of cmocka_unit_test_setup
1398 #define unit_test_setup(test, setup) \
1399 _unit_test_setup(test, setup), \
1401 _unit_test_teardown(test, _unit_test_dummy)
1403 #define _unit_test_teardown(test, teardown) \
1404 { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
1406 /** Initializes a UnitTest structure with a teardown function.
1408 * @deprecated This function was deprecated in favor of cmocka_unit_test_teardown
1410 #define unit_test_teardown(test, teardown) \
1411 _unit_test_setup(test, _unit_test_dummy), \
1413 _unit_test_teardown(test, teardown)
1415 /** Initializes a UnitTest structure for a group setup function.
1417 * @deprecated This function was deprecated in favor of cmocka_run_group_tests
1419 #define group_test_setup(setup) \
1420 { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }
1422 /** Initializes a UnitTest structure for a group teardown function.
1424 * @deprecated This function was deprecated in favor of cmocka_run_group_tests
1426 #define group_test_teardown(teardown) \
1427 { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }
1430 * Initialize an array of UnitTest structures with a setup function for a test
1431 * and a teardown function. Either setup or teardown can be NULL.
1433 * @deprecated This function was deprecated in favor of
1434 * cmocka_unit_test_setup_teardown
1436 #define unit_test_setup_teardown(test, setup, teardown) \
1437 _unit_test_setup(test, setup), \
1439 _unit_test_teardown(test, teardown)
1442 /** Initializes a CMUnitTest structure. */
1443 #define cmocka_unit_test(f) { #f, f, NULL, NULL }
1445 /** Initializes a CMUnitTest structure with a setup function. */
1446 #define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL }
1448 /** Initializes a CMUnitTest structure with a teardown function. */
1449 #define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown }
1452 * Initialize an array of CMUnitTest structures with a setup function for a test
1453 * and a teardown function. Either setup or teardown can be NULL.
1455 #define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown }
1457 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
1458 #define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof(tests)[0])
1462 * @brief Run tests specified by an array of CMUnitTest structures.
1464 * @param[in] group_tests[] The array of unit tests to execute.
1466 * @param[in] group_setup The setup function which should be called before
1467 * all unit tests are executed.
1469 * @param[in] group_teardown The teardown function to be called after all
1470 * tests have finished.
1472 * @return 0 on success, or the number of failed tests.
1475 * static int setup(void **state) {
1476 * int *answer = malloc(sizeof(int));
1477 * if (*answer == NULL) {
1487 * static void teardown(void **state) {
1493 * static void null_test_success(void **state) {
1497 * static void int_test_success(void **state) {
1498 * int *answer = *state;
1499 * assert_int_equal(*answer, 42);
1503 * const struct CMUnitTest tests[] = {
1504 * cmocka_unit_test(null_test_success),
1505 * cmocka_unit_test_setup_teardown(int_test_success, setup, teardown),
1508 * return cmocka_run_group_tests(tests, NULL, NULL);
1512 * @see cmocka_unit_test
1513 * @see cmocka_unit_test_setup
1514 * @see cmocka_unit_test_teardown
1515 * @see cmocka_unit_test_setup_teardown
1517 int cmocka_run_group_tests(const struct CMUnitTest group_tests[],
1518 CMFixtureFunction group_setup,
1519 CMFixtureFunction group_teardown);
1521 # define cmocka_run_group_tests(group_tests, group_setup, group_teardown) \
1522 _cmocka_run_group_tests(#group_tests, group_tests, sizeof(group_tests) / sizeof(group_tests)[0], group_setup, group_teardown)
1527 * @brief Run tests specified by an array of CMUnitTest structures and specify
1530 * @param[in] group_name The name of the group test.
1532 * @param[in] group_tests[] The array of unit tests to execute.
1534 * @param[in] group_setup The setup function which should be called before
1535 * all unit tests are executed.
1537 * @param[in] group_teardown The teardown function to be called after all
1538 * tests have finished.
1540 * @return 0 on success, or the number of failed tests.
1543 * static int setup(void **state) {
1544 * int *answer = malloc(sizeof(int));
1545 * if (*answer == NULL) {
1555 * static void teardown(void **state) {
1561 * static void null_test_success(void **state) {
1565 * static void int_test_success(void **state) {
1566 * int *answer = *state;
1567 * assert_int_equal(*answer, 42);
1571 * const struct CMUnitTest tests[] = {
1572 * cmocka_unit_test(null_test_success),
1573 * cmocka_unit_test_setup_teardown(int_test_success, setup, teardown),
1576 * return cmocka_run_group_tests_name("success_test", tests, NULL, NULL);
1580 * @see cmocka_unit_test
1581 * @see cmocka_unit_test_setup
1582 * @see cmocka_unit_test_teardown
1583 * @see cmocka_unit_test_setup_teardown
1585 int cmocka_run_group_tests_name(const char *group_name,
1586 const struct CMUnitTest group_tests[],
1587 CMFixtureFunction group_setup,
1588 CMFixtureFunction group_teardown);
1590 # define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown) \
1591 _cmocka_run_group_tests(group_name, group_tests, sizeof(group_tests) / sizeof(group_tests)[0], group_setup, group_teardown)
1597 * @defgroup cmocka_alloc Dynamic Memory Allocation
1600 * Memory leaks, buffer overflows and underflows can be checked using cmocka.
1602 * To test for memory leaks, buffer overflows and underflows a module being
1603 * tested by cmocka should replace calls to malloc(), calloc() and free() to
1604 * test_malloc(), test_calloc() and test_free() respectively. Each time a block
1605 * is deallocated using test_free() it is checked for corruption, if a corrupt
1606 * block is found a test failure is signalled. All blocks allocated using the
1607 * test_*() allocation functions are tracked by the cmocka library. When a test
1608 * completes if any allocated blocks (memory leaks) remain they are reported
1609 * and a test failure is signalled.
1611 * For simplicity cmocka currently executes all tests in one process. Therefore
1612 * all test cases in a test application share a single address space which
1613 * means memory corruption from a single test case could potentially cause the
1614 * test application to exit prematurely.
1621 * @brief Test function overriding malloc.
1623 * @param[in] size The bytes which should be allocated.
1625 * @return A pointer to the allocated memory or NULL on error.
1628 * #ifdef UNIT_TESTING
1629 * extern void* _test_malloc(const size_t size, const char* file, const int line);
1631 * #define malloc(size) _test_malloc(size, __FILE__, __LINE__)
1634 * void leak_memory() {
1635 * int * const temporary = (int*)malloc(sizeof(int));
1642 void *test_malloc(size_t size);
1644 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
1649 * @brief Test function overriding calloc.
1651 * The memory is set to zero.
1653 * @param[in] nmemb The number of elements for an array to be allocated.
1655 * @param[in] size The size in bytes of each array element to allocate.
1657 * @return A pointer to the allocated memory, NULL on error.
1661 void *test_calloc(size_t nmemb, size_t size);
1663 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
1668 * @brief Test function overriding realloc which detects buffer overruns
1669 * and memoery leaks.
1671 * @param[in] ptr The memory block which should be changed.
1673 * @param[in] size The bytes which should be allocated.
1675 * @return The newly allocated memory block, NULL on error.
1677 void *test_realloc(void *ptr, size_t size);
1679 #define test_realloc(ptr, size) _test_realloc(ptr, size, __FILE__, __LINE__)
1684 * @brief Test function overriding free(3).
1686 * @param[in] ptr The pointer to the memory space to free.
1690 void test_free(void *ptr);
1692 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
1695 /* Redirect malloc, calloc and free to the unit test allocators. */
1697 #define malloc test_malloc
1698 #define realloc test_realloc
1699 #define calloc test_calloc
1700 #define free test_free
1701 #endif /* UNIT_TESTING */
1707 * @defgroup cmocka_mock_assert Standard Assertions
1710 * How to handle assert(3) of the standard C library.
1712 * Runtime assert macros like the standard C library's assert() should be
1713 * redefined in modules being tested to use cmocka's mock_assert() function.
1714 * Normally mock_assert() signals a test failure. If a function is called using
1715 * the expect_assert_failure() macro, any calls to mock_assert() within the
1716 * function will result in the execution of the test. If no calls to
1717 * mock_assert() occur during the function called via expect_assert_failure() a
1718 * test failure is signalled.
1724 * @brief Function to replace assert(3) in tested code.
1726 * In conjuction with check_assert() it's possible to determine whether an
1727 * assert condition has failed without stopping a test.
1729 * @param[in] result The expression to assert.
1731 * @param[in] expression The expression as string.
1733 * @param[in] file The file mock_assert() is called.
1735 * @param[in] line The line mock_assert() is called.
1738 * #ifdef UNIT_TESTING
1739 * extern void mock_assert(const int result, const char* const expression,
1740 * const char * const file, const int line);
1743 * #define assert(expression) \
1744 * mock_assert((int)(expression), #expression, __FILE__, __LINE__);
1747 * void increment_value(int * const value) {
1754 * @see expect_assert_failure
1756 void mock_assert(const int result, const char* const expression,
1757 const char * const file, const int line);
1761 * @brief Ensure that mock_assert() is called.
1763 * If mock_assert() is called the assert expression string is returned.
1765 * @param[in] fn_call The function will will call mock_assert().
1768 * #define assert mock_assert
1770 * void showmessage(const char *message) {
1774 * int main(int argc, const char* argv[]) {
1775 * expect_assert_failure(show_message(NULL));
1776 * printf("succeeded\n");
1782 void expect_assert_failure(function fn_call);
1784 #define expect_assert_failure(function_call) \
1786 const int result = setjmp(global_expect_assert_env); \
1787 global_expecting_assert = 1; \
1789 print_message("Expected assertion %s occurred\n", \
1790 global_last_failed_assert); \
1791 global_expecting_assert = 0; \
1794 global_expecting_assert = 0; \
1795 print_error("Expected assert in %s\n", #function_call); \
1796 _fail(__FILE__, __LINE__); \
1803 /* Function prototype for setup, test and teardown functions. */
1804 typedef void (*UnitTestFunction)(void **state);
1806 /* Function that determines whether a function parameter value is correct. */
1807 typedef int (*CheckParameterValue)(const LargestIntegralType value,
1808 const LargestIntegralType check_value_data);
1810 /* Type of the unit test function. */
1811 typedef enum UnitTestFunctionType {
1812 UNIT_TEST_FUNCTION_TYPE_TEST = 0,
1813 UNIT_TEST_FUNCTION_TYPE_SETUP,
1814 UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
1815 UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP,
1816 UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN,
1817 } UnitTestFunctionType;
1820 * Stores a unit test function with its name and type.
1821 * NOTE: Every setup function must be paired with a teardown function. It's
1822 * possible to specify NULL function pointers.
1824 typedef struct UnitTest {
1826 UnitTestFunction function;
1827 UnitTestFunctionType function_type;
1830 typedef struct GroupTest {
1831 UnitTestFunction setup;
1832 UnitTestFunction teardown;
1833 const UnitTest *tests;
1834 const size_t number_of_tests;
1837 /* Function prototype for test functions. */
1838 typedef void (*CMUnitTestFunction)(void **state);
1840 /* Function prototype for setup and teardown functions. */
1841 typedef int (*CMFixtureFunction)(void **state);
1845 CMUnitTestFunction test_func;
1846 CMFixtureFunction setup_func;
1847 CMFixtureFunction teardown_func;
1850 /* Location within some source code. */
1851 typedef struct SourceLocation {
1856 /* Event that's called to check a parameter value. */
1857 typedef struct CheckParameterEvent {
1858 SourceLocation location;
1859 const char *parameter_name;
1860 CheckParameterValue check_value;
1861 LargestIntegralType check_value_data;
1862 } CheckParameterEvent;
1864 /* Used by expect_assert_failure() and mock_assert(). */
1865 extern int global_expecting_assert;
1866 extern jmp_buf global_expect_assert_env;
1867 extern const char * global_last_failed_assert;
1869 /* Retrieves a value for the given function, as set by "will_return". */
1870 LargestIntegralType _mock(const char * const function, const char* const file,
1874 const char* const function, const char* const parameter,
1875 const char* const file, const int line,
1876 const CheckParameterValue check_function,
1877 const LargestIntegralType check_data, CheckParameterEvent * const event,
1880 void _expect_in_set(
1881 const char* const function, const char* const parameter,
1882 const char* const file, const int line, const LargestIntegralType values[],
1883 const size_t number_of_values, const int count);
1884 void _expect_not_in_set(
1885 const char* const function, const char* const parameter,
1886 const char* const file, const int line, const LargestIntegralType values[],
1887 const size_t number_of_values, const int count);
1889 void _expect_in_range(
1890 const char* const function, const char* const parameter,
1891 const char* const file, const int line,
1892 const LargestIntegralType minimum,
1893 const LargestIntegralType maximum, const int count);
1894 void _expect_not_in_range(
1895 const char* const function, const char* const parameter,
1896 const char* const file, const int line,
1897 const LargestIntegralType minimum,
1898 const LargestIntegralType maximum, const int count);
1901 const char* const function, const char* const parameter,
1902 const char* const file, const int line, const LargestIntegralType value,
1904 void _expect_not_value(
1905 const char* const function, const char* const parameter,
1906 const char* const file, const int line, const LargestIntegralType value,
1909 void _expect_string(
1910 const char* const function, const char* const parameter,
1911 const char* const file, const int line, const char* string,
1913 void _expect_not_string(
1914 const char* const function, const char* const parameter,
1915 const char* const file, const int line, const char* string,
1918 void _expect_memory(
1919 const char* const function, const char* const parameter,
1920 const char* const file, const int line, const void* const memory,
1921 const size_t size, const int count);
1922 void _expect_not_memory(
1923 const char* const function, const char* const parameter,
1924 const char* const file, const int line, const void* const memory,
1925 const size_t size, const int count);
1928 const char* const function, const char* const parameter,
1929 const char* const file, const int line, const int count);
1931 void _check_expected(
1932 const char * const function_name, const char * const parameter_name,
1933 const char* file, const int line, const LargestIntegralType value);
1935 void _will_return(const char * const function_name, const char * const file,
1936 const int line, const LargestIntegralType value,
1938 void _assert_true(const LargestIntegralType result,
1939 const char* const expression,
1940 const char * const file, const int line);
1941 void _assert_return_code(const LargestIntegralType result,
1943 const LargestIntegralType error,
1944 const char * const expression,
1945 const char * const file,
1947 void _assert_int_equal(
1948 const LargestIntegralType a, const LargestIntegralType b,
1949 const char * const file, const int line);
1950 void _assert_int_not_equal(
1951 const LargestIntegralType a, const LargestIntegralType b,
1952 const char * const file, const int line);
1953 void _assert_string_equal(const char * const a, const char * const b,
1954 const char * const file, const int line);
1955 void _assert_string_not_equal(const char * const a, const char * const b,
1956 const char *file, const int line);
1957 void _assert_memory_equal(const void * const a, const void * const b,
1958 const size_t size, const char* const file,
1960 void _assert_memory_not_equal(const void * const a, const void * const b,
1961 const size_t size, const char* const file,
1963 void _assert_in_range(
1964 const LargestIntegralType value, const LargestIntegralType minimum,
1965 const LargestIntegralType maximum, const char* const file, const int line);
1966 void _assert_not_in_range(
1967 const LargestIntegralType value, const LargestIntegralType minimum,
1968 const LargestIntegralType maximum, const char* const file, const int line);
1969 void _assert_in_set(
1970 const LargestIntegralType value, const LargestIntegralType values[],
1971 const size_t number_of_values, const char* const file, const int line);
1972 void _assert_not_in_set(
1973 const LargestIntegralType value, const LargestIntegralType values[],
1974 const size_t number_of_values, const char* const file, const int line);
1976 void* _test_malloc(const size_t size, const char* file, const int line);
1977 void* _test_realloc(void *ptr, const size_t size, const char* file, const int line);
1978 void* _test_calloc(const size_t number_of_elements, const size_t size,
1979 const char* file, const int line);
1980 void _test_free(void* const ptr, const char* file, const int line);
1982 void _fail(const char * const file, const int line);
1984 void _skip(const char * const file, const int line);
1987 const char * const function_name, const UnitTestFunction Function,
1988 void ** const volatile state, const UnitTestFunctionType function_type,
1989 const void* const heap_check_point);
1990 CMOCKA_DEPRECATED int _run_tests(const UnitTest * const tests,
1991 const size_t number_of_tests);
1992 CMOCKA_DEPRECATED int _run_group_tests(const UnitTest * const tests,
1993 const size_t number_of_tests);
1996 int _cmocka_run_group_tests(const char *group_name,
1997 const struct CMUnitTest * const tests,
1998 const size_t num_tests,
1999 CMFixtureFunction group_setup,
2000 CMFixtureFunction group_teardown);
2002 /* Standard output and error print methods. */
2003 void print_message(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2004 void print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2005 void vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2006 void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2008 enum cm_message_output {
2016 * @brief Function to set the output format for a test.
2018 * The ouput format for the test can either be set globally using this
2019 * function or overriden with environment variable CMOCKA_MESSAGE_OUTPUT.
2021 * The environment variable can be set to either STDOUT, SUBUNIT, TAP or XML.
2023 * @param[in] output The output format to use for the test.
2026 void cmocka_set_message_output(enum cm_message_output output);
2030 #endif /* CMOCKA_H_ */