/third-party/bstring/testaux.c

https://github.com/PrecipiceGames/craftd · C · 405 lines · 308 code · 85 blank · 12 comment · 37 complexity · 097111ac39a8bc7c8997ac9ed1fc325e MD5 · raw file

  1. /*
  2. * This source file is part of the bstring string library. This code was
  3. * written by Paul Hsieh in 2002-2010, and is covered by either the 3-clause
  4. * BSD open source license or GPL v2.0. Refer to the accompanying documentation
  5. * for details on usage and license.
  6. */
  7. /*
  8. * testaux.c
  9. *
  10. * This file is the C unit test for the bstraux module of Bstrlib.
  11. */
  12. #include <stdio.h>
  13. #include "bstrlib.h"
  14. #include "bstraux.h"
  15. static int tWrite (const void * buf, size_t elsize, size_t nelem, void * parm) {
  16. bstring b = (bstring) parm;
  17. size_t i;
  18. if (NULL == b || NULL == buf || 0 == elsize || 0 == nelem)
  19. return -__LINE__;
  20. for (i=0; i < nelem; i++) {
  21. if (0 > bcatblk (b, buf, elsize)) break;
  22. buf = (const void *) (elsize + (const char *) buf);
  23. }
  24. return (int) i;
  25. }
  26. int test0 (void) {
  27. struct bwriteStream * ws;
  28. bstring s;
  29. int ret = 0;
  30. printf ("TEST: struct bwriteStream functions.\n");
  31. ws = bwsOpen ((bNwrite) tWrite, (s = bfromcstr ("")));
  32. bwsBuffLength (ws, 8);
  33. ret += 8 != bwsBuffLength (ws, 0);
  34. bwsWriteBlk (ws, bsStaticBlkParms ("Hello "));
  35. ret += 0 == biseqcstr (s, "");
  36. bwsWriteBlk (ws, bsStaticBlkParms ("World\n"));
  37. ret += 0 == biseqcstr (s, "Hello Wo");
  38. ret += s != bwsClose (ws);
  39. ret += 0 == biseqcstr (s, "Hello World\n");
  40. printf ("\t# failures: %d\n", ret);
  41. return ret;
  42. }
  43. int test1 (void) {
  44. struct tagbstring t = bsStatic ("Hello world");
  45. bstring b, c, d;
  46. int ret = 0;
  47. printf ("TEST: bTail and bHead functions.\n");
  48. b = bTail (&t, 5);
  49. c = bHead (&t, 5);
  50. ret += 0 >= biseqcstr (b, "world");
  51. ret += 0 >= biseqcstr (c, "Hello");
  52. bdestroy (b);
  53. bdestroy (c);
  54. b = bTail (&t, 0);
  55. c = bHead (&t, 0);
  56. ret += 0 >= biseqcstr (b, "");
  57. ret += 0 >= biseqcstr (c, "");
  58. bdestroy (b);
  59. bdestroy (c);
  60. d = bstrcpy (&t);
  61. b = bTail (d, 5);
  62. c = bHead (d, 5);
  63. ret += 0 >= biseqcstr (b, "world");
  64. ret += 0 >= biseqcstr (c, "Hello");
  65. bdestroy (b);
  66. bdestroy (c);
  67. bdestroy (d);
  68. printf ("\t# failures: %d\n", ret);
  69. return ret;
  70. }
  71. int test2 (void) {
  72. struct tagbstring t = bsStatic ("Hello world");
  73. bstring b;
  74. int ret = 0, reto;
  75. printf ("TEST: bSetChar function.\n");
  76. ret += 0 <= bSetChar (&t, 4, ',');
  77. ret += 0 > bSetChar (b = bstrcpy (&t), 4, ',');
  78. ret += 0 >= biseqcstr (b, "Hell, world");
  79. ret += 0 <= bSetChar (b, -1, 'x');
  80. b->slen = 2;
  81. ret += 0 > bSetChar (b, 1, 'i');
  82. ret += 0 >= biseqcstr (b, "Hi");
  83. ret += 0 > bSetChar (b, 2, 's');
  84. ret += 0 >= biseqcstr (b, "His");
  85. ret += 0 > bSetChar (b, 1, '\0');
  86. ret += blength (b) != 3;
  87. ret += bchare (b, 0, '?') != 'H';
  88. ret += bchare (b, 1, '?') != '\0';
  89. ret += bchare (b, 2, '?') != 's';
  90. bdestroy (b);
  91. printf ("\t# failures: %d\n", ret);
  92. reto = ret;
  93. ret = 0;
  94. printf ("TEST: bSetCstrChar function.\n");
  95. ret += 0 <= bSetCstrChar (&t, 4, ',');
  96. ret += 0 > bSetCstrChar (b = bstrcpy (&t), 4, ',');
  97. ret += 0 >= biseqcstr (b, "Hell, world");
  98. ret += 0 <= bSetCstrChar (b, -1, 'x');
  99. b->slen = 2;
  100. ret += 0 > bSetCstrChar (b, 1, 'i');
  101. ret += 0 >= biseqcstr (b, "Hi");
  102. ret += 0 > bSetCstrChar (b, 2, 's');
  103. ret += 0 >= biseqcstr (b, "His");
  104. ret += 0 > bSetCstrChar (b, 1, '\0');
  105. ret += blength (b) != 1;
  106. ret += bchare (b, 0, '?') != 'H';
  107. bdestroy (b);
  108. printf ("\t# failures: %d\n", ret);
  109. return reto + ret;
  110. }
  111. int test3 (void) {
  112. struct tagbstring t = bsStatic ("Hello world");
  113. bstring b;
  114. int ret = 0;
  115. printf ("TEST: bFill function.\n");
  116. ret += 0 <= bFill (&t, 'x', 7);
  117. ret += 0 > bFill (b = bstrcpy (&t), 'x', 7);
  118. ret += 0 >= biseqcstr (b, "xxxxxxx");
  119. ret += 0 <= bFill (b, 'x', -1);
  120. ret += 0 > bFill (b, 'x', 0);
  121. ret += 0 >= biseqcstr (b, "");
  122. bdestroy (b);
  123. printf ("\t# failures: %d\n", ret);
  124. return ret;
  125. }
  126. int test4 (void) {
  127. struct tagbstring t = bsStatic ("foo");
  128. bstring b;
  129. int ret = 0;
  130. printf ("TEST: bReplicate function.\n");
  131. ret += 0 <= bReplicate (&t, 4);
  132. ret += 0 <= bReplicate (b = bstrcpy (&t), -1);
  133. ret += 0 > bReplicate (b, 4);
  134. ret += 0 >= biseqcstr (b, "foofoofoofoo");
  135. ret += 0 > bReplicate (b, 0);
  136. ret += 0 >= biseqcstr (b, "");
  137. bdestroy (b);
  138. printf ("\t# failures: %d\n", ret);
  139. return ret;
  140. }
  141. int test5 (void) {
  142. struct tagbstring t = bsStatic ("Hello world");
  143. bstring b;
  144. int ret = 0;
  145. printf ("TEST: bReverse function.\n");
  146. ret += 0 <= bReverse (&t);
  147. ret += 0 > bReverse (b = bstrcpy (&t));
  148. ret += 0 >= biseqcstr (b, "dlrow olleH");
  149. b->slen = 0;
  150. ret += 0 > bReverse (b);
  151. ret += 0 >= biseqcstr (b, "");
  152. bdestroy (b);
  153. printf ("\t# failures: %d\n", ret);
  154. return ret;
  155. }
  156. int test6 (void) {
  157. struct tagbstring t = bsStatic ("Hello world");
  158. bstring b;
  159. int ret = 0;
  160. printf ("TEST: bInsertChrs function.\n");
  161. ret += 0 <= bInsertChrs (&t, 6, 4, 'x', '?');
  162. ret += 0 > bInsertChrs (b = bstrcpy (&t), 6, 4, 'x', '?');
  163. ret += 0 >= biseqcstr (b, "Hello xxxxworld");
  164. bdestroy (b);
  165. printf ("\t# failures: %d\n", ret);
  166. return ret;
  167. }
  168. int test7 (void) {
  169. struct tagbstring t = bsStatic (" i am ");
  170. bstring b;
  171. int ret = 0;
  172. printf ("TEST: bJustify functions.\n");
  173. ret += 0 <= bJustifyLeft (&t, ' ');
  174. ret += 0 <= bJustifyRight (&t, 8, ' ');
  175. ret += 0 <= bJustifyMargin (&t, 8, ' ');
  176. ret += 0 <= bJustifyCenter (&t, 8, ' ');
  177. ret += 0 > bJustifyLeft (b = bstrcpy (&t), ' ');
  178. ret += 0 >= biseqcstr (b, "i am");
  179. ret += 0 > bJustifyRight (b, 8, ' ');
  180. ret += 0 >= biseqcstr (b, " i am");
  181. ret += 0 > bJustifyMargin (b, 8, ' ');
  182. ret += 0 >= biseqcstr (b, "i am");
  183. ret += 0 > bJustifyCenter (b, 8, ' ');
  184. ret += 0 >= biseqcstr (b, " i am");
  185. bdestroy (b);
  186. printf ("\t# failures: %d\n", ret);
  187. return ret;
  188. }
  189. int test8 (void) {
  190. struct tagbstring t = bsStatic ("Hello world");
  191. bstring b;
  192. char * c;
  193. int ret = 0;
  194. printf ("TEST: NetStr functions.\n");
  195. c = bStr2NetStr (&t);
  196. ret += 0 != strcmp (c, "11:Hello world,");
  197. b = bNetStr2Bstr (c);
  198. ret += 0 >= biseq (b, &t);
  199. bdestroy (b);
  200. bcstrfree (c);
  201. printf ("\t# failures: %d\n", ret);
  202. return ret;
  203. }
  204. int test9 (void) {
  205. struct tagbstring t = bsStatic ("Hello world");
  206. bstring b, c;
  207. int err, ret = 0;
  208. printf ("TEST: Base 64 codec.\n");
  209. b = bBase64Encode (&t);
  210. ret += 0 >= biseqcstr (b, "SGVsbG8gd29ybGQ=");
  211. c = bBase64DecodeEx (b, &err);
  212. ret += 0 != err;
  213. ret += 0 >= biseq (c, &t);
  214. bdestroy (b);
  215. bdestroy (c);
  216. printf ("\t# failures: %d\n", ret);
  217. return ret;
  218. }
  219. int test10 (void) {
  220. struct tagbstring t = bsStatic ("Hello world");
  221. bstring b, c;
  222. int err, ret = 0;
  223. printf ("TEST: UU codec.\n");
  224. b = bUuEncode (&t);
  225. ret += 0 >= biseqcstr (b, "+2&5L;&\\@=V]R;&0`\r\n");
  226. c = bUuDecodeEx (b, &err);
  227. ret += 0 != err;
  228. ret += 0 >= biseq (c, &t);
  229. bdestroy (b);
  230. bdestroy (c);
  231. printf ("\t# failures: %d\n", ret);
  232. return ret;
  233. }
  234. int test11 (void) {
  235. struct tagbstring t = bsStatic ("Hello world");
  236. unsigned char Ytstr[] = {0x72, 0x8f, 0x96, 0x96, 0x99, 0x4a, 0xa1, 0x99, 0x9c, 0x96, 0x8e};
  237. bstring b, c;
  238. int ret = 0;
  239. printf ("TEST: Y codec.\n");
  240. b = bYEncode (&t);
  241. ret += 11 != b->slen;
  242. ret += 0 >= bisstemeqblk (b, Ytstr, 11);
  243. c = bYDecode (b);
  244. ret += 0 >= biseq (c, &t);
  245. bdestroy (b);
  246. bdestroy (c);
  247. printf ("\t# failures: %d\n", ret);
  248. return ret;
  249. }
  250. int test12 (void) {
  251. struct tagbstring t = bsStatic ("Hello world");
  252. struct bStream * s;
  253. bstring b;
  254. int ret = 0;
  255. printf ("TEST: bsFromBstr.\n");
  256. ret = bsread (b = bfromcstr (""), s = bsFromBstr (&t), 6);
  257. ret += 1 != biseqcstr (b, "Hello ");
  258. if (b) b->slen = 0;
  259. ret = bsread (b, s, 6);
  260. ret += 1 != biseqcstr (b, "world");
  261. bdestroy (b);
  262. bsclose (s);
  263. printf ("\t# failures: %d\n", ret);
  264. return ret;
  265. }
  266. struct vfgetc {
  267. int ofs;
  268. bstring base;
  269. };
  270. static int test13_fgetc (void * ctx) {
  271. struct vfgetc * vctx = (struct vfgetc *) ctx;
  272. int c;
  273. if (NULL == vctx || NULL == vctx->base) return EOF;
  274. if (vctx->ofs >= blength (vctx->base)) return EOF;
  275. c = bchare (vctx->base, vctx->ofs, EOF);
  276. vctx->ofs++;
  277. return c;
  278. }
  279. int test13 (void) {
  280. struct tagbstring t0 = bsStatic ("Random String");
  281. struct vfgetc vctx;
  282. bstring b;
  283. int ret = 0;
  284. int i;
  285. printf ("TEST: bSecureInput, bSecureDestroy.\n");
  286. for (i=0; i < 1000; i++) {
  287. unsigned char * h;
  288. vctx.ofs = 0;
  289. vctx.base = &t0;
  290. b = bSecureInput (INT_MAX, '\n', (bNgetc) test13_fgetc, &vctx);
  291. ret += 1 != biseq (b, &t0);
  292. h = b->data;
  293. bSecureDestroy (b);
  294. /* WARNING! Technically unsound code follows: */
  295. ret += (0 == memcmp (h, t0.data, t0.slen));
  296. if (ret) break;
  297. }
  298. printf ("\t# failures: %d\n", ret);
  299. return ret;
  300. }
  301. int main () {
  302. int ret = 0;
  303. printf ("Direct case testing of bstraux functions\n");
  304. ret += test0 ();
  305. ret += test1 ();
  306. ret += test2 ();
  307. ret += test3 ();
  308. ret += test4 ();
  309. ret += test5 ();
  310. ret += test6 ();
  311. ret += test7 ();
  312. ret += test8 ();
  313. ret += test9 ();
  314. ret += test10 ();
  315. ret += test11 ();
  316. ret += test12 ();
  317. ret += test13 ();
  318. printf ("# test failures: %d\n", ret);
  319. return 0;
  320. }