CppUTest
TestHarness_c.h
1 /*
2  * Copyright (c) 2007, Michael Feathers, James Grenning and Bas Vodde
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the <organization> nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE EARLIER MENTIONED AUTHORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /******************************************************************************
29  *
30  * Provides an interface for when working with pure C
31  *
32  *******************************************************************************/
33 
34 #ifndef D_TestHarness_c_h
35 #define D_TestHarness_c_h
36 
37 #include "CppUTestConfig.h"
38 
39 #define CHECK_EQUAL_C_INT(expected,actual) \
40  CHECK_EQUAL_C_INT_LOCATION(expected,actual,__FILE__,__LINE__)
41 
42 #define CHECK_EQUAL_C_REAL(expected,actual,threshold) \
43  CHECK_EQUAL_C_REAL_LOCATION(expected,actual,threshold,__FILE__,__LINE__)
44 
45 #define CHECK_EQUAL_C_CHAR(expected,actual) \
46  CHECK_EQUAL_C_CHAR_LOCATION(expected,actual,__FILE__,__LINE__)
47 
48 #define CHECK_EQUAL_C_STRING(expected,actual) \
49  CHECK_EQUAL_C_STRING_LOCATION(expected,actual,__FILE__,__LINE__)
50 
51 #define FAIL_TEXT_C(text) \
52  FAIL_TEXT_C_LOCATION(text,__FILE__,__LINE__)
53 
54 #define FAIL_C() \
55  FAIL_C_LOCATION(__FILE__,__LINE__)
56 
57 #define CHECK_C(condition) \
58  CHECK_C_LOCATION(condition, #condition, __FILE__,__LINE__)
59 
60 #ifdef __cplusplus
61 extern "C"
62 {
63 #endif
64 
65 
66 /* CHECKS that can be used from C code */
67 extern void CHECK_EQUAL_C_INT_LOCATION(int expected, int actual,
68  const char* fileName, int lineNumber);
69 extern void CHECK_EQUAL_C_REAL_LOCATION(double expected, double actual,
70  double threshold, const char* fileName, int lineNumber);
71 extern void CHECK_EQUAL_C_CHAR_LOCATION(char expected, char actual,
72  const char* fileName, int lineNumber);
73 extern void CHECK_EQUAL_C_STRING_LOCATION(const char* expected,
74  const char* actual, const char* fileName, int lineNumber);
75 extern void FAIL_TEXT_C_LOCATION(const char* text, const char* fileName,
76  int lineNumber);
77 extern void FAIL_C_LOCATION(const char* fileName, int lineNumber);
78 extern void CHECK_C_LOCATION(int condition, const char* conditionString,
79  const char* fileName, int lineNumber);
80 
81 extern void* cpputest_malloc(size_t size);
82 extern void* cpputest_calloc(size_t num, size_t size);
83 extern void* cpputest_realloc(void* ptr, size_t size);
84 extern void cpputest_free(void* buffer);
85 
86 extern void* cpputest_malloc_location(size_t size, const char* file, int line);
87 extern void* cpputest_calloc_location(size_t num, size_t size,
88  const char* file, int line);
89 extern void* cpputest_realloc_location(void* memory, size_t size,
90  const char* file, int line);
91 extern void cpputest_free_location(void* buffer, const char* file, int line);
92 
93 void cpputest_malloc_set_out_of_memory(void);
94 void cpputest_malloc_set_not_out_of_memory(void);
95 void cpputest_malloc_set_out_of_memory_countdown(int);
96 void cpputest_malloc_count_reset(void);
97 int cpputest_malloc_get_count(void);
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 
103 
104 /*
105  * Small additional macro for unused arguments. This is common when stubbing, but in C you cannot remove the
106  * name of the parameter (as in C++).
107  */
108 
109 #ifndef PUNUSED
110 #if defined(__GNUC__)
111 # define PUNUSED(x) PUNUSED_ ##x __attribute__((unused))
112 #else
113 # define PUNUSED(x) x
114 #endif
115 #endif
116 
117 #endif