5 #define _XOPEN_SOURCE 700 /* strndup */
15 #include "test-tools.h"
17 char *unit_name
, *reason
;
18 unsigned int passed
, failed
, skipped
;
19 void (*test_destructor_function
)(void *);
20 void *test_destructor_argument
;
22 #define INIT_TEST(name) { \
23 setlocale(LC_ALL, "C"); \
25 passed = failed = skipped = 0; \
26 printf("Testing unit: %s\n", unit_name); \
29 #define SUM_TEST() { \
30 printf("Test results: unit = %s, passed = %u, failed = %u, skipped = %u\n\n", \
31 unit_name, passed, failed, skipped); \
32 exit(failed ? EXIT_FAILURE : passed ? EXIT_SUCCESS : \
33 skipped ? 77 : EXIT_SUCCESS); \
36 #define TEST_DESTRUCTOR(function, argument) { \
37 test_destructor_function = function; \
38 test_destructor_argument = argument; \
42 if (NULL != test_destructor_function) \
43 test_destructor_function(test_destructor_argument); \
47 #define FAILURE_REASON(...) { \
49 test_asprintf(&reason, __VA_ARGS__); \
52 #define FAIL_TEST(...) { \
53 FAILURE_REASON(__VA_ARGS__); \
54 if (NULL != test_destructor_function) \
55 test_destructor_function(test_destructor_argument); \
59 #define SKIP_TEST(...) { \
60 FAILURE_REASON(__VA_ARGS__); \
61 if (NULL != test_destructor_function) \
62 test_destructor_function(test_destructor_argument); \
66 #define SKIP_TESTS(number, reason) { \
67 if (1 == (number)) { \
68 printf("\t%u test skipped: %s\n", (number), (reason)); \
70 printf("\t%u tests skipped: %s\n", (number), (reason)); \
72 skipped += (number); \
75 #define ABORT_UNIT(message) { \
76 printf("Unit %s procedure aborted: %s\n", unit_name, (message)); \
81 #define TEST(name, function, ...) { \
82 const char *test_message; \
83 free(reason); reason = NULL; \
84 test_destructor_function = NULL; \
85 int status = (function)(__VA_ARGS__); \
88 test_message = "skipped"; \
89 } else if (status) { \
91 test_message = "failed"; \
94 test_message = "passed"; \
96 printf("\t%s: %s\n", (name), test_message); \
97 if (status) printf("\t\treason: %s\n", reason); \
98 free(reason); reason = NULL; \
101 #define TEST_CALLOC(pointer) { \
102 (pointer) = calloc(1, sizeof(*(pointer))); \
104 ABORT_UNIT("No enough memory"); \
107 #define TEST_FILL_STRING(pointer) { \
108 (pointer) = strdup("DATA"); \
110 ABORT_UNIT("No enough memory"); \
113 #define TEST_FILL_INT(pointer) { \
114 (pointer) = malloc(sizeof(*(pointer))); \
116 ABORT_UNIT("No enough memory"); \
123 #define TEST_POINTER_IS_NULL(x) { \
125 FAIL_TEST(STR(x) " is not NULL and should be"); \
128 #define TEST_POINTER_DUPLICITY(x, y) { \
129 if (x == NULL && y != NULL) \
130 FAIL_TEST(STR(x) " is NULL, " STR(y) " is not NULL and should be"); \
131 if (x != NULL && y == NULL) \
132 FAIL_TEST(STR(x) " is not NULL, " STR(y) " is NULL and shouldn't be"); \
133 if (x == y && x != NULL) \
134 FAIL_TEST(STR(y) " is the same pointer as " STR(x)); \
137 #define TEST_STRING_DUPLICITY(x, y) { \
138 TEST_POINTER_DUPLICITY(x, y); \
139 if (x != NULL && y != NULL) { \
141 FAIL_TEST(STR(y) " differs: expected=`%s' is=`%s'", x, y); \
145 #define TEST_INT_DUPLICITY(x, y) { \
147 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", (x), (y)); \
150 #define TEST_INTPTR_DUPLICITY(x, y) { \
151 TEST_POINTER_DUPLICITY(x, y); \
152 if (x != NULL && y != NULL) { \
154 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", *(x), *(y)); \
158 #define TEST_BOOLEAN_DUPLICITY(x, y) { \
160 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!(x), !!(y)); \
163 #define TEST_BOOLEANPTR_DUPLICITY(x, y) { \
164 TEST_POINTER_DUPLICITY(x, y); \
165 if (x != NULL && y != NULL) { \
167 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!*(x), !!*(y)); \
171 #define TEST_TIMEVALPTR_DUPLICITY(x, y) { \
172 TEST_POINTER_DUPLICITY(x, y); \
173 if ((x) != NULL && (y) != NULL) { \
174 if ((x)->tv_sec != (y)->tv_sec) \
175 FAIL_TEST("struct timeval * differs in tv_sec: " \
176 "expected=%d, got=%d", (x)->tv_sec, (y)->tv_sec); \
177 if ((x)->tv_usec != (y)->tv_usec) \
178 FAIL_TEST("struct timeval * differs in tv_usec: " \
179 "expected=%" PRIdMAX ", got=%" PRIdMAX, (intmax_t)(x)->tv_usec, \
180 (intmax_t)(y)->tv_usec); \
184 #define TEST_TMPTR_DUPLICITY(x, y) { \
185 TEST_POINTER_DUPLICITY(x, y); \
186 if ((x) != NULL && (y) != NULL) { \
187 if ((x)->tm_year != (y)->tm_year) \
188 FAIL_TEST("struct tm * differs in tm_year: " \
189 "expected=%d, got=%d", (x)->tm_year, (y)->tm_year); \
190 if ((x)->tm_mon != (y)->tm_mon) \
191 FAIL_TEST("struct tm * differs in tm_mon: " \
192 "expected=%d, got=%d", (x)->tm_mon, (y)->tm_mon); \
193 if ((x)->tm_mday != (y)->tm_mday) \
194 FAIL_TEST("struct tm * differs in tm_mday: " \
195 "expected=%d, got=%d", (x)->tm_mday, (y)->tm_mday); \