1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #undef G_DISABLE_ASSERT
33 #include "glib/gprintf.h"
36 gboolean failed = FALSE;
38 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
41 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
43 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
46 g_print ("."); fflush (stdout); \
49 #define C2P(c) ((gpointer) ((long) (c)))
50 #define P2C(p) ((gchar) ((long) (p)))
52 #define GLIB_TEST_STRING "el dorado "
53 #define GLIB_TEST_STRING_5 "el do"
64 GStringChunk *string_chunk;
66 gchar *tmp_string = NULL, *tmp_string_2;
68 GString *string1, *string2;
70 string_chunk = g_string_chunk_new (1024);
72 for (i = 0; i < 100000; i ++)
74 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
76 if (strcmp ("hi pete", tmp_string) != 0)
77 g_error ("string chunks are broken.\n");
80 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
82 g_assert (tmp_string_2 != tmp_string &&
83 strcmp(tmp_string_2, tmp_string) == 0);
85 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
87 g_assert (tmp_string_2 == tmp_string);
89 g_string_chunk_free (string_chunk);
91 string1 = g_string_new ("hi pete!");
92 string2 = g_string_new (NULL);
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);
102 for (i = 0; i < 10000; i++)
103 g_string_append_c (string1, 'a'+(i%26));
105 g_assert((strlen("hi pete!") + 10000) == string1->len);
106 g_assert((strlen("hi pete!") + 10000) == strlen(string1->str));
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 ",
114 " wuss. everyone agrees.\n",
116 10, 666, 15, 15, 666.666666666, 666.666666666);
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 ",
121 " wuss. everyone agrees.\n",
123 10, 666, 15, 15, 666.666666666, 666.666666666);
126 g_string_free (string1, TRUE);
127 g_string_free (string2, TRUE);
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);
137 string1 = g_string_new ("firsthalf");
138 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
139 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
140 g_string_free (string1, TRUE);
143 string1 = g_string_new ("lasthalf");
144 g_string_prepend (string1, "firsthalf");
145 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
146 g_string_free (string1, TRUE);
149 string1 = g_string_new ("lasthalf");
150 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
151 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
152 g_string_free (string1, TRUE);
155 string1 = g_string_new ("firstlast");
156 g_string_insert (string1, 5, "middle");
157 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
158 g_string_free (string1, TRUE);
160 /* insert with pos == end of the string */
161 string1 = g_string_new ("firstmiddle");
162 g_string_insert (string1, strlen ("firstmiddle"), "last");
163 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
164 g_string_free (string1, TRUE);
168 string1 = g_string_new ("firstlast");
169 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
170 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
171 g_string_free (string1, TRUE);
173 /* insert_len with magic -1 pos for append */
174 string1 = g_string_new ("first");
175 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
176 g_assert (strcmp (string1->str, "firstlast") == 0);
177 g_string_free (string1, TRUE);
179 /* insert_len with magic -1 len for strlen-the-string */
180 string1 = g_string_new ("first");
181 g_string_insert_len (string1, 5, "last", -1);
182 g_assert (strcmp (string1->str, "firstlast") == 0);
183 g_string_free (string1, TRUE);
185 /* insert_len with string overlap */
186 string1 = g_string_new ("textbeforetextafter");
187 g_string_insert_len (string1, 10, string1->str + 8, 5);
188 g_assert (strcmp (string1->str, "textbeforeretextextafter") == 0);
189 g_string_free (string1, TRUE);
191 string1 = g_string_new ("boring text");
192 g_string_insert_len (string1, 7, string1->str + 2, 4);
193 g_assert (strcmp (string1->str, "boring ringtext") == 0);
194 g_string_free (string1, TRUE);
196 string1 = g_string_new ("boring text");
197 g_string_insert_len (string1, 6, string1->str + 7, 4);
198 g_assert (strcmp (string1->str, "boringtext text") == 0);
199 g_string_free (string1, TRUE);
201 /* assign_len with string overlap */
202 string1 = g_string_new ("textbeforetextafter");
203 g_string_assign (string1, string1->str + 10);
204 g_assert (strcmp (string1->str, "textafter") == 0);
205 g_string_free (string1, TRUE);
207 string1 = g_string_new ("boring text");
208 g_string_assign (string1, string1->str);
209 g_assert (strcmp (string1->str, "boring text") == 0);
210 g_string_free (string1, TRUE);
213 string1 = g_string_new ("test");
214 string2 = g_string_new ("te");
215 g_assert (! g_string_equal(string1, string2));
216 g_string_append (string2, "st");
217 g_assert (g_string_equal(string1, string2));
218 g_string_free (string1, TRUE);
219 g_string_free (string2, TRUE);
221 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
222 string1 = g_string_new ("fiddle");
223 string2 = g_string_new ("fiddle");
224 g_assert (g_string_equal(string1, string2));
225 g_string_append_c(string1, '\0');
226 g_assert (! g_string_equal(string1, string2));
227 g_string_append_c(string2, '\0');
228 g_assert (g_string_equal(string1, string2));
229 g_string_append_c(string1, 'x');
230 g_string_append_c(string2, 'y');
231 g_assert (! g_string_equal(string1, string2));
232 g_assert (string1->len == 8);
233 g_string_append(string1, "yzzy");
234 g_assert (string1->len == 12);
235 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
236 g_string_insert(string1, 1, "QED");
237 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
238 g_string_printf (string1, "fiddle%cxyzzy", '\0');
239 g_assert (string1->len == 12);
240 g_assert (memcmp (string1->str, "fiddle\0xyzzy", 13) == 0);
242 g_string_free (string1, TRUE);
243 g_string_free (string2, TRUE);
245 g_assert (g_str_has_prefix("foobar", "gazonk") == FALSE);
246 g_assert (g_str_has_prefix("xyzzy", "xyzzy") == TRUE);
247 g_assert (g_str_has_prefix("xyzzy", "xy") == TRUE);
248 g_assert (g_str_has_prefix("xyzzy", "") == TRUE);
249 g_assert (g_str_has_prefix("xyz", "xyzzy") == FALSE);
250 g_assert (g_str_has_prefix("", "xyzzy") == FALSE);
251 g_assert (g_str_has_prefix("", "") == TRUE);
253 g_assert (g_str_has_suffix("foobar", "gazonk") == FALSE);
254 g_assert (g_str_has_suffix("xyzzy", "xyzzy") == TRUE);
255 g_assert (g_str_has_suffix("xyzzy", "zy") == TRUE);
256 g_assert (g_str_has_suffix("xyzzy", "") == TRUE);
257 g_assert (g_str_has_suffix("zzy", "xyzzy") == FALSE);
258 g_assert (g_str_has_suffix("", "xyzzy") == FALSE);
259 g_assert (g_str_has_suffix("", "") == TRUE);
261 tmp_string = (gchar *) g_malloc (10);
262 g_snprintf (tmp_string, 10, "%2$s %1$s", "a", "b");
263 g_assert (strcmp (tmp_string, "b a") == 0);