]> Pileus Git - ~andy/gtk/blob - glib/testglib.c
Initial revision
[~andy/gtk] / glib / testglib.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 Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 #include <stdio.h>
19 #include <string.h>
20 #include "glib.h"
21
22 int array[10000];
23
24 void
25 my_hash_callback (gpointer key,
26                   gpointer value,
27                   gpointer user_data)
28 {
29   int *d = value;
30   *d = 1;
31 }
32
33 guint
34 my_hash (gpointer key)
35 {
36   return (guint) *((gint*) key);
37 }
38
39 gint
40 my_hash_compare (gpointer a,
41                  gpointer b)
42 {
43   return *((gint*) a) == *((gint*) b);
44 }
45
46 gint
47 my_compare (gpointer a,
48             gpointer b)
49 {
50   char *cha = a;
51   char *chb = b;
52
53   return *cha - *chb;
54 }
55
56 gint
57 my_traverse (gpointer key,
58              gpointer value,
59              gpointer data)
60 {
61   char *ch = key;
62   g_print ("%c ", *ch);
63   return FALSE;
64 }
65
66 int
67 main (int   argc,
68       char *argv[])
69 {
70   GList *list, *t;
71   GSList *slist, *st;
72   GHashTable *hash_table;
73   GMemChunk *mem_chunk;
74   GStringChunk *string_chunk;
75   GTimer *timer;
76   gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
77   gchar *mem[10000], *tmp_string, *tmp_string_2;
78   gint i, j;
79   GArray *garray;
80   GString *string1, *string2;
81   GTree *tree;
82   char chars[62];
83
84   g_print ("checking size of gint8...%d (should be 1)\n", sizeof (gint8));
85   g_print ("checking size of gint16...%d (should be 2)\n", sizeof (gint16));
86   g_print ("checking size of gint32...%d (should be 4)\n", sizeof (gint32));
87
88   g_print ("checking doubly linked lists...");
89
90   list = NULL;
91   for (i = 0; i < 10; i++)
92     list = g_list_append (list, &nums[i]);
93   list = g_list_reverse (list);
94
95   for (i = 0; i < 10; i++)
96     {
97       t = g_list_nth (list, i);
98       if (*((gint*) t->data) != (9 - i))
99         g_error ("failed");
100     }
101
102   g_list_free (list);
103
104   g_print ("ok\n");
105
106
107   g_print ("checking singly linked lists...");
108
109   slist = NULL;
110   for (i = 0; i < 10; i++)
111     slist = g_slist_append (slist, &nums[i]);
112   slist = g_slist_reverse (slist);
113
114   for (i = 0; i < 10; i++)
115     {
116       st = g_slist_nth (slist, i);
117       if (*((gint*) st->data) != (9 - i))
118         g_error ("failed");
119     }
120
121   g_slist_free (slist);
122
123   g_print ("ok\n");
124
125
126   g_print ("checking trees...\n");
127
128   tree = g_tree_new (my_compare);
129   i = 0;
130   for (j = 0; j < 10; j++, i++)
131     {
132       chars[i] = '0' + j;
133       g_tree_insert (tree, &chars[i], &chars[i]);
134     }
135   for (j = 0; j < 26; j++, i++)
136     {
137       chars[i] = 'A' + j;
138       g_tree_insert (tree, &chars[i], &chars[i]);
139     }
140   for (j = 0; j < 26; j++, i++)
141     {
142       chars[i] = 'a' + j;
143       g_tree_insert (tree, &chars[i], &chars[i]);
144     }
145
146   g_print ("tree height: %d\n", g_tree_height (tree));
147   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
148
149   g_print ("tree: ");
150   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
151   g_print ("\n");
152
153   for (i = 0; i < 10; i++)
154     g_tree_remove (tree, &chars[i]);
155
156   g_print ("tree height: %d\n", g_tree_height (tree));
157   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
158
159   g_print ("tree: ");
160   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
161   g_print ("\n");
162
163   g_print ("ok\n");
164
165
166   g_print ("checking mem chunks...");
167
168   mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
169
170   for (i = 0; i < 10000; i++)
171     {
172       mem[i] = g_chunk_new (gchar, mem_chunk);
173
174       for (j = 0; j < 50; j++)
175         mem[i][j] = i * j;
176     }
177
178   for (i = 0; i < 10000; i++)
179     {
180       g_mem_chunk_free (mem_chunk, mem[i]);
181     }
182
183   g_print ("ok\n");
184
185
186   g_print ("checking hash tables...");
187
188   hash_table = g_hash_table_new (my_hash, my_hash_compare);
189   for (i = 0; i < 10000; i++)
190     {
191       array[i] = i;
192       g_hash_table_insert (hash_table, &array[i], &array[i]);
193     }
194   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
195
196   for (i = 0; i < 10000; i++)
197     if (array[i] == 0)
198       g_print ("%d\n", i);
199
200   for (i = 0; i < 10000; i++)
201     g_hash_table_remove (hash_table, &array[i]);
202
203   g_hash_table_destroy (hash_table);
204
205   g_print ("ok\n");
206
207
208   g_print ("checking string chunks...");
209
210   string_chunk = g_string_chunk_new (1024);
211
212   for (i = 0; i < 100000; i ++)
213     {
214       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
215
216       if (strcmp ("hi pete", tmp_string) != 0)
217         g_error ("string chunks are broken.\n");
218     }
219
220   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
221
222   g_assert (tmp_string_2 != tmp_string &&
223             strcmp(tmp_string_2, tmp_string) == 0);
224
225   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
226
227   g_assert (tmp_string_2 == tmp_string);
228
229   g_string_chunk_free (string_chunk);
230
231   g_print ("ok\n");
232
233
234   g_print ("checking arrays...");
235
236   garray = g_array_new (FALSE);
237   for (i = 0; i < 10000; i++)
238     g_array_append_val (garray, gint, i);
239
240   for (i = 0; i < 10000; i++)
241     if (g_array_index (garray, gint, i) != i)
242       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
243
244   g_array_free (garray, TRUE);
245
246   garray = g_array_new (FALSE);
247   for (i = 0; i < 10000; i++)
248     g_array_prepend_val (garray, gint, i);
249
250   for (i = 0; i < 10000; i++)
251     if (g_array_index (garray, gint, i) != (10000 - i - 1))
252       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 10000 - i - 1);
253
254   g_array_free (garray, TRUE);
255
256   g_print ("ok\n");
257
258
259   g_print ("checking strings...");
260
261   string1 = g_string_new ("hi pete!");
262   string2 = g_string_new ("");
263
264   g_assert (strcmp ("hi pete!", string1->str) == 0);
265
266   for (i = 0; i < 10000; i++)
267     g_string_append_c (string1, 'a'+(i%26));
268
269   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
270                     "this pete guy sure is a wuss, like he's the number ",
271                     1,
272                     " wuss.  everyone agrees.\n",
273                     string1->str,
274                     10, 666, 15, 15, 666.666666666, 666.666666666);
275
276   g_print ("ok\n");
277
278   g_print ("checking timers...\n");
279
280   timer = g_timer_new ();
281   g_print ("  spinning for 3 seconds...\n");
282
283   g_timer_start (timer);
284   while (g_timer_elapsed (timer, NULL) < 3)
285     ;
286
287   g_timer_stop (timer);
288   g_timer_destroy (timer);
289
290   g_print ("ok\n");
291
292   /* g_debug (argv[0]); */
293
294
295   return 0;
296 }