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) { \
108 TEST_POINTER_DUPLICITY(x, y); \
109 if (x != NULL && y != NULL) { \
111 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", *(x), *(y)); \
115 #define TEST_BOOLEAN_DUPLICITY(x, y) { \
116 TEST_POINTER_DUPLICITY(x, y); \
117 if (x != NULL && y != NULL) { \
119 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!*(x), !!*(y)); \