include: Fix count parameter of expect_check() macro
[obnox/cmocka.git] / include / cmocka.h
index 9cd37eaba7ccdaf66a69327dec30e1547a2ef6e0..4799da06cb12e77606fa8dea1e62b0fd816c3312 100644 (file)
 #define CMOCKA_H_
 
 #ifdef _WIN32
-#if _MSC_VER < 1500
-#ifdef __cplusplus
+# ifdef _MSC_VER
+
+# ifndef inline
+#define inline __inline
+# endif /* inline */
+
+#  if _MSC_VER < 1500
+#   ifdef __cplusplus
 extern "C" {
-#endif   /* __cplusplus */
+#   endif   /* __cplusplus */
 int __stdcall IsDebuggerPresent();
-#ifdef __cplusplus
+#   ifdef __cplusplus
 } /* extern "C" */
-#endif   /* __cplusplus */
-#endif  /* _MSC_VER < 1500 */
+#   endif   /* __cplusplus */
+#  endif  /* _MSC_VER < 1500 */
+# endif /* _MSC_VER */
 #endif  /* _WIN32 */
 
 /*
@@ -47,9 +54,10 @@ int __stdcall IsDebuggerPresent();
 
 /* GCC have printf type attribute check.  */
 #ifdef __GNUC__
-#define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
+#define CMOCKA_PRINTF_ATTRIBUTE(a,b) \
+    __attribute__ ((__format__ (__printf__, a, b)))
 #else
-#define PRINTF_ATTRIBUTE(a,b)
+#define CMOCKA_PRINTF_ATTRIBUTE(a,b)
 #endif /* __GNUC__ */
 
 /**
@@ -71,9 +79,9 @@ int __stdcall IsDebuggerPresent();
 /* Printf format used to display LargestIntegralType. */
 #ifndef LargestIntegralTypePrintfFormat
 #ifdef _WIN32
-#define LargestIntegralTypePrintfFormat "%I64x"
+#define LargestIntegralTypePrintfFormat "0x%I64x"
 #else
-#define LargestIntegralTypePrintfFormat "%llx"
+#define LargestIntegralTypePrintfFormat "%#llx"
 #endif /* _WIN32 */
 #endif /* LargestIntegralTypePrintfFormat */
 
