[msvc] Update csproj files
[mono.git] / eglib / test / string.c
1 #include <glib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include "test.h"
5
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);}
7
8 RESULT
9 test_append_speed()
10 {
11         GString *s = g_string_new("");
12         gint i;
13         
14         for(i = 0; i < 1024; i++) {
15                 g_string_append(s, "x");
16         }
17         
18         if(strlen (s->str) != 1024) {
19                 return FAILED("Incorrect string size, got: %s %d", 
20                         s->str, strlen(s->str));
21         }
22         
23         g_string_free (s, TRUE);
24
25         return OK;
26 }
27
28 RESULT
29 test_append_c_speed()
30 {
31         GString *s = g_string_new("");
32         gint i;
33         
34         for(i = 0; i < 1024; i++) {
35                 g_string_append_c(s, 'x');
36         }
37         
38         if(strlen(s->str) != 1024) {
39                 return FAILED("Incorrect string size, got: %s %d", s->str, 
40                         strlen(s->str));
41         }
42         
43         g_string_free(s, TRUE);
44
45         return OK;
46 }
47
48 RESULT
49 test_gstring ()
50 {
51         GString *s = g_string_new_len ("My stuff", 2);
52         char *ret;
53         int i;
54
55         if (strcmp (s->str, "My") != 0)
56                 return "Expected only 'My' on the string";
57         g_string_free (s, TRUE);
58
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";
64         }
65
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);
71         }
72
73         g_string_free (s, TRUE);
74
75         s = g_string_new ("");
76         for (i = 0; i < 1024; i++){
77                 g_string_append_c (s, 'x');
78         }
79         if (strlen (s->str) != 1024){
80                 return FAILED("Incorrect string size, got: %s %d\n", s->str, strlen (s->str));
81         }
82         g_string_free (s, TRUE);
83
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);
88         }
89         g_string_free (s, TRUE);
90
91         s = g_string_new ("Hola");
92         g_string_printf (s, "Dingus");
93         
94         /* Test that it does not release it */
95         ret = g_string_free (s, FALSE);
96         g_free (ret);
97
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);
107         
108         return OK;
109 }
110
111 RESULT
112 test_sized ()
113 {
114         GString *s = g_string_sized_new (20);
115
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");
120
121         g_string_free (s, TRUE);
122         
123         return NULL;
124 }
125
126 RESULT
127 test_truncate ()
128 {
129         GString *s = g_string_new ("0123456789");
130         g_string_truncate (s, 3);
131
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);
135         
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");
146         
147         g_string_free (s, TRUE);
148
149         return NULL;
150 }
151
152 RESULT
153 test_prepend ()
154 {
155         GString *s = g_string_new ("dingus");
156         g_string_prepend (s, "one");
157
158         if (strcmp (s->str, "onedingus") != 0)
159                 return FAILED ("Failed, expected onedingus, got [%s]", s->str);
160
161         g_string_free (s, TRUE);
162
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);
169
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);
175
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);
180         
181         return NULL;
182 }
183
184 RESULT
185 test_appendlen ()
186 {
187         GString *s = g_string_new ("");
188
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);
198                 
199         g_string_free (s, TRUE);
200
201         return NULL;
202 }
203
204 RESULT
205 test_macros ()
206 {
207         char *s = g_strdup (G_STRLOC);
208         char *p = strchr (s + 2, ':');
209         int n;
210         
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");
216
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");
220         
221         g_free (s);
222         return NULL;
223 }
224
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}
235 };
236
237 DEFINE_TEST_GROUP_INIT(string_tests_init, string_tests)