Update contrib.
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
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
47 #include <glib_global.h>
48 #include "mrt2_glib2_test.h"
57 #include <io.h> /* For read(), write() etc */
62 static int array[10000];
63 static gboolean failed = FALSE;
65 /* We write (m ? m : "") even in the m != NULL case to suppress a warning with GCC-3.1
67 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
69 { assert_failed = TRUE; \
71 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
73 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)(m ? m : "")); \
77 #define C2P(c) ((gpointer) ((long) (c)))
78 #define P2C(p) ((gchar) ((long) (p)))
80 #define GLIB_TEST_STRING "el dorado "
81 #define GLIB_TEST_STRING_5 "el do"
84 node_build_string (GNode *node,
91 c[0] = P2C (node->data);
93 string = g_strconcat (*p ? *p : "", c, NULL);
110 gchar *tstring, *cstring;
112 //g_print ("checking n-way trees: ");
115 root = g_node_new (C2P ('A'));
116 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
118 node_B = g_node_new (C2P ('B'));
119 g_node_append (root, node_B);
120 TEST (NULL, root->children == node_B);
122 g_node_append_data (node_B, C2P ('E'));
123 g_node_prepend_data (node_B, C2P ('C'));
124 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
126 node_F = g_node_new (C2P ('F'));
127 g_node_append (root, node_F);
128 TEST (NULL, root->children->next == node_F);
130 node_G = g_node_new (C2P ('G'));
131 g_node_append (node_F, node_G);
132 node_J = g_node_new (C2P ('J'));
133 g_node_prepend (node_G, node_J);
134 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
135 g_node_insert_data (node_G, 0, C2P ('H'));
136 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
138 TEST (NULL, g_node_depth (root) == 1);
139 TEST (NULL, g_node_max_height (root) == 4);
140 TEST (NULL, g_node_depth (node_G->children->next) == 4);
141 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
142 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
143 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
144 TEST (NULL, g_node_max_height (node_F) == 3);
145 TEST (NULL, g_node_n_children (node_G) == 4);
146 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
147 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
148 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
150 for (i = 0; i < g_node_n_children (node_B); i++)
152 node = g_node_nth_child (node_B, i);
153 TEST (NULL, P2C (node->data) == ('C' + i));
156 for (i = 0; i < g_node_n_children (node_G); i++)
157 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
167 * for in-order traversal, 'G' is considered to be the "left"
168 * child of 'F', which will cause 'F' to be the last node visited.
172 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
173 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
174 g_free (tstring); tstring = NULL;
175 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
176 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
177 g_free (tstring); tstring = NULL;
178 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
179 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
180 g_free (tstring); tstring = NULL;
181 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
182 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
183 g_free (tstring); tstring = NULL;
185 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
186 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
187 g_free (tstring); tstring = NULL;
188 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
189 TEST (tstring, strcmp (tstring, "ABFG") == 0);
190 g_free (tstring); tstring = NULL;
192 g_node_reverse_children (node_B);
193 g_node_reverse_children (node_G);
195 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
196 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
197 g_free (tstring); tstring = NULL;
200 node = g_node_copy (root);
201 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
202 TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
203 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
204 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
205 TEST (cstring, strcmp (tstring, cstring) == 0);
206 g_free (tstring); tstring = NULL;
207 g_free (cstring); cstring = NULL;
208 g_node_destroy (node);
210 g_node_destroy (root);
212 /* allocation tests */
214 root = g_node_new (NULL);
217 for (i = 0; i < 2048; i++)
219 g_node_append (node, g_node_new (NULL));
221 node = node->children->next;
223 TEST (NULL, g_node_max_height (root) > 100);
224 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
226 g_node_destroy (root);
229 g_print ("g_node_test failed\n");
233 my_hash_callback_remove (gpointer key,
246 my_hash_callback_remove_test (gpointer key,
257 my_hash_callback (gpointer key,
266 my_hash (gconstpointer key)
268 return (guint) *((const gint*) key);
272 my_hash_equal (gconstpointer a,
275 return *((const gint*) a) == *((const gint*) b);
279 my_list_compare_one (gconstpointer a, gconstpointer b)
281 gint one = *((const gint*)a);
282 gint two = *((const gint*)b);
287 my_list_compare_two (gconstpointer a, gconstpointer b)
289 gint one = *((const gint*)a);
290 gint two = *((const gint*)b);
295 my_list_print (gpointer a, gpointer b)
297 gint three = *((gint*)a);
298 g_print("%d", three);
302 my_compare (gconstpointer a,
312 my_traverse (gpointer key,
317 g_print ("%c ", *ch);
322 find_first_that(gpointer key,
327 gint *test = user_data;
328 return (*v == *test);
333 test_g_mkdir_with_parents_1 (const gchar *base)
335 char *p0 = g_build_filename (base, "fum", NULL);
336 char *p1 = g_build_filename (p0, "tem", NULL);
337 char *p2 = g_build_filename (p1, "zap", NULL);
344 if (g_file_test (p0, G_FILE_TEST_EXISTS))
346 g_print ("failed, %s exists, cannot test g_mkdir_with_parents\n", p0);
350 if (g_file_test (p1, G_FILE_TEST_EXISTS))
352 g_print ("failed, %s exists, cannot test g_mkdir_with_parents\n", p1);
353 g_assert(FALSE && "testglib");
357 if (g_file_test (p2, G_FILE_TEST_EXISTS))
359 g_print ("failed, %s exists, cannot test g_mkdir_with_parents\n", p2);
360 g_assert(FALSE && "testglib");
365 if (g_mkdir_with_parents (p2, S_IWUSR) == -1)
367 g_print ("failed, g_mkdir_with_parents(%s) failed: %s\n", p2, g_strerror (errno));
368 g_assert(FALSE && "testglib");
372 if (g_mkdir_with_parents (p2, 0666) == -1)
374 g_print ("failed, g_mkdir_with_parents(%s) failed: %s\n", p2, g_strerror (errno));
375 g_assert(FALSE && "testglib");
380 if (!g_file_test (p2, G_FILE_TEST_IS_DIR))
382 g_print ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p2);
383 g_assert(FALSE && "testglib");
387 if (!g_file_test (p1, G_FILE_TEST_IS_DIR))
389 g_print ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p1);
390 g_assert(FALSE && "testglib");
394 if (!g_file_test (p0, G_FILE_TEST_IS_DIR))
396 g_print ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p0);
397 g_assert(FALSE && "testglib");
402 if (g_file_test (p2, G_FILE_TEST_EXISTS))
404 g_print ("failed, did g_rmdir(%s), but %s is still there\n", p2, p2);
405 g_assert(FALSE && "testglib");
410 if (g_file_test (p1, G_FILE_TEST_EXISTS))
412 g_print ("failed, did g_rmdir(%s), but %s is still there\n", p1, p1);
413 g_assert(FALSE && "testglib");
417 f = g_fopen (p1, "w");
420 g_print ("failed, couldn't create file %s\n", p1);
421 g_assert(FALSE && "testglib");
427 if (g_mkdir_with_parents (p1,S_IWUSR) == 0)
429 g_print ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p1, p1);
430 g_assert(FALSE && "testglib");
434 if (g_mkdir_with_parents (p1, 0666) == 0)
436 g_print ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p1, p1);
437 g_assert(FALSE && "testglib");
443 if (g_mkdir_with_parents (p2, S_IWUSR) == 0)
445 g_print ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p2, p1);
446 g_assert(FALSE && "testglib");
450 if (g_mkdir_with_parents (p2, 0666) == 0)
452 g_print ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p2, p1);
453 g_assert(FALSE && "testglib");
466 test_g_mkdir_with_parents (void)
469 if (!test_g_mkdir_with_parents_1 ("c:\\hum"))
471 g_remove ("c:\\hum");
473 if (!test_g_mkdir_with_parents_1 ("hum"))
479 if (!test_g_mkdir_with_parents_1 ("hii///haa/hee"))
481 g_remove ("hii/haa/hee");
482 g_remove ("hii/haa");
487 if (!test_g_mkdir_with_parents_1 (g_get_current_dir ()))
494 test_g_parse_debug_string (void)
496 GDebugKey keys[3] = {
504 result = g_parse_debug_string ("bar:foo:blubb", keys, n_keys);
505 g_assert (result == 3);
507 result = g_parse_debug_string (":baz::_E@~!_::", keys, n_keys);
508 g_assert (result == 4);
510 result = g_parse_debug_string ("", keys, n_keys);
511 g_assert (result == 0);
513 result = g_parse_debug_string (" : ", keys, n_keys);
514 g_assert (result == 0);
526 GHashTable *hash_table;
527 GMemChunk *mem_chunk;
528 GStringChunk *string_chunk;
529 GTimer *timer, *timer2;
531 gulong elapsed_usecs;
532 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
533 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
538 gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
543 GString *string1, *string2;
544 const gchar *charset;
553 } dirname_checks[] = {
564 { ".\\\\\\\\", "." },
566 { "..\\\\\\\\", ".." },
574 { "//server/share///x", "//server/share" },
580 guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
585 } skip_root_checks[] = {
594 { "\\\\server\\foo", "" },
595 { "\\\\server\\foo\\bar", "bar" },
599 { "//server/share///x", "//x" },
604 guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
606 #ifndef G_DISABLE_ASSERT
607 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
608 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
609 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
610 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
612 const char hello[] = "Hello, World";
613 const int hellolen = sizeof (hello) - 1;
619 /* Can't calculate GLib DLL name at runtime. */
620 gchar *glib_dll = "libglib-2.0-0.dll";
623 gchar *glib_dll = "cygglib-2.0-0.dll";
629 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
630 g_set_print_handler(mrtPrintHandler);
634 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
643 mkdir("C:\\Private\\e000002c", 0666);
644 chdir("C:\\Private\\e000002c");
646 string = g_get_current_dir ();
647 g_assert(!strcmp(string,"C:\\Private\\e000002c"));
649 string = (char *)g_get_user_name ();
650 g_assert(!strcmp(string,"root"));
652 string = (char *)g_get_real_name ();
653 g_assert(!strcmp(string,"Unknown"));
655 string = (char *)g_get_host_name ();
656 gethostname(hostname,256);
657 g_assert(!strcmp(string,hostname));
659 s = g_get_home_dir ();
660 g_assert(!strcmp(s,"C:\\Private\\e000002c") && "Wrong value for g_get_home_dir()");
661 s = g_get_user_data_dir ();
662 g_assert(!strcmp(s,"C:\\Private\\e000002c"));
663 s = g_get_user_config_dir ();
664 g_assert(!strcmp(s,"C:\\Private\\e000002c"));
665 s = g_get_user_cache_dir ();
666 g_assert(!strcmp(s,"C:\\Private\\e000002c"));
667 sv = (gchar **) g_get_system_data_dirs ();
668 g_assert(!strcmp(sv[0],"C:\\Private\\e000002c"));
669 g_assert(sv[1] == NULL);
671 sv = (gchar **) g_get_system_config_dirs ();
672 g_assert(!strcmp(sv[0],"C:\\Private\\e000002c"));
673 g_assert(sv[1] == NULL);
675 string = (char *)g_get_tmp_dir ();
676 g_assert(!strcmp(string,"C:\\Private\\e000002c\\tmp") && "Wrong value for g_get_tmp_dir()");
678 sv = (gchar **) g_get_language_names ();
679 g_assert(!strcmp(sv[0],"C"));
680 g_assert(!strcmp(sv[1],"C"));
681 g_assert(sv[2] == NULL);
684 TEST (NULL, sizeof (gint8) == 1);
686 TEST (NULL, sizeof (gint16) == 2);
688 TEST (NULL, sizeof (gint32) == 4);
690 TEST (NULL, sizeof (gint64) == 8);
693 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
694 g_assert (strcmp (string, "dir") == 0);
697 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
698 g_assert (strcmp (string, "file") == 0);
702 string = g_path_get_basename ("/foo/dir/");
703 g_assert (strcmp (string, "dir") == 0);
705 string = g_path_get_basename ("/foo/file");
706 g_assert (strcmp (string, "file") == 0);
710 //g_print ("checking g_path_get_dirname()...");
711 for (i = 0; i < n_dirname_checks; i++)
715 dirname = g_path_get_dirname (dirname_checks[i].filename);
716 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
718 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
719 dirname_checks[i].filename,
720 dirname_checks[i].dirname,
722 g_assert(FALSE && "testglib");
723 n_dirname_checks = 0;
727 g_assert(n_dirname_checks != 0 && "g_path_get_dirname failed" );
729 //g_print ("checking g_path_skip_root()...");
730 for (i = 0; i < n_skip_root_checks; i++)
732 const gchar *skipped;
734 skipped = g_path_skip_root (skip_root_checks[i].filename);
735 if ((skipped && !skip_root_checks[i].without_root) ||
736 (!skipped && skip_root_checks[i].without_root) ||
737 ((skipped && skip_root_checks[i].without_root) &&
738 strcmp (skipped, skip_root_checks[i].without_root)))
740 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
741 skip_root_checks[i].filename,
742 (skip_root_checks[i].without_root ?
743 skip_root_checks[i].without_root : "<NULL>"),
744 (skipped ? skipped : "<NULL>"));
745 g_assert(FALSE && "testglib");
746 n_skip_root_checks = 0;
749 g_assert(n_skip_root_checks != 0 && "g_path_skip_root failed" );
751 g_assert(test_g_mkdir_with_parents());
752 //g_print ("checking doubly linked lists...");
755 for (i = 0; i < 10; i++)
756 list = g_list_append (list, &nums[i]);
757 list = g_list_reverse (list);
759 for (i = 0; i < 10; i++)
761 t = g_list_nth (list, i);
762 if (*((gint*) t->data) != (9 - i))
763 g_error ("Regular insert failed");
766 for (i = 0; i < 10; i++)
767 if(g_list_position(list, g_list_nth (list, i)) != i)
768 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
773 for (i = 0; i < 10; i++)
774 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
776 for (i = 0; i < 10; i++)
778 t = g_list_nth (list, i);
779 if (*((gint*) t->data) != i)
780 g_error ("Sorted insert failed");
786 for (i = 0; i < 10; i++)
787 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
789 for (i = 0; i < 10; i++)
791 t = g_list_nth (list, i);
792 if (*((gint*) t->data) != (9 - i))
793 g_error ("Sorted insert failed");
799 for (i = 0; i < 10; i++)
800 list = g_list_prepend (list, &morenums[i]);
802 list = g_list_sort (list, my_list_compare_two);
805 for (i = 0; i < 10; i++)
807 t = g_list_nth (list, i);
808 if (*((gint*) t->data) != (9 - i))
809 g_error ("Merge sort failed");
814 //g_print ("checking singly linked lists...");
817 for (i = 0; i < 10; i++)
818 slist = g_slist_append (slist, &nums[i]);
819 slist = g_slist_reverse (slist);
821 for (i = 0; i < 10; i++)
823 st = g_slist_nth (slist, i);
824 if (*((gint*) st->data) != (9 - i))
828 g_slist_free (slist);
831 for (i = 0; i < 10; i++)
832 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
834 for (i = 0; i < 10; i++)
836 st = g_slist_nth (slist, i);
837 if (*((gint*) st->data) != i)
838 g_error ("Sorted insert failed");
844 for (i = 0; i < 10; i++)
845 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
848 for (i = 0; i < 10; i++)
850 st = g_slist_nth (slist, i);
851 if (*((gint*) st->data) != (9 - i))
852 g_error("Sorted insert failed");
858 for (i = 0; i < 10; i++)
859 slist = g_slist_prepend (slist, &morenums[i]);
861 slist = g_slist_sort (slist, my_list_compare_two);
864 for (i = 0; i < 10; i++)
866 st = g_slist_nth (slist, i);
867 if (*((gint*) st->data) != (9 - i))
868 g_error("Sorted insert failed");
873 //g_print ("checking binary trees...\n");
875 tree = g_tree_new (my_compare);
877 for (j = 0; j < 10; j++, i++)
880 g_tree_insert (tree, &chars[i], &chars[i]);
882 for (j = 0; j < 26; j++, i++)
885 g_tree_insert (tree, &chars[i], &chars[i]);
887 for (j = 0; j < 26; j++, i++)
890 g_tree_insert (tree, &chars[i], &chars[i]);
893 g_assert(g_tree_height(tree) == 6);
894 g_assert(g_tree_nnodes(tree) == 62);
896 for (i = 0; i < 10; i++)
897 g_tree_remove (tree, &chars[i]);
899 g_assert(g_tree_height(tree) == 6);
900 g_assert(g_tree_nnodes(tree) == 52);
902 //g_print ("tree: ");
903 //g_tree_foreach (tree, my_traverse, NULL);
907 /* check n-way trees */
910 //g_print ("checking mem chunks...");
911 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
914 for (i = 0; i < 6000 ; i++)
916 mem[i] = g_chunk_new (gchar, mem_chunk);
918 for (j = 0; j < 50; j++)
922 for (i = 0; i < 10000 ; i++)
924 mem[i] = g_chunk_new (gchar, mem_chunk);
926 for (j = 0; j < 50; j++)
933 for (i = 0; i < 6000 ; i++)
935 g_mem_chunk_free (mem_chunk, mem[i]);
938 for (i = 0; i < i 10000; i++)
940 g_mem_chunk_free (mem_chunk, mem[i]);
943 //g_print ("checking hash tables...");
945 hash_table = g_hash_table_new (my_hash, my_hash_equal);
948 for (i = 0; i < 3000 ; i++)
951 g_hash_table_insert (hash_table, &array[i], &array[i]);
954 for (i = 0; i < i 10000 ; i++)
957 g_hash_table_insert (hash_table, &array[i], &array[i]);
961 pvalue = g_hash_table_find (hash_table, find_first_that, &value);
962 if (*pvalue != value)
965 g_print("g_hash_table_find failed");
966 g_assert(FALSE && "testglib");
968 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
971 for (i = 0; i < 3000 ; i++)
975 for (i = 0; i < 10000; i++)
981 for (i = 0; i < 3000 ; i++)
982 g_hash_table_remove (hash_table, &array[i]);
984 for (i = 0; i < 10000; i++)
985 g_hash_table_remove (hash_table, &array[i]);
989 for (i = 0; i < 3000 ; i++)
992 g_hash_table_insert (hash_table, &array[i], &array[i]);
995 for (i = 0; i < 10000; i++)
998 g_hash_table_insert (hash_table, &array[i], &array[i]);
1003 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 1500 ||
1004 g_hash_table_size (hash_table) != 1500 )
1007 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
1008 g_hash_table_size (hash_table) != 5000)
1013 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
1016 g_hash_table_destroy (hash_table);
1018 string_chunk = g_string_chunk_new (1024);
1021 for (i = 0; i < 3000; i ++)
1023 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
1025 if (strcmp ("hi pete", tmp_string) != 0)
1026 g_error ("string chunks are broken.\n");
1029 for (i = 0; i < 100000; i ++)
1031 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
1033 if (strcmp ("hi pete", tmp_string) != 0)
1034 g_error ("string chunks are broken.\n");
1038 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
1040 g_assert (tmp_string_2 != tmp_string &&
1041 strcmp(tmp_string_2, tmp_string) == 0);
1043 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
1045 g_assert (tmp_string_2 == tmp_string);
1047 g_string_chunk_free (string_chunk);
1050 //g_print ("checking arrays...");
1051 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1054 for (i = 0; i < 3000; i++)
1055 g_array_append_val (garray, i);
1057 for (i = 0; i < 10000; i++)
1058 g_array_append_val (garray, i);
1062 for (i = 0; i < 3000 ; i++)
1063 if (g_array_index (garray, gint, i) != i)
1064 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
1066 for (i = 0; i < i 10000; i++)
1067 if (g_array_index (garray, gint, i) != i)
1068 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
1071 g_array_free (garray, TRUE);
1073 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1074 for (i = 0; i < 100; i++)
1075 g_array_prepend_val (garray, i);
1077 for (i = 0; i < 100; i++)
1078 if (g_array_index (garray, gint, i) != (100 - i - 1))
1079 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
1081 g_array_free (garray, TRUE);
1084 //g_print ("checking strings...");
1085 string1 = g_string_new ("hi pete!");
1086 string2 = g_string_new ("");
1088 g_assert (strcmp ("hi pete!", string1->str) == 0);
1090 for (i = 0; i < 10000; i++)
1091 g_string_append_c (string1, 'a'+(i%26));
1094 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
1095 "this pete guy sure is a wuss, like he's the number ",
1097 " wuss. everyone agrees.\n",
1099 10, 666, 15, 15, 666.666666666, 666.666666666);
1102 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
1103 the %10000.10000f format... */
1104 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
1105 "this pete guy sure is a wuss, like he's the number ",
1107 " wuss. everyone agrees.\n",
1109 10, 666, 15, 15, 666.666666666, 666.666666666);
1111 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
1112 "this pete guy sure is a wuss, like he's the number ",
1114 " wuss. everyone agrees.\n",
1116 10, 666, 15, 15, 666.666666666, 666.666666666);
1118 #endif /* SYMBIAN */
1120 g_assert((gulong)string2->len == 10323);
1121 g_string_free (string1, TRUE);
1122 g_string_free (string2, TRUE);
1125 string1 = g_string_new ("firsthalf");
1126 g_string_append (string1, "lasthalf");
1127 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1128 g_string_free (string1, TRUE);
1132 string1 = g_string_new ("firsthalf");
1133 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
1134 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1135 g_string_free (string1, TRUE);
1138 string1 = g_string_new ("lasthalf");
1139 g_string_prepend (string1, "firsthalf");
1140 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1141 g_string_free (string1, TRUE);
1144 string1 = g_string_new ("lasthalf");
1145 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
1146 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1147 g_string_free (string1, TRUE);
1150 string1 = g_string_new ("firstlast");
1151 g_string_insert (string1, 5, "middle");
1152 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1153 g_string_free (string1, TRUE);
1155 /* insert with pos == end of the string */
1156 string1 = g_string_new ("firstmiddle");
1157 g_string_insert (string1, strlen ("firstmiddle"), "last");
1158 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1159 g_string_free (string1, TRUE);
1163 string1 = g_string_new ("firstlast");
1164 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
1165 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1166 g_string_free (string1, TRUE);
1168 /* insert_len with magic -1 pos for append */
1169 string1 = g_string_new ("first");
1170 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
1171 g_assert (strcmp (string1->str, "firstlast") == 0);
1172 g_string_free (string1, TRUE);
1174 /* insert_len with magic -1 len for strlen-the-string */
1175 string1 = g_string_new ("first");
1176 g_string_insert_len (string1, 5, "last", -1);
1177 g_assert (strcmp (string1->str, "firstlast") == 0);
1178 g_string_free (string1, TRUE);
1180 /* g_string_equal */
1181 string1 = g_string_new ("test");
1182 string2 = g_string_new ("te");
1183 g_assert (! g_string_equal(string1, string2));
1184 g_string_append (string2, "st");
1185 g_assert (g_string_equal(string1, string2));
1186 g_string_free (string1, TRUE);
1187 g_string_free (string2, TRUE);
1189 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
1190 string1 = g_string_new ("fiddle");
1191 string2 = g_string_new ("fiddle");
1192 g_assert (g_string_equal(string1, string2));
1193 g_string_append_c(string1, '\0');
1194 g_assert (! g_string_equal(string1, string2));
1195 g_string_append_c(string2, '\0');
1196 g_assert (g_string_equal(string1, string2));
1197 g_string_append_c(string1, 'x');
1198 g_string_append_c(string2, 'y');
1199 g_assert (! g_string_equal(string1, string2));
1200 g_assert (string1->len == 8);
1201 g_string_append(string1, "yzzy");
1202 g_assert (string1->len == 12);
1203 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
1204 g_string_insert(string1, 1, "QED");
1205 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
1206 g_string_free (string1, TRUE);
1207 g_string_free (string2, TRUE);
1209 //g_print ("test positional printf formats (not supported): ");
1210 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
1211 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
1212 g_assert(!strcmp(string,"ab"));
1213 g_assert(!strcmp(tmp_string," c"));
1214 g_free (tmp_string);
1217 timer = g_timer_new ();
1219 g_timer_start (timer);
1220 while (g_timer_elapsed (timer, NULL) < 3)
1223 g_timer_stop (timer);
1224 g_timer_destroy (timer);
1226 timer2 = g_timer_new ();
1228 timer = g_timer_new();
1229 g_usleep(G_USEC_PER_SEC); /* run timer for 1 second */
1230 g_timer_stop(timer);
1232 g_usleep(G_USEC_PER_SEC); /* wait for 1 second */
1234 g_timer_continue(timer);
1235 g_usleep(2*G_USEC_PER_SEC); /* run timer for 2 seconds */
1236 g_timer_stop(timer);
1238 g_usleep((3*G_USEC_PER_SEC)/2); /* wait for 1.5 seconds */
1240 g_timer_continue(timer);
1241 g_usleep(G_USEC_PER_SEC/5); /* run timer for 0.2 seconds */
1242 g_timer_stop(timer);
1244 g_usleep(4*G_USEC_PER_SEC); /* wait for 4 seconds */
1246 g_timer_continue(timer);
1247 g_usleep((29*G_USEC_PER_SEC)/5); /* run timer for 5.8 seconds */
1248 g_timer_stop(timer);
1250 elapsed = g_timer_elapsed (timer, &elapsed_usecs);
1252 if (elapsed > 8.8 && elapsed < 9.2);
1256 g_assert(FALSE && "testglib");
1257 g_print ("g_timer_continue ... ***** FAILED *****\n\n");
1258 g_print ("timer elapsed %d\n",elapsed);
1260 g_timer_stop(timer2);
1262 elapsed = g_timer_elapsed(timer2, &elapsed_usecs);
1264 if (elapsed > (8.8+6.5) && elapsed < (9.2+6.5));
1268 g_assert(FALSE && "testglib");
1269 g_print ("timer2 ... ***** FAILED *****\n\n");
1270 g_print ("timer2 elapsed %d\n",elapsed);
1272 g_timer_destroy(timer);
1273 g_timer_destroy(timer2);
1275 g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
1276 g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
1277 g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
1278 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1279 g_assert (g_ascii_strcasecmp ("", "") == 0);
1280 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
1281 g_assert (g_ascii_strcasecmp ("a", "b") < 0);
1282 g_assert (g_ascii_strcasecmp ("a", "B") < 0);
1283 g_assert (g_ascii_strcasecmp ("A", "b") < 0);
1284 g_assert (g_ascii_strcasecmp ("A", "B") < 0);
1285 g_assert (g_ascii_strcasecmp ("b", "a") > 0);
1286 g_assert (g_ascii_strcasecmp ("b", "A") > 0);
1287 g_assert (g_ascii_strcasecmp ("B", "a") > 0);
1288 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1290 g_assert(g_strdup(NULL) == NULL);
1291 string = g_strdup(GLIB_TEST_STRING);
1292 g_assert(string != NULL);
1293 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
1296 string = g_strconcat(GLIB_TEST_STRING, NULL);
1297 g_assert(string != NULL);
1298 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
1300 string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
1301 GLIB_TEST_STRING, NULL);
1302 g_assert(string != NULL);
1303 g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
1304 GLIB_TEST_STRING) == 0);
1308 /* The following is a torture test for strlcpy/strlcat, with lots of
1309 * checking; normal users wouldn't use them this way!
1311 string = g_malloc (6);
1312 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
1314 g_assert (g_strlcpy(string, "" , 5) == 0);
1315 g_assert ( *string == '\0' );
1317 g_assert (g_strlcpy(string, "abc" , 5) == 3);
1318 g_assert ( *(string + 3) == '\0' );
1319 g_assert (g_str_equal(string, "abc"));
1320 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1321 g_assert ( *(string + 4) == '\0' );
1322 g_assert ( *(string + 5) == 'Z' );
1323 g_assert (g_str_equal(string, "abcd"));
1324 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1325 g_assert ( *(string + 4) == '\0' );
1326 g_assert ( *(string + 5) == 'Z' );
1327 g_assert (g_str_equal(string, "abcd"));
1328 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1329 g_assert ( *(string + 4) == '\0' );
1330 g_assert ( *(string + 5) == 'Z' );
1331 g_assert (g_str_equal(string, "abcd"));
1333 *(string + 1)= '\0';
1334 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1335 g_assert (*string == 'Y');
1337 g_assert (g_strlcat(string, "123" , 5) == 3);
1338 g_assert ( *(string + 3) == '\0' );
1339 g_assert (g_str_equal(string, "123"));
1340 g_assert (g_strlcat(string, "" , 5) == 3);
1341 g_assert ( *(string + 3) == '\0' );
1342 g_assert (g_str_equal(string, "123"));
1343 g_assert (g_strlcat(string, "4", 5) == 4);
1344 g_assert (g_str_equal(string, "1234"));
1345 g_assert (g_strlcat(string, "5", 5) == 5);
1346 g_assert ( *(string + 4) == '\0' );
1347 g_assert (g_str_equal(string, "1234"));
1348 g_assert ( *(string + 5) == 'Z' );
1350 *(string + 1)= '\0';
1351 g_assert (g_strlcat(string, "123" , 0) == 3);
1352 g_assert (*string == 'Y');
1354 /* A few more tests, demonstrating more "normal" use */
1355 g_assert (g_strlcpy(string, "hi", 5) == 2);
1356 g_assert (g_str_equal(string, "hi"));
1357 g_assert (g_strlcat(string, "t", 5) == 3);
1358 g_assert (g_str_equal(string, "hit"));
1361 string = g_strdup_printf ("%05d %-5s", 21, "test");
1362 g_assert (string != NULL);
1363 g_assert (strcmp(string, "00021 test ") == 0);
1367 /* g_debug (argv[0]); */
1369 /* Relation tests */
1371 relation = g_relation_new (2);
1373 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1374 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1377 for (i = 0; i < 250 ; i += 1)
1380 for (i = 0; i < 1024; i += 1)
1385 for (i = 1; i < 250 ; i += 1)
1387 g_relation_insert (relation, data + i, data + i + 1);
1388 g_relation_insert (relation, data + i, data + i - 1);
1391 for (i = 1; i < 1023; i += 1)
1393 g_relation_insert (relation, data + i, data + i + 1);
1394 g_relation_insert (relation, data + i, data + i - 1);
1399 for (i = 2; i < 249; i += 1)
1401 g_assert (! g_relation_exists (relation, data + i, data + i));
1402 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1403 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1406 for (i = 2; i < 1022; i += 1)
1408 g_assert (! g_relation_exists (relation, data + i, data + i));
1409 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1410 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1415 for (i = 1; i < 250 ; i += 1)
1417 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1418 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1421 for (i = 1; i < 1023; i += 1)
1423 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1424 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1429 for (i = 2; i < 249; i += 1)
1431 g_assert (g_relation_count (relation, data + i, 0) == 2);
1432 g_assert (g_relation_count (relation, data + i, 1) == 2);
1435 for (i = 2; i < 1022; i += 1)
1437 g_assert (g_relation_count (relation, data + i, 0) == 2);
1438 g_assert (g_relation_count (relation, data + i, 1) == 2);
1442 g_assert (g_relation_count (relation, data, 0) == 0);
1444 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1445 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1446 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1447 g_relation_delete (relation, data + 42, 0);
1448 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1449 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1450 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1452 tuples = g_relation_select (relation, data + 200, 0);
1454 g_assert (tuples->len == 2);
1458 g_assert (g_relation_exists (relation, data + 100, data + 101 ));
1459 g_relation_delete (relation, data + 100 , 0);
1460 g_assert (!g_relation_exists (relation, data + 100 , data + 101 ));
1462 g_assert (g_relation_exists (relation, data + 300, data + i 301));
1463 g_relation_delete (relation, data + 300, 0);
1464 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1467 g_tuples_destroy (tuples);
1469 g_relation_destroy (relation);
1473 gparray = g_ptr_array_new ();
1476 for (i = 0; i < 4000; i++)
1477 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1478 for (i = 0; i < 4000 ; i++)
1479 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1481 g_assert(FALSE && "testglib");
1482 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1485 for (i = 0; i < 10000; i++)
1486 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1487 for (i = 0; i < 10000; i++)
1488 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1490 g_assert(FALSE && "testglib");
1491 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1495 g_ptr_array_free (gparray, TRUE);
1498 gbarray = g_byte_array_new ();
1501 for (i = 0; i < 4000 ; i++)
1502 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1504 for (i = 0; i < 4000 ; i++)
1506 g_assert (gbarray->data[4*i] == 'a');
1507 g_assert (gbarray->data[4*i+1] == 'b');
1508 g_assert (gbarray->data[4*i+2] == 'c');
1509 g_assert (gbarray->data[4*i+3] == 'd');
1512 for (i = 0; i < 10000; i++)
1513 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1515 for (i = 0; i < 10000; i++)
1517 g_assert (gbarray->data[4*i] == 'a');
1518 g_assert (gbarray->data[4*i+1] == 'b');
1519 g_assert (gbarray->data[4*i+2] == 'c');
1520 g_assert (gbarray->data[4*i+3] == 'd');
1524 g_byte_array_free (gbarray, TRUE);
1528 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
1529 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
1530 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
1533 g_get_charset ((G_CONST_RETURN char**)&charset);
1534 g_assert(!strcmp(charset,"US-ASCII"));
1536 #ifdef G_PLATFORM_WIN32
1537 g_print ("current locale: %s\n", g_win32_getlocale ());
1538 g_print ("GLib DLL name tested for: %s\n", glib_dll);
1540 g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
1542 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
1543 g_print ("Ditto, or from GLib DLL name: %s\n",
1544 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
1545 g_print ("Ditto, only from GLib DLL name: %s\n",
1546 g_win32_get_package_installation_directory (NULL, glib_dll));
1547 g_print ("locale subdirectory of GLib installation directory: %s\n",
1548 g_win32_get_package_installation_subdirectory (NULL, glib_dll, "lib\\locale"));
1549 g_print ("GTK+ 2.0 installation directory, if available: %s\n",
1550 g_win32_get_package_installation_directory ("gtk20", NULL));
1552 g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
1553 g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
1555 g_print ("a Win32 error message: %s\n", g_win32_error_message (2));
1559 strcpy (template, "fooXXXXXX");
1560 fd = g_mkstemp (template);
1563 i = write (fd, hello, hellolen);
1567 i = read (fd, chars, sizeof (chars));
1571 g_assert(!strcmp(chars, hello));
1577 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1578 fd = g_file_open_tmp (template, &name_used, &error);
1583 g_clear_error (&error);
1586 strcpy (template, "zap/barXXXXXX");
1587 fd = g_file_open_tmp (template, &name_used, &error);
1589 g_print ("g_file_open_tmp works even if template contains '/'\n");
1591 g_print ("g_file_open_tmp correctly returns error: %s\n",
1594 g_clear_error (&error);
1597 strcpy (template, "zapXXXXXX");
1598 fd = g_file_open_tmp (template, &name_used, &error);
1603 g_clear_error (&error);
1606 fd = g_file_open_tmp (NULL, &name_used, &error);
1611 g_clear_error (&error);
1615 testResultXml("testglib");
1616 #endif /* EMULATOR */