2010-04-16 Sebastien Pouliot <sebastien@ximian.com>
[mono/afaerber.git] / eglib / test / string.c
blob02ad0ad3ea3691108c9be515c4d80c84ba89e81d
1 #include <glib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include "test.h"
6 #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);}
8 RESULT
9 test_append_speed()
11 GString *s = g_string_new("");
12 gint i;
14 for(i = 0; i < 1024; i++) {
15 g_string_append(s, "x");
18 if(strlen (s->str) != 1024) {
19 return FAILED("Incorrect string size, got: %s %d",
20 s->str, strlen(s->str));
23 g_string_free (s, TRUE);
25 return OK;
28 RESULT
29 test_append_c_speed()
31 GString *s = g_string_new("");
32 gint i;
34 for(i = 0; i < 1024; i++) {
35 g_string_append_c(s, 'x');
38 if(strlen(s->str) != 1024) {
39 return FAILED("Incorrect string size, got: %s %d", s->str,
40 strlen(s->str));
43 g_string_free(s, TRUE);
45 return OK;
48 RESULT
49 test_gstring ()
51 GString *s = g_string_new_len ("My stuff", 2);
52 char *ret;
53 int i;
55 if (strcmp (s->str, "My") != 0)
56 return "Expected only 'My' on the string";
57 g_string_free (s, TRUE);
59 s = g_string_new_len ("My\0\0Rest", 6);
60 if (s->str [2] != 0)
61 return "Null was not copied";
62 if (strcmp (s->str+4, "Re") != 0){
63 return "Did not find the 'Re' part";
66 g_string_append (s, "lalalalalalalalalalalalalalalalalalalalalalal");
67 if (s->str [2] != 0)
68 return "Null as not copied";
69 if (strncmp (s->str+4, "Relala", 6) != 0){
70 return FAILED("Did not copy correctly, got: %s", s->str+4);
73 g_string_free (s, TRUE);
75 s = g_string_new ("");
76 for (i = 0; i < 1024; i++){
77 g_string_append_c (s, 'x');
79 if (strlen (s->str) != 1024){
80 return FAILED("Incorrect string size, got: %s %d\n", s->str, strlen (s->str));
82 g_string_free (s, TRUE);
84 s = g_string_new ("hola");
85 g_string_sprintfa (s, "%s%d", ", bola", 5);
86 if (strcmp (s->str, "hola, bola5") != 0){
87 return FAILED("Incorrect data, got: %s\n", s->str);
89 g_string_free (s, TRUE);
91 s = g_string_new ("Hola");
92 g_string_printf (s, "Dingus");
94 /* Test that it does not release it */
95 ret = g_string_free (s, FALSE);
96 g_free (ret);
98 s = g_string_new_len ("H" "\000" "H", 3);
99 g_string_append_len (s, "1" "\000" "2", 3);
100 sfail ('H', 0);
101 sfail ( 0, 1);
102 sfail ('H', 2);
103 sfail ('1', 3);
104 sfail ( 0, 4);
105 sfail ('2', 5);
106 g_string_free (s, TRUE);
108 return OK;
111 RESULT
112 test_sized ()
114 GString *s = g_string_sized_new (20);
116 if (s->str [0] != 0)
117 return FAILED ("Expected an empty string");
118 if (s->len != 0)
119 return FAILED ("Expected an empty len");
121 g_string_free (s, TRUE);
123 return NULL;
126 RESULT
127 test_truncate ()
129 GString *s = g_string_new ("0123456789");
130 g_string_truncate (s, 3);
132 if (strlen (s->str) != 3)
133 return FAILED ("size of string should have been 3, instead it is [%s]\n", s->str);
134 g_string_free (s, TRUE);
136 s = g_string_new ("a");
137 s = g_string_truncate (s, 10);
138 if (strlen (s->str) != 1)
139 return FAILED ("The size is not 1");
140 g_string_truncate (s, (gsize)-1);
141 if (strlen (s->str) != 1)
142 return FAILED ("The size is not 1");
143 g_string_truncate (s, 0);
144 if (strlen (s->str) != 0)
145 return FAILED ("The size is not 0");
147 g_string_free (s, TRUE);
149 return NULL;
152 RESULT
153 test_prepend ()
155 GString *s = g_string_new ("dingus");
156 g_string_prepend (s, "one");
158 if (strcmp (s->str, "onedingus") != 0)
159 return FAILED ("Failed, expected onedingus, got [%s]", s->str);
161 g_string_free (s, TRUE);
163 /* This is to force the code that where stuff does not fit in the allocated block */
164 s = g_string_sized_new (1);
165 g_string_prepend (s, "one");
166 if (strcmp (s->str, "one") != 0)
167 return FAILED ("Got erroneous result, expected [one] got [%s]", s->str);
168 g_string_free (s, TRUE);
170 /* This is to force the path where things fit */
171 s = g_string_new ("123123123123123123123123");
172 g_string_truncate (s, 1);
173 if (strcmp (s->str, "1") != 0)
174 return FAILED ("Expected [1] string, got [%s]", s->str);
176 g_string_prepend (s, "pre");
177 if (strcmp (s->str, "pre1") != 0)
178 return FAILED ("Expected [pre1], got [%s]", s->str);
179 g_string_free (s, TRUE);
181 return NULL;
184 RESULT
185 test_appendlen ()
187 GString *s = g_string_new ("");
189 g_string_append_len (s, "boo\000x", 0);
190 if (s->len != 0)
191 return FAILED ("The length is not zero %d", s->len);
192 g_string_append_len (s, "boo\000x", 5);
193 if (s->len != 5)
194 return FAILED ("The length is not five %d", s->len);
195 g_string_append_len (s, "ha", -1);
196 if (s->len != 7)
197 return FAILED ("The length is not seven %d", s->len);
199 g_string_free (s, TRUE);
201 return NULL;
204 RESULT
205 test_macros ()
207 char *s = g_strdup (G_STRLOC);
208 char *p = strchr (s + 2, ':');
209 int n;
211 if (p == NULL)
212 return FAILED ("Did not find a separator");
213 n = atoi (p+1);
214 if (n <= 0)
215 return FAILED ("did not find a valid line number");
217 *p = 0;
218 if (strcmp (s + strlen(s) - 8 , "string.c") != 0)
219 return FAILED ("This did not store the filename on G_STRLOC");
221 g_free (s);
222 return NULL;
225 static Test string_tests [] = {
226 {"append-speed", test_append_speed},
227 {"append_c-speed", test_append_c_speed},
228 {"ctor+append", test_gstring },
229 {"ctor+sized", test_sized },
230 {"truncate", test_truncate },
231 {"prepend", test_prepend },
232 {"append_len", test_appendlen },
233 {"macros", test_macros },
234 {NULL, NULL}
237 DEFINE_TEST_GROUP_INIT(string_tests_init, string_tests)