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