cmocka
1.0.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 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. */ 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 /* Perform an unsigned cast to LargestIntegralType. */ 00096 #define cast_to_largest_integral_type(value) \ 00097 ((LargestIntegralType)(value)) 00098 00099 /* Smallest integral type capable of holding a pointer. */ 00100 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED) 00101 # if defined(_WIN32) 00102 /* WIN32 is an ILP32 platform */ 00103 typedef unsigned int uintptr_t; 00104 # elif defined(_WIN64) 00105 typedef unsigned long int uintptr_t 00106 # else /* _WIN32 */ 00107 00108 /* ILP32 and LP64 platforms */ 00109 # ifdef __WORDSIZE /* glibc */ 00110 # if __WORDSIZE == 64 00111 typedef unsigned long int uintptr_t; 00112 # else 00113 typedef unsigned int uintptr_t; 00114 # endif /* __WORDSIZE == 64 */ 00115 # else /* __WORDSIZE */ 00116 # if defined(_LP64) || defined(_I32LPx) 00117 typedef unsigned long int uintptr_t; 00118 # else 00119 typedef unsigned int uintptr_t; 00120 # endif 00121 # endif /* __WORDSIZE */ 00122 # endif /* _WIN32 */ 00123 00124 # define _UINTPTR_T 00125 # define _UINTPTR_T_DEFINED 00126 #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */ 00127 00128 /* Perform an unsigned cast to uintptr_t. */ 00129 #define cast_to_pointer_integral_type(value) \ 00130 ((uintptr_t)((size_t)(value))) 00131 00132 /* Perform a cast of a pointer to LargestIntegralType */ 00133 #define cast_ptr_to_largest_integral_type(value) \ 00134 cast_to_largest_integral_type(cast_to_pointer_integral_type(value)) 00135 00136 /* GCC have printf type attribute check. */ 00137 #ifdef __GNUC__ 00138 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) \ 00139 __attribute__ ((__format__ (__printf__, a, b))) 00140 #else 00141 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) 00142 #endif /* __GNUC__ */ 00143 00144 #if defined(__GNUC__) 00145 #define CMOCKA_DEPRECATED __attribute__ ((deprecated)) 00146 #elif defined(_MSC_VER) 00147 #define CMOCKA_DEPRECATED __declspec(deprecated) 00148 #else 00149 #define CMOCKA_DEPRECATED 00150 #endif 00151 00200 #ifdef DOXYGEN 00201 00208 LargestIntegralType mock(void); 00209 #else 00210 #define mock() _mock(__func__, __FILE__, __LINE__) 00211 #endif 00212 00213 #ifdef DOXYGEN 00214 00234 #type mock_type(#type); 00235 #else 00236 #define mock_type(type) ((type) mock()) 00237 #endif 00238 00239 #ifdef DOXYGEN 00240 00261 type mock_ptr_type(#type); 00262 #else 00263 #define mock_ptr_type(type) ((type) (uintptr_t) mock()) 00264 #endif 00265 00266 00267 #ifdef DOXYGEN 00268 00292 void will_return(#function, LargestIntegralType value); 00293 #else 00294 #define will_return(function, value) \ 00295 _will_return(#function, __FILE__, __LINE__, \ 00296 cast_to_largest_integral_type(value), 1) 00297 #endif 00298 00299 #ifdef DOXYGEN 00300 00313 void will_return_count(#function, LargestIntegralType value, int count); 00314 #else 00315 #define will_return_count(function, value, count) \ 00316 _will_return(#function, __FILE__, __LINE__, \ 00317 cast_to_largest_integral_type(value), count) 00318 #endif 00319 00320 #ifdef DOXYGEN 00321 00336 void will_return_always(#function, LargestIntegralType value); 00337 #else 00338 #define will_return_always(function, value) \ 00339 will_return_count(function, (value), -1) 00340 #endif 00341 00388 /* 00389 * Add a custom parameter checking function. If the event parameter is NULL 00390 * the event structure is allocated internally by this function. If event 00391 * parameter is provided it must be allocated on the heap and doesn't need to 00392 * be deallocated by the caller. 00393 */ 00394 #ifdef DOXYGEN 00395 00411 void expect_check(#function, #parameter, #check_function, const void *check_data); 00412 #else 00413 #define expect_check(function, parameter, check_function, check_data) \ 00414 _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \ 00415 cast_to_largest_integral_type(check_data), NULL, 1) 00416 #endif 00417 00418 #ifdef DOXYGEN 00419 00433 void expect_in_set(#function, #parameter, LargestIntegralType value_array[]); 00434 #else 00435 #define expect_in_set(function, parameter, value_array) \ 00436 expect_in_set_count(function, parameter, value_array, 1) 00437 #endif 00438 00439 #ifdef DOXYGEN 00440 00458 void expect_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count); 00459 #else 00460 #define expect_in_set_count(function, parameter, value_array, count) \ 00461 _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \ 00462 sizeof(value_array) / sizeof((value_array)[0]), count) 00463 #endif 00464 00465 #ifdef DOXYGEN 00466 00480 void expect_not_in_set(#function, #parameter, LargestIntegralType value_array[]); 00481 #else 00482 #define expect_not_in_set(function, parameter, value_array) \ 00483 expect_not_in_set_count(function, parameter, value_array, 1) 00484 #endif 00485 00486 #ifdef DOXYGEN 00487 00505 void expect_not_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count); 00506 #else 00507 #define expect_not_in_set_count(function, parameter, value_array, count) \ 00508 _expect_not_in_set( \ 00509 #function, #parameter, __FILE__, __LINE__, value_array, \ 00510 sizeof(value_array) / sizeof((value_array)[0]), count) 00511 #endif 00512 00513 00514 #ifdef DOXYGEN 00515 00531 void expect_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum); 00532 #else 00533 #define expect_in_range(function, parameter, minimum, maximum) \ 00534 expect_in_range_count(function, parameter, minimum, maximum, 1) 00535 #endif 00536 00537 #ifdef DOXYGEN 00538 00558 void expect_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count); 00559 #else 00560 #define expect_in_range_count(function, parameter, minimum, maximum, count) \ 00561 _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \ 00562 maximum, count) 00563 #endif 00564 00565 #ifdef DOXYGEN 00566 00582 void expect_not_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum); 00583 #else 00584 #define expect_not_in_range(function, parameter, minimum, maximum) \ 00585 expect_not_in_range_count(function, parameter, minimum, maximum, 1) 00586 #endif 00587 00588 #ifdef DOXYGEN 00589 00609 void expect_not_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count); 00610 #else 00611 #define expect_not_in_range_count(function, parameter, minimum, maximum, \ 00612 count) \ 00613 _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \ 00614 minimum, maximum, count) 00615 #endif 00616 00617 #ifdef DOXYGEN 00618 00631 void expect_value(#function, #parameter, LargestIntegralType value); 00632 #else 00633 #define expect_value(function, parameter, value) \ 00634 expect_value_count(function, parameter, value, 1) 00635 #endif 00636 00637 #ifdef DOXYGEN 00638 00655 void expect_value_count(#function, #parameter, LargestIntegralType value, size_t count); 00656 #else 00657 #define expect_value_count(function, parameter, value, count) \ 00658 _expect_value(#function, #parameter, __FILE__, __LINE__, \ 00659 cast_to_largest_integral_type(value), count) 00660 #endif 00661 00662 #ifdef DOXYGEN 00663 00676 void expect_not_value(#function, #parameter, LargestIntegralType value); 00677 #else 00678 #define expect_not_value(function, parameter, value) \ 00679 expect_not_value_count(function, parameter, value, 1) 00680 #endif 00681 00682 #ifdef DOXYGEN 00683 00700 void expect_not_value_count(#function, #parameter, LargestIntegralType value, size_t count); 00701 #else 00702 #define expect_not_value_count(function, parameter, value, count) \ 00703 _expect_not_value(#function, #parameter, __FILE__, __LINE__, \ 00704 cast_to_largest_integral_type(value), count) 00705 #endif 00706 00707 #ifdef DOXYGEN 00708 00722 void expect_string(#function, #parameter, const char *string); 00723 #else 00724 #define expect_string(function, parameter, string) \ 00725 expect_string_count(function, parameter, string, 1) 00726 #endif 00727 00728 #ifdef DOXYGEN 00729 00747 void expect_string_count(#function, #parameter, const char *string, size_t count); 00748 #else 00749 #define expect_string_count(function, parameter, string, count) \ 00750 _expect_string(#function, #parameter, __FILE__, __LINE__, \ 00751 (const char*)(string), count) 00752 #endif 00753 00754 #ifdef DOXYGEN 00755 00769 void expect_not_string(#function, #parameter, const char *string); 00770 #else 00771 #define expect_not_string(function, parameter, string) \ 00772 expect_not_string_count(function, parameter, string, 1) 00773 #endif 00774 00775 #ifdef DOXYGEN 00776 00794 void expect_not_string_count(#function, #parameter, const char *string, size_t count); 00795 #else 00796 #define expect_not_string_count(function, parameter, string, count) \ 00797 _expect_not_string(#function, #parameter, __FILE__, __LINE__, \ 00798 (const char*)(string), count) 00799 #endif 00800 00801 #ifdef DOXYGEN 00802 00817 void expect_memory(#function, #parameter, void *memory, size_t size); 00818 #else 00819 #define expect_memory(function, parameter, memory, size) \ 00820 expect_memory_count(function, parameter, memory, size, 1) 00821 #endif 00822 00823 #ifdef DOXYGEN 00824 00844 void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count); 00845 #else 00846 #define expect_memory_count(function, parameter, memory, size, count) \ 00847 _expect_memory(#function, #parameter, __FILE__, __LINE__, \ 00848 (const void*)(memory), size, count) 00849 #endif 00850 00851 #ifdef DOXYGEN 00852 00868 void expect_not_memory(#function, #parameter, void *memory, size_t size); 00869 #else 00870 #define expect_not_memory(function, parameter, memory, size) \ 00871 expect_not_memory_count(function, parameter, memory, size, 1) 00872 #endif 00873 00874 #ifdef DOXYGEN 00875 00895 void expect_not_memory_count(#function, #parameter, void *memory, size_t size, size_t count); 00896 #else 00897 #define expect_not_memory_count(function, parameter, memory, size, count) \ 00898 _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \ 00899 (const void*)(memory), size, count) 00900 #endif 00901 00902 00903 #ifdef DOXYGEN 00904 00915 void expect_any(#function, #parameter); 00916 #else 00917 #define expect_any(function, parameter) \ 00918 expect_any_count(function, parameter, 1) 00919 #endif 00920 00921 #ifdef DOXYGEN 00922 00938 void expect_any_count(#function, #parameter, size_t count); 00939 #else 00940 #define expect_any_count(function, parameter, count) \ 00941 _expect_any(#function, #parameter, __FILE__, __LINE__, count) 00942 #endif 00943 00944 #ifdef DOXYGEN 00945 00955 void check_expected(#parameter); 00956 #else 00957 #define check_expected(parameter) \ 00958 _check_expected(__func__, #parameter, __FILE__, __LINE__, \ 00959 cast_to_largest_integral_type(parameter)) 00960 #endif 00961 00962 #ifdef DOXYGEN 00963 00973 void check_expected_ptr(#parameter); 00974 #else 00975 #define check_expected_ptr(parameter) \ 00976 _check_expected(__func__, #parameter, __FILE__, __LINE__, \ 00977 cast_ptr_to_largest_integral_type(parameter)) 00978 #endif 00979 01001 #ifdef DOXYGEN 01002 01014 void assert_true(scalar expression); 01015 #else 01016 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \ 01017 __FILE__, __LINE__) 01018 #endif 01019 01020 #ifdef DOXYGEN 01021 01032 void assert_false(scalar expression); 01033 #else 01034 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \ 01035 __FILE__, __LINE__) 01036 #endif 01037 01038 #ifdef DOXYGEN 01039 01051 void assert_return_code(int rc, int error); 01052 #else 01053 #define assert_return_code(rc, error) \ 01054 _assert_return_code(cast_to_largest_integral_type(rc), \ 01055 sizeof(rc), \ 01056 cast_to_largest_integral_type(error), \ 01057 #rc, __FILE__, __LINE__) 01058 #endif 01059 01060 #ifdef DOXYGEN 01061 01071 void assert_non_null(void *pointer); 01072 #else 01073 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \ 01074 __FILE__, __LINE__) 01075 #endif 01076 01077 #ifdef DOXYGEN 01078 01088 void assert_null(void *pointer); 01089 #else 01090 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \ 01091 __FILE__, __LINE__) 01092 #endif 01093 01094 #ifdef DOXYGEN 01095 01105 void assert_ptr_equal(void *a, void *b); 01106 #else 01107 #define assert_ptr_equal(a, b) \ 01108 _assert_int_equal(cast_ptr_to_largest_integral_type(a), \ 01109 cast_ptr_to_largest_integral_type(b), \ 01110 __FILE__, __LINE__) 01111 #endif 01112 01113 #ifdef DOXYGEN 01114 01124 void assert_ptr_not_equal(void *a, void *b); 01125 #else 01126 #define assert_ptr_not_equal(a, b) \ 01127 _assert_int_not_equal(cast_ptr_to_largest_integral_type(a), \ 01128 cast_ptr_to_largest_integral_type(b), \ 01129 __FILE__, __LINE__) 01130 #endif 01131 01132 #ifdef DOXYGEN 01133 01143 void assert_int_equal(int a, int b); 01144 #else 01145 #define assert_int_equal(a, b) \ 01146 _assert_int_equal(cast_to_largest_integral_type(a), \ 01147 cast_to_largest_integral_type(b), \ 01148 __FILE__, __LINE__) 01149 #endif 01150 01151 #ifdef DOXYGEN 01152 01164 void assert_int_not_equal(int a, int b); 01165 #else 01166 #define assert_int_not_equal(a, b) \ 01167 _assert_int_not_equal(cast_to_largest_integral_type(a), \ 01168 cast_to_largest_integral_type(b), \ 01169 __FILE__, __LINE__) 01170 #endif 01171 01172 #ifdef DOXYGEN 01173 01183 void assert_string_equal(const char *a, const char *b); 01184 #else 01185 #define assert_string_equal(a, b) \ 01186 _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \ 01187 __LINE__) 01188 #endif 01189 01190 #ifdef DOXYGEN 01191 01201 void assert_string_not_equal(const char *a, const char *b); 01202 #else 01203 #define assert_string_not_equal(a, b) \ 01204 _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \ 01205 __LINE__) 01206 #endif 01207 01208 #ifdef DOXYGEN 01209 01223 void assert_memory_equal(const void *a, const void *b, size_t size); 01224 #else 01225 #define assert_memory_equal(a, b, size) \ 01226 _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \ 01227 __LINE__) 01228 #endif 01229 01230 #ifdef DOXYGEN 01231 01245 void assert_memory_not_equal(const void *a, const void *b, size_t size); 01246 #else 01247 #define assert_memory_not_equal(a, b, size) \ 01248 _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \ 01249 __FILE__, __LINE__) 01250 #endif 01251 01252 #ifdef DOXYGEN 01253 01266 void assert_in_range(LargestIntegralType value, LargestIntegralType minimum, LargestIntegralType maximum); 01267 #else 01268 #define assert_in_range(value, minimum, maximum) \ 01269 _assert_in_range( \ 01270 cast_to_largest_integral_type(value), \ 01271 cast_to_largest_integral_type(minimum), \ 01272 cast_to_largest_integral_type(maximum), __FILE__, __LINE__) 01273 #endif 01274 01275 #ifdef DOXYGEN 01276 01289 void assert_not_in_range(LargestIntegralType value, LargestIntegralType minimum, LargestIntegralType maximum); 01290 #else 01291 #define assert_not_in_range(value, minimum, maximum) \ 01292 _assert_not_in_range( \ 01293 cast_to_largest_integral_type(value), \ 01294 cast_to_largest_integral_type(minimum), \ 01295 cast_to_largest_integral_type(maximum), __FILE__, __LINE__) 01296 #endif 01297 01298 #ifdef DOXYGEN 01299 01311 void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count); 01312 #else 01313 #define assert_in_set(value, values, number_of_values) \ 01314 _assert_in_set(value, values, number_of_values, __FILE__, __LINE__) 01315 #endif 01316 01317 #ifdef DOXYGEN 01318 01330 void assert_not_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count); 01331 #else 01332 #define assert_not_in_set(value, values, number_of_values) \ 01333 _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__) 01334 #endif 01335 01364 #ifdef DOXYGEN 01365 01368 void fail(void); 01369 #else 01370 #define fail() _fail(__FILE__, __LINE__) 01371 #endif 01372 01373 #ifdef DOXYGEN 01374 01377 void skip(void); 01378 #else 01379 #define skip() _skip(__FILE__, __LINE__) 01380 #endif 01381 01382 #ifdef DOXYGEN 01383 01397 void fail_msg(const char *msg, ...); 01398 #else 01399 #define fail_msg(msg, ...) do { \ 01400 print_error("ERROR: " msg "\n", ##__VA_ARGS__); \ 01401 fail(); \ 01402 } while (0) 01403 #endif 01404 01405 #ifdef DOXYGEN 01406 01425 int run_test(#function); 01426 #else 01427 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL) 01428 #endif 01429 01430 static inline void _unit_test_dummy(void **state) { 01431 (void)state; 01432 } 01433 01438 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST } 01439 01440 #define _unit_test_setup(test, setup) \ 01441 { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP } 01442 01447 #define unit_test_setup(test, setup) \ 01448 _unit_test_setup(test, setup), \ 01449 unit_test(test), \ 01450 _unit_test_teardown(test, _unit_test_dummy) 01451 01452 #define _unit_test_teardown(test, teardown) \ 01453 { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN } 01454 01459 #define unit_test_teardown(test, teardown) \ 01460 _unit_test_setup(test, _unit_test_dummy), \ 01461 unit_test(test), \ 01462 _unit_test_teardown(test, teardown) 01463 01468 #define group_test_setup(setup) \ 01469 { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP } 01470 01475 #define group_test_teardown(teardown) \ 01476 { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN } 01477 01485 #define unit_test_setup_teardown(test, setup, teardown) \ 01486 _unit_test_setup(test, setup), \ 01487 unit_test(test), \ 01488 _unit_test_teardown(test, teardown) 01489 01490 01492 #define cmocka_unit_test(f) { #f, f, NULL, NULL } 01493 01495 #define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL } 01496 01498 #define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown } 01499 01504 #define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown } 01505 01506 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0]) 01507 #define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof(tests)[0]) 01508 01509 #ifdef DOXYGEN 01510 01566 int cmocka_run_group_tests(const struct CMUnitTest group_tests[], 01567 CMFixtureFunction group_setup, 01568 CMFixtureFunction group_teardown); 01569 #else 01570 # define cmocka_run_group_tests(group_tests, group_setup, group_teardown) \ 01571 _cmocka_run_group_tests(#group_tests, group_tests, sizeof(group_tests) / sizeof(group_tests)[0], group_setup, group_teardown) 01572 #endif 01573 01574 #ifdef DOXYGEN 01575 01634 int cmocka_run_group_tests_name(const char *group_name, 01635 const struct CMUnitTest group_tests[], 01636 CMFixtureFunction group_setup, 01637 CMFixtureFunction group_teardown); 01638 #else 01639 # define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown) \ 01640 _cmocka_run_group_tests(group_name, group_tests, sizeof(group_tests) / sizeof(group_tests)[0], group_setup, group_teardown) 01641 #endif 01642 01668 #ifdef DOXYGEN 01669 01691 void *test_malloc(size_t size); 01692 #else 01693 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__) 01694 #endif 01695 01696 #ifdef DOXYGEN 01697 01710 void *test_calloc(size_t nmemb, size_t size); 01711 #else 01712 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__) 01713 #endif 01714 01715 #ifdef DOXYGEN 01716 01726 void *test_realloc(void *ptr, size_t size); 01727 #else 01728 #define test_realloc(ptr, size) _test_realloc(ptr, size, __FILE__, __LINE__) 01729 #endif 01730 01731 #ifdef DOXYGEN 01732 01739 void test_free(void *ptr); 01740 #else 01741 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__) 01742 #endif 01743 01744 /* Redirect malloc, calloc and free to the unit test allocators. */ 01745 #ifdef UNIT_TESTING 01746 #define malloc test_malloc 01747 #define realloc test_realloc 01748 #define calloc test_calloc 01749 #define free test_free 01750 #endif /* UNIT_TESTING */ 01751 01805 void mock_assert(const int result, const char* const expression, 01806 const char * const file, const int line); 01807 01808 #ifdef DOXYGEN 01809 01831 void expect_assert_failure(function fn_call); 01832 #else 01833 #define expect_assert_failure(function_call) \ 01834 { \ 01835 const int result = setjmp(global_expect_assert_env); \ 01836 global_expecting_assert = 1; \ 01837 if (result) { \ 01838 print_message("Expected assertion %s occurred\n", \ 01839 global_last_failed_assert); \ 01840 global_expecting_assert = 0; \ 01841 } else { \ 01842 function_call ; \ 01843 global_expecting_assert = 0; \ 01844 print_error("Expected assert in %s\n", #function_call); \ 01845 _fail(__FILE__, __LINE__); \ 01846 } \ 01847 } 01848 #endif 01849 01852 /* Function prototype for setup, test and teardown functions. */ 01853 typedef void (*UnitTestFunction)(void **state); 01854 01855 /* Function that determines whether a function parameter value is correct. */ 01856 typedef int (*CheckParameterValue)(const LargestIntegralType value, 01857 const LargestIntegralType check_value_data); 01858 01859 /* Type of the unit test function. */ 01860 typedef enum UnitTestFunctionType { 01861 UNIT_TEST_FUNCTION_TYPE_TEST = 0, 01862 UNIT_TEST_FUNCTION_TYPE_SETUP, 01863 UNIT_TEST_FUNCTION_TYPE_TEARDOWN, 01864 UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP, 01865 UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN, 01866 } UnitTestFunctionType; 01867 01868 /* 01869 * Stores a unit test function with its name and type. 01870 * NOTE: Every setup function must be paired with a teardown function. It's 01871 * possible to specify NULL function pointers. 01872 */ 01873 typedef struct UnitTest { 01874 const char* name; 01875 UnitTestFunction function; 01876 UnitTestFunctionType function_type; 01877 } UnitTest; 01878 01879 typedef struct GroupTest { 01880 UnitTestFunction setup; 01881 UnitTestFunction teardown; 01882 const UnitTest *tests; 01883 const size_t number_of_tests; 01884 } GroupTest; 01885 01886 /* Function prototype for test functions. */ 01887 typedef void (*CMUnitTestFunction)(void **state); 01888 01889 /* Function prototype for setup and teardown functions. */ 01890 typedef int (*CMFixtureFunction)(void **state); 01891 01892 struct CMUnitTest { 01893 const char *name; 01894 CMUnitTestFunction test_func; 01895 CMFixtureFunction setup_func; 01896 CMFixtureFunction teardown_func; 01897 }; 01898 01899 /* Location within some source code. */ 01900 typedef struct SourceLocation { 01901 const char* file; 01902 int line; 01903 } SourceLocation; 01904 01905 /* Event that's called to check a parameter value. */ 01906 typedef struct CheckParameterEvent { 01907 SourceLocation location; 01908 const char *parameter_name; 01909 CheckParameterValue check_value; 01910 LargestIntegralType check_value_data; 01911 } CheckParameterEvent; 01912 01913 /* Used by expect_assert_failure() and mock_assert(). */ 01914 extern int global_expecting_assert; 01915 extern jmp_buf global_expect_assert_env; 01916 extern const char * global_last_failed_assert; 01917 01918 /* Retrieves a value for the given function, as set by "will_return". */ 01919 LargestIntegralType _mock(const char * const function, const char* const file, 01920 const int line); 01921 01922 void _expect_check( 01923 const char* const function, const char* const parameter, 01924 const char* const file, const int line, 01925 const CheckParameterValue check_function, 01926 const LargestIntegralType check_data, CheckParameterEvent * const event, 01927 const int count); 01928 01929 void _expect_in_set( 01930 const char* const function, const char* const parameter, 01931 const char* const file, const int line, const LargestIntegralType values[], 01932 const size_t number_of_values, const int count); 01933 void _expect_not_in_set( 01934 const char* const function, const char* const parameter, 01935 const char* const file, const int line, const LargestIntegralType values[], 01936 const size_t number_of_values, const int count); 01937 01938 void _expect_in_range( 01939 const char* const function, const char* const parameter, 01940 const char* const file, const int line, 01941 const LargestIntegralType minimum, 01942 const LargestIntegralType maximum, const int count); 01943 void _expect_not_in_range( 01944 const char* const function, const char* const parameter, 01945 const char* const file, const int line, 01946 const LargestIntegralType minimum, 01947 const LargestIntegralType maximum, const int count); 01948 01949 void _expect_value( 01950 const char* const function, const char* const parameter, 01951 const char* const file, const int line, const LargestIntegralType value, 01952 const int count); 01953 void _expect_not_value( 01954 const char* const function, const char* const parameter, 01955 const char* const file, const int line, const LargestIntegralType value, 01956 const int count); 01957 01958 void _expect_string( 01959 const char* const function, const char* const parameter, 01960 const char* const file, const int line, const char* string, 01961 const int count); 01962 void _expect_not_string( 01963 const char* const function, const char* const parameter, 01964 const char* const file, const int line, const char* string, 01965 const int count); 01966 01967 void _expect_memory( 01968 const char* const function, const char* const parameter, 01969 const char* const file, const int line, const void* const memory, 01970 const size_t size, const int count); 01971 void _expect_not_memory( 01972 const char* const function, const char* const parameter, 01973 const char* const file, const int line, const void* const memory, 01974 const size_t size, const int count); 01975 01976 void _expect_any( 01977 const char* const function, const char* const parameter, 01978 const char* const file, const int line, const int count); 01979 01980 void _check_expected( 01981 const char * const function_name, const char * const parameter_name, 01982 const char* file, const int line, const LargestIntegralType value); 01983 01984 void _will_return(const char * const function_name, const char * const file, 01985 const int line, const LargestIntegralType value, 01986 const int count); 01987 void _assert_true(const LargestIntegralType result, 01988 const char* const expression, 01989 const char * const file, const int line); 01990 void _assert_return_code(const LargestIntegralType result, 01991 size_t rlen, 01992 const LargestIntegralType error, 01993 const char * const expression, 01994 const char * const file, 01995 const int line); 01996 void _assert_int_equal( 01997 const LargestIntegralType a, const LargestIntegralType b, 01998 const char * const file, const int line); 01999 void _assert_int_not_equal( 02000 const LargestIntegralType a, const LargestIntegralType b, 02001 const char * const file, const int line); 02002 void _assert_string_equal(const char * const a, const char * const b, 02003 const char * const file, const int line); 02004 void _assert_string_not_equal(const char * const a, const char * const b, 02005 const char *file, const int line); 02006 void _assert_memory_equal(const void * const a, const void * const b, 02007 const size_t size, const char* const file, 02008 const int line); 02009 void _assert_memory_not_equal(const void * const a, const void * const b, 02010 const size_t size, const char* const file, 02011 const int line); 02012 void _assert_in_range( 02013 const LargestIntegralType value, const LargestIntegralType minimum, 02014 const LargestIntegralType maximum, const char* const file, const int line); 02015 void _assert_not_in_range( 02016 const LargestIntegralType value, const LargestIntegralType minimum, 02017 const LargestIntegralType maximum, const char* const file, const int line); 02018 void _assert_in_set( 02019 const LargestIntegralType value, const LargestIntegralType values[], 02020 const size_t number_of_values, const char* const file, const int line); 02021 void _assert_not_in_set( 02022 const LargestIntegralType value, const LargestIntegralType values[], 02023 const size_t number_of_values, const char* const file, const int line); 02024 02025 void* _test_malloc(const size_t size, const char* file, const int line); 02026 void* _test_realloc(void *ptr, const size_t size, const char* file, const int line); 02027 void* _test_calloc(const size_t number_of_elements, const size_t size, 02028 const char* file, const int line); 02029 void _test_free(void* const ptr, const char* file, const int line); 02030 02031 void _fail(const char * const file, const int line); 02032 02033 void _skip(const char * const file, const int line); 02034 02035 int _run_test( 02036 const char * const function_name, const UnitTestFunction Function, 02037 void ** const volatile state, const UnitTestFunctionType function_type, 02038 const void* const heap_check_point); 02039 CMOCKA_DEPRECATED int _run_tests(const UnitTest * const tests, 02040 const size_t number_of_tests); 02041 CMOCKA_DEPRECATED int _run_group_tests(const UnitTest * const tests, 02042 const size_t number_of_tests); 02043 02044 /* Test runner */ 02045 int _cmocka_run_group_tests(const char *group_name, 02046 const struct CMUnitTest * const tests, 02047 const size_t num_tests, 02048 CMFixtureFunction group_setup, 02049 CMFixtureFunction group_teardown); 02050 02051 /* Standard output and error print methods. */ 02052 void print_message(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2); 02053 void print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2); 02054 void vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0); 02055 void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0); 02056 02057 enum cm_message_output { 02058 CM_OUTPUT_STDOUT, 02059 CM_OUTPUT_SUBUNIT, 02060 CM_OUTPUT_TAP, 02061 CM_OUTPUT_XML, 02062 }; 02063 02075 void cmocka_set_message_output(enum cm_message_output output); 02076 02079 #endif /* CMOCKA_H_ */