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 /* We write (m ? m : "") even in the m != NULL case to suppress a warning with GCC-3.1
55 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
58 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
60 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)(m ? m : "")); \
63 g_print ("."); fflush (stdout); \
66 #define C2P(c) ((gpointer) ((long) (c)))
67 #define P2C(p) ((gchar) ((long) (p)))
69 #define GLIB_TEST_STRING "el dorado "
70 #define GLIB_TEST_STRING_5 "el do"
73 node_build_string (GNode *node,
80 c[0] = P2C (node->data);
82 string = g_strconcat (*p ? *p : "", c, NULL);
99 gchar *tstring, *cstring;
101 g_print ("checking n-way trees: ");
104 root = g_node_new (C2P ('A'));
105 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
107 node_B = g_node_new (C2P ('B'));
108 g_node_append (root, node_B);
109 TEST (NULL, root->children == node_B);
111 g_node_append_data (node_B, C2P ('E'));
112 g_node_prepend_data (node_B, C2P ('C'));
113 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
115 node_F = g_node_new (C2P ('F'));
116 g_node_append (root, node_F);
117 TEST (NULL, root->children->next == node_F);
119 node_G = g_node_new (C2P ('G'));
120 g_node_append (node_F, node_G);
121 node_J = g_node_new (C2P ('J'));
122 g_node_prepend (node_G, node_J);
123 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
124 g_node_insert_data (node_G, 0, C2P ('H'));
125 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
127 TEST (NULL, g_node_depth (root) == 1);
128 TEST (NULL, g_node_max_height (root) == 4);
129 TEST (NULL, g_node_depth (node_G->children->next) == 4);
130 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
131 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
132 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
133 TEST (NULL, g_node_max_height (node_F) == 3);
134 TEST (NULL, g_node_n_children (node_G) == 4);
135 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
136 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
137 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
139 for (i = 0; i < g_node_n_children (node_B); i++)
141 node = g_node_nth_child (node_B, i);
142 TEST (NULL, P2C (node->data) == ('C' + i));
145 for (i = 0; i < g_node_n_children (node_G); i++)
146 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
156 * for in-order traversal, 'G' is considered to be the "left"
157 * child of 'F', which will cause 'F' to be the last node visited.
161 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
162 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
163 g_free (tstring); tstring = NULL;
164 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
165 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
166 g_free (tstring); tstring = NULL;
167 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
168 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
169 g_free (tstring); tstring = NULL;
170 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
171 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
172 g_free (tstring); tstring = NULL;
174 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
175 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
176 g_free (tstring); tstring = NULL;
177 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
178 TEST (tstring, strcmp (tstring, "ABFG") == 0);
179 g_free (tstring); tstring = NULL;
181 g_node_reverse_children (node_B);
182 g_node_reverse_children (node_G);
184 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
185 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
186 g_free (tstring); tstring = NULL;
189 node = g_node_copy (root);
190 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
191 TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
192 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
193 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
194 TEST (cstring, strcmp (tstring, cstring) == 0);
195 g_free (tstring); tstring = NULL;
196 g_free (cstring); cstring = NULL;
197 g_node_destroy (node);
199 g_node_destroy (root);
201 /* allocation tests */
203 root = g_node_new (NULL);
206 for (i = 0; i < 2048; i++)
208 g_node_append (node, g_node_new (NULL));
210 node = node->children->next;
212 TEST (NULL, g_node_max_height (root) > 100);
213 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
215 g_node_destroy (root);
222 my_hash_callback_remove (gpointer key,
235 my_hash_callback_remove_test (gpointer key,
246 my_hash_callback (gpointer key,
255 my_hash (gconstpointer key)
257 return (guint) *((const gint*) key);
261 my_hash_equal (gconstpointer a,
264 return *((const gint*) a) == *((const gint*) b);
268 my_list_compare_one (gconstpointer a, gconstpointer b)
270 gint one = *((const gint*)a);
271 gint two = *((const gint*)b);
276 my_list_compare_two (gconstpointer a, gconstpointer b)
278 gint one = *((const gint*)a);
279 gint two = *((const gint*)b);
284 my_list_print (gpointer a, gpointer b)
286 gint three = *((gint*)a);
287 g_print("%d", three);
291 my_compare (gconstpointer a,
301 my_traverse (gpointer key,
306 g_print ("%c ", *ch);
316 GHashTable *hash_table;
317 GMemChunk *mem_chunk;
318 GStringChunk *string_chunk;
320 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
321 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
324 gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
329 GString *string1, *string2;
338 } dirname_checks[] = {
350 { ".\\\\\\\\", "." },
352 { "..\\\\\\\\", ".." },
354 { "a\\b\\", "a\\b" },
358 { "//server/share///x", "//server/share" },
364 guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
369 } skip_root_checks[] = {
379 { "\\\\server\\foo", "" },
380 { "\\\\server\\foo\\bar", "bar" },
384 { "//server/share///x", "//x" },
389 guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
391 #ifndef G_DISABLE_ASSERT
392 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
393 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
394 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
395 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));
439 g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
440 TEST (NULL, sizeof (gint64) == 8);
443 g_print ("checking g_path_get_basename()...");
444 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
445 g_assert (strcmp (string, "dir") == 0);
447 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
448 g_assert (strcmp (string, "file") == 0);
452 g_print ("checking g_path_get_dirname()...");
453 for (i = 0; i < n_dirname_checks; i++)
457 dirname = g_path_get_dirname (dirname_checks[i].filename);
458 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
460 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
461 dirname_checks[i].filename,
462 dirname_checks[i].dirname,
464 n_dirname_checks = 0;
468 if (n_dirname_checks)
471 g_print ("checking g_path_skip_root()...");
472 for (i = 0; i < n_skip_root_checks; i++)
474 const gchar *skipped;
476 skipped = g_path_skip_root (skip_root_checks[i].filename);
477 if ((skipped && !skip_root_checks[i].without_root) ||
478 (!skipped && skip_root_checks[i].without_root) ||
479 ((skipped && skip_root_checks[i].without_root) &&
480 strcmp (skipped, skip_root_checks[i].without_root)))
482 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
483 skip_root_checks[i].filename,
484 (skip_root_checks[i].without_root ?
485 skip_root_checks[i].without_root : "<NULL>"),
486 (skipped ? skipped : "<NULL>"));
487 n_skip_root_checks = 0;
490 if (n_skip_root_checks)
493 g_print ("checking doubly linked lists...");
496 for (i = 0; i < 10; i++)
497 list = g_list_append (list, &nums[i]);
498 list = g_list_reverse (list);
500 for (i = 0; i < 10; i++)
502 t = g_list_nth (list, i);
503 if (*((gint*) t->data) != (9 - i))
504 g_error ("Regular insert failed");
507 for (i = 0; i < 10; i++)
508 if(g_list_position(list, g_list_nth (list, i)) != i)
509 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
514 for (i = 0; i < 10; i++)
515 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
519 g_list_foreach (list, my_list_print, NULL);
522 for (i = 0; i < 10; i++)
524 t = g_list_nth (list, i);
525 if (*((gint*) t->data) != i)
526 g_error ("Sorted insert failed");
532 for (i = 0; i < 10; i++)
533 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
537 g_list_foreach (list, my_list_print, NULL);
540 for (i = 0; i < 10; i++)
542 t = g_list_nth (list, i);
543 if (*((gint*) t->data) != (9 - i))
544 g_error ("Sorted insert failed");
550 for (i = 0; i < 10; i++)
551 list = g_list_prepend (list, &morenums[i]);
553 list = g_list_sort (list, my_list_compare_two);
557 g_list_foreach (list, my_list_print, NULL);
560 for (i = 0; i < 10; i++)
562 t = g_list_nth (list, i);
563 if (*((gint*) t->data) != (9 - i))
564 g_error ("Merge sort failed");
572 g_print ("checking singly linked lists...");
575 for (i = 0; i < 10; i++)
576 slist = g_slist_append (slist, &nums[i]);
577 slist = g_slist_reverse (slist);
579 for (i = 0; i < 10; i++)
581 st = g_slist_nth (slist, i);
582 if (*((gint*) st->data) != (9 - i))
586 g_slist_free (slist);
589 for (i = 0; i < 10; i++)
590 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
594 g_slist_foreach (slist, my_list_print, NULL);
597 for (i = 0; i < 10; i++)
599 st = g_slist_nth (slist, i);
600 if (*((gint*) st->data) != i)
601 g_error ("Sorted insert failed");
607 for (i = 0; i < 10; i++)
608 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
612 g_slist_foreach (slist, my_list_print, NULL);
615 for (i = 0; i < 10; i++)
617 st = g_slist_nth (slist, i);
618 if (*((gint*) st->data) != (9 - i))
619 g_error("Sorted insert failed");
625 for (i = 0; i < 10; i++)
626 slist = g_slist_prepend (slist, &morenums[i]);
628 slist = g_slist_sort (slist, my_list_compare_two);
632 g_slist_foreach (slist, my_list_print, NULL);
635 for (i = 0; i < 10; i++)
637 st = g_slist_nth (slist, i);
638 if (*((gint*) st->data) != (9 - i))
639 g_error("Sorted insert failed");
647 g_print ("checking binary trees...\n");
649 tree = g_tree_new (my_compare);
651 for (j = 0; j < 10; j++, i++)
654 g_tree_insert (tree, &chars[i], &chars[i]);
656 for (j = 0; j < 26; j++, i++)
659 g_tree_insert (tree, &chars[i], &chars[i]);
661 for (j = 0; j < 26; j++, i++)
664 g_tree_insert (tree, &chars[i], &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_foreach (tree, my_traverse, NULL);
674 for (i = 0; i < 10; i++)
675 g_tree_remove (tree, &chars[i]);
677 g_print ("tree height: %d\n", g_tree_height (tree));
678 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
681 g_tree_foreach (tree, my_traverse, NULL);
687 /* check n-way trees */
690 g_print ("checking mem chunks...");
692 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
694 for (i = 0; i < 10000; i++)
696 mem[i] = g_chunk_new (gchar, mem_chunk);
698 for (j = 0; j < 50; j++)
702 for (i = 0; i < 10000; i++)
704 g_mem_chunk_free (mem_chunk, mem[i]);
710 g_print ("checking hash tables...");
712 hash_table = g_hash_table_new (my_hash, my_hash_equal);
713 for (i = 0; i < 10000; i++)
716 g_hash_table_insert (hash_table, &array[i], &array[i]);
718 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
720 for (i = 0; i < 10000; i++)
724 for (i = 0; i < 10000; i++)
725 g_hash_table_remove (hash_table, &array[i]);
727 for (i = 0; i < 10000; i++)
730 g_hash_table_insert (hash_table, &array[i], &array[i]);
733 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
734 g_hash_table_size (hash_table) != 5000)
737 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
740 g_hash_table_destroy (hash_table);
745 g_print ("checking string chunks...");
747 string_chunk = g_string_chunk_new (1024);
749 for (i = 0; i < 100000; i ++)
751 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
753 if (strcmp ("hi pete", tmp_string) != 0)
754 g_error ("string chunks are broken.\n");
757 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
759 g_assert (tmp_string_2 != tmp_string &&
760 strcmp(tmp_string_2, tmp_string) == 0);
762 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
764 g_assert (tmp_string_2 == tmp_string);
766 g_string_chunk_free (string_chunk);
771 g_print ("checking arrays...");
773 garray = g_array_new (FALSE, FALSE, sizeof (gint));
774 for (i = 0; i < 10000; i++)
775 g_array_append_val (garray, i);
777 for (i = 0; i < 10000; i++)
778 if (g_array_index (garray, gint, i) != i)
779 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
781 g_array_free (garray, TRUE);
783 garray = g_array_new (FALSE, FALSE, sizeof (gint));
784 for (i = 0; i < 100; i++)
785 g_array_prepend_val (garray, i);
787 for (i = 0; i < 100; i++)
788 if (g_array_index (garray, gint, i) != (100 - i - 1))
789 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
791 g_array_free (garray, TRUE);
796 g_print ("checking strings...");
798 string1 = g_string_new ("hi pete!");
799 string2 = g_string_new ("");
801 g_assert (strcmp ("hi pete!", string1->str) == 0);
803 for (i = 0; i < 10000; i++)
804 g_string_append_c (string1, 'a'+(i%26));
807 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
808 the %10000.10000f format... */
809 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
810 "this pete guy sure is a wuss, like he's the number ",
812 " wuss. everyone agrees.\n",
814 10, 666, 15, 15, 666.666666666, 666.666666666);
816 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
817 "this pete guy sure is a wuss, like he's the number ",
819 " wuss. everyone agrees.\n",
821 10, 666, 15, 15, 666.666666666, 666.666666666);
824 g_print ("string2 length = %lu...\n", (gulong)string2->len);
825 string2->str[70] = '\0';
826 g_print ("first 70 chars:\n%s\n", string2->str);
827 string2->str[141] = '\0';
828 g_print ("next 70 chars:\n%s\n", string2->str+71);
829 string2->str[212] = '\0';
830 g_print ("and next 70:\n%s\n", string2->str+142);
831 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
833 g_string_free (string1, TRUE);
834 g_string_free (string2, TRUE);
837 string1 = g_string_new ("firsthalf");
838 g_string_append (string1, "lasthalf");
839 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
840 g_string_free (string1, TRUE);
844 string1 = g_string_new ("firsthalf");
845 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
846 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
847 g_string_free (string1, TRUE);
850 string1 = g_string_new ("lasthalf");
851 g_string_prepend (string1, "firsthalf");
852 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
853 g_string_free (string1, TRUE);
856 string1 = g_string_new ("lasthalf");
857 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
858 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
859 g_string_free (string1, TRUE);
862 string1 = g_string_new ("firstlast");
863 g_string_insert (string1, 5, "middle");
864 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
865 g_string_free (string1, TRUE);
867 /* insert with pos == end of the string */
868 string1 = g_string_new ("firstmiddle");
869 g_string_insert (string1, strlen ("firstmiddle"), "last");
870 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
871 g_string_free (string1, TRUE);
875 string1 = g_string_new ("firstlast");
876 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
877 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
878 g_string_free (string1, TRUE);
880 /* insert_len with magic -1 pos for append */
881 string1 = g_string_new ("first");
882 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
883 g_assert (strcmp (string1->str, "firstlast") == 0);
884 g_string_free (string1, TRUE);
886 /* insert_len with magic -1 len for strlen-the-string */
887 string1 = g_string_new ("first");
888 g_string_insert_len (string1, 5, "last", -1);
889 g_assert (strcmp (string1->str, "firstlast") == 0);
890 g_string_free (string1, TRUE);
895 string1 = g_string_new ("test");
896 string2 = g_string_new ("te");
897 g_assert (! g_string_equal(string1, string2));
898 g_string_append (string2, "st");
899 g_assert (g_string_equal(string1, string2));
900 g_string_free (string1, TRUE);
901 g_string_free (string2, TRUE);
903 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
904 string1 = g_string_new ("fiddle");
905 string2 = g_string_new ("fiddle");
906 g_assert (g_string_equal(string1, string2));
907 g_string_append_c(string1, '\0');
908 g_assert (! g_string_equal(string1, string2));
909 g_string_append_c(string2, '\0');
910 g_assert (g_string_equal(string1, string2));
911 g_string_append_c(string1, 'x');
912 g_string_append_c(string2, 'y');
913 g_assert (! g_string_equal(string1, string2));
914 g_assert (string1->len == 8);
915 g_string_append(string1, "yzzy");
916 g_assert (string1->len == 12);
917 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
918 g_string_insert(string1, 1, "QED");
919 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
920 g_string_free (string1, TRUE);
921 g_string_free (string2, TRUE);
923 g_print ("test positional printf formats (not supported): ");
924 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
925 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
926 g_print ("%s%s\n", string, tmp_string);
930 g_print ("checking timers...\n");
932 timer = g_timer_new ();
933 g_print (" spinning for 3 seconds...\n");
935 g_timer_start (timer);
936 while (g_timer_elapsed (timer, NULL) < 3)
939 g_timer_stop (timer);
940 g_timer_destroy (timer);
944 g_print ("checking g_ascii_strcasecmp...");
945 g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
946 g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
947 g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
948 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
949 g_assert (g_ascii_strcasecmp ("", "") == 0);
950 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
951 g_assert (g_ascii_strcasecmp ("a", "b") < 0);
952 g_assert (g_ascii_strcasecmp ("a", "B") < 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 ("b", "a") > 0);
956 g_assert (g_ascii_strcasecmp ("b", "A") > 0);
957 g_assert (g_ascii_strcasecmp ("B", "a") > 0);
958 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
962 g_print ("checking g_strdup...");
963 g_assert(g_strdup(NULL) == NULL);
964 string = g_strdup(GLIB_TEST_STRING);
965 g_assert(string != NULL);
966 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
971 g_print ("checking g_strconcat...");
972 string = g_strconcat(GLIB_TEST_STRING, NULL);
973 g_assert(string != NULL);
974 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
976 string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
977 GLIB_TEST_STRING, NULL);
978 g_assert(string != NULL);
979 g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
980 GLIB_TEST_STRING) == 0);
985 g_print("checking g_strlcpy/g_strlcat...");
986 /* The following is a torture test for strlcpy/strlcat, with lots of
987 * checking; normal users wouldn't use them this way!
989 string = g_malloc (6);
990 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
992 g_assert (g_strlcpy(string, "" , 5) == 0);
993 g_assert ( *string == '\0' );
995 g_assert (g_strlcpy(string, "abc" , 5) == 3);
996 g_assert ( *(string + 3) == '\0' );
997 g_assert (g_str_equal(string, "abc"));
998 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
999 g_assert ( *(string + 4) == '\0' );
1000 g_assert ( *(string + 5) == 'Z' );
1001 g_assert (g_str_equal(string, "abcd"));
1002 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1003 g_assert ( *(string + 4) == '\0' );
1004 g_assert ( *(string + 5) == 'Z' );
1005 g_assert (g_str_equal(string, "abcd"));
1006 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1007 g_assert ( *(string + 4) == '\0' );
1008 g_assert ( *(string + 5) == 'Z' );
1009 g_assert (g_str_equal(string, "abcd"));
1011 *(string + 1)= '\0';
1012 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1013 g_assert (*string == 'Y');
1015 g_assert (g_strlcat(string, "123" , 5) == 3);
1016 g_assert ( *(string + 3) == '\0' );
1017 g_assert (g_str_equal(string, "123"));
1018 g_assert (g_strlcat(string, "" , 5) == 3);
1019 g_assert ( *(string + 3) == '\0' );
1020 g_assert (g_str_equal(string, "123"));
1021 g_assert (g_strlcat(string, "4", 5) == 4);
1022 g_assert (g_str_equal(string, "1234"));
1023 g_assert (g_strlcat(string, "5", 5) == 5);
1024 g_assert ( *(string + 4) == '\0' );
1025 g_assert (g_str_equal(string, "1234"));
1026 g_assert ( *(string + 5) == 'Z' );
1028 *(string + 1)= '\0';
1029 g_assert (g_strlcat(string, "123" , 0) == 3);
1030 g_assert (*string == 'Y');
1032 /* A few more tests, demonstrating more "normal" use */
1033 g_assert (g_strlcpy(string, "hi", 5) == 2);
1034 g_assert (g_str_equal(string, "hi"));
1035 g_assert (g_strlcat(string, "t", 5) == 3);
1036 g_assert (g_str_equal(string, "hit"));
1042 g_print ("checking g_strdup_printf...");
1043 string = g_strdup_printf ("%05d %-5s", 21, "test");
1044 g_assert (string != NULL);
1045 g_assert (strcmp(string, "00021 test ") == 0);
1050 /* g_debug (argv[0]); */
1052 /* Relation tests */
1054 g_print ("checking relations...");
1056 relation = g_relation_new (2);
1058 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1059 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1061 for (i = 0; i < 1024; i += 1)
1064 for (i = 1; i < 1023; i += 1)
1066 g_relation_insert (relation, data + i, data + i + 1);
1067 g_relation_insert (relation, data + i, data + i - 1);
1070 for (i = 2; i < 1022; i += 1)
1072 g_assert (! g_relation_exists (relation, data + i, data + i));
1073 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1074 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1077 for (i = 1; i < 1023; i += 1)
1079 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1080 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1083 for (i = 2; i < 1022; i += 1)
1085 g_assert (g_relation_count (relation, data + i, 0) == 2);
1086 g_assert (g_relation_count (relation, data + i, 1) == 2);
1089 g_assert (g_relation_count (relation, data, 0) == 0);
1091 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1092 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1093 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1094 g_relation_delete (relation, data + 42, 0);
1095 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1096 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1097 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1099 tuples = g_relation_select (relation, data + 200, 0);
1101 g_assert (tuples->len == 2);
1104 for (i = 0; i < tuples->len; i += 1)
1107 *(gint*) g_tuples_index (tuples, i, 0),
1108 *(gint*) g_tuples_index (tuples, i, 1));
1112 g_assert (g_relation_exists (relation, data + 300, data + 301));
1113 g_relation_delete (relation, data + 300, 0);
1114 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1116 g_tuples_destroy (tuples);
1118 g_relation_destroy (relation);
1124 g_print ("checking pointer arrays...");
1126 gparray = g_ptr_array_new ();
1127 for (i = 0; i < 10000; i++)
1128 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1130 for (i = 0; i < 10000; i++)
1131 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1132 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1134 g_ptr_array_free (gparray, TRUE);
1139 g_print ("checking byte arrays...");
1141 gbarray = g_byte_array_new ();
1142 for (i = 0; i < 10000; i++)
1143 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1145 for (i = 0; i < 10000; i++)
1147 g_assert (gbarray->data[4*i] == 'a');
1148 g_assert (gbarray->data[4*i+1] == 'b');
1149 g_assert (gbarray->data[4*i+2] == 'c');
1150 g_assert (gbarray->data[4*i+3] == 'd');
1153 g_byte_array_free (gbarray, TRUE);
1156 g_printerr ("g_log tests:");
1157 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1158 g_message ("the next warning is a test:");
1162 g_print ("checking endian macros (host is ");
1163 #if G_BYTE_ORDER == G_BIG_ENDIAN
1164 g_print ("big endian)...");
1166 g_print ("little endian)...");
1168 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
1169 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
1170 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
1174 #ifdef G_PLATFORM_WIN32
1175 g_print ("current locale: %s\n", g_win32_getlocale ());
1176 g_print ("GLib DLL name tested for: %s\n", glib_dll);
1178 g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
1180 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
1181 g_print ("Ditto, or from GLib DLL name: %s\n",
1182 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
1183 g_print ("Ditto, only from GLib DLL name: %s\n",
1184 g_win32_get_package_installation_directory (NULL, glib_dll));
1185 g_print ("locale subdirectory of GLib installation directory: %s\n",
1186 g_win32_get_package_installation_subdirectory (NULL, glib_dll, "lib\\locale"));
1187 g_print ("GTK+ 2.0 installation directory, if available: %s\n",
1188 g_win32_get_package_installation_directory ("gtk20", NULL));
1190 g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
1191 g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
1195 g_print ("checking file functions...\n");
1197 strcpy (template, "foobar");
1198 fd = g_mkstemp (template);
1200 g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
1202 strcpy (template, "fooXXXXXX");
1203 fd = g_mkstemp (template);
1205 g_print ("g_mkstemp didn't work for template %s\n", template);
1206 i = write (fd, hello, hellolen);
1208 g_print ("write() failed: %s\n", g_strerror (errno));
1209 else if (i != hellolen)
1210 g_print ("write() should have written %d bytes, wrote %d\n", hellolen, i);
1213 i = read (fd, chars, sizeof (chars));
1215 g_print ("read() failed: %s\n", g_strerror (errno));
1216 else if (i != hellolen)
1217 g_print ("read() should have read %d bytes, got %d\n", hellolen, i);
1220 if (strcmp (chars, hello) != 0)
1221 g_print ("wrote '%s', but got '%s'\n", hello, chars);
1227 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1228 fd = g_file_open_tmp (template, &name_used, &error);
1230 g_print ("g_file_open_tmp works even if template contains '%s'\n",
1233 g_print ("g_file_open_tmp correctly returns error: %s\n",
1236 g_clear_error (&error);
1238 strcpy (template, "zapXXXXXX");
1239 fd = g_file_open_tmp (template, &name_used, &error);
1241 g_print ("g_file_open_tmp didn't work for template '%s': %s\n",
1242 template, error->message);
1244 g_print ("g_file_open_tmp for template '%s' used name '%s'\n",
1245 template, name_used);
1247 g_clear_error (&error);
1250 fd = g_file_open_tmp (NULL, &name_used, &error);
1252 g_print ("g_file_open_tmp didn't work for a NULL template: %s\n",
1255 g_print ("g_file_open_tmp for NULL template used name '%s'\n",
1258 g_clear_error (&error);