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_data (node_B, C2P ('E'));
81 g_node_prepend_data (node_B, 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_prepend (node_G, node_J);
92 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
93 g_node_insert_data (node_G, 0, 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};
258 gchar *mem[10000], *tmp_string, *tmp_string_2;
263 GString *string1, *string2;
272 } dirname_checks[] = {
285 guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
287 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
294 string = g_get_current_dir ();
295 g_print ("cwd: %s\n", string);
299 g_print ("checking size of gint8: %d", sizeof (gint8));
300 TEST (NULL, sizeof (gint8) == 1);
301 g_print ("\nchecking size of gint16: %d", sizeof (gint16));
302 TEST (NULL, sizeof (gint16) == 2);
303 g_print ("\nchecking size of gint32: %d", sizeof (gint32));
304 TEST (NULL, sizeof (gint32) == 4);
306 g_print ("\nchecking size of gint64: %d", sizeof (gint64));
307 TEST (NULL, sizeof (gint64) == 8);
308 #endif /* HAVE_GINT64 */
311 g_print ("checking g_dirname()...");
312 for (i = 0; i < n_dirname_checks; i++)
316 dirname = g_dirname (dirname_checks[i].filename);
317 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
319 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
320 dirname_checks[i].filename,
321 dirname_checks[i].dirname,
323 n_dirname_checks = 0;
327 if (n_dirname_checks)
330 g_print ("checking doubly linked lists...");
333 for (i = 0; i < 10; i++)
334 list = g_list_append (list, &nums[i]);
335 list = g_list_reverse (list);
337 for (i = 0; i < 10; i++)
339 t = g_list_nth (list, i);
340 if (*((gint*) t->data) != (9 - i))
341 g_error ("Regular insert failed");
344 for (i = 0; i < 10; i++)
345 if(g_list_position(list, g_list_nth (list, i)) != i)
346 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
351 for (i = 0; i < 10; i++)
352 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
356 g_list_foreach (list, my_list_print, NULL);
359 for (i = 0; i < 10; i++)
361 t = g_list_nth (list, i);
362 if (*((gint*) t->data) != i)
363 g_error ("Sorted insert failed");
369 for (i = 0; i < 10; i++)
370 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
374 g_list_foreach (list, my_list_print, NULL);
377 for (i = 0; i < 10; i++)
379 t = g_list_nth (list, i);
380 if (*((gint*) t->data) != (9 - i))
381 g_error ("Sorted insert failed");
389 g_print ("checking singly linked lists...");
392 for (i = 0; i < 10; i++)
393 slist = g_slist_append (slist, &nums[i]);
394 slist = g_slist_reverse (slist);
396 for (i = 0; i < 10; i++)
398 st = g_slist_nth (slist, i);
399 if (*((gint*) st->data) != (9 - i))
403 g_slist_free (slist);
406 for (i = 0; i < 10; i++)
407 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
411 g_slist_foreach (slist, my_list_print, NULL);
414 for (i = 0; i < 10; i++)
416 st = g_slist_nth (slist, i);
417 if (*((gint*) st->data) != i)
418 g_error ("Sorted insert failed");
424 for (i = 0; i < 10; i++)
425 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
429 g_slist_foreach (slist, my_list_print, NULL);
432 for (i = 0; i < 10; i++)
434 st = g_slist_nth (slist, i);
435 if (*((gint*) st->data) != (9 - i))
436 g_error("Sorted insert failed");
444 g_print ("checking binary trees...\n");
446 tree = g_tree_new (my_compare);
448 for (j = 0; j < 10; j++, i++)
451 g_tree_insert (tree, &chars[i], &chars[i]);
453 for (j = 0; j < 26; j++, i++)
456 g_tree_insert (tree, &chars[i], &chars[i]);
458 for (j = 0; j < 26; j++, i++)
461 g_tree_insert (tree, &chars[i], &chars[i]);
464 g_print ("tree height: %d\n", g_tree_height (tree));
465 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
468 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
471 for (i = 0; i < 10; i++)
472 g_tree_remove (tree, &chars[i]);
474 g_print ("tree height: %d\n", g_tree_height (tree));
475 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
478 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
484 /* check n-way trees */
487 g_print ("checking mem chunks...");
489 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
491 for (i = 0; i < 10000; i++)
493 mem[i] = g_chunk_new (gchar, mem_chunk);
495 for (j = 0; j < 50; j++)
499 for (i = 0; i < 10000; i++)
501 g_mem_chunk_free (mem_chunk, mem[i]);
507 g_print ("checking hash tables...");
509 hash_table = g_hash_table_new (my_hash, my_hash_compare);
510 for (i = 0; i < 10000; i++)
513 g_hash_table_insert (hash_table, &array[i], &array[i]);
515 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
517 for (i = 0; i < 10000; i++)
521 for (i = 0; i < 10000; i++)
522 g_hash_table_remove (hash_table, &array[i]);
524 g_hash_table_destroy (hash_table);
529 g_print ("checking string chunks...");
531 string_chunk = g_string_chunk_new (1024);
533 for (i = 0; i < 100000; i ++)
535 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
537 if (strcmp ("hi pete", tmp_string) != 0)
538 g_error ("string chunks are broken.\n");
541 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
543 g_assert (tmp_string_2 != tmp_string &&
544 strcmp(tmp_string_2, tmp_string) == 0);
546 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
548 g_assert (tmp_string_2 == tmp_string);
550 g_string_chunk_free (string_chunk);
555 g_print ("checking arrays...");
557 garray = g_array_new (FALSE);
558 for (i = 0; i < 10000; i++)
559 g_array_append_val (garray, gint, i);
561 for (i = 0; i < 10000; i++)
562 if (g_array_index (garray, gint, i) != i)
563 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
565 g_array_free (garray, TRUE);
567 garray = g_array_new (FALSE);
568 for (i = 0; i < 10000; i++)
569 g_array_prepend_val (garray, gint, i);
571 for (i = 0; i < 10000; i++)
572 if (g_array_index (garray, gint, i) != (10000 - i - 1))
573 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 10000 - i - 1);
575 g_array_free (garray, TRUE);
580 g_print ("checking strings...");
582 string1 = g_string_new ("hi pete!");
583 string2 = g_string_new ("");
585 g_assert (strcmp ("hi pete!", string1->str) == 0);
587 for (i = 0; i < 10000; i++)
588 g_string_append_c (string1, 'a'+(i%26));
590 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
591 "this pete guy sure is a wuss, like he's the number ",
593 " wuss. everyone agrees.\n",
595 10, 666, 15, 15, 666.666666666, 666.666666666);
599 g_print ("checking timers...\n");
601 timer = g_timer_new ();
602 g_print (" spinning for 3 seconds...\n");
604 g_timer_start (timer);
605 while (g_timer_elapsed (timer, NULL) < 3)
608 g_timer_stop (timer);
609 g_timer_destroy (timer);
613 g_print ("checking g_strcasecmp...\n");
615 /* g_debug (argv[0]); */
619 g_print ("checking relations...");
621 relation = g_relation_new (2);
623 g_relation_index (relation, 0, g_int_hash, g_int_equal);
624 g_relation_index (relation, 1, g_int_hash, g_int_equal);
626 for (i = 0; i < 1024; i += 1)
629 for (i = 1; i < 1023; i += 1)
631 g_relation_insert (relation, data + i, data + i + 1);
632 g_relation_insert (relation, data + i, data + i - 1);
635 for (i = 2; i < 1022; i += 1)
637 g_assert (! g_relation_exists (relation, data + i, data + i));
638 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
639 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
642 for (i = 1; i < 1023; i += 1)
644 g_assert (g_relation_exists (relation, data + i, data + i + 1));
645 g_assert (g_relation_exists (relation, data + i, data + i - 1));
648 for (i = 2; i < 1022; i += 1)
650 g_assert (g_relation_count (relation, data + i, 0) == 2);
651 g_assert (g_relation_count (relation, data + i, 1) == 2);
654 g_assert (g_relation_count (relation, data, 0) == 0);
656 g_assert (g_relation_count (relation, data + 42, 0) == 2);
657 g_assert (g_relation_count (relation, data + 43, 1) == 2);
658 g_assert (g_relation_count (relation, data + 41, 1) == 2);
659 g_relation_delete (relation, data + 42, 0);
660 g_assert (g_relation_count (relation, data + 42, 0) == 0);
661 g_assert (g_relation_count (relation, data + 43, 1) == 1);
662 g_assert (g_relation_count (relation, data + 41, 1) == 1);
664 tuples = g_relation_select (relation, data + 200, 0);
666 g_assert (tuples->len == 2);
669 for (i = 0; i < tuples->len; i += 1)
672 *(gint*) g_tuples_index (tuples, i, 0),
673 *(gint*) g_tuples_index (tuples, i, 1));
677 g_assert (g_relation_exists (relation, data + 300, data + 301));
678 g_relation_delete (relation, data + 300, 0);
679 g_assert (!g_relation_exists (relation, data + 300, data + 301));
681 g_tuples_destroy (tuples);
683 g_relation_destroy (relation);
689 g_print ("checking pointer arrays...");
691 gparray = g_ptr_array_new ();
692 for (i = 0; i < 10000; i++)
693 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
695 for (i = 0; i < 10000; i++)
696 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
697 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
699 g_ptr_array_free (gparray, TRUE);
704 g_print ("checking byte arrays...");
706 gbarray = g_byte_array_new ();
707 for (i = 0; i < 10000; i++)
708 g_byte_array_append (gbarray, "abcd", 4);
710 for (i = 0; i < 10000; i++)
712 g_assert (gbarray->data[4*i] == 'a');
713 g_assert (gbarray->data[4*i+1] == 'b');
714 g_assert (gbarray->data[4*i+2] == 'c');
715 g_assert (gbarray->data[4*i+3] == 'd');
718 g_byte_array_free (gbarray, TRUE);