test: Add isds_event_free
[libisds.git] / test / test.h
blob13f378f463c950c529dad00729a0a9643c317b8f
1 #ifndef __ISDS_TEST_H
2 #define __ISDS_TEST_H
4 #define _XOPEN_SOURCE 500
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <locale.h>
8 #include <stdint.h>
9 #include <stdarg.h>
10 #include <string.h>
12 char *unit_name, *reason = NULL;
13 unsigned int passed, failed;
15 /* Print formated string into automtically reallocated @uffer.
16 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
17 * memory.
18 * @format format string as for printf(3)
19 * @ap list of variadic arguments, after call will be in udefined state
20 * @Returns number of bytes printed. In case of errror, -1 and NULL @buffer*/
21 int test_vasprintf(char **buffer, const char *format, va_list ap);
24 /* Print formated string into automtically reallocated @uffer.
25 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
26 * memory.
27 * @format format string as for printf(3)
28 * @... variadic arguments
29 * @Returns number of bytes printed. In case of errror, -1 and NULL @buffer*/
30 int test_asprintf(char **buffer, const char *format, ...);
33 /* I/O. Return 0, in case of error -1. */
34 int test_mmap_file(const char *file, int *fd, void **buffer, size_t *length);
35 int test_munmap_file(int fd, void *buffer, size_t length);
37 #define INIT_TEST(name) { \
38 setlocale(LC_ALL, "C"); \
39 unit_name = name; \
40 passed = failed = 0; \
41 printf("Testing unit: %s\n", unit_name); \
44 #define SUM_TEST() { \
45 printf("Test results: unit = %s, passed = %u, failed = %u\n\n", \
46 unit_name, passed, failed); \
47 exit(failed ? EXIT_FAILURE : EXIT_SUCCESS ); \
50 #define PASS_TEST { \
51 return 0; \
52 } \
54 #define FAILURE_REASON(...) { \
55 if (!reason) \
56 test_asprintf(&reason, __VA_ARGS__); \
59 #define FAIL_TEST(...) { \
60 FAILURE_REASON(__VA_ARGS__); \
61 return 1; \
64 #define ABORT_UNIT(message) { \
65 printf("Unit %s procedure aborted: %s\n", unit_name, (message)); \
66 exit(EXIT_FAILURE); \
70 #define TEST(name, function, ...) { \
71 const char *message; \
72 free(reason); reason = NULL; \
73 int status = (function)(__VA_ARGS__); \
74 if (status) { \
75 failed++; \
76 message = "failed"; \
77 } else { \
78 passed++; \
79 message = "passed"; \
80 } \
81 printf("\t%s: %s\n", (name), message); \
82 if (status) printf("\t\treason: %s\n", reason); \
83 free(reason); reason = NULL; \
86 #define TEST_CALLOC(pointer) { \
87 (pointer) = calloc(1, sizeof(*(pointer))); \
88 if (!(pointer)) \
89 ABORT_UNIT("No enough memory"); \
92 #define TEST_FILL_STRING(pointer) { \
93 (pointer) = strdup("DATA"); \
94 if (!(pointer)) \
95 ABORT_UNIT("No enough memory"); \
98 #define TEST_FILL_INT(pointer) { \
99 (pointer) = malloc(sizeof(*(pointer))); \
100 if (!(pointer)) \
101 ABORT_UNIT("No enough memory"); \
102 *(pointer) = 42; \
105 #endif