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/.
29 #undef G_DISABLE_ASSERT
32 #ifdef GLIB_COMPILATION
33 #undef GLIB_COMPILATION
48 #include <io.h> /* For read(), write() etc */
51 static int array[10000];
52 static gboolean failed = FALSE;
54 /* We write (m ? m : "") even in the m != NULL case to suppress a warning with GCC-3.1
56 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
59 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
61 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)(m ? m : "")); \
64 g_print ("."); fflush (stdout); \
67 #define C2P(c) ((gpointer) ((long) (c)))
68 #define P2C(p) ((gchar) ((long) (p)))
70 #define GLIB_TEST_STRING "el dorado "
71 #define GLIB_TEST_STRING_5 "el do"
74 node_build_string (GNode *node,
81 c[0] = P2C (node->data);
83 string = g_strconcat (*p ? *p : "", c, NULL);
100 gchar *tstring, *cstring;
102 g_print ("checking n-way trees: ");
105 root = g_node_new (C2P ('A'));
106 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
108 node_B = g_node_new (C2P ('B'));
109 g_node_append (root, node_B);
110 TEST (NULL, root->children == node_B);
112 g_node_append_data (node_B, C2P ('E'));
113 g_node_prepend_data (node_B, C2P ('C'));
114 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
116 node_F = g_node_new (C2P ('F'));
117 g_node_append (root, node_F);
118 TEST (NULL, root->children->next == node_F);
120 node_G = g_node_new (C2P ('G'));
121 g_node_append (node_F, node_G);
122 node_J = g_node_new (C2P ('J'));
123 g_node_prepend (node_G, node_J);
124 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
125 g_node_insert_data (node_G, 0, C2P ('H'));
126 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
128 TEST (NULL, g_node_depth (root) == 1);
129 TEST (NULL, g_node_max_height (root) == 4);
130 TEST (NULL, g_node_depth (node_G->children->next) == 4);
131 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
132 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
133 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
134 TEST (NULL, g_node_max_height (node_F) == 3);
135 TEST (NULL, g_node_n_children (node_G) == 4);
136 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
137 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
138 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
140 for (i = 0; i < g_node_n_children (node_B); i++)
142 node = g_node_nth_child (node_B, i);
143 TEST (NULL, P2C (node->data) == ('C' + i));
146 for (i = 0; i < g_node_n_children (node_G); i++)
147 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
157 * for in-order traversal, 'G' is considered to be the "left"
158 * child of 'F', which will cause 'F' to be the last node visited.
162 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
163 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
164 g_free (tstring); tstring = NULL;
165 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
166 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
167 g_free (tstring); tstring = NULL;
168 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
169 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
170 g_free (tstring); tstring = NULL;
171 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
172 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
173 g_free (tstring); tstring = NULL;
175 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
176 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
177 g_free (tstring); tstring = NULL;
178 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
179 TEST (tstring, strcmp (tstring, "ABFG") == 0);
180 g_free (tstring); tstring = NULL;
182 g_node_reverse_children (node_B);
183 g_node_reverse_children (node_G);
185 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
186 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
187 g_free (tstring); tstring = NULL;
190 node = g_node_copy (root);
191 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
192 TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
193 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
194 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
195 TEST (cstring, strcmp (tstring, cstring) == 0);
196 g_free (tstring); tstring = NULL;
197 g_free (cstring); cstring = NULL;
198 g_node_destroy (node);
200 g_node_destroy (root);
202 /* allocation tests */
204 root = g_node_new (NULL);
207 for (i = 0; i < 2048; i++)
209 g_node_append (node, g_node_new (NULL));
211 node = node->children->next;
213 TEST (NULL, g_node_max_height (root) > 100);
214 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
216 g_node_destroy (root);
223 my_hash_callback_remove (gpointer key,
236 my_hash_callback_remove_test (gpointer key,
247 my_hash_callback (gpointer key,
256 my_hash (gconstpointer key)
258 return (guint) *((const gint*) key);
262 my_hash_equal (gconstpointer a,
265 return *((const gint*) a) == *((const gint*) b);
269 my_list_compare_one (gconstpointer a, gconstpointer b)
271 gint one = *((const gint*)a);
272 gint two = *((const gint*)b);
277 my_list_compare_two (gconstpointer a, gconstpointer b)
279 gint one = *((const gint*)a);
280 gint two = *((const gint*)b);
285 my_list_print (gpointer a, gpointer b)
287 gint three = *((gint*)a);
288 g_print("%d", three);
292 my_compare (gconstpointer a,
302 my_traverse (gpointer key,
307 g_print ("%c ", *ch);
312 find_first_that(gpointer key,
317 gint *test = user_data;
318 return (*v == *test);
323 test_g_mkdir_with_parents_1 (const gchar *base)
325 char *p0 = g_build_filename (base, "fum", NULL);
326 char *p1 = g_build_filename (p0, "tem", NULL);
327 char *p2 = g_build_filename (p1, "zap", NULL);
334 if (g_file_test (p0, G_FILE_TEST_EXISTS))
336 g_print ("failed, %s exists, cannot test g_mkdir_with_parents\n", p0);
340 if (g_file_test (p1, G_FILE_TEST_EXISTS))
342 g_print ("failed, %s exists, cannot test g_mkdir_with_parents\n", p1);
346 if (g_file_test (p2, G_FILE_TEST_EXISTS))
348 g_print ("failed, %s exists, cannot test g_mkdir_with_parents\n", p2);
352 if (g_mkdir_with_parents (p2, 0666) == -1)
354 g_print ("failed, g_mkdir_with_parents(%s) failed: %s\n", p2, g_strerror (errno));
358 if (!g_file_test (p2, G_FILE_TEST_IS_DIR))
360 g_print ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p2);
364 if (!g_file_test (p1, G_FILE_TEST_IS_DIR))
366 g_print ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p1);
370 if (!g_file_test (p0, G_FILE_TEST_IS_DIR))
372 g_print ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p0);
377 if (g_file_test (p2, G_FILE_TEST_EXISTS))
379 g_print ("failed, did g_rmdir(%s), but %s is still there\n", p2, p2);
384 if (g_file_test (p1, G_FILE_TEST_EXISTS))
386 g_print ("failed, did g_rmdir(%s), but %s is still there\n", p1, p1);
390 f = g_fopen (p1, "w");
393 g_print ("failed, couldn't create file %s\n", p1);
398 if (g_mkdir_with_parents (p1, 0666) == 0)
400 g_print ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p1, p1);
404 if (g_mkdir_with_parents (p2, 0666) == 0)
406 g_print ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p2, p1);
418 test_g_mkdir_with_parents (void)
420 g_print ("checking g_mkdir_with_parents()...");
421 if (!test_g_mkdir_with_parents_1 ("hum"))
424 if (!test_g_mkdir_with_parents_1 ("hii///haa/hee"))
426 g_remove ("hii/haa/hee");
427 g_remove ("hii/haa");
429 if (!test_g_mkdir_with_parents_1 (g_get_current_dir ()))
436 test_g_parse_debug_string (void)
438 GDebugKey keys[3] = {
446 result = g_parse_debug_string ("bar:foo:blubb", keys, n_keys);
447 g_assert (result == 3);
449 result = g_parse_debug_string (":baz::_E@~!_::", keys, n_keys);
450 g_assert (result == 4);
452 result = g_parse_debug_string ("", keys, n_keys);
453 g_assert (result == 0);
455 result = g_parse_debug_string (" : ", keys, n_keys);
456 g_assert (result == 0);
468 GHashTable *hash_table;
469 GMemChunk *mem_chunk;
470 GStringChunk *string_chunk;
471 GTimer *timer, *timer2;
473 gulong elapsed_usecs;
474 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
475 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
479 GTimeVal ref_date, date;
482 gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
487 GString *string1, *string2;
488 const gchar *charset;
497 } dirname_checks[] = {
508 { ".\\\\\\\\", "." },
510 { "..\\\\\\\\", ".." },
518 { "//server/share///x", "//server/share" },
524 guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
529 } skip_root_checks[] = {
538 { "\\\\server\\foo", "" },
539 { "\\\\server\\foo\\bar", "bar" },
543 { "//server/share///x", "//x" },
548 guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
550 #ifndef G_DISABLE_ASSERT
551 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
552 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
553 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
554 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
556 const char hello[] = "Hello, World";
557 const int hellolen = sizeof (hello) - 1;
563 /* Can't calculate GLib DLL name at runtime. */
564 gchar *glib_dll = "libglib-2.0-0.dll";
567 gchar *glib_dll = "cygglib-2.0-0.dll";
570 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
577 string = g_get_current_dir ();
578 g_print ("cwd: %s\n", string);
580 g_print ("user: %s\n", g_get_user_name ());
581 g_print ("real: %s\n", g_get_real_name ());
582 g_print ("host: %s\n", g_get_host_name ());
583 s = g_get_home_dir ();
584 g_print ("home: %s\n", s ? s : "NULL!");
585 s = g_get_user_data_dir ();
586 g_print ("user_data: %s\n", s ? s : "NULL!");
587 s = g_get_user_config_dir ();
588 g_print ("user_config: %s\n", s ? s : "NULL!");
589 s = g_get_user_cache_dir ();
590 g_print ("user_cache: %s\n", s ? s : "NULL!");
591 sv = (gchar **) g_get_system_data_dirs ();
592 g_print ("system_data: %s\n", sv ? g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv) : "NULL!");
593 sv = (gchar **) g_get_system_config_dirs ();
594 g_print ("system_config: %s\n", s ? g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv) : "NULL!");
595 g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
596 sv = (gchar **) g_get_language_names ();
597 g_print ("languages: %s\n", s ? g_strjoinv (":", sv) : "NULL!");
600 s = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
601 g_print ("user_special[DESKTOP]: %s\n", s ? s : "NULL!");
602 s = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
603 g_print ("user_special[DOCUMENTS]: %s\n", s ? s : "NULL!");
604 s = g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE);
605 g_print ("user_special[PUBLIC_SHARE]: %s\n", s ? s : "NULL!");
608 g_print ("checking size of gint8: %" G_GSIZE_FORMAT, sizeof (gint8));
609 TEST (NULL, sizeof (gint8) == 1);
610 g_print ("\nchecking size of gint16: %" G_GSIZE_FORMAT, sizeof (gint16));
611 TEST (NULL, sizeof (gint16) == 2);
612 g_print ("\nchecking size of gint32: %" G_GSIZE_FORMAT, sizeof (gint32));
613 TEST (NULL, sizeof (gint32) == 4);
614 g_print ("\nchecking size of gsize: %" G_GSIZE_FORMAT, sizeof (gsize));
615 g_print ("\nchecking size of gint64: %" G_GSIZE_FORMAT, sizeof (gint64));
616 TEST (NULL, sizeof (gint64) == 8);
619 g_print ("checking g_path_get_basename()...");
620 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
621 g_assert (strcmp (string, "dir") == 0);
623 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
624 g_assert (strcmp (string, "file") == 0);
628 string = g_path_get_basename ("/foo/dir/");
629 g_assert (strcmp (string, "dir") == 0);
631 string = g_path_get_basename ("/foo/file");
632 g_assert (strcmp (string, "file") == 0);
636 g_print ("checking g_path_get_dirname()...");
637 for (i = 0; i < n_dirname_checks; i++)
641 dirname = g_path_get_dirname (dirname_checks[i].filename);
642 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
644 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
645 dirname_checks[i].filename,
646 dirname_checks[i].dirname,
648 n_dirname_checks = 0;
652 if (n_dirname_checks)
655 g_print ("checking g_path_skip_root()...");
656 for (i = 0; i < n_skip_root_checks; i++)
658 const gchar *skipped;
660 skipped = g_path_skip_root (skip_root_checks[i].filename);
661 if ((skipped && !skip_root_checks[i].without_root) ||
662 (!skipped && skip_root_checks[i].without_root) ||
663 ((skipped && skip_root_checks[i].without_root) &&
664 strcmp (skipped, skip_root_checks[i].without_root)))
666 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
667 skip_root_checks[i].filename,
668 (skip_root_checks[i].without_root ?
669 skip_root_checks[i].without_root : "<NULL>"),
670 (skipped ? skipped : "<NULL>"));
671 n_skip_root_checks = 0;
674 if (n_skip_root_checks)
677 if (test_g_mkdir_with_parents ())
680 g_print ("checking doubly linked lists...");
683 for (i = 0; i < 10; i++)
684 list = g_list_append (list, &nums[i]);
685 list = g_list_reverse (list);
687 for (i = 0; i < 10; i++)
689 t = g_list_nth (list, i);
690 if (*((gint*) t->data) != (9 - i))
691 g_error ("Regular insert failed");
694 for (i = 0; i < 10; i++)
695 if(g_list_position(list, g_list_nth (list, i)) != i)
696 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
701 for (i = 0; i < 10; i++)
702 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
706 g_list_foreach (list, my_list_print, NULL);
709 for (i = 0; i < 10; i++)
711 t = g_list_nth (list, i);
712 if (*((gint*) t->data) != i)
713 g_error ("Sorted insert failed");
719 for (i = 0; i < 10; i++)
720 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
724 g_list_foreach (list, my_list_print, NULL);
727 for (i = 0; i < 10; i++)
729 t = g_list_nth (list, i);
730 if (*((gint*) t->data) != (9 - i))
731 g_error ("Sorted insert failed");
737 for (i = 0; i < 10; i++)
738 list = g_list_prepend (list, &morenums[i]);
740 list = g_list_sort (list, my_list_compare_two);
744 g_list_foreach (list, my_list_print, NULL);
747 for (i = 0; i < 10; i++)
749 t = g_list_nth (list, i);
750 if (*((gint*) t->data) != (9 - i))
751 g_error ("Merge sort failed");
759 g_print ("checking singly linked lists...");
762 for (i = 0; i < 10; i++)
763 slist = g_slist_append (slist, &nums[i]);
764 slist = g_slist_reverse (slist);
766 for (i = 0; i < 10; i++)
768 st = g_slist_nth (slist, i);
769 if (*((gint*) st->data) != (9 - i))
773 g_slist_free (slist);
776 for (i = 0; i < 10; i++)
777 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
781 g_slist_foreach (slist, my_list_print, NULL);
784 for (i = 0; i < 10; i++)
786 st = g_slist_nth (slist, i);
787 if (*((gint*) st->data) != i)
788 g_error ("Sorted insert failed");
794 for (i = 0; i < 10; i++)
795 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
799 g_slist_foreach (slist, my_list_print, NULL);
802 for (i = 0; i < 10; i++)
804 st = g_slist_nth (slist, i);
805 if (*((gint*) st->data) != (9 - i))
806 g_error("Sorted insert failed");
812 for (i = 0; i < 10; i++)
813 slist = g_slist_prepend (slist, &morenums[i]);
815 slist = g_slist_sort (slist, my_list_compare_two);
819 g_slist_foreach (slist, my_list_print, NULL);
822 for (i = 0; i < 10; i++)
824 st = g_slist_nth (slist, i);
825 if (*((gint*) st->data) != (9 - i))
826 g_error("Sorted insert failed");
834 g_print ("checking binary trees...\n");
836 tree = g_tree_new (my_compare);
838 for (j = 0; j < 10; j++, i++)
841 g_tree_insert (tree, &chars[i], &chars[i]);
843 for (j = 0; j < 26; j++, i++)
846 g_tree_insert (tree, &chars[i], &chars[i]);
848 for (j = 0; j < 26; j++, i++)
851 g_tree_insert (tree, &chars[i], &chars[i]);
854 g_print ("tree height: %d\n", g_tree_height (tree));
855 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
858 g_tree_foreach (tree, my_traverse, NULL);
861 for (i = 0; i < 10; i++)
862 g_tree_remove (tree, &chars[i]);
864 g_print ("tree height: %d\n", g_tree_height (tree));
865 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
868 g_tree_foreach (tree, my_traverse, NULL);
874 /* check n-way trees */
877 g_print ("checking mem chunks...");
879 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
881 for (i = 0; i < 10000; i++)
883 mem[i] = g_chunk_new (gchar, mem_chunk);
885 for (j = 0; j < 50; j++)
889 for (i = 0; i < 10000; i++)
891 g_mem_chunk_free (mem_chunk, mem[i]);
897 g_print ("checking hash tables...");
899 hash_table = g_hash_table_new (my_hash, my_hash_equal);
900 for (i = 0; i < 10000; i++)
903 g_hash_table_insert (hash_table, &array[i], &array[i]);
905 pvalue = g_hash_table_find (hash_table, find_first_that, &value);
906 if (*pvalue != value)
907 g_print("g_hash_table_find failed");
909 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
911 for (i = 0; i < 10000; i++)
915 for (i = 0; i < 10000; i++)
916 g_hash_table_remove (hash_table, &array[i]);
918 for (i = 0; i < 10000; i++)
921 g_hash_table_insert (hash_table, &array[i], &array[i]);
924 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
925 g_hash_table_size (hash_table) != 5000)
928 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
931 g_hash_table_destroy (hash_table);
936 g_print ("checking string chunks...");
938 string_chunk = g_string_chunk_new (1024);
940 for (i = 0; i < 100000; i ++)
942 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
944 if (strcmp ("hi pete", tmp_string) != 0)
945 g_error ("string chunks are broken.\n");
948 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
950 g_assert (tmp_string_2 != tmp_string &&
951 strcmp(tmp_string_2, tmp_string) == 0);
953 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
955 g_assert (tmp_string_2 == tmp_string);
957 g_string_chunk_free (string_chunk);
962 g_print ("checking arrays...");
964 garray = g_array_new (FALSE, FALSE, sizeof (gint));
965 for (i = 0; i < 10000; i++)
966 g_array_append_val (garray, i);
968 for (i = 0; i < 10000; i++)
969 if (g_array_index (garray, gint, i) != i)
970 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
972 g_array_free (garray, TRUE);
974 garray = g_array_new (FALSE, FALSE, sizeof (gint));
975 for (i = 0; i < 100; i++)
976 g_array_prepend_val (garray, i);
978 for (i = 0; i < 100; i++)
979 if (g_array_index (garray, gint, i) != (100 - i - 1))
980 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
982 g_array_free (garray, TRUE);
987 g_print ("checking strings...");
989 string1 = g_string_new ("hi pete!");
990 string2 = g_string_new ("");
992 g_assert (strcmp ("hi pete!", string1->str) == 0);
994 for (i = 0; i < 10000; i++)
995 g_string_append_c (string1, 'a'+(i%26));
998 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
999 the %10000.10000f format... */
1000 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
1001 "this pete guy sure is a wuss, like he's the number ",
1003 " wuss. everyone agrees.\n",
1005 10, 666, 15, 15, 666.666666666, 666.666666666);
1007 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
1008 "this pete guy sure is a wuss, like he's the number ",
1010 " wuss. everyone agrees.\n",
1012 10, 666, 15, 15, 666.666666666, 666.666666666);
1015 g_print ("string2 length = %lu...\n", (gulong)string2->len);
1016 string2->str[70] = '\0';
1017 g_print ("first 70 chars:\n%s\n", string2->str);
1018 string2->str[141] = '\0';
1019 g_print ("next 70 chars:\n%s\n", string2->str+71);
1020 string2->str[212] = '\0';
1021 g_print ("and next 70:\n%s\n", string2->str+142);
1022 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
1024 g_string_free (string1, TRUE);
1025 g_string_free (string2, TRUE);
1028 string1 = g_string_new ("firsthalf");
1029 g_string_append (string1, "lasthalf");
1030 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1031 g_string_free (string1, TRUE);
1035 string1 = g_string_new ("firsthalf");
1036 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
1037 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1038 g_string_free (string1, TRUE);
1041 string1 = g_string_new ("lasthalf");
1042 g_string_prepend (string1, "firsthalf");
1043 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1044 g_string_free (string1, TRUE);
1047 string1 = g_string_new ("lasthalf");
1048 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
1049 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1050 g_string_free (string1, TRUE);
1053 string1 = g_string_new ("firstlast");
1054 g_string_insert (string1, 5, "middle");
1055 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1056 g_string_free (string1, TRUE);
1058 /* insert with pos == end of the string */
1059 string1 = g_string_new ("firstmiddle");
1060 g_string_insert (string1, strlen ("firstmiddle"), "last");
1061 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1062 g_string_free (string1, TRUE);
1066 string1 = g_string_new ("firstlast");
1067 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
1068 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1069 g_string_free (string1, TRUE);
1071 /* insert_len with magic -1 pos for append */
1072 string1 = g_string_new ("first");
1073 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
1074 g_assert (strcmp (string1->str, "firstlast") == 0);
1075 g_string_free (string1, TRUE);
1077 /* insert_len with magic -1 len for strlen-the-string */
1078 string1 = g_string_new ("first");
1079 g_string_insert_len (string1, 5, "last", -1);
1080 g_assert (strcmp (string1->str, "firstlast") == 0);
1081 g_string_free (string1, TRUE);
1085 /* g_string_equal */
1086 string1 = g_string_new ("test");
1087 string2 = g_string_new ("te");
1088 g_assert (! g_string_equal(string1, string2));
1089 g_string_append (string2, "st");
1090 g_assert (g_string_equal(string1, string2));
1091 g_string_free (string1, TRUE);
1092 g_string_free (string2, TRUE);
1094 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
1095 string1 = g_string_new ("fiddle");
1096 string2 = g_string_new ("fiddle");
1097 g_assert (g_string_equal(string1, string2));
1098 g_string_append_c(string1, '\0');
1099 g_assert (! g_string_equal(string1, string2));
1100 g_string_append_c(string2, '\0');
1101 g_assert (g_string_equal(string1, string2));
1102 g_string_append_c(string1, 'x');
1103 g_string_append_c(string2, 'y');
1104 g_assert (! g_string_equal(string1, string2));
1105 g_assert (string1->len == 8);
1106 g_string_append(string1, "yzzy");
1107 g_assert (string1->len == 12);
1108 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
1109 g_string_insert(string1, 1, "QED");
1110 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
1111 g_string_free (string1, TRUE);
1112 g_string_free (string2, TRUE);
1114 g_print ("test positional printf formats (not supported): ");
1115 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
1116 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
1117 g_print ("%s%s\n", string, tmp_string);
1118 g_free (tmp_string);
1121 g_print ("checking timers...\n");
1123 timer = g_timer_new ();
1124 g_print (" spinning for 3 seconds...\n");
1126 g_timer_start (timer);
1127 while (g_timer_elapsed (timer, NULL) < 3)
1130 g_timer_stop (timer);
1131 g_timer_destroy (timer);
1135 g_print ("checking g_timer_continue...\n");
1137 timer2 = g_timer_new ();
1139 g_print ("\trun for 1 second...\n");
1140 timer = g_timer_new();
1141 g_usleep(G_USEC_PER_SEC); /* run timer for 1 second */
1142 g_timer_stop(timer);
1144 g_print ("\tstop for 1 second...\n");
1145 g_usleep(G_USEC_PER_SEC); /* wait for 1 second */
1146 g_print ("\trun for 2 seconds...\n");
1148 g_timer_continue(timer);
1149 g_usleep(2*G_USEC_PER_SEC); /* run timer for 2 seconds */
1150 g_timer_stop(timer);
1152 g_print ("\tstop for 1.5 seconds...\n");
1153 g_usleep((3*G_USEC_PER_SEC)/2); /* wait for 1.5 seconds */
1154 g_print ("\trun for 0.2 seconds...\n");
1156 g_timer_continue(timer);
1157 g_usleep(G_USEC_PER_SEC/5); /* run timer for 0.2 seconds */
1158 g_timer_stop(timer);
1160 g_print ("\tstop for 4 seconds...\n");
1161 g_usleep(4*G_USEC_PER_SEC); /* wait for 4 seconds */
1162 g_print ("\trun for 5.8 seconds...\n");
1164 g_timer_continue(timer);
1165 g_usleep((29*G_USEC_PER_SEC)/5); /* run timer for 5.8 seconds */
1166 g_timer_stop(timer);
1168 elapsed = g_timer_elapsed (timer, &elapsed_usecs);
1169 g_print ("\t=> timer = %.6f = %d.%06ld (should be: 9.000000) (%.6f off)\n", elapsed, (int) elapsed, elapsed_usecs, ABS (elapsed - 9.));
1171 if (elapsed > 8.8 && elapsed < 9.2)
1172 g_print ("g_timer_continue ... ok\n\n");
1174 g_print ("g_timer_continue ... ***** FAILED *****\n\n");
1176 g_timer_stop(timer2);
1178 elapsed = g_timer_elapsed(timer2, &elapsed_usecs);
1179 g_print ("\t=> timer2 = %.6f = %d.%06ld (should be: %.6f) (%.6f off)\n\n", elapsed, (int) elapsed, elapsed_usecs, 9.+6.5, ABS (elapsed - (9.+6.5)));
1181 if (elapsed > (8.8+6.5) && elapsed < (9.2+6.5))
1182 g_print ("timer2 ... ok\n\n");
1184 g_print ("timer2 ... ***** FAILED *****\n\n");
1186 g_timer_destroy(timer);
1187 g_timer_destroy(timer2);
1189 #define REF_SEC_UTC 320063760
1190 #define REF_STR_UTC "1980-02-22T10:36:00Z"
1191 #define REF_STR_CEST "1980-02-22T12:36:00+02:00"
1192 #define REF_STR_EST "1980-02-22T05:36:00-05:00"
1194 g_print ("checking g_time_val_from_iso8601...\n");
1195 ref_date.tv_sec = REF_SEC_UTC;
1196 ref_date.tv_usec = 0;
1197 g_assert (g_time_val_from_iso8601 (REF_STR_UTC, &date) != FALSE);
1198 g_print ("\t=> UTC stamp = %ld (should be: %ld) (%ld off)\n", date.tv_sec, ref_date.tv_sec, date.tv_sec - ref_date.tv_sec);
1199 g_assert (date.tv_sec == ref_date.tv_sec);
1201 g_assert (g_time_val_from_iso8601 (REF_STR_CEST, &date) != FALSE);
1202 g_print ("\t=> CEST stamp = %ld (should be: %ld) (%ld off)\n", date.tv_sec, ref_date.tv_sec, date.tv_sec - ref_date.tv_sec);
1203 g_assert (date.tv_sec == ref_date.tv_sec);
1205 g_assert (g_time_val_from_iso8601 (REF_STR_EST, &date) != FALSE);
1206 g_print ("\t=> EST stamp = %ld (should be: %ld) (%ld off)\n", date.tv_sec, ref_date.tv_sec, date.tv_sec - ref_date.tv_sec);
1207 g_assert (date.tv_sec == ref_date.tv_sec);
1210 g_print ("checking g_time_val_to_iso8601...\n");
1211 ref_date.tv_sec = REF_SEC_UTC;
1212 ref_date.tv_usec = 1;
1213 date_str = g_time_val_to_iso8601 (&ref_date);
1214 g_assert (date_str != NULL);
1215 g_print ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_UTC);
1216 g_assert (strcmp (date_str, REF_STR_UTC) == 0);
1220 g_print ("checking g_ascii_strcasecmp...");
1221 g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
1222 g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
1223 g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
1224 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1225 g_assert (g_ascii_strcasecmp ("", "") == 0);
1226 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
1227 g_assert (g_ascii_strcasecmp ("a", "b") < 0);
1228 g_assert (g_ascii_strcasecmp ("a", "B") < 0);
1229 g_assert (g_ascii_strcasecmp ("A", "b") < 0);
1230 g_assert (g_ascii_strcasecmp ("A", "B") < 0);
1231 g_assert (g_ascii_strcasecmp ("b", "a") > 0);
1232 g_assert (g_ascii_strcasecmp ("b", "A") > 0);
1233 g_assert (g_ascii_strcasecmp ("B", "a") > 0);
1234 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1238 g_print ("checking g_strdup...");
1239 g_assert(g_strdup(NULL) == NULL);
1240 string = g_strdup(GLIB_TEST_STRING);
1241 g_assert(string != NULL);
1242 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
1247 g_print ("checking g_strconcat...");
1248 string = g_strconcat(GLIB_TEST_STRING, NULL);
1249 g_assert(string != NULL);
1250 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
1252 string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
1253 GLIB_TEST_STRING, NULL);
1254 g_assert(string != NULL);
1255 g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
1256 GLIB_TEST_STRING) == 0);
1261 g_print("checking g_strlcpy/g_strlcat...");
1262 /* The following is a torture test for strlcpy/strlcat, with lots of
1263 * checking; normal users wouldn't use them this way!
1265 string = g_malloc (6);
1266 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
1268 g_assert (g_strlcpy(string, "" , 5) == 0);
1269 g_assert ( *string == '\0' );
1271 g_assert (g_strlcpy(string, "abc" , 5) == 3);
1272 g_assert ( *(string + 3) == '\0' );
1273 g_assert (g_str_equal(string, "abc"));
1274 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1275 g_assert ( *(string + 4) == '\0' );
1276 g_assert ( *(string + 5) == 'Z' );
1277 g_assert (g_str_equal(string, "abcd"));
1278 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1279 g_assert ( *(string + 4) == '\0' );
1280 g_assert ( *(string + 5) == 'Z' );
1281 g_assert (g_str_equal(string, "abcd"));
1282 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1283 g_assert ( *(string + 4) == '\0' );
1284 g_assert ( *(string + 5) == 'Z' );
1285 g_assert (g_str_equal(string, "abcd"));
1287 *(string + 1)= '\0';
1288 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1289 g_assert (*string == 'Y');
1291 g_assert (g_strlcat(string, "123" , 5) == 3);
1292 g_assert ( *(string + 3) == '\0' );
1293 g_assert (g_str_equal(string, "123"));
1294 g_assert (g_strlcat(string, "" , 5) == 3);
1295 g_assert ( *(string + 3) == '\0' );
1296 g_assert (g_str_equal(string, "123"));
1297 g_assert (g_strlcat(string, "4", 5) == 4);
1298 g_assert (g_str_equal(string, "1234"));
1299 g_assert (g_strlcat(string, "5", 5) == 5);
1300 g_assert ( *(string + 4) == '\0' );
1301 g_assert (g_str_equal(string, "1234"));
1302 g_assert ( *(string + 5) == 'Z' );
1304 *(string + 1)= '\0';
1305 g_assert (g_strlcat(string, "123" , 0) == 3);
1306 g_assert (*string == 'Y');
1308 /* A few more tests, demonstrating more "normal" use */
1309 g_assert (g_strlcpy(string, "hi", 5) == 2);
1310 g_assert (g_str_equal(string, "hi"));
1311 g_assert (g_strlcat(string, "t", 5) == 3);
1312 g_assert (g_str_equal(string, "hit"));
1318 g_print ("checking g_strdup_printf...");
1319 string = g_strdup_printf ("%05d %-5s", 21, "test");
1320 g_assert (string != NULL);
1321 g_assert (strcmp(string, "00021 test ") == 0);
1326 /* g_debug (argv[0]); */
1328 /* Relation tests */
1330 g_print ("checking relations...");
1332 relation = g_relation_new (2);
1334 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1335 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1337 for (i = 0; i < 1024; i += 1)
1340 for (i = 1; i < 1023; i += 1)
1342 g_relation_insert (relation, data + i, data + i + 1);
1343 g_relation_insert (relation, data + i, data + i - 1);
1346 for (i = 2; i < 1022; i += 1)
1348 g_assert (! g_relation_exists (relation, data + i, data + i));
1349 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1350 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1353 for (i = 1; i < 1023; i += 1)
1355 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1356 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1359 for (i = 2; i < 1022; i += 1)
1361 g_assert (g_relation_count (relation, data + i, 0) == 2);
1362 g_assert (g_relation_count (relation, data + i, 1) == 2);
1365 g_assert (g_relation_count (relation, data, 0) == 0);
1367 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1368 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1369 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1370 g_relation_delete (relation, data + 42, 0);
1371 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1372 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1373 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1375 tuples = g_relation_select (relation, data + 200, 0);
1377 g_assert (tuples->len == 2);
1380 for (i = 0; i < tuples->len; i += 1)
1383 *(gint*) g_tuples_index (tuples, i, 0),
1384 *(gint*) g_tuples_index (tuples, i, 1));
1388 g_assert (g_relation_exists (relation, data + 300, data + 301));
1389 g_relation_delete (relation, data + 300, 0);
1390 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1392 g_tuples_destroy (tuples);
1394 g_relation_destroy (relation);
1400 g_print ("checking pointer arrays...");
1402 gparray = g_ptr_array_new ();
1403 for (i = 0; i < 10000; i++)
1404 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1406 for (i = 0; i < 10000; i++)
1407 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1408 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1410 g_ptr_array_free (gparray, TRUE);
1415 g_print ("checking byte arrays...");
1417 gbarray = g_byte_array_new ();
1418 for (i = 0; i < 10000; i++)
1419 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1421 for (i = 0; i < 10000; i++)
1423 g_assert (gbarray->data[4*i] == 'a');
1424 g_assert (gbarray->data[4*i+1] == 'b');
1425 g_assert (gbarray->data[4*i+2] == 'c');
1426 g_assert (gbarray->data[4*i+3] == 'd');
1429 g_byte_array_free (gbarray, TRUE);
1432 g_printerr ("g_log tests:");
1433 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1434 g_message ("the next warning is a test:");
1437 g_message ("non-printable UTF-8: \"\xc3\xa4\xda\x85\"");
1438 g_message ("unsafe chars: \"\x10\x11\x12\n\t\x7f\x81\x82\x83\"");
1440 g_print ("checking endian macros (host is ");
1441 #if G_BYTE_ORDER == G_BIG_ENDIAN
1442 g_print ("big endian)...");
1444 g_print ("little endian)...");
1446 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
1447 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
1448 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
1452 if (g_get_charset ((G_CONST_RETURN char**)&charset))
1453 g_print ("current charset is UTF-8: %s\n", charset);
1455 g_print ("current charset is not UTF-8: %s\n", charset);
1457 #ifdef G_PLATFORM_WIN32
1458 g_print ("current locale: %s\n", g_win32_getlocale ());
1459 g_print ("GLib DLL name tested for: %s\n", glib_dll);
1461 g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
1463 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
1464 g_print ("Ditto, or from GLib DLL name: %s\n",
1465 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
1466 g_print ("Ditto, only from GLib DLL name: %s\n",
1467 g_win32_get_package_installation_directory (NULL, glib_dll));
1468 g_print ("locale subdirectory of GLib installation directory: %s\n",
1469 g_win32_get_package_installation_subdirectory (NULL, glib_dll, "lib\\locale"));
1470 g_print ("GTK+ 2.0 installation directory, if available: %s\n",
1471 g_win32_get_package_installation_directory ("gtk20", NULL));
1473 g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
1474 g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
1476 g_print ("a Win32 error message: %s\n", g_win32_error_message (2));
1480 g_print ("checking file functions...\n");
1482 strcpy (template, "foobar");
1483 fd = g_mkstemp (template);
1485 g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
1487 strcpy (template, "fooXXXXXX");
1488 fd = g_mkstemp (template);
1490 g_print ("g_mkstemp didn't work for template %s\n", template);
1491 i = write (fd, hello, hellolen);
1493 g_print ("write() failed: %s\n", g_strerror (errno));
1494 else if (i != hellolen)
1495 g_print ("write() should have written %d bytes, wrote %d\n", hellolen, i);
1498 i = read (fd, chars, sizeof (chars));
1500 g_print ("read() failed: %s\n", g_strerror (errno));
1501 else if (i != hellolen)
1502 g_print ("read() should have read %d bytes, got %d\n", hellolen, i);
1505 if (strcmp (chars, hello) != 0)
1506 g_print ("wrote '%s', but got '%s'\n", hello, chars);
1512 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1513 fd = g_file_open_tmp (template, &name_used, &error);
1515 g_print ("g_file_open_tmp works even if template contains '%s'\n",
1518 g_print ("g_file_open_tmp correctly returns error: %s\n",
1521 g_clear_error (&error);
1524 strcpy (template, "zap/barXXXXXX");
1525 fd = g_file_open_tmp (template, &name_used, &error);
1527 g_print ("g_file_open_tmp works even if template contains '/'\n");
1529 g_print ("g_file_open_tmp correctly returns error: %s\n",
1532 g_clear_error (&error);
1535 strcpy (template, "zapXXXXXX");
1536 fd = g_file_open_tmp (template, &name_used, &error);
1538 g_print ("g_file_open_tmp didn't work for template '%s': %s\n",
1539 template, error->message);
1541 g_print ("g_file_open_tmp for template '%s' used name '%s'\n",
1542 template, name_used);
1544 g_clear_error (&error);
1547 fd = g_file_open_tmp (NULL, &name_used, &error);
1549 g_print ("g_file_open_tmp didn't work for a NULL template: %s\n",
1552 g_print ("g_file_open_tmp for NULL template used name '%s'\n",
1555 g_clear_error (&error);
1558 test_g_parse_debug_string ();