@@ -305,7 +313,7 @@ void will_return_count(#function, void *value, int count);
 void will_return_always(#function, void *value);
 #else
 #define will_return_always(function, value) \
-    will_return_count(#function, (value), -1)
+    will_return_count(function, (value), -1)
 #endif
 
 /** @} */
@@ -368,10 +376,10 @@ void will_return_always(#function, void *value);
  * by this function. If the parameter is provided it must be allocated on the
  * heap and doesn't need to be deallocated by the caller.
  *
- * @param[in]  #function  The fuction to add a custom paramater checking function
- *                        for.
+ * @param[in]  #function  The function to add a custom parameter checking
+ *                        function for.
  *
- * @param[in]  #parameter The parametes passed to the function.
+ * @param[in]  #parameter The parameters passed to the function.
  *
  * @param[in]  #check_function  The check function to call.
  *
@@ -381,10 +389,10 @@ void expect_check(#function, #parameter, #check_function, const void *check_data
 #else
 #define expect_check(function, parameter, check_function, check_data) \
     _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
-                  cast_to_largest_integral_type(check_data), NULL, 0)
+                  cast_to_largest_integral_type(check_data), NULL, 1)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if the parameter value is part of the provided
  *        array.
@@ -405,7 +413,7 @@ void expect_in_set(#function, #parameter, uintmax_t value_array[]);
     expect_in_set_count(function, parameter, value_array, 1)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if the parameter value is part of the provided
  *        array.
@@ -431,7 +439,7 @@ void expect_in_set_count(#function, #parameter, uintmax_t value_array[], size_t
                    sizeof(value_array) / sizeof((value_array)[0]), count)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if the parameter value is not part of the
  *        provided array.
@@ -452,7 +460,7 @@ void expect_not_in_set(#function, #parameter, uintmax_t value_array[]);
     expect_not_in_set_count(function, parameter, value_array, 1)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if the parameter value is not part of the
  *        provided array.
@@ -480,7 +488,7 @@ void expect_not_in_set_count(#function, #parameter, uintmax_t value_array[], siz
 #endif
 
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check a parameter is inside a numerical range.
  * The check would succeed if minimum <= value <= maximum.
@@ -503,7 +511,7 @@ void expect_in_range(#function, #parameter, uintmax_t minimum, uintmax_t maximum
     expect_in_range_count(function, parameter, minimum, maximum, 1)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to repeatedly check a parameter is inside a
  * numerical range. The check would succeed if minimum <= value <= maximum.
@@ -531,7 +539,7 @@ void expect_in_range_count(#function, #parameter, uintmax_t minimum, uintmax_t m
                      maximum, count)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check a parameter is outside a numerical range.
  * The check would succeed if minimum > value > maximum.
@@ -554,7 +562,7 @@ void expect_not_in_range(#function, #parameter, uintmax_t minimum, uintmax_t max
     expect_not_in_range_count(function, parameter, minimum, maximum, 1)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to repeatedly check a parameter is outside a
  * numerical range. The check would succeed if minimum > value > maximum.
@@ -583,7 +591,7 @@ void expect_not_in_range_count(#function, #parameter, uintmax_t minimum, uintmax
                          minimum, maximum, count)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if a parameter is the given value.
  *
@@ -603,7 +611,7 @@ void expect_value(#function, #parameter, uintmax_t value);
     expect_value_count(function, parameter, value, 1)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to repeatedly check if a parameter is the given value.
  *
@@ -628,7 +636,7 @@ void expect_value_count(#function, #parameter, uintmax_t value, size_t count);
                   cast_to_largest_integral_type(value), count)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if a parameter isn't the given value.
  *
@@ -648,7 +656,7 @@ void expect_not_value(#function, #parameter, uintmax_t value);
     expect_not_value_count(function, parameter, value, 1)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to repeatedly check if a parameter isn't the given value.
  *
@@ -673,7 +681,7 @@ void expect_not_value_count(#function, #parameter, uintmax_t value, size_t count
                       cast_to_largest_integral_type(value), count)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if the parameter value is equal to the
  *        provided string.
@@ -694,7 +702,7 @@ void expect_string(#function, #parameter, const char *string);
     expect_string_count(function, parameter, string, 1)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if the parameter value is equal to the
  *        provided string.
@@ -720,7 +728,7 @@ void expect_string_count(#function, #parameter, const char *string, size_t count
                    (const char*)(string), count)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if the parameter value isn't equal to the
  *        provided string.
@@ -741,7 +749,7 @@ void expect_not_string(#function, #parameter, const char *string);
     expect_not_string_count(function, parameter, string, 1)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if the parameter value isn't equal to the
  *        provided string.
@@ -767,7 +775,7 @@ void expect_not_string_count(#function, #parameter, const char *string, size_t c
                        (const char*)(string), count)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if the parameter does match an area of memory.
  *
@@ -789,7 +797,7 @@ void expect_memory(#function, #parameter, void *memory, size_t size);
     expect_memory_count(function, parameter, memory, size, 1)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to repeatedly check if the parameter does match an area
  *        of memory.
@@ -817,7 +825,7 @@ void expect_memory_count(#function, #parameter, void *memory, size_t size, size_
                    (const void*)(memory), size, count)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if the parameter doesn't match an area of
  *        memory.
@@ -840,7 +848,7 @@ void expect_not_memory(#function, #parameter, void *memory, size_t size);
     expect_not_memory_count(function, parameter, memory, size, 1)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to repeatedly check if the parameter doesn't match an
  *        area of memory.
@@ -869,7 +877,7 @@ void expect_not_memory_count(#function, #parameter, void *memory, size_t size, s
 #endif
 
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to check if a parameter (of any value) has been passed.
  *
@@ -887,7 +895,7 @@ void expect_any(#function, #parameter);
     expect_any_count(function, parameter, 1)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Add an event to repeatedly check if a parameter (of any value) has
  *        been passed.
@@ -910,7 +918,7 @@ void expect_any_count(#function, #parameter, size_t count);
     _expect_any(#function, #parameter, __FILE__, __LINE__, count)
 #endif
 
-#if DOXYGEN
+#ifdef DOXYGEN
 /**
  * @brief Determine whether a function parameter is correct.
  *
@@ -986,6 +994,28 @@ void assert_false(scalar expression);
                                      __FILE__, __LINE__)
 #endif
 
+#ifdef DOXYGEN
+/**
+ * @brief Assert if the return_code is smaller than 0.
+ *
+ * The function prints an error message to standard error and terminates the
+ * test by calling fail() if the return code is smaller than 0. If the function
+ * 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.
+ *
+ * @param[in]  rc       The return code to evaluate.
+ *
+ * @param[in]  error    Pass errno here or 0.
+ */
+void assert_return_code(int rc, int error);
+#else
+#define assert_return_code(rc, error) \
+    _assert_return_code(cast_to_largest_integral_type(rc), \
+                        sizeof(rc), \
+                        cast_to_largest_integral_type(error), \
+                        #rc, __FILE__, __LINE__)
+#endif
+
 #ifdef DOXYGEN
 /**
  * @brief Assert that the given pointer is non-NULL.
@@ -1296,6 +1326,10 @@ int run_test(#function);
 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
 #endif
 
+static inline void _unit_test_dummy(void **state) {
+    (void)state;
+}
+
 /** Initializes a UnitTest structure. */
 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
 
@@ -1305,17 +1339,24 @@ int run_test(#function);
 /** Initializes a UnitTest structure with a setup function. */
 #define unit_test_setup(test, setup) \
     _unit_test_setup(test, setup), \
-    unit_test(test)
+    unit_test(test), \
+    _unit_test_teardown(test, _unit_test_dummy)
 
 #define _unit_test_teardown(test, teardown) \
     { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
 
 /** Initializes a UnitTest structure with a teardown function. */
 #define unit_test_teardown(test, teardown) \
-    _unit_test_setup(test, setup), \
+    _unit_test_setup(test, _unit_test_dummy), \
     unit_test(test), \
     _unit_test_teardown(test, teardown)
 
+#define group_test_setup(setup) \
+    { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }
+
+#define group_test_teardown(teardown) \
+    { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }
+
 /**
  * Initialize an array of UnitTest structures with a setup function for a test
  * and a teardown function.  Either setup or teardown can be NULL.
@@ -1376,6 +1417,8 @@ int run_tests(const UnitTest tests[]);
 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
 #endif
 
+#define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof(tests)[0])
+
 /** @} */
 
 /**
@@ -1410,7 +1453,7 @@ int run_tests(const UnitTest tests[]);
  * @return A pointer to the allocated memory or NULL on error.
  *
  * @code
- * #if UNIT_TESTING
+ * #ifdef UNIT_TESTING
  * extern void* _test_malloc(const size_t size, const char* file, const int line);
  *
  * #define malloc(size) _test_malloc(size, __FILE__, __LINE__)
@@ -1462,7 +1505,7 @@ void test_free(void *ptr);
 #endif
 
 /* Redirect malloc, calloc and free to the unit test allocators. */
-#if UNIT_TESTING
+#ifdef UNIT_TESTING
 #define malloc test_malloc
 #define calloc test_calloc
 #define free test_free
@@ -1503,7 +1546,7 @@ void test_free(void *ptr);
  * @param[in]  line  The line mock_assert() is called.
  *
  * @code
- * #if UNIT_TESTING
+ * #ifdef UNIT_TESTING
  * extern void mock_assert(const int result, const char* const expression,
  *                         const char * const file, const int line);
  *
@@ -1580,6 +1623,8 @@ typedef enum UnitTestFunctionType {
     UNIT_TEST_FUNCTION_TYPE_TEST = 0,
     UNIT_TEST_FUNCTION_TYPE_SETUP,
     UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
+    UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP,
+    UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN,
 } UnitTestFunctionType;
 
 /*
@@ -1593,6 +1638,12 @@ typedef struct UnitTest {
     UnitTestFunctionType function_type;
 } UnitTest;
 
+typedef struct GroupTest {
+    UnitTestFunction setup;
+    UnitTestFunction teardown;
+    const UnitTest *tests;
+    const size_t number_of_tests;
+} GroupTest;
 
 /* Location within some source code. */
 typedef struct SourceLocation {
@@ -1685,6 +1736,12 @@ void _will_return(const char * const function_name, const char * const file,
 void _assert_true(const LargestIntegralType result,
                   const char* const expression,
                   const char * const file, const int line);
+void _assert_return_code(const LargestIntegralType result,
+                         size_t rlen,
+                         const LargestIntegralType error,
+                         const char * const expression,
+                         const char * const file,
+                         const int line);
 void _assert_int_equal(
     const LargestIntegralType a, const LargestIntegralType b,
     const char * const file, const int line);
@@ -1725,12 +1782,14 @@ int _run_test(
     void ** const volatile state, const UnitTestFunctionType function_type,
     const void* const heap_check_point);
 int _run_tests(const UnitTest * const tests, const size_t number_of_tests);
+int _run_group_tests(const UnitTest * const tests,
+                     const size_t number_of_tests);
 
 /* Standard output and error print methods. */
-void print_message(const char* const format, ...) PRINTF_ATTRIBUTE(1, 2);
-void print_error(const char* const format, ...) PRINTF_ATTRIBUTE(1, 2);
-void vprint_message(const char* const format, va_list args) PRINTF_ATTRIBUTE(1, 0);
-void vprint_error(const char* const format, va_list args) PRINTF_ATTRIBUTE(1, 0);
+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 vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
+void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
 
 /** @} */