cmocka.h   cmocka.h 
skipping to change at line 38 skipping to change at line 38
extern "C" { extern "C" {
# endif /* __cplusplus */ # endif /* __cplusplus */
int __stdcall IsDebuggerPresent(); int __stdcall IsDebuggerPresent();
# ifdef __cplusplus # ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */
# endif /* __cplusplus */ # endif /* __cplusplus */
# endif /* _MSC_VER < 1500 */ # endif /* _MSC_VER < 1500 */
# endif /* _MSC_VER */ # endif /* _MSC_VER */
#endif /* _WIN32 */ #endif /* _WIN32 */
/* /**
* @defgroup cmocka The CMocka API
*
* These headers or their equivalents should be included prior to including * These headers or their equivalents should be included prior to including
* this header file. * this header file.
* * @code
* #include <stdarg.h> * #include <stdarg.h>
* #include <stddef.h> * #include <stddef.h>
* #include <setjmp.h> * #include <setjmp.h>
* @endcode
* *
* This allows test applications to use custom definitions of C standard * This allows test applications to use custom definitions of C standard
* library functions and types. * library functions and types.
*
* @{
*/ */
/* For those who are used to __func__ from gcc. */ /* For those who are used to __func__ from gcc. */
#ifndef __func__ #ifndef __func__
#define __func__ __FUNCTION__ #define __func__ __FUNCTION__
#endif #endif
/* GCC have printf type attribute check. */ /* If __WORDSIZE is not set, try to figure it out and default to 32 bit. */
#ifdef __GNUC__ #ifndef __WORDSIZE
#define CMOCKA_PRINTF_ATTRIBUTE(a,b) \ # if defined(__x86_64__) && !defined(__ILP32__)
__attribute__ ((__format__ (__printf__, a, b))) # define __WORDSIZE 64
#else # else
#define CMOCKA_PRINTF_ATTRIBUTE(a,b) # define __WORDSIZE 32
#endif /* __GNUC__ */ # endif
#endif
#ifdef DOXYGEN
/** /**
* @defgroup cmocka The CMocka API
*
* TODO Describe cmocka.
*
* @{
*/
/*
* Largest integral type. This type should be large enough to hold any * Largest integral type. This type should be large enough to hold any
* pointer or integer supported by the compiler. * pointer or integer supported by the compiler.
*/ */
typedef uintmax_t LargestIntegralType;
#else /* DOXGEN */
#ifndef LargestIntegralType #ifndef LargestIntegralType
#define LargestIntegralType unsigned long long # if __WORDSIZE == 64
# define LargestIntegralType unsigned long int
# else
# define LargestIntegralType unsigned long long int
# endif
#endif /* LargestIntegralType */ #endif /* LargestIntegralType */
#endif /* DOXYGEN */
/* Printf format used to display LargestIntegralType. */ /* Printf format used to display LargestIntegralType. */
#ifndef LargestIntegralTypePrintfFormat #ifndef LargestIntegralTypePrintfFormat
#ifdef _WIN32 # ifdef _WIN32
#define LargestIntegralTypePrintfFormat "0x%I64x" # define LargestIntegralTypePrintfFormat "0x%I64x"
#else # else
#define LargestIntegralTypePrintfFormat "%#llx" # if __WORDSIZE == 64
#endif /* _WIN32 */ # define LargestIntegralTypePrintfFormat "%#lx"
# else
# define LargestIntegralTypePrintfFormat "%#llx"
# endif
# endif /* _WIN32 */
#endif /* LargestIntegralTypePrintfFormat */ #endif /* LargestIntegralTypePrintfFormat */
/* Perform an unsigned cast to LargestIntegralType. */ /* Perform an unsigned cast to LargestIntegralType. */
#define cast_to_largest_integral_type(value) \ #define cast_to_largest_integral_type(value) \
((LargestIntegralType)((size_t)(value))) ((LargestIntegralType)(value))
/* Smallest integral type capable of holding a pointer. */ /* Smallest integral type capable of holding a pointer. */
#if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED) #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED)
# if defined(_WIN32) # if defined(_WIN32)
/* WIN32 is an ILP32 platform */ /* WIN32 is an ILP32 platform */
typedef unsigned int uintptr_t; typedef unsigned int uintptr_t;
# elif defined(_WIN64) # elif defined(_WIN64)
typedef unsigned long int uintptr_t typedef unsigned long int uintptr_t
# else /* _WIN32 */ # else /* _WIN32 */
skipping to change at line 123 skipping to change at line 133
# endif # endif
# endif /* __WORDSIZE */ # endif /* __WORDSIZE */
# endif /* _WIN32 */ # endif /* _WIN32 */
# define _UINTPTR_T # define _UINTPTR_T
# define _UINTPTR_T_DEFINED # define _UINTPTR_T_DEFINED
#endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */ #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */
/* Perform an unsigned cast to uintptr_t. */ /* Perform an unsigned cast to uintptr_t. */
#define cast_to_pointer_integral_type(value) \ #define cast_to_pointer_integral_type(value) \
((uintptr_t)(value)) ((uintptr_t)((size_t)(value)))
/* Perform a cast of a pointer to uintmax_t */ /* Perform a cast of a pointer to LargestIntegralType */
#define cast_ptr_to_largest_integral_type(value) \ #define cast_ptr_to_largest_integral_type(value) \
cast_to_largest_integral_type(cast_to_pointer_integral_type(value)) cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
/* GCC have printf type attribute check. */
#ifdef __GNUC__
#define CMOCKA_PRINTF_ATTRIBUTE(a,b) \
__attribute__ ((__format__ (__printf__, a, b)))
#else
#define CMOCKA_PRINTF_ATTRIBUTE(a,b)
#endif /* __GNUC__ */
#if defined(__GNUC__)
#define CMOCKA_DEPRECATED __attribute__ ((deprecated))
#elif defined(_MSC_VER)
#define CMOCKA_DEPRECATED __declspec(deprecated)
#else
#define CMOCKA_DEPRECATED
#endif
/** /**
* @defgroup cmocka_mock Mock Objects * @defgroup cmocka_mock Mock Objects
* @ingroup cmocka * @ingroup cmocka
* *
* Mock objects mock objects are simulated objects that mimic the behavior of * Mock objects mock objects are simulated objects that mimic the behavior of
* real objects. Instead of calling the real objects, the tested object cal ls a * real objects. Instead of calling the real objects, the tested object cal ls a
* mock object that merely asserts that the correct methods were called, wi th * mock object that merely asserts that the correct methods were called, wi th
* the expected parameters, in the correct order. * the expected parameters, in the correct order.
* *
* <ul> * <ul>
skipping to change at line 185 skipping to change at line 211
*/ */
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Retrieve a return value of the current function. * @brief Retrieve a return value of the current function.
* *
* @return The value which was stored to return by this function. * @return The value which was stored to return by this function.
* *
* @see will_return() * @see will_return()
*/ */
void *mock(void); LargestIntegralType mock(void);
#else #else
#define mock() _mock(__func__, __FILE__, __LINE__) #define mock() _mock(__func__, __FILE__, __LINE__)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Retrieve a typed return value of the current function. * @brief Retrieve a typed return value of the current function.
* *
* The value would be casted to type internally to avoid having the * The value would be casted to type internally to avoid having the
* caller to do the cast manually. * caller to do the cast manually.
skipping to change at line 211 skipping to change at line 237
* @code * @code
* int param; * int param;
* *
* param = mock_type(int); * param = mock_type(int);
* @endcode * @endcode
* *
* @see will_return() * @see will_return()
* @see mock() * @see mock()
* @see mock_ptr_type() * @see mock_ptr_type()
*/ */
void *mock_type(#type); #type mock_type(#type);
#else #else
#define mock_type(type) ((type) mock()) #define mock_type(type) ((type) mock())
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Retrieve a typed return value of the current function. * @brief Retrieve a typed return value of the current function.
* *
* The value would be casted to type internally to avoid having the * The value would be casted to type internally to avoid having the
* caller to do the cast manually but also casted to uintptr_t to make * caller to do the cast manually but also casted to uintptr_t to make
skipping to change at line 238 skipping to change at line 264
* @code * @code
* char *param; * char *param;
* *
* param = mock_ptr_type(char *); * param = mock_ptr_type(char *);
* @endcode * @endcode
* *
* @see will_return() * @see will_return()
* @see mock() * @see mock()
* @see mock_type() * @see mock_type()
*/ */
void *mock_ptr_type(#type); type mock_ptr_type(#type);
#else #else
#define mock_ptr_type(type) ((type) (uintptr_t) mock()) #define mock_ptr_type(type) ((type) (uintptr_t) mock())
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Store a value to be returned by mock() later. * @brief Store a value to be returned by mock() later.
* *
* @param[in] #function The function which should return the given value. * @param[in] #function The function which should return the given value.
* *
skipping to change at line 268 skipping to change at line 294
* { * {
* will_return(return_integer, 42); * will_return(return_integer, 42);
* *
* assert_int_equal(my_function_calling_return_integer(), 42); * assert_int_equal(my_function_calling_return_integer(), 42);
* } * }
* @endcode * @endcode
* *
* @see mock() * @see mock()
* @see will_return_count() * @see will_return_count()
*/ */
void will_return(#function, void *value); void will_return(#function, LargestIntegralType value);
#else #else
#define will_return(function, value) \ #define will_return(function, value) \
_will_return(#function, __FILE__, __LINE__, \ _will_return(#function, __FILE__, __LINE__, \
cast_to_largest_integral_type(value), 1) cast_to_largest_integral_type(value), 1)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Store a value to be returned by mock() later. * @brief Store a value to be returned by mock() later.
* *
* @param[in] #function The function which should return the given value. * @param[in] #function The function which should return the given value.
* *
* @param[in] value The value to be returned by mock(). * @param[in] value The value to be returned by mock().
* *
* @param[in] count The parameter returns the number of times the value sh ould * @param[in] count The parameter returns the number of times the value sh ould
* be returned by mock(). If count is set to -1 the value will * be returned by mock(). If count is set to -1 the value will
* always be returned. * always be returned.
* *
* @see mock() * @see mock()
*/ */
void will_return_count(#function, void *value, int count); void will_return_count(#function, LargestIntegralType value, int count);
#else #else
#define will_return_count(function, value, count) \ #define will_return_count(function, value, count) \
_will_return(#function, __FILE__, __LINE__, \ _will_return(#function, __FILE__, __LINE__, \
cast_to_largest_integral_type(value), count) cast_to_largest_integral_type(value), count)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Store a value that will be always returned by mock(). * @brief Store a value that will be always returned by mock().
* *
* @param[in] #function The function which should return the given value. * @param[in] #function The function which should return the given value.
* *
* @param[in] value The value to be returned by mock(). * @param[in] #value The value to be returned by mock().
* *
* This is equivalent to: * This is equivalent to:
* @code * @code
* will_return_count(function, value, -1); * will_return_count(function, value, -1);
* @endcode * @endcode
* *
* @see will_return_count() * @see will_return_count()
* @see mock() * @see mock()
*/ */
void will_return_always(#function, void *value); void will_return_always(#function, LargestIntegralType value);
#else #else
#define will_return_always(function, value) \ #define will_return_always(function, value) \
will_return_count(function, (value), -1) will_return_count(function, (value), -1)
#endif #endif
/** @} */ /** @} */
/** /**
* @defgroup cmocka_param Checking Parameters * @defgroup cmocka_param Checking Parameters
* @ingroup cmocka * @ingroup cmocka
skipping to change at line 409 skipping to change at line 435
* The event is triggered by calling check_expected() in the mocked functio n. * The event is triggered by calling check_expected() in the mocked functio n.
* *
* @param[in] #function The function to add the check for. * @param[in] #function The function to add the check for.
* *
* @param[in] #parameter The name of the parameter passed to the function. * @param[in] #parameter The name of the parameter passed to the function.
* *
* @param[in] value_array[] The array to check for the value. * @param[in] value_array[] The array to check for the value.
* *
* @see check_expected(). * @see check_expected().
*/ */
void expect_in_set(#function, #parameter, uintmax_t value_array[]); void expect_in_set(#function, #parameter, LargestIntegralType value_array[] );
#else #else
#define expect_in_set(function, parameter, value_array) \ #define expect_in_set(function, parameter, value_array) \
expect_in_set_count(function, parameter, value_array, 1) expect_in_set_count(function, parameter, value_array, 1)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Add an event to check if the parameter value is part of the provi ded * @brief Add an event to check if the parameter value is part of the provi ded
* array. * array.
* *
skipping to change at line 434 skipping to change at line 460
* @param[in] #parameter The name of the parameter passed to the function. * @param[in] #parameter The name of the parameter passed to the function.
* *
* @param[in] value_array[] The array to check for the value. * @param[in] value_array[] The array to check for the value.
* *
* @param[in] count The count parameter returns the number of times the v alue * @param[in] count The count parameter returns the number of times the v alue
* should be returned by check_expected(). If count is s et * should be returned by check_expected(). If count is s et
* to -1 the value will always be returned. * to -1 the value will always be returned.
* *
* @see check_expected(). * @see check_expected().
*/ */
void expect_in_set_count(#function, #parameter, uintmax_t value_array[], si ze_t count); void expect_in_set_count(#function, #parameter, LargestIntegralType value_a rray[], size_t count);
#else #else
#define expect_in_set_count(function, parameter, value_array, count) \ #define expect_in_set_count(function, parameter, value_array, count) \
_expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \ _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
sizeof(value_array) / sizeof((value_array)[0]), count) sizeof(value_array) / sizeof((value_array)[0]), count)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Add an event to check if the parameter value is not part of the * @brief Add an event to check if the parameter value is not part of the
* provided array. * provided array.
skipping to change at line 456 skipping to change at line 482
* The event is triggered by calling check_expected() in the mocked functio n. * The event is triggered by calling check_expected() in the mocked functio n.
* *
* @param[in] #function The function to add the check for. * @param[in] #function The function to add the check for.
* *
* @param[in] #parameter The name of the parameter passed to the function. * @param[in] #parameter The name of the parameter passed to the function.
* *
* @param[in] value_array[] The array to check for the value. * @param[in] value_array[] The array to check for the value.
* *
* @see check_expected(). * @see check_expected().
*/ */
void expect_not_in_set(#function, #parameter, uintmax_t value_array[]); void expect_not_in_set(#function, #parameter, LargestIntegralType value_arr ay[]);
#else #else
#define expect_not_in_set(function, parameter, value_array) \ #define expect_not_in_set(function, parameter, value_array) \
expect_not_in_set_count(function, parameter, value_array, 1) expect_not_in_set_count(function, parameter, value_array, 1)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Add an event to check if the parameter value is not part of the * @brief Add an event to check if the parameter value is not part of the
* provided array. * provided array.
* *
skipping to change at line 481 skipping to change at line 507
* @param[in] #parameter The name of the parameter passed to the function. * @param[in] #parameter The name of the parameter passed to the function.
* *
* @param[in] value_array[] The array to check for the value. * @param[in] value_array[] The array to check for the value.
* *
* @param[in] count The count parameter returns the number of times the v alue * @param[in] count The count parameter returns the number of times the v alue
* should be returned by check_expected(). If count is s et * should be returned by check_expected(). If count is s et
* to -1 the value will always be returned. * to -1 the value will always be returned.
* *
* @see check_expected(). * @see check_expected().
*/ */
void expect_not_in_set_count(#function, #parameter, uintmax_t value_array[] , size_t count); void expect_not_in_set_count(#function, #parameter, LargestIntegralType val ue_array[], size_t count);
#else #else
#define expect_not_in_set_count(function, parameter, value_array, count) \ #define expect_not_in_set_count(function, parameter, value_array, count) \
_expect_not_in_set( \ _expect_not_in_set( \
#function, #parameter, __FILE__, __LINE__, value_array, \ #function, #parameter, __FILE__, __LINE__, value_array, \
sizeof(value_array) / sizeof((value_array)[0]), count) sizeof(value_array) / sizeof((value_array)[0]), count)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Add an event to check a parameter is inside a numerical range. * @brief Add an event to check a parameter is inside a numerical range.
skipping to change at line 506 skipping to change at line 532
* @param[in] #function The function to add the check for. * @param[in] #function The function to add the check for.
* *
* @param[in] #parameter The name of the parameter passed to the function. * @param[in] #parameter The name of the parameter passed to the function.
* *
* @param[in] minimum The lower boundary of the interval to check against . * @param[in] minimum The lower boundary of the interval to check against .
* *
* @param[in] maximum The upper boundary of the interval to check against . * @param[in] maximum The upper boundary of the interval to check against .
* *
* @see check_expected(). * @see check_expected().
*/ */
void expect_in_range(#function, #parameter, uintmax_t minimum, uintmax_t ma ximum); void expect_in_range(#function, #parameter, LargestIntegralType minimum, La rgestIntegralType maximum);
#else #else
#define expect_in_range(function, parameter, minimum, maximum) \ #define expect_in_range(function, parameter, minimum, maximum) \
expect_in_range_count(function, parameter, minimum, maximum, 1) expect_in_range_count(function, parameter, minimum, maximum, 1)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Add an event to repeatedly check a parameter is inside a * @brief Add an event to repeatedly check a parameter is inside a
* numerical range. The check would succeed if minimum <= value <= maximum. * numerical range. The check would succeed if minimum <= value <= maximum.
* *
skipping to change at line 533 skipping to change at line 559
* @param[in] minimum The lower boundary of the interval to check against . * @param[in] minimum The lower boundary of the interval to check against .
* *
* @param[in] maximum The upper boundary of the interval to check against . * @param[in] maximum The upper boundary of the interval to check against .
* *
* @param[in] count The count parameter returns the number of times the v alue * @param[in] count The count parameter returns the number of times the v alue
* should be returned by check_expected(). If count is s et * should be returned by check_expected(). If count is s et
* to -1 the value will always be returned. * to -1 the value will always be returned.
* *
* @see check_expected(). * @see check_expected().
*/ */
void expect_in_range_count(#function, #parameter, uintmax_t minimum, uintma x_t maximum, size_t count); void expect_in_range_count(#function, #parameter, LargestIntegralType minim um, LargestIntegralType maximum, size_t count);
#else #else
#define expect_in_range_count(function, parameter, minimum, maximum, count) \ #define expect_in_range_count(function, parameter, minimum, maximum, count) \
_expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \ _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
maximum, count) maximum, count)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Add an event to check a parameter is outside a numerical range. * @brief Add an event to check a parameter is outside a numerical range.
* The check would succeed if minimum > value > maximum. * The check would succeed if minimum > value > maximum.
skipping to change at line 557 skipping to change at line 583
* @param[in] #function The function to add the check for. * @param[in] #function The function to add the check for.
* *
* @param[in] #parameter The name of the parameter passed to the function. * @param[in] #parameter The name of the parameter passed to the function.
* *
* @param[in] minimum The lower boundary of the interval to check against . * @param[in] minimum The lower boundary of the interval to check against .
* *
* @param[in] maximum The upper boundary of the interval to check against . * @param[in] maximum The upper boundary of the interval to check against .
* *
* @see check_expected(). * @see check_expected().
*/ */
void expect_not_in_range(#function, #parameter, uintmax_t minimum, uintmax_ t maximum); void expect_not_in_range(#function, #parameter, LargestIntegralType minimum , LargestIntegralType maximum);
#else #else
#define expect_not_in_range(function, parameter, minimum, maximum) \ #define expect_not_in_range(function, parameter, minimum, maximum) \
expect_not_in_range_count(function, parameter, minimum, maximum, 1) expect_not_in_range_count(function, parameter, minimum, maximum, 1)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Add an event to repeatedly check a parameter is outside a * @brief Add an event to repeatedly check a parameter is outside a
* numerical range. The check would succeed if minimum > value > maximum. * numerical range. The check would succeed if minimum > value > maximum.
* *
skipping to change at line 584 skipping to change at line 610
* @param[in] minimum The lower boundary of the interval to check against . * @param[in] minimum The lower boundary of the interval to check against .
* *
* @param[in] maximum The upper boundary of the interval to check against . * @param[in] maximum The upper boundary of the interval to check against .
* *
* @param[in] count The count parameter returns the number of times the v alue * @param[in] count The count parameter returns the number of times the v alue
* should be returned by check_expected(). If count is s et * should be returned by check_expected(). If count is s et
* to -1 the value will always be returned. * to -1 the value will always be returned.
* *
* @see check_expected(). * @see check_expected().
*/ */
void expect_not_in_range_count(#function, #parameter, uintmax_t minimum, ui ntmax_t maximum, size_t count); void expect_not_in_range_count(#function, #parameter, LargestIntegralType m inimum, LargestIntegralType maximum, size_t count);
#else #else
#define expect_not_in_range_count(function, parameter, minimum, maximum, \ #define expect_not_in_range_count(function, parameter, minimum, maximum, \
count) \ count) \
_expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \ _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
minimum, maximum, count) minimum, maximum, count)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Add an event to check if a parameter is the given value. * @brief Add an event to check if a parameter is the given value.
skipping to change at line 606 skipping to change at line 632
* The event is triggered by calling check_expected() in the mocked functio n. * The event is triggered by calling check_expected() in the mocked functio n.
* *
* @param[in] #function The function to add the check for. * @param[in] #function The function to add the check for.
* *
* @param[in] #parameter The name of the parameter passed to the function. * @param[in] #parameter The name of the parameter passed to the function.
* *
* @param[in] value The value to check. * @param[in] value The value to check.
* *
* @see check_expected(). * @see check_expected().
*/ */
void expect_value(#function, #parameter, uintmax_t value); void expect_value(#function, #parameter, LargestIntegralType value);
#else #else
#define expect_value(function, parameter, value) \ #define expect_value(function, parameter, value) \
expect_value_count(function, parameter, value, 1) expect_value_count(function, parameter, value, 1)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Add an event to repeatedly check if a parameter is the given valu e. * @brief Add an event to repeatedly check if a parameter is the given valu e.
* *
* The event is triggered by calling check_expected() in the mocked functio n. * The event is triggered by calling check_expected() in the mocked functio n.
skipping to change at line 630 skipping to change at line 656
* @param[in] #parameter The name of the parameter passed to the function. * @param[in] #parameter The name of the parameter passed to the function.
* *
* @param[in] value The value to check. * @param[in] value The value to check.
* *
* @param[in] count The count parameter returns the number of times the v alue * @param[in] count The count parameter returns the number of times the v alue
* should be returned by check_expected(). If count is s et * should be returned by check_expected(). If count is s et
* to -1 the value will always be returned. * to -1 the value will always be returned.
* *
* @see check_expected(). * @see check_expected().
*/ */
void expect_value_count(#function, #parameter, uintmax_t value, size_t coun t); void expect_value_count(#function, #parameter, LargestIntegralType value, s ize_t count);
#else #else
#define expect_value_count(function, parameter, value, count) \ #define expect_value_count(function, parameter, value, count) \
_expect_value(#function, #parameter, __FILE__, __LINE__, \ _expect_value(#function, #parameter, __FILE__, __LINE__, \
cast_to_largest_integral_type(value), count) cast_to_largest_integral_type(value), count)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Add an event to check if a parameter isn't the given value. * @brief Add an event to check if a parameter isn't the given value.
* *
* The event is triggered by calling check_expected() in the mocked functio n. * The event is triggered by calling check_expected() in the mocked functio n.
* *
* @param[in] #function The function to add the check for. * @param[in] #function The function to add the check for.
* *
* @param[in] #parameter The name of the parameter passed to the function. * @param[in] #parameter The name of the parameter passed to the function.
* *
* @param[in] value The value to check. * @param[in] value The value to check.
* *
* @see check_expected(). * @see check_expected().
*/ */
void expect_not_value(#function, #parameter, uintmax_t value); void expect_not_value(#function, #parameter, LargestIntegralType value);
#else #else
#define expect_not_value(function, parameter, value) \ #define expect_not_value(function, parameter, value) \
expect_not_value_count(function, parameter, value, 1) expect_not_value_count(function, parameter, value, 1)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Add an event to repeatedly check if a parameter isn't the given v alue. * @brief Add an event to repeatedly check if a parameter isn't the given v alue.
* *
* The event is triggered by calling check_expected() in the mocked functio n. * The event is triggered by calling check_expected() in the mocked functio n.
skipping to change at line 675 skipping to change at line 701
* @param[in] #parameter The name of the parameter passed to the function. * @param[in] #parameter The name of the parameter passed to the function.
* *
* @param[in] value The value to check. * @param[in] value The value to check.
* *
* @param[in] count The count parameter returns the number of times the v alue * @param[in] count The count parameter returns the number of times the v alue
* should be returned by check_expected(). If count is s et * should be returned by check_expected(). If count is s et
* to -1 the value will always be returned. * to -1 the value will always be returned.
* *
* @see check_expected(). * @see check_expected().
*/ */
void expect_not_value_count(#function, #parameter, uintmax_t value, size_t count); void expect_not_value_count(#function, #parameter, LargestIntegralType valu e, size_t count);
#else #else
#define expect_not_value_count(function, parameter, value, count) \ #define expect_not_value_count(function, parameter, value, count) \
_expect_not_value(#function, #parameter, __FILE__, __LINE__, \ _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
cast_to_largest_integral_type(value), count) cast_to_largest_integral_type(value), count)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Add an event to check if the parameter value is equal to the * @brief Add an event to check if the parameter value is equal to the
* provided string. * provided string.
skipping to change at line 936 skipping to change at line 962
* *
* @param[in] #parameter The parameter to check. * @param[in] #parameter The parameter to check.
*/ */
void check_expected(#parameter); void check_expected(#parameter);
#else #else
#define check_expected(parameter) \ #define check_expected(parameter) \
_check_expected(__func__, #parameter, __FILE__, __LINE__, \ _check_expected(__func__, #parameter, __FILE__, __LINE__, \
cast_to_largest_integral_type(parameter)) cast_to_largest_integral_type(parameter))
#endif #endif
#ifdef DOXYGEN
/**
* @brief Determine whether a function parameter is correct.
*
* This ensures the next value queued by one of the expect_*() macros match
es
* the specified variable.
*
* This function needs to be called in the mock object.
*
* @param[in] #parameter The pointer to check.
*/
void check_expected_ptr(#parameter);
#else
#define check_expected_ptr(parameter) \
_check_expected(__func__, #parameter, __FILE__, __LINE__, \
cast_ptr_to_largest_integral_type(parameter))
#endif
/** @} */ /** @} */
/** /**
* @defgroup cmocka_asserts Assert Macros * @defgroup cmocka_asserts Assert Macros
* @ingroup cmocka * @ingroup cmocka
* *
* This is a set of useful assert macros like the standard C libary's * This is a set of useful assert macros like the standard C libary's
* assert(3) macro. * assert(3) macro.
* *
* On an assertion failure a cmocka assert macro will write the failure to the * On an assertion failure a cmocka assert macro will write the failure to the
skipping to change at line 996 skipping to change at line 1040
* @see assert_string_equal() * @see assert_string_equal()
*/ */
void assert_false(scalar expression); void assert_false(scalar expression);
#else #else
#define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), # c, \ #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), # c, \
__FILE__, __LINE__) __FILE__, __LINE__)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Assert if the return_code is smaller than 0. * @brief Assert that the return_code is greater than or equal to 0.
* *
* The function prints an error message to standard error and terminates th e * The function prints an error message to standard error and terminates th e
* test by calling fail() if the return code is smaller than 0. If the func tion * test by calling fail() if the return code is smaller than 0. If the func tion
* you check sets an errno if it fails you can pass it to the function and * you check sets an errno if it fails you can pass it to the function and
* it will be printed as part of the error message. * it will be printed as part of the error message.
* *
* @param[in] rc The return code to evaluate. * @param[in] rc The return code to evaluate.
* *
* @param[in] error Pass errno here or 0. * @param[in] error Pass errno here or 0.
*/ */
skipping to change at line 1172 skipping to change at line 1216
*/ */
void assert_memory_not_equal(const void *a, const void *b, size_t size); void assert_memory_not_equal(const void *a, const void *b, size_t size);
#else #else
#define assert_memory_not_equal(a, b, size) \ #define assert_memory_not_equal(a, b, size) \
_assert_memory_not_equal((const void*)(a), (const void*)(b), size, \ _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
__FILE__, __LINE__) __FILE__, __LINE__)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Assert that the specified value is bigger than the minimum and * @brief Assert that the specified value is not smaller than the minimum
* smaller than the maximum. * and and not greater than the maximum.
* *
* The function prints an error message to standard error and terminates th e * The function prints an error message to standard error and terminates th e
* test by calling fail() if value is not in range. * test by calling fail() if value is not in range.
* *
* @param[in] value The value to check. * @param[in] value The value to check.
* *
* @param[in] minimum The minimum value allowed. * @param[in] minimum The minimum value allowed.
* *
* @param[in] maximum The maximum value allowed. * @param[in] maximum The maximum value allowed.
*/ */
void assert_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum) ; void assert_in_range(LargestIntegralType value, LargestIntegralType minimum , LargestIntegralType maximum);
#else #else
#define assert_in_range(value, minimum, maximum) \ #define assert_in_range(value, minimum, maximum) \
_assert_in_range( \ _assert_in_range( \
cast_to_largest_integral_type(value), \ cast_to_largest_integral_type(value), \
cast_to_largest_integral_type(minimum), \ cast_to_largest_integral_type(minimum), \
cast_to_largest_integral_type(maximum), __FILE__, __LINE__) cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Assert that the specified value is smaller than the minimum and * @brief Assert that the specified value is smaller than the minimum or
* bigger than the maximum. * greater than the maximum.
* *
* The function prints an error message to standard error and terminates th e * The function prints an error message to standard error and terminates th e
* test by calling fail() if value is in range. * test by calling fail() if value is in range.
* *
* @param[in] value The value to check. * @param[in] value The value to check.
* *
* @param[in] minimum The minimum value to compare. * @param[in] minimum The minimum value to compare.
* *
* @param[in] maximum The maximum value to compare. * @param[in] maximum The maximum value to compare.
*/ */
void assert_not_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maxi mum); void assert_not_in_range(LargestIntegralType value, LargestIntegralType min imum, LargestIntegralType maximum);
#else #else
#define assert_not_in_range(value, minimum, maximum) \ #define assert_not_in_range(value, minimum, maximum) \
_assert_not_in_range( \ _assert_not_in_range( \
cast_to_largest_integral_type(value), \ cast_to_largest_integral_type(value), \
cast_to_largest_integral_type(minimum), \ cast_to_largest_integral_type(minimum), \
cast_to_largest_integral_type(maximum), __FILE__, __LINE__) cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
skipping to change at line 1229 skipping to change at line 1273
* *
* The function prints an error message to standard error and terminates th e * The function prints an error message to standard error and terminates th e
* test by calling fail() if value is not within a set. * test by calling fail() if value is not within a set.
* *
* @param[in] value The value to look up * @param[in] value The value to look up
* *
* @param[in] values[] The array to check for the value. * @param[in] values[] The array to check for the value.
* *
* @param[in] count The size of the values array. * @param[in] count The size of the values array.
*/ */
void assert_in_set(uintmax_t value, uintmax_t values[], size_t count); void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
#else #else
#define assert_in_set(value, values, number_of_values) \ #define assert_in_set(value, values, number_of_values) \
_assert_in_set(value, values, number_of_values, __FILE__, __LINE__) _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Assert that the specified value is not within a set. * @brief Assert that the specified value is not within a set.
* *
* The function prints an error message to standard error and terminates th e * The function prints an error message to standard error and terminates th e
* test by calling fail() if value is within a set. * test by calling fail() if value is within a set.
* *
* @param[in] value The value to look up * @param[in] value The value to look up
* *
* @param[in] values[] The array to check for the value. * @param[in] values[] The array to check for the value.
* *
* @param[in] count The size of the values array. * @param[in] count The size of the values array.
*/ */
void assert_not_in_set(uintmax_t value, uintmax_t values[], size_t count); void assert_not_in_set(LargestIntegralType value, LargestIntegralType value s[], size_t count);
#else #else
#define assert_not_in_set(value, values, number_of_values) \ #define assert_not_in_set(value, values, number_of_values) \
_assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__) _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
#endif #endif
/** @} */ /** @} */
/** /**
* @defgroup cmocka_exec Running Tests * @defgroup cmocka_exec Running Tests
* @ingroup cmocka * @ingroup cmocka
skipping to change at line 1270 skipping to change at line 1314
* This is the way tests are executed with CMocka. * This is the way tests are executed with CMocka.
* *
* The following example illustrates this macro's use with the unit_test ma cro. * The following example illustrates this macro's use with the unit_test ma cro.
* *
* @code * @code
* void Test0(void **state); * void Test0(void **state);
* void Test1(void **state); * void Test1(void **state);
* *
* int main(void) * int main(void)
* { * {
* const UnitTest tests[] = { * const struct CMUnitTest tests[] = {
* unit_test(Test0), * cmocka_unit_test(Test0),
* unit_test(Test1), * cmocka_unit_test(Test1),
* }; * };
* *
* return run_tests(tests); * return cmocka_run_group_tests(tests, NULL, NULL);
* } * }
* @endcode * @endcode
* *
* @{ * @{
*/ */
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Forces the test to fail immediately and quit. * @brief Forces the test to fail immediately and quit.
*/ */
void fail(void); void fail(void);
#else #else
#define fail() _fail(__FILE__, __LINE__) #define fail() _fail(__FILE__, __LINE__)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Forces the test to not be executed, but marked as skipped
*/
void skip(void);
#else
#define skip() _skip(__FILE__, __LINE__)
#endif
#ifdef DOXYGEN
/**
* @brief Forces the test to fail immediately and quit, printing the reason . * @brief Forces the test to fail immediately and quit, printing the reason .
* *
* @code * @code
* fail_msg("This is some error message for test"); * fail_msg("This is some error message for test");
* @endcode * @endcode
* *
* or * or
* *
* @code * @code
* char *error_msg = "This is some error message for test"; * char *error_msg = "This is some error message for test";
skipping to change at line 1318 skipping to change at line 1371
#define fail_msg(msg, ...) do { \ #define fail_msg(msg, ...) do { \
print_error("ERROR: " msg "\n", ##__VA_ARGS__); \ print_error("ERROR: " msg "\n", ##__VA_ARGS__); \
fail(); \ fail(); \
} while (0) } while (0)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Generic method to run a single test. * @brief Generic method to run a single test.
* *
* @deprecated This function was deprecated in favor of cmocka_run_group_te
sts
*
* @param[in] #function The function to test. * @param[in] #function The function to test.
* *
* @return 0 on success, 1 if an error occured. * @return 0 on success, 1 if an error occured.
* *
* @code * @code
* // A test case that does nothing and succeeds. * // A test case that does nothing and succeeds.
* void null_test_success(void **state) { * void null_test_success(void **state) {
* } * }
* *
* int main(void) { * int main(void) {
skipping to change at line 1341 skipping to change at line 1396
*/ */
int run_test(#function); int run_test(#function);
#else #else
#define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NU LL) #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NU LL)
#endif #endif
static inline void _unit_test_dummy(void **state) { static inline void _unit_test_dummy(void **state) {
(void)state; (void)state;
} }
/** Initializes a UnitTest structure. */ /** Initializes a UnitTest structure.
*
* @deprecated This function was deprecated in favor of cmocka_unit_test
*/
#define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST } #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
#define _unit_test_setup(test, setup) \ #define _unit_test_setup(test, setup) \
{ #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP } { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
/** Initializes a UnitTest structure with a setup function. */ /** Initializes a UnitTest structure with a setup function.
*
* @deprecated This function was deprecated in favor of cmocka_unit_test_se
tup
*/
#define unit_test_setup(test, setup) \ #define unit_test_setup(test, setup) \
_unit_test_setup(test, setup), \ _unit_test_setup(test, setup), \
unit_test(test), \ unit_test(test), \
_unit_test_teardown(test, _unit_test_dummy) _unit_test_teardown(test, _unit_test_dummy)
#define _unit_test_teardown(test, teardown) \ #define _unit_test_teardown(test, teardown) \
{ #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN } { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
/** Initializes a UnitTest structure with a teardown function. */ /** Initializes a UnitTest structure with a teardown function.
*
* @deprecated This function was deprecated in favor of cmocka_unit_test_te
ardown
*/
#define unit_test_teardown(test, teardown) \ #define unit_test_teardown(test, teardown) \
_unit_test_setup(test, _unit_test_dummy), \ _unit_test_setup(test, _unit_test_dummy), \
unit_test(test), \ unit_test(test), \
_unit_test_teardown(test, teardown) _unit_test_teardown(test, teardown)
/** Initializes a UnitTest structure for a group setup function. */ /** Initializes a UnitTest structure for a group setup function.
*
* @deprecated This function was deprecated in favor of cmocka_run_group_te
sts
*/
#define group_test_setup(setup) \ #define group_test_setup(setup) \
{ "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP } { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }
/** Initializes a UnitTest structure for a group teardown function. */ /** Initializes a UnitTest structure for a group teardown function.
*
* @deprecated This function was deprecated in favor of cmocka_run_group_te
sts
*/
#define group_test_teardown(teardown) \ #define group_test_teardown(teardown) \
{ "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN } { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }
/** /**
* Initialize an array of UnitTest structures with a setup function for a t est * Initialize an array of UnitTest structures with a setup function for a t est
* and a teardown function. Either setup or teardown can be NULL. * and a teardown function. Either setup or teardown can be NULL.
*
* @deprecated This function was deprecated in favor of
* cmocka_unit_test_setup_teardown
*/ */
#define unit_test_setup_teardown(test, setup, teardown) \ #define unit_test_setup_teardown(test, setup, teardown) \
_unit_test_setup(test, setup), \ _unit_test_setup(test, setup), \
unit_test(test), \ unit_test(test), \
_unit_test_teardown(test, teardown) _unit_test_teardown(test, teardown)
/** Initializes a CMUnitTest structure. */
#define cmocka_unit_test(f) { #f, f, NULL, NULL }
/** Initializes a CMUnitTest structure with a setup function. */
#define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL }
/** Initializes a CMUnitTest structure with a teardown function. */
#define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown }
/**
* Initialize an array of CMUnitTest structures with a setup function for a
test
* and a teardown function. Either setup or teardown can be NULL.
*/
#define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup,
teardown }
#define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0]
)
#define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / size
of(tests)[0])
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Run tests specified by an array of UnitTest structures. * @brief Run tests specified by an array of CMUnitTest structures.
* *
* @param[in] tests[] The array of unit tests to execute. * @param[in] group_tests[] The array of unit tests to execute.
* *
* @return 0 on success, 1 if an error occured. * @param[in] group_setup The setup function which should be called bef
ore
* all unit tests are executed.
*
* @param[in] group_teardown The teardown function to be called after all
* tests have finished.
*
* @return 0 on success, or the number of failed tests.
* *
* @code * @code
* static void setup(void **state) { * static int setup(void **state) {
* int *answer = malloc(sizeof(int)); * int *answer = malloc(sizeof(int));
* assert_non_null(answer); * if (*answer == NULL) {
* * return -1;
* }
* *answer = 42; * *answer = 42;
* *
* *state = answer; * *state = answer;
*
* return 0;
* } * }
* *
* static void teardown(void **state) { * static void teardown(void **state) {
* free(*state); * free(*state);
*
* return 0;
* } * }
* *
* static void null_test_success(void **state) { * static void null_test_success(void **state) {
* (void) state; * (void) state;
* } * }
* *
* static void int_test_success(void **state) { * static void int_test_success(void **state) {
* int *answer = *state; * int *answer = *state;
* assert_int_equal(*answer, 42); * assert_int_equal(*answer, 42);
* } * }
* *
* int main(void) { * int main(void) {
* const UnitTest tests[] = { * const struct CMUnitTest tests[] = {
* unit_test(null_test_success), * cmocka_unit_test(null_test_success),
* unit_test_setup_teardown(int_test_success, setup, teardown), * cmocka_unit_test_setup_teardown(int_test_success, setup, teardow
n),
* }; * };
* *
* return run_tests(tests); * return cmocka_run_group_tests(tests, NULL, NULL);
* } * }
* @endcode * @endcode
* *
* @see unit_test * @see cmocka_unit_test
* @see unit_test_setup * @see cmocka_unit_test_setup
* @see unit_test_teardown * @see cmocka_unit_test_teardown
* @see unit_test_setup_teardown * @see cmocka_unit_test_setup_teardown
*/ */
int run_tests(const UnitTest tests[]); int cmocka_run_group_tests(const struct CMUnitTest group_tests[],
CMFixtureFunction group_setup,
CMFixtureFunction group_teardown);
#else #else
#define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0] # define cmocka_run_group_tests(group_tests, group_setup, group_teardown) \
) _cmocka_run_group_tests(#group_tests, group_tests, sizeof(group_tes
ts) / sizeof(group_tests)[0], group_setup, group_teardown)
#endif #endif
#define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / size #ifdef DOXYGEN
of(tests)[0]) /**
* @brief Run tests specified by an array of CMUnitTest structures and spec
ify
* a name.
*
* @param[in] group_name The name of the group test.
*
* @param[in] group_tests[] The array of unit tests to execute.
*
* @param[in] group_setup The setup function which should be called bef
ore
* all unit tests are executed.
*
* @param[in] group_teardown The teardown function to be called after all
* tests have finished.
*
* @return 0 on success, or the number of failed tests.
*
* @code
* static int setup(void **state) {
* int *answer = malloc(sizeof(int));
* if (*answer == NULL) {
* return -1;
* }
* *answer = 42;
*
* *state = answer;
*
* return 0;
* }
*
* static void teardown(void **state) {
* free(*state);
*
* return 0;
* }
*
* static void null_test_success(void **state) {
* (void) state;
* }
*
* static void int_test_success(void **state) {
* int *answer = *state;
* assert_int_equal(*answer, 42);
* }
*
* int main(void) {
* const struct CMUnitTest tests[] = {
* cmocka_unit_test(null_test_success),
* cmocka_unit_test_setup_teardown(int_test_success, setup, teardow
n),
* };
*
* return cmocka_run_group_tests_name("success_test", tests, NULL, NULL
);
* }
* @endcode
*
* @see cmocka_unit_test
* @see cmocka_unit_test_setup
* @see cmocka_unit_test_teardown
* @see cmocka_unit_test_setup_teardown
*/
int cmocka_run_group_tests_name(const char *group_name,
const struct CMUnitTest group_tests[],
CMFixtureFunction group_setup,
CMFixtureFunction group_teardown);
#else
# define cmocka_run_group_tests_name(group_name, group_tests, group_setup,
group_teardown) \
_cmocka_run_group_tests(group_name, group_tests, sizeof(group_tests
) / sizeof(group_tests)[0], group_setup, group_teardown)
#endif
/** @} */ /** @} */
/** /**
* @defgroup cmocka_alloc Dynamic Memory Allocation * @defgroup cmocka_alloc Dynamic Memory Allocation
* @ingroup cmocka * @ingroup cmocka
* *
* Memory leaks, buffer overflows and underflows can be checked using cmock a. * Memory leaks, buffer overflows and underflows can be checked using cmock a.
* *
* To test for memory leaks, buffer overflows and underflows a module being * To test for memory leaks, buffer overflows and underflows a module being
skipping to change at line 1506 skipping to change at line 1678
* *
* @see calloc(3) * @see calloc(3)
*/ */
void *test_calloc(size_t nmemb, size_t size); void *test_calloc(size_t nmemb, size_t size);
#else #else
#define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
#endif #endif
#ifdef DOXYGEN #ifdef DOXYGEN
/** /**
* @brief Test function overriding realloc which detects buffer overruns
* and memoery leaks.
*
* @param[in] ptr The memory block which should be changed.
*
* @param[in] size The bytes which should be allocated.
*
* @return The newly allocated memory block, NULL on error.
*/
void *test_realloc(void *ptr, size_t size);
#else
#define test_realloc(ptr, size) _test_realloc(ptr, size, __FILE__, __LINE__
)
#endif
#ifdef DOXYGEN
/**
* @brief Test function overriding free(3). * @brief Test function overriding free(3).
* *
* @param[in] ptr The pointer to the memory space to free. * @param[in] ptr The pointer to the memory space to free.
* *
* @see free(3). * @see free(3).
*/ */
void test_free(void *ptr); void test_free(void *ptr);
#else #else
#define test_free(ptr) _test_free(ptr, __FILE__, __LINE__) #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
#endif #endif
/* Redirect malloc, calloc and free to the unit test allocators. */ /* Redirect malloc, calloc and free to the unit test allocators. */
#ifdef UNIT_TESTING #ifdef UNIT_TESTING
#define malloc test_malloc #define malloc test_malloc
#define realloc test_realloc
#define calloc test_calloc #define calloc test_calloc
#define free test_free #define free test_free
#endif /* UNIT_TESTING */ #endif /* UNIT_TESTING */
/** @} */ /** @} */
/** /**
* @defgroup cmocka_mock_assert Standard Assertions * @defgroup cmocka_mock_assert Standard Assertions
* @ingroup cmocka * @ingroup cmocka
* *
skipping to change at line 1657 skipping to change at line 1846
UnitTestFunctionType function_type; UnitTestFunctionType function_type;
} UnitTest; } UnitTest;
typedef struct GroupTest { typedef struct GroupTest {
UnitTestFunction setup; UnitTestFunction setup;
UnitTestFunction teardown; UnitTestFunction teardown;
const UnitTest *tests; const UnitTest *tests;
const size_t number_of_tests; const size_t number_of_tests;
} GroupTest; } GroupTest;
/* Function prototype for test functions. */
typedef void (*CMUnitTestFunction)(void **state);
/* Function prototype for setup and teardown functions. */
typedef int (*CMFixtureFunction)(void **state);
struct CMUnitTest {
const char *name;
CMUnitTestFunction test_func;
CMFixtureFunction setup_func;
CMFixtureFunction teardown_func;
};
/* Location within some source code. */ /* Location within some source code. */
typedef struct SourceLocation { typedef struct SourceLocation {
const char* file; const char* file;
int line; int line;
} SourceLocation; } SourceLocation;
/* Event that's called to check a parameter value. */ /* Event that's called to check a parameter value. */
typedef struct CheckParameterEvent { typedef struct CheckParameterEvent {
SourceLocation location; SourceLocation location;
const char *parameter_name; const char *parameter_name;
skipping to change at line 1784 skipping to change at line 1986
const LargestIntegralType value, const LargestIntegralType minimum, const LargestIntegralType value, const LargestIntegralType minimum,
const LargestIntegralType maximum, const char* const file, const int li ne); const LargestIntegralType maximum, const char* const file, const int li ne);
void _assert_in_set( void _assert_in_set(
const LargestIntegralType value, const LargestIntegralType values[], const LargestIntegralType value, const LargestIntegralType values[],
const size_t number_of_values, const char* const file, const int line); const size_t number_of_values, const char* const file, const int line);
void _assert_not_in_set( void _assert_not_in_set(
const LargestIntegralType value, const LargestIntegralType values[], const LargestIntegralType value, const LargestIntegralType values[],
const size_t number_of_values, const char* const file, const int line); const size_t number_of_values, const char* const file, const int line);
void* _test_malloc(const size_t size, const char* file, const int line); void* _test_malloc(const size_t size, const char* file, const int line);
void* _test_realloc(void *ptr, const size_t size, const char* file, const i nt line);
void* _test_calloc(const size_t number_of_elements, const size_t size, void* _test_calloc(const size_t number_of_elements, const size_t size,
const char* file, const int line); const char* file, const int line);
void _test_free(void* const ptr, const char* file, const int line); void _test_free(void* const ptr, const char* file, const int line);
void _fail(const char * const file, const int line); void _fail(const char * const file, const int line);
void _skip(const char * const file, const int line);
int _run_test( int _run_test(
const char * const function_name, const UnitTestFunction Function, const char * const function_name, const UnitTestFunction Function,
void ** const volatile state, const UnitTestFunctionType function_type, void ** const volatile state, const UnitTestFunctionType function_type,
const void* const heap_check_point); const void* const heap_check_point);
int _run_tests(const UnitTest * const tests, const size_t number_of_tests); CMOCKA_DEPRECATED int _run_tests(const UnitTest * const tests,
int _run_group_tests(const UnitTest * const tests, const size_t number_of_tests);
const size_t number_of_tests); CMOCKA_DEPRECATED int _run_group_tests(const UnitTest * const tests,
const size_t number_of_tests);
/* Test runner */
int _cmocka_run_group_tests(const char *group_name,
const struct CMUnitTest * const tests,
const size_t num_tests,
CMFixtureFunction group_setup,
CMFixtureFunction group_teardown);
/* Standard output and error print methods. */ /* Standard output and error print methods. */
void print_message(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1 , 2); void print_message(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1 , 2);
void print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2); void print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
void vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_A TTRIBUTE(1, 0); void vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_A TTRIBUTE(1, 0);
void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATT RIBUTE(1, 0); void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATT RIBUTE(1, 0);
enum cm_message_output {
CM_OUTPUT_STDOUT,
CM_OUTPUT_SUBUNIT,
CM_OUTPUT_TAP,
CM_OUTPUT_XML,
};
/**
* @brief Function to set the output format for a test.
*
* The ouput format for the test can either be set globally using this
* function or overriden with environment variable CMOCKA_MESSAGE_OUTPUT.
*
* The environment variable can be set to either STDOUT, SUBUNIT, TAP or XM
L.
*
* @param[in] output The output format to use for the test.
*
*/
void cmocka_set_message_output(enum cm_message_output output);
/** @} */ /** @} */
#endif /* CMOCKA_H_ */ #endif /* CMOCKA_H_ */
 End of changes. 73 change blocks. 
85 lines changed or deleted 338 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/