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.
26 gboolean failed = FALSE;
28 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
31 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
33 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
36 g_print ("."); fflush (stdout); \
39 #define C2P(c) ((gpointer) ((long) (c)))
40 #define P2C(p) ((gchar) ((long) (p)))
43 node_build_string (GNode *node,
50 c[0] = P2C (node->data);
52 string = g_strconcat (*p ? *p : "", c, NULL);
71 g_print ("checking n-way trees: ");
74 root = g_node_new (C2P ('A'));
75 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
77 node_B = g_node_new (C2P ('B'));
78 g_node_append (root, node_B);
79 TEST (NULL, root->children == node_B);
81 g_node_append_data (node_B, C2P ('E'));
82 g_node_prepend_data (node_B, C2P ('C'));
83 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
85 node_F = g_node_new (C2P ('F'));
86 g_node_append (root, node_F);
87 TEST (NULL, root->children->next == node_F);
89 node_G = g_node_new (C2P ('G'));
90 g_node_append (node_F, node_G);
91 node_J = g_node_new (C2P ('J'));
92 g_node_prepend (node_G, node_J);
93 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
94 g_node_insert_data (node_G, 0, C2P ('H'));
95 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
97 TEST (NULL, g_node_depth (root) == 1);
98 TEST (NULL, g_node_max_height (root) == 4);
99 TEST (NULL, g_node_depth (node_G->children->next) == 4);
100 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
101 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
102 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
103 TEST (NULL, g_node_max_height (node_F) == 3);
104 TEST (NULL, g_node_n_children (node_G) == 4);
105 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
106 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
107 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
109 for (i = 0; i < g_node_n_children (node_B); i++)
111 node = g_node_nth_child (node_B, i);
112 TEST (NULL, P2C (node->data) == ('C' + i));
115 for (i = 0; i < g_node_n_children (node_G); i++)
116 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
126 * for in-order traversal, 'G' is considered to be the "left"
127 * child of 'F', which will cause 'F' to be the last node visited.
131 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
132 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
133 g_free (tstring); tstring = NULL;
134 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
135 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
136 g_free (tstring); tstring = NULL;
137 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
138 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
139 g_free (tstring); tstring = NULL;
140 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
141 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
142 g_free (tstring); tstring = NULL;
144 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
145 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
146 g_free (tstring); tstring = NULL;
147 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
148 TEST (tstring, strcmp (tstring, "ABFG") == 0);
149 g_free (tstring); tstring = NULL;
151 g_node_reverse_children (node_B);
152 g_node_reverse_children (node_G);
154 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
155 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
156 g_free (tstring); tstring = NULL;
158 g_node_destroy (root);
160 /* allocation tests */
162 root = g_node_new (NULL);
165 for (i = 0; i < 2048; i++)
167 g_node_append (node, g_node_new (NULL));
169 node = node->children->next;
171 TEST (NULL, g_node_max_height (root) > 100);
172 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
174 g_node_destroy (root);
181 my_hash_callback_remove (gpointer key,
194 my_hash_callback_remove_test (gpointer key,
205 my_hash_callback (gpointer key,
214 my_hash (gconstpointer key)
216 return (guint) *((const gint*) key);
220 my_hash_compare (gconstpointer a,
223 return *((const gint*) a) == *((const gint*) b);
227 my_list_compare_one (gconstpointer a, gconstpointer b)
229 gint one = *((const gint*)a);
230 gint two = *((const gint*)b);
235 my_list_compare_two (gconstpointer a, gconstpointer b)
237 gint one = *((const gint*)a);
238 gint two = *((const gint*)b);
243 my_list_print (gpointer a, gpointer b)
245 gint three = *((gint*)a);
246 g_print("%d", three);
250 my_compare (gconstpointer a,
260 my_traverse (gpointer key,
265 g_print ("%c ", *ch);
275 GHashTable *hash_table;
276 GMemChunk *mem_chunk;
277 GStringChunk *string_chunk;
279 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
280 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
283 gchar *mem[10000], *tmp_string, *tmp_string_2;
288 GString *string1, *string2;
297 } dirname_checks[] = {
312 { ".\\\\\\\\", "." },
316 { "..\\\\\\\\", ".." },
319 { "a\\b\\", "a\\b" },
323 guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
325 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
332 string = g_get_current_dir ();
333 g_print ("cwd: %s\n", string);
337 g_print ("checking size of gint8: %d", (int)sizeof (gint8));
338 TEST (NULL, sizeof (gint8) == 1);
339 g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
340 TEST (NULL, sizeof (gint16) == 2);
341 g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
342 TEST (NULL, sizeof (gint32) == 4);
344 g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
345 TEST (NULL, sizeof (gint64) == 8);
346 #endif /* HAVE_GINT64 */
349 g_print ("checking g_dirname()...");
350 for (i = 0; i < n_dirname_checks; i++)
354 dirname = g_dirname (dirname_checks[i].filename);
355 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
357 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
358 dirname_checks[i].filename,
359 dirname_checks[i].dirname,
361 n_dirname_checks = 0;
365 if (n_dirname_checks)
368 g_print ("checking doubly linked lists...");
371 for (i = 0; i < 10; i++)
372 list = g_list_append (list, &nums[i]);
373 list = g_list_reverse (list);
375 for (i = 0; i < 10; i++)
377 t = g_list_nth (list, i);
378 if (*((gint*) t->data) != (9 - i))
379 g_error ("Regular insert failed");
382 for (i = 0; i < 10; i++)
383 if(g_list_position(list, g_list_nth (list, i)) != i)
384 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
389 for (i = 0; i < 10; i++)
390 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
394 g_list_foreach (list, my_list_print, NULL);
397 for (i = 0; i < 10; i++)
399 t = g_list_nth (list, i);
400 if (*((gint*) t->data) != i)
401 g_error ("Sorted insert failed");
407 for (i = 0; i < 10; i++)
408 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
412 g_list_foreach (list, my_list_print, NULL);
415 for (i = 0; i < 10; i++)
417 t = g_list_nth (list, i);
418 if (*((gint*) t->data) != (9 - i))
419 g_error ("Sorted insert failed");
427 g_print ("checking singly linked lists...");
430 for (i = 0; i < 10; i++)
431 slist = g_slist_append (slist, &nums[i]);
432 slist = g_slist_reverse (slist);
434 for (i = 0; i < 10; i++)
436 st = g_slist_nth (slist, i);
437 if (*((gint*) st->data) != (9 - i))
441 g_slist_free (slist);
444 for (i = 0; i < 10; i++)
445 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
449 g_slist_foreach (slist, my_list_print, NULL);
452 for (i = 0; i < 10; i++)
454 st = g_slist_nth (slist, i);
455 if (*((gint*) st->data) != i)
456 g_error ("Sorted insert failed");
462 for (i = 0; i < 10; i++)
463 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
467 g_slist_foreach (slist, my_list_print, NULL);
470 for (i = 0; i < 10; i++)
472 st = g_slist_nth (slist, i);
473 if (*((gint*) st->data) != (9 - i))
474 g_error("Sorted insert failed");
482 g_print ("checking binary trees...\n");
484 tree = g_tree_new (my_compare);
486 for (j = 0; j < 10; j++, i++)
489 g_tree_insert (tree, &chars[i], &chars[i]);
491 for (j = 0; j < 26; j++, i++)
494 g_tree_insert (tree, &chars[i], &chars[i]);
496 for (j = 0; j < 26; j++, i++)
499 g_tree_insert (tree, &chars[i], &chars[i]);
502 g_print ("tree height: %d\n", g_tree_height (tree));
503 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
506 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
509 for (i = 0; i < 10; i++)
510 g_tree_remove (tree, &chars[i]);
512 g_print ("tree height: %d\n", g_tree_height (tree));
513 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
516 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
522 /* check n-way trees */
525 g_print ("checking mem chunks...");
527 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
529 for (i = 0; i < 10000; i++)
531 mem[i] = g_chunk_new (gchar, mem_chunk);
533 for (j = 0; j < 50; j++)
537 for (i = 0; i < 10000; i++)
539 g_mem_chunk_free (mem_chunk, mem[i]);
545 g_print ("checking hash tables...");
547 hash_table = g_hash_table_new (my_hash, my_hash_compare);
548 for (i = 0; i < 10000; i++)
551 g_hash_table_insert (hash_table, &array[i], &array[i]);
553 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
555 for (i = 0; i < 10000; i++)
559 for (i = 0; i < 10000; i++)
560 g_hash_table_remove (hash_table, &array[i]);
562 for (i = 0; i < 10000; i++)
565 g_hash_table_insert (hash_table, &array[i], &array[i]);
568 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
569 g_hash_table_size (hash_table) != 5000)
572 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
575 g_hash_table_destroy (hash_table);
580 g_print ("checking string chunks...");
582 string_chunk = g_string_chunk_new (1024);
584 for (i = 0; i < 100000; i ++)
586 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
588 if (strcmp ("hi pete", tmp_string) != 0)
589 g_error ("string chunks are broken.\n");
592 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
594 g_assert (tmp_string_2 != tmp_string &&
595 strcmp(tmp_string_2, tmp_string) == 0);
597 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
599 g_assert (tmp_string_2 == tmp_string);
601 g_string_chunk_free (string_chunk);
606 g_print ("checking arrays...");
608 garray = g_array_new (FALSE, FALSE, sizeof (gint));
609 for (i = 0; i < 10000; i++)
610 g_array_append_val (garray, i);
612 for (i = 0; i < 10000; i++)
613 if (g_array_index (garray, gint, i) != i)
614 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
616 g_array_free (garray, TRUE);
618 garray = g_array_new (FALSE, FALSE, sizeof (gint));
619 for (i = 0; i < 100; i++)
620 g_array_prepend_val (garray, i);
622 for (i = 0; i < 100; i++)
623 if (g_array_index (garray, gint, i) != (100 - i - 1))
624 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
626 g_array_free (garray, TRUE);
631 g_print ("checking strings...");
633 string1 = g_string_new ("hi pete!");
634 string2 = g_string_new ("");
636 g_assert (strcmp ("hi pete!", string1->str) == 0);
638 for (i = 0; i < 10000; i++)
639 g_string_append_c (string1, 'a'+(i%26));
641 #if !(defined (_MSC_VER) || defined (__LCC__))
642 /* MSVC and LCC use the same run-time C library, which doesn't like
643 the %10000.10000f format... */
644 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
645 "this pete guy sure is a wuss, like he's the number ",
647 " wuss. everyone agrees.\n",
649 10, 666, 15, 15, 666.666666666, 666.666666666);
651 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
652 "this pete guy sure is a wuss, like he's the number ",
654 " wuss. everyone agrees.\n",
656 10, 666, 15, 15, 666.666666666, 666.666666666);
659 g_print ("string2 length = %d...\n", string2->len);
660 string2->str[70] = '\0';
661 g_print ("first 70 chars:\n%s\n", string2->str);
662 string2->str[141] = '\0';
663 g_print ("next 70 chars:\n%s\n", string2->str+71);
664 string2->str[212] = '\0';
665 g_print ("and next 70:\n%s\n", string2->str+142);
666 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
670 g_print ("checking timers...\n");
672 timer = g_timer_new ();
673 g_print (" spinning for 3 seconds...\n");
675 g_timer_start (timer);
676 while (g_timer_elapsed (timer, NULL) < 3)
679 g_timer_stop (timer);
680 g_timer_destroy (timer);
684 g_print ("checking g_strcasecmp...");
685 g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0);
686 g_assert (g_strcasecmp ("frobozz", "frobozz") == 0);
687 g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0);
688 g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0);
689 g_assert (g_strcasecmp ("", "") == 0);
690 g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0);
691 g_assert (g_strcasecmp ("a", "b") < 0);
692 g_assert (g_strcasecmp ("a", "B") < 0);
693 g_assert (g_strcasecmp ("A", "b") < 0);
694 g_assert (g_strcasecmp ("A", "B") < 0);
695 g_assert (g_strcasecmp ("b", "a") > 0);
696 g_assert (g_strcasecmp ("b", "A") > 0);
697 g_assert (g_strcasecmp ("B", "a") > 0);
698 g_assert (g_strcasecmp ("B", "A") > 0);
702 /* g_debug (argv[0]); */
706 g_print ("checking relations...");
708 relation = g_relation_new (2);
710 g_relation_index (relation, 0, g_int_hash, g_int_equal);
711 g_relation_index (relation, 1, g_int_hash, g_int_equal);
713 for (i = 0; i < 1024; i += 1)
716 for (i = 1; i < 1023; i += 1)
718 g_relation_insert (relation, data + i, data + i + 1);
719 g_relation_insert (relation, data + i, data + i - 1);
722 for (i = 2; i < 1022; i += 1)
724 g_assert (! g_relation_exists (relation, data + i, data + i));
725 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
726 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
729 for (i = 1; i < 1023; i += 1)
731 g_assert (g_relation_exists (relation, data + i, data + i + 1));
732 g_assert (g_relation_exists (relation, data + i, data + i - 1));
735 for (i = 2; i < 1022; i += 1)
737 g_assert (g_relation_count (relation, data + i, 0) == 2);
738 g_assert (g_relation_count (relation, data + i, 1) == 2);
741 g_assert (g_relation_count (relation, data, 0) == 0);
743 g_assert (g_relation_count (relation, data + 42, 0) == 2);
744 g_assert (g_relation_count (relation, data + 43, 1) == 2);
745 g_assert (g_relation_count (relation, data + 41, 1) == 2);
746 g_relation_delete (relation, data + 42, 0);
747 g_assert (g_relation_count (relation, data + 42, 0) == 0);
748 g_assert (g_relation_count (relation, data + 43, 1) == 1);
749 g_assert (g_relation_count (relation, data + 41, 1) == 1);
751 tuples = g_relation_select (relation, data + 200, 0);
753 g_assert (tuples->len == 2);
756 for (i = 0; i < tuples->len; i += 1)
759 *(gint*) g_tuples_index (tuples, i, 0),
760 *(gint*) g_tuples_index (tuples, i, 1));
764 g_assert (g_relation_exists (relation, data + 300, data + 301));
765 g_relation_delete (relation, data + 300, 0);
766 g_assert (!g_relation_exists (relation, data + 300, data + 301));
768 g_tuples_destroy (tuples);
770 g_relation_destroy (relation);
776 g_print ("checking pointer arrays...");
778 gparray = g_ptr_array_new ();
779 for (i = 0; i < 10000; i++)
780 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
782 for (i = 0; i < 10000; i++)
783 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
784 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
786 g_ptr_array_free (gparray, TRUE);
791 g_print ("checking byte arrays...");
793 gbarray = g_byte_array_new ();
794 for (i = 0; i < 10000; i++)
795 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
797 for (i = 0; i < 10000; i++)
799 g_assert (gbarray->data[4*i] == 'a');
800 g_assert (gbarray->data[4*i+1] == 'b');
801 g_assert (gbarray->data[4*i+2] == 'c');
802 g_assert (gbarray->data[4*i+3] == 'd');
805 g_byte_array_free (gbarray, TRUE);
808 g_printerr ("g_log tests:");
809 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
810 g_message ("the next warning is a test:");