tests: Fix building with GCC 10
[libisds.git] / test / test.h
blob1559a9d92e9db05385f9fba7ff8e1749642b340a
1 #ifndef __ISDS_TEST_H
2 #define __ISDS_TEST_H
4 #ifndef _XOPEN_SOURCE
5 #define _XOPEN_SOURCE 700 /* strndup */
6 #endif
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <locale.h>
10 #include <stdint.h>
11 #include <inttypes.h>
12 #include <stdarg.h>
13 #include <string.h>
15 #include "test-tools.h"
17 extern char *unit_name;
18 extern char *reason;
19 extern unsigned int passed, failed, skipped;
20 extern void (*test_destructor_function)(void *);
21 extern void *test_destructor_argument;
23 #define INIT_TEST(name) { \
24 setlocale(LC_ALL, "C"); \
25 unit_name = name; \
26 passed = failed = skipped = 0; \
27 printf("Testing unit: %s\n", unit_name); \
30 #define SUM_TEST() { \
31 printf("Test results: unit = %s, passed = %u, failed = %u, skipped = %u\n\n", \
32 unit_name, passed, failed, skipped); \
33 exit(failed ? EXIT_FAILURE : passed ? EXIT_SUCCESS : \
34 skipped ? 77 : EXIT_SUCCESS); \
37 #define TEST_DESTRUCTOR(function, argument) { \
38 test_destructor_function = function; \
39 test_destructor_argument = argument; \
42 #define PASS_TEST { \
43 if (NULL != test_destructor_function) \
44 test_destructor_function(test_destructor_argument); \
45 return 0; \
46 } \
48 #define FAILURE_REASON(...) { \
49 if (!reason) \
50 test_asprintf(&reason, __VA_ARGS__); \
53 #define FAIL_TEST(...) { \
54 FAILURE_REASON(__VA_ARGS__); \
55 if (NULL != test_destructor_function) \
56 test_destructor_function(test_destructor_argument); \
57 return 1; \
60 #define SKIP_TEST(...) { \
61 FAILURE_REASON(__VA_ARGS__); \
62 if (NULL != test_destructor_function) \
63 test_destructor_function(test_destructor_argument); \
64 return 77; \
67 #define SKIP_TESTS(number, reason) { \
68 if (1 == (number)) { \
69 printf("\t%u test skipped: %s\n", (number), (reason)); \
70 } else {\
71 printf("\t%u tests skipped: %s\n", (number), (reason)); \
72 } \
73 skipped += (number); \
76 #define ABORT_UNIT(message) { \
77 printf("Unit %s procedure aborted: %s\n", unit_name, (message)); \
78 exit(EXIT_FAILURE); \
82 #define TEST(name, function, ...) { \
83 const char *test_message; \
84 free(reason); reason = NULL; \
85 test_destructor_function = NULL; \
86 int status = (function)(__VA_ARGS__); \
87 if (77 == status) { \
88 skipped++; \
89 test_message = "skipped"; \
90 } else if (status) { \
91 failed++; \
92 test_message = "failed"; \
93 } else { \
94 passed++; \
95 test_message = "passed"; \
96 } \
97 printf("\t%s: %s\n", (name), test_message); \
98 if (status) printf("\t\treason: %s\n", reason); \
99 free(reason); reason = NULL; \
102 #define TEST_CALLOC(pointer) { \
103 (pointer) = calloc(1, sizeof(*(pointer))); \
104 if (!(pointer)) \
105 ABORT_UNIT("No enough memory"); \
108 #define TEST_FILL_STRING(pointer) { \
109 (pointer) = strdup("DATA"); \
110 if (!(pointer)) \
111 ABORT_UNIT("No enough memory"); \
114 #define TEST_FILL_INT(pointer) { \
115 (pointer) = malloc(sizeof(*(pointer))); \
116 if (!(pointer)) \
117 ABORT_UNIT("No enough memory"); \
118 *(pointer) = 42; \
122 #define STR(x) #x
124 #define TEST_POINTER_IS_NULL(x) { \
125 if (NULL != (x)) \
126 FAIL_TEST(STR(x) " is not NULL and should be"); \
129 #define TEST_POINTER_DUPLICITY(x, y) { \
130 if (x == NULL && y != NULL) \
131 FAIL_TEST(STR(x) " is NULL, " STR(y) " is not NULL and should be"); \
132 if (x != NULL && y == NULL) \
133 FAIL_TEST(STR(x) " is not NULL, " STR(y) " is NULL and shouldn't be"); \
134 if (x == y && x != NULL) \
135 FAIL_TEST(STR(y) " is the same pointer as " STR(x)); \
138 #define TEST_STRING_DUPLICITY(x, y) { \
139 TEST_POINTER_DUPLICITY(x, y); \
140 if (x != NULL && y != NULL) { \
141 if (strcmp(x, y)) \
142 FAIL_TEST(STR(y) " differs: expected=`%s' is=`%s'", x, y); \
146 #define TEST_INT_DUPLICITY(x, y) { \
147 if ((x) != (y)) \
148 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", (x), (y)); \
151 #define TEST_INTPTR_DUPLICITY(x, y) { \
152 TEST_POINTER_DUPLICITY(x, y); \
153 if (x != NULL && y != NULL) { \
154 if (*(x) != *(y)) \
155 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", *(x), *(y)); \
159 #define TEST_BOOLEAN_DUPLICITY(x, y) { \
160 if ((x) != (y)) \
161 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!(x), !!(y)); \
164 #define TEST_BOOLEANPTR_DUPLICITY(x, y) { \
165 TEST_POINTER_DUPLICITY(x, y); \
166 if (x != NULL && y != NULL) { \
167 if (*(x) != *(y)) \
168 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!*(x), !!*(y)); \
172 #define TEST_TIMEVALPTR_DUPLICITY(x, y) { \
173 TEST_POINTER_DUPLICITY(x, y); \
174 if ((x) != NULL && (y) != NULL) { \
175 if ((x)->tv_sec != (y)->tv_sec) \
176 FAIL_TEST("struct timeval * differs in tv_sec: " \
177 "expected=%d, got=%d", (x)->tv_sec, (y)->tv_sec); \
178 if ((x)->tv_usec != (y)->tv_usec) \
179 FAIL_TEST("struct timeval * differs in tv_usec: " \
180 "expected=%" PRIdMAX ", got=%" PRIdMAX, (intmax_t)(x)->tv_usec, \
181 (intmax_t)(y)->tv_usec); \
185 #define TEST_TMPTR_DUPLICITY(x, y) { \
186 TEST_POINTER_DUPLICITY(x, y); \
187 if ((x) != NULL && (y) != NULL) { \
188 if ((x)->tm_year != (y)->tm_year) \
189 FAIL_TEST("struct tm * differs in tm_year: " \
190 "expected=%d, got=%d", (x)->tm_year, (y)->tm_year); \
191 if ((x)->tm_mon != (y)->tm_mon) \
192 FAIL_TEST("struct tm * differs in tm_mon: " \
193 "expected=%d, got=%d", (x)->tm_mon, (y)->tm_mon); \
194 if ((x)->tm_mday != (y)->tm_mday) \
195 FAIL_TEST("struct tm * differs in tm_mday: " \
196 "expected=%d, got=%d", (x)->tm_mday, (y)->tm_mday); \
199 #endif