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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 my_hash_callback (gpointer key,
34 my_hash (gpointer key)
36 return (guint) *((gint*) key);
40 my_hash_compare (gpointer a,
43 return *((gint*) a) == *((gint*) b);
47 my_compare (gpointer a,
57 my_traverse (gpointer key,
72 GHashTable *hash_table;
74 GStringChunk *string_chunk;
76 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
77 gchar *mem[10000], *tmp_string, *tmp_string_2;
80 GString *string1, *string2;
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));
88 g_print ("checking doubly linked lists...");
91 for (i = 0; i < 10; i++)
92 list = g_list_append (list, &nums[i]);
93 list = g_list_reverse (list);
95 for (i = 0; i < 10; i++)
97 t = g_list_nth (list, i);
98 if (*((gint*) t->data) != (9 - i))
107 g_print ("checking singly linked lists...");
110 for (i = 0; i < 10; i++)
111 slist = g_slist_append (slist, &nums[i]);
112 slist = g_slist_reverse (slist);
114 for (i = 0; i < 10; i++)
116 st = g_slist_nth (slist, i);
117 if (*((gint*) st->data) != (9 - i))
121 g_slist_free (slist);
126 g_print ("checking trees...\n");
128 tree = g_tree_new (my_compare);
130 for (j = 0; j < 10; j++, i++)
133 g_tree_insert (tree, &chars[i], &chars[i]);
135 for (j = 0; j < 26; j++, i++)
138 g_tree_insert (tree, &chars[i], &chars[i]);
140 for (j = 0; j < 26; j++, i++)
143 g_tree_insert (tree, &chars[i], &chars[i]);
146 g_print ("tree height: %d\n", g_tree_height (tree));
147 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
150 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
153 for (i = 0; i < 10; i++)
154 g_tree_remove (tree, &chars[i]);
156 g_print ("tree height: %d\n", g_tree_height (tree));
157 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
160 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
166 g_print ("checking mem chunks...");
168 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
170 for (i = 0; i < 10000; i++)
172 mem[i] = g_chunk_new (gchar, mem_chunk);
174 for (j = 0; j < 50; j++)
178 for (i = 0; i < 10000; i++)
180 g_mem_chunk_free (mem_chunk, mem[i]);
186 g_print ("checking hash tables...");
188 hash_table = g_hash_table_new (my_hash, my_hash_compare);
189 for (i = 0; i < 10000; i++)
192 g_hash_table_insert (hash_table, &array[i], &array[i]);
194 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
196 for (i = 0; i < 10000; i++)
200 for (i = 0; i < 10000; i++)
201 g_hash_table_remove (hash_table, &array[i]);
203 g_hash_table_destroy (hash_table);
208 g_print ("checking string chunks...");
210 string_chunk = g_string_chunk_new (1024);
212 for (i = 0; i < 100000; i ++)
214 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
216 if (strcmp ("hi pete", tmp_string) != 0)
217 g_error ("string chunks are broken.\n");
220 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
222 g_assert (tmp_string_2 != tmp_string &&
223 strcmp(tmp_string_2, tmp_string) == 0);
225 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
227 g_assert (tmp_string_2 == tmp_string);
229 g_string_chunk_free (string_chunk);
234 g_print ("checking arrays...");
236 garray = g_array_new (FALSE);
237 for (i = 0; i < 10000; i++)
238 g_array_append_val (garray, gint, i);
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);
244 g_array_free (garray, TRUE);
246 garray = g_array_new (FALSE);
247 for (i = 0; i < 10000; i++)
248 g_array_prepend_val (garray, gint, i);
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);
254 g_array_free (garray, TRUE);
259 g_print ("checking strings...");
261 string1 = g_string_new ("hi pete!");
262 string2 = g_string_new ("");
264 g_assert (strcmp ("hi pete!", string1->str) == 0);
266 for (i = 0; i < 10000; i++)
267 g_string_append_c (string1, 'a'+(i%26));
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 ",
272 " wuss. everyone agrees.\n",
274 10, 666, 15, 15, 666.666666666, 666.666666666);
278 g_print ("checking timers...\n");
280 timer = g_timer_new ();
281 g_print (" spinning for 3 seconds...\n");
283 g_timer_start (timer);
284 while (g_timer_elapsed (timer, NULL) < 3)
287 g_timer_stop (timer);
288 g_timer_destroy (timer);
292 /* g_debug (argv[0]); */