test: Add tests for isd_get_commercial_credit()
[libisds.git] / test / test.h
blob366c2a43bbbfb46efdd3d677bdfd9327e52a0a20
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 if ((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) { \
115 if (*(x) != *(y)) \
116 FAIL_TEST(STR(y) " differs: expected=%ld is=%ld", *(x), *(y)); \
120 #define TEST_BOOLEAN_DUPLICITY(x, y) { \
121 if ((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) { \
128 if (*(x) != *(y)) \
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); \
159 #endif