cmockery.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * Copyright 2008 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef CMOCKERY_H_
  17. #define CMOCKERY_H_
  18. #ifdef _WIN32
  19. #if _MSC_VER < 1500
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif // __cplusplus
  23. int __stdcall IsDebuggerPresent();
  24. #ifdef __cplusplus
  25. } /* extern "C" */
  26. #endif // __cplusplus
  27. #endif // _MSC_VER < 1500
  28. #endif // _WIN32
  29. /*
  30. * These headers or their equivalents should be included prior to including
  31. * this header file.
  32. *
  33. * #include <stdarg.h>
  34. * #include <stddef.h>
  35. * #include <setjmp.h>
  36. * #include <inttypes.h>
  37. *
  38. * This allows test applications to use custom definitions of C standard
  39. * library functions and types.
  40. */
  41. // For those who are used to __func__ from gcc.
  42. #ifndef __func__
  43. #define __func__ __FUNCTION__
  44. #endif
  45. /* Largest integral type. This type should be large enough to hold any
  46. * pointer or integer supported by the compiler. */
  47. #ifndef _UINTMAX_T
  48. #define _UINTMAX_T
  49. typedef unsigned long long uintmax_t;
  50. #endif /* _UINTMAX_T */
  51. /* Printf formats used to display uintmax_t. */
  52. #ifdef _WIN32
  53. #ifndef PRIdMAX
  54. #define PRIdMAX "I64d"
  55. #endif /* PRIdMAX */
  56. #ifndef PRIiMAX
  57. #define PRIiMAX "I64i"
  58. #endif /* PRIiMAX */
  59. #ifndef PRIoMAX
  60. #define PRIoMAX "I64o"
  61. #endif /* PRIoMAX */
  62. #ifndef PRIuMAX
  63. #define PRIuMAX "I64u"
  64. #endif /* PRIuMAX */
  65. #ifndef PRIxMAX
  66. #define PRIxMAX "I64x"
  67. #endif /* PRIxMAX */
  68. #ifndef PRIXMAX
  69. #define PRIXMAX "I64X"
  70. #endif /* PRIXMAX */
  71. #else /* _WIN32 */
  72. #ifndef PRIdMAX
  73. #define PRIdMAX "lld"
  74. #endif /* PRIdMAX */
  75. #ifndef PRIiMAX
  76. #define PRIiMAX "lli"
  77. #endif /* PRIiMAX */
  78. #ifndef PRIoMAX
  79. #define PRIoMAX "llo"
  80. #endif /* PRIoMAX */
  81. #ifndef PRIuMAX
  82. #define PRIuMAX "llu"
  83. #endif /* PRIuMAX */
  84. #ifndef PRIxMAX
  85. #define PRIxMAX "llx"
  86. #endif /* PRIxMAX */
  87. #ifndef PRIXMAX
  88. #define PRIXMAX "llX"
  89. #endif /* PRIXMAX */
  90. #endif /* _WIN32 */
  91. // Perform an unsigned cast to uintmax_t.
  92. #define cast_to_largest_integral_type(value) \
  93. ((uintmax_t)(value))
  94. /* Smallest integral type capable of holding a pointer. */
  95. #ifndef _UINTPTR_T
  96. #define _UINTPTR_T
  97. #ifdef _WIN32
  98. /* WIN32 is an ILP32 platform */
  99. typedef unsigned long uintptr_t;
  100. #else /* _WIN32 */
  101. /* what about 64-bit windows?
  102. * what's the right preprocessor symbol?
  103. typedef unsigned long long uintptr_t */
  104. #endif /* _WIN32 */
  105. #endif /* _UINTPTR_T */
  106. /* Perform an unsigned cast to uintptr_t. */
  107. #define cast_to_pointer_integral_type(value) \
  108. ((uintptr_t)(value))
  109. /* Perform a cast of a pointer to uintmax_t */
  110. #define cast_ptr_to_largest_integral_type(value) \
  111. cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
  112. // Retrieves a return value for the current function.
  113. #define mock() _mock(__func__, __FILE__, __LINE__)
  114. /* Stores a value to be returned by the specified function later.
  115. * The count parameter returns the number of times the value should be returned
  116. * by mock(). If count is set to -1 the value will always be returned.
  117. */
  118. #define will_return(function, value) \
  119. _will_return(#function, __FILE__, __LINE__, \
  120. cast_to_largest_integral_type(value), 1)
  121. #define will_return_count(function, value, count) \
  122. _will_return(#function, __FILE__, __LINE__, \
  123. cast_to_largest_integral_type(value), count)
  124. /* Add a custom parameter checking function. If the event parameter is NULL
  125. * the event structure is allocated internally by this function. If event
  126. * parameter is provided it must be allocated on the heap and doesn't need to
  127. * be deallocated by the caller.
  128. */
  129. #define expect_check(function, parameter, check_function, check_data) \
  130. _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
  131. cast_to_largest_integral_type(check_data), NULL, 0)
  132. /* Add an event to check a parameter, using check_expected(), against a set of
  133. * values. See will_return() for a description of the count parameter.
  134. */
  135. #define expect_in_set(function, parameter, value_array) \
  136. expect_in_set_count(function, parameter, value_array, 1)
  137. #define expect_in_set_count(function, parameter, value_array, count) \
  138. _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
  139. sizeof(value_array) / sizeof((value_array)[0]), count)
  140. #define expect_not_in_set(function, parameter, value_array) \
  141. expect_not_in_set_count(function, parameter, value_array, 1)
  142. #define expect_not_in_set_count(function, parameter, value_array, count) \
  143. _expect_not_in_set( \
  144. #function, #parameter, __FILE__, __LINE__, value_array, \
  145. sizeof(value_array) / sizeof((value_array)[0]), count)
  146. /* Add an event to check a parameter, using check_expected(), against a
  147. * signed range. Where range is minimum <= value <= maximum.
  148. * See will_return() for a description of the count parameter.
  149. */
  150. #define expect_in_range(function, parameter, minimum, maximum) \
  151. expect_in_range_count(function, parameter, minimum, maximum, 1)
  152. #define expect_in_range_count(function, parameter, minimum, maximum, count) \
  153. _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
  154. maximum, count)
  155. /* Add an event to check a parameter, using check_expected(), against a
  156. * signed range. Where range is value < minimum or value > maximum.
  157. * See will_return() for a description of the count parameter.
  158. */
  159. #define expect_not_in_range(function, parameter, minimum, maximum) \
  160. expect_not_in_range_count(function, parameter, minimum, maximum, 1)
  161. #define expect_not_in_range_count(function, parameter, minimum, maximum, \
  162. count) \
  163. _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
  164. minimum, maximum, count)
  165. /* Add an event to check whether a parameter, using check_expected(), is or
  166. * isn't a value. See will_return() for a description of the count parameter.
  167. */
  168. #define expect_value(function, parameter, value) \
  169. expect_value_count(function, parameter, value, 1)
  170. #define expect_value_count(function, parameter, value, count) \
  171. _expect_value(#function, #parameter, __FILE__, __LINE__, \
  172. cast_to_largest_integral_type(value), count)
  173. #define expect_not_value(function, parameter, value) \
  174. expect_not_value_count(function, parameter, value, 1)
  175. #define expect_not_value_count(function, parameter, value, count) \
  176. _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
  177. cast_to_largest_integral_type(value), count)
  178. /* Add an event to check whether a parameter, using check_expected(),
  179. * is or isn't a string. See will_return() for a description of the count
  180. * parameter.
  181. */
  182. #define expect_string(function, parameter, string) \
  183. expect_string_count(function, parameter, string, 1)
  184. #define expect_string_count(function, parameter, string, count) \
  185. _expect_string(#function, #parameter, __FILE__, __LINE__, \
  186. (const char*)(string), count)
  187. #define expect_not_string(function, parameter, string) \
  188. expect_not_string_count(function, parameter, string, 1)
  189. #define expect_not_string_count(function, parameter, string, count) \
  190. _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
  191. (const char*)(string), count)
  192. /* Add an event to check whether a parameter, using check_expected() does or
  193. * doesn't match an area of memory. See will_return() for a description of
  194. * the count parameter.
  195. */
  196. #define expect_memory(function, parameter, memory, size) \
  197. expect_memory_count(function, parameter, memory, size, 1)
  198. #define expect_memory_count(function, parameter, memory, size, count) \
  199. _expect_memory(#function, #parameter, __FILE__, __LINE__, \
  200. (const void*)(memory), size, count)
  201. #define expect_not_memory(function, parameter, memory, size) \
  202. expect_not_memory_count(function, parameter, memory, size, 1)
  203. #define expect_not_memory_count(function, parameter, memory, size, count) \
  204. _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
  205. (const void*)(memory), size, count)
  206. /* Add an event to allow any value for a parameter checked using
  207. * check_expected(). See will_return() for a description of the count
  208. * parameter.
  209. */
  210. #define expect_any(function, parameter) \
  211. expect_any_count(function, parameter, 1)
  212. #define expect_any_count(function, parameter, count) \
  213. _expect_any(#function, #parameter, __FILE__, __LINE__, count)
  214. /* Determine whether a function parameter is correct. This ensures the next
  215. * value queued by one of the expect_*() macros matches the specified variable.
  216. */
  217. #define check_expected(parameter) \
  218. _check_expected(__func__, #parameter, __FILE__, __LINE__, \
  219. cast_to_largest_integral_type(parameter))
  220. // Assert that the given expression is true.
  221. #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
  222. __FILE__, __LINE__)
  223. // Assert that the given expression is false.
  224. #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
  225. __FILE__, __LINE__)
  226. // Assert that the given pointer is non-NULL.
  227. #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
  228. __FILE__, __LINE__)
  229. // Assert that the given pointer is NULL.
  230. #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
  231. __FILE__, __LINE__)
  232. // Assert that the two given integers are equal, otherwise fail.
  233. #define assert_int_equal(a, b) \
  234. _assert_int_equal(cast_to_largest_integral_type(a), \
  235. cast_to_largest_integral_type(b), \
  236. __FILE__, __LINE__)
  237. // Assert that the two given integers are not equal, otherwise fail.
  238. #define assert_int_not_equal(a, b) \
  239. _assert_int_not_equal(cast_to_largest_integral_type(a), \
  240. cast_to_largest_integral_type(b), \
  241. __FILE__, __LINE__)
  242. // Assert that the two given strings are equal, otherwise fail.
  243. #define assert_string_equal(a, b) \
  244. _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
  245. __LINE__)
  246. // Assert that the two given strings are not equal, otherwise fail.
  247. #define assert_string_not_equal(a, b) \
  248. _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
  249. __LINE__)
  250. // Assert that the two given areas of memory are equal, otherwise fail.
  251. #define assert_memory_equal(a, b, size) \
  252. _assert_memory_equal((const char*)(a), (const char*)(b), size, __FILE__, \
  253. __LINE__)
  254. // Assert that the two given areas of memory are not equal, otherwise fail.
  255. #define assert_memory_not_equal(a, b, size) \
  256. _assert_memory_not_equal((const char*)(a), (const char*)(b), size, \
  257. __FILE__, __LINE__)
  258. // Assert that the specified value is >= minimum and <= maximum.
  259. #define assert_in_range(value, minimum, maximum) \
  260. _assert_in_range( \
  261. cast_to_largest_integral_type(value), \
  262. cast_to_largest_integral_type(minimum), \
  263. cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
  264. // Assert that the specified value is < minumum or > maximum
  265. #define assert_not_in_range(value, minimum, maximum) \
  266. _assert_not_in_range( \
  267. cast_to_largest_integral_type(value), \
  268. cast_to_largest_integral_type(minimum), \
  269. cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
  270. // Assert that the specified value is within a set.
  271. #define assert_in_set(value, values, number_of_values) \
  272. _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
  273. // Assert that the specified value is not within a set.
  274. #define assert_not_in_set(value, values, number_of_values) \
  275. _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
  276. // Forces the test to fail immediately and quit.
  277. #define fail() _fail(__FILE__, __LINE__)
  278. // Generic method to kick off testing
  279. #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
  280. // Initializes a UnitTest structure.
  281. #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
  282. #define unit_test_setup(test, setup) \
  283. { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
  284. #define unit_test_teardown(test, teardown) \
  285. { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
  286. /* Initialize an array of UnitTest structures with a setup function for a test
  287. * and a teardown function. Either setup or teardown can be NULL.
  288. */
  289. #define unit_test_setup_teardown(test, setup, teardown) \
  290. unit_test_setup(test, setup), \
  291. unit_test(test), \
  292. unit_test_teardown(test, teardown)
  293. /*
  294. * Run tests specified by an array of UnitTest structures. The following
  295. * example illustrates this macro's use with the unit_test macro.
  296. *
  297. * void Test0();
  298. * void Test1();
  299. *
  300. * int main(int argc, char* argv[]) {
  301. * const UnitTest tests[] = {
  302. * unit_test(Test0);
  303. * unit_test(Test1);
  304. * };
  305. * return run_tests(tests);
  306. * }
  307. */
  308. #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
  309. // Dynamic allocators
  310. #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
  311. #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
  312. #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
  313. // Redirect malloc, calloc and free to the unit test allocators.
  314. #if UNIT_TESTING
  315. #define malloc test_malloc
  316. #define calloc test_calloc
  317. #define free test_free
  318. #endif // UNIT_TESTING
  319. /*
  320. * Ensure mock_assert() is called. If mock_assert() is called the assert
  321. * expression string is returned.
  322. * For example:
  323. *
  324. * #define assert mock_assert
  325. *
  326. * void showmessage(const char *message) {
  327. * assert(message);
  328. * }
  329. *
  330. * int main(int argc, const char* argv[]) {
  331. * expect_assert_failure(show_message(NULL));
  332. * printf("succeeded\n");
  333. * return 0;
  334. * }
  335. */
  336. #define expect_assert_failure(function_call) \
  337. { \
  338. const int expression = setjmp(global_expect_assert_env); \
  339. global_expecting_assert = 1; \
  340. if (expression) { \
  341. print_message("Expected assertion %s occurred\n", \
  342. *((const char**)&expression)); \
  343. global_expecting_assert = 0; \
  344. } else { \
  345. function_call ; \
  346. global_expecting_assert = 0; \
  347. print_error("Expected assert in %s\n", #function_call); \
  348. _fail(__FILE__, __LINE__); \
  349. } \
  350. }
  351. // Function prototype for setup, test and teardown functions.
  352. typedef void (*UnitTestFunction)(void **state);
  353. // Function that determines whether a function parameter value is correct.
  354. typedef int (*CheckParameterValue)(const uintmax_t value,
  355. const uintmax_t check_value_data);
  356. // Type of the unit test function.
  357. typedef enum UnitTestFunctionType {
  358. UNIT_TEST_FUNCTION_TYPE_TEST = 0,
  359. UNIT_TEST_FUNCTION_TYPE_SETUP,
  360. UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
  361. } UnitTestFunctionType;
  362. /* Stores a unit test function with its name and type.
  363. * NOTE: Every setup function must be paired with a teardown function. It's
  364. * possible to specify NULL function pointers.
  365. */
  366. typedef struct UnitTest {
  367. const char* name;
  368. UnitTestFunction function;
  369. UnitTestFunctionType function_type;
  370. } UnitTest;
  371. // Location within some source code.
  372. typedef struct SourceLocation {
  373. const char* file;
  374. int line;
  375. } SourceLocation;
  376. // Event that's called to check a parameter value.
  377. typedef struct CheckParameterEvent {
  378. SourceLocation location;
  379. const char *parameter_name;
  380. CheckParameterValue check_value;
  381. uintmax_t check_value_data;
  382. } CheckParameterEvent;
  383. // Used by expect_assert_failure() and mock_assert().
  384. extern int global_expecting_assert;
  385. extern jmp_buf global_expect_assert_env;
  386. // Retrieves a value for the given function, as set by "will_return".
  387. uintmax_t _mock(const char * const function, const char* const file,
  388. const int line);
  389. void _expect_check(
  390. const char* const function, const char* const parameter,
  391. const char* const file, const int line,
  392. const CheckParameterValue check_function,
  393. const uintmax_t check_data, CheckParameterEvent * const event,
  394. const int count);
  395. void _expect_in_set(
  396. const char* const function, const char* const parameter,
  397. const char* const file, const int line, const uintmax_t values[],
  398. const size_t number_of_values, const int count);
  399. void _expect_not_in_set(
  400. const char* const function, const char* const parameter,
  401. const char* const file, const int line, const uintmax_t values[],
  402. const size_t number_of_values, const int count);
  403. void _expect_in_range(
  404. const char* const function, const char* const parameter,
  405. const char* const file, const int line,
  406. const uintmax_t minimum,
  407. const uintmax_t maximum, const int count);
  408. void _expect_not_in_range(
  409. const char* const function, const char* const parameter,
  410. const char* const file, const int line,
  411. const uintmax_t minimum,
  412. const uintmax_t maximum, const int count);
  413. void _expect_value(
  414. const char* const function, const char* const parameter,
  415. const char* const file, const int line, const uintmax_t value,
  416. const int count);
  417. void _expect_not_value(
  418. const char* const function, const char* const parameter,
  419. const char* const file, const int line, const uintmax_t value,
  420. const int count);
  421. void _expect_string(
  422. const char* const function, const char* const parameter,
  423. const char* const file, const int line, const char* string,
  424. const int count);
  425. void _expect_not_string(
  426. const char* const function, const char* const parameter,
  427. const char* const file, const int line, const char* string,
  428. const int count);
  429. void _expect_memory(
  430. const char* const function, const char* const parameter,
  431. const char* const file, const int line, const void* const memory,
  432. const size_t size, const int count);
  433. void _expect_not_memory(
  434. const char* const function, const char* const parameter,
  435. const char* const file, const int line, const void* const memory,
  436. const size_t size, const int count);
  437. void _expect_any(
  438. const char* const function, const char* const parameter,
  439. const char* const file, const int line, const int count);
  440. void _check_expected(
  441. const char * const function_name, const char * const parameter_name,
  442. const char* file, const int line, const uintmax_t value);
  443. // Can be used to replace assert in tested code so that in conjuction with
  444. // check_assert() it's possible to determine whether an assert condition has
  445. // failed without stopping a test.
  446. void mock_assert(const int result, const char* const expression,
  447. const char * const file, const int line);
  448. void _will_return(const char * const function_name, const char * const file,
  449. const int line, const uintmax_t value,
  450. const int count);
  451. void _assert_true(const uintmax_t result,
  452. const char* const expression,
  453. const char * const file, const int line);
  454. void _assert_int_equal(
  455. const uintmax_t a, const uintmax_t b,
  456. const char * const file, const int line);
  457. void _assert_int_not_equal(
  458. const uintmax_t a, const uintmax_t b,
  459. const char * const file, const int line);
  460. void _assert_string_equal(const char * const a, const char * const b,
  461. const char * const file, const int line);
  462. void _assert_string_not_equal(const char * const a, const char * const b,
  463. const char *file, const int line);
  464. void _assert_memory_equal(const void * const a, const void * const b,
  465. const size_t size, const char* const file,
  466. const int line);
  467. void _assert_memory_not_equal(const void * const a, const void * const b,
  468. const size_t size, const char* const file,
  469. const int line);
  470. void _assert_in_range(
  471. const uintmax_t value, const uintmax_t minimum,
  472. const uintmax_t maximum, const char* const file, const int line);
  473. void _assert_not_in_range(
  474. const uintmax_t value, const uintmax_t minimum,
  475. const uintmax_t maximum, const char* const file, const int line);
  476. void _assert_in_set(
  477. const uintmax_t value, const uintmax_t values[],
  478. const size_t number_of_values, const char* const file, const int line);
  479. void _assert_not_in_set(
  480. const uintmax_t value, const uintmax_t values[],
  481. const size_t number_of_values, const char* const file, const int line);
  482. void* _test_malloc(const size_t size, const char* file, const int line);
  483. void* _test_calloc(const size_t number_of_elements, const size_t size,
  484. const char* file, const int line);
  485. void _test_free(void* const ptr, const char* file, const int line);
  486. void _fail(const char * const file, const int line);
  487. int _run_test(
  488. const char * const function_name, const UnitTestFunction Function,
  489. void ** const volatile state, const UnitTestFunctionType function_type,
  490. const void* const heap_check_point);
  491. int _run_tests(const UnitTest * const tests, const size_t number_of_tests);
  492. // Standard output and error print methods.
  493. void print_message(const char* const format, ...);
  494. void print_error(const char* const format, ...);
  495. void vprint_message(const char* const format, va_list args);
  496. void vprint_error(const char* const format, va_list args);
  497. #endif // CMOCKERY_H_