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/.
31 #ifdef GLIB_COMPILATION
32 #undef GLIB_COMPILATION
46 #include <io.h> /* For read(), write() etc */
49 static int array[10000];
50 static gboolean failed = FALSE;
52 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
55 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
57 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
60 g_print ("."); fflush (stdout); \
63 #define C2P(c) ((gpointer) ((long) (c)))
64 #define P2C(p) ((gchar) ((long) (p)))
66 #define GLIB_TEST_STRING "el dorado "
67 #define GLIB_TEST_STRING_5 "el do"
70 node_build_string (GNode *node,
77 c[0] = P2C (node->data);
79 string = g_strconcat (*p ? *p : "", c, NULL);
96 gchar *tstring, *cstring;
98 g_print ("checking n-way trees: ");
101 root = g_node_new (C2P ('A'));
102 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
104 node_B = g_node_new (C2P ('B'));
105 g_node_append (root, node_B);
106 TEST (NULL, root->children == node_B);
108 g_node_append_data (node_B, C2P ('E'));
109 g_node_prepend_data (node_B, C2P ('C'));
110 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
112 node_F = g_node_new (C2P ('F'));
113 g_node_append (root, node_F);
114 TEST (NULL, root->children->next == node_F);
116 node_G = g_node_new (C2P ('G'));
117 g_node_append (node_F, node_G);
118 node_J = g_node_new (C2P ('J'));
119 g_node_prepend (node_G, node_J);
120 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
121 g_node_insert_data (node_G, 0, C2P ('H'));
122 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
124 TEST (NULL, g_node_depth (root) == 1);
125 TEST (NULL, g_node_max_height (root) == 4);
126 TEST (NULL, g_node_depth (node_G->children->next) == 4);
127 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
128 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
129 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
130 TEST (NULL, g_node_max_height (node_F) == 3);
131 TEST (NULL, g_node_n_children (node_G) == 4);
132 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
133 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
134 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
136 for (i = 0; i < g_node_n_children (node_B); i++)
138 node = g_node_nth_child (node_B, i);
139 TEST (NULL, P2C (node->data) == ('C' + i));
142 for (i = 0; i < g_node_n_children (node_G); i++)
143 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
153 * for in-order traversal, 'G' is considered to be the "left"
154 * child of 'F', which will cause 'F' to be the last node visited.
158 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
159 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
160 g_free (tstring); tstring = NULL;
161 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
162 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
163 g_free (tstring); tstring = NULL;
164 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
165 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
166 g_free (tstring); tstring = NULL;
167 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
168 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
169 g_free (tstring); tstring = NULL;
171 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
172 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
173 g_free (tstring); tstring = NULL;
174 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
175 TEST (tstring, strcmp (tstring, "ABFG") == 0);
176 g_free (tstring); tstring = NULL;
178 g_node_reverse_children (node_B);
179 g_node_reverse_children (node_G);
181 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
182 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
183 g_free (tstring); tstring = NULL;
186 node = g_node_copy (root);
187 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
188 TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
189 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
190 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
191 TEST (cstring, strcmp (tstring, cstring) == 0);
192 g_free (tstring); tstring = NULL;
193 g_free (cstring); cstring = NULL;
194 g_node_destroy (node);
196 g_node_destroy (root);
198 /* allocation tests */
200 root = g_node_new (NULL);
203 for (i = 0; i < 2048; i++)
205 g_node_append (node, g_node_new (NULL));
207 node = node->children->next;
209 TEST (NULL, g_node_max_height (root) > 100);
210 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
212 g_node_destroy (root);
219 my_hash_callback_remove (gpointer key,
232 my_hash_callback_remove_test (gpointer key,
243 my_hash_callback (gpointer key,
252 my_hash (gconstpointer key)
254 return (guint) *((const gint*) key);
258 my_hash_equal (gconstpointer a,
261 return *((const gint*) a) == *((const gint*) b);
265 my_list_compare_one (gconstpointer a, gconstpointer b)
267 gint one = *((const gint*)a);
268 gint two = *((const gint*)b);
273 my_list_compare_two (gconstpointer a, gconstpointer b)
275 gint one = *((const gint*)a);
276 gint two = *((const gint*)b);
281 my_list_print (gpointer a, gpointer b)
283 gint three = *((gint*)a);
284 g_print("%d", three);
288 my_compare (gconstpointer a,
298 my_traverse (gpointer key,
303 g_print ("%c ", *ch);
313 GHashTable *hash_table;
314 GMemChunk *mem_chunk;
315 GStringChunk *string_chunk;
317 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
318 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
321 gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
326 GString *string1, *string2;
335 } dirname_checks[] = {
347 { ".\\\\\\\\", "." },
349 { "..\\\\\\\\", ".." },
351 { "a\\b\\", "a\\b" },
355 { "//server/share///x", "//server/share" },
361 guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
366 } skip_root_checks[] = {
376 { "\\\\server\\foo", "" },
377 { "\\\\server\\foo\\bar", "bar" },
381 { "//server/share///x", "//x" },
386 guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
388 #ifndef G_DISABLE_ASSERT
389 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
390 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
392 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
393 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
396 const char hello[] = "Hello, World";
397 const int hellolen = sizeof (hello) - 1;
403 gchar *glib_dll = g_strdup_printf ("glib-%d.%d.dll",
408 gchar *glib_dll = g_strdup_printf ("cygglib-%d.%d.dll",
413 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
420 string = g_get_current_dir ();
421 g_print ("cwd: %s\n", string);
423 g_print ("user: %s\n", g_get_user_name ());
424 g_print ("real: %s\n", g_get_real_name ());
425 g_print ("home: %s\n", g_get_home_dir ());
426 g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
429 g_print ("checking size of gint8: %d", (int)sizeof (gint8));
430 TEST (NULL, sizeof (gint8) == 1);
431 g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
432 TEST (NULL, sizeof (gint16) == 2);
433 g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
434 TEST (NULL, sizeof (gint32) == 4);
435 g_print ("\nchecking size of gsize: %d", (int)sizeof (gsize));
437 g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
438 TEST (NULL, sizeof (gint64) == 8);
439 #endif /* G_HAVE_GINT64 */
442 g_print ("checking g_path_get_dirname()...");
443 for (i = 0; i < n_dirname_checks; i++)
447 dirname = g_path_get_dirname (dirname_checks[i].filename);
448 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
450 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
451 dirname_checks[i].filename,
452 dirname_checks[i].dirname,
454 n_dirname_checks = 0;
458 if (n_dirname_checks)
461 g_print ("checking g_path_skip_root()...");
462 for (i = 0; i < n_skip_root_checks; i++)
464 const gchar *skipped;
466 skipped = g_path_skip_root (skip_root_checks[i].filename);
467 if ((skipped && !skip_root_checks[i].without_root) ||
468 (!skipped && skip_root_checks[i].without_root) ||
469 ((skipped && skip_root_checks[i].without_root) &&
470 strcmp (skipped, skip_root_checks[i].without_root)))
472 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
473 skip_root_checks[i].filename,
474 (skip_root_checks[i].without_root ?
475 skip_root_checks[i].without_root : "<NULL>"),
476 (skipped ? skipped : "<NULL>"));
477 n_skip_root_checks = 0;
480 if (n_skip_root_checks)
483 g_print ("checking doubly linked lists...");
486 for (i = 0; i < 10; i++)
487 list = g_list_append (list, &nums[i]);
488 list = g_list_reverse (list);
490 for (i = 0; i < 10; i++)
492 t = g_list_nth (list, i);
493 if (*((gint*) t->data) != (9 - i))
494 g_error ("Regular insert failed");
497 for (i = 0; i < 10; i++)
498 if(g_list_position(list, g_list_nth (list, i)) != i)
499 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
504 for (i = 0; i < 10; i++)
505 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
509 g_list_foreach (list, my_list_print, NULL);
512 for (i = 0; i < 10; i++)
514 t = g_list_nth (list, i);
515 if (*((gint*) t->data) != i)
516 g_error ("Sorted insert failed");
522 for (i = 0; i < 10; i++)
523 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
527 g_list_foreach (list, my_list_print, NULL);
530 for (i = 0; i < 10; i++)
532 t = g_list_nth (list, i);
533 if (*((gint*) t->data) != (9 - i))
534 g_error ("Sorted insert failed");
540 for (i = 0; i < 10; i++)
541 list = g_list_prepend (list, &morenums[i]);
543 list = g_list_sort (list, my_list_compare_two);
547 g_list_foreach (list, my_list_print, NULL);
550 for (i = 0; i < 10; i++)
552 t = g_list_nth (list, i);
553 if (*((gint*) t->data) != (9 - i))
554 g_error ("Merge sort failed");
562 g_print ("checking singly linked lists...");
565 for (i = 0; i < 10; i++)
566 slist = g_slist_append (slist, &nums[i]);
567 slist = g_slist_reverse (slist);
569 for (i = 0; i < 10; i++)
571 st = g_slist_nth (slist, i);
572 if (*((gint*) st->data) != (9 - i))
576 g_slist_free (slist);
579 for (i = 0; i < 10; i++)
580 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
584 g_slist_foreach (slist, my_list_print, NULL);
587 for (i = 0; i < 10; i++)
589 st = g_slist_nth (slist, i);
590 if (*((gint*) st->data) != i)
591 g_error ("Sorted insert failed");
597 for (i = 0; i < 10; i++)
598 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
602 g_slist_foreach (slist, my_list_print, NULL);
605 for (i = 0; i < 10; i++)
607 st = g_slist_nth (slist, i);
608 if (*((gint*) st->data) != (9 - i))
609 g_error("Sorted insert failed");
615 for (i = 0; i < 10; i++)
616 slist = g_slist_prepend (slist, &morenums[i]);
618 slist = g_slist_sort (slist, my_list_compare_two);
622 g_slist_foreach (slist, my_list_print, NULL);
625 for (i = 0; i < 10; i++)
627 st = g_slist_nth (slist, i);
628 if (*((gint*) st->data) != (9 - i))
629 g_error("Sorted insert failed");
637 g_print ("checking binary trees...\n");
639 tree = g_tree_new (my_compare);
641 for (j = 0; j < 10; j++, i++)
644 g_tree_insert (tree, &chars[i], &chars[i]);
646 for (j = 0; j < 26; j++, i++)
649 g_tree_insert (tree, &chars[i], &chars[i]);
651 for (j = 0; j < 26; j++, i++)
654 g_tree_insert (tree, &chars[i], &chars[i]);
657 g_print ("tree height: %d\n", g_tree_height (tree));
658 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
661 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
664 for (i = 0; i < 10; i++)
665 g_tree_remove (tree, &chars[i]);
667 g_print ("tree height: %d\n", g_tree_height (tree));
668 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
671 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
677 /* check n-way trees */
680 g_print ("checking mem chunks...");
682 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
684 for (i = 0; i < 10000; i++)
686 mem[i] = g_chunk_new (gchar, mem_chunk);
688 for (j = 0; j < 50; j++)
692 for (i = 0; i < 10000; i++)
694 g_mem_chunk_free (mem_chunk, mem[i]);
700 g_print ("checking hash tables...");
702 hash_table = g_hash_table_new (my_hash, my_hash_equal);
703 for (i = 0; i < 10000; i++)
706 g_hash_table_insert (hash_table, &array[i], &array[i]);
708 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
710 for (i = 0; i < 10000; i++)
714 for (i = 0; i < 10000; i++)
715 g_hash_table_remove (hash_table, &array[i]);
717 for (i = 0; i < 10000; i++)
720 g_hash_table_insert (hash_table, &array[i], &array[i]);
723 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
724 g_hash_table_size (hash_table) != 5000)
727 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
730 g_hash_table_destroy (hash_table);
735 g_print ("checking string chunks...");
737 string_chunk = g_string_chunk_new (1024);
739 for (i = 0; i < 100000; i ++)
741 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
743 if (strcmp ("hi pete", tmp_string) != 0)
744 g_error ("string chunks are broken.\n");
747 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
749 g_assert (tmp_string_2 != tmp_string &&
750 strcmp(tmp_string_2, tmp_string) == 0);
752 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
754 g_assert (tmp_string_2 == tmp_string);
756 g_string_chunk_free (string_chunk);
761 g_print ("checking arrays...");
763 garray = g_array_new (FALSE, FALSE, sizeof (gint));
764 for (i = 0; i < 10000; i++)
765 g_array_append_val (garray, i);
767 for (i = 0; i < 10000; i++)
768 if (g_array_index (garray, gint, i) != i)
769 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
771 g_array_free (garray, TRUE);
773 garray = g_array_new (FALSE, FALSE, sizeof (gint));
774 for (i = 0; i < 100; i++)
775 g_array_prepend_val (garray, i);
777 for (i = 0; i < 100; i++)
778 if (g_array_index (garray, gint, i) != (100 - i - 1))
779 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
781 g_array_free (garray, TRUE);
786 g_print ("checking strings...");
788 string1 = g_string_new ("hi pete!");
789 string2 = g_string_new ("");
791 g_assert (strcmp ("hi pete!", string1->str) == 0);
793 for (i = 0; i < 10000; i++)
794 g_string_append_c (string1, 'a'+(i%26));
797 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
798 the %10000.10000f format... */
799 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
800 "this pete guy sure is a wuss, like he's the number ",
802 " wuss. everyone agrees.\n",
804 10, 666, 15, 15, 666.666666666, 666.666666666);
806 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
807 "this pete guy sure is a wuss, like he's the number ",
809 " wuss. everyone agrees.\n",
811 10, 666, 15, 15, 666.666666666, 666.666666666);
814 g_print ("string2 length = %d...\n", string2->len);
815 string2->str[70] = '\0';
816 g_print ("first 70 chars:\n%s\n", string2->str);
817 string2->str[141] = '\0';
818 g_print ("next 70 chars:\n%s\n", string2->str+71);
819 string2->str[212] = '\0';
820 g_print ("and next 70:\n%s\n", string2->str+142);
821 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
823 g_string_free (string1, TRUE);
824 g_string_free (string2, TRUE);
827 string1 = g_string_new ("firsthalf");
828 g_string_append (string1, "lasthalf");
829 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
830 g_string_free (string1, TRUE);
834 string1 = g_string_new ("firsthalf");
835 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
836 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
837 g_string_free (string1, TRUE);
840 string1 = g_string_new ("lasthalf");
841 g_string_prepend (string1, "firsthalf");
842 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
843 g_string_free (string1, TRUE);
846 string1 = g_string_new ("lasthalf");
847 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
848 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
849 g_string_free (string1, TRUE);
852 string1 = g_string_new ("firstlast");
853 g_string_insert (string1, 5, "middle");
854 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
855 g_string_free (string1, TRUE);
857 /* insert with pos == end of the string */
858 string1 = g_string_new ("firstmiddle");
859 g_string_insert (string1, strlen ("firstmiddle"), "last");
860 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
861 g_string_free (string1, TRUE);
865 string1 = g_string_new ("firstlast");
866 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
867 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
868 g_string_free (string1, TRUE);
870 /* insert_len with magic -1 pos for append */
871 string1 = g_string_new ("first");
872 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
873 g_assert (strcmp (string1->str, "firstlast") == 0);
874 g_string_free (string1, TRUE);
876 /* insert_len with magic -1 len for strlen-the-string */
877 string1 = g_string_new ("first");
878 g_string_insert_len (string1, 5, "last", -1);
879 g_assert (strcmp (string1->str, "firstlast") == 0);
880 g_string_free (string1, TRUE);
885 string1 = g_string_new ("test");
886 string2 = g_string_new ("te");
887 g_assert (! g_string_equal(string1, string2));
888 g_string_append (string2, "st");
889 g_assert (g_string_equal(string1, string2));
890 g_string_free (string1, TRUE);
891 g_string_free (string2, TRUE);
893 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
894 string1 = g_string_new ("fiddle");
895 string2 = g_string_new ("fiddle");
896 g_assert (g_string_equal(string1, string2));
897 g_string_append_c(string1, '\0');
898 g_assert (! g_string_equal(string1, string2));
899 g_string_append_c(string2, '\0');
900 g_assert (g_string_equal(string1, string2));
901 g_string_append_c(string1, 'x');
902 g_string_append_c(string2, 'y');
903 g_assert (! g_string_equal(string1, string2));
904 g_assert (string1->len == 8);
905 g_string_append(string1, "yzzy");
906 g_assert (string1->len == 12);
907 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
908 g_string_insert(string1, 1, "QED");
909 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
910 g_string_free (string1, TRUE);
911 g_string_free (string2, TRUE);
913 g_print ("test positional printf formats (not supported): ");
914 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
915 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
916 g_print ("%s%s\n", string, tmp_string);
920 g_print ("checking timers...\n");
922 timer = g_timer_new ();
923 g_print (" spinning for 3 seconds...\n");
925 g_timer_start (timer);
926 while (g_timer_elapsed (timer, NULL) < 3)
929 g_timer_stop (timer);
930 g_timer_destroy (timer);
934 g_print ("checking g_ascii_strcasecmp...");
935 g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
936 g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
937 g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
938 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
939 g_assert (g_ascii_strcasecmp ("", "") == 0);
940 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
941 g_assert (g_ascii_strcasecmp ("a", "b") < 0);
942 g_assert (g_ascii_strcasecmp ("a", "B") < 0);
943 g_assert (g_ascii_strcasecmp ("A", "b") < 0);
944 g_assert (g_ascii_strcasecmp ("A", "B") < 0);
945 g_assert (g_ascii_strcasecmp ("b", "a") > 0);
946 g_assert (g_ascii_strcasecmp ("b", "A") > 0);
947 g_assert (g_ascii_strcasecmp ("B", "a") > 0);
948 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
952 g_print ("checking g_strdup...");
953 g_assert(g_strdup(NULL) == NULL);
954 string = g_strdup(GLIB_TEST_STRING);
955 g_assert(string != NULL);
956 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
961 g_print ("checking g_strconcat...");
962 string = g_strconcat(GLIB_TEST_STRING, NULL);
963 g_assert(string != NULL);
964 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
966 string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
967 GLIB_TEST_STRING, NULL);
968 g_assert(string != NULL);
969 g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
970 GLIB_TEST_STRING) == 0);
975 g_print("checking g_strlcpy/g_strlcat...");
976 /* The following is a torture test for strlcpy/strlcat, with lots of
977 * checking; normal users wouldn't use them this way!
979 string = g_malloc (6);
980 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
982 g_assert (g_strlcpy(string, "" , 5) == 0);
983 g_assert ( *string == '\0' );
985 g_assert (g_strlcpy(string, "abc" , 5) == 3);
986 g_assert ( *(string + 3) == '\0' );
987 g_assert (g_str_equal(string, "abc"));
988 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
989 g_assert ( *(string + 4) == '\0' );
990 g_assert ( *(string + 5) == 'Z' );
991 g_assert (g_str_equal(string, "abcd"));
992 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
993 g_assert ( *(string + 4) == '\0' );
994 g_assert ( *(string + 5) == 'Z' );
995 g_assert (g_str_equal(string, "abcd"));
996 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
997 g_assert ( *(string + 4) == '\0' );
998 g_assert ( *(string + 5) == 'Z' );
999 g_assert (g_str_equal(string, "abcd"));
1001 *(string + 1)= '\0';
1002 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1003 g_assert (*string == 'Y');
1005 g_assert (g_strlcat(string, "123" , 5) == 3);
1006 g_assert ( *(string + 3) == '\0' );
1007 g_assert (g_str_equal(string, "123"));
1008 g_assert (g_strlcat(string, "" , 5) == 3);
1009 g_assert ( *(string + 3) == '\0' );
1010 g_assert (g_str_equal(string, "123"));
1011 g_assert (g_strlcat(string, "4", 5) == 4);
1012 g_assert (g_str_equal(string, "1234"));
1013 g_assert (g_strlcat(string, "5", 5) == 5);
1014 g_assert ( *(string + 4) == '\0' );
1015 g_assert (g_str_equal(string, "1234"));
1016 g_assert ( *(string + 5) == 'Z' );
1018 *(string + 1)= '\0';
1019 g_assert (g_strlcat(string, "123" , 0) == 3);
1020 g_assert (*string == 'Y');
1022 /* A few more tests, demonstrating more "normal" use */
1023 g_assert (g_strlcpy(string, "hi", 5) == 2);
1024 g_assert (g_str_equal(string, "hi"));
1025 g_assert (g_strlcat(string, "t", 5) == 3);
1026 g_assert (g_str_equal(string, "hit"));
1032 g_print ("checking g_strdup_printf...");
1033 string = g_strdup_printf ("%05d %-5s", 21, "test");
1034 g_assert (string != NULL);
1035 g_assert (strcmp(string, "00021 test ") == 0);
1040 /* g_debug (argv[0]); */
1042 /* Relation tests */
1044 g_print ("checking relations...");
1046 relation = g_relation_new (2);
1048 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1049 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1051 for (i = 0; i < 1024; i += 1)
1054 for (i = 1; i < 1023; i += 1)
1056 g_relation_insert (relation, data + i, data + i + 1);
1057 g_relation_insert (relation, data + i, data + i - 1);
1060 for (i = 2; i < 1022; i += 1)
1062 g_assert (! g_relation_exists (relation, data + i, data + i));
1063 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1064 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1067 for (i = 1; i < 1023; i += 1)
1069 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1070 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1073 for (i = 2; i < 1022; i += 1)
1075 g_assert (g_relation_count (relation, data + i, 0) == 2);
1076 g_assert (g_relation_count (relation, data + i, 1) == 2);
1079 g_assert (g_relation_count (relation, data, 0) == 0);
1081 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1082 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1083 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1084 g_relation_delete (relation, data + 42, 0);
1085 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1086 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1087 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1089 tuples = g_relation_select (relation, data + 200, 0);
1091 g_assert (tuples->len == 2);
1094 for (i = 0; i < tuples->len; i += 1)
1097 *(gint*) g_tuples_index (tuples, i, 0),
1098 *(gint*) g_tuples_index (tuples, i, 1));
1102 g_assert (g_relation_exists (relation, data + 300, data + 301));
1103 g_relation_delete (relation, data + 300, 0);
1104 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1106 g_tuples_destroy (tuples);
1108 g_relation_destroy (relation);
1114 g_print ("checking pointer arrays...");
1116 gparray = g_ptr_array_new ();
1117 for (i = 0; i < 10000; i++)
1118 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1120 for (i = 0; i < 10000; i++)
1121 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1122 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1124 g_ptr_array_free (gparray, TRUE);
1129 g_print ("checking byte arrays...");
1131 gbarray = g_byte_array_new ();
1132 for (i = 0; i < 10000; i++)
1133 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1135 for (i = 0; i < 10000; i++)
1137 g_assert (gbarray->data[4*i] == 'a');
1138 g_assert (gbarray->data[4*i+1] == 'b');
1139 g_assert (gbarray->data[4*i+2] == 'c');
1140 g_assert (gbarray->data[4*i+3] == 'd');
1143 g_byte_array_free (gbarray, TRUE);
1146 g_printerr ("g_log tests:");
1147 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1148 g_message ("the next warning is a test:");
1152 g_print ("checking endian macros (host is ");
1153 #if G_BYTE_ORDER == G_BIG_ENDIAN
1154 g_print ("big endian)...");
1156 g_print ("little endian)...");
1158 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
1159 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
1160 #ifdef G_HAVE_GINT64
1161 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
1166 #ifdef G_PLATFORM_WIN32
1167 g_print ("current locale: %s\n", g_win32_getlocale ());
1169 g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
1171 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
1172 g_print ("Ditto, or from GLib DLL name: %s\n",
1173 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
1174 g_print ("Ditto, only from GLib DLL name: %s\n",
1175 g_win32_get_package_installation_directory (NULL, glib_dll));
1176 g_print ("locale subdirectory of GLib installation directory: %s\n",
1177 g_win32_get_package_installation_subdirectory (NULL, glib_dll, "locale"));
1178 g_print ("GTK+ 2.0 installation directory, if available: %s\n",
1179 g_win32_get_package_installation_directory ("gtk20", NULL));
1181 g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
1182 g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
1186 g_print ("checking file functions...\n");
1188 strcpy (template, "foobar");
1189 fd = g_mkstemp (template);
1191 g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
1193 strcpy (template, "fooXXXXXX");
1194 fd = g_mkstemp (template);
1196 g_print ("g_mkstemp didn't work for template %s\n", template);
1197 i = write (fd, hello, hellolen);
1199 g_print ("write() failed: %s\n", g_strerror (errno));
1200 else if (i != hellolen)
1201 g_print ("write() should have written %d bytes, wrote %d\n", hellolen, i);
1204 i = read (fd, chars, sizeof (chars));
1206 g_print ("read() failed: %s\n", g_strerror (errno));
1207 else if (i != hellolen)
1208 g_print ("read() should have read %d bytes, got %d\n", hellolen, i);
1211 if (strcmp (chars, hello) != 0)
1212 g_print ("wrote '%s', but got '%s'\n", hello, chars);
1218 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1219 fd = g_file_open_tmp (template, &name_used, &error);
1221 g_print ("g_file_open_tmp works even if template contains '%s'\n",
1224 g_print ("g_file_open_tmp correctly returns error: %s\n",
1227 g_clear_error (&error);
1229 strcpy (template, "zapXXXXXX");
1230 fd = g_file_open_tmp (template, &name_used, &error);
1232 g_print ("g_file_open_tmp didn't work for template '%s': %s\n",
1233 template, error->message);
1235 g_print ("g_file_open_tmp for template '%s' used name '%s'\n",
1236 template, name_used);
1238 g_clear_error (&error);
1241 fd = g_file_open_tmp (NULL, &name_used, &error);
1243 g_print ("g_file_open_tmp didn't work for a NULL template: %s\n",
1246 g_print ("g_file_open_tmp for NULL template used name '%s'\n",
1249 g_clear_error (&error);