test/offline/compute_hash: Skip a test if an algorithm is not supported
[libisds.git] / test / test.h
blob63f22f9d55456c1499a4c3356ff9e301e35ecc9b
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;
18 unsigned int passed, failed, skipped;
19 void (*test_destructor_function)(void *);
20 void *test_destructor_argument;
22 #define INIT_TEST(name) { \
23 setlocale(LC_ALL, "C"); \
24 unit_name = name; \
25 passed = failed = skipped = 0; \
26 printf("Testing unit: %s\n", unit_name); \
29 #define SUM_TEST() { \
30 printf("Test results: unit = %s, passed = %u, failed = %u, skipped = %u\n\n", \
31 unit_name, passed, failed, skipped); \
32 exit(failed ? EXIT_FAILURE : passed ? EXIT_SUCCESS : \
33 skipped ? 77 : EXIT_SUCCESS); \
36 #define TEST_DESTRUCTOR(function, argument) { \
37 test_destructor_function = function; \
38 test_destructor_argument = argument; \
41 #define PASS_TEST { \
42 if (NULL != test_destructor_function) \
43 test_destructor_function(test_destructor_argument); \
44 return 0; \
45 } \
47 #define FAILURE_REASON(...) { \
48 if (!reason) \
49 test_asprintf(&reason, __VA_ARGS__); \
52 #define FAIL_TEST(...) { \
53 FAILURE_REASON(__VA_ARGS__); \
54 if (NULL != test_destructor_function) \
55 test_destructor_function(test_destructor_argument); \
56 return 1; \
59 #define SKIP_TEST(...) { \
60 FAILURE_REASON(__VA_ARGS__); \
61 if (NULL != test_destructor_function) \
62 test_destructor_function(test_destructor_argument); \
63 return 77; \
66 #define SKIP_TESTS(number, reason) { \
67 if (1 == (number)) { \
68 printf("\t%u test skipped: %s\n", (number), (reason)); \
69 } else {\
70 printf("\t%u tests skipped: %s\n", (number), (reason)); \
71 } \
72 skipped += (number); \
75 #define ABORT_UNIT(message) { \
76 printf("Unit %s procedure aborted: %s\n", unit_name, (message)); \
77 exit(EXIT_FAILURE); \
81 #define TEST(name, function, ...) { \
82 const char *test_message; \
83 free(reason); reason = NULL; \
84 test_destructor_function = NULL; \
85 int status = (function)(__VA_ARGS__); \
86 if (77 == status) { \
87 skipped++; \
88 test_message = "skipped"; \
89 } else if (status) { \
90 failed++; \
91 test_message = "failed"; \
92 } else { \
93 passed++; \
94 test_message = "passed"; \
95 } \
96 printf("\t%s: %s\n", (name), test_message); \
97 if (status) printf("\t\treason: %s\n", reason); \
98 free(reason); reason = NULL; \
101 #define TEST_CALLOC(pointer) { \
102 (pointer) = calloc(1, sizeof(*(pointer))); \
103 if (!(pointer)) \
104 ABORT_UNIT("No enough memory"); \
107 #define TEST_FILL_STRING(pointer) { \
108 (pointer) = strdup("DATA"); \
109 if (!(pointer)) \
110 ABORT_UNIT("No enough memory"); \
113 #define TEST_FILL_INT(pointer) { \
114 (pointer) = malloc(sizeof(*(pointer))); \
115 if (!(pointer)) \
116 ABORT_UNIT("No enough memory"); \
117 *(pointer) = 42; \
121 #define STR(x) #x
123 #define TEST_POINTER_IS_NULL(x) { \
124 if (NULL != (x)) \
125 FAIL_TEST(STR(x) " is not NULL and should be"); \
128 #define TEST_POINTER_DUPLICITY(x, y) { \
129 if (x == NULL && y != NULL) \
130 FAIL_TEST(STR(x) " is NULL, " STR(y) " is not NULL and should be"); \
131 if (x != NULL && y == NULL) \
132 FAIL_TEST(STR(x) " is not NULL, " STR(y) " is NULL and shouldn't be"); \
133 if (x == y && x != NULL) \
134 FAIL_TEST(STR(y) " is the same pointer as " STR(x)); \
137 #define TEST_STRING_DUPLICITY(x, y) { \
138 TEST_POINTER_DUPLICITY(x, y); \
139 if (x != NULL && y != NULL) { \
140 if (strcmp(x, y)) \
141 FAIL_TEST(STR(y) " differs: expected=`%s' is=`%s'", x, y); \
145 #define TEST_INT_DUPLICITY(x, y) { \
146 if ((x) != (y)) \
147 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", (x), (y)); \
150 #define TEST_INTPTR_DUPLICITY(x, y) { \
151 TEST_POINTER_DUPLICITY(x, y); \
152 if (x != NULL && y != NULL) { \
153 if (*(x) != *(y)) \
154 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", *(x), *(y)); \
158 #define TEST_BOOLEAN_DUPLICITY(x, y) { \
159 if ((x) != (y)) \
160 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!(x), !!(y)); \
163 #define TEST_BOOLEANPTR_DUPLICITY(x, y) { \
164 TEST_POINTER_DUPLICITY(x, y); \
165 if (x != NULL && y != NULL) { \
166 if (*(x) != *(y)) \
167 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!*(x), !!*(y)); \
171 #define TEST_TIMEVALPTR_DUPLICITY(x, y) { \
172 TEST_POINTER_DUPLICITY(x, y); \
173 if ((x) != NULL && (y) != NULL) { \
174 if ((x)->tv_sec != (y)->tv_sec) \
175 FAIL_TEST("struct timeval * differs in tv_sec: " \
176 "expected=%d, got=%d", (x)->tv_sec, (y)->tv_sec); \
177 if ((x)->tv_usec != (y)->tv_usec) \
178 FAIL_TEST("struct timeval * differs in tv_usec: " \
179 "expected=%" PRIdMAX ", got=%" PRIdMAX, (intmax_t)(x)->tv_usec, \
180 (intmax_t)(y)->tv_usec); \
184 #define TEST_TMPTR_DUPLICITY(x, y) { \
185 TEST_POINTER_DUPLICITY(x, y); \
186 if ((x) != NULL && (y) != NULL) { \
187 if ((x)->tm_year != (y)->tm_year) \
188 FAIL_TEST("struct tm * differs in tm_year: " \
189 "expected=%d, got=%d", (x)->tm_year, (y)->tm_year); \
190 if ((x)->tm_mon != (y)->tm_mon) \
191 FAIL_TEST("struct tm * differs in tm_mon: " \
192 "expected=%d, got=%d", (x)->tm_mon, (y)->tm_mon); \
193 if ((x)->tm_mday != (y)->tm_mday) \
194 FAIL_TEST("struct tm * differs in tm_mday: " \
195 "expected=%d, got=%d", (x)->tm_mday, (y)->tm_mday); \
198 #endif