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;
109 GString *string1, *string2;
116 g_print ("checking size of gint8...%ld (should be 1)\n", (glong)sizeof (gint8));
117 g_print ("checking size of gint16...%ld (should be 2)\n", (glong)sizeof (gint16));
118 g_print ("checking size of gint32...%ld (should be 4)\n", (glong)sizeof (gint32));
120 g_print ("checking doubly linked lists...");
123 for (i = 0; i < 10; i++)
124 list = g_list_append (list, &nums[i]);
125 list = g_list_reverse (list);
127 for (i = 0; i < 10; i++)
129 t = g_list_nth (list, i);
130 if (*((gint*) t->data) != (9 - i))
131 g_error ("Regular insert failed");
134 for (i = 0; i < 10; i++)
135 if(g_list_position(list, g_list_nth (list, i)) != i)
136 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
141 for (i = 0; i < 10; i++)
142 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
146 g_list_foreach (list, my_list_print, NULL);
149 for (i = 0; i < 10; i++)
151 t = g_list_nth (list, i);
152 if (*((gint*) t->data) != i)
153 g_error ("Sorted insert failed");
159 for (i = 0; i < 10; i++)
160 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
164 g_list_foreach (list, my_list_print, NULL);
167 for (i = 0; i < 10; i++)
169 t = g_list_nth (list, i);
170 if (*((gint*) t->data) != (9 - i))
171 g_error ("Sorted insert failed");
179 g_print ("checking singly linked lists...");
182 for (i = 0; i < 10; i++)
183 slist = g_slist_append (slist, &nums[i]);
184 slist = g_slist_reverse (slist);
186 for (i = 0; i < 10; i++)
188 st = g_slist_nth (slist, i);
189 if (*((gint*) st->data) != (9 - i))
193 g_slist_free (slist);
196 for (i = 0; i < 10; i++)
197 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
201 g_slist_foreach (slist, my_list_print, NULL);
204 for (i = 0; i < 10; i++)
206 st = g_slist_nth (slist, i);
207 if (*((gint*) st->data) != i)
208 g_error ("Sorted insert failed");
214 for (i = 0; i < 10; i++)
215 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
219 g_slist_foreach (slist, my_list_print, NULL);
222 for (i = 0; i < 10; i++)
224 st = g_slist_nth (slist, i);
225 if (*((gint*) st->data) != (9 - i))
226 g_error("Sorted insert failed");
234 g_print ("checking trees...\n");
236 tree = g_tree_new (my_compare);
238 for (j = 0; j < 10; 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]);
248 for (j = 0; j < 26; j++, i++)
251 g_tree_insert (tree, &chars[i], &chars[i]);
254 g_print ("tree height: %d\n", g_tree_height (tree));
255 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
258 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
261 for (i = 0; i < 10; i++)
262 g_tree_remove (tree, &chars[i]);
264 g_print ("tree height: %d\n", g_tree_height (tree));
265 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
268 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
274 g_print ("checking mem chunks...");
276 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
278 for (i = 0; i < 10000; i++)
280 mem[i] = g_chunk_new (gchar, mem_chunk);
282 for (j = 0; j < 50; j++)
286 for (i = 0; i < 10000; i++)
288 g_mem_chunk_free (mem_chunk, mem[i]);
294 g_print ("checking hash tables...");
296 hash_table = g_hash_table_new (my_hash, my_hash_compare);
297 for (i = 0; i < 10000; i++)
300 g_hash_table_insert (hash_table, &array[i], &array[i]);
302 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
304 for (i = 0; i < 10000; i++)
308 for (i = 0; i < 10000; i++)
309 g_hash_table_remove (hash_table, &array[i]);
311 g_hash_table_destroy (hash_table);
316 g_print ("checking string chunks...");
318 string_chunk = g_string_chunk_new (1024);
320 for (i = 0; i < 100000; i ++)
322 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
324 if (strcmp ("hi pete", tmp_string) != 0)
325 g_error ("string chunks are broken.\n");
328 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
330 g_assert (tmp_string_2 != tmp_string &&
331 strcmp(tmp_string_2, tmp_string) == 0);
333 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
335 g_assert (tmp_string_2 == tmp_string);
337 g_string_chunk_free (string_chunk);
342 g_print ("checking arrays...");
344 garray = g_array_new (FALSE);
345 for (i = 0; i < 10000; i++)
346 g_array_append_val (garray, gint, i);
348 for (i = 0; i < 10000; i++)
349 if (g_array_index (garray, gint, i) != i)
350 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
352 g_array_free (garray, TRUE);
354 garray = g_array_new (FALSE);
355 for (i = 0; i < 10000; i++)
356 g_array_prepend_val (garray, gint, i);
358 for (i = 0; i < 10000; i++)
359 if (g_array_index (garray, gint, i) != (10000 - i - 1))
360 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 10000 - i - 1);
362 g_array_free (garray, TRUE);
367 g_print ("checking strings...");
369 string1 = g_string_new ("hi pete!");
370 string2 = g_string_new ("");
372 g_assert (strcmp ("hi pete!", string1->str) == 0);
374 for (i = 0; i < 10000; i++)
375 g_string_append_c (string1, 'a'+(i%26));
377 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
378 "this pete guy sure is a wuss, like he's the number ",
380 " wuss. everyone agrees.\n",
382 10, 666, 15, 15, 666.666666666, 666.666666666);
386 g_print ("checking timers...\n");
388 timer = g_timer_new ();
389 g_print (" spinning for 3 seconds...\n");
391 g_timer_start (timer);
392 while (g_timer_elapsed (timer, NULL) < 3)
395 g_timer_stop (timer);
396 g_timer_destroy (timer);
400 g_print ("checking g_strcasecmp...\n");
402 /* g_debug (argv[0]); */
406 g_print ("checking relations...");
408 relation = g_relation_new (2);
410 g_relation_index (relation, 0, g_int_hash, g_int_equal);
411 g_relation_index (relation, 1, g_int_hash, g_int_equal);
413 for (i = 0; i < 1024; i += 1)
416 for (i = 1; i < 1023; i += 1)
418 g_relation_insert (relation, data + i, data + i + 1);
419 g_relation_insert (relation, data + i, data + i - 1);
422 for (i = 2; i < 1022; i += 1)
424 g_assert (! g_relation_exists (relation, data + i, data + i));
425 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
426 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
429 for (i = 1; i < 1023; i += 1)
431 g_assert (g_relation_exists (relation, data + i, data + i + 1));
432 g_assert (g_relation_exists (relation, data + i, data + i - 1));
435 for (i = 2; i < 1022; i += 1)
437 g_assert (g_relation_count (relation, data + i, 0) == 2);
438 g_assert (g_relation_count (relation, data + i, 1) == 2);
441 g_assert (g_relation_count (relation, data, 0) == 0);
443 g_assert (g_relation_count (relation, data + 42, 0) == 2);
444 g_assert (g_relation_count (relation, data + 43, 1) == 2);
445 g_assert (g_relation_count (relation, data + 41, 1) == 2);
446 g_relation_delete (relation, data + 42, 0);
447 g_assert (g_relation_count (relation, data + 42, 0) == 0);
448 g_assert (g_relation_count (relation, data + 43, 1) == 1);
449 g_assert (g_relation_count (relation, data + 41, 1) == 1);
451 tuples = g_relation_select (relation, data + 200, 0);
453 g_assert (tuples->len == 2);
456 for (i = 0; i < tuples->len; i += 1)
459 *(gint*) g_tuples_index (tuples, i, 0),
460 *(gint*) g_tuples_index (tuples, i, 1));
464 g_assert (g_relation_exists (relation, data + 300, data + 301));
465 g_relation_delete (relation, data + 300, 0);
466 g_assert (!g_relation_exists (relation, data + 300, data + 301));
468 g_tuples_destroy (tuples);
470 g_relation_destroy (relation);
476 g_print ("checking pointer arrays...");
478 gparray = g_ptr_array_new ();
479 for (i = 0; i < 10000; i++)
480 g_ptr_array_add (gparray, (void*)i);
482 for (i = 0; i < 10000; i++)
483 if (g_ptr_array_index (gparray, i) != (void*)i)
484 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), (void*)i);
486 g_ptr_array_free (gparray, TRUE);
491 g_print ("checking byte arrays...");
493 gbarray = g_byte_array_new ();
494 for (i = 0; i < 10000; i++)
495 g_byte_array_append (gbarray, "abcd", 4);
497 for (i = 0; i < 10000; i++)
499 g_assert (gbarray->data[4*i] == 'a');
500 g_assert (gbarray->data[4*i+1] == 'b');
501 g_assert (gbarray->data[4*i+2] == 'c');
502 g_assert (gbarray->data[4*i+3] == 'd');
505 g_byte_array_free (gbarray, TRUE);