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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
34 gboolean failed = FALSE;
36 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
39 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
41 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
44 g_print ("."); fflush (stdout); \
47 #define C2P(c) ((gpointer) ((long) (c)))
48 #define P2C(p) ((gchar) ((long) (p)))
50 #define GLIB_TEST_STRING "el dorado "
51 #define GLIB_TEST_STRING_5 "el do"
54 node_build_string (GNode *node,
61 c[0] = P2C (node->data);
63 string = g_strconcat (*p ? *p : "", c, NULL);
80 gchar *tstring, *cstring;
82 g_print ("checking n-way trees: ");
85 root = g_node_new (C2P ('A'));
86 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
88 node_B = g_node_new (C2P ('B'));
89 g_node_append (root, node_B);
90 TEST (NULL, root->children == node_B);
92 g_node_append_data (node_B, C2P ('E'));
93 g_node_prepend_data (node_B, C2P ('C'));
94 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
96 node_F = g_node_new (C2P ('F'));
97 g_node_append (root, node_F);
98 TEST (NULL, root->children->next == node_F);
100 node_G = g_node_new (C2P ('G'));
101 g_node_append (node_F, node_G);
102 node_J = g_node_new (C2P ('J'));
103 g_node_prepend (node_G, node_J);
104 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
105 g_node_insert_data (node_G, 0, C2P ('H'));
106 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
108 TEST (NULL, g_node_depth (root) == 1);
109 TEST (NULL, g_node_max_height (root) == 4);
110 TEST (NULL, g_node_depth (node_G->children->next) == 4);
111 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
112 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
113 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
114 TEST (NULL, g_node_max_height (node_F) == 3);
115 TEST (NULL, g_node_n_children (node_G) == 4);
116 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
117 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
118 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
120 for (i = 0; i < g_node_n_children (node_B); i++)
122 node = g_node_nth_child (node_B, i);
123 TEST (NULL, P2C (node->data) == ('C' + i));
126 for (i = 0; i < g_node_n_children (node_G); i++)
127 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
137 * for in-order traversal, 'G' is considered to be the "left"
138 * child of 'F', which will cause 'F' to be the last node visited.
142 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
143 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
144 g_free (tstring); tstring = NULL;
145 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
146 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
147 g_free (tstring); tstring = NULL;
148 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
149 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
150 g_free (tstring); tstring = NULL;
151 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
152 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
153 g_free (tstring); tstring = NULL;
155 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
156 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
157 g_free (tstring); tstring = NULL;
158 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
159 TEST (tstring, strcmp (tstring, "ABFG") == 0);
160 g_free (tstring); tstring = NULL;
162 g_node_reverse_children (node_B);
163 g_node_reverse_children (node_G);
165 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
166 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
167 g_free (tstring); tstring = NULL;
170 node = g_node_copy (root);
171 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
172 TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
173 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
174 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
175 TEST (cstring, strcmp (tstring, cstring) == 0);
176 g_free (tstring); tstring = NULL;
177 g_free (cstring); cstring = NULL;
178 g_node_destroy (node);
180 g_node_destroy (root);
182 /* allocation tests */
184 root = g_node_new (NULL);
187 for (i = 0; i < 2048; i++)
189 g_node_append (node, g_node_new (NULL));
191 node = node->children->next;
193 TEST (NULL, g_node_max_height (root) > 100);
194 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
196 g_node_destroy (root);
203 my_hash_callback_remove (gpointer key,
216 my_hash_callback_remove_test (gpointer key,
227 my_hash_callback (gpointer key,
236 my_hash (gconstpointer key)
238 return (guint) *((const gint*) key);
242 my_hash_compare (gconstpointer a,
245 return *((const gint*) a) == *((const gint*) b);
249 my_list_compare_one (gconstpointer a, gconstpointer b)
251 gint one = *((const gint*)a);
252 gint two = *((const gint*)b);
257 my_list_compare_two (gconstpointer a, gconstpointer b)
259 gint one = *((const gint*)a);
260 gint two = *((const gint*)b);
265 my_list_print (gpointer a, gpointer b)
267 gint three = *((gint*)a);
268 g_print("%d", three);
272 my_compare (gconstpointer a,
282 my_traverse (gpointer key,
287 g_print ("%c ", *ch);
297 GHashTable *hash_table;
298 GMemChunk *mem_chunk;
299 GStringChunk *string_chunk;
301 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
302 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
305 gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
310 GString *string1, *string2;
319 } dirname_checks[] = {
334 { ".\\\\\\\\", "." },
338 { "..\\\\\\\\", ".." },
341 { "a\\b\\", "a\\b" },
345 guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
346 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
347 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
349 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
350 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
353 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
360 string = g_get_current_dir ();
361 g_print ("cwd: %s\n", string);
363 g_print ("user: %s\n", g_get_user_name ());
364 g_print ("real: %s\n", g_get_real_name ());
365 g_print ("home: %s\n", g_get_home_dir ());
366 g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
369 g_print ("checking size of gint8: %d", (int)sizeof (gint8));
370 TEST (NULL, sizeof (gint8) == 1);
371 g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
372 TEST (NULL, sizeof (gint16) == 2);
373 g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
374 TEST (NULL, sizeof (gint32) == 4);
376 g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
377 TEST (NULL, sizeof (gint64) == 8);
378 #endif /* G_HAVE_GINT64 */
381 g_print ("checking g_path_get_dirname()...");
382 for (i = 0; i < n_dirname_checks; i++)
386 dirname = g_path_get_dirname (dirname_checks[i].filename);
387 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
389 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
390 dirname_checks[i].filename,
391 dirname_checks[i].dirname,
393 n_dirname_checks = 0;
397 if (n_dirname_checks)
400 g_print ("checking doubly linked lists...");
403 for (i = 0; i < 10; i++)
404 list = g_list_append (list, &nums[i]);
405 list = g_list_reverse (list);
407 for (i = 0; i < 10; i++)
409 t = g_list_nth (list, i);
410 if (*((gint*) t->data) != (9 - i))
411 g_error ("Regular insert failed");
414 for (i = 0; i < 10; i++)
415 if(g_list_position(list, g_list_nth (list, i)) != i)
416 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
421 for (i = 0; i < 10; i++)
422 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
426 g_list_foreach (list, my_list_print, NULL);
429 for (i = 0; i < 10; i++)
431 t = g_list_nth (list, i);
432 if (*((gint*) t->data) != i)
433 g_error ("Sorted insert failed");
439 for (i = 0; i < 10; i++)
440 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
444 g_list_foreach (list, my_list_print, NULL);
447 for (i = 0; i < 10; i++)
449 t = g_list_nth (list, i);
450 if (*((gint*) t->data) != (9 - i))
451 g_error ("Sorted insert failed");
457 for (i = 0; i < 10; i++)
458 list = g_list_prepend (list, &morenums[i]);
460 list = g_list_sort (list, my_list_compare_two);
464 g_list_foreach (list, my_list_print, NULL);
467 for (i = 0; i < 10; i++)
469 t = g_list_nth (list, i);
470 if (*((gint*) t->data) != (9 - i))
471 g_error ("Merge sort failed");
479 g_print ("checking singly linked lists...");
482 for (i = 0; i < 10; i++)
483 slist = g_slist_append (slist, &nums[i]);
484 slist = g_slist_reverse (slist);
486 for (i = 0; i < 10; i++)
488 st = g_slist_nth (slist, i);
489 if (*((gint*) st->data) != (9 - i))
493 g_slist_free (slist);
496 for (i = 0; i < 10; i++)
497 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
501 g_slist_foreach (slist, my_list_print, NULL);
504 for (i = 0; i < 10; i++)
506 st = g_slist_nth (slist, i);
507 if (*((gint*) st->data) != i)
508 g_error ("Sorted insert failed");
514 for (i = 0; i < 10; i++)
515 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
519 g_slist_foreach (slist, my_list_print, NULL);
522 for (i = 0; i < 10; i++)
524 st = g_slist_nth (slist, i);
525 if (*((gint*) st->data) != (9 - i))
526 g_error("Sorted insert failed");
532 for (i = 0; i < 10; i++)
533 slist = g_slist_prepend (slist, &morenums[i]);
535 slist = g_slist_sort (slist, my_list_compare_two);
539 g_slist_foreach (slist, my_list_print, NULL);
542 for (i = 0; i < 10; i++)
544 st = g_slist_nth (slist, i);
545 if (*((gint*) st->data) != (9 - i))
546 g_error("Sorted insert failed");
554 g_print ("checking binary trees...\n");
556 tree = g_tree_new (my_compare);
558 for (j = 0; j < 10; j++, i++)
561 g_tree_insert (tree, &chars[i], &chars[i]);
563 for (j = 0; j < 26; j++, i++)
566 g_tree_insert (tree, &chars[i], &chars[i]);
568 for (j = 0; j < 26; j++, i++)
571 g_tree_insert (tree, &chars[i], &chars[i]);
574 g_print ("tree height: %d\n", g_tree_height (tree));
575 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
578 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
581 for (i = 0; i < 10; i++)
582 g_tree_remove (tree, &chars[i]);
584 g_print ("tree height: %d\n", g_tree_height (tree));
585 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
588 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
594 /* check n-way trees */
597 g_print ("checking mem chunks...");
599 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
601 for (i = 0; i < 10000; i++)
603 mem[i] = g_chunk_new (gchar, mem_chunk);
605 for (j = 0; j < 50; j++)
609 for (i = 0; i < 10000; i++)
611 g_mem_chunk_free (mem_chunk, mem[i]);
617 g_print ("checking hash tables...");
619 hash_table = g_hash_table_new (my_hash, my_hash_compare);
620 for (i = 0; i < 10000; i++)
623 g_hash_table_insert (hash_table, &array[i], &array[i]);
625 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
627 for (i = 0; i < 10000; i++)
631 for (i = 0; i < 10000; i++)
632 g_hash_table_remove (hash_table, &array[i]);
634 for (i = 0; i < 10000; i++)
637 g_hash_table_insert (hash_table, &array[i], &array[i]);
640 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
641 g_hash_table_size (hash_table) != 5000)
644 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
647 g_hash_table_destroy (hash_table);
652 g_print ("checking string chunks...");
654 string_chunk = g_string_chunk_new (1024);
656 for (i = 0; i < 100000; i ++)
658 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
660 if (strcmp ("hi pete", tmp_string) != 0)
661 g_error ("string chunks are broken.\n");
664 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
666 g_assert (tmp_string_2 != tmp_string &&
667 strcmp(tmp_string_2, tmp_string) == 0);
669 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
671 g_assert (tmp_string_2 == tmp_string);
673 g_string_chunk_free (string_chunk);
678 g_print ("checking arrays...");
680 garray = g_array_new (FALSE, FALSE, sizeof (gint));
681 for (i = 0; i < 10000; i++)
682 g_array_append_val (garray, i);
684 for (i = 0; i < 10000; i++)
685 if (g_array_index (garray, gint, i) != i)
686 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
688 g_array_free (garray, TRUE);
690 garray = g_array_new (FALSE, FALSE, sizeof (gint));
691 for (i = 0; i < 100; i++)
692 g_array_prepend_val (garray, i);
694 for (i = 0; i < 100; i++)
695 if (g_array_index (garray, gint, i) != (100 - i - 1))
696 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
698 g_array_free (garray, TRUE);
703 g_print ("checking strings...");
705 string1 = g_string_new ("hi pete!");
706 string2 = g_string_new ("");
708 g_assert (strcmp ("hi pete!", string1->str) == 0);
710 for (i = 0; i < 10000; i++)
711 g_string_append_c (string1, 'a'+(i%26));
714 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
715 the %10000.10000f format... */
716 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
717 "this pete guy sure is a wuss, like he's the number ",
719 " wuss. everyone agrees.\n",
721 10, 666, 15, 15, 666.666666666, 666.666666666);
723 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
724 "this pete guy sure is a wuss, like he's the number ",
726 " wuss. everyone agrees.\n",
728 10, 666, 15, 15, 666.666666666, 666.666666666);
731 g_print ("string2 length = %d...\n", string2->len);
732 string2->str[70] = '\0';
733 g_print ("first 70 chars:\n%s\n", string2->str);
734 string2->str[141] = '\0';
735 g_print ("next 70 chars:\n%s\n", string2->str+71);
736 string2->str[212] = '\0';
737 g_print ("and next 70:\n%s\n", string2->str+142);
738 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
740 g_string_free (string1, TRUE);
741 g_string_free (string2, TRUE);
744 string1 = g_string_new ("firsthalf");
745 g_string_append (string1, "lasthalf");
746 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
747 g_string_free (string1, TRUE);
751 string1 = g_string_new ("firsthalf");
752 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
753 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
754 g_string_free (string1, TRUE);
757 string1 = g_string_new ("lasthalf");
758 g_string_prepend (string1, "firsthalf");
759 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
760 g_string_free (string1, TRUE);
763 string1 = g_string_new ("lasthalf");
764 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
765 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
766 g_string_free (string1, TRUE);
769 string1 = g_string_new ("firstlast");
770 g_string_insert (string1, 5, "middle");
771 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
772 g_string_free (string1, TRUE);
774 /* insert with pos == end of the string */
775 string1 = g_string_new ("firstmiddle");
776 g_string_insert (string1, strlen ("firstmiddle"), "last");
777 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
778 g_string_free (string1, TRUE);
782 string1 = g_string_new ("firstlast");
783 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
784 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
785 g_string_free (string1, TRUE);
787 /* insert_len with magic -1 pos for append */
788 string1 = g_string_new ("first");
789 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
790 g_assert (strcmp (string1->str, "firstlast") == 0);
791 g_string_free (string1, TRUE);
793 /* insert_len with magic -1 len for strlen-the-string */
794 string1 = g_string_new ("first");
795 g_string_insert_len (string1, 5, "last", -1);
796 g_assert (strcmp (string1->str, "firstlast") == 0);
797 g_string_free (string1, TRUE);
802 string1 = g_string_new ("test");
803 string2 = g_string_new ("te");
804 g_assert (! g_string_equal(string1, string2));
805 g_string_append (string2, "st");
806 g_assert (g_string_equal(string1, string2));
807 g_string_free (string1, TRUE);
808 g_string_free (string2, TRUE);
810 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
811 string1 = g_string_new ("fiddle");
812 string2 = g_string_new ("fiddle");
813 g_assert (g_string_equal(string1, string2));
814 g_string_append_c(string1, '\0');
815 g_assert (! g_string_equal(string1, string2));
816 g_string_append_c(string2, '\0');
817 g_assert (g_string_equal(string1, string2));
818 g_string_append_c(string1, 'x');
819 g_string_append_c(string2, 'y');
820 g_assert (! g_string_equal(string1, string2));
821 g_assert (string1->len == 8);
822 g_string_append(string1, "yzzy");
823 g_assert (string1->len == 12);
824 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
825 g_string_insert(string1, 1, "QED");
826 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
827 g_string_free (string1, TRUE);
828 g_string_free (string2, TRUE);
830 g_print ("checking timers...\n");
832 timer = g_timer_new ();
833 g_print (" spinning for 3 seconds...\n");
835 g_timer_start (timer);
836 while (g_timer_elapsed (timer, NULL) < 3)
839 g_timer_stop (timer);
840 g_timer_destroy (timer);
844 g_print ("checking g_strcasecmp...");
845 g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0);
846 g_assert (g_strcasecmp ("frobozz", "frobozz") == 0);
847 g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0);
848 g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0);
849 g_assert (g_strcasecmp ("", "") == 0);
850 g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0);
851 g_assert (g_strcasecmp ("a", "b") < 0);
852 g_assert (g_strcasecmp ("a", "B") < 0);
853 g_assert (g_strcasecmp ("A", "b") < 0);
854 g_assert (g_strcasecmp ("A", "B") < 0);
855 g_assert (g_strcasecmp ("b", "a") > 0);
856 g_assert (g_strcasecmp ("b", "A") > 0);
857 g_assert (g_strcasecmp ("B", "a") > 0);
858 g_assert (g_strcasecmp ("B", "A") > 0);
862 g_print ("checking g_strdup...");
863 g_assert(g_strdup(NULL) == NULL);
864 string = g_strdup(GLIB_TEST_STRING);
865 g_assert(string != NULL);
866 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
871 g_print ("checking g_strconcat...");
872 string = g_strconcat(GLIB_TEST_STRING, NULL);
873 g_assert(string != NULL);
874 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
876 string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
877 GLIB_TEST_STRING, NULL);
878 g_assert(string != NULL);
879 g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
880 GLIB_TEST_STRING) == 0);
885 g_print ("checking g_strdup_printf...");
886 string = g_strdup_printf ("%05d %-5s", 21, "test");
887 g_assert (string != NULL);
888 g_assert (strcmp(string, "00021 test ") == 0);
893 /* g_debug (argv[0]); */
897 g_print ("checking relations...");
899 relation = g_relation_new (2);
901 g_relation_index (relation, 0, g_int_hash, g_int_equal);
902 g_relation_index (relation, 1, g_int_hash, g_int_equal);
904 for (i = 0; i < 1024; i += 1)
907 for (i = 1; i < 1023; i += 1)
909 g_relation_insert (relation, data + i, data + i + 1);
910 g_relation_insert (relation, data + i, data + i - 1);
913 for (i = 2; i < 1022; i += 1)
915 g_assert (! g_relation_exists (relation, data + i, data + i));
916 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
917 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
920 for (i = 1; i < 1023; i += 1)
922 g_assert (g_relation_exists (relation, data + i, data + i + 1));
923 g_assert (g_relation_exists (relation, data + i, data + i - 1));
926 for (i = 2; i < 1022; i += 1)
928 g_assert (g_relation_count (relation, data + i, 0) == 2);
929 g_assert (g_relation_count (relation, data + i, 1) == 2);
932 g_assert (g_relation_count (relation, data, 0) == 0);
934 g_assert (g_relation_count (relation, data + 42, 0) == 2);
935 g_assert (g_relation_count (relation, data + 43, 1) == 2);
936 g_assert (g_relation_count (relation, data + 41, 1) == 2);
937 g_relation_delete (relation, data + 42, 0);
938 g_assert (g_relation_count (relation, data + 42, 0) == 0);
939 g_assert (g_relation_count (relation, data + 43, 1) == 1);
940 g_assert (g_relation_count (relation, data + 41, 1) == 1);
942 tuples = g_relation_select (relation, data + 200, 0);
944 g_assert (tuples->len == 2);
947 for (i = 0; i < tuples->len; i += 1)
950 *(gint*) g_tuples_index (tuples, i, 0),
951 *(gint*) g_tuples_index (tuples, i, 1));
955 g_assert (g_relation_exists (relation, data + 300, data + 301));
956 g_relation_delete (relation, data + 300, 0);
957 g_assert (!g_relation_exists (relation, data + 300, data + 301));
959 g_tuples_destroy (tuples);
961 g_relation_destroy (relation);
967 g_print ("checking pointer arrays...");
969 gparray = g_ptr_array_new ();
970 for (i = 0; i < 10000; i++)
971 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
973 for (i = 0; i < 10000; i++)
974 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
975 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
977 g_ptr_array_free (gparray, TRUE);
982 g_print ("checking byte arrays...");
984 gbarray = g_byte_array_new ();
985 for (i = 0; i < 10000; i++)
986 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
988 for (i = 0; i < 10000; i++)
990 g_assert (gbarray->data[4*i] == 'a');
991 g_assert (gbarray->data[4*i+1] == 'b');
992 g_assert (gbarray->data[4*i+2] == 'c');
993 g_assert (gbarray->data[4*i+3] == 'd');
996 g_byte_array_free (gbarray, TRUE);
999 g_printerr ("g_log tests:");
1000 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1001 g_message ("the next warning is a test:");
1005 g_print ("checking endian macros (host is ");
1006 #if G_BYTE_ORDER == G_BIG_ENDIAN
1007 g_print ("big endian)...");
1009 g_print ("little endian)...");
1011 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
1012 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
1013 #ifdef G_HAVE_GINT64
1014 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
1020 g_print ("current locale: %s\n", g_win32_getlocale ());