5 #define _XOPEN_SOURCE 700 /* strndup */
15 #include "test-tools.h"
17 char *unit_name
, *reason
;
18 unsigned int passed
, failed
;
19 void (*test_destructor_function
)(void *);
20 void *test_destructor_argument
;
22 #define INIT_TEST(name) { \
23 setlocale(LC_ALL, "C"); \
25 passed = failed = 0; \
26 printf("Testing unit: %s\n", unit_name); \
29 #define SUM_TEST() { \
30 printf("Test results: unit = %s, passed = %u, failed = %u\n\n", \
31 unit_name, passed, failed); \
32 exit(failed ? EXIT_FAILURE : EXIT_SUCCESS ); \
35 #define TEST_DESTRUCTOR(function, argument) { \
36 test_destructor_function = function; \
37 test_destructor_argument = argument; \
41 if (NULL != test_destructor_function) \
42 test_destructor_function(test_destructor_argument); \
46 #define FAILURE_REASON(...) { \
48 test_asprintf(&reason, __VA_ARGS__); \
51 #define FAIL_TEST(...) { \
52 FAILURE_REASON(__VA_ARGS__); \
53 if (NULL != test_destructor_function) \
54 test_destructor_function(test_destructor_argument); \
58 #define ABORT_UNIT(message) { \
59 printf("Unit %s procedure aborted: %s\n", unit_name, (message)); \
64 #define TEST(name, function, ...) { \
65 const char *test_message; \
66 free(reason); reason = NULL; \
67 test_destructor_function = NULL; \
68 int status = (function)(__VA_ARGS__); \
71 test_message = "failed"; \
74 test_message = "passed"; \
76 printf("\t%s: %s\n", (name), test_message); \
77 if (status) printf("\t\treason: %s\n", reason); \
78 free(reason); reason = NULL; \
81 #define TEST_CALLOC(pointer) { \
82 (pointer) = calloc(1, sizeof(*(pointer))); \
84 ABORT_UNIT("No enough memory"); \
87 #define TEST_FILL_STRING(pointer) { \
88 (pointer) = strdup("DATA"); \
90 ABORT_UNIT("No enough memory"); \
93 #define TEST_FILL_INT(pointer) { \
94 (pointer) = malloc(sizeof(*(pointer))); \
96 ABORT_UNIT("No enough memory"); \
103 #define TEST_POINTER_IS_NULL(x) { \
105 FAIL_TEST(STR(x) " is not NULL and should be"); \
108 #define TEST_POINTER_DUPLICITY(x, y) { \
109 if (x == NULL && y != NULL) \
110 FAIL_TEST(STR(x) " is NULL, " STR(y) " is not NULL and should be"); \
111 if (x != NULL && y == NULL) \
112 FAIL_TEST(STR(x) " is not NULL, " STR(y) " is NULL and shouldn't be"); \
113 if (x == y && x != NULL) \
114 FAIL_TEST(STR(y) " is the same pointer as " STR(x)); \
117 #define TEST_STRING_DUPLICITY(x, y) { \
118 TEST_POINTER_DUPLICITY(x, y); \
119 if (x != NULL && y != NULL) { \
121 FAIL_TEST(STR(y) " differs: expected=`%s' is=`%s'", x, y); \
125 #define TEST_INT_DUPLICITY(x, y) { \
127 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", (x), (y)); \
130 #define TEST_INTPTR_DUPLICITY(x, y) { \
131 TEST_POINTER_DUPLICITY(x, y); \
132 if (x != NULL && y != NULL) { \
134 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", *(x), *(y)); \
138 #define TEST_BOOLEAN_DUPLICITY(x, y) { \
140 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!(x), !!(y)); \
143 #define TEST_BOOLEANPTR_DUPLICITY(x, y) { \
144 TEST_POINTER_DUPLICITY(x, y); \
145 if (x != NULL && y != NULL) { \
147 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!*(x), !!*(y)); \
151 #define TEST_TIMEVALPTR_DUPLICITY(x, y) { \
152 TEST_POINTER_DUPLICITY(x, y); \
153 if ((x) != NULL && (y) != NULL) { \
154 if ((x)->tv_sec != (y)->tv_sec) \
155 FAIL_TEST("struct timeval * differs in tv_sec: " \
156 "expected=%d, got=%d", (x)->tv_sec, (y)->tv_sec); \
157 if ((x)->tv_usec != (y)->tv_usec) \
158 FAIL_TEST("struct timeval * differs in tv_usec: " \
159 "expected=%" PRIdMAX ", got=%" PRIdMAX, (intmax_t)(x)->tv_usec, \
160 (intmax_t)(y)->tv_usec); \
164 #define TEST_TMPTR_DUPLICITY(x, y) { \
165 TEST_POINTER_DUPLICITY(x, y); \
166 if ((x) != NULL && (y) != NULL) { \
167 if ((x)->tm_year != (y)->tm_year) \
168 FAIL_TEST("struct tm * differs in tm_year: " \
169 "expected=%d, got=%d", (x)->tm_year, (y)->tm_year); \
170 if ((x)->tm_mon != (y)->tm_mon) \
171 FAIL_TEST("struct tm * differs in tm_mon: " \
172 "expected=%d, got=%d", (x)->tm_mon, (y)->tm_mon); \
173 if ((x)->tm_mday != (y)->tm_mday) \
174 FAIL_TEST("struct tm * differs in tm_mday: " \
175 "expected=%d, got=%d", (x)->tm_mday, (y)->tm_mday); \