Ported to MinGW
[libisds.git] / test / test.h
blobff882d94087b27a468c4041539f9e878588b79e4
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 <stdarg.h>
12 #include <string.h>
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"); \
21 unit_name = name; \
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 ); \
32 #define PASS_TEST { \
33 return 0; \
34 } \
36 #define FAILURE_REASON(...) { \
37 if (!reason) \
38 test_asprintf(&reason, __VA_ARGS__); \
41 #define FAIL_TEST(...) { \
42 FAILURE_REASON(__VA_ARGS__); \
43 return 1; \
46 #define ABORT_UNIT(message) { \
47 printf("Unit %s procedure aborted: %s\n", unit_name, (message)); \
48 exit(EXIT_FAILURE); \
52 #define TEST(name, function, ...) { \
53 const char *test_message; \
54 free(reason); reason = NULL; \
55 int status = (function)(__VA_ARGS__); \
56 if (status) { \
57 failed++; \
58 test_message = "failed"; \
59 } else { \
60 passed++; \
61 test_message = "passed"; \
62 } \
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))); \
70 if (!(pointer)) \
71 ABORT_UNIT("No enough memory"); \
74 #define TEST_FILL_STRING(pointer) { \
75 (pointer) = strdup("DATA"); \
76 if (!(pointer)) \
77 ABORT_UNIT("No enough memory"); \
80 #define TEST_FILL_INT(pointer) { \
81 (pointer) = malloc(sizeof(*(pointer))); \
82 if (!(pointer)) \
83 ABORT_UNIT("No enough memory"); \
84 *(pointer) = 42; \
88 #define STR(x) #x
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) { \
102 if (strcmp(x, y)) \
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) { \
110 if (*(x) != *(y)) \
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) { \
118 if (*(x) != *(y)) \
119 FAIL_TEST(STR(y) " differs: expected=%d is=%d", !!*(x), !!*(y)); \
123 #endif