]> pd.if.org Git - ctap/blob - ctap.c
fix uninitialized pointer
[ctap] / 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 have, unsigned long want, const char *fmt, ...) {
175         va_list args;
176         int pass;
177
178         pass = have == want;
179         if (fmt) {
180                 va_start(args, fmt);
181                 okv(pass, fmt, args);
182                 va_end(args);
183         } else {
184                 ok(pass, NULL);
185         }
186         if (!pass) {
187                 diag("wanted: %ld", want);
188                 diag("got   : %ld", have);
189         }
190 }
191
192 void is_int(long have, long want, const char *fmt, ...) {
193         va_list args;
194         int pass;
195
196         pass = have == want;
197         if (fmt) {
198                 va_start(args, fmt);
199                 okv(pass, fmt, args);
200                 va_end(args);
201         } else {
202                 ok(pass, NULL);
203         }
204         if (!pass) {
205                 diag("wanted: %ld", want);
206                 diag("got   : %ld", have);
207         }
208 }
209
210 void is_double(double have, double want, double eps, const char *fmt, ...) {
211         int pass;
212         va_list args;
213
214         pass = fabs(want - have) <= eps;
215         if (fmt) {
216                 va_start(args, fmt);
217                 okv(pass, fmt, args);
218                 va_end(args);
219         } else {
220                 ok(pass, NULL);
221         }
222         if (!pass) {
223                 diag("wanted: %f", want);
224                 diag("got   : %f", have);
225         }
226 }
227
228 void is_compare(void *have, void *want,
229                 int (*cmp)(void *,void *),
230                 const char *fmt, ...
231                 ) {
232         int pass;
233         va_list args;
234
235         pass = !cmp(have, want);
236         if (fmt) {
237                 va_start(args, fmt);
238                 okv(pass, fmt, args);
239                 va_end(args);
240         } else {
241                 ok(pass, NULL);
242         }
243         /* no way to print them with out more functions
244         if (!pass) {
245                 diag("wanted: %s", want);
246                 diag("got   : %s", have);
247         }
248         */
249 }
250
251 void is_string(const char *have, const char *want, const char *fmt, ...) {
252         int pass;
253         va_list args;
254
255         pass = !strcmp(have,want);
256         if (fmt) {
257                 va_start(args, fmt);
258                 okv(pass, fmt, args);
259                 va_end(args);
260         } else {
261                 ok(pass, NULL);
262         }
263         if (!pass) {
264                 diag("wanted: %s", want);
265                 diag("got   : %s", have);
266         }
267 }