]> begriffs open source - libtap/blob - tap.h
added va_copy definition for systems like winblows that don't have it
[libtap] / tap.h
1 #ifndef __TAP_H__
2 #define __TAP_H__
3
4 #ifndef va_copy
5 #define va_copy(d,s) ((d) = (s))
6 #endif
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <stdarg.h>
11
12 #define NO_PLAN          -1
13 #define ok(...)          ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
14
15 #ifdef _WIN32
16 #define pass(...)        ok_at_loc(__FILE__, __LINE__, 1, __VA_ARGS__, NULL)
17 #define fail(...)        ok_at_loc(__FILE__, __LINE__, 0, __VA_ARGS__, NULL)
18 #else
19 #define pass(...)        ok(1, ## __VA_ARGS__)
20 #define fail(...)        ok(0, ## __VA_ARGS__)
21 #endif
22
23 #define is(...)          is_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
24 #define isnt(...)        isnt_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
25 #define cmp_ok(...)      cmp_ok_at_loc(__FILE__, __LINE__, __VA_ARGS__, NULL)
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 int     vok_at_loc      (const char *file, int line, int test, const char *fmt,
31                          va_list args);
32 void    plan            (int tests);
33 int     ok_at_loc       (const char *file, int line, int test, const char *fmt,
34                          ...);
35 int     diag            (const char *fmt, ...);
36 int     note            (const char *fmt, ...);
37 int     exit_status     (void);
38 void    skippy          (int n, const char *fmt, ...);
39 void    ctodo           (int ignore, const char *fmt, ...);
40 void    cendtodo        (void);
41 int     is_at_loc       (const char *file, int line, const char *got,
42                          const char *expected, const char *fmt, ...);
43 int     isnt_at_loc     (const char *file, int line, const char *got,
44                          const char *expected, const char *fmt, ...);
45 int     cmp_ok_at_loc   (const char *file, int line, int a, const char *op,
46                          int b, const char *fmt, ...);
47 #ifdef __cplusplus
48 }
49 #endif
50
51
52 #ifdef _WIN32
53 #define like(...)   skippy(1, "like is not implemented on MSWin32")
54 #define unlike(...) like()
55 #else
56 #define like(...)   like_at_loc(1, __FILE__, __LINE__, __VA_ARGS__, NULL)
57 #define unlike(...) like_at_loc(0, __FILE__, __LINE__, __VA_ARGS__, NULL)
58 #ifdef __cplusplus
59 extern "C"
60 #endif
61 int     like_at_loc     (int for_match, const char *file, int line,
62                          const char *got, const char *expected,
63                          const char *fmt, ...);
64 #endif
65
66 #define skip(test, ...)  do {if (test) {skippy(__VA_ARGS__, NULL); break;}
67 #define endskip          } while (0)
68
69 #define todo(...)        ctodo(0, ## __VA_ARGS__, NULL)
70 #define endtodo          cendtodo()
71
72 #define dies_ok(code, ...)  dies_ok_common(code, 1, ## __VA_ARGS__)
73 #define lives_ok(code, ...) dies_ok_common(code, 0, ## __VA_ARGS__)
74
75 #ifdef _WIN32
76 #define dies_ok_common(...) \
77     skippy(1, "Death detection is not supported on MSWin32")
78 #else
79 #include <unistd.h>
80 #include <sys/types.h>
81 #include <sys/wait.h>
82 #ifdef __cplusplus
83 extern "C"
84 #endif
85 int tap_test_died (int status);
86 #define dies_ok_common(code, for_death, ...)                \
87     do {                                                    \
88         tap_test_died(1);                                   \
89         int cpid = fork();                                  \
90         switch (cpid) {                                     \
91         case -1:                                            \
92             perror("fork error");                           \
93             exit(EXIT_FAILURE);                             \
94         case 0: /* child  */                                \
95             close(1); close(2);                             \
96             code                                            \
97             tap_test_died(0);                               \
98             exit(EXIT_SUCCESS);                             \
99         }                                                   \
100         if (waitpid(cpid, NULL, 0) < 0) {                   \
101             perror("waitpid error");                        \
102             exit(EXIT_FAILURE);                             \
103         }                                                   \
104         int it_died = tap_test_died(0);                     \
105         if (!it_died) {code}                                \
106         ok(for_death ? it_died : !it_died, ## __VA_ARGS__); \
107     } while (0)
108 #endif
109 #endif