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