cmocka
1.1.1
|
00001 /* 00002 * Copyright 2008 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef CMOCKA_H_ 00017 #define CMOCKA_H_ 00018 00019 #ifdef _WIN32 00020 # ifdef _MSC_VER 00021 00022 #define __func__ __FUNCTION__ 00023 00024 # ifndef inline 00025 #define inline __inline 00026 # endif /* inline */ 00027 00028 # if _MSC_VER < 1500 00029 # ifdef __cplusplus 00030 extern "C" { 00031 # endif /* __cplusplus */ 00032 int __stdcall IsDebuggerPresent(); 00033 # ifdef __cplusplus 00034 } /* extern "C" */ 00035 # endif /* __cplusplus */ 00036 # endif /* _MSC_VER < 1500 */ 00037 # endif /* _MSC_VER */ 00038 #endif /* _WIN32 */ 00039 00057 /* If __WORDSIZE is not set, try to figure it out and default to 32 bit. */ 00058 #ifndef __WORDSIZE 00059 # if defined(__x86_64__) && !defined(__ILP32__) 00060 # define __WORDSIZE 64 00061 # else 00062 # define __WORDSIZE 32 00063 # endif 00064 #endif 00065 00066 #ifdef DOXYGEN 00067 00071 typedef uintmax_t LargestIntegralType; 00072 #else /* DOXGEN */ 00073 #ifndef LargestIntegralType 00074 # if __WORDSIZE == 64 && !defined(_WIN64) 00075 # define LargestIntegralType unsigned long int 00076 # else 00077 # define LargestIntegralType unsigned long long int 00078 # endif 00079 #endif /* LargestIntegralType */ 00080 #endif /* DOXYGEN */ 00081 00082 /* Printf format used to display LargestIntegralType as a hexidecimal. */ 00083 #ifndef LargestIntegralTypePrintfFormat 00084 # ifdef _WIN32 00085 # define LargestIntegralTypePrintfFormat "0x%I64x" 00086 # else 00087 # if __WORDSIZE == 64 00088 # define LargestIntegralTypePrintfFormat "%#lx" 00089 # else 00090 # define LargestIntegralTypePrintfFormat "%#llx" 00091 # endif 00092 # endif /* _WIN32 */ 00093 #endif /* LargestIntegralTypePrintfFormat */ 00094 00095 /* Printf format used to display LargestIntegralType as a decimal. */ 00096 #ifndef LargestIntegralTypePrintfFormatDecimal 00097 # ifdef _WIN32 00098 # define LargestIntegralTypePrintfFormatDecimal "%I64u" 00099 # else 00100 # if __WORDSIZE == 64 00101 # define LargestIntegralTypePrintfFormatDecimal "%lu" 00102 # else 00103 # define LargestIntegralTypePrintfFormatDecimal "%llu" 00104 # endif 00105 # endif /* _WIN32 */ 00106 #endif /* LargestIntegralTypePrintfFormat */ 00107 00108 /* Perform an unsigned cast to LargestIntegralType. */ 00109 #define cast_to_largest_integral_type(value) \ 00110 ((LargestIntegralType)(value)) 00111 00112 /* Smallest integral type capable of holding a pointer. */ 00113 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED) 00114 # if defined(_WIN32) 00115 /* WIN32 is an ILP32 platform */ 00116 typedef unsigned int uintptr_t; 00117 # elif defined(_WIN64) 00118 typedef unsigned long int uintptr_t 00119 # else /* _WIN32 */ 00120 00121 /* ILP32 and LP64 platforms */ 00122 # ifdef __WORDSIZE /* glibc */ 00123 # if __WORDSIZE == 64 00124 typedef unsigned long int uintptr_t; 00125 # else 00126 typedef unsigned int uintptr_t; 00127 # endif /* __WORDSIZE == 64 */ 00128 # else /* __WORDSIZE */ 00129 # if defined(_LP64) || defined(_I32LPx) 00130 typedef unsigned long int uintptr_t; 00131 # else 00132 typedef unsigned int uintptr_t; 00133 # endif 00134 # endif /* __WORDSIZE */ 00135 # endif /* _WIN32 */ 00136 00137 # define _UINTPTR_T 00138 # define _UINTPTR_T_DEFINED 00139 #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */ 00140 00141 /* Perform an unsigned cast to uintptr_t. */ 00142 #define cast_to_pointer_integral_type(value) \ 00143 ((uintptr_t)((size_t)(value))) 00144 00145 /* Perform a cast of a pointer to LargestIntegralType */ 00146 #define cast_ptr_to_largest_integral_type(value) \ 00147 cast_to_largest_integral_type(cast_to_pointer_integral_type(value)) 00148 00149 /* GCC have printf type attribute check. */ 00150 #ifdef __GNUC__ 00151 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) \ 00152 __attribute__ ((__format__ (__printf__, a, b))) 00153 #else 00154 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) 00155 #endif /* __GNUC__ */ 00156 00157 #if defined(__GNUC__) 00158 #define CMOCKA_DEPRECATED __attribute__ ((deprecated)) 00159 #elif defined(_MSC_VER) 00160 #define CMOCKA_DEPRECATED __declspec(deprecated) 00161 #else 00162 #define CMOCKA_DEPRECATED 00163 #endif 00164 00165 #define WILL_RETURN_ALWAYS -1 00166 #define WILL_RETURN_ONCE -2 00167 00216 #ifdef DOXYGEN 00217 00224 LargestIntegralType mock(void); 00225 #else 00226 #define mock() _mock(__func__, __FILE__, __LINE__) 00227 #endif 00228 00229 #ifdef DOXYGEN 00230 00250 #type mock_type(#type); 00251 #else 00252 #define mock_type(type) ((type) mock()) 00253 #endif 00254 00255 #ifdef DOXYGEN 00256 00277 type mock_ptr_type(#type); 00278 #else 00279 #define mock_ptr_type(type) ((type) (uintptr_t) mock()) 00280 #endif 00281 00282 00283 #ifdef DOXYGEN 00284 00308 void will_return(#function, LargestIntegralType value); 00309 #else 00310 #define will_return(function, value) \ 00311 _will_return(#function, __FILE__, __LINE__, \ 00312 cast_to_largest_integral_type(value), 1) 00313 #endif 00314 00315 #ifdef DOXYGEN 00316 00331 void will_return_count(#function, LargestIntegralType value, int count); 00332 #else 00333 #define will_return_count(function, value, count) \ 00334 _will_return(#function, __FILE__, __LINE__, \ 00335 cast_to_largest_integral_type(value), count) 00336 #endif 00337 00338 #ifdef DOXYGEN 00339 00354 void will_return_always(#function, LargestIntegralType value); 00355 #else 00356 #define will_return_always(function, value) \ 00357 will_return_count(function, (value), WILL_RETURN_ALWAYS) 00358 #endif 00359 00360 #ifdef DOXYGEN 00361 00382 void will_return_maybe(#function, LargestIntegralType value); 00383 #else 00384 #define will_return_maybe(function, value) \ 00385 will_return_count(function, (value), WILL_RETURN_ONCE) 00386 #endif 00387 00433 /* 00434 * Add a custom parameter checking function. If the event parameter is NULL 00435 * the event structure is allocated internally by this function. If event 00436 * parameter is provided it must be allocated on the heap and doesn't need to 00437 * be deallocated by the caller. 00438 */ 00439 #ifdef DOXYGEN 00440 00456 void expect_check(#function, #parameter, #check_function, const void *check_data); 00457 #else 00458 #define expect_check(function, parameter, check_function, check_data) \ 00459 _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \ 00460 cast_to_largest_integral_type(check_data), NULL, 1) 00461 #endif 00462 00463 #ifdef DOXYGEN 00464 00478 void expect_in_set(#function, #parameter, LargestIntegralType value_array[]); 00479 #else 00480 #define expect_in_set(function, parameter, value_array) \ 00481 expect_in_set_count(function, parameter, value_array, 1) 00482 #endif 00483 00484 #ifdef DOXYGEN 00485 00503 void expect_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count); 00504 #else 00505 #define expect_in_set_count(function, parameter, value_array, count) \ 00506 _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \ 00507 sizeof(value_array) / sizeof((value_array)[0]), count) 00508 #endif 00509 00510 #ifdef DOXYGEN 00511 00525 void expect_not_in_set(#function, #parameter, LargestIntegralType value_array[]); 00526 #else 00527 #define expect_not_in_set(function, parameter, value_array) \ 00528 expect_not_in_set_count(function, parameter, value_array, 1) 00529 #endif 00530 00531 #ifdef DOXYGEN 00532 00550 void expect_not_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count); 00551 #else 00552 #define expect_not_in_set_count(function, parameter, value_array, count) \ 00553 _expect_not_in_set( \ 00554 #function, #parameter, __FILE__, __LINE__, value_array, \ 00555 sizeof(value_array) / sizeof((value_array)[0]), count) 00556 #endif 00557 00558 00559 #ifdef DOXYGEN 00560 00576 void expect_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum); 00577 #else 00578 #define expect_in_range(function, parameter, minimum, maximum) \ 00579 expect_in_range_count(function, parameter, minimum, maximum, 1) 00580 #endif 00581 00582 #ifdef DOXYGEN 00583 00603 void expect_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count); 00604 #else 00605 #define expect_in_range_count(function, parameter, minimum, maximum, count) \ 00606 _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \ 00607 maximum, count) 00608 #endif 00609 00610 #ifdef DOXYGEN 00611 00627 void expect_not_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum); 00628 #else 00629 #define expect_not_in_range(function, parameter, minimum, maximum) \ 00630 expect_not_in_range_count(function, parameter, minimum, maximum, 1) 00631 #endif 00632 00633 #ifdef DOXYGEN 00634 00654 void expect_not_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count); 00655 #else 00656 #define expect_not_in_range_count(function, parameter, minimum, maximum, \ 00657 count) \ 00658 _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \ 00659 minimum, maximum, count) 00660 #endif 00661 00662 #ifdef DOXYGEN 00663 00676 void expect_value(#function, #parameter, LargestIntegralType value); 00677 #else 00678 #define expect_value(function, parameter, value) \ 00679 expect_value_count(function, parameter, value, 1) 00680 #endif 00681 00682 #ifdef DOXYGEN 00683 00700 void expect_value_count(#function, #parameter, LargestIntegralType value, size_t count); 00701 #else 00702 #define expect_value_count(function, parameter, value, count) \ 00703 _expect_value(#function, #parameter, __FILE__, __LINE__, \ 00704 cast_to_largest_integral_type(value), count) 00705 #endif 00706 00707 #ifdef DOXYGEN 00708 00721 void expect_not_value(#function, #parameter, LargestIntegralType value); 00722 #else 00723 #define expect_not_value(function, parameter, value) \ 00724 expect_not_value_count(function, parameter, value, 1) 00725 #endif 00726 00727 #ifdef DOXYGEN 00728 00745 void expect_not_value_count(#function, #parameter, LargestIntegralType value, size_t count); 00746 #else 00747 #define expect_not_value_count(function, parameter, value, count) \ 00748 _expect_not_value(#function, #parameter, __FILE__, __LINE__, \ 00749 cast_to_largest_integral_type(value), count) 00750 #endif 00751 00752 #ifdef DOXYGEN 00753 00767 void expect_string(#function, #parameter, const char *string); 00768 #else 00769 #define expect_string(function, parameter, string) \ 00770 expect_string_count(function, parameter, string, 1) 00771 #endif 00772 00773 #ifdef DOXYGEN 00774 00792 void expect_string_count(#function, #parameter, const char *string, size_t count); 00793 #else 00794 #define expect_string_count(function, parameter, string, count) \ 00795 _expect_string(#function, #parameter, __FILE__, __LINE__, \ 00796 (const char*)(string), count) 00797 #endif 00798 00799 #ifdef DOXYGEN 00800 00814 void expect_not_string(#function, #parameter, const char *string); 00815 #else 00816 #define expect_not_string(function, parameter, string) \ 00817 expect_not_string_count(function, parameter, string, 1) 00818 #endif 00819 00820 #ifdef DOXYGEN 00821 00839 void expect_not_string_count(#function, #parameter, const char *string, size_t count); 00840 #else 00841 #define expect_not_string_count(function, parameter, string, count) \ 00842 _expect_not_string(#function, #parameter, __FILE__, __LINE__, \ 00843 (const char*)(string), count) 00844 #endif 00845 00846 #ifdef DOXYGEN 00847 00862 void expect_memory(#function, #parameter, void *memory, size_t size); 00863 #else 00864 #define expect_memory(function, parameter, memory, size) \ 00865 expect_memory_count(function, parameter, memory, size, 1) 00866 #endif 00867 00868 #ifdef DOXYGEN 00869 00889 void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count); 00890 #else 00891 #define expect_memory_count(function, parameter, memory, size, count) \ 00892 _expect_memory(#function, #parameter, __FILE__, __LINE__, \ 00893 (const void*)(memory), size, count) 00894 #endif 00895 00896 #ifdef DOXYGEN 00897 00913 void expect_not_memory(#function, #parameter, void *memory, size_t size); 00914 #else 00915 #define expect_not_memory(function, parameter, memory, size) \ 00916 expect_not_memory_count(function, parameter, memory, size, 1) 00917 #endif 00918 00919 #ifdef DOXYGEN 00920 00940 void expect_not_memory_count(#function, #parameter, void *memory, size_t size, size_t count); 00941 #else 00942 #define expect_not_memory_count(function, parameter, memory, size, count) \ 00943 _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \ 00944 (const void*)(memory), size, count) 00945 #endif 00946 00947 00948 #ifdef DOXYGEN 00949 00960 void expect_any(#function, #parameter); 00961 #else 00962 #define expect_any(function, parameter) \ 00963 expect_any_count(function, parameter, 1) 00964 #endif 00965 00966 #ifdef DOXYGEN 00967 00983 void expect_any_count(#function, #parameter, size_t count); 00984 #else 00985 #define expect_any_count(function, parameter, count) \ 00986 _expect_any(#function, #parameter, __FILE__, __LINE__, count) 00987 #endif 00988 00989 #ifdef DOXYGEN 00990 01000 void check_expected(#parameter); 01001 #else 01002 #define check_expected(parameter) \ 01003 _check_expected(__func__, #parameter, __FILE__, __LINE__, \ 01004 cast_to_largest_integral_type(parameter)) 01005 #endif 01006 01007 #ifdef DOXYGEN 01008 01018 void check_expected_ptr(#parameter); 01019 #else 01020 #define check_expected_ptr(parameter) \ 01021 _check_expected(__func__, #parameter, __FILE__, __LINE__, \ 01022 cast_ptr_to_largest_integral_type(parameter)) 01023 #endif 01024 01046 #ifdef DOXYGEN 01047 01059 void assert_true(scalar expression); 01060 #else 01061 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \ 01062 __FILE__, __LINE__) 01063 #endif 01064 01065 #ifdef DOXYGEN 01066 01077 void assert_false(scalar expression); 01078 #else 01079 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \ 01080 __FILE__, __LINE__) 01081 #endif 01082 01083 #ifdef DOXYGEN 01084 01096 void assert_return_code(int rc, int error); 01097 #else 01098 #define assert_return_code(rc, error) \ 01099 _assert_return_code(cast_to_largest_integral_type(rc), \ 01100 sizeof(rc), \ 01101 cast_to_largest_integral_type(error), \ 01102 #rc, __FILE__, __LINE__) 01103 #endif 01104 01105 #ifdef DOXYGEN 01106 01116 void assert_non_null(void *pointer); 01117 #else 01118 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \ 01119 __FILE__, __LINE__) 01120 #endif 01121 01122 #ifdef DOXYGEN 01123 01133 void assert_null(void *pointer); 01134 #else 01135 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \ 01136 __FILE__, __LINE__) 01137 #endif 01138 01139 #ifdef DOXYGEN 01140 01150 void assert_ptr_equal(void *a, void *b); 01151 #else 01152 #define assert_ptr_equal(a, b) \ 01153 _assert_int_equal(cast_ptr_to_largest_integral_type(a), \ 01154 cast_ptr_to_largest_integral_type(b), \ 01155 __FILE__, __LINE__) 01156 #endif 01157 01158 #ifdef DOXYGEN 01159 01169 void assert_ptr_not_equal(void *a, void *b); 01170 #else 01171 #define assert_ptr_not_equal(a, b) \ 01172 _assert_int_not_equal(cast_ptr_to_largest_integral_type(a), \ 01173 cast_ptr_to_largest_integral_type(b), \ 01174 __FILE__, __LINE__) 01175 #endif 01176 01177 #ifdef DOXYGEN 01178 01188 void assert_int_equal(int a, int b); 01189 #else 01190 #define assert_int_equal(a, b) \ 01191 _assert_int_equal(cast_to_largest_integral_type(a), \ 01192 cast_to_largest_integral_type(b), \ 01193 __FILE__, __LINE__) 01194 #endif 01195 01196 #ifdef DOXYGEN 01197 01209 void assert_int_not_equal(int a, int b); 01210 #else 01211 #define assert_int_not_equal(a, b) \ 01212 _assert_int_not_equal(cast_to_largest_integral_type(a), \ 01213 cast_to_largest_integral_type(b), \ 01214 __FILE__, __LINE__) 01215 #endif 01216 01217 #ifdef DOXYGEN 01218 01228 void assert_string_equal(const char *a, const char *b); 01229 #else 01230 #define assert_string_equal(a, b) \ 01231 _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \ 01232 __LINE__) 01233 #endif 01234 01235 #ifdef DOXYGEN 01236 01246 void assert_string_not_equal(const char *a, const char *b); 01247 #else 01248 #define assert_string_not_equal(a, b) \ 01249 _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \ 01250 __LINE__) 01251 #endif 01252 01253 #ifdef DOXYGEN 01254 01268 void assert_memory_equal(const void *a, const void *b, size_t size); 01269 #else 01270 #define assert_memory_equal(a, b, size) \ 01271 _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \ 01272 __LINE__) 01273 #endif 01274 01275 #ifdef DOXYGEN 01276 01290 void assert_memory_not_equal(const void *a, const void *b, size_t size); 01291 #else 01292 #define assert_memory_not_equal(a, b, size) \ 01293 _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \ 01294 __FILE__, __LINE__) 01295 #endif 01296 01297 #ifdef DOXYGEN 01298 01311 void assert_in_range(LargestIntegralType value, LargestIntegralType minimum, LargestIntegralType maximum); 01312 #else 01313 #define assert_in_range(value, minimum, maximum) \ 01314 _assert_in_range( \ 01315 cast_to_largest_integral_type(value), \ 01316 cast_to_largest_integral_type(minimum), \ 01317 cast_to_largest_integral_type(maximum), __FILE__, __LINE__) 01318 #endif 01319 01320 #ifdef DOXYGEN 01321 01334 void assert_not_in_range(LargestIntegralType value, LargestIntegralType minimum, LargestIntegralType maximum); 01335 #else 01336 #define assert_not_in_range(value, minimum, maximum) \ 01337 _assert_not_in_range( \ 01338 cast_to_largest_integral_type(value), \ 01339 cast_to_largest_integral_type(minimum), \ 01340 cast_to_largest_integral_type(maximum), __FILE__, __LINE__) 01341 #endif 01342 01343 #ifdef DOXYGEN 01344 01356 void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count); 01357 #else 01358 #define assert_in_set(value, values, number_of_values) \ 01359 _assert_in_set(value, values, number_of_values, __FILE__, __LINE__) 01360 #endif 01361 01362 #ifdef DOXYGEN 01363 01375 void assert_not_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count); 01376 #else 01377 #define assert_not_in_set(value, values, number_of_values) \ 01378 _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__) 01379 #endif 01380 01441 #ifdef DOXYGEN 01442 01448 void function_called(void); 01449 #else 01450 #define function_called() _function_called(__func__, __FILE__, __LINE__) 01451 #endif 01452 01453 #ifdef DOXYGEN 01454 01464 void expect_function_calls(#function, const int times); 01465 #else 01466 #define expect_function_calls(function, times) \ 01467 _expect_function_call(#function, __FILE__, __LINE__, times) 01468 #endif 01469 01470 #ifdef DOXYGEN 01471 01479 void expect_function_call(#function); 01480 #else 01481 #define expect_function_call(function) \ 01482 _expect_function_call(#function, __FILE__, __LINE__, 1) 01483 #endif 01484 01485 #ifdef DOXYGEN 01486 01493 void expect_function_call_any(#function); 01494 #else 01495 #define expect_function_call_any(function) \ 01496 _expect_function_call(#function, __FILE__, __LINE__, -1) 01497 #endif 01498 01499 #ifdef DOXYGEN 01500 01507 void ignore_function_calls(#function); 01508 #else 01509 #define ignore_function_calls(function) \ 01510 _expect_function_call(#function, __FILE__, __LINE__, -2) 01511 #endif 01512 01541 #ifdef DOXYGEN 01542 01545 void fail(void); 01546 #else 01547 #define fail() _fail(__FILE__, __LINE__) 01548 #endif 01549 01550 #ifdef DOXYGEN 01551 01554 void skip(void); 01555 #else 01556 #define skip() _skip(__FILE__, __LINE__) 01557 #endif 01558 01559 #ifdef DOXYGEN 01560 01574 void fail_msg(const char *msg, ...); 01575 #else 01576 #define fail_msg(msg, ...) do { \ 01577 print_error("ERROR: " msg "\n", ##__VA_ARGS__); \ 01578 fail(); \ 01579 } while (0) 01580 #endif 01581 01582 #ifdef DOXYGEN 01583 01602 int run_test(#function); 01603 #else 01604 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL) 01605 #endif 01606 01607 static inline void _unit_test_dummy(void **state) { 01608 (void)state; 01609 } 01610 01615 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST } 01616 01617 #define _unit_test_setup(test, setup) \ 01618 { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP } 01619 01624 #define unit_test_setup(test, setup) \ 01625 _unit_test_setup(test, setup), \ 01626 unit_test(test), \ 01627 _unit_test_teardown(test, _unit_test_dummy) 01628 01629 #define _unit_test_teardown(test, teardown) \ 01630 { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN } 01631 01636 #define unit_test_teardown(test, teardown) \ 01637 _unit_test_setup(test, _unit_test_dummy), \ 01638 unit_test(test), \ 01639 _unit_test_teardown(test, teardown) 01640 01645 #define group_test_setup(setup) \ 01646 { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP } 01647 01652 #define group_test_teardown(teardown) \ 01653 { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN } 01654 01662 #define unit_test_setup_teardown(test, setup, teardown) \ 01663 _unit_test_setup(test, setup), \ 01664 unit_test(test), \ 01665 _unit_test_teardown(test, teardown) 01666 01667 01669 #define cmocka_unit_test(f) { #f, f, NULL, NULL, NULL } 01670 01672 #define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL, NULL } 01673 01675 #define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown, NULL } 01676 01681 #define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown, NULL } 01682 01690 #define cmocka_unit_test_prestate(f, state) { #f, f, NULL, NULL, state } 01691 01699 #define cmocka_unit_test_prestate_setup_teardown(f, setup, teardown, state) { #f, f, setup, teardown, state } 01700 01701 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0]) 01702 #define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof(tests)[0]) 01703 01704 #ifdef DOXYGEN 01705 01761 int cmocka_run_group_tests(const struct CMUnitTest group_tests[], 01762 CMFixtureFunction group_setup, 01763 CMFixtureFunction group_teardown); 01764 #else 01765 # define cmocka_run_group_tests(group_tests, group_setup, group_teardown) \ 01766 _cmocka_run_group_tests(#group_tests, group_tests, sizeof(group_tests) / sizeof(group_tests)[0], group_setup, group_teardown) 01767 #endif 01768 01769 #ifdef DOXYGEN 01770 01829 int cmocka_run_group_tests_name(const char *group_name, 01830 const struct CMUnitTest group_tests[], 01831 CMFixtureFunction group_setup, 01832 CMFixtureFunction group_teardown); 01833 #else 01834 # define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown) \ 01835 _cmocka_run_group_tests(group_name, group_tests, sizeof(group_tests) / sizeof(group_tests)[0], group_setup, group_teardown) 01836 #endif 01837 01863 #ifdef DOXYGEN 01864 01886 void *test_malloc(size_t size); 01887 #else 01888 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__) 01889 #endif 01890 01891 #ifdef DOXYGEN 01892 01905 void *test_calloc(size_t nmemb, size_t size); 01906 #else 01907 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) 01908 #endif 01909 01910 #ifdef DOXYGEN 01911 01921 void *test_realloc(void *ptr, size_t size); 01922 #else 01923 #define test_realloc(ptr, size) _test_realloc(ptr, size, __FILE__, __LINE__) 01924 #endif 01925 01926 #ifdef DOXYGEN 01927 01934 void test_free(void *ptr); 01935 #else 01936 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__) 01937 #endif 01938 01939 /* Redirect malloc, calloc and free to the unit test allocators. */ 01940 #ifdef UNIT_TESTING 01941 #define malloc test_malloc 01942 #define realloc test_realloc 01943 #define calloc test_calloc 01944 #define free test_free 01945 #endif /* UNIT_TESTING */ 01946 02000 void mock_assert(const int result, const char* const expression, 02001 const char * const file, const int line); 02002 02003 #ifdef DOXYGEN 02004 02026 void expect_assert_failure(function fn_call); 02027 #else 02028 #define expect_assert_failure(function_call) \ 02029 { \ 02030 const int result = setjmp(global_expect_assert_env); \ 02031 global_expecting_assert = 1; \ 02032 if (result) { \ 02033 print_message("Expected assertion %s occurred\n", \ 02034 global_last_failed_assert); \ 02035 global_expecting_assert = 0; \ 02036 } else { \ 02037 function_call ; \ 02038 global_expecting_assert = 0; \ 02039 print_error("Expected assert in %s\n", #function_call); \ 02040 _fail(__FILE__, __LINE__); \ 02041 } \ 02042 } 02043 #endif 02044 02047 /* Function prototype for setup, test and teardown functions. */ 02048 typedef void (*UnitTestFunction)(void **state); 02049 02050 /* Function that determines whether a function parameter value is correct. */ 02051 typedef int (*CheckParameterValue)(const LargestIntegralType value, 02052 const LargestIntegralType check_value_data); 02053 02054 /* Type of the unit test function. */ 02055 typedef enum UnitTestFunctionType { 02056 UNIT_TEST_FUNCTION_TYPE_TEST = 0, 02057 UNIT_TEST_FUNCTION_TYPE_SETUP, 02058 UNIT_TEST_FUNCTION_TYPE_TEARDOWN, 02059 UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP, 02060 UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN, 02061 } UnitTestFunctionType; 02062 02063 /* 02064 * Stores a unit test function with its name and type. 02065 * NOTE: Every setup function must be paired with a teardown function. It's 02066 * possible to specify NULL function pointers. 02067 */ 02068 typedef struct UnitTest { 02069 const char* name; 02070 UnitTestFunction function; 02071 UnitTestFunctionType function_type; 02072 } UnitTest; 02073 02074 typedef struct GroupTest { 02075 UnitTestFunction setup; 02076 UnitTestFunction teardown; 02077 const UnitTest *tests; 02078 const size_t number_of_tests; 02079 } GroupTest; 02080 02081 /* Function prototype for test functions. */ 02082 typedef void (*CMUnitTestFunction)(void **state); 02083 02084 /* Function prototype for setup and teardown functions. */ 02085 typedef int (*CMFixtureFunction)(void **state); 02086 02087 struct CMUnitTest { 02088 const char *name; 02089 CMUnitTestFunction test_func; 02090 CMFixtureFunction setup_func; 02091 CMFixtureFunction teardown_func; 02092 void *initial_state; 02093 }; 02094 02095 /* Location within some source code. */ 02096 typedef struct SourceLocation { 02097 const char* file; 02098 int line; 02099 } SourceLocation; 02100 02101 /* Event that's called to check a parameter value. */ 02102 typedef struct CheckParameterEvent { 02103 SourceLocation location; 02104 const char *parameter_name; 02105 CheckParameterValue check_value; 02106 LargestIntegralType check_value_data; 02107 } CheckParameterEvent; 02108 02109 /* Used by expect_assert_failure() and mock_assert(). */ 02110 extern int global_expecting_assert; 02111 extern jmp_buf global_expect_assert_env; 02112 extern const char * global_last_failed_assert; 02113 02114 /* Retrieves a value for the given function, as set by "will_return". */ 02115 LargestIntegralType _mock(const char * const function, const char* const file, 02116 const int line); 02117 02118 void _expect_function_call( 02119 const char * const function_name, 02120 const char * const file, 02121 const int line, 02122 const int count); 02123 02124 void _function_called(const char * const function, const char* const file, 02125 const int line); 02126 02127 void _expect_check( 02128 const char* const function, const char* const parameter, 02129 const char* const file, const int line, 02130 const CheckParameterValue check_function, 02131 const LargestIntegralType check_data, CheckParameterEvent * const event, 02132 const int count); 02133 02134 void _expect_in_set( 02135 const char* const function, const char* const parameter, 02136 const char* const file, const int line, const LargestIntegralType values[], 02137 const size_t number_of_values, const int count); 02138 void _expect_not_in_set( 02139 const char* const function, const char* const parameter, 02140 const char* const file, const int line, const LargestIntegralType values[], 02141 const size_t number_of_values, const int count); 02142 02143 void _expect_in_range( 02144 const char* const function, const char* const parameter, 02145 const char* const file, const int line, 02146 const LargestIntegralType minimum, 02147 const LargestIntegralType maximum, const int count); 02148 void _expect_not_in_range( 02149 const char* const function, const char* const parameter, 02150 const char* const file, const int line, 02151 const LargestIntegralType minimum, 02152 const LargestIntegralType maximum, const int count); 02153 02154 void _expect_value( 02155 const char* const function, const char* const parameter, 02156 const char* const file, const int line, const LargestIntegralType value, 02157 const int count); 02158 void _expect_not_value( 02159 const char* const function, const char* const parameter, 02160 const char* const file, const int line, const LargestIntegralType value, 02161 const int count); 02162 02163 void _expect_string( 02164 const char* const function, const char* const parameter, 02165 const char* const file, const int line, const char* string, 02166 const int count); 02167 void _expect_not_string( 02168 const char* const function, const char* const parameter, 02169 const char* const file, const int line, const char* string, 02170 const int count); 02171 02172 void _expect_memory( 02173 const char* const function, const char* const parameter, 02174 const char* const file, const int line, const void* const memory, 02175 const size_t size, const int count); 02176 void _expect_not_memory( 02177 const char* const function, const char* const parameter, 02178 const char* const file, const int line, const void* const memory, 02179 const size_t size, const int count); 02180 02181 void _expect_any( 02182 const char* const function, const char* const parameter, 02183 const char* const file, const int line, const int count); 02184 02185 void _check_expected( 02186 const char * const function_name, const char * const parameter_name, 02187 const char* file, const int line, const LargestIntegralType value); 02188 02189 void _will_return(const char * const function_name, const char * const file, 02190 const int line, const LargestIntegralType value, 02191 const int count); 02192 void _assert_true(const LargestIntegralType result, 02193 const char* const expression, 02194 const char * const file, const int line); 02195 void _assert_return_code(const LargestIntegralType result, 02196 size_t rlen, 02197 const LargestIntegralType error, 02198 const char * const expression, 02199 const char * const file, 02200 const int line); 02201 void _assert_int_equal( 02202 const LargestIntegralType a, const LargestIntegralType b, 02203 const char * const file, const int line); 02204 void _assert_int_not_equal( 02205 const LargestIntegralType a, const LargestIntegralType b, 02206 const char * const file, const int line); 02207 void _assert_string_equal(const char * const a, const char * const b, 02208 const char * const file, const int line); 02209 void _assert_string_not_equal(const char * const a, const char * const b, 02210 const char *file, const int line); 02211 void _assert_memory_equal(const void * const a, const void * const b, 02212 const size_t size, const char* const file, 02213 const int line); 02214 void _assert_memory_not_equal(const void * const a, const void * const b, 02215 const size_t size, const char* const file, 02216 const int line); 02217 void _assert_in_range( 02218 const LargestIntegralType value, const LargestIntegralType minimum, 02219 const LargestIntegralType maximum, const char* const file, const int line); 02220 void _assert_not_in_range( 02221 const LargestIntegralType value, const LargestIntegralType minimum, 02222 const LargestIntegralType maximum, const char* const file, const int line); 02223 void _assert_in_set( 02224 const LargestIntegralType value, const LargestIntegralType values[], 02225 const size_t number_of_values, const char* const file, const int line); 02226 void _assert_not_in_set( 02227 const LargestIntegralType value, const LargestIntegralType values[], 02228 const size_t number_of_values, const char* const file, const int line); 02229 02230 void* _test_malloc(const size_t size, const char* file, const int line); 02231 void* _test_realloc(void *ptr, const size_t size, const char* file, const int line); 02232 void* _test_calloc(const size_t number_of_elements, const size_t size, 02233 const char* file, const int line); 02234 void _test_free(void* const ptr, const char* file, const int line); 02235 02236 void _fail(const char * const file, const int line); 02237 02238 void _skip(const char * const file, const int line); 02239 02240 int _run_test( 02241 const char * const function_name, const UnitTestFunction Function, 02242 void ** const volatile state, const UnitTestFunctionType function_type, 02243 const void* const heap_check_point); 02244 CMOCKA_DEPRECATED int _run_tests(const UnitTest * const tests, 02245 const size_t number_of_tests); 02246 CMOCKA_DEPRECATED int _run_group_tests(const UnitTest * const tests, 02247 const size_t number_of_tests); 02248 02249 /* Test runner */ 02250 int _cmocka_run_group_tests(const char *group_name, 02251 const struct CMUnitTest * const tests, 02252 const size_t num_tests, 02253 CMFixtureFunction group_setup, 02254 CMFixtureFunction group_teardown); 02255 02256 /* Standard output and error print methods. */ 02257 void print_message(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2); 02258 void print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2); 02259 void vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0); 02260 void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0); 02261 02262 enum cm_message_output { 02263 CM_OUTPUT_STDOUT, 02264 CM_OUTPUT_SUBUNIT, 02265 CM_OUTPUT_TAP, 02266 CM_OUTPUT_XML, 02267 }; 02268 02280 void cmocka_set_message_output(enum cm_message_output output); 02281 02284 #endif /* CMOCKA_H_ */