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/.
35 gboolean failed = FALSE;
37 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
40 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
42 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
45 g_print ("."); fflush (stdout); \
48 #define C2P(c) ((gpointer) ((long) (c)))
49 #define P2C(p) ((gchar) ((long) (p)))
51 #define GLIB_TEST_STRING "el dorado "
52 #define GLIB_TEST_STRING_5 "el do"
55 node_build_string (GNode *node,
62 c[0] = P2C (node->data);
64 string = g_strconcat (*p ? *p : "", c, NULL);
81 gchar *tstring, *cstring;
83 g_print ("checking n-way trees: ");
86 root = g_node_new (C2P ('A'));
87 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
89 node_B = g_node_new (C2P ('B'));
90 g_node_append (root, node_B);
91 TEST (NULL, root->children == node_B);
93 g_node_append_data (node_B, C2P ('E'));
94 g_node_prepend_data (node_B, C2P ('C'));
95 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
97 node_F = g_node_new (C2P ('F'));
98 g_node_append (root, node_F);
99 TEST (NULL, root->children->next == node_F);
101 node_G = g_node_new (C2P ('G'));
102 g_node_append (node_F, node_G);
103 node_J = g_node_new (C2P ('J'));
104 g_node_prepend (node_G, node_J);
105 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
106 g_node_insert_data (node_G, 0, C2P ('H'));
107 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
109 TEST (NULL, g_node_depth (root) == 1);
110 TEST (NULL, g_node_max_height (root) == 4);
111 TEST (NULL, g_node_depth (node_G->children->next) == 4);
112 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
113 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
114 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
115 TEST (NULL, g_node_max_height (node_F) == 3);
116 TEST (NULL, g_node_n_children (node_G) == 4);
117 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
118 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
119 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
121 for (i = 0; i < g_node_n_children (node_B); i++)
123 node = g_node_nth_child (node_B, i);
124 TEST (NULL, P2C (node->data) == ('C' + i));
127 for (i = 0; i < g_node_n_children (node_G); i++)
128 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
138 * for in-order traversal, 'G' is considered to be the "left"
139 * child of 'F', which will cause 'F' to be the last node visited.
143 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
144 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
145 g_free (tstring); tstring = NULL;
146 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
147 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
148 g_free (tstring); tstring = NULL;
149 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
150 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
151 g_free (tstring); tstring = NULL;
152 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
153 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
154 g_free (tstring); tstring = NULL;
156 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
157 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
158 g_free (tstring); tstring = NULL;
159 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
160 TEST (tstring, strcmp (tstring, "ABFG") == 0);
161 g_free (tstring); tstring = NULL;
163 g_node_reverse_children (node_B);
164 g_node_reverse_children (node_G);
166 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
167 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
168 g_free (tstring); tstring = NULL;
171 node = g_node_copy (root);
172 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
173 TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
174 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
175 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
176 TEST (cstring, strcmp (tstring, cstring) == 0);
177 g_free (tstring); tstring = NULL;
178 g_free (cstring); cstring = NULL;
179 g_node_destroy (node);
181 g_node_destroy (root);
183 /* allocation tests */
185 root = g_node_new (NULL);
188 for (i = 0; i < 2048; i++)
190 g_node_append (node, g_node_new (NULL));
192 node = node->children->next;
194 TEST (NULL, g_node_max_height (root) > 100);
195 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
197 g_node_destroy (root);
204 my_hash_callback_remove (gpointer key,
217 my_hash_callback_remove_test (gpointer key,
228 my_hash_callback (gpointer key,
237 my_hash (gconstpointer key)
239 return (guint) *((const gint*) key);
243 my_hash_equal (gconstpointer a,
246 return *((const gint*) a) == *((const gint*) b);
250 my_list_compare_one (gconstpointer a, gconstpointer b)
252 gint one = *((const gint*)a);
253 gint two = *((const gint*)b);
258 my_list_compare_two (gconstpointer a, gconstpointer b)
260 gint one = *((const gint*)a);
261 gint two = *((const gint*)b);
266 my_list_print (gpointer a, gpointer b)
268 gint three = *((gint*)a);
269 g_print("%d", three);
273 my_compare (gconstpointer a,
283 my_traverse (gpointer key,
288 g_print ("%c ", *ch);
298 GHashTable *hash_table;
299 GMemChunk *mem_chunk;
300 GStringChunk *string_chunk;
302 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
303 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
306 gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
311 GString *string1, *string2;
320 } dirname_checks[] = {
335 { ".\\\\\\\\", "." },
339 { "..\\\\\\\\", ".." },
342 { "a\\b\\", "a\\b" },
346 guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
347 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
348 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
350 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
351 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
353 const char hello[] = "Hello, World";
354 const int hellolen = sizeof (hello) - 1;
360 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
367 string = g_get_current_dir ();
368 g_print ("cwd: %s\n", string);
370 g_print ("user: %s\n", g_get_user_name ());
371 g_print ("real: %s\n", g_get_real_name ());
372 g_print ("home: %s\n", g_get_home_dir ());
373 g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
376 g_print ("checking size of gint8: %d", (int)sizeof (gint8));
377 TEST (NULL, sizeof (gint8) == 1);
378 g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
379 TEST (NULL, sizeof (gint16) == 2);
380 g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
381 TEST (NULL, sizeof (gint32) == 4);
382 g_print ("\nchecking size of gsize: %d", (int)sizeof (gsize));
384 g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
385 TEST (NULL, sizeof (gint64) == 8);
386 #endif /* G_HAVE_GINT64 */
389 g_print ("checking g_path_get_dirname()...");
390 for (i = 0; i < n_dirname_checks; i++)
394 dirname = g_path_get_dirname (dirname_checks[i].filename);
395 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
397 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
398 dirname_checks[i].filename,
399 dirname_checks[i].dirname,
401 n_dirname_checks = 0;
405 if (n_dirname_checks)
408 g_print ("checking doubly linked lists...");
411 for (i = 0; i < 10; i++)
412 list = g_list_append (list, &nums[i]);
413 list = g_list_reverse (list);
415 for (i = 0; i < 10; i++)
417 t = g_list_nth (list, i);
418 if (*((gint*) t->data) != (9 - i))
419 g_error ("Regular insert failed");
422 for (i = 0; i < 10; i++)
423 if(g_list_position(list, g_list_nth (list, i)) != i)
424 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
429 for (i = 0; i < 10; i++)
430 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
434 g_list_foreach (list, my_list_print, NULL);
437 for (i = 0; i < 10; i++)
439 t = g_list_nth (list, i);
440 if (*((gint*) t->data) != i)
441 g_error ("Sorted insert failed");
447 for (i = 0; i < 10; i++)
448 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
452 g_list_foreach (list, my_list_print, NULL);
455 for (i = 0; i < 10; i++)
457 t = g_list_nth (list, i);
458 if (*((gint*) t->data) != (9 - i))
459 g_error ("Sorted insert failed");
465 for (i = 0; i < 10; i++)
466 list = g_list_prepend (list, &morenums[i]);
468 list = g_list_sort (list, my_list_compare_two);
472 g_list_foreach (list, my_list_print, NULL);
475 for (i = 0; i < 10; i++)
477 t = g_list_nth (list, i);
478 if (*((gint*) t->data) != (9 - i))
479 g_error ("Merge sort failed");
487 g_print ("checking singly linked lists...");
490 for (i = 0; i < 10; i++)
491 slist = g_slist_append (slist, &nums[i]);
492 slist = g_slist_reverse (slist);
494 for (i = 0; i < 10; i++)
496 st = g_slist_nth (slist, i);
497 if (*((gint*) st->data) != (9 - i))
501 g_slist_free (slist);
504 for (i = 0; i < 10; i++)
505 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
509 g_slist_foreach (slist, my_list_print, NULL);
512 for (i = 0; i < 10; i++)
514 st = g_slist_nth (slist, i);
515 if (*((gint*) st->data) != i)
516 g_error ("Sorted insert failed");
522 for (i = 0; i < 10; i++)
523 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
527 g_slist_foreach (slist, my_list_print, NULL);
530 for (i = 0; i < 10; i++)
532 st = g_slist_nth (slist, i);
533 if (*((gint*) st->data) != (9 - i))
534 g_error("Sorted insert failed");
540 for (i = 0; i < 10; i++)
541 slist = g_slist_prepend (slist, &morenums[i]);
543 slist = g_slist_sort (slist, my_list_compare_two);
547 g_slist_foreach (slist, my_list_print, NULL);
550 for (i = 0; i < 10; i++)
552 st = g_slist_nth (slist, i);
553 if (*((gint*) st->data) != (9 - i))
554 g_error("Sorted insert failed");
562 g_print ("checking binary trees...\n");
564 tree = g_tree_new (my_compare);
566 for (j = 0; j < 10; j++, i++)
569 g_tree_insert (tree, &chars[i], &chars[i]);
571 for (j = 0; j < 26; j++, i++)
574 g_tree_insert (tree, &chars[i], &chars[i]);
576 for (j = 0; j < 26; j++, i++)
579 g_tree_insert (tree, &chars[i], &chars[i]);
582 g_print ("tree height: %d\n", g_tree_height (tree));
583 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
586 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
589 for (i = 0; i < 10; i++)
590 g_tree_remove (tree, &chars[i]);
592 g_print ("tree height: %d\n", g_tree_height (tree));
593 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
596 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
602 /* check n-way trees */
605 g_print ("checking mem chunks...");
607 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
609 for (i = 0; i < 10000; i++)
611 mem[i] = g_chunk_new (gchar, mem_chunk);
613 for (j = 0; j < 50; j++)
617 for (i = 0; i < 10000; i++)
619 g_mem_chunk_free (mem_chunk, mem[i]);
625 g_print ("checking hash tables...");
627 hash_table = g_hash_table_new (my_hash, my_hash_equal);
628 for (i = 0; i < 10000; i++)
631 g_hash_table_insert (hash_table, &array[i], &array[i]);
633 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
635 for (i = 0; i < 10000; i++)
639 for (i = 0; i < 10000; i++)
640 g_hash_table_remove (hash_table, &array[i]);
642 for (i = 0; i < 10000; i++)
645 g_hash_table_insert (hash_table, &array[i], &array[i]);
648 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
649 g_hash_table_size (hash_table) != 5000)
652 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
655 g_hash_table_destroy (hash_table);
660 g_print ("checking string chunks...");
662 string_chunk = g_string_chunk_new (1024);
664 for (i = 0; i < 100000; i ++)
666 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
668 if (strcmp ("hi pete", tmp_string) != 0)
669 g_error ("string chunks are broken.\n");
672 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
674 g_assert (tmp_string_2 != tmp_string &&
675 strcmp(tmp_string_2, tmp_string) == 0);
677 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
679 g_assert (tmp_string_2 == tmp_string);
681 g_string_chunk_free (string_chunk);
686 g_print ("checking arrays...");
688 garray = g_array_new (FALSE, FALSE, sizeof (gint));
689 for (i = 0; i < 10000; i++)
690 g_array_append_val (garray, i);
692 for (i = 0; i < 10000; i++)
693 if (g_array_index (garray, gint, i) != i)
694 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
696 g_array_free (garray, TRUE);
698 garray = g_array_new (FALSE, FALSE, sizeof (gint));
699 for (i = 0; i < 100; i++)
700 g_array_prepend_val (garray, i);
702 for (i = 0; i < 100; i++)
703 if (g_array_index (garray, gint, i) != (100 - i - 1))
704 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
706 g_array_free (garray, TRUE);
711 g_print ("checking strings...");
713 string1 = g_string_new ("hi pete!");
714 string2 = g_string_new ("");
716 g_assert (strcmp ("hi pete!", string1->str) == 0);
718 for (i = 0; i < 10000; i++)
719 g_string_append_c (string1, 'a'+(i%26));
722 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
723 the %10000.10000f format... */
724 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
725 "this pete guy sure is a wuss, like he's the number ",
727 " wuss. everyone agrees.\n",
729 10, 666, 15, 15, 666.666666666, 666.666666666);
731 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
732 "this pete guy sure is a wuss, like he's the number ",
734 " wuss. everyone agrees.\n",
736 10, 666, 15, 15, 666.666666666, 666.666666666);
739 g_print ("string2 length = %d...\n", string2->len);
740 string2->str[70] = '\0';
741 g_print ("first 70 chars:\n%s\n", string2->str);
742 string2->str[141] = '\0';
743 g_print ("next 70 chars:\n%s\n", string2->str+71);
744 string2->str[212] = '\0';
745 g_print ("and next 70:\n%s\n", string2->str+142);
746 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
748 g_string_free (string1, TRUE);
749 g_string_free (string2, TRUE);
752 string1 = g_string_new ("firsthalf");
753 g_string_append (string1, "lasthalf");
754 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
755 g_string_free (string1, TRUE);
759 string1 = g_string_new ("firsthalf");
760 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
761 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
762 g_string_free (string1, TRUE);
765 string1 = g_string_new ("lasthalf");
766 g_string_prepend (string1, "firsthalf");
767 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
768 g_string_free (string1, TRUE);
771 string1 = g_string_new ("lasthalf");
772 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
773 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
774 g_string_free (string1, TRUE);
777 string1 = g_string_new ("firstlast");
778 g_string_insert (string1, 5, "middle");
779 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
780 g_string_free (string1, TRUE);
782 /* insert with pos == end of the string */
783 string1 = g_string_new ("firstmiddle");
784 g_string_insert (string1, strlen ("firstmiddle"), "last");
785 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
786 g_string_free (string1, TRUE);
790 string1 = g_string_new ("firstlast");
791 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
792 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
793 g_string_free (string1, TRUE);
795 /* insert_len with magic -1 pos for append */
796 string1 = g_string_new ("first");
797 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
798 g_assert (strcmp (string1->str, "firstlast") == 0);
799 g_string_free (string1, TRUE);
801 /* insert_len with magic -1 len for strlen-the-string */
802 string1 = g_string_new ("first");
803 g_string_insert_len (string1, 5, "last", -1);
804 g_assert (strcmp (string1->str, "firstlast") == 0);
805 g_string_free (string1, TRUE);
810 string1 = g_string_new ("test");
811 string2 = g_string_new ("te");
812 g_assert (! g_string_equal(string1, string2));
813 g_string_append (string2, "st");
814 g_assert (g_string_equal(string1, string2));
815 g_string_free (string1, TRUE);
816 g_string_free (string2, TRUE);
818 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
819 string1 = g_string_new ("fiddle");
820 string2 = g_string_new ("fiddle");
821 g_assert (g_string_equal(string1, string2));
822 g_string_append_c(string1, '\0');
823 g_assert (! g_string_equal(string1, string2));
824 g_string_append_c(string2, '\0');
825 g_assert (g_string_equal(string1, string2));
826 g_string_append_c(string1, 'x');
827 g_string_append_c(string2, 'y');
828 g_assert (! g_string_equal(string1, string2));
829 g_assert (string1->len == 8);
830 g_string_append(string1, "yzzy");
831 g_assert (string1->len == 12);
832 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
833 g_string_insert(string1, 1, "QED");
834 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
835 g_string_free (string1, TRUE);
836 g_string_free (string2, TRUE);
838 g_print ("test positional printf formats (not supported): ");
839 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
840 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
841 g_print ("%s%s\n", string, tmp_string);
845 g_print ("checking timers...\n");
847 timer = g_timer_new ();
848 g_print (" spinning for 3 seconds...\n");
850 g_timer_start (timer);
851 while (g_timer_elapsed (timer, NULL) < 3)
854 g_timer_stop (timer);
855 g_timer_destroy (timer);
859 g_print ("checking g_strcasecmp...");
860 g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0);
861 g_assert (g_strcasecmp ("frobozz", "frobozz") == 0);
862 g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0);
863 g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0);
864 g_assert (g_strcasecmp ("", "") == 0);
865 g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0);
866 g_assert (g_strcasecmp ("a", "b") < 0);
867 g_assert (g_strcasecmp ("a", "B") < 0);
868 g_assert (g_strcasecmp ("A", "b") < 0);
869 g_assert (g_strcasecmp ("A", "B") < 0);
870 g_assert (g_strcasecmp ("b", "a") > 0);
871 g_assert (g_strcasecmp ("b", "A") > 0);
872 g_assert (g_strcasecmp ("B", "a") > 0);
873 g_assert (g_strcasecmp ("B", "A") > 0);
877 g_print ("checking g_strdup...");
878 g_assert(g_strdup(NULL) == NULL);
879 string = g_strdup(GLIB_TEST_STRING);
880 g_assert(string != NULL);
881 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
886 g_print ("checking g_strconcat...");
887 string = g_strconcat(GLIB_TEST_STRING, NULL);
888 g_assert(string != NULL);
889 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
891 string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
892 GLIB_TEST_STRING, NULL);
893 g_assert(string != NULL);
894 g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
895 GLIB_TEST_STRING) == 0);
900 g_print("checking g_strlcpy/g_strlcat...");
901 /* The following is a torture test for strlcpy/strlcat, with lots of
902 * checking; normal users wouldn't use them this way!
904 string = g_malloc (6);
905 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
907 g_assert (g_strlcpy(string, "" , 5) == 0);
908 g_assert ( *string == '\0' );
910 g_assert (g_strlcpy(string, "abc" , 5) == 3);
911 g_assert ( *(string + 3) == '\0' );
912 g_assert (g_str_equal(string, "abc"));
913 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
914 g_assert ( *(string + 4) == '\0' );
915 g_assert ( *(string + 5) == 'Z' );
916 g_assert (g_str_equal(string, "abcd"));
917 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
918 g_assert ( *(string + 4) == '\0' );
919 g_assert ( *(string + 5) == 'Z' );
920 g_assert (g_str_equal(string, "abcd"));
921 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
922 g_assert ( *(string + 4) == '\0' );
923 g_assert ( *(string + 5) == 'Z' );
924 g_assert (g_str_equal(string, "abcd"));
927 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
928 g_assert (*string == 'Y');
930 g_assert (g_strlcat(string, "123" , 5) == 3);
931 g_assert ( *(string + 3) == '\0' );
932 g_assert (g_str_equal(string, "123"));
933 g_assert (g_strlcat(string, "" , 5) == 3);
934 g_assert ( *(string + 3) == '\0' );
935 g_assert (g_str_equal(string, "123"));
936 g_assert (g_strlcat(string, "4", 5) == 4);
937 g_assert (g_str_equal(string, "1234"));
938 g_assert (g_strlcat(string, "5", 5) == 5);
939 g_assert ( *(string + 4) == '\0' );
940 g_assert (g_str_equal(string, "1234"));
941 g_assert ( *(string + 5) == 'Z' );
944 g_assert (g_strlcat(string, "123" , 0) == 3);
945 g_assert (*string == 'Y');
947 /* A few more tests, demonstrating more "normal" use */
948 g_assert (g_strlcpy(string, "hi", 5) == 2);
949 g_assert (g_str_equal(string, "hi"));
950 g_assert (g_strlcat(string, "t", 5) == 3);
951 g_assert (g_str_equal(string, "hit"));
957 g_print ("checking g_strdup_printf...");
958 string = g_strdup_printf ("%05d %-5s", 21, "test");
959 g_assert (string != NULL);
960 g_assert (strcmp(string, "00021 test ") == 0);
965 /* g_debug (argv[0]); */
969 g_print ("checking relations...");
971 relation = g_relation_new (2);
973 g_relation_index (relation, 0, g_int_hash, g_int_equal);
974 g_relation_index (relation, 1, g_int_hash, g_int_equal);
976 for (i = 0; i < 1024; i += 1)
979 for (i = 1; i < 1023; i += 1)
981 g_relation_insert (relation, data + i, data + i + 1);
982 g_relation_insert (relation, data + i, data + i - 1);
985 for (i = 2; i < 1022; i += 1)
987 g_assert (! g_relation_exists (relation, data + i, data + i));
988 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
989 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
992 for (i = 1; i < 1023; i += 1)
994 g_assert (g_relation_exists (relation, data + i, data + i + 1));
995 g_assert (g_relation_exists (relation, data + i, data + i - 1));
998 for (i = 2; i < 1022; i += 1)
1000 g_assert (g_relation_count (relation, data + i, 0) == 2);
1001 g_assert (g_relation_count (relation, data + i, 1) == 2);
1004 g_assert (g_relation_count (relation, data, 0) == 0);
1006 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1007 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1008 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1009 g_relation_delete (relation, data + 42, 0);
1010 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1011 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1012 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1014 tuples = g_relation_select (relation, data + 200, 0);
1016 g_assert (tuples->len == 2);
1019 for (i = 0; i < tuples->len; i += 1)
1022 *(gint*) g_tuples_index (tuples, i, 0),
1023 *(gint*) g_tuples_index (tuples, i, 1));
1027 g_assert (g_relation_exists (relation, data + 300, data + 301));
1028 g_relation_delete (relation, data + 300, 0);
1029 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1031 g_tuples_destroy (tuples);
1033 g_relation_destroy (relation);
1039 g_print ("checking pointer arrays...");
1041 gparray = g_ptr_array_new ();
1042 for (i = 0; i < 10000; i++)
1043 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1045 for (i = 0; i < 10000; i++)
1046 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1047 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1049 g_ptr_array_free (gparray, TRUE);
1054 g_print ("checking byte arrays...");
1056 gbarray = g_byte_array_new ();
1057 for (i = 0; i < 10000; i++)
1058 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1060 for (i = 0; i < 10000; i++)
1062 g_assert (gbarray->data[4*i] == 'a');
1063 g_assert (gbarray->data[4*i+1] == 'b');
1064 g_assert (gbarray->data[4*i+2] == 'c');
1065 g_assert (gbarray->data[4*i+3] == 'd');
1068 g_byte_array_free (gbarray, TRUE);
1071 g_printerr ("g_log tests:");
1072 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1073 g_message ("the next warning is a test:");
1077 g_print ("checking endian macros (host is ");
1078 #if G_BYTE_ORDER == G_BIG_ENDIAN
1079 g_print ("big endian)...");
1081 g_print ("little endian)...");
1083 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
1084 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
1085 #ifdef G_HAVE_GINT64
1086 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
1092 g_print ("current locale: %s\n", g_win32_getlocale ());
1095 g_print ("checking file functions...\n");
1097 strcpy (template, "foobar");
1098 fd = g_mkstemp (template);
1100 g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
1102 strcpy (template, "fooXXXXXX");
1103 fd = g_mkstemp (template);
1105 g_print ("g_mkstemp didn't work for template %s\n", template);
1106 i = write (fd, hello, hellolen);
1108 g_print ("write() failed: %s\n", g_strerror (errno));
1109 else if (i != hellolen)
1110 g_print ("write() should have written %d bytes, wrote %d\n", hellolen, i);
1113 i = read (fd, chars, sizeof (chars));
1115 g_print ("read() failed: %s\n", g_strerror (errno));
1116 else if (i != hellolen)
1117 g_print ("read() should have read %d bytes, got %d\n", hellolen, i);
1120 if (strcmp (chars, hello) != 0)
1121 g_print ("wrote '%s', but got '%s'\n", hello, chars);
1126 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1127 fd = g_file_open_tmp (template, &name_used, &error);
1129 g_print ("g_file_open_tmp works even if template contains '%s'\n",
1132 g_print ("g_file_open_tmp correctly returns error: %s\n",
1135 g_clear_error (&error);
1137 strcpy (template, "zapXXXXXX");
1138 fd = g_file_open_tmp (template, &name_used, &error);
1140 g_print ("g_file_open_tmp didn't work for template '%s': %s\n",
1141 template, error->message);
1143 g_print ("g_file_open_tmp for template '%s' used name '%s'\n",
1144 template, name_used);
1146 g_clear_error (&error);
1149 fd = g_file_open_tmp (NULL, &name_used, &error);
1151 g_print ("g_file_open_tmp didn't work for a NULL template: %s\n",
1154 g_print ("g_file_open_tmp for NULL template used name '%s'\n",
1157 g_clear_error (&error);