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.
25 gboolean failed = FALSE;
27 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
30 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
32 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
35 g_print ("."); fflush (stdout); \
38 #define C2P(c) ((gpointer) ((long) (c)))
39 #define P2C(p) ((gchar) ((long) (p)))
42 node_build_string (GNode *node,
49 c[0] = P2C (node->data);
51 string = g_strconcat (*p ? *p : "", c, NULL);
70 g_print ("checking n-way trees: ");
73 root = g_node_new (C2P ('A'));
74 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
76 node_B = g_node_new (C2P ('B'));
77 g_node_append (root, node_B);
78 TEST (NULL, root->children == node_B);
80 g_node_append (node_B, g_node_new (C2P ('E')));
81 g_node_prepend (node_B, g_node_new (C2P ('C')));
82 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
84 node_F = g_node_new (C2P ('F'));
85 g_node_append (root, node_F);
86 TEST (NULL, root->children->next == node_F);
88 node_G = g_node_new (C2P ('G'));
89 g_node_append (node_F, node_G);
90 node_J = g_node_new (C2P ('J'));
91 g_node_insert (node_G, -1, node_J);
92 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
93 g_node_insert (node_G, 0, g_node_new (C2P ('H')));
94 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
96 TEST (NULL, g_node_depth (root) == 1);
97 TEST (NULL, g_node_max_height (root) == 4);
98 TEST (NULL, g_node_depth (node_G->children->next) == 4);
99 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
100 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
101 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
102 TEST (NULL, g_node_max_height (node_F) == 3);
103 TEST (NULL, g_node_n_children (node_G) == 4);
104 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
105 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
106 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
108 for (i = 0; i < g_node_n_children (node_B); i++)
110 node = g_node_nth_child (node_B, i);
111 TEST (NULL, P2C (node->data) == ('C' + i));
114 for (i = 0; i < g_node_n_children (node_G); i++)
115 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
125 * for in-order traversal, 'G' is considered to be the "left"
126 * child of 'F', which will cause 'F' to be the last node visited.
130 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
131 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
132 g_free (tstring); tstring = NULL;
133 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
134 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
135 g_free (tstring); tstring = NULL;
136 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
137 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
138 g_free (tstring); tstring = NULL;
139 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
140 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
141 g_free (tstring); tstring = NULL;
143 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
144 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
145 g_free (tstring); tstring = NULL;
146 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
147 TEST (tstring, strcmp (tstring, "ABFG") == 0);
148 g_free (tstring); tstring = NULL;
150 g_node_reverse_children (node_B);
151 g_node_reverse_children (node_G);
153 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
154 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
155 g_free (tstring); tstring = NULL;
157 g_node_destroy (root);
159 /* allocation tests */
161 root = g_node_new (NULL);
164 for (i = 0; i < 2048; i++)
166 g_node_append (node, g_node_new (NULL));
168 node = node->children->next;
170 TEST (NULL, g_node_max_height (root) > 100);
171 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
173 g_node_destroy (root);
180 my_hash_callback (gpointer key,
189 my_hash (gconstpointer key)
191 return (guint) *((const gint*) key);
195 my_hash_compare (gconstpointer a,
198 return *((const gint*) a) == *((const gint*) b);
202 my_list_compare_one (gconstpointer a, gconstpointer b)
204 gint one = *((const gint*)a);
205 gint two = *((const gint*)b);
210 my_list_compare_two (gconstpointer a, gconstpointer b)
212 gint one = *((const gint*)a);
213 gint two = *((const gint*)b);
218 my_list_print (gpointer a, gpointer b)
220 gint three = *((gint*)a);
221 g_print("%d", three);
225 my_compare (gconstpointer a,
235 my_traverse (gpointer key,
240 g_print ("%c ", *ch);
250 GHashTable *hash_table;
251 GMemChunk *mem_chunk;
252 GStringChunk *string_chunk;
254 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
255 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
257 gchar *mem[10000], *tmp_string, *tmp_string_2;
262 GString *string1, *string2;
271 } dirname_checks[] = {
284 guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
287 g_print ("checking size of gint8...%ld (should be 1)\n", (glong)sizeof (gint8));
288 g_print ("checking size of gint16...%ld (should be 2)\n", (glong)sizeof (gint16));
289 g_print ("checking size of gint32...%ld (should be 4)\n", (glong)sizeof (gint32));
291 g_print ("checking g_dirname()...");
292 for (i = 0; i < n_dirname_checks; i++)
296 dirname = g_dirname (dirname_checks[i].filename);
297 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
299 g_print ("failed for \"%s\"==\"%s\" (returned: \"%s\")\n",
300 dirname_checks[i].filename,
301 dirname_checks[i].dirname,
303 n_dirname_checks = 0;
307 if (n_dirname_checks)
310 g_print ("checking doubly linked lists...");
313 for (i = 0; i < 10; i++)
314 list = g_list_append (list, &nums[i]);
315 list = g_list_reverse (list);
317 for (i = 0; i < 10; i++)
319 t = g_list_nth (list, i);
320 if (*((gint*) t->data) != (9 - i))
321 g_error ("Regular insert failed");
324 for (i = 0; i < 10; i++)
325 if(g_list_position(list, g_list_nth (list, i)) != i)
326 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
331 for (i = 0; i < 10; i++)
332 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
336 g_list_foreach (list, my_list_print, NULL);
339 for (i = 0; i < 10; i++)
341 t = g_list_nth (list, i);
342 if (*((gint*) t->data) != i)
343 g_error ("Sorted insert failed");
349 for (i = 0; i < 10; i++)
350 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
354 g_list_foreach (list, my_list_print, NULL);
357 for (i = 0; i < 10; i++)
359 t = g_list_nth (list, i);
360 if (*((gint*) t->data) != (9 - i))
361 g_error ("Sorted insert failed");
369 g_print ("checking singly linked lists...");
372 for (i = 0; i < 10; i++)
373 slist = g_slist_append (slist, &nums[i]);
374 slist = g_slist_reverse (slist);
376 for (i = 0; i < 10; i++)
378 st = g_slist_nth (slist, i);
379 if (*((gint*) st->data) != (9 - i))
383 g_slist_free (slist);
386 for (i = 0; i < 10; i++)
387 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
391 g_slist_foreach (slist, my_list_print, NULL);
394 for (i = 0; i < 10; i++)
396 st = g_slist_nth (slist, i);
397 if (*((gint*) st->data) != i)
398 g_error ("Sorted insert failed");
404 for (i = 0; i < 10; i++)
405 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
409 g_slist_foreach (slist, my_list_print, NULL);
412 for (i = 0; i < 10; i++)
414 st = g_slist_nth (slist, i);
415 if (*((gint*) st->data) != (9 - i))
416 g_error("Sorted insert failed");
424 g_print ("checking binary trees...\n");
426 tree = g_tree_new (my_compare);
428 for (j = 0; j < 10; j++, i++)
431 g_tree_insert (tree, &chars[i], &chars[i]);
433 for (j = 0; j < 26; j++, i++)
436 g_tree_insert (tree, &chars[i], &chars[i]);
438 for (j = 0; j < 26; j++, i++)
441 g_tree_insert (tree, &chars[i], &chars[i]);
444 g_print ("tree height: %d\n", g_tree_height (tree));
445 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
448 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
451 for (i = 0; i < 10; i++)
452 g_tree_remove (tree, &chars[i]);
454 g_print ("tree height: %d\n", g_tree_height (tree));
455 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
458 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
464 /* check n-way trees */
467 g_print ("checking mem chunks...");
469 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
471 for (i = 0; i < 10000; i++)
473 mem[i] = g_chunk_new (gchar, mem_chunk);
475 for (j = 0; j < 50; j++)
479 for (i = 0; i < 10000; i++)
481 g_mem_chunk_free (mem_chunk, mem[i]);
487 g_print ("checking hash tables...");
489 hash_table = g_hash_table_new (my_hash, my_hash_compare);
490 for (i = 0; i < 10000; i++)
493 g_hash_table_insert (hash_table, &array[i], &array[i]);
495 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
497 for (i = 0; i < 10000; i++)
501 for (i = 0; i < 10000; i++)
502 g_hash_table_remove (hash_table, &array[i]);
504 g_hash_table_destroy (hash_table);
509 g_print ("checking string chunks...");
511 string_chunk = g_string_chunk_new (1024);
513 for (i = 0; i < 100000; i ++)
515 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
517 if (strcmp ("hi pete", tmp_string) != 0)
518 g_error ("string chunks are broken.\n");
521 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
523 g_assert (tmp_string_2 != tmp_string &&
524 strcmp(tmp_string_2, tmp_string) == 0);
526 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
528 g_assert (tmp_string_2 == tmp_string);
530 g_string_chunk_free (string_chunk);
535 g_print ("checking arrays...");
537 garray = g_array_new (FALSE);
538 for (i = 0; i < 10000; i++)
539 g_array_append_val (garray, gint, i);
541 for (i = 0; i < 10000; i++)
542 if (g_array_index (garray, gint, i) != i)
543 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
545 g_array_free (garray, TRUE);
547 garray = g_array_new (FALSE);
548 for (i = 0; i < 10000; i++)
549 g_array_prepend_val (garray, gint, i);
551 for (i = 0; i < 10000; i++)
552 if (g_array_index (garray, gint, i) != (10000 - i - 1))
553 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 10000 - i - 1);
555 g_array_free (garray, TRUE);
560 g_print ("checking strings...");
562 string1 = g_string_new ("hi pete!");
563 string2 = g_string_new ("");
565 g_assert (strcmp ("hi pete!", string1->str) == 0);
567 for (i = 0; i < 10000; i++)
568 g_string_append_c (string1, 'a'+(i%26));
570 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
571 "this pete guy sure is a wuss, like he's the number ",
573 " wuss. everyone agrees.\n",
575 10, 666, 15, 15, 666.666666666, 666.666666666);
579 g_print ("checking timers...\n");
581 timer = g_timer_new ();
582 g_print (" spinning for 3 seconds...\n");
584 g_timer_start (timer);
585 while (g_timer_elapsed (timer, NULL) < 3)
588 g_timer_stop (timer);
589 g_timer_destroy (timer);
593 g_print ("checking g_strcasecmp...\n");
595 /* g_debug (argv[0]); */
599 g_print ("checking relations...");
601 relation = g_relation_new (2);
603 g_relation_index (relation, 0, g_int_hash, g_int_equal);
604 g_relation_index (relation, 1, g_int_hash, g_int_equal);
606 for (i = 0; i < 1024; i += 1)
609 for (i = 1; i < 1023; i += 1)
611 g_relation_insert (relation, data + i, data + i + 1);
612 g_relation_insert (relation, data + i, data + i - 1);
615 for (i = 2; i < 1022; i += 1)
617 g_assert (! g_relation_exists (relation, data + i, data + i));
618 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
619 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
622 for (i = 1; i < 1023; i += 1)
624 g_assert (g_relation_exists (relation, data + i, data + i + 1));
625 g_assert (g_relation_exists (relation, data + i, data + i - 1));
628 for (i = 2; i < 1022; i += 1)
630 g_assert (g_relation_count (relation, data + i, 0) == 2);
631 g_assert (g_relation_count (relation, data + i, 1) == 2);
634 g_assert (g_relation_count (relation, data, 0) == 0);
636 g_assert (g_relation_count (relation, data + 42, 0) == 2);
637 g_assert (g_relation_count (relation, data + 43, 1) == 2);
638 g_assert (g_relation_count (relation, data + 41, 1) == 2);
639 g_relation_delete (relation, data + 42, 0);
640 g_assert (g_relation_count (relation, data + 42, 0) == 0);
641 g_assert (g_relation_count (relation, data + 43, 1) == 1);
642 g_assert (g_relation_count (relation, data + 41, 1) == 1);
644 tuples = g_relation_select (relation, data + 200, 0);
646 g_assert (tuples->len == 2);
649 for (i = 0; i < tuples->len; i += 1)
652 *(gint*) g_tuples_index (tuples, i, 0),
653 *(gint*) g_tuples_index (tuples, i, 1));
657 g_assert (g_relation_exists (relation, data + 300, data + 301));
658 g_relation_delete (relation, data + 300, 0);
659 g_assert (!g_relation_exists (relation, data + 300, data + 301));
661 g_tuples_destroy (tuples);
663 g_relation_destroy (relation);
669 g_print ("checking pointer arrays...");
671 gparray = g_ptr_array_new ();
672 for (i = 0; i < 10000; i++)
673 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
675 for (i = 0; i < 10000; i++)
676 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
677 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
679 g_ptr_array_free (gparray, TRUE);
684 g_print ("checking byte arrays...");
686 gbarray = g_byte_array_new ();
687 for (i = 0; i < 10000; i++)
688 g_byte_array_append (gbarray, "abcd", 4);
690 for (i = 0; i < 10000; i++)
692 g_assert (gbarray->data[4*i] == 'a');
693 g_assert (gbarray->data[4*i+1] == 'b');
694 g_assert (gbarray->data[4*i+2] == 'c');
695 g_assert (gbarray->data[4*i+3] == 'd');
698 g_byte_array_free (gbarray, TRUE);