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