string.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include <glib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include "test.h"
  5. #define sfail(k,p) if (s->str [p] != k) { g_string_free (s,TRUE); return FAILED("Got %s, Failed at %d, expected '%c'", s->str, p, k);}
  6. RESULT
  7. test_append_speed()
  8. {
  9. GString *s = g_string_new("");
  10. gint i;
  11. for(i = 0; i < 1024; i++) {
  12. g_string_append(s, "x");
  13. }
  14. if(strlen (s->str) != 1024) {
  15. return FAILED("Incorrect string size, got: %s %d",
  16. s->str, strlen(s->str));
  17. }
  18. g_string_free (s, TRUE);
  19. return OK;
  20. }
  21. RESULT
  22. test_append_c_speed()
  23. {
  24. GString *s = g_string_new("");
  25. gint i;
  26. for(i = 0; i < 1024; i++) {
  27. g_string_append_c(s, 'x');
  28. }
  29. if(strlen(s->str) != 1024) {
  30. return FAILED("Incorrect string size, got: %s %d", s->str,
  31. strlen(s->str));
  32. }
  33. g_string_free(s, TRUE);
  34. return OK;
  35. }
  36. RESULT
  37. test_gstring ()
  38. {
  39. GString *s = g_string_new_len ("My stuff", 2);
  40. char *ret;
  41. int i;
  42. if (strcmp (s->str, "My") != 0)
  43. return "Expected only 'My' on the string";
  44. g_string_free (s, TRUE);
  45. s = g_string_new_len ("My\0\0Rest", 6);
  46. if (s->str [2] != 0)
  47. return "Null was not copied";
  48. if (strcmp (s->str+4, "Re") != 0){
  49. return "Did not find the 'Re' part";
  50. }
  51. g_string_append (s, "lalalalalalalalalalalalalalalalalalalalalalal");
  52. if (s->str [2] != 0)
  53. return "Null as not copied";
  54. if (strncmp (s->str+4, "Relala", 6) != 0){
  55. return FAILED("Did not copy correctly, got: %s", s->str+4);
  56. }
  57. g_string_free (s, TRUE);
  58. s = g_string_new ("");
  59. for (i = 0; i < 1024; i++){
  60. g_string_append_c (s, 'x');
  61. }
  62. if (strlen (s->str) != 1024){
  63. return FAILED("Incorrect string size, got: %s %d\n", s->str, strlen (s->str));
  64. }
  65. g_string_free (s, TRUE);
  66. s = g_string_new ("hola");
  67. g_string_sprintfa (s, "%s%d", ", bola", 5);
  68. if (strcmp (s->str, "hola, bola5") != 0){
  69. return FAILED("Incorrect data, got: %s\n", s->str);
  70. }
  71. g_string_free (s, TRUE);
  72. s = g_string_new ("Hola");
  73. g_string_printf (s, "Dingus");
  74. /* Test that it does not release it */
  75. ret = g_string_free (s, FALSE);
  76. g_free (ret);
  77. s = g_string_new_len ("H" "\000" "H", 3);
  78. g_string_append_len (s, "1" "\000" "2", 3);
  79. sfail ('H', 0);
  80. sfail ( 0, 1);
  81. sfail ('H', 2);
  82. sfail ('1', 3);
  83. sfail ( 0, 4);
  84. sfail ('2', 5);
  85. g_string_free (s, TRUE);
  86. return OK;
  87. }
  88. RESULT
  89. test_sized ()
  90. {
  91. GString *s = g_string_sized_new (20);
  92. if (s->str [0] != 0)
  93. return FAILED ("Expected an empty string");
  94. if (s->len != 0)
  95. return FAILED ("Expected an empty len");
  96. g_string_free (s, TRUE);
  97. return NULL;
  98. }
  99. RESULT
  100. test_truncate ()
  101. {
  102. GString *s = g_string_new ("0123456789");
  103. g_string_truncate (s, 3);
  104. if (strlen (s->str) != 3)
  105. return FAILED ("size of string should have been 3, instead it is [%s]\n", s->str);
  106. g_string_free (s, TRUE);
  107. s = g_string_new ("a");
  108. s = g_string_truncate (s, 10);
  109. if (strlen (s->str) != 1)
  110. return FAILED ("The size is not 1");
  111. g_string_truncate (s, (gsize)-1);
  112. if (strlen (s->str) != 1)
  113. return FAILED ("The size is not 1");
  114. g_string_truncate (s, 0);
  115. if (strlen (s->str) != 0)
  116. return FAILED ("The size is not 0");
  117. g_string_free (s, TRUE);
  118. return NULL;
  119. }
  120. RESULT
  121. test_prepend ()
  122. {
  123. GString *s = g_string_new ("dingus");
  124. g_string_prepend (s, "one");
  125. if (strcmp (s->str, "onedingus") != 0)
  126. return FAILED ("Failed, expected onedingus, got [%s]", s->str);
  127. g_string_free (s, TRUE);
  128. /* This is to force the code that where stuff does not fit in the allocated block */
  129. s = g_string_sized_new (1);
  130. g_string_prepend (s, "one");
  131. if (strcmp (s->str, "one") != 0)
  132. return FAILED ("Got erroneous result, expected [one] got [%s]", s->str);
  133. g_string_free (s, TRUE);
  134. /* This is to force the path where things fit */
  135. s = g_string_new ("123123123123123123123123");
  136. g_string_truncate (s, 1);
  137. if (strcmp (s->str, "1") != 0)
  138. return FAILED ("Expected [1] string, got [%s]", s->str);
  139. g_string_prepend (s, "pre");
  140. if (strcmp (s->str, "pre1") != 0)
  141. return FAILED ("Expected [pre1], got [%s]", s->str);
  142. g_string_free (s, TRUE);
  143. return NULL;
  144. }
  145. RESULT
  146. test_appendlen ()
  147. {
  148. GString *s = g_string_new ("");
  149. g_string_append_len (s, "boo\000x", 0);
  150. if (s->len != 0)
  151. return FAILED ("The length is not zero %d", s->len);
  152. g_string_append_len (s, "boo\000x", 5);
  153. if (s->len != 5)
  154. return FAILED ("The length is not five %d", s->len);
  155. g_string_append_len (s, "ha", -1);
  156. if (s->len != 7)
  157. return FAILED ("The length is not seven %d", s->len);
  158. g_string_free (s, TRUE);
  159. return NULL;
  160. }
  161. RESULT
  162. test_macros ()
  163. {
  164. char *s = g_strdup (G_STRLOC);
  165. char *p = strchr (s + 2, ':');
  166. int n;
  167. if (p == NULL)
  168. return FAILED ("Did not find a separator");
  169. n = atoi (p+1);
  170. if (n <= 0)
  171. return FAILED ("did not find a valid line number");
  172. *p = 0;
  173. if (strcmp (s + strlen(s) - 8 , "string.c") != 0)
  174. return FAILED ("This did not store the filename on G_STRLOC");
  175. g_free (s);
  176. return NULL;
  177. }
  178. static Test string_tests [] = {
  179. {"append-speed", test_append_speed},
  180. {"append_c-speed", test_append_c_speed},
  181. {"ctor+append", test_gstring },
  182. {"ctor+sized", test_sized },
  183. {"truncate", test_truncate },
  184. {"prepend", test_prepend },
  185. {"append_len", test_appendlen },
  186. {"macros", test_macros },
  187. {NULL, NULL}
  188. };
  189. DEFINE_TEST_GROUP_INIT(string_tests_init, string_tests)