2 * Copyright 2008 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #endif /* __cplusplus */
24 int __stdcall IsDebuggerPresent();
27 #endif /* __cplusplus */
28 #endif /* _MSC_VER < 1500 */
32 * These headers or their equivalents should be included prior to including
39 * This allows test applications to use custom definitions of C standard
40 * library functions and types.
43 /* For those who are used to __func__ from gcc. */
45 #define __func__ __FUNCTION__
48 /* GCC have printf type attribute check. */
50 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
52 #define PRINTF_ATTRIBUTE(a,b)
56 * @defgroup cmocka The CMocka API
58 * TODO Describe cmocka.
64 * Largest integral type. This type should be large enough to hold any
65 * pointer or integer supported by the compiler.
67 #ifndef LargestIntegralType
68 #define LargestIntegralType unsigned long long
69 #endif /* LargestIntegralType */
71 /* Printf format used to display LargestIntegralType. */
72 #ifndef LargestIntegralTypePrintfFormat
74 #define LargestIntegralTypePrintfFormat "%I64x"
76 #define LargestIntegralTypePrintfFormat "%llx"
78 #endif /* LargestIntegralTypePrintfFormat */
80 /* Perform an unsigned cast to LargestIntegralType. */
81 #define cast_to_largest_integral_type(value) \
82 ((LargestIntegralType)((size_t)(value)))
84 /* Smallest integral type capable of holding a pointer. */
89 /* WIN32 is an ILP32 platform */
90 typedef unsigned long uintptr_t;
92 /* what about 64-bit windows?
93 * what's the right preprocessor symbol?
94 typedef unsigned long long uintptr_t */
98 /* ILP32 and LP64 platforms */
99 #ifdef __WORDSIZE /* glibc */
100 # if __WORDSIZE == 64
101 typedef unsigned long int uintptr_t;
103 typedef unsigned int uintptr_t;
104 # endif /* __WORDSIZE == 64 */
105 #else /* __WORDSIZE */
107 # if defined(_LP64) || defined(_I32LPx)
108 typedef unsigned long int uintptr_t;
110 typedef unsigned int uintptr_t;
113 #endif /* __WORDSIZE */
116 #endif /* _UINTPTR_T */
118 /* Perform an unsigned cast to uintptr_t. */
119 #define cast_to_pointer_integral_type(value) \
122 /* Perform a cast of a pointer to uintmax_t */
123 #define cast_ptr_to_largest_integral_type(value) \
124 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
127 * @defgroup cmocka_mock Mock Objects
130 * Mock objects mock objects are simulated objects that mimic the behavior of
131 * real objects. Instead of calling the real objects, the tested object calls a
132 * mock object that merely asserts that the correct methods were called, with
133 * the expected parameters, in the correct order.
140 * @brief Retrieve a return value of the current function.
142 * @return The value which was stored to return by this function.
148 #define mock() _mock(__func__, __FILE__, __LINE__)
153 * @brief Retrieve a typed return value of the current function.
155 * The value would be casted to type internally to avoid having the
156 * caller to do the cast manually.
158 * @param[in] #type The expected type of the return value
160 * @return The value which was stored to return by this function.
165 * param = mock_type(int);
170 * @see mock_ptr_type()
172 void *mock_type(#type);
174 #define mock_type(type) ((type) mock())
179 * @brief Retrieve a typed return value of the current function.
181 * The value would be casted to type internally to avoid having the
182 * caller to do the cast manually but also casted to uintptr_t to make
183 * sure the result has a valid size to be used as a pointer.
185 * @param[in] #type The expected type of the return value
187 * @return The value which was stored to return by this function.
192 * param = mock_ptr_type(char *);
199 void *mock_ptr_type(#type);
201 #define mock_ptr_type(type) ((type) (uintptr_t) mock())
207 * @brief Store a value to be returned by mock() later.
209 * @param[in] #function The function which should return the given value.
211 * @param[in] value The value to be returned by mock().
214 * int return_integer(void)
216 * return (int)mock();
219 * static void test_integer_return(void **state)
221 * will_return(return_integer, 42);
223 * assert_int_equal(my_function_calling_return_integer(), 42);
228 * @see will_return_count()
230 void will_return(#function, void *value);
232 #define will_return(function, value) \
233 _will_return(#function, __FILE__, __LINE__, \
234 cast_to_largest_integral_type(value), 1)
239 * @brief Store a value to be returned by mock() later.
241 * @param[in] #function The function which should return the given value.
243 * @param[in] value The value to be returned by mock().
245 * @param[in] count The parameter returns the number of times the value should
246 * be returned by mock(). If count is set to -1 the value will
247 * always be returned.
251 void will_return_count(#function, void *value, int count);
253 #define will_return_count(function, value, count) \
254 _will_return(#function, __FILE__, __LINE__, \
255 cast_to_largest_integral_type(value), count)
261 * @defgroup cmocka_param Checking Parameters
264 * Functionality to store expected values for mock function parameters.
266 * In addition to storing the return values of mock functions, cmocka provides
267 * functionality to store expected values for mock function parameters using
268 * the expect_*() functions provided. A mock function parameter can then be
269 * validated using the check_expected() macro.
271 * Successive calls to expect_*() macros for a parameter queues values to check
272 * the specified parameter. check_expected() checks a function parameter
273 * against the next value queued using expect_*(), if the parameter check fails
274 * a test failure is signalled. In addition if check_expected() is called and
275 * no more parameter values are queued a test failure occurs.
277 * The following test stub illustrates how to do this. First is the the function
278 * we call in the test driver:
281 * static void test_driver(void **state)
283 * expect_string(chef_cook, order, "hotdog");
287 * Now the chef_cook function can check if the parameter we got passed is the
288 * parameter which is expected by the test driver. This can be done the
292 * int chef_cook(const char *order, char **dish_out)
294 * check_expected(order);
298 * For a complete example please at a look at
299 * <a href="http://git.cryptomilk.org/projects/cmocka.git/tree/example/chef_wrap/waiter_test_wrap.c">here</a>
305 * Add a custom parameter checking function. If the event parameter is NULL
306 * the event structure is allocated internally by this function. If event
307 * parameter is provided it must be allocated on the heap and doesn't need to
308 * be deallocated by the caller.
312 * @brief Add a custom parameter checking function.
314 * If the event parameter is NULL the event structure is allocated internally
315 * by this function. If the parameter is provided it must be allocated on the
316 * heap and doesn't need to be deallocated by the caller.
318 * @param[in] #function The fuction to add a custom paramater checking function
321 * @param[in] #parameter The parametes passed to the function.
323 * @param[in] #check_function The check function to call.
325 * @param[in] check_data The data to pass to the check function.
327 void expect_check(#function, #parameter, #check_function, const void *check_data);
329 #define expect_check(function, parameter, check_function, check_data) \
330 _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
331 cast_to_largest_integral_type(check_data), NULL, 0)
336 * @brief Add an event to check if the parameter value is part of the provided
339 * The event is triggered by calling check_expected() in the mocked function.
341 * @param[in] #function The function to add the check for.
343 * @param[in] #parameter The name of the parameter passed to the function.
345 * @param[in] value_array[] The array to check for the value.
347 * @see check_expected().
349 void expect_in_set(#function, #parameter, uintmax_t value_array[]);
351 #define expect_in_set(function, parameter, value_array) \
352 expect_in_set_count(function, parameter, value_array, 1)
357 * @brief Add an event to check if the parameter value is part of the provided
360 * The event is triggered by calling check_expected() in the mocked function.
362 * @param[in] #function The function to add the check for.
364 * @param[in] #parameter The name of the parameter passed to the function.
366 * @param[in] value_array[] The array to check for the value.
368 * @param[in] count The count parameter returns the number of times the value
369 * should be returned by check_expected(). If count is set
370 * to -1 the value will always be returned.
372 * @see check_expected().
374 void expect_in_set_count(#function, #parameter, uintmax_t value_array[], size_t count);
376 #define expect_in_set_count(function, parameter, value_array, count) \
377 _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
378 sizeof(value_array) / sizeof((value_array)[0]), count)
383 * @brief Add an event to check if the parameter value is not part of the
386 * The event is triggered by calling check_expected() in the mocked function.
388 * @param[in] #function The function to add the check for.
390 * @param[in] #parameter The name of the parameter passed to the function.
392 * @param[in] value_array[] The array to check for the value.
394 * @see check_expected().
396 void expect_not_in_set(#function, #parameter, uintmax_t value_array[]);
398 #define expect_not_in_set(function, parameter, value_array) \
399 expect_not_in_set_count(function, parameter, value_array, 1)
404 * @brief Add an event to check if the parameter value is not part of the
407 * The event is triggered by calling check_expected() in the mocked function.
409 * @param[in] #function The function to add the check for.
411 * @param[in] #parameter The name of the parameter passed to the function.
413 * @param[in] value_array[] The array to check for the value.
415 * @param[in] count The count parameter returns the number of times the value
416 * should be returned by check_expected(). If count is set
417 * to -1 the value will always be returned.
419 * @see check_expected().
421 void expect_not_in_set_count(#function, #parameter, uintmax_t value_array[], size_t count);
423 #define expect_not_in_set_count(function, parameter, value_array, count) \
424 _expect_not_in_set( \
425 #function, #parameter, __FILE__, __LINE__, value_array, \
426 sizeof(value_array) / sizeof((value_array)[0]), count)
432 * @brief Add an event to check a parameter is inside a numerical range.
433 * The check would succeed if minimum <= value <= maximum.
435 * The event is triggered by calling check_expected() in the mocked function.
437 * @param[in] #function The function to add the check for.
439 * @param[in] #parameter The name of the parameter passed to the function.
441 * @param[in] minimum The lower boundary of the interval to check against.
443 * @param[in] maximum The upper boundary of the interval to check against.
445 * @see check_expected().
447 void expect_in_range(#function, #parameter, uintmax_t minimum, uintmax_t maximum);
449 #define expect_in_range(function, parameter, minimum, maximum) \
450 expect_in_range_count(function, parameter, minimum, maximum, 1)
455 * @brief Add an event to repeatedly check a parameter is inside a
456 * numerical range. The check would succeed if minimum <= value <= maximum.
458 * The event is triggered by calling check_expected() in the mocked function.
460 * @param[in] #function The function to add the check for.
462 * @param[in] #parameter The name of the parameter passed to the function.
464 * @param[in] minimum The lower boundary of the interval to check against.
466 * @param[in] maximum The upper boundary of the interval to check against.
468 * @param[in] count The count parameter returns the number of times the value
469 * should be returned by check_expected(). If count is set
470 * to -1 the value will always be returned.
472 * @see check_expected().
474 void expect_in_range_count(#function, #parameter, uintmax_t minimum, uintmax_t maximum, size_t count);
476 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
477 _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
483 * @brief Add an event to check a parameter is outside a numerical range.
484 * The check would succeed if minimum > value > maximum.
486 * The event is triggered by calling check_expected() in the mocked function.
488 * @param[in] #function The function to add the check for.
490 * @param[in] #parameter The name of the parameter passed to the function.
492 * @param[in] minimum The lower boundary of the interval to check against.
494 * @param[in] maximum The upper boundary of the interval to check against.
496 * @see check_expected().
498 void expect_not_in_range(#function, #parameter, uintmax_t minimum, uintmax_t maximum);
500 #define expect_not_in_range(function, parameter, minimum, maximum) \
501 expect_not_in_range_count(function, parameter, minimum, maximum, 1)
506 * @brief Add an event to repeatedly check a parameter is outside a
507 * numerical range. The check would succeed if minimum > value > maximum.
509 * The event is triggered by calling check_expected() in the mocked function.
511 * @param[in] #function The function to add the check for.
513 * @param[in] #parameter The name of the parameter passed to the function.
515 * @param[in] minimum The lower boundary of the interval to check against.
517 * @param[in] maximum The upper boundary of the interval to check against.
519 * @param[in] count The count parameter returns the number of times the value
520 * should be returned by check_expected(). If count is set
521 * to -1 the value will always be returned.
523 * @see check_expected().
525 void expect_not_in_range_count(#function, #parameter, uintmax_t minimum, uintmax_t maximum, size_t count);
527 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
529 _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
530 minimum, maximum, count)
535 * @brief Add an event to check if a parameter is the given value.
537 * The event is triggered by calling check_expected() in the mocked function.
539 * @param[in] #function The function to add the check for.
541 * @param[in] #parameter The name of the parameter passed to the function.
543 * @param[in] value The value to check.
545 * @see check_expected().
547 void expect_value(#function, #parameter, uintmax_t value);
549 #define expect_value(function, parameter, value) \
550 expect_value_count(function, parameter, value, 1)
555 * @brief Add an event to repeatedly check if a parameter is the given value.
557 * The event is triggered by calling check_expected() in the mocked function.
559 * @param[in] #function The function to add the check for.
561 * @param[in] #parameter The name of the parameter passed to the function.
563 * @param[in] value The value to check.
565 * @param[in] count The count parameter returns the number of times the value
566 * should be returned by check_expected(). If count is set
567 * to -1 the value will always be returned.
569 * @see check_expected().
571 void expect_value_count(#function, #parameter, uintmax_t value, size_t count);
573 #define expect_value_count(function, parameter, value, count) \
574 _expect_value(#function, #parameter, __FILE__, __LINE__, \
575 cast_to_largest_integral_type(value), count)
580 * @brief Add an event to check if a parameter isn't the given value.
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] value The value to check.
590 * @see check_expected().
592 void expect_not_value(#function, #parameter, uintmax_t value);
594 #define expect_not_value(function, parameter, value) \
595 expect_not_value_count(function, parameter, value, 1)
600 * @brief Add an event to repeatedly check if a parameter isn't the given value.
602 * The event is triggered by calling check_expected() in the mocked function.
604 * @param[in] #function The function to add the check for.
606 * @param[in] #parameter The name of the parameter passed to the function.
608 * @param[in] value The value to check.
610 * @param[in] count The count parameter returns the number of times the value
611 * should be returned by check_expected(). If count is set
612 * to -1 the value will always be returned.
614 * @see check_expected().
616 void expect_not_value_count(#function, #parameter, uintmax_t value, size_t count);
618 #define expect_not_value_count(function, parameter, value, count) \
619 _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
620 cast_to_largest_integral_type(value), count)
625 * @brief Add an event to check if the parameter value is equal to the
628 * The event is triggered by calling check_expected() in the mocked function.
630 * @param[in] #function The function to add the check for.
632 * @param[in] #parameter The name of the parameter passed to the function.
634 * @param[in] string The string value to compare.
636 * @see check_expected().
638 void expect_string(#function, #parameter, const char *string);
640 #define expect_string(function, parameter, string) \
641 expect_string_count(function, parameter, string, 1)
646 * @brief Add an event to check if the parameter value is equal to the
649 * The event is triggered by calling check_expected() in the mocked function.
651 * @param[in] #function The function to add the check for.
653 * @param[in] #parameter The name of the parameter passed to the function.
655 * @param[in] string The string value to compare.
657 * @param[in] count The count parameter returns the number of times the value
658 * should be returned by check_expected(). If count is set
659 * to -1 the value will always be returned.
661 * @see check_expected().
663 void expect_string_count(#function, #parameter, const char *string, size_t count);
665 #define expect_string_count(function, parameter, string, count) \
666 _expect_string(#function, #parameter, __FILE__, __LINE__, \
667 (const char*)(string), count)
672 * @brief Add an event to check if the parameter value isn't equal to the
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] string The string value to compare.
683 * @see check_expected().
685 void expect_not_string(#function, #parameter, const char *string);
687 #define expect_not_string(function, parameter, string) \
688 expect_not_string_count(function, parameter, string, 1)
693 * @brief Add an event to check if the parameter value isn't equal to the
696 * The event is triggered by calling check_expected() in the mocked function.
698 * @param[in] #function The function to add the check for.
700 * @param[in] #parameter The name of the parameter passed to the function.
702 * @param[in] string The string value to compare.
704 * @param[in] count The count parameter returns the number of times the value
705 * should be returned by check_expected(). If count is set
706 * to -1 the value will always be returned.
708 * @see check_expected().
710 void expect_not_string_count(#function, #parameter, const char *string, size_t count);
712 #define expect_not_string_count(function, parameter, string, count) \
713 _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
714 (const char*)(string), count)
718 * Add an event to check whether a parameter, using check_expected() does or
719 * doesn't match an area of memory. See will_return() for a description of
720 * the count parameter.
722 #define expect_memory(function, parameter, memory, size) \
723 expect_memory_count(function, parameter, memory, size, 1)
724 #define expect_memory_count(function, parameter, memory, size, count) \
725 _expect_memory(#function, #parameter, __FILE__, __LINE__, \
726 (const void*)(memory), size, count)
727 #define expect_not_memory(function, parameter, memory, size) \
728 expect_not_memory_count(function, parameter, memory, size, 1)
729 #define expect_not_memory_count(function, parameter, memory, size, count) \
730 _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
731 (const void*)(memory), size, count)
736 * @brief Add an event to check if a parameter (of any value) has been passed.
738 * The event is triggered by calling check_expected() in the mocked function.
740 * @param[in] #function The function to add the check for.
742 * @param[in] #parameter The name of the parameter passed to the function.
744 * @see check_expected().
746 void expect_any(#function, #parameter);
748 #define expect_any(function, parameter) \
749 expect_any_count(function, parameter, 1)
754 * @brief Add an event to repeatedly check if a parameter (of any value) has
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] count The count parameter returns the number of times the value
764 * should be returned by check_expected(). If count is set
765 * to -1 the value will always be returned.
767 * @see check_expected().
769 void expect_any_count(#function, #parameter, size_t count);
771 #define expect_any_count(function, parameter, count) \
772 _expect_any(#function, #parameter, __FILE__, __LINE__, count)
777 * @brief Determine whether a function parameter is correct.
779 * This ensures the next value queued by one of the expect_*() macros matches
780 * the specified variable.
782 * This function needs to be called in the mock object.
784 * @param[in] #parameter The parameter to check.
786 void check_expected(#parameter);
788 #define check_expected(parameter) \
789 _check_expected(__func__, #parameter, __FILE__, __LINE__, \
790 cast_to_largest_integral_type(parameter))
796 * @defgroup cmocka_asserts Assert Macros
799 * This is a set of useful assert macros like the standard C libary's
802 * On an assertion failure a cmocka assert macro will write the failure to the
803 * standard error stream and signal a test failure. Due to limitations of the C
804 * language the general C standard library assert() and cmocka's assert_true()
805 * and assert_false() macros can only display the expression that caused the
806 * assert failure. cmocka's type specific assert macros, assert_{type}_equal()
807 * and assert_{type}_not_equal(), display the data that caused the assertion
808 * failure which increases data visibility aiding debugging of failing test
816 * @brief Assert that the given expression is true.
818 * The function prints an error message to standard error and terminates the
819 * test by calling fail() if expression is false (i.e., compares equal to
822 * @param[in] expression The expression to evaluate.
824 * @see assert_int_equal()
825 * @see assert_string_equal()
827 void assert_true(scalar expression);
829 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
835 * @brief Assert that the given expression is false.
837 * The function prints an error message to standard error and terminates the
838 * test by calling fail() if expression is true.
840 * @param[in] expression The expression to evaluate.
842 * @see assert_int_equal()
843 * @see assert_string_equal()
845 void assert_false(scalar expression);
847 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
853 * @brief Assert that the given pointer is non-NULL.
855 * The function prints an error message to standard error and terminates the
856 * test by calling fail() if the pointer is non-NULL.
858 * @param[in] pointer The pointer to evaluate.
862 void assert_non_null(void *pointer);
864 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
870 * @brief Assert that the given pointer is NULL.
872 * The function prints an error message to standard error and terminates the
873 * test by calling fail() if the pointer is non-NULL.
875 * @param[in] pointer The pointer to evaluate.
877 * @see assert_non_null()
879 void assert_null(void *pointer);
881 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
887 * @brief Assert that the two given integers are equal.
889 * The function prints an error message to standard error and terminates the
890 * test by calling fail() if the integers are not equal.
892 * @param[in] a The first integer to compare.
894 * @param[in] b The integer to compare against the first one.
896 void assert_int_equal(int a, int b);
898 #define assert_int_equal(a, b) \
899 _assert_int_equal(cast_to_largest_integral_type(a), \
900 cast_to_largest_integral_type(b), \
906 * @brief Assert that the two given integers are not equal.
908 * The function prints an error message to standard error and terminates the
909 * test by calling fail() if the integers are equal.
911 * @param[in] a The first integer to compare.
913 * @param[in] b The integer to compare against the first one.
915 * @see assert_int_equal()
917 void assert_int_not_equal(int a, int b);
919 #define assert_int_not_equal(a, b) \
920 _assert_int_not_equal(cast_to_largest_integral_type(a), \
921 cast_to_largest_integral_type(b), \
927 * @brief Assert that the two given strings are equal.
929 * The function prints an error message to standard error and terminates the
930 * test by calling fail() if the strings are not equal.
932 * @param[in] a The string to check.
934 * @param[in] b The other string to compare.
936 void assert_string_equal(const char *a, const char *b);
938 #define assert_string_equal(a, b) \
939 _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
945 * @brief Assert that the two given strings are not equal.
947 * The function prints an error message to standard error and terminates the
948 * test by calling fail() if the strings are equal.
950 * @param[in] a The string to check.
952 * @param[in] b The other string to compare.
954 void assert_string_not_equal(const char *a, const char *b);
956 #define assert_string_not_equal(a, b) \
957 _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
963 * @brief Assert that the two given areas of memory are equal, otherwise fail.
965 * The function prints an error message to standard error and terminates the
966 * test by calling fail() if the memory is not equal.
968 * @param[in] a The first memory area to compare
969 * (interpreted as unsigned char).
971 * @param[in] b The second memory area to compare
972 * (interpreted as unsigned char).
974 * @param[in] size The first n bytes of the memory areas to compare.
976 void assert_memory_equal(const void *a, const void *b, size_t size);
978 #define assert_memory_equal(a, b, size) \
979 _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \
985 * @brief Assert that the two given areas of memory are not equal.
987 * The function prints an error message to standard error and terminates the
988 * test by calling fail() if the memory is equal.
990 * @param[in] a The first memory area to compare
991 * (interpreted as unsigned char).
993 * @param[in] b The second memory area to compare
994 * (interpreted as unsigned char).
996 * @param[in] size The first n bytes of the memory areas to compare.
998 void assert_memory_not_equal(const void *a, const void *b, size_t size);
1000 #define assert_memory_not_equal(a, b, size) \
1001 _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
1007 * @brief Assert that the specified value is bigger than the minimum and
1008 * smaller than the maximum.
1010 * The function prints an error message to standard error and terminates the
1011 * test by calling fail() if value is not in range.
1013 * @param[in] value The value to check.
1015 * @param[in] minimum The minimum value allowed.
1017 * @param[in] maximum The maximum value allowed.
1019 void assert_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum);
1021 #define assert_in_range(value, minimum, maximum) \
1023 cast_to_largest_integral_type(value), \
1024 cast_to_largest_integral_type(minimum), \
1025 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1030 * @brief Assert that the specified value is smaller than the minimum and
1031 * bigger than the maximum.
1033 * The function prints an error message to standard error and terminates the
1034 * test by calling fail() if value is in range.
1036 * @param[in] value The value to check.
1038 * @param[in] minimum The minimum value to compare.
1040 * @param[in] maximum The maximum value to compare.
1042 void assert_not_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum);
1044 #define assert_not_in_range(value, minimum, maximum) \
1045 _assert_not_in_range( \
1046 cast_to_largest_integral_type(value), \
1047 cast_to_largest_integral_type(minimum), \
1048 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1053 * @brief Assert that the specified value is within a set.
1055 * The function prints an error message to standard error and terminates the
1056 * test by calling fail() if value is not within a set.
1058 * @param[in] value The value to look up
1060 * @param[in] values[] The array to check for the value.
1062 * @param[in] count The size of the values array.
1064 void assert_in_set(uintmax_t value, uintmax_t values[], size_t count);
1066 #define assert_in_set(value, values, number_of_values) \
1067 _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
1072 * @brief Assert that the specified value is not within a set.
1074 * The function prints an error message to standard error and terminates the
1075 * test by calling fail() if value is within a set.
1077 * @param[in] value The value to look up
1079 * @param[in] values[] The array to check for the value.
1081 * @param[in] count The size of the values array.
1083 void assert_not_in_set(uintmax_t value, uintmax_t values[], size_t count);
1085 #define assert_not_in_set(value, values, number_of_values) \
1086 _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
1092 * @defgroup cmocka_exec Running Tests
1095 * This is the way tests are executed with CMocka.
1097 * The following example illustrates this macro's use with the unit_test macro.
1100 * void Test0(void **state);
1101 * void Test1(void **state);
1105 * const UnitTest tests[] = {
1110 * return run_tests(tests);
1119 * @brief Forces the test to fail immediately and quit.
1123 #define fail() _fail(__FILE__, __LINE__)
1128 * @brief Generic method to run a single test.
1130 * @param[in] #function The function to test.
1132 * @return 0 on success, 1 if an error occured.
1135 * // A test case that does nothing and succeeds.
1136 * void null_test_success(void **state) {
1140 * return run_test(null_test_success);
1144 int run_test(#function);
1146 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
1149 /** Initializes a UnitTest structure. */
1150 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
1152 /** Initializes a UnitTest structure with a setup function. */
1153 #define unit_test_setup(test, setup) \
1154 { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
1156 /** Initializes a UnitTest structure with a teardown function. */
1157 #define unit_test_teardown(test, teardown) \
1158 { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
1161 * Initialize an array of UnitTest structures with a setup function for a test
1162 * and a teardown function. Either setup or teardown can be NULL.
1164 #define unit_test_setup_teardown(test, setup, teardown) \
1165 unit_test_setup(test, setup), \
1167 unit_test_teardown(test, teardown)
1171 * @brief Run tests specified by an array of UnitTest structures.
1173 * @param[in] tests[] The array of unit tests to execute.
1175 * @return 0 on success, 1 if an error occured.
1178 * static void setup(void **state) {
1179 * int *answer = malloc(sizeof(int));
1180 * assert_non_null(answer);
1187 * static void teardown(void **state) {
1191 * static void null_test_success(void **state) {
1195 * static void int_test_success(void **state) {
1196 * int *answer = *state;
1197 * assert_int_equal(*answer, 42);
1201 * const UnitTest tests[] = {
1202 * unit_test(null_test_success),
1203 * unit_test_setup_teardown(int_test_success, setup, teardown),
1206 * return run_tests(tests);
1211 * @see unit_test_setup
1212 * @see unit_test_teardown
1213 * @see unit_test_setup_teardown
1215 int run_tests(const UnitTest tests[]);
1217 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
1223 * @defgroup cmocka_alloc Dynamic Memory Allocation
1226 * Memory leaks, buffer overflows and underflows can be checked using cmocka.
1228 * To test for memory leaks, buffer overflows and underflows a module being
1229 * tested by cmocka should replace calls to malloc(), calloc() and free() to
1230 * test_malloc(), test_calloc() and test_free() respectively. Each time a block
1231 * is deallocated using test_free() it is checked for corruption, if a corrupt
1232 * block is found a test failure is signalled. All blocks allocated using the
1233 * test_*() allocation functions are tracked by the cmocka library. When a test
1234 * completes if any allocated blocks (memory leaks) remain they are reported
1235 * and a test failure is signalled.
1237 * For simplicity cmocka currently executes all tests in one process. Therefore
1238 * all test cases in a test application share a single address space which
1239 * means memory corruption from a single test case could potentially cause the
1240 * test application to exit prematurely.
1247 * @brief Test function overriding malloc.
1249 * @param[in] size The bytes which should be allocated.
1251 * @return A pointer to the allocated memory or NULL on error.
1255 * extern void* _test_malloc(const size_t size, const char* file, const int line);
1257 * #define malloc(size) _test_malloc(size, __FILE__, __LINE__)
1260 * void leak_memory() {
1261 * int * const temporary = (int*)malloc(sizeof(int));
1268 void *test_malloc(size_t size);
1270 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
1275 * @brief Test function overriding calloc.
1277 * The memory is set to zero.
1279 * @param[in] nmemb The number of elements for an array to be allocated.
1281 * @param[in] size The size in bytes of each array element to allocate.
1283 * @return A pointer to the allocated memory, NULL on error.
1287 void *test_calloc(size_t nmemb, size_t size);
1289 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
1294 * @brief Test function overriding free(3).
1296 * @param[in] ptr The pointer to the memory space to free.
1300 void test_free(void *ptr);
1302 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
1305 /* Redirect malloc, calloc and free to the unit test allocators. */
1307 #define malloc test_malloc
1308 #define calloc test_calloc
1309 #define free test_free
1310 #endif /* UNIT_TESTING */
1316 * @defgroup cmocka_mock_assert Standard Assertions
1319 * How to handle assert(3) of the standard C library.
1321 * Runtime assert macros like the standard C library's assert() should be
1322 * redefined in modules being tested to use cmocka's mock_assert() function.
1323 * Normally mock_assert() signals a test failure. If a function is called using
1324 * the expect_assert_failure() macro, any calls to mock_assert() within the
1325 * function will result in the execution of the test. If no calls to
1326 * mock_assert() occur during the function called via expect_assert_failure() a
1327 * test failure is signalled.
1333 * @brief Function to replace assert(3) in tested code.
1335 * In conjuction with check_assert() it's possible to determine whether an
1336 * assert condition has failed without stopping a test.
1338 * @param[in] result The expression to assert.
1340 * @param[in] expression The expression as string.
1342 * @param[in] file The file mock_assert() is called.
1344 * @param[in] line The line mock_assert() is called.
1348 * extern void mock_assert(const int result, const char* const expression,
1349 * const char * const file, const int line);
1352 * #define assert(expression) \
1353 * mock_assert((int)(expression), #expression, __FILE__, __LINE__);
1356 * void increment_value(int * const value) {
1363 * @see expect_assert_failure
1365 void mock_assert(const int result, const char* const expression,
1366 const char * const file, const int line);
1370 * @brief Ensure that mock_assert() is called.
1372 * If mock_assert() is called the assert expression string is returned.
1374 * @param[in] fn_call The function will will call mock_assert().
1377 * #define assert mock_assert
1379 * void showmessage(const char *message) {
1383 * int main(int argc, const char* argv[]) {
1384 * expect_assert_failure(show_message(NULL));
1385 * printf("succeeded\n");
1391 void expect_assert_failure(function fn_call);
1393 #define expect_assert_failure(function_call) \
1395 const int result = setjmp(global_expect_assert_env); \
1396 global_expecting_assert = 1; \
1398 print_message("Expected assertion %s occurred\n", \
1399 global_last_failed_assert); \
1400 global_expecting_assert = 0; \
1403 global_expecting_assert = 0; \
1404 print_error("Expected assert in %s\n", #function_call); \
1405 _fail(__FILE__, __LINE__); \
1412 /* Function prototype for setup, test and teardown functions. */
1413 typedef void (*UnitTestFunction)(void **state);
1415 /* Function that determines whether a function parameter value is correct. */
1416 typedef int (*CheckParameterValue)(const LargestIntegralType value,
1417 const LargestIntegralType check_value_data);
1419 /* Type of the unit test function. */
1420 typedef enum UnitTestFunctionType {
1421 UNIT_TEST_FUNCTION_TYPE_TEST = 0,
1422 UNIT_TEST_FUNCTION_TYPE_SETUP,
1423 UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
1424 } UnitTestFunctionType;
1427 * Stores a unit test function with its name and type.
1428 * NOTE: Every setup function must be paired with a teardown function. It's
1429 * possible to specify NULL function pointers.
1431 typedef struct UnitTest {
1433 UnitTestFunction function;
1434 UnitTestFunctionType function_type;
1438 /* Location within some source code. */
1439 typedef struct SourceLocation {
1444 /* Event that's called to check a parameter value. */
1445 typedef struct CheckParameterEvent {
1446 SourceLocation location;
1447 const char *parameter_name;
1448 CheckParameterValue check_value;
1449 LargestIntegralType check_value_data;
1450 } CheckParameterEvent;
1452 /* Used by expect_assert_failure() and mock_assert(). */
1453 extern int global_expecting_assert;
1454 extern jmp_buf global_expect_assert_env;
1455 extern const char * global_last_failed_assert;
1457 /* Retrieves a value for the given function, as set by "will_return". */
1458 LargestIntegralType _mock(const char * const function, const char* const file,
1462 const char* const function, const char* const parameter,
1463 const char* const file, const int line,
1464 const CheckParameterValue check_function,
1465 const LargestIntegralType check_data, CheckParameterEvent * const event,
1468 void _expect_in_set(
1469 const char* const function, const char* const parameter,
1470 const char* const file, const int line, const LargestIntegralType values[],
1471 const size_t number_of_values, const int count);
1472 void _expect_not_in_set(
1473 const char* const function, const char* const parameter,
1474 const char* const file, const int line, const LargestIntegralType values[],
1475 const size_t number_of_values, const int count);
1477 void _expect_in_range(
1478 const char* const function, const char* const parameter,
1479 const char* const file, const int line,
1480 const LargestIntegralType minimum,
1481 const LargestIntegralType maximum, const int count);
1482 void _expect_not_in_range(
1483 const char* const function, const char* const parameter,
1484 const char* const file, const int line,
1485 const LargestIntegralType minimum,
1486 const LargestIntegralType maximum, const int count);
1489 const char* const function, const char* const parameter,
1490 const char* const file, const int line, const LargestIntegralType value,
1492 void _expect_not_value(
1493 const char* const function, const char* const parameter,
1494 const char* const file, const int line, const LargestIntegralType value,
1497 void _expect_string(
1498 const char* const function, const char* const parameter,
1499 const char* const file, const int line, const char* string,
1501 void _expect_not_string(
1502 const char* const function, const char* const parameter,
1503 const char* const file, const int line, const char* string,
1506 void _expect_memory(
1507 const char* const function, const char* const parameter,
1508 const char* const file, const int line, const void* const memory,
1509 const size_t size, const int count);
1510 void _expect_not_memory(
1511 const char* const function, const char* const parameter,
1512 const char* const file, const int line, const void* const memory,
1513 const size_t size, const int count);
1516 const char* const function, const char* const parameter,
1517 const char* const file, const int line, const int count);
1519 void _check_expected(
1520 const char * const function_name, const char * const parameter_name,
1521 const char* file, const int line, const LargestIntegralType value);
1523 void _will_return(const char * const function_name, const char * const file,
1524 const int line, const LargestIntegralType value,
1526 void _assert_true(const LargestIntegralType result,
1527 const char* const expression,
1528 const char * const file, const int line);
1529 void _assert_int_equal(
1530 const LargestIntegralType a, const LargestIntegralType b,
1531 const char * const file, const int line);
1532 void _assert_int_not_equal(
1533 const LargestIntegralType a, const LargestIntegralType b,
1534 const char * const file, const int line);
1535 void _assert_string_equal(const char * const a, const char * const b,
1536 const char * const file, const int line);
1537 void _assert_string_not_equal(const char * const a, const char * const b,
1538 const char *file, const int line);
1539 void _assert_memory_equal(const void * const a, const void * const b,
1540 const size_t size, const char* const file,
1542 void _assert_memory_not_equal(const void * const a, const void * const b,
1543 const size_t size, const char* const file,
1545 void _assert_in_range(
1546 const LargestIntegralType value, const LargestIntegralType minimum,
1547 const LargestIntegralType maximum, const char* const file, const int line);
1548 void _assert_not_in_range(
1549 const LargestIntegralType value, const LargestIntegralType minimum,
1550 const LargestIntegralType maximum, const char* const file, const int line);
1551 void _assert_in_set(
1552 const LargestIntegralType value, const LargestIntegralType values[],
1553 const size_t number_of_values, const char* const file, const int line);
1554 void _assert_not_in_set(
1555 const LargestIntegralType value, const LargestIntegralType values[],
1556 const size_t number_of_values, const char* const file, const int line);
1558 void* _test_malloc(const size_t size, const char* file, const int line);
1559 void* _test_calloc(const size_t number_of_elements, const size_t size,
1560 const char* file, const int line);
1561 void _test_free(void* const ptr, const char* file, const int line);
1563 void _fail(const char * const file, const int line);
1565 const char * const function_name, const UnitTestFunction Function,
1566 void ** const volatile state, const UnitTestFunctionType function_type,
1567 const void* const heap_check_point);
1568 int _run_tests(const UnitTest * const tests, const size_t number_of_tests);
1570 /* Standard output and error print methods. */
1571 void print_message(const char* const format, ...) PRINTF_ATTRIBUTE(1, 2);
1572 void print_error(const char* const format, ...) PRINTF_ATTRIBUTE(1, 2);
1573 void vprint_message(const char* const format, va_list args) PRINTF_ATTRIBUTE(1, 0);
1574 void vprint_error(const char* const format, va_list args) PRINTF_ATTRIBUTE(1, 0);
1578 #endif /* CMOCKA_H_ */