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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
42 #include <io.h> /* For read(), write() etc */
46 gboolean failed = FALSE;
48 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
51 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
53 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
56 g_print ("."); fflush (stdout); \
59 #define C2P(c) ((gpointer) ((long) (c)))
60 #define P2C(p) ((gchar) ((long) (p)))
62 #define GLIB_TEST_STRING "el dorado "
63 #define GLIB_TEST_STRING_5 "el do"
66 node_build_string (GNode *node,
73 c[0] = P2C (node->data);
75 string = g_strconcat (*p ? *p : "", c, NULL);
92 gchar *tstring, *cstring;
94 g_print ("checking n-way trees: ");
97 root = g_node_new (C2P ('A'));
98 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
100 node_B = g_node_new (C2P ('B'));
101 g_node_append (root, node_B);
102 TEST (NULL, root->children == node_B);
104 g_node_append_data (node_B, C2P ('E'));
105 g_node_prepend_data (node_B, C2P ('C'));
106 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
108 node_F = g_node_new (C2P ('F'));
109 g_node_append (root, node_F);
110 TEST (NULL, root->children->next == node_F);
112 node_G = g_node_new (C2P ('G'));
113 g_node_append (node_F, node_G);
114 node_J = g_node_new (C2P ('J'));
115 g_node_prepend (node_G, node_J);
116 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
117 g_node_insert_data (node_G, 0, C2P ('H'));
118 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
120 TEST (NULL, g_node_depth (root) == 1);
121 TEST (NULL, g_node_max_height (root) == 4);
122 TEST (NULL, g_node_depth (node_G->children->next) == 4);
123 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
124 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
125 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
126 TEST (NULL, g_node_max_height (node_F) == 3);
127 TEST (NULL, g_node_n_children (node_G) == 4);
128 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
129 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
130 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
132 for (i = 0; i < g_node_n_children (node_B); i++)
134 node = g_node_nth_child (node_B, i);
135 TEST (NULL, P2C (node->data) == ('C' + i));
138 for (i = 0; i < g_node_n_children (node_G); i++)
139 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
149 * for in-order traversal, 'G' is considered to be the "left"
150 * child of 'F', which will cause 'F' to be the last node visited.
154 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
155 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
156 g_free (tstring); tstring = NULL;
157 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
158 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
159 g_free (tstring); tstring = NULL;
160 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
161 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
162 g_free (tstring); tstring = NULL;
163 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
164 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
165 g_free (tstring); tstring = NULL;
167 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
168 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
169 g_free (tstring); tstring = NULL;
170 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
171 TEST (tstring, strcmp (tstring, "ABFG") == 0);
172 g_free (tstring); tstring = NULL;
174 g_node_reverse_children (node_B);
175 g_node_reverse_children (node_G);
177 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
178 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
179 g_free (tstring); tstring = NULL;
182 node = g_node_copy (root);
183 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
184 TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
185 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
186 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
187 TEST (cstring, strcmp (tstring, cstring) == 0);
188 g_free (tstring); tstring = NULL;
189 g_free (cstring); cstring = NULL;
190 g_node_destroy (node);
192 g_node_destroy (root);
194 /* allocation tests */
196 root = g_node_new (NULL);
199 for (i = 0; i < 2048; i++)
201 g_node_append (node, g_node_new (NULL));
203 node = node->children->next;
205 TEST (NULL, g_node_max_height (root) > 100);
206 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
208 g_node_destroy (root);
215 my_hash_callback_remove (gpointer key,
228 my_hash_callback_remove_test (gpointer key,
239 my_hash_callback (gpointer key,
248 my_hash (gconstpointer key)
250 return (guint) *((const gint*) key);
254 my_hash_equal (gconstpointer a,
257 return *((const gint*) a) == *((const gint*) b);
261 my_list_compare_one (gconstpointer a, gconstpointer b)
263 gint one = *((const gint*)a);
264 gint two = *((const gint*)b);
269 my_list_compare_two (gconstpointer a, gconstpointer b)
271 gint one = *((const gint*)a);
272 gint two = *((const gint*)b);
277 my_list_print (gpointer a, gpointer b)
279 gint three = *((gint*)a);
280 g_print("%d", three);
284 my_compare (gconstpointer a,
294 my_traverse (gpointer key,
299 g_print ("%c ", *ch);
309 GHashTable *hash_table;
310 GMemChunk *mem_chunk;
311 GStringChunk *string_chunk;
313 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
314 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
317 gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
322 GString *string1, *string2;
331 } dirname_checks[] = {
343 { ".\\\\\\\\", "." },
345 { "..\\\\\\\\", ".." },
347 { "a\\b\\", "a\\b" },
354 guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
359 } skip_root_checks[] = {
369 { "\\\\server\\foo", "" },
370 { "\\\\server\\foo\\bar", "bar" },
376 guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
378 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
379 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
381 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
382 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
384 const char hello[] = "Hello, World";
385 const int hellolen = sizeof (hello) - 1;
392 gchar *glib_dll = g_strdup_printf ("glib-%d.%d.dll",
397 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
404 string = g_get_current_dir ();
405 g_print ("cwd: %s\n", string);
407 g_print ("user: %s\n", g_get_user_name ());
408 g_print ("real: %s\n", g_get_real_name ());
409 g_print ("home: %s\n", g_get_home_dir ());
410 g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
413 g_print ("checking size of gint8: %d", (int)sizeof (gint8));
414 TEST (NULL, sizeof (gint8) == 1);
415 g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
416 TEST (NULL, sizeof (gint16) == 2);
417 g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
418 TEST (NULL, sizeof (gint32) == 4);
419 g_print ("\nchecking size of gsize: %d", (int)sizeof (gsize));
421 g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
422 TEST (NULL, sizeof (gint64) == 8);
423 #endif /* G_HAVE_GINT64 */
426 g_print ("checking g_path_get_dirname()...");
427 for (i = 0; i < n_dirname_checks; i++)
431 dirname = g_path_get_dirname (dirname_checks[i].filename);
432 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
434 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
435 dirname_checks[i].filename,
436 dirname_checks[i].dirname,
438 n_dirname_checks = 0;
442 if (n_dirname_checks)
445 g_print ("checking g_path_skip_root()...");
446 for (i = 0; i < n_skip_root_checks; i++)
448 const gchar *skipped;
450 skipped = g_path_skip_root (skip_root_checks[i].filename);
451 if ((skipped && !skip_root_checks[i].without_root) ||
452 (!skipped && skip_root_checks[i].without_root) ||
453 ((skipped && skip_root_checks[i].without_root) &&
454 strcmp (skipped, skip_root_checks[i].without_root)))
456 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
457 skip_root_checks[i].filename,
458 (skip_root_checks[i].without_root ?
459 skip_root_checks[i].without_root : "<NULL>"),
460 (skipped ? skipped : "<NULL>"));
461 n_skip_root_checks = 0;
464 if (n_skip_root_checks)
467 g_print ("checking doubly linked lists...");
470 for (i = 0; i < 10; i++)
471 list = g_list_append (list, &nums[i]);
472 list = g_list_reverse (list);
474 for (i = 0; i < 10; i++)
476 t = g_list_nth (list, i);
477 if (*((gint*) t->data) != (9 - i))
478 g_error ("Regular insert failed");
481 for (i = 0; i < 10; i++)
482 if(g_list_position(list, g_list_nth (list, i)) != i)
483 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
488 for (i = 0; i < 10; i++)
489 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
493 g_list_foreach (list, my_list_print, NULL);
496 for (i = 0; i < 10; i++)
498 t = g_list_nth (list, i);
499 if (*((gint*) t->data) != i)
500 g_error ("Sorted insert failed");
506 for (i = 0; i < 10; i++)
507 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
511 g_list_foreach (list, my_list_print, NULL);
514 for (i = 0; i < 10; i++)
516 t = g_list_nth (list, i);
517 if (*((gint*) t->data) != (9 - i))
518 g_error ("Sorted insert failed");
524 for (i = 0; i < 10; i++)
525 list = g_list_prepend (list, &morenums[i]);
527 list = g_list_sort (list, my_list_compare_two);
531 g_list_foreach (list, my_list_print, NULL);
534 for (i = 0; i < 10; i++)
536 t = g_list_nth (list, i);
537 if (*((gint*) t->data) != (9 - i))
538 g_error ("Merge sort failed");
546 g_print ("checking singly linked lists...");
549 for (i = 0; i < 10; i++)
550 slist = g_slist_append (slist, &nums[i]);
551 slist = g_slist_reverse (slist);
553 for (i = 0; i < 10; i++)
555 st = g_slist_nth (slist, i);
556 if (*((gint*) st->data) != (9 - i))
560 g_slist_free (slist);
563 for (i = 0; i < 10; i++)
564 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
568 g_slist_foreach (slist, my_list_print, NULL);
571 for (i = 0; i < 10; i++)
573 st = g_slist_nth (slist, i);
574 if (*((gint*) st->data) != i)
575 g_error ("Sorted insert failed");
581 for (i = 0; i < 10; i++)
582 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
586 g_slist_foreach (slist, my_list_print, NULL);
589 for (i = 0; i < 10; i++)
591 st = g_slist_nth (slist, i);
592 if (*((gint*) st->data) != (9 - i))
593 g_error("Sorted insert failed");
599 for (i = 0; i < 10; i++)
600 slist = g_slist_prepend (slist, &morenums[i]);
602 slist = g_slist_sort (slist, my_list_compare_two);
606 g_slist_foreach (slist, my_list_print, NULL);
609 for (i = 0; i < 10; i++)
611 st = g_slist_nth (slist, i);
612 if (*((gint*) st->data) != (9 - i))
613 g_error("Sorted insert failed");
621 g_print ("checking binary trees...\n");
623 tree = g_tree_new (my_compare);
625 for (j = 0; j < 10; j++, i++)
628 g_tree_insert (tree, &chars[i], &chars[i]);
630 for (j = 0; j < 26; j++, i++)
633 g_tree_insert (tree, &chars[i], &chars[i]);
635 for (j = 0; j < 26; j++, i++)
638 g_tree_insert (tree, &chars[i], &chars[i]);
641 g_print ("tree height: %d\n", g_tree_height (tree));
642 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
645 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
648 for (i = 0; i < 10; i++)
649 g_tree_remove (tree, &chars[i]);
651 g_print ("tree height: %d\n", g_tree_height (tree));
652 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
655 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
661 /* check n-way trees */
664 g_print ("checking mem chunks...");
666 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
668 for (i = 0; i < 10000; i++)
670 mem[i] = g_chunk_new (gchar, mem_chunk);
672 for (j = 0; j < 50; j++)
676 for (i = 0; i < 10000; i++)
678 g_mem_chunk_free (mem_chunk, mem[i]);
684 g_print ("checking hash tables...");
686 hash_table = g_hash_table_new (my_hash, my_hash_equal);
687 for (i = 0; i < 10000; i++)
690 g_hash_table_insert (hash_table, &array[i], &array[i]);
692 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
694 for (i = 0; i < 10000; i++)
698 for (i = 0; i < 10000; i++)
699 g_hash_table_remove (hash_table, &array[i]);
701 for (i = 0; i < 10000; i++)
704 g_hash_table_insert (hash_table, &array[i], &array[i]);
707 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
708 g_hash_table_size (hash_table) != 5000)
711 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
714 g_hash_table_destroy (hash_table);
719 g_print ("checking string chunks...");
721 string_chunk = g_string_chunk_new (1024);
723 for (i = 0; i < 100000; i ++)
725 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
727 if (strcmp ("hi pete", tmp_string) != 0)
728 g_error ("string chunks are broken.\n");
731 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
733 g_assert (tmp_string_2 != tmp_string &&
734 strcmp(tmp_string_2, tmp_string) == 0);
736 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
738 g_assert (tmp_string_2 == tmp_string);
740 g_string_chunk_free (string_chunk);
745 g_print ("checking arrays...");
747 garray = g_array_new (FALSE, FALSE, sizeof (gint));
748 for (i = 0; i < 10000; i++)
749 g_array_append_val (garray, i);
751 for (i = 0; i < 10000; i++)
752 if (g_array_index (garray, gint, i) != i)
753 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
755 g_array_free (garray, TRUE);
757 garray = g_array_new (FALSE, FALSE, sizeof (gint));
758 for (i = 0; i < 100; i++)
759 g_array_prepend_val (garray, i);
761 for (i = 0; i < 100; i++)
762 if (g_array_index (garray, gint, i) != (100 - i - 1))
763 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
765 g_array_free (garray, TRUE);
770 g_print ("checking strings...");
772 string1 = g_string_new ("hi pete!");
773 string2 = g_string_new ("");
775 g_assert (strcmp ("hi pete!", string1->str) == 0);
777 for (i = 0; i < 10000; i++)
778 g_string_append_c (string1, 'a'+(i%26));
781 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
782 the %10000.10000f format... */
783 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
784 "this pete guy sure is a wuss, like he's the number ",
786 " wuss. everyone agrees.\n",
788 10, 666, 15, 15, 666.666666666, 666.666666666);
790 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
791 "this pete guy sure is a wuss, like he's the number ",
793 " wuss. everyone agrees.\n",
795 10, 666, 15, 15, 666.666666666, 666.666666666);
798 g_print ("string2 length = %d...\n", string2->len);
799 string2->str[70] = '\0';
800 g_print ("first 70 chars:\n%s\n", string2->str);
801 string2->str[141] = '\0';
802 g_print ("next 70 chars:\n%s\n", string2->str+71);
803 string2->str[212] = '\0';
804 g_print ("and next 70:\n%s\n", string2->str+142);
805 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
807 g_string_free (string1, TRUE);
808 g_string_free (string2, TRUE);
811 string1 = g_string_new ("firsthalf");
812 g_string_append (string1, "lasthalf");
813 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
814 g_string_free (string1, TRUE);
818 string1 = g_string_new ("firsthalf");
819 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
820 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
821 g_string_free (string1, TRUE);
824 string1 = g_string_new ("lasthalf");
825 g_string_prepend (string1, "firsthalf");
826 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
827 g_string_free (string1, TRUE);
830 string1 = g_string_new ("lasthalf");
831 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
832 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
833 g_string_free (string1, TRUE);
836 string1 = g_string_new ("firstlast");
837 g_string_insert (string1, 5, "middle");
838 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
839 g_string_free (string1, TRUE);
841 /* insert with pos == end of the string */
842 string1 = g_string_new ("firstmiddle");
843 g_string_insert (string1, strlen ("firstmiddle"), "last");
844 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
845 g_string_free (string1, TRUE);
849 string1 = g_string_new ("firstlast");
850 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
851 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
852 g_string_free (string1, TRUE);
854 /* insert_len with magic -1 pos for append */
855 string1 = g_string_new ("first");
856 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
857 g_assert (strcmp (string1->str, "firstlast") == 0);
858 g_string_free (string1, TRUE);
860 /* insert_len with magic -1 len for strlen-the-string */
861 string1 = g_string_new ("first");
862 g_string_insert_len (string1, 5, "last", -1);
863 g_assert (strcmp (string1->str, "firstlast") == 0);
864 g_string_free (string1, TRUE);
869 string1 = g_string_new ("test");
870 string2 = g_string_new ("te");
871 g_assert (! g_string_equal(string1, string2));
872 g_string_append (string2, "st");
873 g_assert (g_string_equal(string1, string2));
874 g_string_free (string1, TRUE);
875 g_string_free (string2, TRUE);
877 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
878 string1 = g_string_new ("fiddle");
879 string2 = g_string_new ("fiddle");
880 g_assert (g_string_equal(string1, string2));
881 g_string_append_c(string1, '\0');
882 g_assert (! g_string_equal(string1, string2));
883 g_string_append_c(string2, '\0');
884 g_assert (g_string_equal(string1, string2));
885 g_string_append_c(string1, 'x');
886 g_string_append_c(string2, 'y');
887 g_assert (! g_string_equal(string1, string2));
888 g_assert (string1->len == 8);
889 g_string_append(string1, "yzzy");
890 g_assert (string1->len == 12);
891 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
892 g_string_insert(string1, 1, "QED");
893 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
894 g_string_free (string1, TRUE);
895 g_string_free (string2, TRUE);
897 g_print ("test positional printf formats (not supported): ");
898 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
899 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
900 g_print ("%s%s\n", string, tmp_string);
904 g_print ("checking timers...\n");
906 timer = g_timer_new ();
907 g_print (" spinning for 3 seconds...\n");
909 g_timer_start (timer);
910 while (g_timer_elapsed (timer, NULL) < 3)
913 g_timer_stop (timer);
914 g_timer_destroy (timer);
918 g_print ("checking g_strcasecmp...");
919 g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0);
920 g_assert (g_strcasecmp ("frobozz", "frobozz") == 0);
921 g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0);
922 g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0);
923 g_assert (g_strcasecmp ("", "") == 0);
924 g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0);
925 g_assert (g_strcasecmp ("a", "b") < 0);
926 g_assert (g_strcasecmp ("a", "B") < 0);
927 g_assert (g_strcasecmp ("A", "b") < 0);
928 g_assert (g_strcasecmp ("A", "B") < 0);
929 g_assert (g_strcasecmp ("b", "a") > 0);
930 g_assert (g_strcasecmp ("b", "A") > 0);
931 g_assert (g_strcasecmp ("B", "a") > 0);
932 g_assert (g_strcasecmp ("B", "A") > 0);
936 g_print ("checking g_strdup...");
937 g_assert(g_strdup(NULL) == NULL);
938 string = g_strdup(GLIB_TEST_STRING);
939 g_assert(string != NULL);
940 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
945 g_print ("checking g_strconcat...");
946 string = g_strconcat(GLIB_TEST_STRING, NULL);
947 g_assert(string != NULL);
948 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
950 string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
951 GLIB_TEST_STRING, NULL);
952 g_assert(string != NULL);
953 g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
954 GLIB_TEST_STRING) == 0);
959 g_print("checking g_strlcpy/g_strlcat...");
960 /* The following is a torture test for strlcpy/strlcat, with lots of
961 * checking; normal users wouldn't use them this way!
963 string = g_malloc (6);
964 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
966 g_assert (g_strlcpy(string, "" , 5) == 0);
967 g_assert ( *string == '\0' );
969 g_assert (g_strlcpy(string, "abc" , 5) == 3);
970 g_assert ( *(string + 3) == '\0' );
971 g_assert (g_str_equal(string, "abc"));
972 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
973 g_assert ( *(string + 4) == '\0' );
974 g_assert ( *(string + 5) == 'Z' );
975 g_assert (g_str_equal(string, "abcd"));
976 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
977 g_assert ( *(string + 4) == '\0' );
978 g_assert ( *(string + 5) == 'Z' );
979 g_assert (g_str_equal(string, "abcd"));
980 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
981 g_assert ( *(string + 4) == '\0' );
982 g_assert ( *(string + 5) == 'Z' );
983 g_assert (g_str_equal(string, "abcd"));
986 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
987 g_assert (*string == 'Y');
989 g_assert (g_strlcat(string, "123" , 5) == 3);
990 g_assert ( *(string + 3) == '\0' );
991 g_assert (g_str_equal(string, "123"));
992 g_assert (g_strlcat(string, "" , 5) == 3);
993 g_assert ( *(string + 3) == '\0' );
994 g_assert (g_str_equal(string, "123"));
995 g_assert (g_strlcat(string, "4", 5) == 4);
996 g_assert (g_str_equal(string, "1234"));
997 g_assert (g_strlcat(string, "5", 5) == 5);
998 g_assert ( *(string + 4) == '\0' );
999 g_assert (g_str_equal(string, "1234"));
1000 g_assert ( *(string + 5) == 'Z' );
1002 *(string + 1)= '\0';
1003 g_assert (g_strlcat(string, "123" , 0) == 3);
1004 g_assert (*string == 'Y');
1006 /* A few more tests, demonstrating more "normal" use */
1007 g_assert (g_strlcpy(string, "hi", 5) == 2);
1008 g_assert (g_str_equal(string, "hi"));
1009 g_assert (g_strlcat(string, "t", 5) == 3);
1010 g_assert (g_str_equal(string, "hit"));
1016 g_print ("checking g_strdup_printf...");
1017 string = g_strdup_printf ("%05d %-5s", 21, "test");
1018 g_assert (string != NULL);
1019 g_assert (strcmp(string, "00021 test ") == 0);
1024 /* g_debug (argv[0]); */
1026 /* Relation tests */
1028 g_print ("checking relations...");
1030 relation = g_relation_new (2);
1032 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1033 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1035 for (i = 0; i < 1024; i += 1)
1038 for (i = 1; i < 1023; i += 1)
1040 g_relation_insert (relation, data + i, data + i + 1);
1041 g_relation_insert (relation, data + i, data + i - 1);
1044 for (i = 2; i < 1022; i += 1)
1046 g_assert (! g_relation_exists (relation, data + i, data + i));
1047 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1048 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1051 for (i = 1; i < 1023; i += 1)
1053 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1054 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1057 for (i = 2; i < 1022; i += 1)
1059 g_assert (g_relation_count (relation, data + i, 0) == 2);
1060 g_assert (g_relation_count (relation, data + i, 1) == 2);
1063 g_assert (g_relation_count (relation, data, 0) == 0);
1065 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1066 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1067 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1068 g_relation_delete (relation, data + 42, 0);
1069 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1070 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1071 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1073 tuples = g_relation_select (relation, data + 200, 0);
1075 g_assert (tuples->len == 2);
1078 for (i = 0; i < tuples->len; i += 1)
1081 *(gint*) g_tuples_index (tuples, i, 0),
1082 *(gint*) g_tuples_index (tuples, i, 1));
1086 g_assert (g_relation_exists (relation, data + 300, data + 301));
1087 g_relation_delete (relation, data + 300, 0);
1088 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1090 g_tuples_destroy (tuples);
1092 g_relation_destroy (relation);
1098 g_print ("checking pointer arrays...");
1100 gparray = g_ptr_array_new ();
1101 for (i = 0; i < 10000; i++)
1102 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1104 for (i = 0; i < 10000; i++)
1105 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1106 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1108 g_ptr_array_free (gparray, TRUE);
1113 g_print ("checking byte arrays...");
1115 gbarray = g_byte_array_new ();
1116 for (i = 0; i < 10000; i++)
1117 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1119 for (i = 0; i < 10000; i++)
1121 g_assert (gbarray->data[4*i] == 'a');
1122 g_assert (gbarray->data[4*i+1] == 'b');
1123 g_assert (gbarray->data[4*i+2] == 'c');
1124 g_assert (gbarray->data[4*i+3] == 'd');
1127 g_byte_array_free (gbarray, TRUE);
1130 g_printerr ("g_log tests:");
1131 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1132 g_message ("the next warning is a test:");
1136 g_print ("checking endian macros (host is ");
1137 #if G_BYTE_ORDER == G_BIG_ENDIAN
1138 g_print ("big endian)...");
1140 g_print ("little endian)...");
1142 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
1143 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
1144 #ifdef G_HAVE_GINT64
1145 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
1151 g_print ("current locale: %s\n", g_win32_getlocale ());
1153 g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
1155 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
1156 g_print ("Ditto, or from GLib DLL name: %s\n",
1157 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
1158 g_print ("Ditto, only from GLib DLL name: %s\n",
1159 g_win32_get_package_installation_directory (NULL, glib_dll));
1160 g_print ("locale subdirectory of GLib installation directory: %s\n",
1161 g_win32_get_package_installation_subdirectory (NULL, glib_dll, "locale"));
1162 g_print ("GTK+ 2.0 installation directory, if available: %s\n",
1163 g_win32_get_package_installation_directory ("gtk20", NULL));
1166 g_print ("checking file functions...\n");
1168 strcpy (template, "foobar");
1169 fd = g_mkstemp (template);
1171 g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
1173 strcpy (template, "fooXXXXXX");
1174 fd = g_mkstemp (template);
1176 g_print ("g_mkstemp didn't work for template %s\n", template);
1177 i = write (fd, hello, hellolen);
1179 g_print ("write() failed: %s\n", g_strerror (errno));
1180 else if (i != hellolen)
1181 g_print ("write() should have written %d bytes, wrote %d\n", hellolen, i);
1184 i = read (fd, chars, sizeof (chars));
1186 g_print ("read() failed: %s\n", g_strerror (errno));
1187 else if (i != hellolen)
1188 g_print ("read() should have read %d bytes, got %d\n", hellolen, i);
1191 if (strcmp (chars, hello) != 0)
1192 g_print ("wrote '%s', but got '%s'\n", hello, chars);
1197 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1198 fd = g_file_open_tmp (template, &name_used, &error);
1200 g_print ("g_file_open_tmp works even if template contains '%s'\n",
1203 g_print ("g_file_open_tmp correctly returns error: %s\n",
1206 g_clear_error (&error);
1208 strcpy (template, "zapXXXXXX");
1209 fd = g_file_open_tmp (template, &name_used, &error);
1211 g_print ("g_file_open_tmp didn't work for template '%s': %s\n",
1212 template, error->message);
1214 g_print ("g_file_open_tmp for template '%s' used name '%s'\n",
1215 template, name_used);
1217 g_clear_error (&error);
1220 fd = g_file_open_tmp (NULL, &name_used, &error);
1222 g_print ("g_file_open_tmp didn't work for a NULL template: %s\n",
1225 g_print ("g_file_open_tmp for NULL template used name '%s'\n",
1228 g_clear_error (&error);