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