5 #define _XOPEN_SOURCE 700 /* strndup */
14 #include "test-tools.h"
16 char *unit_name
, *reason
= NULL
;
17 unsigned int passed
, failed
;
18 void (*test_destructor_function
)(void *) = NULL
;
19 void *test_destructor_argument
;
21 #define INIT_TEST(name) { \
22 setlocale(LC_ALL, "C"); \
24 passed = failed = 0; \
25 printf("Testing unit: %s\n", unit_name); \
28 #define SUM_TEST() { \
29 printf("Test results: unit = %s, passed = %u, failed = %u\n\n", \
30 unit_name, passed, failed); \
31 exit(failed ? EXIT_FAILURE : EXIT_SUCCESS ); \
34 #define TEST_DESTRUCTOR(function, argument) { \
35 test_destructor_function = function; \
36 test_destructor_argument = argument; \
40 if (NULL != test_destructor_function) \
41 test_destructor_function(test_destructor_argument); \
45 #define FAILURE_REASON(...) { \
47 test_asprintf(&reason, __VA_ARGS__); \
50 #define FAIL_TEST(...) { \
51 FAILURE_REASON(__VA_ARGS__); \
52 if (NULL != test_destructor_function) \
53 test_destructor_function(test_destructor_argument); \
57 #define ABORT_UNIT(message) { \
58 printf("Unit %s procedure aborted: %s\n", unit_name, (message)); \
63 #define TEST(name, function, ...) { \
64 const char *test_message; \
65 free(reason); reason = NULL; \
66 test_destructor_function = NULL; \
67 int status = (function)(__VA_ARGS__); \
70 test_message = "failed"; \
73 test_message = "passed"; \
75 printf("\t%s: %s\n", (name), test_message); \
76 if (status) printf("\t\treason: %s\n", reason); \
77 free(reason); reason = NULL; \
80 #define TEST_CALLOC(pointer) { \
81 (pointer) = calloc(1, sizeof(*(pointer))); \
83 ABORT_UNIT("No enough memory"); \
86 #define TEST_FILL_STRING(pointer) { \
87 (pointer) = strdup("DATA"); \
89 ABORT_UNIT("No enough memory"); \
92 #define TEST_FILL_INT(pointer) { \
93 (pointer) = malloc(sizeof(*(pointer))); \
95 ABORT_UNIT("No enough memory"); \
102 #define TEST_POINTER_IS_NULL(x) { \
104 FAIL_TEST(STR(x) " is not NULL and should be"); \
107 #define TEST_POINTER_DUPLICITY(x, y) { \
108 if (x == NULL && y != NULL) \
109 FAIL_TEST(STR(x) " is NULL, " STR(y) " is not NULL and should be"); \
110 if (x != NULL && y == NULL) \
111 FAIL_TEST(STR(x) " is not NULL, " STR(y) " is NULL and shouldn't be"); \
112 if (x == y && x != NULL) \
113 FAIL_TEST(STR(y) " is the same pointer as " STR(x)); \
116 #define TEST_STRING_DUPLICITY(x, y) { \
117 TEST_POINTER_DUPLICITY(x, y); \
118 if (x != NULL && y != NULL) { \
120 FAIL_TEST(STR(y) " differs: expected=`%s' is=`%s'", x, y); \
124 #define TEST_INT_DUPLICITY(x, y) { \
126 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", (x), (y)); \
129 #define TEST_INTPTR_DUPLICITY(x, y) { \
130 TEST_POINTER_DUPLICITY(x, y); \
131 if (x != NULL && y != NULL) { \
133 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", *(x), *(y)); \
137 #define TEST_BOOLEAN_DUPLICITY(x, y) { \
139 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!(x), !!(y)); \
142 #define TEST_BOOLEANPTR_DUPLICITY(x, y) { \
143 TEST_POINTER_DUPLICITY(x, y); \
144 if (x != NULL && y != NULL) { \
146 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!*(x), !!*(y)); \
150 #define TEST_TIMEVALPTR_DUPLICITY(x, y) { \
151 TEST_POINTER_DUPLICITY(x, y); \
152 if ((x) != NULL && (y) != NULL) { \
153 if ((x)->tv_sec != (y)->tv_sec) \
154 FAIL_TEST("struct timeval * differs in tv_sec: " \
155 "expected=%d, got=%d", (x)->tv_sec, (y)->tv_sec); \
156 if ((x)->tv_usec != (y)->tv_usec) \
157 FAIL_TEST("struct timeval * differs in tv_usec: " \
158 "expected=%jd, got=%jd", (intmax_t)(x)->tv_usec, \
159 (intmax_t)(y)->tv_usec); \
163 #define TEST_TMPTR_DUPLICITY(x, y) { \
164 TEST_POINTER_DUPLICITY(x, y); \
165 if ((x) != NULL && (y) != NULL) { \
166 if ((x)->tm_year != (y)->tm_year) \
167 FAIL_TEST("struct tm * differs in tm_year: " \
168 "expected=%d, got=%d", (x)->tm_year, (y)->tm_year); \
169 if ((x)->tm_mon != (y)->tm_mon) \
170 FAIL_TEST("struct tm * differs in tm_mon: " \
171 "expected=%d, got=%d", (x)->tm_mon, (y)->tm_mon); \
172 if ((x)->tm_mday != (y)->tm_mday) \
173 FAIL_TEST("struct tm * differs in tm_mday: " \
174 "expected=%d, got=%d", (x)->tm_mday, (y)->tm_mday); \