1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
29 #undef GLIB_COMPILATION
36 #include <glib/gstdio.h>
45 #include <io.h> /* For read(), write() etc */
49 #define GLIB_TEST_STRING "el dorado "
50 #define GLIB_TEST_STRING_5 "el do"
53 /* --- variables --- */
54 static gint test_nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
55 static gint more_nums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
57 /* --- functions --- */
59 my_list_compare_one (gconstpointer a, gconstpointer b)
61 gint one = *((const gint*)a);
62 gint two = *((const gint*)b);
67 my_list_compare_two (gconstpointer a, gconstpointer b)
69 gint one = *((const gint*)a);
70 gint two = *((const gint*)b);
75 my_list_print (gpointer a, gpointer b)
77 gint three = *((gint*)a);
87 for (i = 0; i < 10; i++)
88 list = g_list_append (list, &test_nums[i]);
89 list = g_list_reverse (list);
91 for (i = 0; i < 10; i++)
93 GList *t = g_list_nth (list, i);
94 if (*((gint*) t->data) != (9 - i))
95 g_error ("Regular insert failed");
98 for (i = 0; i < 10; i++)
99 if (g_list_position (list, g_list_nth (list, i)) != i)
100 g_error ("g_list_position does not seem to be the inverse of g_list_nth\n");
105 for (i = 0; i < 10; i++)
106 list = g_list_insert_sorted (list, &more_nums[i], my_list_compare_one);
110 g_list_foreach (list, my_list_print, NULL);
113 for (i = 0; i < 10; i++)
115 GList *t = g_list_nth (list, i);
116 if (*((gint*) t->data) != i)
117 g_error ("Sorted insert failed");
123 for (i = 0; i < 10; i++)
124 list = g_list_insert_sorted (list, &more_nums[i], my_list_compare_two);
128 g_list_foreach (list, my_list_print, NULL);
131 for (i = 0; i < 10; i++)
133 GList *t = g_list_nth (list, i);
134 if (*((gint*) t->data) != (9 - i))
135 g_error ("Sorted insert failed");
141 for (i = 0; i < 10; i++)
142 list = g_list_prepend (list, &more_nums[i]);
144 list = g_list_sort (list, my_list_compare_two);
148 g_list_foreach (list, my_list_print, NULL);
151 for (i = 0; i < 10; i++)
153 GList *t = g_list_nth (list, i);
154 if (*((gint*) t->data) != (9 - i))
155 g_error ("Merge sort failed");
164 GSList *slist = NULL;
167 for (i = 0; i < 10; i++)
168 slist = g_slist_append (slist, &test_nums[i]);
169 slist = g_slist_reverse (slist);
171 for (i = 0; i < 10; i++)
173 GSList *st = g_slist_nth (slist, i);
174 if (*((gint*) st->data) != (9 - i))
178 g_slist_free (slist);
181 for (i = 0; i < 10; i++)
182 slist = g_slist_insert_sorted (slist, &more_nums[i], my_list_compare_one);
186 g_slist_foreach (slist, my_list_print, NULL);
189 for (i = 0; i < 10; i++)
191 GSList *st = g_slist_nth (slist, i);
192 if (*((gint*) st->data) != i)
193 g_error ("Sorted insert failed");
196 g_slist_free (slist);
199 for (i = 0; i < 10; i++)
200 slist = g_slist_insert_sorted (slist, &more_nums[i], my_list_compare_two);
204 g_slist_foreach (slist, my_list_print, NULL);
207 for (i = 0; i < 10; i++)
209 GSList *st = g_slist_nth (slist, i);
210 if (*((gint*) st->data) != (9 - i))
211 g_error("Sorted insert failed");
217 for (i = 0; i < 10; i++)
218 slist = g_slist_prepend (slist, &more_nums[i]);
220 slist = g_slist_sort (slist, my_list_compare_two);
224 g_slist_foreach (slist, my_list_print, NULL);
227 for (i = 0; i < 10; i++)
229 GSList *st = g_slist_nth (slist, i);
230 if (*((gint*) st->data) != (9 - i))
231 g_error("Sorted insert failed");
238 node_build_string (GNode *node,
245 c[0] = ((gchar) ((gintptr) (node->data)));
247 string = g_strconcat (*p ? *p : "", c, NULL);
257 #define C2P(c) ((gpointer) ((long) (c)))
258 #define P2C(p) ((gchar) ((gintptr) (p)))
266 gchar *tstring, *cstring;
268 root = g_node_new (C2P ('A'));
269 g_assert (g_node_depth (root) == 1 && g_node_max_height (root) == 1);
271 node_B = g_node_new (C2P ('B'));
272 g_node_append (root, node_B);
273 g_assert (root->children == node_B);
275 g_node_append_data (node_B, C2P ('E'));
276 g_node_prepend_data (node_B, C2P ('C'));
277 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
279 node_F = g_node_new (C2P ('F'));
280 g_node_append (root, node_F);
281 g_assert (root->children->next == node_F);
283 node_G = g_node_new (C2P ('G'));
284 g_node_append (node_F, node_G);
285 node_J = g_node_new (C2P ('J'));
286 g_node_prepend (node_G, node_J);
287 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
288 g_node_insert_data (node_G, 0, C2P ('H'));
289 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
291 g_assert (g_node_depth (root) == 1);
292 g_assert (g_node_max_height (root) == 4);
293 g_assert (g_node_depth (node_G->children->next) == 4);
294 g_assert (g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
295 g_assert (g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
296 g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
297 g_assert (g_node_max_height (node_F) == 3);
298 g_assert (g_node_n_children (node_G) == 4);
299 g_assert (g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
300 g_assert (g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
301 g_assert (g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
303 for (i = 0; i < g_node_n_children (node_B); i++)
305 node = g_node_nth_child (node_B, i);
306 g_assert (P2C (node->data) == ('C' + i));
309 for (i = 0; i < g_node_n_children (node_G); i++)
310 g_assert (g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
320 * for in-order traversal, 'G' is considered to be the "left"
321 * child of 'F', which will cause 'F' to be the last node visited.
325 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
326 g_assert_cmpstr (tstring, ==, "ABCDEFGHIJK");
327 g_free (tstring); tstring = NULL;
328 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
329 g_assert_cmpstr (tstring, ==, "CDEBHIJKGFA");
330 g_free (tstring); tstring = NULL;
331 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
332 g_assert_cmpstr (tstring, ==, "CBDEAHGIJKF");
333 g_free (tstring); tstring = NULL;
334 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
335 g_assert_cmpstr (tstring, ==, "ABFCDEGHIJK");
336 g_free (tstring); tstring = NULL;
338 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
339 g_assert_cmpstr (tstring, ==, "CDEHIJK");
340 g_free (tstring); tstring = NULL;
341 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
342 g_assert_cmpstr (tstring, ==, "ABFG");
343 g_free (tstring); tstring = NULL;
345 g_node_reverse_children (node_B);
346 g_node_reverse_children (node_G);
348 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
349 g_assert_cmpstr (tstring, ==, "ABFEDCGKJIH");
350 g_free (tstring); tstring = NULL;
353 node = g_node_copy (root);
354 g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
355 g_assert (g_node_max_height (root) == g_node_max_height (node));
356 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
357 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
358 g_assert_cmpstr (tstring, ==, cstring);
359 g_free (tstring); tstring = NULL;
360 g_free (cstring); cstring = NULL;
361 g_node_destroy (node);
363 g_node_destroy (root);
365 /* allocation tests */
367 root = g_node_new (NULL);
370 for (i = 0; i < 2048; i++)
372 g_node_append (node, g_node_new (NULL));
374 node = node->children->next;
376 g_assert (g_node_max_height (root) > 100);
377 g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
379 g_node_destroy (root);
385 my_compare (gconstpointer a,
395 my_traverse (gpointer key,
400 g_print ("%c ", *ch);
405 binary_tree_test (void)
411 tree = g_tree_new (my_compare);
413 for (j = 0; j < 10; j++, i++)
416 g_tree_insert (tree, &chars[i], &chars[i]);
418 for (j = 0; j < 26; j++, i++)
421 g_tree_insert (tree, &chars[i], &chars[i]);
423 for (j = 0; j < 26; j++, i++)
426 g_tree_insert (tree, &chars[i], &chars[i]);
429 g_assert_cmpint (g_tree_nnodes (tree), ==, 10 + 26 + 26);
430 g_assert_cmpint (g_tree_height (tree), ==, 6);
432 if (g_test_verbose())
435 g_tree_foreach (tree, my_traverse, NULL);
439 for (i = 0; i < 10; i++)
440 g_tree_remove (tree, &chars[i]);
442 g_assert_cmpint (g_tree_nnodes (tree), ==, 26 + 26);
443 g_assert_cmpint (g_tree_height (tree), ==, 6);
445 if (g_test_verbose())
448 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)
512 GDebugKey keys[3] = {
520 result = g_parse_debug_string ("bar:foo:blubb", keys, n_keys);
521 g_assert (result == 3);
523 result = g_parse_debug_string (":baz::_E@~!_::", keys, n_keys);
524 g_assert (result == 4);
526 result = g_parse_debug_string ("", keys, n_keys);
527 g_assert (result == 0);
529 result = g_parse_debug_string (" : ", keys, n_keys);
530 g_assert (result == 0);
534 log_warning_error_tests (void)
536 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
538 g_message ("this is a g_message test.");
539 g_message ("non-printable UTF-8: \"\xc3\xa4\xda\x85\"");
540 g_message ("unsafe chars: \"\x10\x11\x12\n\t\x7f\x81\x82\x83\"");
543 g_test_trap_assert_passed();
544 g_test_trap_assert_stderr ("*is a g_message test*");
545 g_test_trap_assert_stderr ("*non-printable UTF-8*");
546 g_test_trap_assert_stderr ("*unsafe chars*");
547 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
549 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
552 g_test_trap_assert_failed(); /* we have fatal-warnings enabled */
553 g_test_trap_assert_stderr ("*harmless warning*");
554 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
559 g_test_trap_assert_failed(); /* we have fatal-warnings enabled */
560 g_test_trap_assert_stderr ("*g_print*assertion*failed*");
561 g_test_trap_assert_stderr ("*NULL*");
567 GTimer *timer, *timer2;
571 timer = g_timer_new ();
572 g_timer_start (timer);
573 elapsed = g_timer_elapsed (timer, NULL);
574 g_timer_stop (timer);
575 g_assert_cmpfloat (elapsed, <=, g_timer_elapsed (timer, NULL));
576 g_timer_destroy (timer);
580 if (g_test_verbose())
581 g_print ("checking timers...\n");
582 timer = g_timer_new ();
583 if (g_test_verbose())
584 g_print (" spinning for 3 seconds...\n");
585 g_timer_start (timer);
586 while (g_timer_elapsed (timer, NULL) < 3)
588 g_timer_stop (timer);
589 g_timer_destroy (timer);
590 if (g_test_verbose())
596 gulong elapsed_usecs;
597 if (g_test_verbose())
598 g_print ("checking g_timer_continue...\n");
599 timer2 = g_timer_new ();
600 if (g_test_verbose())
601 g_print ("\trun for 1 second...\n");
602 timer = g_timer_new();
603 g_usleep (G_USEC_PER_SEC); /* run timer for 1 second */
604 g_timer_stop (timer);
605 if (g_test_verbose())
606 g_print ("\tstop for 1 second...\n");
607 g_usleep (G_USEC_PER_SEC); /* wait for 1 second */
608 if (g_test_verbose())
609 g_print ("\trun for 2 seconds...\n");
610 g_timer_continue (timer);
611 g_usleep (2 * G_USEC_PER_SEC); /* run timer for 2 seconds */
613 if (g_test_verbose())
614 g_print ("\tstop for 1.5 seconds...\n");
615 g_usleep ((3 * G_USEC_PER_SEC) / 2); /* wait for 1.5 seconds */
616 if (g_test_verbose())
617 g_print ("\trun for 0.2 seconds...\n");
618 g_timer_continue (timer);
619 g_usleep (G_USEC_PER_SEC / 5); /* run timer for 0.2 seconds */
620 g_timer_stop (timer);
621 if (g_test_verbose())
622 g_print ("\tstop for 4 seconds...\n");
623 g_usleep (4 * G_USEC_PER_SEC); /* wait for 4 seconds */
624 if (g_test_verbose())
625 g_print ("\trun for 5.8 seconds...\n");
626 g_timer_continue (timer);
627 g_usleep ((29 * G_USEC_PER_SEC) / 5); /* run timer for 5.8 seconds */
629 elapsed = g_timer_elapsed (timer, &elapsed_usecs);
630 if (g_test_verbose())
631 g_print ("\t=> timer = %.6f = %d.%06ld (should be: 9.000000) (%.6f off)\n", elapsed, (int) elapsed, elapsed_usecs, ABS (elapsed - 9.));
632 g_assert_cmpfloat (elapsed, >, 8.8);
633 g_assert_cmpfloat (elapsed, <, 9.2);
634 if (g_test_verbose())
635 g_print ("g_timer_continue ... ok\n\n");
636 g_timer_stop (timer2);
637 elapsed = g_timer_elapsed (timer2, &elapsed_usecs);
638 if (g_test_verbose())
639 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)));
640 g_assert_cmpfloat (elapsed, >, 8.8 + 6.5);
641 g_assert_cmpfloat (elapsed, <, 9.2 + 6.5);
642 if (g_test_verbose())
643 g_print ("timer2 ... ok\n\n");
644 g_timer_destroy (timer);
645 g_timer_destroy (timer2);
652 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
653 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
654 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
655 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
657 g_assert_cmpint (sizeof (gint8), ==, 1);
658 g_assert_cmpint (sizeof (gint16), ==, 2);
659 g_assert_cmpint (sizeof (gint32), ==, 4);
660 g_assert_cmpint (sizeof (gint64), ==, 8);
662 if (g_test_verbose())
663 g_print ("checking endian macros (host is %s)...\n",
664 G_BYTE_ORDER == G_BIG_ENDIAN ? "big endian" : "little endian");
665 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
666 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
667 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
673 const gchar *un, *rn, *hn;
674 const gchar *tmpdir, *homedir, *userdatadir, *uconfdir, *ucachedir;
675 const gchar *uddesktop, *udddocs, *uddpubshare, *uruntimedir;
676 gchar **sv, *cwd, *sdatadirs, *sconfdirs, *langnames;
677 const gchar *charset;
678 gboolean charset_is_utf8;
679 if (g_test_verbose())
680 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
687 cwd = g_get_current_dir ();
688 un = g_get_user_name();
689 rn = g_get_real_name();
690 hn = g_get_host_name();
691 if (g_test_verbose())
693 g_print ("cwd: %s\n", cwd);
694 g_print ("user: %s\n", un);
695 g_print ("real: %s\n", rn);
696 g_print ("host: %s\n", hn);
700 /* reload, just for fun */
701 g_reload_user_special_dirs_cache ();
702 g_reload_user_special_dirs_cache ();
704 tmpdir = g_get_tmp_dir();
705 g_assert (tmpdir != NULL);
706 homedir = g_get_home_dir ();
707 g_assert (homedir != NULL);
708 userdatadir = g_get_user_data_dir ();
709 g_assert (userdatadir != NULL);
710 uconfdir = g_get_user_config_dir ();
711 g_assert (uconfdir != NULL);
712 ucachedir = g_get_user_cache_dir ();
713 g_assert (ucachedir != NULL);
715 uddesktop = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
716 g_assert (uddesktop != NULL);
717 udddocs = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
718 uddpubshare = g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE);
719 uruntimedir = g_get_user_runtime_dir ();
720 g_assert (uruntimedir != NULL);
722 sv = (gchar **) g_get_system_data_dirs ();
723 sdatadirs = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv);
724 sv = (gchar **) g_get_system_config_dirs ();
725 sconfdirs = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv);
726 sv = (gchar **) g_get_language_names ();
727 langnames = g_strjoinv (":", sv);
729 if (g_test_verbose())
731 g_print ("tmp-dir: %s\n", tmpdir);
732 g_print ("home: %s\n", homedir);
733 g_print ("user_data: %s\n", userdatadir);
734 g_print ("user_config: %s\n", uconfdir);
735 g_print ("user_cache: %s\n", ucachedir);
736 g_print ("user_runtime: %s\n", uruntimedir);
737 g_print ("system_data: %s\n", sdatadirs);
738 g_print ("system_config: %s\n", sconfdirs);
739 g_print ("languages: %s\n", langnames);
740 g_print ("user_special[DESKTOP]: %s\n", uddesktop);
741 g_print ("user_special[DOCUMENTS]: %s\n", udddocs);
742 g_print ("user_special[PUBLIC_SHARE]: %s\n", uddpubshare);
748 charset_is_utf8 = g_get_charset ((const char**)&charset);
750 if (g_test_verbose())
753 g_print ("current charset is UTF-8: %s\n", charset);
755 g_print ("current charset is not UTF-8: %s\n", charset);
758 if (g_test_verbose())
760 #ifdef G_PLATFORM_WIN32
761 g_print ("current locale: %s\n", g_win32_getlocale ());
763 g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
764 g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
766 g_print ("a Win32 error message: %s\n", g_win32_error_message (2));
777 } dirname_checks[] = {
788 { ".\\\\\\\\", "." },
790 { "..\\\\\\\\", ".." },
798 { "//server/share///x", "//server/share" },
804 const guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
808 } skip_root_checks[] = {
817 { "\\\\server\\foo", "" },
818 { "\\\\server\\foo\\bar", "bar" },
822 { "//server/share///x", "//x" },
827 const guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
830 if (g_test_verbose())
831 g_print ("checking g_path_get_basename()...");
832 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
833 g_assert (strcmp (string, "dir") == 0);
835 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
836 g_assert (strcmp (string, "file") == 0);
838 if (g_test_verbose())
842 string = g_path_get_basename ("/foo/dir/");
843 g_assert (strcmp (string, "dir") == 0);
845 string = g_path_get_basename ("/foo/file");
846 g_assert (strcmp (string, "file") == 0);
850 if (g_test_verbose())
851 g_print ("checking g_path_get_dirname()...");
852 for (i = 0; i < n_dirname_checks; i++)
854 gchar *dirname = g_path_get_dirname (dirname_checks[i].filename);
855 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
857 g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
858 dirname_checks[i].filename,
859 dirname_checks[i].dirname,
864 if (g_test_verbose())
867 if (g_test_verbose())
868 g_print ("checking g_path_skip_root()...");
869 for (i = 0; i < n_skip_root_checks; i++)
871 const gchar *skipped = g_path_skip_root (skip_root_checks[i].filename);
872 if ((skipped && !skip_root_checks[i].without_root) ||
873 (!skipped && skip_root_checks[i].without_root) ||
874 ((skipped && skip_root_checks[i].without_root) &&
875 strcmp (skipped, skip_root_checks[i].without_root)))
877 g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
878 skip_root_checks[i].filename,
879 (skip_root_checks[i].without_root ?
880 skip_root_checks[i].without_root : "<NULL>"),
881 (skipped ? skipped : "<NULL>"));
884 if (g_test_verbose())
889 test_file_functions (void)
891 const char hello[] = "Hello, World";
892 const int hellolen = sizeof (hello) - 1;
895 char *name_used, chars[62];
898 strcpy (template, "foobar");
899 fd = g_mkstemp (template);
900 if (g_test_verbose() && fd != -1)
901 g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
904 strcpy (template, "fooXXXXXX");
905 fd = g_mkstemp (template);
907 g_error ("g_mkstemp didn't work for template %s\n", template);
908 n = write (fd, hello, hellolen);
910 g_error ("write() failed: %s\n", g_strerror (errno));
911 else if (n != hellolen)
912 g_error ("write() should have written %d bytes, wrote %d\n", hellolen, n);
915 n = read (fd, chars, sizeof (chars));
917 g_error ("read() failed: %s\n", g_strerror (errno));
918 else if (n != hellolen)
919 g_error ("read() should have read %d bytes, got %d\n", hellolen, n);
922 if (strcmp (chars, hello) != 0)
923 g_error ("wrote '%s', but got '%s'\n", hello, chars);
929 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
930 fd = g_file_open_tmp (template, &name_used, &error);
931 if (g_test_verbose())
934 g_print ("g_file_open_tmp works even if template contains '%s'\n", G_DIR_SEPARATOR_S);
936 g_print ("g_file_open_tmp correctly returns error: %s\n", error->message);
939 g_clear_error (&error);
942 strcpy (template, "zap/barXXXXXX");
943 fd = g_file_open_tmp (template, &name_used, &error);
944 if (g_test_verbose())
947 g_print ("g_file_open_tmp works even if template contains '/'\n");
949 g_print ("g_file_open_tmp correctly returns error: %s\n", error->message);
952 g_clear_error (&error);
955 strcpy (template, "zapXXXXXX");
956 fd = g_file_open_tmp (template, &name_used, &error);
958 g_error ("g_file_open_tmp didn't work for template '%s': %s\n", template, error->message);
959 else if (g_test_verbose())
960 g_print ("g_file_open_tmp for template '%s' used name '%s'\n", template, name_used);
962 g_clear_error (&error);
965 fd = g_file_open_tmp (NULL, &name_used, &error);
967 g_error ("g_file_open_tmp didn't work for a NULL template: %s\n", error->message);
969 g_clear_error (&error);
981 gparray = g_ptr_array_new ();
982 for (i = 0; i < 10000; i++)
983 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
984 for (i = 0; i < 10000; i++)
985 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
986 g_error ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
987 g_ptr_array_free (gparray, TRUE);
989 gbarray = g_byte_array_new ();
990 for (i = 0; i < 10000; i++)
991 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
992 for (i = 0; i < 10000; i++)
994 g_assert (gbarray->data[4*i] == 'a');
995 g_assert (gbarray->data[4*i+1] == 'b');
996 g_assert (gbarray->data[4*i+2] == 'c');
997 g_assert (gbarray->data[4*i+3] == 'd');
999 g_byte_array_free (gbarray, TRUE);
1001 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1002 for (i = 0; i < 10000; i++)
1003 g_array_append_val (garray, i);
1004 for (i = 0; i < 10000; i++)
1005 if (g_array_index (garray, gint, i) != i)
1006 g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), i);
1007 g_array_free (garray, TRUE);
1009 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1010 for (i = 0; i < 100; i++)
1011 g_array_prepend_val (garray, i);
1012 for (i = 0; i < 100; i++)
1013 if (g_array_index (garray, gint, i) != (100 - i - 1))
1014 g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
1015 g_array_free (garray, TRUE);
1019 hash_table_tests (void)
1021 GHashTable *hash_table;
1023 gint *pvalue = NULL;
1027 hash_table = g_hash_table_new (my_hash, my_hash_equal);
1028 for (i = 0; i < 10000; i++)
1031 g_hash_table_insert (hash_table, &array[i], &array[i]);
1033 pvalue = g_hash_table_find (hash_table, find_first_that, &value);
1034 if (*pvalue != value)
1035 g_error ("g_hash_table_find failed");
1036 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
1037 for (i = 0; i < 10000; i++)
1039 g_error ("hashtable-test: wrong value: %d\n", i);
1040 for (i = 0; i < 10000; i++)
1041 g_hash_table_remove (hash_table, &array[i]);
1042 for (i = 0; i < 10000; i++)
1045 g_hash_table_insert (hash_table, &array[i], &array[i]);
1047 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
1048 g_hash_table_size (hash_table) != 5000)
1049 g_error ("hashtable removal failed\n");
1050 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
1051 g_hash_table_destroy (hash_table);
1054 #ifndef G_DISABLE_DEPRECATED
1056 relation_test (void)
1058 GRelation *relation = g_relation_new (2);
1063 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1064 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1066 for (i = 0; i < 1024; i += 1)
1069 for (i = 1; i < 1023; i += 1)
1071 g_relation_insert (relation, data + i, data + i + 1);
1072 g_relation_insert (relation, data + i, data + i - 1);
1075 for (i = 2; i < 1022; i += 1)
1077 g_assert (! g_relation_exists (relation, data + i, data + i));
1078 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1079 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1082 for (i = 1; i < 1023; i += 1)
1084 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1085 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1088 for (i = 2; i < 1022; i += 1)
1090 g_assert (g_relation_count (relation, data + i, 0) == 2);
1091 g_assert (g_relation_count (relation, data + i, 1) == 2);
1094 g_assert (g_relation_count (relation, data, 0) == 0);
1096 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1097 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1098 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1099 g_relation_delete (relation, data + 42, 0);
1100 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1101 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1102 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1104 tuples = g_relation_select (relation, data + 200, 0);
1106 g_assert (tuples->len == 2);
1109 for (i = 0; i < tuples->len; i += 1)
1112 *(gint*) g_tuples_index (tuples, i, 0),
1113 *(gint*) g_tuples_index (tuples, i, 1));
1117 g_assert (g_relation_exists (relation, data + 300, data + 301));
1118 g_relation_delete (relation, data + 300, 0);
1119 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1121 g_tuples_destroy (tuples);
1123 g_relation_destroy (relation);
1130 gstring_tests (void)
1132 GString *string1, *string2;
1135 if (g_test_verbose())
1136 g_print ("test GString basics\n");
1138 string1 = g_string_new ("hi pete!");
1139 string2 = g_string_new ("");
1141 g_assert (strcmp ("hi pete!", string1->str) == 0);
1143 for (i = 0; i < 10000; i++)
1144 g_string_append_c (string1, 'a'+(i%26));
1147 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
1148 the %10000.10000f format... */
1149 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
1150 "this pete guy sure is a wuss, like he's the number ",
1152 " wuss. everyone agrees.\n",
1154 10, 666, 15, 15, 666.666666666, 666.666666666);
1156 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
1157 "this pete guy sure is a wuss, like he's the number ",
1159 " wuss. everyone agrees.\n",
1161 10, 666, 15, 15, 666.666666666, 666.666666666);
1164 if (g_test_verbose())
1165 g_print ("string2 length = %lu...\n", (gulong)string2->len);
1166 string2->str[70] = '\0';
1167 if (g_test_verbose())
1168 g_print ("first 70 chars:\n%s\n", string2->str);
1169 string2->str[141] = '\0';
1170 if (g_test_verbose())
1171 g_print ("next 70 chars:\n%s\n", string2->str+71);
1172 string2->str[212] = '\0';
1173 if (g_test_verbose())
1174 g_print ("and next 70:\n%s\n", string2->str+142);
1175 if (g_test_verbose())
1176 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
1178 g_string_free (string1, TRUE);
1179 g_string_free (string2, TRUE);
1182 string1 = g_string_new ("firsthalf");
1183 g_string_append (string1, "lasthalf");
1184 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1185 g_string_free (string1, TRUE);
1188 string1 = g_string_new ("firsthalf");
1189 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
1190 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1191 g_string_free (string1, TRUE);
1194 string1 = g_string_new ("lasthalf");
1195 g_string_prepend (string1, "firsthalf");
1196 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1197 g_string_free (string1, TRUE);
1200 string1 = g_string_new ("lasthalf");
1201 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
1202 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1203 g_string_free (string1, TRUE);
1206 string1 = g_string_new ("firstlast");
1207 g_string_insert (string1, 5, "middle");
1208 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1209 g_string_free (string1, TRUE);
1211 /* insert with pos == end of the string */
1212 string1 = g_string_new ("firstmiddle");
1213 g_string_insert (string1, strlen ("firstmiddle"), "last");
1214 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1215 g_string_free (string1, TRUE);
1218 string1 = g_string_new ("firstlast");
1219 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
1220 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1221 g_string_free (string1, TRUE);
1223 /* insert_len with magic -1 pos for append */
1224 string1 = g_string_new ("first");
1225 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
1226 g_assert (strcmp (string1->str, "firstlast") == 0);
1227 g_string_free (string1, TRUE);
1229 /* insert_len with magic -1 len for strlen-the-string */
1230 string1 = g_string_new ("first");
1231 g_string_insert_len (string1, 5, "last", -1);
1232 g_assert (strcmp (string1->str, "firstlast") == 0);
1233 g_string_free (string1, TRUE);
1235 /* g_string_equal */
1236 string1 = g_string_new ("test");
1237 string2 = g_string_new ("te");
1238 g_assert (! g_string_equal(string1, string2));
1239 g_string_append (string2, "st");
1240 g_assert (g_string_equal(string1, string2));
1241 g_string_free (string1, TRUE);
1242 g_string_free (string2, TRUE);
1244 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
1245 if (g_test_verbose())
1246 g_print ("test embedded ASCII 0 (NUL) characters in GString\n");
1247 string1 = g_string_new ("fiddle");
1248 string2 = g_string_new ("fiddle");
1249 g_assert (g_string_equal(string1, string2));
1250 g_string_append_c(string1, '\0');
1251 g_assert (! g_string_equal(string1, string2));
1252 g_string_append_c(string2, '\0');
1253 g_assert (g_string_equal(string1, string2));
1254 g_string_append_c(string1, 'x');
1255 g_string_append_c(string2, 'y');
1256 g_assert (! g_string_equal(string1, string2));
1257 g_assert (string1->len == 8);
1258 g_string_append(string1, "yzzy");
1259 g_assert (string1->len == 12);
1260 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
1261 g_string_insert(string1, 1, "QED");
1262 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
1263 g_string_free (string1, TRUE);
1264 g_string_free (string2, TRUE);
1268 various_string_tests (void)
1270 GStringChunk *string_chunk;
1271 GTimeVal ref_date, date;
1272 gchar *tmp_string = NULL, *tmp_string_2, *string, *date_str;
1276 if (g_test_verbose())
1277 g_print ("checking string chunks...");
1278 string_chunk = g_string_chunk_new (1024);
1279 for (i = 0; i < 100000; i ++)
1281 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
1282 if (strcmp ("hi pete", tmp_string) != 0)
1283 g_error ("string chunks are broken.\n");
1285 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
1286 g_assert (tmp_string_2 != tmp_string && strcmp (tmp_string_2, tmp_string) == 0);
1287 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
1288 g_assert (tmp_string_2 == tmp_string);
1289 g_string_chunk_free (string_chunk);
1291 if (g_test_verbose())
1292 g_print ("test positional printf formats (not supported):");
1293 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
1294 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
1295 if (g_test_verbose())
1296 g_print ("%s%s\n", string, tmp_string);
1297 g_free (tmp_string);
1300 #define REF_INVALID1 "Wed Dec 19 17:20:20 GMT 2007"
1301 #define REF_INVALID2 "1980-02-22T10:36:00Zulu"
1302 #define REF_INVALID3 "1980-02-22T"
1303 #define REF_SEC_UTC 320063760
1304 #define REF_STR_UTC "1980-02-22T10:36:00Z"
1305 #define REF_STR_LOCAL "1980-02-22T13:36:00"
1306 #define REF_STR_CEST "1980-02-22T12:36:00+02:00"
1307 #define REF_STR_EST "19800222T053600-0500"
1308 #define REF_STR_NST "1980-02-22T07:06:00-03:30"
1309 #define REF_USEC_UTC 50000
1310 #define REF_STR_USEC_UTC "1980-02-22T10:36:00.050000Z"
1311 #define REF_STR_USEC_CEST "19800222T123600.050000000+0200"
1312 #define REF_STR_USEC_EST "1980-02-22T05:36:00,05-05:00"
1313 #define REF_STR_USEC_NST "19800222T070600,0500-0330"
1314 #define REF_STR_DATE_ONLY "1980-02-22"
1316 if (g_test_verbose())
1317 g_print ("checking g_time_val_from_iso8601...\n");
1318 ref_date.tv_sec = REF_SEC_UTC;
1319 ref_date.tv_usec = 0;
1320 g_assert (g_time_val_from_iso8601 (REF_INVALID1, &date) == FALSE);
1321 g_assert (g_time_val_from_iso8601 (REF_INVALID2, &date) == FALSE);
1322 g_assert (g_time_val_from_iso8601 (REF_INVALID3, &date) == FALSE);
1323 g_assert (g_time_val_from_iso8601 (REF_STR_DATE_ONLY, &date) != FALSE);
1324 g_assert (g_time_val_from_iso8601 (REF_STR_UTC, &date) != FALSE);
1325 if (g_test_verbose())
1326 g_print ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1327 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1328 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1329 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1331 /* predefine time zone */
1332 tz = g_getenv("TZ");
1333 g_setenv("TZ", "UTC-03:00", 1);
1336 g_assert (g_time_val_from_iso8601 (REF_STR_LOCAL, &date) != FALSE);
1337 if (g_test_verbose())
1338 g_print ("\t=> LOCAL stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1339 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1340 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1341 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1343 /* revert back user defined time zone */
1345 g_setenv("TZ", tz, TRUE);
1350 g_assert (g_time_val_from_iso8601 (REF_STR_CEST, &date) != FALSE);
1351 if (g_test_verbose())
1352 g_print ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1353 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1354 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1355 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1357 g_assert (g_time_val_from_iso8601 (REF_STR_EST, &date) != FALSE);
1358 if (g_test_verbose())
1359 g_print ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1360 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1361 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1362 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1364 g_assert (g_time_val_from_iso8601 (REF_STR_NST, &date) != FALSE);
1365 if (g_test_verbose())
1366 g_print ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1367 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1368 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1369 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1371 ref_date.tv_usec = REF_USEC_UTC;
1372 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_UTC, &date) != FALSE);
1373 if (g_test_verbose())
1374 g_print ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1375 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1376 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1377 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1379 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_CEST, &date) != FALSE);
1380 if (g_test_verbose())
1381 g_print ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1382 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1383 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1384 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1386 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_EST, &date) != FALSE);
1387 if (g_test_verbose())
1388 g_print ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1389 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1390 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1391 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1393 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_NST, &date) != FALSE);
1394 if (g_test_verbose())
1395 g_print ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1396 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1397 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1398 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1400 if (g_test_verbose())
1401 g_print ("checking g_time_val_to_iso8601...\n");
1402 ref_date.tv_sec = REF_SEC_UTC;
1403 ref_date.tv_usec = 0;
1404 date_str = g_time_val_to_iso8601 (&ref_date);
1405 g_assert (date_str != NULL);
1406 if (g_test_verbose())
1407 g_print ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_UTC);
1408 g_assert (strcmp (date_str, REF_STR_UTC) == 0);
1411 ref_date.tv_usec = REF_USEC_UTC;
1412 date_str = g_time_val_to_iso8601 (&ref_date);
1413 g_assert (date_str != NULL);
1414 if (g_test_verbose())
1415 g_print ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_USEC_UTC);
1416 g_assert (strcmp (date_str, REF_STR_USEC_UTC) == 0);
1419 if (g_test_verbose())
1420 g_print ("checking g_ascii_strcasecmp...");
1421 g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
1422 g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
1423 g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
1424 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1425 g_assert (g_ascii_strcasecmp ("", "") == 0);
1426 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
1427 g_assert (g_ascii_strcasecmp ("a", "b") < 0);
1428 g_assert (g_ascii_strcasecmp ("a", "B") < 0);
1429 g_assert (g_ascii_strcasecmp ("A", "b") < 0);
1430 g_assert (g_ascii_strcasecmp ("A", "B") < 0);
1431 g_assert (g_ascii_strcasecmp ("b", "a") > 0);
1432 g_assert (g_ascii_strcasecmp ("b", "A") > 0);
1433 g_assert (g_ascii_strcasecmp ("B", "a") > 0);
1434 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1436 if (g_test_verbose())
1437 g_print ("checking g_strdup...\n");
1438 g_assert (g_strdup (NULL) == NULL);
1439 string = g_strdup (GLIB_TEST_STRING);
1440 g_assert (string != NULL);
1441 g_assert (strcmp(string, GLIB_TEST_STRING) == 0);
1444 if (g_test_verbose())
1445 g_print ("checking g_strconcat...\n");
1446 string = g_strconcat (GLIB_TEST_STRING, NULL);
1447 g_assert (string != NULL);
1448 g_assert (strcmp (string, GLIB_TEST_STRING) == 0);
1450 string = g_strconcat (GLIB_TEST_STRING, GLIB_TEST_STRING,
1451 GLIB_TEST_STRING, NULL);
1452 g_assert (string != NULL);
1453 g_assert (strcmp (string, GLIB_TEST_STRING GLIB_TEST_STRING
1454 GLIB_TEST_STRING) == 0);
1457 if (g_test_verbose())
1458 g_print ("checking g_strlcpy/g_strlcat...");
1459 /* The following is a torture test for strlcpy/strlcat, with lots of
1460 * checking; normal users wouldn't use them this way!
1462 string = g_malloc (6);
1463 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
1465 g_assert (g_strlcpy(string, "" , 5) == 0);
1466 g_assert ( *string == '\0' );
1468 g_assert (g_strlcpy(string, "abc" , 5) == 3);
1469 g_assert ( *(string + 3) == '\0' );
1470 g_assert (g_str_equal(string, "abc"));
1471 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1472 g_assert ( *(string + 4) == '\0' );
1473 g_assert ( *(string + 5) == 'Z' );
1474 g_assert (g_str_equal(string, "abcd"));
1475 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1476 g_assert ( *(string + 4) == '\0' );
1477 g_assert ( *(string + 5) == 'Z' );
1478 g_assert (g_str_equal(string, "abcd"));
1479 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1480 g_assert ( *(string + 4) == '\0' );
1481 g_assert ( *(string + 5) == 'Z' );
1482 g_assert (g_str_equal(string, "abcd"));
1484 *(string + 1)= '\0';
1485 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1486 g_assert (*string == 'Y');
1488 g_assert (g_strlcat(string, "123" , 5) == 3);
1489 g_assert ( *(string + 3) == '\0' );
1490 g_assert (g_str_equal(string, "123"));
1491 g_assert (g_strlcat(string, "" , 5) == 3);
1492 g_assert ( *(string + 3) == '\0' );
1493 g_assert (g_str_equal(string, "123"));
1494 g_assert (g_strlcat(string, "4", 5) == 4);
1495 g_assert (g_str_equal(string, "1234"));
1496 g_assert (g_strlcat(string, "5", 5) == 5);
1497 g_assert ( *(string + 4) == '\0' );
1498 g_assert (g_str_equal(string, "1234"));
1499 g_assert ( *(string + 5) == 'Z' );
1501 *(string + 1)= '\0';
1502 g_assert (g_strlcat(string, "123" , 0) == 3);
1503 g_assert (*string == 'Y');
1505 /* A few more tests, demonstrating more "normal" use */
1506 g_assert (g_strlcpy(string, "hi", 5) == 2);
1507 g_assert (g_str_equal(string, "hi"));
1508 g_assert (g_strlcat(string, "t", 5) == 3);
1509 g_assert (g_str_equal(string, "hit"));
1512 if (g_test_verbose())
1513 g_print ("checking g_strdup_printf...\n");
1514 string = g_strdup_printf ("%05d %-5s", 21, "test");
1515 g_assert (string != NULL);
1516 g_assert (strcmp(string, "00021 test ") == 0);
1519 /* g_debug (argv[0]); */
1522 #ifndef G_DISABLE_DEPRECATED
1524 test_mem_chunks (void)
1526 GMemChunk *mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
1529 for (i = 0; i < 10000; i++)
1532 mem[i] = g_chunk_new (gchar, mem_chunk);
1533 for (j = 0; j < 50; j++)
1536 for (i = 0; i < 10000; i++)
1537 g_mem_chunk_free (mem_chunk, mem[i]);
1545 g_test_init (&argc, &argv, NULL);
1547 g_test_add_func ("/testglib/Infos", test_info);
1548 g_test_add_func ("/testglib/Types Sizes", type_sizes);
1549 g_test_add_func ("/testglib/GStrings", gstring_tests);
1550 g_test_add_func ("/testglib/Various Strings", various_string_tests);
1551 g_test_add_func ("/testglib/GList", glist_test);
1552 g_test_add_func ("/testglib/GSList", gslist_test);
1553 g_test_add_func ("/testglib/GNode", gnode_test);
1554 g_test_add_func ("/testglib/GTree", binary_tree_test);
1555 g_test_add_func ("/testglib/Arrays", test_arrays);
1556 g_test_add_func ("/testglib/GHashTable", hash_table_tests);
1557 #ifndef G_DISABLE_DEPRECATED
1558 g_test_add_func ("/testglib/Relation (deprecated)", relation_test);
1560 g_test_add_func ("/testglib/File Paths", test_paths);
1561 g_test_add_func ("/testglib/File Functions", test_file_functions);
1562 g_test_add_func ("/testglib/Parse Debug Strings", test_g_parse_debug_string);
1563 #ifndef G_DISABLE_DEPRECATED
1564 g_test_add_func ("/testglib/GMemChunk (deprecated)", test_mem_chunks);
1566 g_test_add_func ("/testglib/Warnings & Errors", log_warning_error_tests);
1567 g_test_add_func ("/testglib/Timers (slow)", timer_tests);
1569 return g_test_run();