applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[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_LOG_DOMAIN
28
29 #include <stdio.h>
30 #include <string.h>
31 #include "glib.h"
32
33 int array[10000];
34 gboolean failed = FALSE;
35
36 #define TEST(m,cond)    G_STMT_START { failed = !(cond); \
37 if (failed) \
38   { if (!m) \
39       g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
40     else \
41       g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
42   } \
43 else \
44   g_print ("."); fflush (stdout); \
45 } G_STMT_END
46
47 #define C2P(c)          ((gpointer) ((long) (c)))
48 #define P2C(p)          ((gchar) ((long) (p)))
49
50 #define GLIB_TEST_STRING "el dorado "
51 #define GLIB_TEST_STRING_5 "el do"
52
53 typedef struct {
54         guint age;
55         gchar name[40];
56 } GlibTestInfo;
57
58 int
59 main (int   argc,
60       char *argv[])
61 {
62   GStringChunk *string_chunk;
63
64   gchar *tmp_string = NULL, *tmp_string_2;
65   gint i;
66   GString *string1, *string2;
67
68   string_chunk = g_string_chunk_new (1024);
69
70   for (i = 0; i < 100000; i ++)
71     {
72       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
73
74       if (strcmp ("hi pete", tmp_string) != 0)
75         g_error ("string chunks are broken.\n");
76     }
77
78   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
79
80   g_assert (tmp_string_2 != tmp_string &&
81             strcmp(tmp_string_2, tmp_string) == 0);
82
83   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
84
85   g_assert (tmp_string_2 == tmp_string);
86
87   g_string_chunk_free (string_chunk);
88
89   string1 = g_string_new ("hi pete!");
90   string2 = g_string_new ("");
91
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);
99
100   for (i = 0; i < 10000; i++)
101     g_string_append_c (string1, 'a'+(i%26));
102
103   g_assert((strlen("hi pete!") + 10000) == string1->len);
104   g_assert((strlen("hi pete!") + 10000) == strlen(string1->str));
105
106 #ifndef G_OS_WIN32
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 ",
111                     1,
112                     " wuss.  everyone agrees.\n",
113                     string1->str,
114                     10, 666, 15, 15, 666.666666666, 666.666666666);
115 #else
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 ",
118                     1,
119                     " wuss.  everyone agrees.\n",
120                     string1->str,
121                     10, 666, 15, 15, 666.666666666, 666.666666666);
122 #endif
123   
124   g_string_free (string1, TRUE);
125   g_string_free (string2, TRUE);
126
127   /* append */
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);
132
133   /* append_len */
134
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);  
139   
140   /* prepend */
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);
145
146   /* prepend_len */
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);
151   
152   /* insert */
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);
157
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);
163   
164   /* insert_len */
165
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   /* g_string_equal */
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);
191   
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);
211   
212   return 0;
213 }
214