Use PRIdMAX instead of %jd formatting string
[libisds.git] / test / test.h
blobe04ce4b53013094664263653f5a339e7db07041c
1 #ifndef __ISDS_TEST_H
2 #define __ISDS_TEST_H
4 #ifndef _XOPEN_SOURCE
5 #define _XOPEN_SOURCE 700 /* strndup */
6 #endif
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <locale.h>
10 #include <stdint.h>
11 #include <inttypes.h>
12 #include <stdarg.h>
13 #include <string.h>
15 #include "test-tools.h"
17 char *unit_name, *reason = NULL;
18 unsigned int passed, failed;
19 void (*test_destructor_function)(void *) = NULL;
20 void *test_destructor_argument;
22 #define INIT_TEST(name) { \
23 setlocale(LC_ALL, "C"); \
24 unit_name = name; \
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; \
40 #define PASS_TEST { \
41 if (NULL != test_destructor_function) \
42 test_destructor_function(test_destructor_argument); \
43 return 0; \
44 } \
46 #define FAILURE_REASON(...) { \
47 if (!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); \
55 return 1; \
58 #define ABORT_UNIT(message) { \
59 printf("Unit %s procedure aborted: %s\n", unit_name, (message)); \
60 exit(EXIT_FAILURE); \
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__); \
69 if (status) { \
70 failed++; \
71 test_message = "failed"; \
72 } else { \
73 passed++; \
74 test_message = "passed"; \
75 } \
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))); \
83 if (!(pointer)) \
84 ABORT_UNIT("No enough memory"); \
87 #define TEST_FILL_STRING(pointer) { \
88 (pointer) = strdup("DATA"); \
89 if (!(pointer)) \
90 ABORT_UNIT("No enough memory"); \
93 #define TEST_FILL_INT(pointer) { \
94 (pointer) = malloc(sizeof(*(pointer))); \
95 if (!(pointer)) \
96 ABORT_UNIT("No enough memory"); \
97 *(pointer) = 42; \
101 #define STR(x) #x
103 #define TEST_POINTER_IS_NULL(x) { \
104 if (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) { \
120 if (strcmp(x, y)) \
121 FAIL_TEST(STR(y) " differs: expected=`%s' is=`%s'", x, y); \
125 #define TEST_INT_DUPLICITY(x, y) { \
126 if ((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) { \
133 if (*(x) != *(y)) \
134 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", *(x), *(y)); \
138 #define TEST_BOOLEAN_DUPLICITY(x, y) { \
139 if ((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) { \
146 if (*(x) != *(y)) \
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); \
178 #endif