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
11 * This file is the C unit test for the bstraux module of Bstrlib.
18 static int tWrite (const void * buf
, size_t elsize
, size_t nelem
, void * parm
) {
19 bstring b
= (bstring
) parm
;
22 if (NULL
== b
|| NULL
== buf
|| 0 == elsize
|| 0 == nelem
)
25 for (i
=0; i
< nelem
; i
++) {
26 if (0 > bcatblk (b
, buf
, elsize
)) break;
27 buf
= (const void *) (elsize
+ (const char *) buf
);
33 struct bwriteStream
* ws
;
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
);
55 struct tagbstring t
= bsStatic ("Hello world");
59 printf ("TEST: bTail and bHead functions.\n");
62 ret
+= 0 >= biseqcstr (b
, "world");
63 ret
+= 0 >= biseqcstr (c
, "Hello");
69 ret
+= 0 >= biseqcstr (b
, "");
70 ret
+= 0 >= biseqcstr (c
, "");
77 ret
+= 0 >= biseqcstr (b
, "world");
78 ret
+= 0 >= biseqcstr (c
, "Hello");
83 printf ("\t# failures: %d\n", ret
);
89 struct tagbstring t
= bsStatic ("Hello world");
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');
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';
110 printf ("\t# failures: %d\n", ret
);
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');
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';
130 printf ("\t# failures: %d\n", ret
);
136 struct tagbstring t
= bsStatic ("Hello world");
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
, "");
149 printf ("\t# failures: %d\n", ret
);
155 struct tagbstring t
= bsStatic ("foo");
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
, "");
168 printf ("\t# failures: %d\n", ret
);
174 struct tagbstring t
= bsStatic ("Hello world");
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");
183 ret
+= 0 > bReverse (b
);
184 ret
+= 0 >= biseqcstr (b
, "");
187 printf ("\t# failures: %d\n", ret
);
193 struct tagbstring t
= bsStatic ("Hello world");
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");
203 printf ("\t# failures: %d\n", ret
);
209 struct tagbstring t
= bsStatic (" i am ");
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");
228 printf ("\t# failures: %d\n", ret
);
234 struct tagbstring t
= bsStatic ("Hello world");
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
);
247 printf ("\t# failures: %d\n", ret
);
253 struct tagbstring t
= bsStatic ("Hello world");
257 printf ("TEST: Base 64 codec.\n");
259 b
= bBase64Encode (&t
);
260 ret
+= 0 >= biseqcstr (b
, "SGVsbG8gd29ybGQ=");
261 c
= bBase64DecodeEx (b
, &err
);
263 ret
+= 0 >= biseq (c
, &t
);
267 printf ("\t# failures: %d\n", ret
);
273 struct tagbstring t
= bsStatic ("Hello world");
277 printf ("TEST: UU codec.\n");
280 ret
+= 0 >= biseqcstr (b
, "+2&5L;&\\@=V]R;&0`\r\n");
281 c
= bUuDecodeEx (b
, &err
);
283 ret
+= 0 >= biseq (c
, &t
);
287 printf ("\t# failures: %d\n", ret
);
293 struct tagbstring t
= bsStatic ("Hello world");
294 unsigned char Ytstr
[] = {0x72, 0x8f, 0x96, 0x96, 0x99, 0x4a, 0xa1, 0x99, 0x9c, 0x96, 0x8e};
298 printf ("TEST: Y codec.\n");
301 ret
+= 11 != b
->slen
;
302 ret
+= 0 >= bisstemeqblk (b
, Ytstr
, 11);
304 ret
+= 0 >= biseq (c
, &t
);
308 printf ("\t# failures: %d\n", ret
);
314 struct tagbstring t
= bsStatic ("Hello world");
319 printf ("TEST: bsFromBstr.\n");
321 ret
= bsread (b
= bfromcstr (""), s
= bsFromBstr (&t
), 6);
322 ret
+= 1 != biseqcstr (b
, "Hello ");
324 ret
= bsread (b
, s
, 6);
325 ret
+= 1 != biseqcstr (b
, "world");
330 printf ("\t# failures: %d\n", ret
);
339 printf ("Direct case testing of bstraux functions\n");
355 printf ("# test failures: %d\n", ret
);