]> pd.if.org Git - dumppoints/blob - t/ctap.c
added email address to README
[dumppoints] / t / ctap.c
1 #include <stdarg.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <math.h>
7
8 #include "ctap.h"
9
10 /* global variable testnum? */
11 static int test = 0; /* the test number */
12 static int planned = 0;
13 static int intodo = 0;
14
15 void begin_todo(void) {
16         intodo = 1;
17 }
18
19 void end_todo(void) {
20         intodo = 0;
21 }
22
23 void plan(int tests) {
24         test = 0;
25         planned = tests;
26         printf("1..%d\n", tests);
27 }
28
29 static void print_lazy_plan(void) {
30         printf("1..%d\n", test);
31         fflush(stdout);
32 }
33
34 void plan_lazy(void) {
35         test = 0;
36         planned = 0;
37         atexit(print_lazy_plan);
38 }
39
40 void skip_all(const char *why, ...) {
41         printf("1..0");
42         if (why) {
43                 va_list args;
44                 printf(" # SKIP ");
45                 va_start(args, why);
46                 vfprintf(stdout, why, args);
47                 va_end(args);
48         }
49         printf("\n");
50 }
51
52 static void vfmtline(int pass, const char *directive, const char *fmt, va_list args) {
53         printf("%sok %d", pass ? "" : "not ", ++test);
54         if (fmt && !directive) {
55                 printf(" -");
56         }
57         if (directive) {
58                 printf(" # %s", directive);
59         }
60         if (fmt) {
61                 printf(" ");
62                 vfprintf(stdout, fmt, args);
63         }
64         printf("\n");
65 }
66
67 #if 0
68 static void fmtline(int pass, const char *info, const char *fmt, ...) {
69         va_list args;
70
71         va_start(args,fmt);
72         vfmtline(pass, info, fmt, args);
73         va_end(args);
74 }
75 #endif
76
77 void okv(int pass, const char *fmt, va_list args) {
78         vfmtline(pass, intodo ? "TODO" : 0, fmt, args);
79 }
80
81 void ok(int pass, char *fmt, ...) {
82         va_list args;
83
84         va_start(args, fmt);
85         okv(pass, fmt, args);
86         va_end(args);
87 }
88
89 void ok_block(unsigned long count, int pass, const char *fmt, ...) {
90         va_list args;
91         va_list copy;
92
93         if (count == 0) {
94                 return;
95         }
96
97         va_start(args, fmt);
98         while (count--) {
99                 va_copy(copy, args);
100                 okv(pass, fmt, copy);
101                 va_end(copy);
102         }
103         va_end(args);
104 }
105
106 void skip(const char *why, ...) {
107         va_list args;
108
109         va_start(args, why);
110         vfmtline(1, "SKIP", why, args);
111         va_end(args);
112 }
113
114 void skip_block(unsigned long count, const char *why, ...) {
115         va_list args;
116         va_list copy;
117         va_start(args, why);
118
119         while (count--) {
120                 va_copy(copy, args);
121                 vfmtline(1, "SKIP", why, args);
122                 va_end(copy);
123         }
124
125         va_end(args);
126 }
127
128 void bail(const char *fmt, ...) {
129         va_list args;
130         printf("Bail out!");
131         if (fmt) {
132                 va_start(args, fmt);
133                 vfprintf(stdout, fmt, args);
134                 va_end(args);
135         }
136         printf("\n");
137         fflush(stdout);
138         exit(1);
139 }
140
141 void sysbail(const char *fmt, ...) {
142         va_list args;
143         printf("Bail out!");
144         if (fmt) {
145                 va_start(args, fmt);
146                 vfprintf(stdout, fmt, args);
147                 va_end(args);
148         }
149         printf(": %s\n", strerror(errno));
150         fflush(stdout);
151         exit(1);
152 }
153
154 void sysdiag(const char *fmt, ...) {
155         va_list args;
156         if (!fmt) { return; }
157         printf("# ");
158         va_start(args, fmt);
159         vfprintf(stdout, fmt, args);
160         va_end(args);
161         printf(": %s\n", strerror(errno));
162 }
163
164 void diag(const char *fmt, ...) {
165         va_list args;
166         if (!fmt) { return; }
167         printf("# ");
168         va_start(args, fmt);
169         vfprintf(stdout, fmt, args);
170         va_end(args);
171         printf("\n");
172 }
173
174 void is_hex(unsigned long wanted, unsigned long seen, const char *fmt, ...) {
175         va_list args;
176         if (fmt) {
177                 va_start(args, fmt);
178                 okv(wanted == seen, fmt, args);
179                 va_end(args);
180         } else {
181                 ok(wanted == seen, NULL);
182         }
183         if (wanted != seen) {
184                 diag("wanted: %ld", wanted);
185                 diag("got   : %ld", seen);
186         }
187 }
188
189
190 void is_int(long wanted, long seen, const char *fmt, ...) {
191         va_list args;
192         if (fmt) {
193                 va_start(args, fmt);
194                 okv(wanted == seen, fmt, args);
195                 va_end(args);
196         } else {
197                 ok(wanted == seen, NULL);
198         }
199         if (wanted != seen) {
200                 diag("wanted: %ld", wanted);
201                 diag("got   : %ld", seen);
202         }
203 }
204
205 void is_double(double wanted, double seen, double eps, const char *fmt, ...) {
206         int pass;
207         va_list args;
208
209         pass = fabs(wanted - seen) <= eps;
210         if (fmt) {
211                 va_start(args, fmt);
212                 okv(pass, fmt, args);
213                 va_end(args);
214         } else {
215                 ok(wanted == seen, NULL);
216         }
217         if (!pass) {
218                 diag("wanted: %f", wanted);
219                 diag("got   : %f", seen);
220         }
221 }
222
223 void is_string(const char *wanted, const char *seen, const char *fmt, ...) {
224         int pass;
225         va_list args;
226
227         pass = !strcmp(wanted,seen);
228         if (fmt) {
229                 va_start(args, fmt);
230                 okv(pass, fmt, args);
231                 va_end(args);
232         } else {
233                 ok(wanted == seen, NULL);
234         }
235         if (!pass) {
236                 diag("wanted: %s", wanted);
237                 diag("got   : %s", seen);
238         }
239 }