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
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
27 my_hash_callback (gpointer key,
36 my_hash (gconstpointer key)
38 return (guint) *((const gint*) key);
42 my_hash_compare (gconstpointer a,
45 return *((const gint*) a) == *((const gint*) b);
49 my_list_compare_one (gconstpointer a, gconstpointer b)
51 gint one = *((const gint*)a);
52 gint two = *((const gint*)b);
57 my_list_compare_two (gconstpointer a, gconstpointer b)
59 gint one = *((const gint*)a);
60 gint two = *((const gint*)b);
65 my_list_print (gpointer a, gpointer b)
67 gint three = *((gint*)a);
72 my_compare (gconstpointer a,
82 my_traverse (gpointer key,
97 GHashTable *hash_table;
99 GStringChunk *string_chunk;
101 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
102 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
104 gchar *mem[10000], *tmp_string, *tmp_string_2;
107 GString *string1, *string2;
111 g_print ("checking size of gint8...%ld (should be 1)\n", (glong)sizeof (gint8));
112 g_print ("checking size of gint16...%ld (should be 2)\n", (glong)sizeof (gint16));
113 g_print ("checking size of gint32...%ld (should be 4)\n", (glong)sizeof (gint32));
115 g_print ("checking doubly linked lists...");
118 for (i = 0; i < 10; i++)
119 list = g_list_append (list, &nums[i]);
120 list = g_list_reverse (list);
122 for (i = 0; i < 10; i++)
124 t = g_list_nth (list, i);
125 if (*((gint*) t->data) != (9 - i))
126 g_error ("Regular insert failed");
129 for (i = 0; i < 10; i++)
130 if(g_list_position(list, g_list_nth (list, i)) != i)
131 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
136 for (i = 0; i < 10; i++)
137 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
141 g_list_foreach (list, my_list_print, NULL);
144 for (i = 0; i < 10; i++)
146 t = g_list_nth (list, i);
147 if (*((gint*) t->data) != i)
148 g_error ("Sorted insert failed");
154 for (i = 0; i < 10; i++)
155 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
159 g_list_foreach (list, my_list_print, NULL);
162 for (i = 0; i < 10; i++)
164 t = g_list_nth (list, i);
165 if (*((gint*) t->data) != (9 - i))
166 g_error ("Sorted insert failed");
174 g_print ("checking singly linked lists...");
177 for (i = 0; i < 10; i++)
178 slist = g_slist_append (slist, &nums[i]);
179 slist = g_slist_reverse (slist);
181 for (i = 0; i < 10; i++)
183 st = g_slist_nth (slist, i);
184 if (*((gint*) st->data) != (9 - i))
188 g_slist_free (slist);
191 for (i = 0; i < 10; i++)
192 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
196 g_slist_foreach (slist, my_list_print, NULL);
199 for (i = 0; i < 10; i++)
201 st = g_slist_nth (slist, i);
202 if (*((gint*) st->data) != i)
203 g_error ("Sorted insert failed");
209 for (i = 0; i < 10; i++)
210 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
214 g_slist_foreach (slist, my_list_print, NULL);
217 for (i = 0; i < 10; i++)
219 st = g_slist_nth (slist, i);
220 if (*((gint*) st->data) != (9 - i))
221 g_error("Sorted insert failed");
229 g_print ("checking trees...\n");
231 tree = g_tree_new (my_compare);
233 for (j = 0; j < 10; j++, i++)
236 g_tree_insert (tree, &chars[i], &chars[i]);
238 for (j = 0; j < 26; j++, i++)
241 g_tree_insert (tree, &chars[i], &chars[i]);
243 for (j = 0; j < 26; j++, i++)
246 g_tree_insert (tree, &chars[i], &chars[i]);
249 g_print ("tree height: %d\n", g_tree_height (tree));
250 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
253 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
256 for (i = 0; i < 10; i++)
257 g_tree_remove (tree, &chars[i]);
259 g_print ("tree height: %d\n", g_tree_height (tree));
260 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
263 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
269 g_print ("checking mem chunks...");
271 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
273 for (i = 0; i < 10000; i++)
275 mem[i] = g_chunk_new (gchar, mem_chunk);
277 for (j = 0; j < 50; j++)
281 for (i = 0; i < 10000; i++)
283 g_mem_chunk_free (mem_chunk, mem[i]);
289 g_print ("checking hash tables...");
291 hash_table = g_hash_table_new (my_hash, my_hash_compare);
292 for (i = 0; i < 10000; i++)
295 g_hash_table_insert (hash_table, &array[i], &array[i]);
297 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
299 for (i = 0; i < 10000; i++)
303 for (i = 0; i < 10000; i++)
304 g_hash_table_remove (hash_table, &array[i]);
306 g_hash_table_destroy (hash_table);
311 g_print ("checking string chunks...");
313 string_chunk = g_string_chunk_new (1024);
315 for (i = 0; i < 100000; i ++)
317 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
319 if (strcmp ("hi pete", tmp_string) != 0)
320 g_error ("string chunks are broken.\n");
323 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
325 g_assert (tmp_string_2 != tmp_string &&
326 strcmp(tmp_string_2, tmp_string) == 0);
328 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
330 g_assert (tmp_string_2 == tmp_string);
332 g_string_chunk_free (string_chunk);
337 g_print ("checking arrays...");
339 garray = g_array_new (FALSE);
340 for (i = 0; i < 10000; i++)
341 g_array_append_val (garray, gint, i);
343 for (i = 0; i < 10000; i++)
344 if (g_array_index (garray, gint, i) != i)
345 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
347 g_array_free (garray, TRUE);
349 garray = g_array_new (FALSE);
350 for (i = 0; i < 10000; i++)
351 g_array_prepend_val (garray, gint, i);
353 for (i = 0; i < 10000; i++)
354 if (g_array_index (garray, gint, i) != (10000 - i - 1))
355 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 10000 - i - 1);
357 g_array_free (garray, TRUE);
362 g_print ("checking strings...");
364 string1 = g_string_new ("hi pete!");
365 string2 = g_string_new ("");
367 g_assert (strcmp ("hi pete!", string1->str) == 0);
369 for (i = 0; i < 10000; i++)
370 g_string_append_c (string1, 'a'+(i%26));
372 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
373 "this pete guy sure is a wuss, like he's the number ",
375 " wuss. everyone agrees.\n",
377 10, 666, 15, 15, 666.666666666, 666.666666666);
381 g_print ("checking timers...\n");
383 timer = g_timer_new ();
384 g_print (" spinning for 3 seconds...\n");
386 g_timer_start (timer);
387 while (g_timer_elapsed (timer, NULL) < 3)
390 g_timer_stop (timer);
391 g_timer_destroy (timer);
395 g_print ("checking g_strcasecmp...\n");
397 /* g_debug (argv[0]); */