Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-ctest/inc/ctest.h

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #ifndef CTEST_H
5 #define CTEST_H
6
7 #include "azure_macro_utils/macro_utils.h"
8
9 #ifdef __cplusplus
10 #include <cstddef>
11 #include <cstring>
12 #include <cstdio>
13 #include <csetjmp>
14 #include <setjmp.h> /* Some compilers do not want to play by the standard, specifically ARM CC */
15 #include <stdio.h> /* Some compilers do not want to play by the standard, specifically ARM CC */
16 #define C_LINKAGE "C"
17 #define C_LINKAGE_PREFIX extern "C"
18 #else
19 #include <stddef.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <setjmp.h>
23 #include <wchar.h>
24 #define C_LINKAGE
25 #define C_LINKAGE_PREFIX
26 #endif
27
28 #if defined _MSC_VER
29 #include "ctest_windows.h"
30 #define CTEST_USE_STDINT
31 #if _MSC_VER < 1900
32 /*https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/snprintf-snprintf-snprintf-l-snwprintf-snwprintf-l?view=vs-2019 says snprintf is C99 compliant since VS 2015*/
33 /*https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019 says VS 2015 is 1900*/
34 /*so for all the "old" VSes use _snprintf*/
35 #define snprintf _snprintf
36 #endif
37 #elif defined __cplusplus
38 #define CTEST_USE_STDINT
39 #elif defined __STDC_VERSION__
40 #if ((__STDC_VERSION__ == 199901L) || (__STDC_VERSION__ == 201000L) || (__STDC_VERSION__ == 201112L))
41 #define CTEST_USE_STDINT
42 #else
43 C_LINKAGE_PREFIX int snprintf(char * s, size_t n, const char * format, ...);
44 #endif
45 #endif
46
47 #if defined _MSC_VER && _MSC_VER <= 1700
48 #pragma warning(disable: 4054 4127 4510 4512 4610) /* MSC 1500 (VS2008) incorrectly fires this */
49 #endif
50
51 #if defined CTEST_USE_STDINT
52 #include <stdint.h>
53 #endif
54
55 #ifdef __cplusplus
56 extern "C" {
57 #endif
58
59 #ifdef USE_COLORING
60 #define CTEST_ANSI_COLOR_RED "\x1b[31m"
61 #define CTEST_ANSI_COLOR_GREEN "\x1b[32m"
62 #define CTEST_ANSI_COLOR_YELLOW "\x1b[33m"
63 #define CTEST_ANSI_COLOR_RESET "\x1b[0m"
64 #else
65 #define CTEST_ANSI_COLOR_RED ""
66 #define CTEST_ANSI_COLOR_GREEN ""
67 #define CTEST_ANSI_COLOR_YELLOW ""
68 #define CTEST_ANSI_COLOR_RESET ""
69 #endif
70
71 typedef void(*TEST_FUNC)(void);
72
73 #define CTEST_FUNCTION_TYPE_VALUES \
74 CTEST_BEGIN_SUITE, \
75 CTEST_END_SUITE, \
76 CTEST_TEST_FUNCTION, \
77 CTEST_TEST_SUITE_INITIALIZE, \
78 CTEST_TEST_SUITE_CLEANUP, \
79 CTEST_TEST_FUNCTION_INITIALIZE, \
80 CTEST_TEST_FUNCTION_CLEANUP
81
82 MU_DEFINE_ENUM(CTEST_FUNCTION_TYPE, CTEST_FUNCTION_TYPE_VALUES)
83
84 #define TEST_RESULT_VALUES \
85 TEST_SUCCESS, \
86 TEST_FAILED, \
87 TEST_NOT_EXECUTED \
88
89 MU_DEFINE_ENUM(TEST_RESULT, TEST_RESULT_VALUES)
90
91 typedef struct TEST_FUNCTION_DATA_TAG
92 {
93 const TEST_FUNC TestFunction;
94 const char* TestFunctionName;
95 const void* const NextTestFunctionData;
96 TEST_RESULT* const TestResult;
97 const CTEST_FUNCTION_TYPE FunctionType;
98 } TEST_FUNCTION_DATA;
99
100 #define EXPAND_1(A) A
101
102 extern const TEST_FUNCTION_DATA* g_CurrentTestFunction;
103 extern jmp_buf g_ExceptionJump;
104
105 #define CTEST_BEGIN_TEST_SUITE(testSuiteName) \
106 C_LINKAGE_PREFIX const int TestListHead_Begin_##testSuiteName = 0; \
107 static const TEST_FUNCTION_DATA MU_C2(TestFunctionData, MU_C1(__COUNTER__)) = { NULL, NULL, NULL, NULL, CTEST_BEGIN_SUITE }; \
108
109 #define CTEST_FUNCTION(funcName) \
110 static void funcName(void); \
111 static TEST_RESULT funcName##_TestResult; \
112 static const TEST_FUNCTION_DATA MU_C2(TestFunctionData, MU_C1(MU_INC(__COUNTER__))) = \
113 { funcName, #funcName, &MU_C2(TestFunctionData, MU_C1(MU_DEC(MU_DEC(__COUNTER__)))), &funcName##_TestResult, CTEST_TEST_FUNCTION }; \
114 static void funcName(void)
115
116 #define CTEST_SUITE_INITIALIZE() \
117 static void TestSuiteInitialize(void); \
118 static const TEST_FUNCTION_DATA MU_C2(TestFunctionData, MU_C1(MU_INC(__COUNTER__))) = \
119 { TestSuiteInitialize, "TestSuiteInitialize", &MU_C2(TestFunctionData, MU_C1(MU_DEC(MU_DEC(__COUNTER__)))), NULL, CTEST_TEST_SUITE_INITIALIZE }; \
120 static void TestSuiteInitialize(void)
121
122 #define CTEST_SUITE_CLEANUP() \
123 static void TestSuiteCleanup(void); \
124 static const TEST_FUNCTION_DATA MU_C2(TestFunctionData, MU_C1(MU_INC(__COUNTER__))) = \
125 { &TestSuiteCleanup, "TestSuiteCleanup", &MU_C2(TestFunctionData, MU_C1(MU_DEC(MU_DEC(__COUNTER__)))), NULL, CTEST_TEST_SUITE_CLEANUP }; \
126 static void TestSuiteCleanup(void)
127
128 #define CTEST_FUNCTION_INITIALIZE() \
129 static void TestFunctionInitialize(void); \
130 static const TEST_FUNCTION_DATA MU_C2(TestFunctionData, MU_C1(MU_INC(__COUNTER__))) = \
131 { TestFunctionInitialize, "TestFunctionInitialize", &MU_C2(TestFunctionData, MU_C1(MU_DEC(MU_DEC(__COUNTER__)))), NULL, CTEST_TEST_FUNCTION_INITIALIZE }; \
132 static void TestFunctionInitialize(void)
133
134 #define CTEST_FUNCTION_CLEANUP() \
135 static void TestFunctionCleanup(void); \
136 static const TEST_FUNCTION_DATA MU_C2(TestFunctionData, MU_C1(MU_INC(__COUNTER__))) = \
137 { &TestFunctionCleanup, "TestFunctionCleanup", &MU_C2(TestFunctionData, MU_C1(MU_DEC(MU_DEC(__COUNTER__)))), NULL, CTEST_TEST_FUNCTION_CLEANUP }; \
138 static void TestFunctionCleanup(void)
139
140 #define CTEST_END_TEST_SUITE(testSuiteName) \
141 static const TEST_FUNCTION_DATA MU_C2(TestFunctionData, MU_C1(MU_INC(__COUNTER__))) = { NULL, NULL, NULL, NULL, CTEST_TEST_FUNCTION }; \
142 C_LINKAGE_PREFIX const TEST_FUNCTION_DATA TestListHead_##testSuiteName = { NULL, NULL, &MU_C2(TestFunctionData, MU_C1(MU_DEC(MU_DEC(__COUNTER__)))), NULL, CTEST_END_SUITE }; \
143 static const int* TestListHead_End_##testSuiteName = &TestListHead_Begin_##testSuiteName;
144
145 #define PRINT_MY_ARG_2(A)
146
147 #define PRINT_MY_ARG_1(A) \
148 A +=
149
150 #ifdef _MSC_VER
151 #define PRINT_SECOND_ARG(argCount, B) \
152 MU_C2(PRINT_MY_ARG_,argCount) MU_LPAREN B )
153 #else
154 #define PRINT_SECOND_ARG(argCount, B) \
155 MU_C2(PRINT_MY_ARG_,argCount) (B)
156 #endif
157
158 #define FIRST_ARG(A, ...) A
159
160 #define CTEST_RUN_TEST_SUITE(...) \
161 do \
162 { \
163 extern C_LINKAGE const TEST_FUNCTION_DATA MU_C2(TestListHead_,FIRST_ARG(__VA_ARGS__)); \
164 MU_IF(MU_DIV2(MU_COUNT_ARG(__VA_ARGS__)),MU_FOR_EACH_1_COUNTED(PRINT_SECOND_ARG, __VA_ARGS__),) RunTests(&MU_C2(TestListHead_, FIRST_ARG(__VA_ARGS__)), MU_TOSTRING(FIRST_ARG(__VA_ARGS__))); \
165 } while ((void)0,0)
166
167 typedef const char* char_ptr;
168 typedef const wchar_t* wchar_ptr;
169 typedef void* void_ptr;
170 typedef long double long_double;
171 typedef unsigned long unsigned_long;
172
173 extern C_LINKAGE void int_ToString(char* string, size_t bufferSize, int val);
174 extern C_LINKAGE void char_ToString(char* string, size_t bufferSize, char val);
175 extern C_LINKAGE void short_ToString(char* string, size_t bufferSize, short val);
176 extern C_LINKAGE void long_ToString(char* string, size_t bufferSize, long val);
177 extern C_LINKAGE void size_t_ToString(char* string, size_t bufferSize, size_t val);
178 extern C_LINKAGE void float_ToString(char* string, size_t bufferSize, float val);
179 extern C_LINKAGE void double_ToString(char* string, size_t bufferSize, double val);
180 extern C_LINKAGE void long_double_ToString(char* string, size_t bufferSize, long double val);
181 extern C_LINKAGE void char_ptr_ToString(char* string, size_t bufferSize, const char* val);
182 extern C_LINKAGE void wchar_ptr_ToString(char* string, size_t bufferSize, const wchar_t* val);
183 extern C_LINKAGE void void_ptr_ToString(char* string, size_t bufferSize, const void* val);
184 extern C_LINKAGE void unsigned_long_ToString(char* string, size_t bufferSize, unsigned long val);
185 extern C_LINKAGE int int_Compare(int left, int right);
186 extern C_LINKAGE int char_Compare(char left, char right);
187 extern C_LINKAGE int short_Compare(short left, short right);
188 extern C_LINKAGE int long_Compare(long left, long right);
189 extern C_LINKAGE int size_t_Compare(size_t left, size_t right);
190 extern C_LINKAGE int float_Compare(float left, float right);
191 extern C_LINKAGE int double_Compare(double left, double right);
192 extern C_LINKAGE int long_double_Compare(long double left, long double right);
193 extern C_LINKAGE int char_ptr_Compare(const char* left, const char* right);
194 extern C_LINKAGE int wchar_ptr_Compare(const wchar_t* left, const wchar_t* right);
195 extern C_LINKAGE int void_ptr_Compare(const void * left, const void* right);
196 extern C_LINKAGE int unsigned_long_Compare(unsigned long left, unsigned long right);
197
198 #if defined CTEST_USE_STDINT
199 extern C_LINKAGE void uint8_t_ToString(char* string, size_t bufferSize, uint8_t val);
200 extern C_LINKAGE void int8_t_ToString(char* string, size_t bufferSize, int8_t val);
201 extern C_LINKAGE void uint16_t_ToString(char* string, size_t bufferSize, uint16_t val);
202 extern C_LINKAGE void int16_t_ToString(char* string, size_t bufferSize, int16_t val);
203 extern C_LINKAGE void uint32_t_ToString(char* string, size_t bufferSize, uint32_t val);
204 extern C_LINKAGE void int32_t_ToString(char* string, size_t bufferSize, int32_t val);
205 extern C_LINKAGE void uint64_t_ToString(char* string, size_t bufferSize, uint64_t val);
206 extern C_LINKAGE void int64_t_ToString(char* string, size_t bufferSize, int64_t val);
207 extern C_LINKAGE int uint8_t_Compare(uint8_t left, uint8_t right);
208 extern C_LINKAGE int int8_t_Compare(int8_t left, int8_t right);
209 extern C_LINKAGE int uint16_t_Compare(uint16_t left, uint16_t right);
210 extern C_LINKAGE int int16_t_Compare(int16_t left, int16_t right);
211 extern C_LINKAGE int uint32_t_Compare(uint32_t left, uint32_t right);
212 extern C_LINKAGE int int32_t_Compare(int32_t left, int32_t right);
213 extern C_LINKAGE int uint64_t_Compare(uint64_t left, uint64_t right);
214 extern C_LINKAGE int int64_t_Compare(int64_t left, int64_t right);
215 #endif
216
217 extern C_LINKAGE char* ctest_sprintf_char(const char* format, ...);
218 extern C_LINKAGE void ctest_sprintf_free(char* string);
219
220 #define CTEST_COMPARE(toStringType, cType) \
221 typedef cType toStringType; \
222 static int MU_C2(toStringType,_Compare)(toStringType left, toStringType right)
223
224 #define CTEST_TO_STRING(toStringType, cType, string, bufferSize, value) \
225 static void MU_C2(toStringType,_ToString)(char* string, size_t bufferSize, cType value)
226
227 // these are generic macros for formatting the optional message
228 // they can be used in all the ASSERT macros without repeating the code over and over again
229 #define GET_MESSAGE_FORMATTED(format, ...) \
230 MU_IF(MU_COUNT_ARG(__VA_ARGS__), ctest_sprintf_char(format, __VA_ARGS__);(void)(0 && printf(format, __VA_ARGS__)), ctest_sprintf_char(format));
231
232 #define GET_MESSAGE_FORMATTED_EMPTY(...) \
233 NULL
234
235 #define GET_MESSAGE(...) \
236 MU_IF(MU_COUNT_ARG(__VA_ARGS__), GET_MESSAGE_FORMATTED, GET_MESSAGE_FORMATTED_EMPTY)(__VA_ARGS__)
237
238 void do_jump(jmp_buf *exceptionJump, const volatile void* expected, const volatile void* actual);
239
240 #define CTEST_ASSERT_ARE_EQUAL(type, A, B, ...) \
241 do { \
242 const type A_value = (const type)(A); \
243 const type B_value = (const type)(B); \
244 char expectedString[1024]; \
245 char actualString[1024]; \
246 MU_C2(type,_ToString)(expectedString, sizeof(expectedString), A_value); /*one evaluation per argument*/ \
247 MU_C2(type,_ToString)(actualString, sizeof(actualString), B_value);/*one evaluation per argument*/ \
248 if (MU_C2(type,_Compare)(A_value, B_value)) \
249 { \
250 char* ctest_message = GET_MESSAGE(__VA_ARGS__); \
251 (void)printf(" Assert failed in line %d %s Expected: %s, Actual: %s\n", __LINE__, (ctest_message == NULL) ? "" : ctest_message, expectedString, actualString); \
252 ctest_sprintf_free(ctest_message); \
253 if (g_CurrentTestFunction != NULL) *g_CurrentTestFunction->TestResult = TEST_FAILED; \
254 do_jump(&g_ExceptionJump, expectedString, actualString); \
255 } \
256 } while (0)
257
258 #define CTEST_ASSERT_ARE_NOT_EQUAL(type, A, B, ...) \
259 do { \
260 const type A_value = (const type)(A); \
261 const type B_value = (const type)(B); \
262 char expectedString[1024]; \
263 char actualString[1024]; \
264 MU_C2(type,_ToString)(expectedString, sizeof(expectedString), A_value); /*one evaluation per argument*/ \
265 MU_C2(type,_ToString)(actualString, sizeof(actualString), B_value);/*one evaluation per argument*/ \
266 if (!MU_C2(type,_Compare)(A_value, B_value)) \
267 { \
268 char* ctest_message = GET_MESSAGE(__VA_ARGS__); \
269 (void)printf(" Assert failed in line %d: %s Expected: %s, Actual: %s\n", __LINE__, (ctest_message == NULL) ? "" : ctest_message, expectedString, actualString); \
270 ctest_sprintf_free(ctest_message); \
271 if (g_CurrentTestFunction != NULL) *g_CurrentTestFunction->TestResult = TEST_FAILED; \
272 do_jump(&g_ExceptionJump, "some expected string", actualString); \
273 } \
274 } while (0)
275
276 #define CTEST_ASSERT_IS_NULL(value, ...) \
277 do \
278 { \
279 const void* copy_of_value = (void*)(value); \
280 if (copy_of_value != NULL) \
281 { \
282 char* ctest_message = GET_MESSAGE(__VA_ARGS__); \
283 (void)printf(" Assert failed in line %d: NULL expected, actual: 0x%p. %s\n", __LINE__, copy_of_value, (ctest_message == NULL) ? "" : ctest_message); \
284 ctest_sprintf_free(ctest_message); \
285 if (g_CurrentTestFunction != NULL) *g_CurrentTestFunction->TestResult = TEST_FAILED; \
286 do_jump(&g_ExceptionJump, "expected it to be NULL (actual is the value)", copy_of_value); \
287 } \
288 } \
289 while(0)
290
291 #define CTEST_ASSERT_IS_NOT_NULL(value, ...) \
292 do \
293 { \
294 const void* copy_of_value = (void*)(value); \
295 if (copy_of_value == NULL) \
296 { \
297 char* ctest_message = GET_MESSAGE(__VA_ARGS__); \
298 (void)printf(" Assert failed in line %d: non-NULL expected. %s\n", __LINE__, (ctest_message == NULL) ? "" : ctest_message); \
299 ctest_sprintf_free(ctest_message); \
300 if (g_CurrentTestFunction != NULL) *g_CurrentTestFunction->TestResult = TEST_FAILED; \
301 do_jump(&g_ExceptionJump, "expected it not to be NULL (actual is value)", copy_of_value); \
302 } \
303 }while(0)
304
305 #define CTEST_ASSERT_IS_TRUE(expression, ...) \
306 do { \
307 int expression_is_false = ((expression)==0);/*one evaluation per argument*/ \
308 if (expression_is_false) \
309 { \
310 char* ctest_message = GET_MESSAGE(__VA_ARGS__); \
311 (void)printf(" Assert failed in line %d: Expression should be true: %s. %s\n", __LINE__, #expression, (ctest_message == NULL) ? "" : ctest_message); \
312 ctest_sprintf_free(ctest_message); \
313 if (g_CurrentTestFunction != NULL) *g_CurrentTestFunction->TestResult = TEST_FAILED; \
314 do_jump(&g_ExceptionJump, "expected it to be true", "but it wasn't"); \
315 } \
316 }while(0)
317
318 #define CTEST_ASSERT_IS_FALSE(expression, ...) \
319 do { \
320 int expression_is_true = ((expression)!=0);/*one evaluation per argument*/ \
321 if (expression_is_true) \
322 { \
323 char* ctest_message = GET_MESSAGE(__VA_ARGS__); \
324 (void)printf(" Assert failed in line %d: Expression should be false: %s. %s\n", __LINE__, #expression, (ctest_message == NULL) ? "" : ctest_message); \
325 ctest_sprintf_free(ctest_message); \
326 if (g_CurrentTestFunction != NULL) *g_CurrentTestFunction->TestResult = TEST_FAILED; \
327 do_jump(&g_ExceptionJump, "expected it to be false", "but it was true"); \
328 } \
329 }while(0)
330
331 #define CTEST_ASSERT_FAIL(...) \
332 do \
333 { \
334 char* ctest_message = GET_MESSAGE(__VA_ARGS__); \
335 (void)printf(" Assert failed in line %d: %s \n" , __LINE__, (ctest_message == NULL) ? "" : ctest_message); \
336 ctest_sprintf_free(ctest_message); \
337 if (g_CurrentTestFunction != NULL) *g_CurrentTestFunction->TestResult = TEST_FAILED; \
338 do_jump(&g_ExceptionJump, (void*)"nothing expected, 100% fail", (void*)"nothing actual, 100% fail"); \
339 ctest_sprintf_free(ctest_message); \
340 } \
341 while(0)
342
343 // this macro expands to the needed _ToString and _Compare functions for an enum,
344 // while using the macro utils ENUM_TO_STRING
345 #define CTEST_DEFINE_ENUM_TYPE(enum_name, ...) \
346 static void MU_C2(enum_name, _ToString)(char* dest, size_t bufferSize, enum_name enumValue) \
347 { \
348 (void)snprintf(dest, bufferSize, "%s", MU_ENUM_TO_STRING(enum_name, enumValue)); \
349 } \
350 static int MU_C2(enum_name, _Compare)(enum_name left, enum_name right) \
351 { \
352 return left != right; \
353 }
354
355 // this macro expands to the needed _ToString and _Compare functions for an enum defined with MU_DEFINE_ENUM_2,
356 // while using the macro utils ENUM_TO_STRING_2
357 #define CTEST_DEFINE_ENUM_2_TYPE(enum_name, ...) \
358 static void MU_C2(enum_name, _ToString)(char* dest, size_t bufferSize, enum_name enumValue) \
359 { \
360 (void)snprintf(dest, bufferSize, "%s", MU_ENUM_TO_STRING_2(enum_name, enumValue)); \
361 } \
362 static int MU_C2(enum_name, _Compare)(enum_name left, enum_name right) \
363 { \
364 return left != right; \
365 }
366
367 extern C_LINKAGE size_t RunTests(const TEST_FUNCTION_DATA* testListHead, const char* testSuiteName);
368
369 #ifdef __cplusplus
370 }
371 #endif
372
373 #endif