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