add array-test.c
[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 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.
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  * Library General Public License for more details.
13  *
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.
18  */
19 #undef G_LOG_DOMAIN
20
21 #include <stdio.h>
22 #include <string.h>
23 #include "glib.h"
24
25 int array[10000];
26 gboolean failed = FALSE;
27
28 #define TEST(m,cond)    G_STMT_START { failed = !(cond); \
29 if (failed) \
30   { if (!m) \
31       g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
32     else \
33       g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
34   } \
35 else \
36   g_print ("."); fflush (stdout); \
37 } G_STMT_END
38
39 #define C2P(c)          ((gpointer) ((long) (c)))
40 #define P2C(p)          ((gchar) ((long) (p)))
41
42 #define GLIB_TEST_STRING "el dorado "
43 #define GLIB_TEST_STRING_5 "el do"
44
45 typedef struct {
46         guint age;
47         gchar name[40];
48 } GlibTestInfo;
49
50 int
51 main (int   argc,
52       char *argv[])
53 {
54   GStringChunk *string_chunk;
55   gchar *string;
56
57   gchar *tmp_string, *tmp_string_2;
58   gint i;
59   GString *string1, *string2;
60
61   string_chunk = g_string_chunk_new (1024);
62
63   for (i = 0; i < 100000; i ++)
64     {
65       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
66
67       if (strcmp ("hi pete", tmp_string) != 0)
68         g_error ("string chunks are broken.\n");
69     }
70
71   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
72
73   g_assert (tmp_string_2 != tmp_string &&
74             strcmp(tmp_string_2, tmp_string) == 0);
75
76   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
77
78   g_assert (tmp_string_2 == tmp_string);
79
80   g_string_chunk_free (string_chunk);
81
82   string1 = g_string_new ("hi pete!");
83   string2 = g_string_new ("");
84
85   g_assert (string1 != NULL);
86   g_assert (string2 != NULL);
87   g_assert (strcmp ("hi pete!", string1->str) == 0);
88   g_assert (strcmp ("", string2->str) == 0);
89
90   for (i = 0; i < 10000; i++)
91     g_string_append_c (string1, 'a'+(i%26));
92
93   g_assert((strlen("hi pete!") + 10000) == string1->len);
94   g_assert((strlen("hi pete!") + 10000) == strlen(string1->str));
95
96 #if !(defined (_MSC_VER) || defined (__LCC__))
97   /* MSVC and LCC use the same run-time C library, which doesn't like
98      the %10000.10000f format... */
99   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
100                     "this pete guy sure is a wuss, like he's the number ",
101                     1,
102                     " wuss.  everyone agrees.\n",
103                     string1->str,
104                     10, 666, 15, 15, 666.666666666, 666.666666666);
105 #else
106   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
107                     "this pete guy sure is a wuss, like he's the number ",
108                     1,
109                     " wuss.  everyone agrees.\n",
110                     string1->str,
111                     10, 666, 15, 15, 666.666666666, 666.666666666);
112 #endif
113
114   g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0);
115   g_assert (g_strcasecmp ("frobozz", "frobozz") == 0);
116   g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0);
117   g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0);
118   g_assert (g_strcasecmp ("", "") == 0);
119   g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0);
120   g_assert (g_strcasecmp ("a", "b") < 0);
121   g_assert (g_strcasecmp ("a", "B") < 0);
122   g_assert (g_strcasecmp ("A", "b") < 0);
123   g_assert (g_strcasecmp ("A", "B") < 0);
124   g_assert (g_strcasecmp ("b", "a") > 0);
125   g_assert (g_strcasecmp ("b", "A") > 0);
126   g_assert (g_strcasecmp ("B", "a") > 0);
127   g_assert (g_strcasecmp ("B", "A") > 0);
128
129   g_assert(g_strdup(NULL) == NULL);
130   string = g_strdup(GLIB_TEST_STRING);
131   g_assert(string != NULL);
132   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
133   g_free(string);
134
135   string = g_strconcat(GLIB_TEST_STRING, NULL);
136   g_assert(string != NULL);
137   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
138   g_free(string);
139
140   string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING, 
141                        GLIB_TEST_STRING, NULL);
142   g_assert(string != NULL);
143   g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
144                           GLIB_TEST_STRING) == 0);
145   g_free(string);
146   
147   string = g_strdup_printf ("%05d %-5s", 21, "test");
148   g_assert (string != NULL);
149   g_assert (strcmp(string, "00021 test ") == 0);
150   g_free (string);
151
152   return 0;
153 }
154