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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
34 gboolean failed
= FALSE
;
36 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
39 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
41 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
44 g_print ("."); fflush (stdout); \
47 #define C2P(c) ((gpointer) ((long) (c)))
48 #define P2C(p) ((gchar) ((long) (p)))
50 #define GLIB_TEST_STRING "el dorado "
51 #define GLIB_TEST_STRING_5 "el do"
62 GStringChunk
*string_chunk
;
64 gchar
*tmp_string
= NULL
, *tmp_string_2
;
66 GString
*string1
, *string2
;
68 string_chunk
= g_string_chunk_new (1024);
70 for (i
= 0; i
< 100000; i
++)
72 tmp_string
= g_string_chunk_insert (string_chunk
, "hi pete");
74 if (strcmp ("hi pete", tmp_string
) != 0)
75 g_error ("string chunks are broken.\n");
78 tmp_string_2
= g_string_chunk_insert_const (string_chunk
, tmp_string
);
80 g_assert (tmp_string_2
!= tmp_string
&&
81 strcmp(tmp_string_2
, tmp_string
) == 0);
83 tmp_string
= g_string_chunk_insert_const (string_chunk
, tmp_string
);
85 g_assert (tmp_string_2
== tmp_string
);
87 g_string_chunk_free (string_chunk
);
89 string1
= g_string_new ("hi pete!");
90 string2
= g_string_new ("");
92 g_assert (string1
!= NULL
);
93 g_assert (string2
!= NULL
);
94 g_assert (strlen (string1
->str
) == string1
->len
);
95 g_assert (strlen (string2
->str
) == string2
->len
);
96 g_assert (string2
->len
== 0);
97 g_assert (strcmp ("hi pete!", string1
->str
) == 0);
98 g_assert (strcmp ("", string2
->str
) == 0);
100 for (i
= 0; i
< 10000; i
++)
101 g_string_append_c (string1
, 'a'+(i
%26));
103 g_assert((strlen("hi pete!") + 10000) == string1
->len
);
104 g_assert((strlen("hi pete!") + 10000) == strlen(string1
->str
));
107 /* MSVC and mingw32 use the same run-time C library, which doesn't like
108 the %10000.10000f format... */
109 g_string_sprintf (string2
, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
110 "this pete guy sure is a wuss, like he's the number ",
112 " wuss. everyone agrees.\n",
114 10, 666, 15, 15, 666.666666666, 666.666666666);
116 g_string_sprintf (string2
, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
117 "this pete guy sure is a wuss, like he's the number ",
119 " wuss. everyone agrees.\n",
121 10, 666, 15, 15, 666.666666666, 666.666666666);
124 g_string_free (string1
, TRUE
);
125 g_string_free (string2
, TRUE
);
128 string1
= g_string_new ("firsthalf");
129 g_string_append (string1
, "lasthalf");
130 g_assert (strcmp (string1
->str
, "firsthalflasthalf") == 0);
131 g_string_free (string1
, TRUE
);
135 string1
= g_string_new ("firsthalf");
136 g_string_append_len (string1
, "lasthalfjunkjunk", strlen ("lasthalf"));
137 g_assert (strcmp (string1
->str
, "firsthalflasthalf") == 0);
138 g_string_free (string1
, TRUE
);
141 string1
= g_string_new ("lasthalf");
142 g_string_prepend (string1
, "firsthalf");
143 g_assert (strcmp (string1
->str
, "firsthalflasthalf") == 0);
144 g_string_free (string1
, TRUE
);
147 string1
= g_string_new ("lasthalf");
148 g_string_prepend_len (string1
, "firsthalfjunkjunk", strlen ("firsthalf"));
149 g_assert (strcmp (string1
->str
, "firsthalflasthalf") == 0);
150 g_string_free (string1
, TRUE
);
153 string1
= g_string_new ("firstlast");
154 g_string_insert (string1
, 5, "middle");
155 g_assert (strcmp (string1
->str
, "firstmiddlelast") == 0);
156 g_string_free (string1
, TRUE
);
158 /* insert with pos == end of the string */
159 string1
= g_string_new ("firstmiddle");
160 g_string_insert (string1
, strlen ("firstmiddle"), "last");
161 g_assert (strcmp (string1
->str
, "firstmiddlelast") == 0);
162 g_string_free (string1
, TRUE
);
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
);
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
);
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
);
184 string1
= g_string_new ("test");
185 string2
= g_string_new ("te");
186 g_assert (! g_string_equal(string1
, string2
));
187 g_string_append (string2
, "st");
188 g_assert (g_string_equal(string1
, string2
));
189 g_string_free (string1
, TRUE
);
190 g_string_free (string2
, TRUE
);
192 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
193 string1
= g_string_new ("fiddle");
194 string2
= g_string_new ("fiddle");
195 g_assert (g_string_equal(string1
, string2
));
196 g_string_append_c(string1
, '\0');
197 g_assert (! g_string_equal(string1
, string2
));
198 g_string_append_c(string2
, '\0');
199 g_assert (g_string_equal(string1
, string2
));
200 g_string_append_c(string1
, 'x');
201 g_string_append_c(string2
, 'y');
202 g_assert (! g_string_equal(string1
, string2
));
203 g_assert (string1
->len
== 8);
204 g_string_append(string1
, "yzzy");
205 g_assert (string1
->len
== 12);
206 g_assert ( memcmp(string1
->str
, "fiddle\0xyzzy", 13) == 0);
207 g_string_insert(string1
, 1, "QED");
208 g_assert ( memcmp(string1
->str
, "fQEDiddle\0xyzzy", 16) == 0);
209 g_string_free (string1
, TRUE
);
210 g_string_free (string2
, TRUE
);