removed glib.def from EXTRA_DIST ... ... and added it here.
[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
34 int array[10000];
35 gboolean failed = FALSE;
36
37 #define TEST(m,cond)    G_STMT_START { failed = !(cond); \
38 if (failed) \
39   { if (!m) \
40       g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
41     else \
42       g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
43   } \
44 else \
45   g_print ("."); fflush (stdout); \
46 } G_STMT_END
47
48 #define C2P(c)          ((gpointer) ((long) (c)))
49 #define P2C(p)          ((gchar) ((long) (p)))
50
51 #define GLIB_TEST_STRING "el dorado "
52 #define GLIB_TEST_STRING_5 "el do"
53
54 typedef struct {
55         guint age;
56         gchar name[40];
57 } GlibTestInfo;
58
59 int
60 main (int   argc,
61       char *argv[])
62 {
63   GStringChunk *string_chunk;
64
65   gchar *tmp_string = NULL, *tmp_string_2;
66   gint i;
67   GString *string1, *string2;
68
69   string_chunk = g_string_chunk_new (1024);
70
71   for (i = 0; i < 100000; i ++)
72     {
73       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
74
75       if (strcmp ("hi pete", tmp_string) != 0)
76         g_error ("string chunks are broken.\n");
77     }
78
79   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
80
81   g_assert (tmp_string_2 != tmp_string &&
82             strcmp(tmp_string_2, tmp_string) == 0);
83
84   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
85
86   g_assert (tmp_string_2 == tmp_string);
87
88   g_string_chunk_free (string_chunk);
89
90   string1 = g_string_new ("hi pete!");
91   string2 = g_string_new ("");
92
93   g_assert (string1 != NULL);
94   g_assert (string2 != NULL);
95   g_assert (strlen (string1->str) == string1->len);
96   g_assert (strlen (string2->str) == string2->len);
97   g_assert (string2->len == 0);
98   g_assert (strcmp ("hi pete!", string1->str) == 0);
99   g_assert (strcmp ("", string2->str) == 0);
100
101   for (i = 0; i < 10000; i++)
102     g_string_append_c (string1, 'a'+(i%26));
103
104   g_assert((strlen("hi pete!") + 10000) == string1->len);
105   g_assert((strlen("hi pete!") + 10000) == strlen(string1->str));
106
107 #ifndef G_OS_WIN32
108   /* MSVC and mingw32 use the same run-time C library, which doesn't like
109      the %10000.10000f format... */
110   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
111                     "this pete guy sure is a wuss, like he's the number ",
112                     1,
113                     " wuss.  everyone agrees.\n",
114                     string1->str,
115                     10, 666, 15, 15, 666.666666666, 666.666666666);
116 #else
117   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
118                     "this pete guy sure is a wuss, like he's the number ",
119                     1,
120                     " wuss.  everyone agrees.\n",
121                     string1->str,
122                     10, 666, 15, 15, 666.666666666, 666.666666666);
123 #endif
124   
125   g_string_free (string1, TRUE);
126   g_string_free (string2, TRUE);
127
128   /* append */
129   string1 = g_string_new ("firsthalf");
130   g_string_append (string1, "lasthalf");
131   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
132   g_string_free (string1, TRUE);
133
134   /* append_len */
135
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
167   string1 = g_string_new ("firstlast");
168   g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
169   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
170   g_string_free (string1, TRUE);
171
172   /* insert_len with magic -1 pos for append */
173   string1 = g_string_new ("first");
174   g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
175   g_assert (strcmp (string1->str, "firstlast") == 0);
176   g_string_free (string1, TRUE);
177   
178   /* insert_len with magic -1 len for strlen-the-string */
179   string1 = g_string_new ("first");
180   g_string_insert_len (string1, 5, "last", -1);
181   g_assert (strcmp (string1->str, "firstlast") == 0);
182   g_string_free (string1, TRUE);
183
184   /* g_string_equal */
185   string1 = g_string_new ("test");
186   string2 = g_string_new ("te");
187   g_assert (! g_string_equal(string1, string2));
188   g_string_append (string2, "st");
189   g_assert (g_string_equal(string1, string2));
190   g_string_free (string1, TRUE);
191   g_string_free (string2, TRUE);
192   
193   /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
194   string1 = g_string_new ("fiddle");
195   string2 = g_string_new ("fiddle");
196   g_assert (g_string_equal(string1, string2));
197   g_string_append_c(string1, '\0');
198   g_assert (! g_string_equal(string1, string2));
199   g_string_append_c(string2, '\0');
200   g_assert (g_string_equal(string1, string2));
201   g_string_append_c(string1, 'x');
202   g_string_append_c(string2, 'y');
203   g_assert (! g_string_equal(string1, string2));
204   g_assert (string1->len == 8);
205   g_string_append(string1, "yzzy");
206   g_assert (string1->len == 12);
207   g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
208   g_string_insert(string1, 1, "QED");
209   g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
210   g_string_free (string1, TRUE);
211   g_string_free (string2, TRUE);
212   
213   return 0;
214 }
215