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/.
27 #undef GLIB_COMPILATION
34 #include <glib/gstdio.h>
43 #include <io.h> /* For read(), write() etc */
47 #define GLIB_TEST_STRING "el dorado "
48 #define GLIB_TEST_STRING_5 "el do"
51 /* --- variables --- */
52 static gint test_nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
53 static gint more_nums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
55 /* --- functions --- */
57 my_list_compare_one (gconstpointer a, gconstpointer b)
59 gint one = *((const gint*)a);
60 gint two = *((const gint*)b);
65 my_list_compare_two (gconstpointer a, gconstpointer b)
67 gint one = *((const gint*)a);
68 gint two = *((const gint*)b);
73 my_list_print (gpointer a, gpointer b)
75 gint three = *((gint*)a);
85 for (i = 0; i < 10; i++)
86 list = g_list_append (list, &test_nums[i]);
87 list = g_list_reverse (list);
89 for (i = 0; i < 10; i++)
91 GList *t = g_list_nth (list, i);
92 if (*((gint*) t->data) != (9 - i))
93 g_error ("Regular insert failed");
96 for (i = 0; i < 10; i++)
97 if (g_list_position (list, g_list_nth (list, i)) != i)
98 g_error ("g_list_position does not seem to be the inverse of g_list_nth\n");
103 for (i = 0; i < 10; i++)
104 list = g_list_insert_sorted (list, &more_nums[i], my_list_compare_one);
108 g_list_foreach (list, my_list_print, NULL);
111 for (i = 0; i < 10; i++)
113 GList *t = g_list_nth (list, i);
114 if (*((gint*) t->data) != i)
115 g_error ("Sorted insert failed");
121 for (i = 0; i < 10; i++)
122 list = g_list_insert_sorted (list, &more_nums[i], my_list_compare_two);
126 g_list_foreach (list, my_list_print, NULL);
129 for (i = 0; i < 10; i++)
131 GList *t = g_list_nth (list, i);
132 if (*((gint*) t->data) != (9 - i))
133 g_error ("Sorted insert failed");
139 for (i = 0; i < 10; i++)
140 list = g_list_prepend (list, &more_nums[i]);
142 list = g_list_sort (list, my_list_compare_two);
146 g_list_foreach (list, my_list_print, NULL);
149 for (i = 0; i < 10; i++)
151 GList *t = g_list_nth (list, i);
152 if (*((gint*) t->data) != (9 - i))
153 g_error ("Merge sort failed");
162 GSList *slist = NULL;
165 for (i = 0; i < 10; i++)
166 slist = g_slist_append (slist, &test_nums[i]);
167 slist = g_slist_reverse (slist);
169 for (i = 0; i < 10; i++)
171 GSList *st = g_slist_nth (slist, i);
172 if (*((gint*) st->data) != (9 - i))
176 g_slist_free (slist);
179 for (i = 0; i < 10; i++)
180 slist = g_slist_insert_sorted (slist, &more_nums[i], my_list_compare_one);
184 g_slist_foreach (slist, my_list_print, NULL);
187 for (i = 0; i < 10; i++)
189 GSList *st = g_slist_nth (slist, i);
190 if (*((gint*) st->data) != i)
191 g_error ("Sorted insert failed");
194 g_slist_free (slist);
197 for (i = 0; i < 10; i++)
198 slist = g_slist_insert_sorted (slist, &more_nums[i], my_list_compare_two);
202 g_slist_foreach (slist, my_list_print, NULL);
205 for (i = 0; i < 10; i++)
207 GSList *st = g_slist_nth (slist, i);
208 if (*((gint*) st->data) != (9 - i))
209 g_error("Sorted insert failed");
215 for (i = 0; i < 10; i++)
216 slist = g_slist_prepend (slist, &more_nums[i]);
218 slist = g_slist_sort (slist, my_list_compare_two);
222 g_slist_foreach (slist, my_list_print, NULL);
225 for (i = 0; i < 10; i++)
227 GSList *st = g_slist_nth (slist, i);
228 if (*((gint*) st->data) != (9 - i))
229 g_error("Sorted insert failed");
236 node_build_string (GNode *node,
243 c[0] = ((gchar) ((gintptr) (node->data)));
245 string = g_strconcat (*p ? *p : "", c, NULL);
255 #define C2P(c) ((gpointer) ((long) (c)))
256 #define P2C(p) ((gchar) ((gintptr) (p)))
264 gchar *tstring, *cstring;
266 root = g_node_new (C2P ('A'));
267 g_assert (g_node_depth (root) == 1 && g_node_max_height (root) == 1);
269 node_B = g_node_new (C2P ('B'));
270 g_node_append (root, node_B);
271 g_assert (root->children == node_B);
273 g_node_append_data (node_B, C2P ('E'));
274 g_node_prepend_data (node_B, C2P ('C'));
275 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
277 node_F = g_node_new (C2P ('F'));
278 g_node_append (root, node_F);
279 g_assert (root->children->next == node_F);
281 node_G = g_node_new (C2P ('G'));
282 g_node_append (node_F, node_G);
283 node_J = g_node_new (C2P ('J'));
284 g_node_prepend (node_G, node_J);
285 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
286 g_node_insert_data (node_G, 0, C2P ('H'));
287 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
289 g_assert (g_node_depth (root) == 1);
290 g_assert (g_node_max_height (root) == 4);
291 g_assert (g_node_depth (node_G->children->next) == 4);
292 g_assert (g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
293 g_assert (g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
294 g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
295 g_assert (g_node_max_height (node_F) == 3);
296 g_assert (g_node_n_children (node_G) == 4);
297 g_assert (g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
298 g_assert (g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
299 g_assert (g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
301 for (i = 0; i < g_node_n_children (node_B); i++)
303 node = g_node_nth_child (node_B, i);
304 g_assert (P2C (node->data) == ('C' + i));
307 for (i = 0; i < g_node_n_children (node_G); i++)
308 g_assert (g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
318 * for in-order traversal, 'G' is considered to be the "left"
319 * child of 'F', which will cause 'F' to be the last node visited.
323 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
324 g_assert_cmpstr (tstring, ==, "ABCDEFGHIJK");
325 g_free (tstring); tstring = NULL;
326 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
327 g_assert_cmpstr (tstring, ==, "CDEBHIJKGFA");
328 g_free (tstring); tstring = NULL;
329 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
330 g_assert_cmpstr (tstring, ==, "CBDEAHGIJKF");
331 g_free (tstring); tstring = NULL;
332 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
333 g_assert_cmpstr (tstring, ==, "ABFCDEGHIJK");
334 g_free (tstring); tstring = NULL;
336 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
337 g_assert_cmpstr (tstring, ==, "CDEHIJK");
338 g_free (tstring); tstring = NULL;
339 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
340 g_assert_cmpstr (tstring, ==, "ABFG");
341 g_free (tstring); tstring = NULL;
343 g_node_reverse_children (node_B);
344 g_node_reverse_children (node_G);
346 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
347 g_assert_cmpstr (tstring, ==, "ABFEDCGKJIH");
348 g_free (tstring); tstring = NULL;
351 node = g_node_copy (root);
352 g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
353 g_assert (g_node_max_height (root) == g_node_max_height (node));
354 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
355 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
356 g_assert_cmpstr (tstring, ==, cstring);
357 g_free (tstring); tstring = NULL;
358 g_free (cstring); cstring = NULL;
359 g_node_destroy (node);
361 g_node_destroy (root);
363 /* allocation tests */
365 root = g_node_new (NULL);
368 for (i = 0; i < 2048; i++)
370 g_node_append (node, g_node_new (NULL));
372 node = node->children->next;
374 g_assert (g_node_max_height (root) > 100);
375 g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
377 g_node_destroy (root);
383 my_compare (gconstpointer a,
393 my_traverse (gpointer key,
398 g_print ("%c ", *ch);
403 binary_tree_test (void)
409 tree = g_tree_new (my_compare);
411 for (j = 0; j < 10; j++, i++)
414 g_tree_insert (tree, &chars[i], &chars[i]);
416 for (j = 0; j < 26; j++, i++)
419 g_tree_insert (tree, &chars[i], &chars[i]);
421 for (j = 0; j < 26; j++, i++)
424 g_tree_insert (tree, &chars[i], &chars[i]);
427 g_assert_cmpint (g_tree_nnodes (tree), ==, 10 + 26 + 26);
428 g_assert_cmpint (g_tree_height (tree), ==, 6);
430 if (g_test_verbose())
433 g_tree_foreach (tree, my_traverse, NULL);
437 for (i = 0; i < 10; i++)
438 g_tree_remove (tree, &chars[i]);
440 g_assert_cmpint (g_tree_nnodes (tree), ==, 26 + 26);
441 g_assert_cmpint (g_tree_height (tree), ==, 6);
443 if (g_test_verbose())
446 g_tree_foreach (tree, my_traverse, NULL);
454 my_hash_callback_remove (gpointer key,
467 my_hash_callback_remove_test (gpointer key,
478 my_hash_callback (gpointer key,
487 my_hash (gconstpointer key)
489 return (guint) *((const gint*) key);
493 my_hash_equal (gconstpointer a,
496 return *((const gint*) a) == *((const gint*) b);
500 find_first_that(gpointer key,
505 gint *test = user_data;
506 return (*v == *test);
510 test_g_parse_debug_string (void)
518 guint n_keys = G_N_ELEMENTS (keys);
521 result = g_parse_debug_string ("bar:foo:blubb", keys, n_keys);
522 g_assert (result == 3);
524 result = g_parse_debug_string (":baz::_E@~!_::", keys, n_keys);
525 g_assert (result == 4);
527 result = g_parse_debug_string ("", keys, n_keys);
528 g_assert (result == 0);
530 result = g_parse_debug_string (" : ", keys, n_keys);
531 g_assert (result == 0);
533 result = g_parse_debug_string ("all", keys, n_keys);
534 g_assert_cmpuint (result, ==, (1 << n_keys) - 1);
536 /* Test subtracting debug flags from "all" */
537 result = g_parse_debug_string ("all:foo", keys, n_keys);
538 g_assert_cmpuint (result, ==, 2 | 4 | 8);
540 result = g_parse_debug_string ("foo baz,all", keys, n_keys);
541 g_assert_cmpuint (result, ==, 2 | 8);
543 result = g_parse_debug_string ("all,fooo,baz", keys, n_keys);
544 g_assert_cmpuint (result, ==, 1 | 2 | 8);
546 result = g_parse_debug_string ("all:weird", keys, n_keys);
547 g_assert_cmpuint (result, ==, 1 | 2 | 4);
551 log_warning_error_tests (void)
553 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE,
554 "*is a g_message test*");
555 g_message ("this is a g_message test.");
556 g_test_assert_expected_messages ();
558 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE,
559 "*non-printable UTF-8*");
560 g_message ("non-printable UTF-8: \"\xc3\xa4\xda\x85\"");
561 g_test_assert_expected_messages ();
563 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE,
565 g_message ("unsafe chars: \"\x10\x11\x12\n\t\x7f\x81\x82\x83\"");
566 g_test_assert_expected_messages ();
568 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
569 "*harmless warning*");
570 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
571 g_test_assert_expected_messages ();
573 g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
574 "*g_print*assertion*failed*");
576 g_test_assert_expected_messages ();
582 GTimer *timer, *timer2;
586 timer = g_timer_new ();
587 g_timer_start (timer);
588 elapsed = g_timer_elapsed (timer, NULL);
589 g_timer_stop (timer);
590 g_assert_cmpfloat (elapsed, <=, g_timer_elapsed (timer, NULL));
591 g_timer_destroy (timer);
595 if (g_test_verbose())
596 g_print ("checking timers...\n");
597 timer = g_timer_new ();
598 if (g_test_verbose())
599 g_print (" spinning for 3 seconds...\n");
600 g_timer_start (timer);
601 while (g_timer_elapsed (timer, NULL) < 3)
603 g_timer_stop (timer);
604 g_timer_destroy (timer);
605 if (g_test_verbose())
611 gulong elapsed_usecs;
612 if (g_test_verbose())
613 g_print ("checking g_timer_continue...\n");
614 timer2 = g_timer_new ();
615 if (g_test_verbose())
616 g_print ("\trun for 1 second...\n");
617 timer = g_timer_new();
618 g_usleep (G_USEC_PER_SEC); /* run timer for 1 second */
619 g_timer_stop (timer);
620 if (g_test_verbose())
621 g_print ("\tstop for 1 second...\n");
622 g_usleep (G_USEC_PER_SEC); /* wait for 1 second */
623 if (g_test_verbose())
624 g_print ("\trun for 2 seconds...\n");
625 g_timer_continue (timer);
626 g_usleep (2 * G_USEC_PER_SEC); /* run timer for 2 seconds */
628 if (g_test_verbose())
629 g_print ("\tstop for 1.5 seconds...\n");
630 g_usleep ((3 * G_USEC_PER_SEC) / 2); /* wait for 1.5 seconds */
631 if (g_test_verbose())
632 g_print ("\trun for 0.2 seconds...\n");
633 g_timer_continue (timer);
634 g_usleep (G_USEC_PER_SEC / 5); /* run timer for 0.2 seconds */
635 g_timer_stop (timer);
636 if (g_test_verbose())
637 g_print ("\tstop for 4 seconds...\n");
638 g_usleep (4 * G_USEC_PER_SEC); /* wait for 4 seconds */
639 if (g_test_verbose())
640 g_print ("\trun for 5.8 seconds...\n");
641 g_timer_continue (timer);
642 g_usleep ((29 * G_USEC_PER_SEC) / 5); /* run timer for 5.8 seconds */
644 elapsed = g_timer_elapsed (timer, &elapsed_usecs);
645 if (g_test_verbose())
646 g_print ("\t=> timer = %.6f = %d.%06ld (should be: 9.000000) (%.6f off)\n", elapsed, (int) elapsed, elapsed_usecs, ABS (elapsed - 9.));
647 g_assert_cmpfloat (elapsed, >, 8.8);
648 g_assert_cmpfloat (elapsed, <, 9.2);
649 if (g_test_verbose())
650 g_print ("g_timer_continue ... ok\n\n");
651 g_timer_stop (timer2);
652 elapsed = g_timer_elapsed (timer2, &elapsed_usecs);
653 if (g_test_verbose())
654 g_print ("\t=> timer2 = %.6f = %d.%06ld (should be: %.6f) (%.6f off)\n\n", elapsed, (int) elapsed, elapsed_usecs, 9.+6.5, ABS (elapsed - (9.+6.5)));
655 g_assert_cmpfloat (elapsed, >, 8.8 + 6.5);
656 g_assert_cmpfloat (elapsed, <, 9.2 + 6.5);
657 if (g_test_verbose())
658 g_print ("timer2 ... ok\n\n");
659 g_timer_destroy (timer);
660 g_timer_destroy (timer2);
667 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
668 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
669 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
670 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
672 g_assert_cmpint (sizeof (gint8), ==, 1);
673 g_assert_cmpint (sizeof (gint16), ==, 2);
674 g_assert_cmpint (sizeof (gint32), ==, 4);
675 g_assert_cmpint (sizeof (gint64), ==, 8);
677 if (g_test_verbose())
678 g_print ("checking endian macros (host is %s)...\n",
679 G_BYTE_ORDER == G_BIG_ENDIAN ? "big endian" : "little endian");
680 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
681 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
682 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
688 const gchar *un, *rn, *hn;
689 const gchar *tmpdir, *homedir, *userdatadir, *uconfdir, *ucachedir;
690 const gchar *uddesktop, *udddocs, *uddpubshare, *uruntimedir;
691 gchar **sv, *cwd, *sdatadirs, *sconfdirs, *langnames;
692 const gchar *charset;
693 gboolean charset_is_utf8;
694 if (g_test_verbose())
695 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
702 cwd = g_get_current_dir ();
703 un = g_get_user_name();
704 rn = g_get_real_name();
705 hn = g_get_host_name();
706 if (g_test_verbose())
708 g_print ("cwd: %s\n", cwd);
709 g_print ("user: %s\n", un);
710 g_print ("real: %s\n", rn);
711 g_print ("host: %s\n", hn);
715 /* reload, just for fun */
716 g_reload_user_special_dirs_cache ();
717 g_reload_user_special_dirs_cache ();
719 tmpdir = g_get_tmp_dir();
720 g_assert (tmpdir != NULL);
721 homedir = g_get_home_dir ();
722 g_assert (homedir != NULL);
723 userdatadir = g_get_user_data_dir ();
724 g_assert (userdatadir != NULL);
725 uconfdir = g_get_user_config_dir ();
726 g_assert (uconfdir != NULL);
727 ucachedir = g_get_user_cache_dir ();
728 g_assert (ucachedir != NULL);
730 uddesktop = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
731 g_assert (uddesktop != NULL);
732 udddocs = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
733 uddpubshare = g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE);
734 uruntimedir = g_get_user_runtime_dir ();
735 g_assert (uruntimedir != NULL);
737 sv = (gchar **) g_get_system_data_dirs ();
738 sdatadirs = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv);
739 sv = (gchar **) g_get_system_config_dirs ();
740 sconfdirs = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv);
741 sv = (gchar **) g_get_language_names ();
742 langnames = g_strjoinv (":", sv);
744 if (g_test_verbose())
746 g_print ("tmp-dir: %s\n", tmpdir);
747 g_print ("home: %s\n", homedir);
748 g_print ("user_data: %s\n", userdatadir);
749 g_print ("user_config: %s\n", uconfdir);
750 g_print ("user_cache: %s\n", ucachedir);
751 g_print ("user_runtime: %s\n", uruntimedir);
752 g_print ("system_data: %s\n", sdatadirs);
753 g_print ("system_config: %s\n", sconfdirs);
754 g_print ("languages: %s\n", langnames);
755 g_print ("user_special[DESKTOP]: %s\n", uddesktop);
756 g_print ("user_special[DOCUMENTS]: %s\n", udddocs);
757 g_print ("user_special[PUBLIC_SHARE]: %s\n", uddpubshare);
763 charset_is_utf8 = g_get_charset ((const char**)&charset);
765 if (g_test_verbose())
768 g_print ("current charset is UTF-8: %s\n", charset);
770 g_print ("current charset is not UTF-8: %s\n", charset);
773 if (g_test_verbose())
775 #ifdef G_PLATFORM_WIN32
776 g_print ("current locale: %s\n", g_win32_getlocale ());
778 g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
779 g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
781 g_print ("a Win32 error message: %s\n", g_win32_error_message (2));
792 } dirname_checks[] = {
803 { ".\\\\\\\\", "." },
805 { "..\\\\\\\\", ".." },
813 { "//server/share///x", "//server/share" },
819 const guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
823 } skip_root_checks[] = {
832 { "\\\\server\\foo", "" },
833 { "\\\\server\\foo\\bar", "bar" },
837 { "//server/share///x", "//x" },
842 const guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
845 if (g_test_verbose())
846 g_print ("checking g_path_get_basename()...");
847 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
848 g_assert (strcmp (string, "dir") == 0);
850 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
851 g_assert (strcmp (string, "file") == 0);
853 if (g_test_verbose())
857 string = g_path_get_basename ("/foo/dir/");
858 g_assert (strcmp (string, "dir") == 0);
860 string = g_path_get_basename ("/foo/file");
861 g_assert (strcmp (string, "file") == 0);
865 if (g_test_verbose())
866 g_print ("checking g_path_get_dirname()...");
867 for (i = 0; i < n_dirname_checks; i++)
869 gchar *dirname = g_path_get_dirname (dirname_checks[i].filename);
870 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
872 g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
873 dirname_checks[i].filename,
874 dirname_checks[i].dirname,
879 if (g_test_verbose())
882 if (g_test_verbose())
883 g_print ("checking g_path_skip_root()...");
884 for (i = 0; i < n_skip_root_checks; i++)
886 const gchar *skipped = g_path_skip_root (skip_root_checks[i].filename);
887 if ((skipped && !skip_root_checks[i].without_root) ||
888 (!skipped && skip_root_checks[i].without_root) ||
889 ((skipped && skip_root_checks[i].without_root) &&
890 strcmp (skipped, skip_root_checks[i].without_root)))
892 g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
893 skip_root_checks[i].filename,
894 (skip_root_checks[i].without_root ?
895 skip_root_checks[i].without_root : "<NULL>"),
896 (skipped ? skipped : "<NULL>"));
899 if (g_test_verbose())
904 test_file_functions (void)
906 const char hello[] = "Hello, World";
907 const int hellolen = sizeof (hello) - 1;
910 char *name_used, chars[62];
913 strcpy (template, "foobar");
914 fd = g_mkstemp (template);
915 if (g_test_verbose() && fd != -1)
916 g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
919 strcpy (template, "fooXXXXXX");
920 fd = g_mkstemp (template);
922 g_error ("g_mkstemp didn't work for template %s\n", template);
923 n = write (fd, hello, hellolen);
925 g_error ("write() failed: %s\n", g_strerror (errno));
926 else if (n != hellolen)
927 g_error ("write() should have written %d bytes, wrote %d\n", hellolen, n);
930 n = read (fd, chars, sizeof (chars));
932 g_error ("read() failed: %s\n", g_strerror (errno));
933 else if (n != hellolen)
934 g_error ("read() should have read %d bytes, got %d\n", hellolen, n);
937 if (strcmp (chars, hello) != 0)
938 g_error ("wrote '%s', but got '%s'\n", hello, chars);
945 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
946 fd = g_file_open_tmp (template, &name_used, &error);
947 if (g_test_verbose())
950 g_print ("g_file_open_tmp works even if template contains '%s'\n", G_DIR_SEPARATOR_S);
952 g_print ("g_file_open_tmp correctly returns error: %s\n", error->message);
956 g_clear_error (&error);
961 strcpy (template, "zap/barXXXXXX");
962 fd = g_file_open_tmp (template, &name_used, &error);
963 if (g_test_verbose())
966 g_print ("g_file_open_tmp works even if template contains '/'\n");
968 g_print ("g_file_open_tmp correctly returns error: %s\n", error->message);
972 g_clear_error (&error);
976 strcpy (template, "zapXXXXXX");
978 fd = g_file_open_tmp (template, &name_used, &error);
980 g_error ("g_file_open_tmp didn't work for template '%s': %s\n", template, error->message);
981 else if (g_test_verbose())
982 g_print ("g_file_open_tmp for template '%s' used name '%s'\n", template, name_used);
985 g_clear_error (&error);
990 fd = g_file_open_tmp (NULL, &name_used, &error);
992 g_error ("g_file_open_tmp didn't work for a NULL template: %s\n", error->message);
995 g_clear_error (&error);
1003 GByteArray *gbarray;
1008 gparray = g_ptr_array_new ();
1009 for (i = 0; i < 10000; i++)
1010 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1011 for (i = 0; i < 10000; i++)
1012 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1013 g_error ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1014 g_ptr_array_free (gparray, TRUE);
1016 gbarray = g_byte_array_new ();
1017 for (i = 0; i < 10000; i++)
1018 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1019 for (i = 0; i < 10000; i++)
1021 g_assert (gbarray->data[4*i] == 'a');
1022 g_assert (gbarray->data[4*i+1] == 'b');
1023 g_assert (gbarray->data[4*i+2] == 'c');
1024 g_assert (gbarray->data[4*i+3] == 'd');
1026 g_byte_array_free (gbarray, TRUE);
1028 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1029 for (i = 0; i < 10000; i++)
1030 g_array_append_val (garray, i);
1031 for (i = 0; i < 10000; i++)
1032 if (g_array_index (garray, gint, i) != i)
1033 g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), i);
1034 g_array_free (garray, TRUE);
1036 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1037 for (i = 0; i < 100; i++)
1038 g_array_prepend_val (garray, i);
1039 for (i = 0; i < 100; i++)
1040 if (g_array_index (garray, gint, i) != (100 - i - 1))
1041 g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
1042 g_array_free (garray, TRUE);
1046 hash_table_tests (void)
1048 GHashTable *hash_table;
1050 gint *pvalue = NULL;
1054 hash_table = g_hash_table_new (my_hash, my_hash_equal);
1055 for (i = 0; i < 10000; i++)
1058 g_hash_table_insert (hash_table, &array[i], &array[i]);
1060 pvalue = g_hash_table_find (hash_table, find_first_that, &value);
1061 if (*pvalue != value)
1062 g_error ("g_hash_table_find failed");
1063 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
1064 for (i = 0; i < 10000; i++)
1066 g_error ("hashtable-test: wrong value: %d\n", i);
1067 for (i = 0; i < 10000; i++)
1068 g_hash_table_remove (hash_table, &array[i]);
1069 for (i = 0; i < 10000; i++)
1072 g_hash_table_insert (hash_table, &array[i], &array[i]);
1074 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
1075 g_hash_table_size (hash_table) != 5000)
1076 g_error ("hashtable removal failed\n");
1077 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
1078 g_hash_table_destroy (hash_table);
1081 #ifndef G_DISABLE_DEPRECATED
1083 relation_test (void)
1085 GRelation *relation = g_relation_new (2);
1090 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1091 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1093 for (i = 0; i < 1024; i += 1)
1096 for (i = 1; i < 1023; i += 1)
1098 g_relation_insert (relation, data + i, data + i + 1);
1099 g_relation_insert (relation, data + i, data + i - 1);
1102 for (i = 2; i < 1022; i += 1)
1104 g_assert (! g_relation_exists (relation, data + i, data + i));
1105 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1106 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1109 for (i = 1; i < 1023; i += 1)
1111 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1112 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1115 for (i = 2; i < 1022; i += 1)
1117 g_assert (g_relation_count (relation, data + i, 0) == 2);
1118 g_assert (g_relation_count (relation, data + i, 1) == 2);
1121 g_assert (g_relation_count (relation, data, 0) == 0);
1123 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1124 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1125 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1126 g_relation_delete (relation, data + 42, 0);
1127 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1128 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1129 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1131 tuples = g_relation_select (relation, data + 200, 0);
1133 g_assert (tuples->len == 2);
1136 for (i = 0; i < tuples->len; i += 1)
1139 *(gint*) g_tuples_index (tuples, i, 0),
1140 *(gint*) g_tuples_index (tuples, i, 1));
1144 g_assert (g_relation_exists (relation, data + 300, data + 301));
1145 g_relation_delete (relation, data + 300, 0);
1146 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1148 g_tuples_destroy (tuples);
1150 g_relation_destroy (relation);
1157 gstring_tests (void)
1159 GString *string1, *string2;
1162 if (g_test_verbose())
1163 g_print ("test GString basics\n");
1165 string1 = g_string_new ("hi pete!");
1166 string2 = g_string_new ("");
1168 g_assert (strcmp ("hi pete!", string1->str) == 0);
1170 for (i = 0; i < 10000; i++)
1171 g_string_append_c (string1, 'a'+(i%26));
1174 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
1175 the %10000.10000f format... */
1176 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
1177 "this pete guy sure is a wuss, like he's the number ",
1179 " wuss. everyone agrees.\n",
1181 10, 666, 15, 15, 666.666666666, 666.666666666);
1183 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
1184 "this pete guy sure is a wuss, like he's the number ",
1186 " wuss. everyone agrees.\n",
1188 10, 666, 15, 15, 666.666666666, 666.666666666);
1191 if (g_test_verbose())
1192 g_print ("string2 length = %lu...\n", (gulong)string2->len);
1193 string2->str[70] = '\0';
1194 if (g_test_verbose())
1195 g_print ("first 70 chars:\n%s\n", string2->str);
1196 string2->str[141] = '\0';
1197 if (g_test_verbose())
1198 g_print ("next 70 chars:\n%s\n", string2->str+71);
1199 string2->str[212] = '\0';
1200 if (g_test_verbose())
1201 g_print ("and next 70:\n%s\n", string2->str+142);
1202 if (g_test_verbose())
1203 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
1205 g_string_free (string1, TRUE);
1206 g_string_free (string2, TRUE);
1209 string1 = g_string_new ("firsthalf");
1210 g_string_append (string1, "lasthalf");
1211 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1212 g_string_free (string1, TRUE);
1215 string1 = g_string_new ("firsthalf");
1216 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
1217 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1218 g_string_free (string1, TRUE);
1221 string1 = g_string_new ("lasthalf");
1222 g_string_prepend (string1, "firsthalf");
1223 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1224 g_string_free (string1, TRUE);
1227 string1 = g_string_new ("lasthalf");
1228 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
1229 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1230 g_string_free (string1, TRUE);
1233 string1 = g_string_new ("firstlast");
1234 g_string_insert (string1, 5, "middle");
1235 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1236 g_string_free (string1, TRUE);
1238 /* insert with pos == end of the string */
1239 string1 = g_string_new ("firstmiddle");
1240 g_string_insert (string1, strlen ("firstmiddle"), "last");
1241 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1242 g_string_free (string1, TRUE);
1245 string1 = g_string_new ("firstlast");
1246 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
1247 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1248 g_string_free (string1, TRUE);
1250 /* insert_len with magic -1 pos for append */
1251 string1 = g_string_new ("first");
1252 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
1253 g_assert (strcmp (string1->str, "firstlast") == 0);
1254 g_string_free (string1, TRUE);
1256 /* insert_len with magic -1 len for strlen-the-string */
1257 string1 = g_string_new ("first");
1258 g_string_insert_len (string1, 5, "last", -1);
1259 g_assert (strcmp (string1->str, "firstlast") == 0);
1260 g_string_free (string1, TRUE);
1262 /* g_string_equal */
1263 string1 = g_string_new ("test");
1264 string2 = g_string_new ("te");
1265 g_assert (! g_string_equal(string1, string2));
1266 g_string_append (string2, "st");
1267 g_assert (g_string_equal(string1, string2));
1268 g_string_free (string1, TRUE);
1269 g_string_free (string2, TRUE);
1271 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
1272 if (g_test_verbose())
1273 g_print ("test embedded ASCII 0 (NUL) characters in GString\n");
1274 string1 = g_string_new ("fiddle");
1275 string2 = g_string_new ("fiddle");
1276 g_assert (g_string_equal(string1, string2));
1277 g_string_append_c(string1, '\0');
1278 g_assert (! g_string_equal(string1, string2));
1279 g_string_append_c(string2, '\0');
1280 g_assert (g_string_equal(string1, string2));
1281 g_string_append_c(string1, 'x');
1282 g_string_append_c(string2, 'y');
1283 g_assert (! g_string_equal(string1, string2));
1284 g_assert (string1->len == 8);
1285 g_string_append(string1, "yzzy");
1286 g_assert (string1->len == 12);
1287 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
1288 g_string_insert(string1, 1, "QED");
1289 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
1290 g_string_free (string1, TRUE);
1291 g_string_free (string2, TRUE);
1295 various_string_tests (void)
1297 GStringChunk *string_chunk;
1298 GTimeVal ref_date, date;
1299 gchar *tmp_string = NULL, *tmp_string_2, *string, *date_str;
1303 if (g_test_verbose())
1304 g_print ("checking string chunks...");
1305 string_chunk = g_string_chunk_new (1024);
1306 for (i = 0; i < 100000; i ++)
1308 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
1309 if (strcmp ("hi pete", tmp_string) != 0)
1310 g_error ("string chunks are broken.\n");
1312 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
1313 g_assert (tmp_string_2 != tmp_string && strcmp (tmp_string_2, tmp_string) == 0);
1314 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
1315 g_assert (tmp_string_2 == tmp_string);
1316 g_string_chunk_free (string_chunk);
1318 if (g_test_verbose())
1319 g_print ("test positional printf formats (not supported):");
1320 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
1321 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
1322 if (g_test_verbose())
1323 g_print ("%s%s\n", string, tmp_string);
1324 g_free (tmp_string);
1327 #define REF_INVALID1 "Wed Dec 19 17:20:20 GMT 2007"
1328 #define REF_INVALID2 "1980-02-22T10:36:00Zulu"
1329 #define REF_INVALID3 "1980-02-22T"
1330 #define REF_SEC_UTC 320063760
1331 #define REF_STR_UTC "1980-02-22T10:36:00Z"
1332 #define REF_STR_LOCAL "1980-02-22T13:36:00"
1333 #define REF_STR_CEST "1980-02-22T12:36:00+02:00"
1334 #define REF_STR_EST "19800222T053600-0500"
1335 #define REF_STR_NST "1980-02-22T07:06:00-03:30"
1336 #define REF_USEC_UTC 50000
1337 #define REF_STR_USEC_UTC "1980-02-22T10:36:00.050000Z"
1338 #define REF_STR_USEC_CEST "19800222T123600.050000000+0200"
1339 #define REF_STR_USEC_EST "1980-02-22T05:36:00,05-05:00"
1340 #define REF_STR_USEC_NST "19800222T070600,0500-0330"
1341 #define REF_STR_DATE_ONLY "1980-02-22"
1343 if (g_test_verbose())
1344 g_print ("checking g_time_val_from_iso8601...\n");
1345 ref_date.tv_sec = REF_SEC_UTC;
1346 ref_date.tv_usec = 0;
1347 g_assert (g_time_val_from_iso8601 (REF_INVALID1, &date) == FALSE);
1348 g_assert (g_time_val_from_iso8601 (REF_INVALID2, &date) == FALSE);
1349 g_assert (g_time_val_from_iso8601 (REF_INVALID3, &date) == FALSE);
1350 g_assert (g_time_val_from_iso8601 (REF_STR_DATE_ONLY, &date) != FALSE);
1351 g_assert (g_time_val_from_iso8601 (REF_STR_UTC, &date) != FALSE);
1352 if (g_test_verbose())
1353 g_print ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1354 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1355 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1356 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1358 /* predefine time zone */
1359 tz = g_getenv("TZ");
1360 g_setenv("TZ", "UTC-03:00", 1);
1363 g_assert (g_time_val_from_iso8601 (REF_STR_LOCAL, &date) != FALSE);
1364 if (g_test_verbose())
1365 g_print ("\t=> LOCAL stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1366 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1367 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1368 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1370 /* revert back user defined time zone */
1372 g_setenv("TZ", tz, TRUE);
1377 g_assert (g_time_val_from_iso8601 (REF_STR_CEST, &date) != FALSE);
1378 if (g_test_verbose())
1379 g_print ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1380 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1381 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1382 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1384 g_assert (g_time_val_from_iso8601 (REF_STR_EST, &date) != FALSE);
1385 if (g_test_verbose())
1386 g_print ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1387 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1388 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1389 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1391 g_assert (g_time_val_from_iso8601 (REF_STR_NST, &date) != FALSE);
1392 if (g_test_verbose())
1393 g_print ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1394 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1395 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1396 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1398 ref_date.tv_usec = REF_USEC_UTC;
1399 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_UTC, &date) != FALSE);
1400 if (g_test_verbose())
1401 g_print ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1402 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1403 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1404 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1406 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_CEST, &date) != FALSE);
1407 if (g_test_verbose())
1408 g_print ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1409 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1410 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1411 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1413 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_EST, &date) != FALSE);
1414 if (g_test_verbose())
1415 g_print ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1416 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1417 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1418 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1420 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_NST, &date) != FALSE);
1421 if (g_test_verbose())
1422 g_print ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1423 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1424 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1425 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1427 if (g_test_verbose())
1428 g_print ("checking g_time_val_to_iso8601...\n");
1429 ref_date.tv_sec = REF_SEC_UTC;
1430 ref_date.tv_usec = 0;
1431 date_str = g_time_val_to_iso8601 (&ref_date);
1432 g_assert (date_str != NULL);
1433 if (g_test_verbose())
1434 g_print ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_UTC);
1435 g_assert (strcmp (date_str, REF_STR_UTC) == 0);
1438 ref_date.tv_usec = REF_USEC_UTC;
1439 date_str = g_time_val_to_iso8601 (&ref_date);
1440 g_assert (date_str != NULL);
1441 if (g_test_verbose())
1442 g_print ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_USEC_UTC);
1443 g_assert (strcmp (date_str, REF_STR_USEC_UTC) == 0);
1446 if (g_test_verbose())
1447 g_print ("checking g_ascii_strcasecmp...");
1448 g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
1449 g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
1450 g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
1451 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1452 g_assert (g_ascii_strcasecmp ("", "") == 0);
1453 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
1454 g_assert (g_ascii_strcasecmp ("a", "b") < 0);
1455 g_assert (g_ascii_strcasecmp ("a", "B") < 0);
1456 g_assert (g_ascii_strcasecmp ("A", "b") < 0);
1457 g_assert (g_ascii_strcasecmp ("A", "B") < 0);
1458 g_assert (g_ascii_strcasecmp ("b", "a") > 0);
1459 g_assert (g_ascii_strcasecmp ("b", "A") > 0);
1460 g_assert (g_ascii_strcasecmp ("B", "a") > 0);
1461 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1463 if (g_test_verbose())
1464 g_print ("checking g_strdup...\n");
1465 g_assert (g_strdup (NULL) == NULL);
1466 string = g_strdup (GLIB_TEST_STRING);
1467 g_assert (string != NULL);
1468 g_assert (strcmp(string, GLIB_TEST_STRING) == 0);
1471 if (g_test_verbose())
1472 g_print ("checking g_strconcat...\n");
1473 string = g_strconcat (GLIB_TEST_STRING, NULL);
1474 g_assert (string != NULL);
1475 g_assert (strcmp (string, GLIB_TEST_STRING) == 0);
1477 string = g_strconcat (GLIB_TEST_STRING, GLIB_TEST_STRING,
1478 GLIB_TEST_STRING, NULL);
1479 g_assert (string != NULL);
1480 g_assert (strcmp (string, GLIB_TEST_STRING GLIB_TEST_STRING
1481 GLIB_TEST_STRING) == 0);
1484 if (g_test_verbose())
1485 g_print ("checking g_strlcpy/g_strlcat...");
1486 /* The following is a torture test for strlcpy/strlcat, with lots of
1487 * checking; normal users wouldn't use them this way!
1489 string = g_malloc (6);
1490 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
1492 g_assert (g_strlcpy(string, "" , 5) == 0);
1493 g_assert ( *string == '\0' );
1495 g_assert (g_strlcpy(string, "abc" , 5) == 3);
1496 g_assert ( *(string + 3) == '\0' );
1497 g_assert (g_str_equal(string, "abc"));
1498 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1499 g_assert ( *(string + 4) == '\0' );
1500 g_assert ( *(string + 5) == 'Z' );
1501 g_assert (g_str_equal(string, "abcd"));
1502 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1503 g_assert ( *(string + 4) == '\0' );
1504 g_assert ( *(string + 5) == 'Z' );
1505 g_assert (g_str_equal(string, "abcd"));
1506 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1507 g_assert ( *(string + 4) == '\0' );
1508 g_assert ( *(string + 5) == 'Z' );
1509 g_assert (g_str_equal(string, "abcd"));
1511 *(string + 1)= '\0';
1512 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1513 g_assert (*string == 'Y');
1515 g_assert (g_strlcat(string, "123" , 5) == 3);
1516 g_assert ( *(string + 3) == '\0' );
1517 g_assert (g_str_equal(string, "123"));
1518 g_assert (g_strlcat(string, "" , 5) == 3);
1519 g_assert ( *(string + 3) == '\0' );
1520 g_assert (g_str_equal(string, "123"));
1521 g_assert (g_strlcat(string, "4", 5) == 4);
1522 g_assert (g_str_equal(string, "1234"));
1523 g_assert (g_strlcat(string, "5", 5) == 5);
1524 g_assert ( *(string + 4) == '\0' );
1525 g_assert (g_str_equal(string, "1234"));
1526 g_assert ( *(string + 5) == 'Z' );
1528 *(string + 1)= '\0';
1529 g_assert (g_strlcat(string, "123" , 0) == 3);
1530 g_assert (*string == 'Y');
1532 /* A few more tests, demonstrating more "normal" use */
1533 g_assert (g_strlcpy(string, "hi", 5) == 2);
1534 g_assert (g_str_equal(string, "hi"));
1535 g_assert (g_strlcat(string, "t", 5) == 3);
1536 g_assert (g_str_equal(string, "hit"));
1539 if (g_test_verbose())
1540 g_print ("checking g_strdup_printf...\n");
1541 string = g_strdup_printf ("%05d %-5s", 21, "test");
1542 g_assert (string != NULL);
1543 g_assert (strcmp(string, "00021 test ") == 0);
1546 /* g_debug (argv[0]); */
1549 #ifndef G_DISABLE_DEPRECATED
1551 test_mem_chunks (void)
1553 GMemChunk *mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
1556 for (i = 0; i < 10000; i++)
1559 mem[i] = g_chunk_new (gchar, mem_chunk);
1560 for (j = 0; j < 50; j++)
1563 for (i = 0; i < 10000; i++)
1564 g_mem_chunk_free (mem_chunk, mem[i]);
1566 g_mem_chunk_destroy (mem_chunk);
1574 g_test_init (&argc, &argv, NULL);
1576 g_test_add_func ("/testglib/Infos", test_info);
1577 g_test_add_func ("/testglib/Types Sizes", type_sizes);
1578 g_test_add_func ("/testglib/GStrings", gstring_tests);
1579 g_test_add_func ("/testglib/Various Strings", various_string_tests);
1580 g_test_add_func ("/testglib/GList", glist_test);
1581 g_test_add_func ("/testglib/GSList", gslist_test);
1582 g_test_add_func ("/testglib/GNode", gnode_test);
1583 g_test_add_func ("/testglib/GTree", binary_tree_test);
1584 g_test_add_func ("/testglib/Arrays", test_arrays);
1585 g_test_add_func ("/testglib/GHashTable", hash_table_tests);
1586 #ifndef G_DISABLE_DEPRECATED
1587 g_test_add_func ("/testglib/Relation (deprecated)", relation_test);
1589 g_test_add_func ("/testglib/File Paths", test_paths);
1590 g_test_add_func ("/testglib/File Functions", test_file_functions);
1591 g_test_add_func ("/testglib/Parse Debug Strings", test_g_parse_debug_string);
1592 #ifndef G_DISABLE_DEPRECATED
1593 g_test_add_func ("/testglib/GMemChunk (deprecated)", test_mem_chunks);
1595 g_test_add_func ("/testglib/Warnings & Errors", log_warning_error_tests);
1596 g_test_add_func ("/testglib/Timers (slow)", timer_tests);
1598 return g_test_run();