string-util.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. #include <glib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include "test.h"
  5. /* This test is just to be used with valgrind */
  6. RESULT
  7. test_strfreev ()
  8. {
  9. gchar **array = g_new (gchar *, 4);
  10. array [0] = g_strdup ("one");
  11. array [1] = g_strdup ("two");
  12. array [2] = g_strdup ("three");
  13. array [3] = NULL;
  14. g_strfreev (array);
  15. g_strfreev (NULL);
  16. return OK;
  17. }
  18. RESULT
  19. test_concat ()
  20. {
  21. gchar *x = g_strconcat ("Hello", ", ", "world", NULL);
  22. if (strcmp (x, "Hello, world") != 0)
  23. return FAILED("concat failed, got: %s", x);
  24. g_free (x);
  25. return OK;
  26. }
  27. RESULT
  28. test_split ()
  29. {
  30. const gchar *to_split = "Hello world, how are we doing today?";
  31. gint i;
  32. gchar **v;
  33. v= g_strsplit(to_split, " ", 0);
  34. if(v == NULL) {
  35. return FAILED("split failed, got NULL vector (1)");
  36. }
  37. for(i = 0; v[i] != NULL; i++);
  38. if(i != 7) {
  39. return FAILED("split failed, expected 7 tokens, got %d", i);
  40. }
  41. g_strfreev(v);
  42. v = g_strsplit(to_split, ":", -1);
  43. if(v == NULL) {
  44. return FAILED("split failed, got NULL vector (2)");
  45. }
  46. for(i = 0; v[i] != NULL; i++);
  47. if(i != 1) {
  48. return FAILED("split failed, expected 1 token, got %d", i);
  49. }
  50. if(strcmp(v[0], to_split) != 0) {
  51. return FAILED("expected vector[0] to be '%s' but it was '%s'",
  52. to_split, v[0]);
  53. }
  54. g_strfreev(v);
  55. v = g_strsplit ("", ":", 0);
  56. if (v == NULL)
  57. return FAILED ("g_strsplit returned NULL");
  58. g_strfreev (v);
  59. v = g_strsplit ("/home/miguel/dingus", "/", 0);
  60. if (v [0][0] != 0)
  61. return FAILED ("Got a non-empty first element");
  62. g_strfreev (v);
  63. v = g_strsplit ("appdomain1, Version=0.0.0.0, Culture=neutral", ",", 4);
  64. if (strcmp (v [0], "appdomain1") != 0)
  65. return FAILED ("Invalid value");
  66. if (strcmp (v [1], " Version=0.0.0.0") != 0)
  67. return FAILED ("Invalid value");
  68. if (strcmp (v [2], " Culture=neutral") != 0)
  69. return FAILED ("Invalid value");
  70. if (v [3] != NULL)
  71. return FAILED ("Expected only 3 elements");
  72. g_strfreev (v);
  73. v = g_strsplit ("abcXYdefXghiXYjklYmno", "XY", 4);
  74. if (strcmp (v [0], "abc") != 0)
  75. return FAILED ("Invalid value 0");
  76. if (strcmp (v [1], "defXghi") != 0)
  77. return FAILED ("Invalid value 1");
  78. if (strcmp (v [2], "jklYmno") != 0)
  79. return FAILED ("Invalid value 2");
  80. if (v [3] != NULL)
  81. return FAILED ("Expected only 3 elements (1)");
  82. g_strfreev (v);
  83. v = g_strsplit ("abcXYdefXghiXYjklYmno", "XY", 2);
  84. if (strcmp (v [0], "abc") != 0)
  85. return FAILED ("Invalid value 3");
  86. if (strcmp (v [1], "defXghiXYjklYmno") != 0)
  87. return FAILED ("Invalid value 4");
  88. if (v [2] != NULL)
  89. return FAILED ("Expected only 2 elements (2)");
  90. g_strfreev (v);
  91. v = g_strsplit ("abcXYdefXghiXYjklYmnoXY", "XY", 3);
  92. if (strcmp (v [0], "abc") != 0)
  93. return FAILED ("Invalid value 5");
  94. if (strcmp (v [1], "defXghi") != 0)
  95. return FAILED ("Invalid value 6");
  96. if (strcmp (v [2], "jklYmnoXY") != 0)
  97. return FAILED ("Invalid value 7");
  98. if (v [3] != NULL)
  99. return FAILED ("Expected only 3 elements (3)");
  100. g_strfreev (v);
  101. v = g_strsplit ("abcXYXYXYdefXY", "XY", -1);
  102. if (strcmp (v [0], "abc") != 0)
  103. return FAILED ("Invalid value 8");
  104. if (strcmp (v [1], "") != 0)
  105. return FAILED ("Invalid value 9");
  106. if (strcmp (v [2], "") != 0)
  107. return FAILED ("Invalid value 10");
  108. if (strcmp (v [3], "def") != 0)
  109. return FAILED ("Invalid value 11");
  110. if (strcmp (v [4], "") != 0)
  111. return FAILED ("Invalid value 12");
  112. if (v [5] != NULL)
  113. return FAILED ("Expected only 5 elements (4)");
  114. g_strfreev (v);
  115. v = g_strsplit ("XYXYXYabcXYdef", "XY", -1);
  116. if (strcmp (v [0], "") != 0)
  117. return FAILED ("Invalid value 13");
  118. if (strcmp (v [1], "") != 0)
  119. return FAILED ("Invalid value 14");
  120. if (strcmp (v [2], "") != 0)
  121. return FAILED ("Invalid value 15");
  122. if (strcmp (v [3], "abc") != 0)
  123. return FAILED ("Invalid value 16");
  124. if (strcmp (v [4], "def") != 0)
  125. return FAILED ("Invalid value 17");
  126. if (v [5] != NULL)
  127. return FAILED ("Expected only 5 elements (5)");
  128. g_strfreev (v);
  129. return OK;
  130. }
  131. RESULT
  132. test_split_set ()
  133. {
  134. gchar **v;
  135. v = g_strsplit_set ("abcXYdefXghiXYjklYmno", "XY", 6);
  136. if (strcmp (v [0], "abc") != 0)
  137. return FAILED ("Invalid value 0");
  138. if (strcmp (v [1], "") != 0)
  139. return FAILED ("Invalid value 1");
  140. if (strcmp (v [2], "def") != 0)
  141. return FAILED ("Invalid value 2");
  142. if (strcmp (v [3], "ghi") != 0)
  143. return FAILED ("Invalid value 3");
  144. if (strcmp (v [4], "") != 0)
  145. return FAILED ("Invalid value 4");
  146. if (strcmp (v [5], "jklYmno") != 0)
  147. return FAILED ("Invalid value 5");
  148. if (v [6] != NULL)
  149. return FAILED ("Expected only 6 elements (1)");
  150. g_strfreev (v);
  151. v = g_strsplit_set ("abcXYdefXghiXYjklYmno", "XY", 3);
  152. if (strcmp (v [0], "abc") != 0)
  153. return FAILED ("Invalid value 6");
  154. if (strcmp (v [1], "") != 0)
  155. return FAILED ("Invalid value 7");
  156. if (strcmp (v [2], "defXghiXYjklYmno") != 0)
  157. return FAILED ("Invalid value 8");
  158. if (v [3] != NULL)
  159. return FAILED ("Expected only 3 elements (2)");
  160. g_strfreev (v);
  161. v = g_strsplit_set ("abcXdefYghiXjklYmnoX", "XY", 5);
  162. if (strcmp (v [0], "abc") != 0)
  163. return FAILED ("Invalid value 9");
  164. if (strcmp (v [1], "def") != 0)
  165. return FAILED ("Invalid value 10");
  166. if (strcmp (v [2], "ghi") != 0)
  167. return FAILED ("Invalid value 11");
  168. if (strcmp (v [3], "jkl") != 0)
  169. return FAILED ("Invalid value 12");
  170. if (strcmp (v [4], "mnoX") != 0)
  171. return FAILED ("Invalid value 13");
  172. if (v [5] != NULL)
  173. return FAILED ("Expected only 5 elements (5)");
  174. g_strfreev (v);
  175. v = g_strsplit_set ("abcXYXdefXY", "XY", -1);
  176. if (strcmp (v [0], "abc") != 0)
  177. return FAILED ("Invalid value 14");
  178. if (strcmp (v [1], "") != 0)
  179. return FAILED ("Invalid value 15");
  180. if (strcmp (v [2], "") != 0)
  181. return FAILED ("Invalid value 16");
  182. if (strcmp (v [3], "def") != 0)
  183. return FAILED ("Invalid value 17");
  184. if (strcmp (v [4], "") != 0)
  185. return FAILED ("Invalid value 18");
  186. if (strcmp (v [5], "") != 0)
  187. return FAILED ("Invalid value 19");
  188. if (v [6] != NULL)
  189. return FAILED ("Expected only 6 elements (4)");
  190. g_strfreev (v);
  191. v = g_strsplit_set ("XYXabcXYdef", "XY", -1);
  192. if (strcmp (v [0], "") != 0)
  193. return FAILED ("Invalid value 20");
  194. if (strcmp (v [1], "") != 0)
  195. return FAILED ("Invalid value 21");
  196. if (strcmp (v [2], "") != 0)
  197. return FAILED ("Invalid value 22");
  198. if (strcmp (v [3], "abc") != 0)
  199. return FAILED ("Invalid value 23");
  200. if (strcmp (v [4], "") != 0)
  201. return FAILED ("Invalid value 24");
  202. if (strcmp (v [5], "def") != 0)
  203. return FAILED ("Invalid value 25");
  204. if (v [6] != NULL)
  205. return FAILED ("Expected only 6 elements (5)");
  206. g_strfreev (v);
  207. return OK;
  208. }
  209. RESULT
  210. test_strreverse ()
  211. {
  212. gchar *a = g_strdup ("onetwothree");
  213. gchar *a_target = "eerhtowteno";
  214. gchar *b = g_strdup ("onetwothre");
  215. gchar *b_target = "erhtowteno";
  216. g_strreverse (a);
  217. if (strcmp (a, a_target)) {
  218. g_free (b);
  219. g_free (a);
  220. return FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", a, a_target);
  221. }
  222. g_strreverse (b);
  223. if (strcmp (b, b_target)) {
  224. g_free (b);
  225. g_free (a);
  226. return FAILED("strreverse failed. Expecting: '%s' and got '%s'\n", b, b_target);
  227. }
  228. g_free (b);
  229. g_free (a);
  230. return OK;
  231. }
  232. RESULT
  233. test_strjoin ()
  234. {
  235. char *s;
  236. s = g_strjoin (NULL, "a", "b", NULL);
  237. if (strcmp (s, "ab") != 0)
  238. return FAILED ("Join of two strings with no separator fails");
  239. g_free (s);
  240. s = g_strjoin ("", "a", "b", NULL);
  241. if (strcmp (s, "ab") != 0)
  242. return FAILED ("Join of two strings with empty separator fails");
  243. g_free (s);
  244. s = g_strjoin ("-", "a", "b", NULL);
  245. if (strcmp (s, "a-b") != 0)
  246. return FAILED ("Join of two strings with separator fails");
  247. g_free (s);
  248. s = g_strjoin ("-", "aaaa", "bbbb", "cccc", "dddd", NULL);
  249. if (strcmp (s, "aaaa-bbbb-cccc-dddd") != 0)
  250. return FAILED ("Join of multiple strings fails");
  251. g_free (s);
  252. s = g_strjoin ("-", NULL);
  253. if (s == NULL || (strcmp (s, "") != 0))
  254. return FAILED ("Failed to join empty arguments");
  255. g_free (s);
  256. return OK;
  257. }
  258. RESULT
  259. test_strchug ()
  260. {
  261. char *str = g_strdup (" \t\n hola");
  262. g_strchug (str);
  263. if (strcmp ("hola", str)) {
  264. fprintf (stderr, "%s\n", str);
  265. g_free (str);
  266. return FAILED ("Failed.");
  267. }
  268. g_free (str);
  269. return OK;
  270. }
  271. RESULT
  272. test_strchomp ()
  273. {
  274. char *str = g_strdup ("hola \t");
  275. g_strchomp (str);
  276. if (strcmp ("hola", str)) {
  277. fprintf (stderr, "%s\n", str);
  278. g_free (str);
  279. return FAILED ("Failed.");
  280. }
  281. g_free (str);
  282. return OK;
  283. }
  284. RESULT
  285. test_strstrip ()
  286. {
  287. char *str = g_strdup (" \t hola ");
  288. g_strstrip (str);
  289. if (strcmp ("hola", str)) {
  290. fprintf (stderr, "%s\n", str);
  291. g_free (str);
  292. return FAILED ("Failed.");
  293. }
  294. g_free (str);
  295. return OK;
  296. }
  297. #define urit(so,j) do { s = g_filename_to_uri (so, NULL, NULL); if (strcmp (s, j) != 0) return FAILED("Got %s expected %s", s, j); g_free (s); } while (0);
  298. #define errit(so) do { s = g_filename_to_uri (so, NULL, NULL); if (s != NULL) return FAILED ("got %s, expected NULL", s); } while (0);
  299. RESULT
  300. test_filename_to_uri ()
  301. {
  302. char *s;
  303. urit ("/a", "file:///a");
  304. urit ("/home/miguel", "file:///home/miguel");
  305. urit ("/home/mig uel", "file:///home/mig%20uel");
  306. urit ("/\303\241", "file:///%C3%A1");
  307. urit ("/\303\241/octal", "file:///%C3%A1/octal");
  308. urit ("/%", "file:///%25");
  309. urit ("/\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037\040", "file:///%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%20");
  310. urit ("/!$&'()*+,-./", "file:///!$&'()*+,-./");
  311. urit ("/\042\043\045", "file:///%22%23%25");
  312. urit ("/0123456789:=", "file:///0123456789:=");
  313. urit ("/\073\074\076\077", "file:///%3B%3C%3E%3F");
  314. urit ("/\133\134\135\136_\140\173\174\175", "file:///%5B%5C%5D%5E_%60%7B%7C%7D");
  315. urit ("/\173\174\175\176\177\200", "file:///%7B%7C%7D~%7F%80");
  316. urit ("/@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "file:///@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
  317. errit ("a");
  318. errit ("./hola");
  319. return OK;
  320. }
  321. #define fileit(so,j) do { s = g_filename_from_uri (so, NULL, NULL); if (strcmp (s, j) != 0) return FAILED("Got %s expected %s", s, j); g_free (s); } while (0);
  322. #define ferrit(so) do { s = g_filename_from_uri (so, NULL, NULL); if (s != NULL) return FAILED ("got %s, expected NULL", s); } while (0);
  323. RESULT
  324. test_filename_from_uri ()
  325. {
  326. char *s;
  327. fileit ("file:///a", "/a");
  328. fileit ("file:///%41", "/A");
  329. fileit ("file:///home/miguel", "/home/miguel");
  330. fileit ("file:///home/mig%20uel", "/home/mig uel");
  331. ferrit ("/a");
  332. ferrit ("a");
  333. ferrit ("file://a");
  334. ferrit ("file:a");
  335. ferrit ("file:///%");
  336. ferrit ("file:///%0");
  337. ferrit ("file:///%jj");
  338. return OK;
  339. }
  340. RESULT
  341. test_ascii_xdigit_value ()
  342. {
  343. int i;
  344. gchar j;
  345. i = g_ascii_xdigit_value ('9' + 1);
  346. if (i != -1)
  347. return FAILED ("'9' + 1");
  348. i = g_ascii_xdigit_value ('0' - 1);
  349. if (i != -1)
  350. return FAILED ("'0' - 1");
  351. i = g_ascii_xdigit_value ('a' - 1);
  352. if (i != -1)
  353. return FAILED ("'a' - 1");
  354. i = g_ascii_xdigit_value ('f' + 1);
  355. if (i != -1)
  356. return FAILED ("'f' + 1");
  357. i = g_ascii_xdigit_value ('A' - 1);
  358. if (i != -1)
  359. return FAILED ("'A' - 1");
  360. i = g_ascii_xdigit_value ('F' + 1);
  361. if (i != -1)
  362. return FAILED ("'F' + 1");
  363. for (j = '0'; j < '9'; j++) {
  364. int c = g_ascii_xdigit_value (j);
  365. if (c != (j - '0'))
  366. return FAILED ("Digits %c -> %d", j, c);
  367. }
  368. for (j = 'a'; j < 'f'; j++) {
  369. int c = g_ascii_xdigit_value (j);
  370. if (c != (j - 'a' + 10))
  371. return FAILED ("Lower %c -> %d", j, c);
  372. }
  373. for (j = 'A'; j < 'F'; j++) {
  374. int c = g_ascii_xdigit_value (j);
  375. if (c != (j - 'A' + 10))
  376. return FAILED ("Upper %c -> %d", j, c);
  377. }
  378. return OK;
  379. }
  380. RESULT
  381. test_strdelimit ()
  382. {
  383. gchar *str;
  384. str = g_strdup (G_STR_DELIMITERS);
  385. str = g_strdelimit (str, NULL, 'a');
  386. if (0 != strcmp ("aaaaaaa", str))
  387. return FAILED ("All delimiters: '%s'", str);
  388. g_free (str);
  389. str = g_strdup ("hola");
  390. str = g_strdelimit (str, "ha", '+');
  391. if (0 != strcmp ("+ol+", str))
  392. return FAILED ("2 delimiters: '%s'", str);
  393. g_free (str);
  394. return OK;
  395. }
  396. #define NUMBERS "0123456789"
  397. RESULT
  398. test_strlcpy ()
  399. {
  400. const gchar *src = "onetwothree";
  401. gchar *dest;
  402. gsize i;
  403. dest = g_malloc (strlen (src) + 1);
  404. memset (dest, 0, strlen (src) + 1);
  405. i = g_strlcpy (dest, src, (gsize)-1);
  406. if (i != strlen (src))
  407. return FAILED ("Test1 got %d", i);
  408. if (0 != strcmp (dest, src))
  409. return FAILED ("Src and dest not equal");
  410. i = g_strlcpy (dest, src, 3);
  411. if (i != strlen (src))
  412. return FAILED ("Test1 got %d", i);
  413. if (0 != strcmp (dest, "on"))
  414. return FAILED ("Test2");
  415. i = g_strlcpy (dest, src, 1);
  416. if (i != strlen (src))
  417. return FAILED ("Test3 got %d", i);
  418. if (*dest != '\0')
  419. return FAILED ("Test4");
  420. i = g_strlcpy (dest, src, 12345);
  421. if (i != strlen (src))
  422. return FAILED ("Test4 got %d", i);
  423. if (0 != strcmp (dest, src))
  424. return FAILED ("Src and dest not equal 2");
  425. g_free (dest);
  426. /* This is a test for g_filename_from_utf8, even if it does not look like it */
  427. dest = g_filename_from_utf8 (NUMBERS, strlen (NUMBERS), NULL, NULL, NULL);
  428. if (0 != strcmp (dest, NUMBERS))
  429. return FAILED ("problem [%s] and [%s]", dest, NUMBERS);
  430. g_free (dest);
  431. return OK;
  432. }
  433. RESULT
  434. test_strescape ()
  435. {
  436. gchar *str;
  437. str = g_strescape ("abc", NULL);
  438. if (strcmp ("abc", str))
  439. return FAILED ("#1");
  440. str = g_strescape ("\t\b\f\n\r\\\"abc", NULL);
  441. if (strcmp ("\\t\\b\\f\\n\\r\\\\\\\"abc", str))
  442. return FAILED ("#2 %s", str);
  443. str = g_strescape ("\001abc", NULL);
  444. if (strcmp ("\\001abc", str))
  445. return FAILED ("#3 %s", str);
  446. str = g_strescape ("\001abc", "\001");
  447. if (strcmp ("\001abc", str))
  448. return FAILED ("#3 %s", str);
  449. return OK;
  450. }
  451. RESULT
  452. test_ascii_strncasecmp ()
  453. {
  454. int n;
  455. n = g_ascii_strncasecmp ("123", "123", 1);
  456. if (n != 0)
  457. return FAILED ("Should have been 0");
  458. n = g_ascii_strncasecmp ("423", "123", 1);
  459. if (n != 3)
  460. return FAILED ("Should have been 3, got %d", n);
  461. n = g_ascii_strncasecmp ("123", "423", 1);
  462. if (n != -3)
  463. return FAILED ("Should have been -3, got %d", n);
  464. n = g_ascii_strncasecmp ("1", "1", 10);
  465. if (n != 0)
  466. return FAILED ("Should have been 0, got %d", n);
  467. return OK;
  468. }
  469. RESULT
  470. test_ascii_strdown ()
  471. {
  472. const gchar *a = "~09+AaBcDeFzZ$0909EmPAbCdEEEEEZZZZAAA";
  473. const gchar *b = "~09+aabcdefzz$0909empabcdeeeeezzzzaaa";
  474. gchar *c;
  475. gint n, l;
  476. l = (gint)strlen (b);
  477. c = g_ascii_strdown (a, l);
  478. n = g_ascii_strncasecmp (b, c, l);
  479. if (n != 0) {
  480. g_free (c);
  481. return FAILED ("Should have been 0, got %d", n);
  482. }
  483. g_free (c);
  484. return OK;
  485. }
  486. static Test strutil_tests [] = {
  487. {"g_strfreev", test_strfreev},
  488. {"g_strconcat", test_concat},
  489. {"g_strsplit", test_split},
  490. {"g_strsplit_set", test_split_set},
  491. {"g_strreverse", test_strreverse},
  492. {"g_strjoin", test_strjoin},
  493. {"g_strchug", test_strchug},
  494. {"g_strchomp", test_strchomp},
  495. {"g_strstrip", test_strstrip},
  496. {"g_filename_to_uri", test_filename_to_uri},
  497. {"g_filename_from_uri", test_filename_from_uri},
  498. {"g_ascii_xdigit_value", test_ascii_xdigit_value},
  499. {"g_strdelimit", test_strdelimit},
  500. {"g_strlcpy", test_strlcpy},
  501. {"g_strescape", test_strescape},
  502. {"g_ascii_strncasecmp", test_ascii_strncasecmp },
  503. {"g_ascii_strdown", test_ascii_strdown },
  504. {NULL, NULL}
  505. };
  506. DEFINE_TEST_GROUP_INIT(strutil_tests_init, strutil_tests)