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)))
42 #define GLIB_TEST_STRING "el dorado "
43 #define GLIB_TEST_STRING_5 "el do"
46 node_build_string (GNode *node,
53 c[0] = P2C (node->data);
55 string = g_strconcat (*p ? *p : "", c, NULL);
74 g_print ("checking n-way trees: ");
77 root = g_node_new (C2P ('A'));
78 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
80 node_B = g_node_new (C2P ('B'));
81 g_node_append (root, node_B);
82 TEST (NULL, root->children == node_B);
84 g_node_append_data (node_B, C2P ('E'));
85 g_node_prepend_data (node_B, C2P ('C'));
86 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
88 node_F = g_node_new (C2P ('F'));
89 g_node_append (root, node_F);
90 TEST (NULL, root->children->next == node_F);
92 node_G = g_node_new (C2P ('G'));
93 g_node_append (node_F, node_G);
94 node_J = g_node_new (C2P ('J'));
95 g_node_prepend (node_G, node_J);
96 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
97 g_node_insert_data (node_G, 0, C2P ('H'));
98 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
100 TEST (NULL, g_node_depth (root) == 1);
101 TEST (NULL, g_node_max_height (root) == 4);
102 TEST (NULL, g_node_depth (node_G->children->next) == 4);
103 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
104 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
105 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
106 TEST (NULL, g_node_max_height (node_F) == 3);
107 TEST (NULL, g_node_n_children (node_G) == 4);
108 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
109 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
110 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
112 for (i = 0; i < g_node_n_children (node_B); i++)
114 node = g_node_nth_child (node_B, i);
115 TEST (NULL, P2C (node->data) == ('C' + i));
118 for (i = 0; i < g_node_n_children (node_G); i++)
119 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
129 * for in-order traversal, 'G' is considered to be the "left"
130 * child of 'F', which will cause 'F' to be the last node visited.
134 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
135 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
136 g_free (tstring); tstring = NULL;
137 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
138 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
139 g_free (tstring); tstring = NULL;
140 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
141 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
142 g_free (tstring); tstring = NULL;
143 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
144 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
145 g_free (tstring); tstring = NULL;
147 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
148 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
149 g_free (tstring); tstring = NULL;
150 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
151 TEST (tstring, strcmp (tstring, "ABFG") == 0);
152 g_free (tstring); tstring = NULL;
154 g_node_reverse_children (node_B);
155 g_node_reverse_children (node_G);
157 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
158 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
159 g_free (tstring); tstring = NULL;
161 g_node_destroy (root);
163 /* allocation tests */
165 root = g_node_new (NULL);
168 for (i = 0; i < 2048; i++)
170 g_node_append (node, g_node_new (NULL));
172 node = node->children->next;
174 TEST (NULL, g_node_max_height (root) > 100);
175 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
177 g_node_destroy (root);
184 my_hash_callback_remove (gpointer key,
197 my_hash_callback_remove_test (gpointer key,
208 my_hash_callback (gpointer key,
217 my_hash (gconstpointer key)
219 return (guint) *((const gint*) key);
223 my_hash_compare (gconstpointer a,
226 return *((const gint*) a) == *((const gint*) b);
230 my_list_compare_one (gconstpointer a, gconstpointer b)
232 gint one = *((const gint*)a);
233 gint two = *((const gint*)b);
238 my_list_compare_two (gconstpointer a, gconstpointer b)
240 gint one = *((const gint*)a);
241 gint two = *((const gint*)b);
246 my_list_print (gpointer a, gpointer b)
248 gint three = *((gint*)a);
249 g_print("%d", three);
253 my_compare (gconstpointer a,
263 my_traverse (gpointer key,
268 g_print ("%c ", *ch);
278 GHashTable *hash_table;
279 GMemChunk *mem_chunk;
280 GStringChunk *string_chunk;
282 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
283 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
286 gchar *mem[10000], *tmp_string, *tmp_string_2;
291 GString *string1, *string2;
300 } dirname_checks[] = {
315 { ".\\\\\\\\", "." },
319 { "..\\\\\\\\", ".." },
322 { "a\\b\\", "a\\b" },
326 guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
327 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
328 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
330 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
331 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
334 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
341 string = g_get_current_dir ();
342 g_print ("cwd: %s\n", string);
346 g_print ("checking size of gint8: %d", (int)sizeof (gint8));
347 TEST (NULL, sizeof (gint8) == 1);
348 g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
349 TEST (NULL, sizeof (gint16) == 2);
350 g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
351 TEST (NULL, sizeof (gint32) == 4);
353 g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
354 TEST (NULL, sizeof (gint64) == 8);
355 #endif /* G_HAVE_GINT64 */
358 g_print ("checking g_dirname()...");
359 for (i = 0; i < n_dirname_checks; i++)
363 dirname = g_dirname (dirname_checks[i].filename);
364 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
366 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
367 dirname_checks[i].filename,
368 dirname_checks[i].dirname,
370 n_dirname_checks = 0;
374 if (n_dirname_checks)
377 g_print ("checking doubly linked lists...");
380 for (i = 0; i < 10; i++)
381 list = g_list_append (list, &nums[i]);
382 list = g_list_reverse (list);
384 for (i = 0; i < 10; i++)
386 t = g_list_nth (list, i);
387 if (*((gint*) t->data) != (9 - i))
388 g_error ("Regular insert failed");
391 for (i = 0; i < 10; i++)
392 if(g_list_position(list, g_list_nth (list, i)) != i)
393 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
398 for (i = 0; i < 10; i++)
399 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
403 g_list_foreach (list, my_list_print, NULL);
406 for (i = 0; i < 10; i++)
408 t = g_list_nth (list, i);
409 if (*((gint*) t->data) != i)
410 g_error ("Sorted insert failed");
416 for (i = 0; i < 10; i++)
417 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
421 g_list_foreach (list, my_list_print, NULL);
424 for (i = 0; i < 10; i++)
426 t = g_list_nth (list, i);
427 if (*((gint*) t->data) != (9 - i))
428 g_error ("Sorted insert failed");
434 for (i = 0; i < 10; i++)
435 list = g_list_prepend (list, &morenums[i]);
437 list = g_list_sort (list, my_list_compare_two);
441 g_list_foreach (list, my_list_print, NULL);
444 for (i = 0; i < 10; i++)
446 t = g_list_nth (list, i);
447 if (*((gint*) t->data) != (9 - i))
448 g_error ("Merge sort failed");
456 g_print ("checking singly linked lists...");
459 for (i = 0; i < 10; i++)
460 slist = g_slist_append (slist, &nums[i]);
461 slist = g_slist_reverse (slist);
463 for (i = 0; i < 10; i++)
465 st = g_slist_nth (slist, i);
466 if (*((gint*) st->data) != (9 - i))
470 g_slist_free (slist);
473 for (i = 0; i < 10; i++)
474 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
478 g_slist_foreach (slist, my_list_print, NULL);
481 for (i = 0; i < 10; i++)
483 st = g_slist_nth (slist, i);
484 if (*((gint*) st->data) != i)
485 g_error ("Sorted insert failed");
491 for (i = 0; i < 10; i++)
492 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
496 g_slist_foreach (slist, my_list_print, NULL);
499 for (i = 0; i < 10; i++)
501 st = g_slist_nth (slist, i);
502 if (*((gint*) st->data) != (9 - i))
503 g_error("Sorted insert failed");
509 for (i = 0; i < 10; i++)
510 slist = g_slist_prepend (slist, &morenums[i]);
512 slist = g_slist_sort (slist, my_list_compare_two);
516 g_slist_foreach (slist, my_list_print, NULL);
519 for (i = 0; i < 10; i++)
521 st = g_slist_nth (slist, i);
522 if (*((gint*) st->data) != (9 - i))
523 g_error("Sorted insert failed");
531 g_print ("checking binary trees...\n");
533 tree = g_tree_new (my_compare);
535 for (j = 0; j < 10; j++, i++)
538 g_tree_insert (tree, &chars[i], &chars[i]);
540 for (j = 0; j < 26; j++, i++)
543 g_tree_insert (tree, &chars[i], &chars[i]);
545 for (j = 0; j < 26; j++, i++)
548 g_tree_insert (tree, &chars[i], &chars[i]);
551 g_print ("tree height: %d\n", g_tree_height (tree));
552 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
555 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
558 for (i = 0; i < 10; i++)
559 g_tree_remove (tree, &chars[i]);
561 g_print ("tree height: %d\n", g_tree_height (tree));
562 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
565 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
571 /* check n-way trees */
574 g_print ("checking mem chunks...");
576 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
578 for (i = 0; i < 10000; i++)
580 mem[i] = g_chunk_new (gchar, mem_chunk);
582 for (j = 0; j < 50; j++)
586 for (i = 0; i < 10000; i++)
588 g_mem_chunk_free (mem_chunk, mem[i]);
594 g_print ("checking hash tables...");
596 hash_table = g_hash_table_new (my_hash, my_hash_compare);
597 for (i = 0; i < 10000; i++)
600 g_hash_table_insert (hash_table, &array[i], &array[i]);
602 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
604 for (i = 0; i < 10000; i++)
608 for (i = 0; i < 10000; i++)
609 g_hash_table_remove (hash_table, &array[i]);
611 for (i = 0; i < 10000; i++)
614 g_hash_table_insert (hash_table, &array[i], &array[i]);
617 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
618 g_hash_table_size (hash_table) != 5000)
621 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
624 g_hash_table_destroy (hash_table);
629 g_print ("checking string chunks...");
631 string_chunk = g_string_chunk_new (1024);
633 for (i = 0; i < 100000; i ++)
635 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
637 if (strcmp ("hi pete", tmp_string) != 0)
638 g_error ("string chunks are broken.\n");
641 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
643 g_assert (tmp_string_2 != tmp_string &&
644 strcmp(tmp_string_2, tmp_string) == 0);
646 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
648 g_assert (tmp_string_2 == tmp_string);
650 g_string_chunk_free (string_chunk);
655 g_print ("checking arrays...");
657 garray = g_array_new (FALSE, FALSE, sizeof (gint));
658 for (i = 0; i < 10000; i++)
659 g_array_append_val (garray, i);
661 for (i = 0; i < 10000; i++)
662 if (g_array_index (garray, gint, i) != i)
663 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
665 g_array_free (garray, TRUE);
667 garray = g_array_new (FALSE, FALSE, sizeof (gint));
668 for (i = 0; i < 100; i++)
669 g_array_prepend_val (garray, i);
671 for (i = 0; i < 100; i++)
672 if (g_array_index (garray, gint, i) != (100 - i - 1))
673 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
675 g_array_free (garray, TRUE);
680 g_print ("checking strings...");
682 string1 = g_string_new ("hi pete!");
683 string2 = g_string_new ("");
685 g_assert (strcmp ("hi pete!", string1->str) == 0);
687 for (i = 0; i < 10000; i++)
688 g_string_append_c (string1, 'a'+(i%26));
690 #if !(defined (_MSC_VER) || defined (__LCC__))
691 /* MSVC and LCC use the same run-time C library, which doesn't like
692 the %10000.10000f format... */
693 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
694 "this pete guy sure is a wuss, like he's the number ",
696 " wuss. everyone agrees.\n",
698 10, 666, 15, 15, 666.666666666, 666.666666666);
700 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
701 "this pete guy sure is a wuss, like he's the number ",
703 " wuss. everyone agrees.\n",
705 10, 666, 15, 15, 666.666666666, 666.666666666);
708 g_print ("string2 length = %d...\n", string2->len);
709 string2->str[70] = '\0';
710 g_print ("first 70 chars:\n%s\n", string2->str);
711 string2->str[141] = '\0';
712 g_print ("next 70 chars:\n%s\n", string2->str+71);
713 string2->str[212] = '\0';
714 g_print ("and next 70:\n%s\n", string2->str+142);
715 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
719 g_print ("checking timers...\n");
721 timer = g_timer_new ();
722 g_print (" spinning for 3 seconds...\n");
724 g_timer_start (timer);
725 while (g_timer_elapsed (timer, NULL) < 3)
728 g_timer_stop (timer);
729 g_timer_destroy (timer);
733 g_print ("checking g_strcasecmp...");
734 g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0);
735 g_assert (g_strcasecmp ("frobozz", "frobozz") == 0);
736 g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0);
737 g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0);
738 g_assert (g_strcasecmp ("", "") == 0);
739 g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0);
740 g_assert (g_strcasecmp ("a", "b") < 0);
741 g_assert (g_strcasecmp ("a", "B") < 0);
742 g_assert (g_strcasecmp ("A", "b") < 0);
743 g_assert (g_strcasecmp ("A", "B") < 0);
744 g_assert (g_strcasecmp ("b", "a") > 0);
745 g_assert (g_strcasecmp ("b", "A") > 0);
746 g_assert (g_strcasecmp ("B", "a") > 0);
747 g_assert (g_strcasecmp ("B", "A") > 0);
751 g_print ("checking g_strdup...");
752 g_assert(g_strdup(NULL) == NULL);
753 string = g_strdup(GLIB_TEST_STRING);
754 g_assert(string != NULL);
755 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
760 g_print ("checking g_strconcat...");
761 string = g_strconcat(GLIB_TEST_STRING, NULL);
762 g_assert(string != NULL);
763 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
765 string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
766 GLIB_TEST_STRING, NULL);
767 g_assert(string != NULL);
768 g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
769 GLIB_TEST_STRING) == 0);
774 g_print ("checking g_strdup_printf...");
775 string = g_strdup_printf ("%05d %-5s", 21, "test");
776 g_assert (string != NULL);
777 g_assert (strcmp(string, "00021 test ") == 0);
782 /* g_debug (argv[0]); */
786 g_print ("checking relations...");
788 relation = g_relation_new (2);
790 g_relation_index (relation, 0, g_int_hash, g_int_equal);
791 g_relation_index (relation, 1, g_int_hash, g_int_equal);
793 for (i = 0; i < 1024; i += 1)
796 for (i = 1; i < 1023; i += 1)
798 g_relation_insert (relation, data + i, data + i + 1);
799 g_relation_insert (relation, data + i, data + i - 1);
802 for (i = 2; i < 1022; i += 1)
804 g_assert (! g_relation_exists (relation, data + i, data + i));
805 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
806 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
809 for (i = 1; i < 1023; i += 1)
811 g_assert (g_relation_exists (relation, data + i, data + i + 1));
812 g_assert (g_relation_exists (relation, data + i, data + i - 1));
815 for (i = 2; i < 1022; i += 1)
817 g_assert (g_relation_count (relation, data + i, 0) == 2);
818 g_assert (g_relation_count (relation, data + i, 1) == 2);
821 g_assert (g_relation_count (relation, data, 0) == 0);
823 g_assert (g_relation_count (relation, data + 42, 0) == 2);
824 g_assert (g_relation_count (relation, data + 43, 1) == 2);
825 g_assert (g_relation_count (relation, data + 41, 1) == 2);
826 g_relation_delete (relation, data + 42, 0);
827 g_assert (g_relation_count (relation, data + 42, 0) == 0);
828 g_assert (g_relation_count (relation, data + 43, 1) == 1);
829 g_assert (g_relation_count (relation, data + 41, 1) == 1);
831 tuples = g_relation_select (relation, data + 200, 0);
833 g_assert (tuples->len == 2);
836 for (i = 0; i < tuples->len; i += 1)
839 *(gint*) g_tuples_index (tuples, i, 0),
840 *(gint*) g_tuples_index (tuples, i, 1));
844 g_assert (g_relation_exists (relation, data + 300, data + 301));
845 g_relation_delete (relation, data + 300, 0);
846 g_assert (!g_relation_exists (relation, data + 300, data + 301));
848 g_tuples_destroy (tuples);
850 g_relation_destroy (relation);
856 g_print ("checking pointer arrays...");
858 gparray = g_ptr_array_new ();
859 for (i = 0; i < 10000; i++)
860 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
862 for (i = 0; i < 10000; i++)
863 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
864 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
866 g_ptr_array_free (gparray, TRUE);
871 g_print ("checking byte arrays...");
873 gbarray = g_byte_array_new ();
874 for (i = 0; i < 10000; i++)
875 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
877 for (i = 0; i < 10000; i++)
879 g_assert (gbarray->data[4*i] == 'a');
880 g_assert (gbarray->data[4*i+1] == 'b');
881 g_assert (gbarray->data[4*i+2] == 'c');
882 g_assert (gbarray->data[4*i+3] == 'd');
885 g_byte_array_free (gbarray, TRUE);
888 g_printerr ("g_log tests:");
889 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
890 g_message ("the next warning is a test:");
894 g_print ("checking endian macros (host is ");
895 #if G_BYTE_ORDER == G_BIG_ENDIAN
896 g_print ("big endian)...");
898 g_print ("little endian)...");
900 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
901 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
903 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);