NetBurner 3.1
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_BOOL(expected,actual) \
40  CHECK_EQUAL_C_BOOL_LOCATION(expected,actual,__FILE__,__LINE__)
41 
42 #define CHECK_EQUAL_C_INT(expected,actual) \
43  CHECK_EQUAL_C_INT_LOCATION(expected,actual,__FILE__,__LINE__)
44 
45 #define CHECK_EQUAL_C_UINT(expected,actual) \
46  CHECK_EQUAL_C_UINT_LOCATION(expected,actual,__FILE__,__LINE__)
47 
48 #define CHECK_EQUAL_C_LONG(expected,actual) \
49  CHECK_EQUAL_C_LONG_LOCATION(expected,actual,__FILE__,__LINE__)
50 
51 #define CHECK_EQUAL_C_ULONG(expected,actual) \
52  CHECK_EQUAL_C_ULONG_LOCATION(expected,actual,__FILE__,__LINE__)
53 
54 #define CHECK_EQUAL_C_LONGLONG(expected,actual) \
55  CHECK_EQUAL_C_LONGLONG_LOCATION(expected,actual,__FILE__,__LINE__)
56 
57 #define CHECK_EQUAL_C_ULONGLONG(expected,actual) \
58  CHECK_EQUAL_C_ULONGLONG_LOCATION(expected,actual,__FILE__,__LINE__)
59 
60 #define CHECK_EQUAL_C_REAL(expected,actual,threshold) \
61  CHECK_EQUAL_C_REAL_LOCATION(expected,actual,threshold,__FILE__,__LINE__)
62 
63 #define CHECK_EQUAL_C_CHAR(expected,actual) \
64  CHECK_EQUAL_C_CHAR_LOCATION(expected,actual,__FILE__,__LINE__)
65 
66 #define CHECK_EQUAL_C_UBYTE(expected,actual) \
67  CHECK_EQUAL_C_UBYTE_LOCATION(expected,actual,__FILE__,__LINE__)
68 
69 #define CHECK_EQUAL_C_SBYTE(expected,actual) \
70  CHECK_EQUAL_C_SBYTE_LOCATION(expected,actual,__FILE__,__LINE__)
71 
72 #define CHECK_EQUAL_C_STRING(expected,actual) \
73  CHECK_EQUAL_C_STRING_LOCATION(expected,actual,__FILE__,__LINE__)
74 
75 #define CHECK_EQUAL_C_POINTER(expected,actual) \
76  CHECK_EQUAL_C_POINTER_LOCATION(expected,actual,__FILE__,__LINE__)
77 
78 #define CHECK_EQUAL_C_BITS(expected, actual, mask)\
79  CHECK_EQUAL_C_BITS_LOCATION(expected, actual, mask, sizeof(actual), __FILE__, __LINE__)
80 
81 #define FAIL_TEXT_C(text) \
82  FAIL_TEXT_C_LOCATION(text,__FILE__,__LINE__)
83 
84 #define FAIL_C() \
85  FAIL_C_LOCATION(__FILE__,__LINE__)
86 
87 #define CHECK_C(condition) \
88  CHECK_C_LOCATION(condition, #condition, __FILE__,__LINE__)
89 
90 
91 /******************************************************************************
92  *
93  * TEST macros for in C.
94  *
95  *******************************************************************************/
96 
97 /* For use in C file */
98 #define TEST_GROUP_C_SETUP(group_name) \
99  extern void group_##group_name##_setup_wrapper_c(void); \
100  void group_##group_name##_setup_wrapper_c()
101 
102 #define TEST_GROUP_C_TEARDOWN(group_name) \
103  extern void group_##group_name##_teardown_wrapper_c(void); \
104  void group_##group_name##_teardown_wrapper_c()
105 
106 #define TEST_C(group_name, test_name) \
107  extern void test_##group_name##_##test_name##_wrapper_c(void);\
108  void test_##group_name##_##test_name##_wrapper_c()
109 
110 
111 /* For use in C++ file */
112 
113 #define TEST_GROUP_C_WRAPPER(group_name) \
114  extern "C" void group_##group_name##_setup_wrapper_c(void); \
115  extern "C" void group_##group_name##_teardown_wrapper_c(void); \
116  TEST_GROUP(group_name)
117 
118 #define TEST_GROUP_C_SETUP_WRAPPER(group_name) \
119  void setup() { \
120  group_##group_name##_setup_wrapper_c(); \
121  }
122 
123 #define TEST_GROUP_C_TEARDOWN_WRAPPER(group_name) \
124  void teardown() { \
125  group_##group_name##_teardown_wrapper_c(); \
126  }
127 
128 #define TEST_C_WRAPPER(group_name, test_name) \
129  extern "C" void test_##group_name##_##test_name##_wrapper_c(); \
130  TEST(group_name, test_name) { \
131  test_##group_name##_##test_name##_wrapper_c(); \
132  }
133 
134 #ifdef __cplusplus
135 extern "C"
136 {
137 #endif
138 
139 
140 /* CHECKS that can be used from C code */
141 extern void CHECK_EQUAL_C_BOOL_LOCATION(int expected, int actual,
142  const char* fileName, int lineNumber);
143 extern void CHECK_EQUAL_C_INT_LOCATION(int expected, int actual,
144  const char* fileName, int lineNumber);
145 extern void CHECK_EQUAL_C_UINT_LOCATION(unsigned int expected, unsigned int actual,
146  const char* fileName, int lineNumber);
147 extern void CHECK_EQUAL_C_LONG_LOCATION(long expected, long actual,
148  const char* fileName, int lineNumber);
149 extern void CHECK_EQUAL_C_ULONG_LOCATION(unsigned long expected, unsigned long actual,
150  const char* fileName, int lineNumber);
151 extern void CHECK_EQUAL_C_LONGLONG_LOCATION(cpputest_longlong expected, cpputest_longlong actual,
152  const char* fileName, int lineNumber);
153 extern void CHECK_EQUAL_C_ULONGLONG_LOCATION(cpputest_ulonglong expected, cpputest_ulonglong actual,
154  const char* fileName, int lineNumber);
155 extern void CHECK_EQUAL_C_REAL_LOCATION(double expected, double actual,
156  double threshold, const char* fileName, int lineNumber);
157 extern void CHECK_EQUAL_C_CHAR_LOCATION(char expected, char actual,
158  const char* fileName, int lineNumber);
159 extern void CHECK_EQUAL_C_UBYTE_LOCATION(unsigned char expected, unsigned char actual,
160  const char* fileName, int lineNumber);
161 extern void CHECK_EQUAL_C_SBYTE_LOCATION(signed char expected, signed char actual,
162  const char* fileName, int lineNumber);
163 extern void CHECK_EQUAL_C_STRING_LOCATION(const char* expected,
164  const char* actual, const char* fileName, int lineNumber);
165 extern void CHECK_EQUAL_C_POINTER_LOCATION(const void* expected,
166  const void* actual, const char* fileName, int lineNumber);
167 extern void CHECK_EQUAL_C_BITS_LOCATION(unsigned int expected, unsigned int actual,
168  unsigned int mask, size_t size, const char* fileName, int lineNumber);
169 extern void FAIL_TEXT_C_LOCATION(const char* text, const char* fileName,
170  int lineNumber);
171 extern void FAIL_C_LOCATION(const char* fileName, int lineNumber);
172 extern void CHECK_C_LOCATION(int condition, const char* conditionString,
173  const char* fileName, int lineNumber);
174 
175 extern void* cpputest_malloc(size_t size);
176 extern void* cpputest_calloc(size_t num, size_t size);
177 extern void* cpputest_realloc(void* ptr, size_t size);
178 extern void cpputest_free(void* buffer);
179 
180 extern void* cpputest_malloc_location(size_t size, const char* file, int line);
181 extern void* cpputest_calloc_location(size_t num, size_t size,
182  const char* file, int line);
183 extern void* cpputest_realloc_location(void* memory, size_t size,
184  const char* file, int line);
185 extern void cpputest_free_location(void* buffer, const char* file, int line);
186 
187 void cpputest_malloc_set_out_of_memory(void);
188 void cpputest_malloc_set_not_out_of_memory(void);
189 void cpputest_malloc_set_out_of_memory_countdown(int);
190 void cpputest_malloc_count_reset(void);
191 int cpputest_malloc_get_count(void);
192 
193 #ifdef __cplusplus
194 }
195 #endif
196 
197 
198 /*
199  * Small additional macro for unused arguments. This is common when stubbing, but in C you cannot remove the
200  * name of the parameter (as in C++).
201  */
202 
203 #ifndef PUNUSED
204 #if defined(__GNUC__)
205 # define PUNUSED(x) PUNUSED_ ##x __attribute__((unused))
206 #else
207 # define PUNUSED(x) x
208 #endif
209 #endif
210 
211 #endif