Imported File#ftype spec from rubyspecs.
[rbx.git] / shotgun / external_libs / libbstring / testaux.c
blobf5ec187fd74dfc0dd730cc488f40741d5afbcded
1 /*
2 * This source file is part of the bstring string library. This code was
3 * written by Paul Hsieh in 2002-2006, and is covered by the BSD open source
4 * license. Refer to the accompanying documentation for details on usage and
5 * license.
6 */
8 /*
9 * testaux.c
11 * This file is the C unit test for the bstraux module of Bstrlib.
14 #include <stdio.h>
15 #include "bstrlib.h"
16 #include "bstraux.h"
18 static int tWrite (const void * buf, size_t elsize, size_t nelem, void * parm) {
19 bstring b = (bstring) parm;
20 size_t i;
22 if (NULL == b || NULL == buf || 0 == elsize || 0 == nelem)
23 return -__LINE__;
25 for (i=0; i < nelem; i++) {
26 if (0 > bcatblk (b, buf, elsize)) break;
27 buf = (const void *) (elsize + (const char *) buf);
29 return (int) i;
32 int test0 (void) {
33 struct bwriteStream * ws;
34 bstring s;
35 int ret = 0;
37 printf ("TEST: struct bwriteStream functions.\n");
39 ws = bwsOpen ((bNwrite) tWrite, (s = bfromcstr ("")));
40 bwsBuffLength (ws, 8);
41 ret += 8 != bwsBuffLength (ws, 0);
42 bwsWriteBlk (ws, bsStaticBlkParms ("Hello "));
43 ret += 0 == biseqcstr (s, "");
44 bwsWriteBlk (ws, bsStaticBlkParms ("World\n"));
45 ret += 0 == biseqcstr (s, "Hello Wo");
46 ret += s != bwsClose (ws);
47 ret += 0 == biseqcstr (s, "Hello World\n");
49 printf ("\t# failures: %d\n", ret);
51 return ret;
54 int test1 (void) {
55 struct tagbstring t = bsStatic ("Hello world");
56 bstring b, c, d;
57 int ret = 0;
59 printf ("TEST: bTail and bHead functions.\n");
60 b = bTail (&t, 5);
61 c = bHead (&t, 5);
62 ret += 0 >= biseqcstr (b, "world");
63 ret += 0 >= biseqcstr (c, "Hello");
64 bdestroy (b);
65 bdestroy (c);
67 b = bTail (&t, 0);
68 c = bHead (&t, 0);
69 ret += 0 >= biseqcstr (b, "");
70 ret += 0 >= biseqcstr (c, "");
71 bdestroy (b);
72 bdestroy (c);
74 d = bstrcpy (&t);
75 b = bTail (d, 5);
76 c = bHead (d, 5);
77 ret += 0 >= biseqcstr (b, "world");
78 ret += 0 >= biseqcstr (c, "Hello");
79 bdestroy (b);
80 bdestroy (c);
81 bdestroy (d);
83 printf ("\t# failures: %d\n", ret);
85 return ret;
88 int test2 (void) {
89 struct tagbstring t = bsStatic ("Hello world");
90 bstring b;
91 int ret = 0, reto;
93 printf ("TEST: bSetChar function.\n");
94 ret += 0 <= bSetChar (&t, 4, ',');
95 ret += 0 > bSetChar (b = bstrcpy (&t), 4, ',');
96 ret += 0 >= biseqcstr (b, "Hell, world");
97 ret += 0 <= bSetChar (b, -1, 'x');
98 b->slen = 2;
99 ret += 0 > bSetChar (b, 1, 'i');
100 ret += 0 >= biseqcstr (b, "Hi");
101 ret += 0 > bSetChar (b, 2, 's');
102 ret += 0 >= biseqcstr (b, "His");
103 ret += 0 > bSetChar (b, 1, '\0');
104 ret += blength (b) != 3;
105 ret += bchare (b, 0, '?') != 'H';
106 ret += bchare (b, 1, '?') != '\0';
107 ret += bchare (b, 2, '?') != 's';
108 bdestroy (b);
110 printf ("\t# failures: %d\n", ret);
112 reto = ret;
113 ret = 0;
115 printf ("TEST: bSetCstrChar function.\n");
116 ret += 0 <= bSetCstrChar (&t, 4, ',');
117 ret += 0 > bSetCstrChar (b = bstrcpy (&t), 4, ',');
118 ret += 0 >= biseqcstr (b, "Hell, world");
119 ret += 0 <= bSetCstrChar (b, -1, 'x');
120 b->slen = 2;
121 ret += 0 > bSetCstrChar (b, 1, 'i');
122 ret += 0 >= biseqcstr (b, "Hi");
123 ret += 0 > bSetCstrChar (b, 2, 's');
124 ret += 0 >= biseqcstr (b, "His");
125 ret += 0 > bSetCstrChar (b, 1, '\0');
126 ret += blength (b) != 1;
127 ret += bchare (b, 0, '?') != 'H';
128 bdestroy (b);
130 printf ("\t# failures: %d\n", ret);
132 return reto + ret;
135 int test3 (void) {
136 struct tagbstring t = bsStatic ("Hello world");
137 bstring b;
138 int ret = 0;
140 printf ("TEST: bFill function.\n");
141 ret += 0 <= bFill (&t, 'x', 7);
142 ret += 0 > bFill (b = bstrcpy (&t), 'x', 7);
143 ret += 0 >= biseqcstr (b, "xxxxxxx");
144 ret += 0 <= bFill (b, 'x', -1);
145 ret += 0 > bFill (b, 'x', 0);
146 ret += 0 >= biseqcstr (b, "");
147 bdestroy (b);
149 printf ("\t# failures: %d\n", ret);
151 return ret;
154 int test4 (void) {
155 struct tagbstring t = bsStatic ("foo");
156 bstring b;
157 int ret = 0;
159 printf ("TEST: bReplicate function.\n");
160 ret += 0 <= bReplicate (&t, 4);
161 ret += 0 <= bReplicate (b = bstrcpy (&t), -1);
162 ret += 0 > bReplicate (b, 4);
163 ret += 0 >= biseqcstr (b, "foofoofoofoo");
164 ret += 0 > bReplicate (b, 0);
165 ret += 0 >= biseqcstr (b, "");
166 bdestroy (b);
168 printf ("\t# failures: %d\n", ret);
170 return ret;
173 int test5 (void) {
174 struct tagbstring t = bsStatic ("Hello world");
175 bstring b;
176 int ret = 0;
178 printf ("TEST: bReverse function.\n");
179 ret += 0 <= bReverse (&t);
180 ret += 0 > bReverse (b = bstrcpy (&t));
181 ret += 0 >= biseqcstr (b, "dlrow olleH");
182 b->slen = 0;
183 ret += 0 > bReverse (b);
184 ret += 0 >= biseqcstr (b, "");
185 bdestroy (b);
187 printf ("\t# failures: %d\n", ret);
189 return ret;
192 int test6 (void) {
193 struct tagbstring t = bsStatic ("Hello world");
194 bstring b;
195 int ret = 0;
197 printf ("TEST: bInsertChrs function.\n");
198 ret += 0 <= bInsertChrs (&t, 6, 4, 'x', '?');
199 ret += 0 > bInsertChrs (b = bstrcpy (&t), 6, 4, 'x', '?');
200 ret += 0 >= biseqcstr (b, "Hello xxxxworld");
201 bdestroy (b);
203 printf ("\t# failures: %d\n", ret);
205 return ret;
208 int test7 (void) {
209 struct tagbstring t = bsStatic (" i am ");
210 bstring b;
211 int ret = 0;
213 printf ("TEST: bJustify functions.\n");
214 ret += 0 <= bJustifyLeft (&t, ' ');
215 ret += 0 <= bJustifyRight (&t, 8, ' ');
216 ret += 0 <= bJustifyMargin (&t, 8, ' ');
217 ret += 0 <= bJustifyCenter (&t, 8, ' ');
218 ret += 0 > bJustifyLeft (b = bstrcpy (&t), ' ');
219 ret += 0 >= biseqcstr (b, "i am");
220 ret += 0 > bJustifyRight (b, 8, ' ');
221 ret += 0 >= biseqcstr (b, " i am");
222 ret += 0 > bJustifyMargin (b, 8, ' ');
223 ret += 0 >= biseqcstr (b, "i am");
224 ret += 0 > bJustifyCenter (b, 8, ' ');
225 ret += 0 >= biseqcstr (b, " i am");
226 bdestroy (b);
228 printf ("\t# failures: %d\n", ret);
230 return ret;
233 int test8 (void) {
234 struct tagbstring t = bsStatic ("Hello world");
235 bstring b;
236 char * c;
237 int ret = 0;
239 printf ("TEST: NetStr functions.\n");
240 c = bStr2NetStr (&t);
241 ret += 0 != strcmp (c, "11:Hello world,");
242 b = bNetStr2Bstr (c);
243 ret += 0 >= biseq (b, &t);
244 bdestroy (b);
245 bcstrfree (c);
247 printf ("\t# failures: %d\n", ret);
249 return ret;
252 int test9 (void) {
253 struct tagbstring t = bsStatic ("Hello world");
254 bstring b, c;
255 int err, ret = 0;
257 printf ("TEST: Base 64 codec.\n");
259 b = bBase64Encode (&t);
260 ret += 0 >= biseqcstr (b, "SGVsbG8gd29ybGQ=");
261 c = bBase64DecodeEx (b, &err);
262 ret += 0 != err;
263 ret += 0 >= biseq (c, &t);
264 bdestroy (b);
265 bdestroy (c);
267 printf ("\t# failures: %d\n", ret);
269 return ret;
272 int test10 (void) {
273 struct tagbstring t = bsStatic ("Hello world");
274 bstring b, c;
275 int err, ret = 0;
277 printf ("TEST: UU codec.\n");
279 b = bUuEncode (&t);
280 ret += 0 >= biseqcstr (b, "+2&5L;&\\@=V]R;&0`\r\n");
281 c = bUuDecodeEx (b, &err);
282 ret += 0 != err;
283 ret += 0 >= biseq (c, &t);
284 bdestroy (b);
285 bdestroy (c);
287 printf ("\t# failures: %d\n", ret);
289 return ret;
292 int test11 (void) {
293 struct tagbstring t = bsStatic ("Hello world");
294 unsigned char Ytstr[] = {0x72, 0x8f, 0x96, 0x96, 0x99, 0x4a, 0xa1, 0x99, 0x9c, 0x96, 0x8e};
295 bstring b, c;
296 int ret = 0;
298 printf ("TEST: Y codec.\n");
300 b = bYEncode (&t);
301 ret += 11 != b->slen;
302 ret += 0 >= bisstemeqblk (b, Ytstr, 11);
303 c = bYDecode (b);
304 ret += 0 >= biseq (c, &t);
305 bdestroy (b);
306 bdestroy (c);
308 printf ("\t# failures: %d\n", ret);
310 return ret;
313 int test12 (void) {
314 struct tagbstring t = bsStatic ("Hello world");
315 struct bStream * s;
316 bstring b;
317 int ret = 0;
319 printf ("TEST: bsFromBstr.\n");
321 ret = bsread (b = bfromcstr (""), s = bsFromBstr (&t), 6);
322 ret += 1 != biseqcstr (b, "Hello ");
323 if (b) b->slen = 0;
324 ret = bsread (b, s, 6);
325 ret += 1 != biseqcstr (b, "world");
327 bdestroy (b);
328 bsclose (s);
330 printf ("\t# failures: %d\n", ret);
332 return ret;
336 int main () {
337 int ret = 0;
339 printf ("Direct case testing of bstraux functions\n");
341 ret += test0 ();
342 ret += test1 ();
343 ret += test2 ();
344 ret += test3 ();
345 ret += test4 ();
346 ret += test5 ();
347 ret += test6 ();
348 ret += test7 ();
349 ret += test8 ();
350 ret += test9 ();
351 ret += test10 ();
352 ret += test11 ();
353 ret += test12 ();
355 printf ("# test failures: %d\n", ret);
357 return 0;