f80c438602395c1b1a4a2bd159612585bff49e24
[platform/upstream/glib.git] / tests / string-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #undef G_DISABLE_ASSERT
28 #undef G_LOG_DOMAIN
29
30 #include <stdio.h>
31 #include <string.h>
32 #include "glib.h"
33 #include "glib/gprintf.h"
34
35 int array[10000];
36 gboolean failed = FALSE;
37
38 #define TEST(m,cond)    G_STMT_START { failed = !(cond); \
39 if (failed) \
40   { if (!m) \
41       g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
42     else \
43       g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
44   } \
45 else \
46   g_print ("."); fflush (stdout); \
47 } G_STMT_END
48
49 #define C2P(c)          ((gpointer) ((long) (c)))
50 #define P2C(p)          ((gchar) ((long) (p)))
51
52 #define GLIB_TEST_STRING "el dorado "
53 #define GLIB_TEST_STRING_5 "el do"
54
55 typedef struct {
56         guint age;
57         gchar name[40];
58 } GlibTestInfo;
59
60 int
61 main (int   argc,
62       char *argv[])
63 {
64   GStringChunk *string_chunk;
65
66   gchar *tmp_string = NULL, *tmp_string_2;
67   gint i;
68   GString *string1, *string2;
69
70   string_chunk = g_string_chunk_new (1024);
71
72   for (i = 0; i < 100000; i ++)
73     {
74       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
75
76       if (strcmp ("hi pete", tmp_string) != 0)
77         g_error ("string chunks are broken.\n");
78     }
79
80   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
81
82   g_assert (tmp_string_2 != tmp_string &&
83             strcmp(tmp_string_2, tmp_string) == 0);
84
85   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
86
87   g_assert (tmp_string_2 == tmp_string);
88
89   g_string_chunk_free (string_chunk);
90
91   string1 = g_string_new ("hi pete!");
92   string2 = g_string_new (NULL);
93
94   g_assert (string1 != NULL);
95   g_assert (string2 != NULL);
96   g_assert (strlen (string1->str) == string1->len);
97   g_assert (strlen (string2->str) == string2->len);
98   g_assert (string2->len == 0);
99   g_assert (strcmp ("hi pete!", string1->str) == 0);
100   g_assert (strcmp ("", string2->str) == 0);
101
102   for (i = 0; i < 10000; i++)
103     g_string_append_c (string1, 'a'+(i%26));
104
105   g_assert((strlen("hi pete!") + 10000) == string1->len);
106   g_assert((strlen("hi pete!") + 10000) == strlen(string1->str));
107
108 #ifndef G_OS_WIN32
109   /* MSVC and mingw32 use the same run-time C library, which doesn't like
110      the %10000.10000f format... */
111   g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
112                    "this pete guy sure is a wuss, like he's the number ",
113                    1,
114                    " wuss.  everyone agrees.\n",
115                    string1->str,
116                    10, 666, 15, 15, 666.666666666, 666.666666666);
117 #else
118   g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
119                    "this pete guy sure is a wuss, like he's the number ",
120                    1,
121                    " wuss.  everyone agrees.\n",
122                    string1->str,
123                    10, 666, 15, 15, 666.666666666, 666.666666666);
124 #endif
125   
126   g_string_free (string1, TRUE);
127   g_string_free (string2, TRUE);
128
129   /* append */
130   string1 = g_string_new ("firsthalf");
131   g_string_append (string1, "lasthalf");
132   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
133   g_string_free (string1, TRUE);
134
135   /* append_len */
136   string1 = g_string_new ("firsthalf");
137   g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
138   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
139   g_string_free (string1, TRUE);  
140   
141   /* prepend */
142   string1 = g_string_new ("lasthalf");
143   g_string_prepend (string1, "firsthalf");
144   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
145   g_string_free (string1, TRUE);
146
147   /* prepend_len */
148   string1 = g_string_new ("lasthalf");
149   g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
150   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
151   g_string_free (string1, TRUE);
152   
153   /* insert */
154   string1 = g_string_new ("firstlast");
155   g_string_insert (string1, 5, "middle");
156   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
157   g_string_free (string1, TRUE);
158
159   /* insert with pos == end of the string */
160   string1 = g_string_new ("firstmiddle");
161   g_string_insert (string1, strlen ("firstmiddle"), "last");
162   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
163   g_string_free (string1, TRUE);
164   
165   /* insert_len */
166   string1 = g_string_new ("firstlast");
167   g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
168   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
169   g_string_free (string1, TRUE);
170
171   /* insert_len with magic -1 pos for append */
172   string1 = g_string_new ("first");
173   g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
174   g_assert (strcmp (string1->str, "firstlast") == 0);
175   g_string_free (string1, TRUE);
176   
177   /* insert_len with magic -1 len for strlen-the-string */
178   string1 = g_string_new ("first");
179   g_string_insert_len (string1, 5, "last", -1);
180   g_assert (strcmp (string1->str, "firstlast") == 0);
181   g_string_free (string1, TRUE);
182
183   /* insert_len with string overlap */
184   string1 = g_string_new ("textbeforetextafter");
185   g_string_insert_len (string1, 10, string1->str + 8, 5);
186   g_assert (strcmp (string1->str, "textbeforeretextextafter") == 0);
187   g_string_free (string1, TRUE);
188
189   string1 = g_string_new ("boring text");
190   g_string_insert_len (string1, 7, string1->str + 2, 4);
191   g_assert (strcmp (string1->str, "boring ringtext") == 0);
192   g_string_free (string1, TRUE);
193
194   string1 = g_string_new ("boring text");
195   g_string_insert_len (string1, 6, string1->str + 7, 4);
196   g_assert (strcmp (string1->str, "boringtext text") == 0);
197   g_string_free (string1, TRUE);
198
199   /* assign_len with string overlap */
200   string1 = g_string_new ("textbeforetextafter");
201   g_string_assign (string1, string1->str + 10);
202   g_assert (strcmp (string1->str, "textafter") == 0);
203   g_string_free (string1, TRUE);
204
205   string1 = g_string_new ("boring text");
206   g_string_assign (string1, string1->str);
207   g_assert (strcmp (string1->str, "boring text") == 0);
208   g_string_free (string1, TRUE);
209
210   /* insert_unichar with insertion in middle */
211   string1 = g_string_new ("firsthalf");
212   g_string_insert_unichar (string1, 5, 0x0041);
213   g_assert (strcmp (string1->str, "first\x41half") == 0);
214   g_string_free (string1, TRUE);
215
216   string1 = g_string_new ("firsthalf");
217   g_string_insert_unichar (string1, 5, 0x0298);
218   g_assert (strcmp (string1->str, "first\xCA\x98half") == 0);
219   g_string_free (string1, TRUE);
220
221   string1 = g_string_new ("firsthalf");
222   g_string_insert_unichar (string1, 5, 0xFFFD);
223   g_assert (strcmp (string1->str, "first\xEF\xBF\xBDhalf") == 0);
224   g_string_free (string1, TRUE);
225
226   string1 = g_string_new ("firsthalf");
227   g_string_insert_unichar (string1, 5, 0x1D100);
228   g_assert (strcmp (string1->str, "first\xF0\x9D\x84\x80half") == 0);
229   g_string_free (string1, TRUE);
230
231   /* insert_unichar with insertion at end */
232   string1 = g_string_new ("start");
233   g_string_insert_unichar (string1, -1, 0x0041);
234   g_assert (strcmp (string1->str, "start\x41") == 0);
235   g_string_free (string1, TRUE);
236
237   string1 = g_string_new ("start");
238   g_string_insert_unichar (string1, -1, 0x0298);
239   g_assert (strcmp (string1->str, "start\xCA\x98") == 0);
240   g_string_free (string1, TRUE);
241
242   string1 = g_string_new ("start");
243   g_string_insert_unichar (string1, -1, 0xFFFD);
244   g_assert (strcmp (string1->str, "start\xEF\xBF\xBD") == 0);
245   g_string_free (string1, TRUE);
246
247   string1 = g_string_new ("start");
248   g_string_insert_unichar (string1, -1, 0x1D100);
249   g_assert (strcmp (string1->str, "start\xF0\x9D\x84\x80") == 0);
250   g_string_free (string1, TRUE);
251
252   /* g_string_equal */
253   string1 = g_string_new ("test");
254   string2 = g_string_new ("te");
255   g_assert (! g_string_equal(string1, string2));
256   g_string_append (string2, "st");
257   g_assert (g_string_equal(string1, string2));
258   g_string_free (string1, TRUE);
259   g_string_free (string2, TRUE);
260
261   /* overwriting functions */
262   string1 = g_string_new ("testing");
263
264   g_string_overwrite (string1, 4, " and expand");
265   g_assert (15 == string1->len);
266   g_assert ('\0' == string1->str[15]);
267   g_assert (g_str_equal ("test and expand", string1->str));
268
269   g_string_overwrite (string1, 5, "NOT-");
270   g_assert (15 == string1->len);
271   g_assert ('\0' == string1->str[15]);
272   g_assert (g_str_equal ("test NOT-expand", string1->str));
273
274   g_string_overwrite_len (string1, 9, "blablabla", 6);
275   g_assert (15 == string1->len);
276   g_assert ('\0' == string1->str[15]);
277   g_assert (g_str_equal ("test NOT-blabla", string1->str));
278
279   g_string_free (string1, TRUE);
280
281   /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
282   string1 = g_string_new ("fiddle");
283   string2 = g_string_new ("fiddle");
284   g_assert (g_string_equal(string1, string2));
285   g_string_append_c(string1, '\0');
286   g_assert (! g_string_equal(string1, string2));
287   g_string_append_c(string2, '\0');
288   g_assert (g_string_equal(string1, string2));
289   g_string_append_c(string1, 'x');
290   g_string_append_c(string2, 'y');
291   g_assert (! g_string_equal(string1, string2));
292   g_assert (string1->len == 8);
293   g_string_append(string1, "yzzy");
294   g_assert (string1->len == 12);
295   g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
296   g_string_insert(string1, 1, "QED");
297   g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
298   g_string_printf (string1, "fiddle%cxyzzy", '\0');
299   g_assert (string1->len == 12);
300   g_assert (memcmp (string1->str, "fiddle\0xyzzy", 13) == 0);
301
302   g_string_free (string1, TRUE);
303   g_string_free (string2, TRUE);
304   
305   g_assert (g_str_has_prefix("foobar", "gazonk") == FALSE);
306   g_assert (g_str_has_prefix("xyzzy", "xyzzy") == TRUE);
307   g_assert (g_str_has_prefix("xyzzy", "xy") == TRUE);
308   g_assert (g_str_has_prefix("xyzzy", "") == TRUE);
309   g_assert (g_str_has_prefix("xyz", "xyzzy") == FALSE);
310   g_assert (g_str_has_prefix("", "xyzzy") == FALSE);
311   g_assert (g_str_has_prefix("", "") == TRUE);
312
313   g_assert (g_str_has_suffix("foobar", "gazonk") == FALSE);
314   g_assert (g_str_has_suffix("xyzzy", "xyzzy") == TRUE);
315   g_assert (g_str_has_suffix("xyzzy", "zy") == TRUE);
316   g_assert (g_str_has_suffix("xyzzy", "") == TRUE);
317   g_assert (g_str_has_suffix("zzy", "xyzzy") == FALSE);
318   g_assert (g_str_has_suffix("", "xyzzy") == FALSE);
319   g_assert (g_str_has_suffix("", "") == TRUE);
320
321   tmp_string = (gchar *) g_malloc (10);
322   g_snprintf (tmp_string, 10, "%2$s %1$s", "a", "b");
323   g_assert (strcmp (tmp_string, "b a") == 0);
324   g_free (tmp_string);
325
326   return 0;
327 }
328
329