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
47 #include <io.h> /* For read(), write() etc */
50 static int array[10000];
51 static gboolean failed = FALSE;
53 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
56 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
58 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
61 g_print ("."); fflush (stdout); \
64 #define C2P(c) ((gpointer) ((long) (c)))
65 #define P2C(p) ((gchar) ((long) (p)))
67 #define GLIB_TEST_STRING "el dorado "
68 #define GLIB_TEST_STRING_5 "el do"
71 node_build_string (GNode *node,
78 c[0] = P2C (node->data);
80 string = g_strconcat (*p ? *p : "", c, NULL);
97 gchar *tstring, *cstring;
99 g_print ("checking n-way trees: ");
102 root = g_node_new (C2P ('A'));
103 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
105 node_B = g_node_new (C2P ('B'));
106 g_node_append (root, node_B);
107 TEST (NULL, root->children == node_B);
109 g_node_append_data (node_B, C2P ('E'));
110 g_node_prepend_data (node_B, C2P ('C'));
111 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
113 node_F = g_node_new (C2P ('F'));
114 g_node_append (root, node_F);
115 TEST (NULL, root->children->next == node_F);
117 node_G = g_node_new (C2P ('G'));
118 g_node_append (node_F, node_G);
119 node_J = g_node_new (C2P ('J'));
120 g_node_prepend (node_G, node_J);
121 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
122 g_node_insert_data (node_G, 0, C2P ('H'));
123 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
125 TEST (NULL, g_node_depth (root) == 1);
126 TEST (NULL, g_node_max_height (root) == 4);
127 TEST (NULL, g_node_depth (node_G->children->next) == 4);
128 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
129 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
130 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
131 TEST (NULL, g_node_max_height (node_F) == 3);
132 TEST (NULL, g_node_n_children (node_G) == 4);
133 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
134 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
135 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
137 for (i = 0; i < g_node_n_children (node_B); i++)
139 node = g_node_nth_child (node_B, i);
140 TEST (NULL, P2C (node->data) == ('C' + i));
143 for (i = 0; i < g_node_n_children (node_G); i++)
144 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
154 * for in-order traversal, 'G' is considered to be the "left"
155 * child of 'F', which will cause 'F' to be the last node visited.
159 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
160 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
161 g_free (tstring); tstring = NULL;
162 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
163 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
164 g_free (tstring); tstring = NULL;
165 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
166 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
167 g_free (tstring); tstring = NULL;
168 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
169 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
170 g_free (tstring); tstring = NULL;
172 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
173 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
174 g_free (tstring); tstring = NULL;
175 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
176 TEST (tstring, strcmp (tstring, "ABFG") == 0);
177 g_free (tstring); tstring = NULL;
179 g_node_reverse_children (node_B);
180 g_node_reverse_children (node_G);
182 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
183 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
184 g_free (tstring); tstring = NULL;
187 node = g_node_copy (root);
188 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
189 TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
190 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
191 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
192 TEST (cstring, strcmp (tstring, cstring) == 0);
193 g_free (tstring); tstring = NULL;
194 g_free (cstring); cstring = NULL;
195 g_node_destroy (node);
197 g_node_destroy (root);
199 /* allocation tests */
201 root = g_node_new (NULL);
204 for (i = 0; i < 2048; i++)
206 g_node_append (node, g_node_new (NULL));
208 node = node->children->next;
210 TEST (NULL, g_node_max_height (root) > 100);
211 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
213 g_node_destroy (root);
220 my_hash_callback_remove (gpointer key,
233 my_hash_callback_remove_test (gpointer key,
244 my_hash_callback (gpointer key,
253 my_hash (gconstpointer key)
255 return (guint) *((const gint*) key);
259 my_hash_equal (gconstpointer a,
262 return *((const gint*) a) == *((const gint*) b);
266 my_list_compare_one (gconstpointer a, gconstpointer b)
268 gint one = *((const gint*)a);
269 gint two = *((const gint*)b);
274 my_list_compare_two (gconstpointer a, gconstpointer b)
276 gint one = *((const gint*)a);
277 gint two = *((const gint*)b);
282 my_list_print (gpointer a, gpointer b)
284 gint three = *((gint*)a);
285 g_print("%d", three);
289 my_compare (gconstpointer a,
299 my_traverse (gpointer key,
304 g_print ("%c ", *ch);
314 GHashTable *hash_table;
315 GMemChunk *mem_chunk;
316 GStringChunk *string_chunk;
318 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
319 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
322 gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
327 GString *string1, *string2;
336 } dirname_checks[] = {
348 { ".\\\\\\\\", "." },
350 { "..\\\\\\\\", ".." },
352 { "a\\b\\", "a\\b" },
356 { "//server/share///x", "//server/share" },
362 guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
367 } skip_root_checks[] = {
377 { "\\\\server\\foo", "" },
378 { "\\\\server\\foo\\bar", "bar" },
382 { "//server/share///x", "//x" },
387 guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
389 #ifndef G_DISABLE_ASSERT
390 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
391 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
393 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
394 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
397 const char hello[] = "Hello, World";
398 const int hellolen = sizeof (hello) - 1;
404 gchar *glib_dll = g_strdup_printf ("libglib-%d.%d-%d.dll",
407 GLIB_MICRO_VERSION - GLIB_BINARY_AGE);
410 gchar *glib_dll = g_strdup_printf ("cygglib-%d.%d-%d.dll",
413 GLIB_MICRO_VERSION - GLIB_BINARY_AGE);
416 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
423 string = g_get_current_dir ();
424 g_print ("cwd: %s\n", string);
426 g_print ("user: %s\n", g_get_user_name ());
427 g_print ("real: %s\n", g_get_real_name ());
428 g_print ("home: %s\n", g_get_home_dir ());
429 g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
432 g_print ("checking size of gint8: %d", (int)sizeof (gint8));
433 TEST (NULL, sizeof (gint8) == 1);
434 g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
435 TEST (NULL, sizeof (gint16) == 2);
436 g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
437 TEST (NULL, sizeof (gint32) == 4);
438 g_print ("\nchecking size of gsize: %d", (int)sizeof (gsize));
440 g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
441 TEST (NULL, sizeof (gint64) == 8);
442 #endif /* G_HAVE_GINT64 */
445 g_print ("checking g_path_get_basename()...");
446 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
447 g_assert (strcmp (string, "dir") == 0);
449 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
450 g_assert (strcmp (string, "file") == 0);
454 g_print ("checking g_path_get_dirname()...");
455 for (i = 0; i < n_dirname_checks; i++)
459 dirname = g_path_get_dirname (dirname_checks[i].filename);
460 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
462 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
463 dirname_checks[i].filename,
464 dirname_checks[i].dirname,
466 n_dirname_checks = 0;
470 if (n_dirname_checks)
473 g_print ("checking g_path_skip_root()...");
474 for (i = 0; i < n_skip_root_checks; i++)
476 const gchar *skipped;
478 skipped = g_path_skip_root (skip_root_checks[i].filename);
479 if ((skipped && !skip_root_checks[i].without_root) ||
480 (!skipped && skip_root_checks[i].without_root) ||
481 ((skipped && skip_root_checks[i].without_root) &&
482 strcmp (skipped, skip_root_checks[i].without_root)))
484 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
485 skip_root_checks[i].filename,
486 (skip_root_checks[i].without_root ?
487 skip_root_checks[i].without_root : "<NULL>"),
488 (skipped ? skipped : "<NULL>"));
489 n_skip_root_checks = 0;
492 if (n_skip_root_checks)
495 g_print ("checking doubly linked lists...");
498 for (i = 0; i < 10; i++)
499 list = g_list_append (list, &nums[i]);
500 list = g_list_reverse (list);
502 for (i = 0; i < 10; i++)
504 t = g_list_nth (list, i);
505 if (*((gint*) t->data) != (9 - i))
506 g_error ("Regular insert failed");
509 for (i = 0; i < 10; i++)
510 if(g_list_position(list, g_list_nth (list, i)) != i)
511 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
516 for (i = 0; i < 10; i++)
517 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
521 g_list_foreach (list, my_list_print, NULL);
524 for (i = 0; i < 10; i++)
526 t = g_list_nth (list, i);
527 if (*((gint*) t->data) != i)
528 g_error ("Sorted insert failed");
534 for (i = 0; i < 10; i++)
535 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
539 g_list_foreach (list, my_list_print, NULL);
542 for (i = 0; i < 10; i++)
544 t = g_list_nth (list, i);
545 if (*((gint*) t->data) != (9 - i))
546 g_error ("Sorted insert failed");
552 for (i = 0; i < 10; i++)
553 list = g_list_prepend (list, &morenums[i]);
555 list = g_list_sort (list, my_list_compare_two);
559 g_list_foreach (list, my_list_print, NULL);
562 for (i = 0; i < 10; i++)
564 t = g_list_nth (list, i);
565 if (*((gint*) t->data) != (9 - i))
566 g_error ("Merge sort failed");
574 g_print ("checking singly linked lists...");
577 for (i = 0; i < 10; i++)
578 slist = g_slist_append (slist, &nums[i]);
579 slist = g_slist_reverse (slist);
581 for (i = 0; i < 10; i++)
583 st = g_slist_nth (slist, i);
584 if (*((gint*) st->data) != (9 - i))
588 g_slist_free (slist);
591 for (i = 0; i < 10; i++)
592 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
596 g_slist_foreach (slist, my_list_print, NULL);
599 for (i = 0; i < 10; i++)
601 st = g_slist_nth (slist, i);
602 if (*((gint*) st->data) != i)
603 g_error ("Sorted insert failed");
609 for (i = 0; i < 10; i++)
610 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
614 g_slist_foreach (slist, my_list_print, NULL);
617 for (i = 0; i < 10; i++)
619 st = g_slist_nth (slist, i);
620 if (*((gint*) st->data) != (9 - i))
621 g_error("Sorted insert failed");
627 for (i = 0; i < 10; i++)
628 slist = g_slist_prepend (slist, &morenums[i]);
630 slist = g_slist_sort (slist, my_list_compare_two);
634 g_slist_foreach (slist, my_list_print, NULL);
637 for (i = 0; i < 10; i++)
639 st = g_slist_nth (slist, i);
640 if (*((gint*) st->data) != (9 - i))
641 g_error("Sorted insert failed");
649 g_print ("checking binary trees...\n");
651 tree = g_tree_new (my_compare);
653 for (j = 0; j < 10; j++, i++)
656 g_tree_insert (tree, &chars[i], &chars[i]);
658 for (j = 0; j < 26; j++, i++)
661 g_tree_insert (tree, &chars[i], &chars[i]);
663 for (j = 0; j < 26; j++, i++)
666 g_tree_insert (tree, &chars[i], &chars[i]);
669 g_print ("tree height: %d\n", g_tree_height (tree));
670 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
673 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
676 for (i = 0; i < 10; i++)
677 g_tree_remove (tree, &chars[i]);
679 g_print ("tree height: %d\n", g_tree_height (tree));
680 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
683 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
689 /* check n-way trees */
692 g_print ("checking mem chunks...");
694 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
696 for (i = 0; i < 10000; i++)
698 mem[i] = g_chunk_new (gchar, mem_chunk);
700 for (j = 0; j < 50; j++)
704 for (i = 0; i < 10000; i++)
706 g_mem_chunk_free (mem_chunk, mem[i]);
712 g_print ("checking hash tables...");
714 hash_table = g_hash_table_new (my_hash, my_hash_equal);
715 for (i = 0; i < 10000; i++)
718 g_hash_table_insert (hash_table, &array[i], &array[i]);
720 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
722 for (i = 0; i < 10000; i++)
726 for (i = 0; i < 10000; i++)
727 g_hash_table_remove (hash_table, &array[i]);
729 for (i = 0; i < 10000; i++)
732 g_hash_table_insert (hash_table, &array[i], &array[i]);
735 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
736 g_hash_table_size (hash_table) != 5000)
739 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
742 g_hash_table_destroy (hash_table);
747 g_print ("checking string chunks...");
749 string_chunk = g_string_chunk_new (1024);
751 for (i = 0; i < 100000; i ++)
753 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
755 if (strcmp ("hi pete", tmp_string) != 0)
756 g_error ("string chunks are broken.\n");
759 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
761 g_assert (tmp_string_2 != tmp_string &&
762 strcmp(tmp_string_2, tmp_string) == 0);
764 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
766 g_assert (tmp_string_2 == tmp_string);
768 g_string_chunk_free (string_chunk);
773 g_print ("checking arrays...");
775 garray = g_array_new (FALSE, FALSE, sizeof (gint));
776 for (i = 0; i < 10000; i++)
777 g_array_append_val (garray, i);
779 for (i = 0; i < 10000; i++)
780 if (g_array_index (garray, gint, i) != i)
781 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
783 g_array_free (garray, TRUE);
785 garray = g_array_new (FALSE, FALSE, sizeof (gint));
786 for (i = 0; i < 100; i++)
787 g_array_prepend_val (garray, i);
789 for (i = 0; i < 100; i++)
790 if (g_array_index (garray, gint, i) != (100 - i - 1))
791 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
793 g_array_free (garray, TRUE);
798 g_print ("checking strings...");
800 string1 = g_string_new ("hi pete!");
801 string2 = g_string_new ("");
803 g_assert (strcmp ("hi pete!", string1->str) == 0);
805 for (i = 0; i < 10000; i++)
806 g_string_append_c (string1, 'a'+(i%26));
809 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
810 the %10000.10000f format... */
811 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
812 "this pete guy sure is a wuss, like he's the number ",
814 " wuss. everyone agrees.\n",
816 10, 666, 15, 15, 666.666666666, 666.666666666);
818 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
819 "this pete guy sure is a wuss, like he's the number ",
821 " wuss. everyone agrees.\n",
823 10, 666, 15, 15, 666.666666666, 666.666666666);
826 g_print ("string2 length = %lu...\n", (gulong)string2->len);
827 string2->str[70] = '\0';
828 g_print ("first 70 chars:\n%s\n", string2->str);
829 string2->str[141] = '\0';
830 g_print ("next 70 chars:\n%s\n", string2->str+71);
831 string2->str[212] = '\0';
832 g_print ("and next 70:\n%s\n", string2->str+142);
833 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
835 g_string_free (string1, TRUE);
836 g_string_free (string2, TRUE);
839 string1 = g_string_new ("firsthalf");
840 g_string_append (string1, "lasthalf");
841 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
842 g_string_free (string1, TRUE);
846 string1 = g_string_new ("firsthalf");
847 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
848 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
849 g_string_free (string1, TRUE);
852 string1 = g_string_new ("lasthalf");
853 g_string_prepend (string1, "firsthalf");
854 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
855 g_string_free (string1, TRUE);
858 string1 = g_string_new ("lasthalf");
859 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
860 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
861 g_string_free (string1, TRUE);
864 string1 = g_string_new ("firstlast");
865 g_string_insert (string1, 5, "middle");
866 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
867 g_string_free (string1, TRUE);
869 /* insert with pos == end of the string */
870 string1 = g_string_new ("firstmiddle");
871 g_string_insert (string1, strlen ("firstmiddle"), "last");
872 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
873 g_string_free (string1, TRUE);
877 string1 = g_string_new ("firstlast");
878 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
879 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
880 g_string_free (string1, TRUE);
882 /* insert_len with magic -1 pos for append */
883 string1 = g_string_new ("first");
884 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
885 g_assert (strcmp (string1->str, "firstlast") == 0);
886 g_string_free (string1, TRUE);
888 /* insert_len with magic -1 len for strlen-the-string */
889 string1 = g_string_new ("first");
890 g_string_insert_len (string1, 5, "last", -1);
891 g_assert (strcmp (string1->str, "firstlast") == 0);
892 g_string_free (string1, TRUE);
897 string1 = g_string_new ("test");
898 string2 = g_string_new ("te");
899 g_assert (! g_string_equal(string1, string2));
900 g_string_append (string2, "st");
901 g_assert (g_string_equal(string1, string2));
902 g_string_free (string1, TRUE);
903 g_string_free (string2, TRUE);
905 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
906 string1 = g_string_new ("fiddle");
907 string2 = g_string_new ("fiddle");
908 g_assert (g_string_equal(string1, string2));
909 g_string_append_c(string1, '\0');
910 g_assert (! g_string_equal(string1, string2));
911 g_string_append_c(string2, '\0');
912 g_assert (g_string_equal(string1, string2));
913 g_string_append_c(string1, 'x');
914 g_string_append_c(string2, 'y');
915 g_assert (! g_string_equal(string1, string2));
916 g_assert (string1->len == 8);
917 g_string_append(string1, "yzzy");
918 g_assert (string1->len == 12);
919 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
920 g_string_insert(string1, 1, "QED");
921 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
922 g_string_free (string1, TRUE);
923 g_string_free (string2, TRUE);
925 g_print ("test positional printf formats (not supported): ");
926 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
927 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
928 g_print ("%s%s\n", string, tmp_string);
932 g_print ("checking timers...\n");
934 timer = g_timer_new ();
935 g_print (" spinning for 3 seconds...\n");
937 g_timer_start (timer);
938 while (g_timer_elapsed (timer, NULL) < 3)
941 g_timer_stop (timer);
942 g_timer_destroy (timer);
946 g_print ("checking g_ascii_strcasecmp...");
947 g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
948 g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
949 g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
950 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
951 g_assert (g_ascii_strcasecmp ("", "") == 0);
952 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
953 g_assert (g_ascii_strcasecmp ("a", "b") < 0);
954 g_assert (g_ascii_strcasecmp ("a", "B") < 0);
955 g_assert (g_ascii_strcasecmp ("A", "b") < 0);
956 g_assert (g_ascii_strcasecmp ("A", "B") < 0);
957 g_assert (g_ascii_strcasecmp ("b", "a") > 0);
958 g_assert (g_ascii_strcasecmp ("b", "A") > 0);
959 g_assert (g_ascii_strcasecmp ("B", "a") > 0);
960 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
964 g_print ("checking g_strdup...");
965 g_assert(g_strdup(NULL) == NULL);
966 string = g_strdup(GLIB_TEST_STRING);
967 g_assert(string != NULL);
968 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
973 g_print ("checking g_strconcat...");
974 string = g_strconcat(GLIB_TEST_STRING, NULL);
975 g_assert(string != NULL);
976 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
978 string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
979 GLIB_TEST_STRING, NULL);
980 g_assert(string != NULL);
981 g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
982 GLIB_TEST_STRING) == 0);
987 g_print("checking g_strlcpy/g_strlcat...");
988 /* The following is a torture test for strlcpy/strlcat, with lots of
989 * checking; normal users wouldn't use them this way!
991 string = g_malloc (6);
992 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
994 g_assert (g_strlcpy(string, "" , 5) == 0);
995 g_assert ( *string == '\0' );
997 g_assert (g_strlcpy(string, "abc" , 5) == 3);
998 g_assert ( *(string + 3) == '\0' );
999 g_assert (g_str_equal(string, "abc"));
1000 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1001 g_assert ( *(string + 4) == '\0' );
1002 g_assert ( *(string + 5) == 'Z' );
1003 g_assert (g_str_equal(string, "abcd"));
1004 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1005 g_assert ( *(string + 4) == '\0' );
1006 g_assert ( *(string + 5) == 'Z' );
1007 g_assert (g_str_equal(string, "abcd"));
1008 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1009 g_assert ( *(string + 4) == '\0' );
1010 g_assert ( *(string + 5) == 'Z' );
1011 g_assert (g_str_equal(string, "abcd"));
1013 *(string + 1)= '\0';
1014 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1015 g_assert (*string == 'Y');
1017 g_assert (g_strlcat(string, "123" , 5) == 3);
1018 g_assert ( *(string + 3) == '\0' );
1019 g_assert (g_str_equal(string, "123"));
1020 g_assert (g_strlcat(string, "" , 5) == 3);
1021 g_assert ( *(string + 3) == '\0' );
1022 g_assert (g_str_equal(string, "123"));
1023 g_assert (g_strlcat(string, "4", 5) == 4);
1024 g_assert (g_str_equal(string, "1234"));
1025 g_assert (g_strlcat(string, "5", 5) == 5);
1026 g_assert ( *(string + 4) == '\0' );
1027 g_assert (g_str_equal(string, "1234"));
1028 g_assert ( *(string + 5) == 'Z' );
1030 *(string + 1)= '\0';
1031 g_assert (g_strlcat(string, "123" , 0) == 3);
1032 g_assert (*string == 'Y');
1034 /* A few more tests, demonstrating more "normal" use */
1035 g_assert (g_strlcpy(string, "hi", 5) == 2);
1036 g_assert (g_str_equal(string, "hi"));
1037 g_assert (g_strlcat(string, "t", 5) == 3);
1038 g_assert (g_str_equal(string, "hit"));
1044 g_print ("checking g_strdup_printf...");
1045 string = g_strdup_printf ("%05d %-5s", 21, "test");
1046 g_assert (string != NULL);
1047 g_assert (strcmp(string, "00021 test ") == 0);
1052 /* g_debug (argv[0]); */
1054 /* Relation tests */
1056 g_print ("checking relations...");
1058 relation = g_relation_new (2);
1060 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1061 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1063 for (i = 0; i < 1024; i += 1)
1066 for (i = 1; i < 1023; i += 1)
1068 g_relation_insert (relation, data + i, data + i + 1);
1069 g_relation_insert (relation, data + i, data + i - 1);
1072 for (i = 2; i < 1022; i += 1)
1074 g_assert (! g_relation_exists (relation, data + i, data + i));
1075 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1076 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1079 for (i = 1; i < 1023; i += 1)
1081 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1082 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1085 for (i = 2; i < 1022; i += 1)
1087 g_assert (g_relation_count (relation, data + i, 0) == 2);
1088 g_assert (g_relation_count (relation, data + i, 1) == 2);
1091 g_assert (g_relation_count (relation, data, 0) == 0);
1093 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1094 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1095 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1096 g_relation_delete (relation, data + 42, 0);
1097 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1098 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1099 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1101 tuples = g_relation_select (relation, data + 200, 0);
1103 g_assert (tuples->len == 2);
1106 for (i = 0; i < tuples->len; i += 1)
1109 *(gint*) g_tuples_index (tuples, i, 0),
1110 *(gint*) g_tuples_index (tuples, i, 1));
1114 g_assert (g_relation_exists (relation, data + 300, data + 301));
1115 g_relation_delete (relation, data + 300, 0);
1116 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1118 g_tuples_destroy (tuples);
1120 g_relation_destroy (relation);
1126 g_print ("checking pointer arrays...");
1128 gparray = g_ptr_array_new ();
1129 for (i = 0; i < 10000; i++)
1130 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1132 for (i = 0; i < 10000; i++)
1133 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1134 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1136 g_ptr_array_free (gparray, TRUE);
1141 g_print ("checking byte arrays...");
1143 gbarray = g_byte_array_new ();
1144 for (i = 0; i < 10000; i++)
1145 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1147 for (i = 0; i < 10000; i++)
1149 g_assert (gbarray->data[4*i] == 'a');
1150 g_assert (gbarray->data[4*i+1] == 'b');
1151 g_assert (gbarray->data[4*i+2] == 'c');
1152 g_assert (gbarray->data[4*i+3] == 'd');
1155 g_byte_array_free (gbarray, TRUE);
1158 g_printerr ("g_log tests:");
1159 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1160 g_message ("the next warning is a test:");
1164 g_print ("checking endian macros (host is ");
1165 #if G_BYTE_ORDER == G_BIG_ENDIAN
1166 g_print ("big endian)...");
1168 g_print ("little endian)...");
1170 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
1171 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
1172 #ifdef G_HAVE_GINT64
1173 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
1178 #ifdef G_PLATFORM_WIN32
1179 g_print ("current locale: %s\n", g_win32_getlocale ());
1180 g_print ("GLib DLL name tested for: %s\n", glib_dll);
1182 g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
1184 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
1185 g_print ("Ditto, or from GLib DLL name: %s\n",
1186 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
1187 g_print ("Ditto, only from GLib DLL name: %s\n",
1188 g_win32_get_package_installation_directory (NULL, glib_dll));
1189 g_print ("locale subdirectory of GLib installation directory: %s\n",
1190 g_win32_get_package_installation_subdirectory (NULL, glib_dll, "lib\\locale"));
1191 g_print ("GTK+ 2.0 installation directory, if available: %s\n",
1192 g_win32_get_package_installation_directory ("gtk20", NULL));
1194 g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
1195 g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
1199 g_print ("checking file functions...\n");
1201 strcpy (template, "foobar");
1202 fd = g_mkstemp (template);
1204 g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
1206 strcpy (template, "fooXXXXXX");
1207 fd = g_mkstemp (template);
1209 g_print ("g_mkstemp didn't work for template %s\n", template);
1210 i = write (fd, hello, hellolen);
1212 g_print ("write() failed: %s\n", g_strerror (errno));
1213 else if (i != hellolen)
1214 g_print ("write() should have written %d bytes, wrote %d\n", hellolen, i);
1217 i = read (fd, chars, sizeof (chars));
1219 g_print ("read() failed: %s\n", g_strerror (errno));
1220 else if (i != hellolen)
1221 g_print ("read() should have read %d bytes, got %d\n", hellolen, i);
1224 if (strcmp (chars, hello) != 0)
1225 g_print ("wrote '%s', but got '%s'\n", hello, chars);
1231 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1232 fd = g_file_open_tmp (template, &name_used, &error);
1234 g_print ("g_file_open_tmp works even if template contains '%s'\n",
1237 g_print ("g_file_open_tmp correctly returns error: %s\n",
1240 g_clear_error (&error);
1242 strcpy (template, "zapXXXXXX");
1243 fd = g_file_open_tmp (template, &name_used, &error);
1245 g_print ("g_file_open_tmp didn't work for template '%s': %s\n",
1246 template, error->message);
1248 g_print ("g_file_open_tmp for template '%s' used name '%s'\n",
1249 template, name_used);
1251 g_clear_error (&error);
1254 fd = g_file_open_tmp (NULL, &name_used, &error);
1256 g_print ("g_file_open_tmp didn't work for a NULL template: %s\n",
1259 g_print ("g_file_open_tmp for NULL template used name '%s'\n",
1262 g_clear_error (&error);