5 #define _XOPEN_SOURCE 700 /* strndup */
14 #include "test-tools.h"
16 char *unit_name
, *reason
= NULL
;
17 unsigned int passed
, failed
;
19 #define INIT_TEST(name) { \
20 setlocale(LC_ALL, "C"); \
22 passed = failed = 0; \
23 printf("Testing unit: %s\n", unit_name); \
26 #define SUM_TEST() { \
27 printf("Test results: unit = %s, passed = %u, failed = %u\n\n", \
28 unit_name, passed, failed); \
29 exit(failed ? EXIT_FAILURE : EXIT_SUCCESS ); \
36 #define FAILURE_REASON(...) { \
38 test_asprintf(&reason, __VA_ARGS__); \
41 #define FAIL_TEST(...) { \
42 FAILURE_REASON(__VA_ARGS__); \
46 #define ABORT_UNIT(message) { \
47 printf("Unit %s procedure aborted: %s\n", unit_name, (message)); \
52 #define TEST(name, function, ...) { \
53 const char *test_message; \
54 free(reason); reason = NULL; \
55 int status = (function)(__VA_ARGS__); \
58 test_message = "failed"; \
61 test_message = "passed"; \
63 printf("\t%s: %s\n", (name), test_message); \
64 if (status) printf("\t\treason: %s\n", reason); \
65 free(reason); reason = NULL; \
68 #define TEST_CALLOC(pointer) { \
69 (pointer) = calloc(1, sizeof(*(pointer))); \
71 ABORT_UNIT("No enough memory"); \
74 #define TEST_FILL_STRING(pointer) { \
75 (pointer) = strdup("DATA"); \
77 ABORT_UNIT("No enough memory"); \
80 #define TEST_FILL_INT(pointer) { \
81 (pointer) = malloc(sizeof(*(pointer))); \
83 ABORT_UNIT("No enough memory"); \
90 #define TEST_POINTER_DUPLICITY(x, y) { \
91 if (x == NULL && y != NULL) \
92 FAIL_TEST(STR(x) " is NULL, " STR(y) " is not NULL and should be"); \
93 if (x != NULL && y == NULL) \
94 FAIL_TEST(STR(x) " is not NULL, " STR(y) " is NULL and shouldn't be"); \
95 if (x == y && x != NULL) \
96 FAIL_TEST(STR(y) " is the same pointer as " STR(x)); \
99 #define TEST_STRING_DUPLICITY(x, y) { \
100 TEST_POINTER_DUPLICITY(x, y); \
101 if (x != NULL && y != NULL) { \
103 FAIL_TEST(STR(y) " differs: expected=`%s' is=`%s'", x, y); \
107 #define TEST_INT_DUPLICITY(x, y) { \
109 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", (x), (y)); \
112 #define TEST_INTPTR_DUPLICITY(x, y) { \
113 TEST_POINTER_DUPLICITY(x, y); \
114 if (x != NULL && y != NULL) { \
116 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", *(x), *(y)); \
120 #define TEST_BOOLEAN_DUPLICITY(x, y) { \
122 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!(x), !!(y)); \
125 #define TEST_BOOLEANPTR_DUPLICITY(x, y) { \
126 TEST_POINTER_DUPLICITY(x, y); \
127 if (x != NULL && y != NULL) { \
129 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!*(x), !!*(y)); \
133 #define TEST_TIMEVALPTR_DUPLICITY(x, y) { \
134 TEST_POINTER_DUPLICITY(x, y); \
135 if ((x) != NULL && (y) != NULL) { \
136 if ((x)->tv_sec != (y)->tv_sec) \
137 FAIL_TEST("struct timeval * differs in tv_sec: " \
138 "expected=%d, got=%d", (x)->tv_sec, (y)->tv_sec); \
139 if ((x)->tv_usec != (y)->tv_usec) \
140 FAIL_TEST("struct timeval * differs in tv_usec: " \
141 "expected=%d, got=%d", (x)->tv_usec, (y)->tv_usec); \
145 #define TEST_TMPTR_DUPLICITY(x, y) { \
146 TEST_POINTER_DUPLICITY(x, y); \
147 if ((x) != NULL && (y) != NULL) { \
148 if ((x)->tm_year != (y)->tm_year) \
149 FAIL_TEST("struct tm * differs in tm_year: " \
150 "expected=%d, got=%d", (x)->tm_year, (y)->tm_year); \
151 if ((x)->tm_mon != (y)->tm_mon) \
152 FAIL_TEST("struct tm * differs in tm_mon: " \
153 "expected=%d, got=%d", (x)->tm_mon, (y)->tm_mon); \
154 if ((x)->tm_mday != (y)->tm_mday) \
155 FAIL_TEST("struct tm * differs in tm_mday: " \
156 "expected=%d, got=%d", (x)->tm_mday, (y)->tm_mday); \