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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 #undef GLIB_COMPILATION
32 #include <glib/gstdio.h>
41 #include <io.h> /* For read(), write() etc */
45 #define GLIB_TEST_STRING "el dorado "
46 #define GLIB_TEST_STRING_5 "el do"
49 /* --- variables --- */
50 static gint test_nums
[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
51 static gint more_nums
[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
53 /* --- functions --- */
55 my_list_compare_one (gconstpointer a
, gconstpointer b
)
57 gint one
= *((const gint
*)a
);
58 gint two
= *((const gint
*)b
);
63 my_list_compare_two (gconstpointer a
, gconstpointer b
)
65 gint one
= *((const gint
*)a
);
66 gint two
= *((const gint
*)b
);
71 my_list_print (gpointer a, gpointer b)
73 gint three = *((gint*)a);
74 g_printerr ("%d", three);
83 for (i
= 0; i
< 10; i
++)
84 list
= g_list_append (list
, &test_nums
[i
]);
85 list
= g_list_reverse (list
);
87 for (i
= 0; i
< 10; i
++)
89 GList
*t
= g_list_nth (list
, i
);
90 if (*((gint
*) t
->data
) != (9 - i
))
91 g_error ("Regular insert failed");
94 for (i
= 0; i
< 10; i
++)
95 if (g_list_position (list
, g_list_nth (list
, i
)) != i
)
96 g_error ("g_list_position does not seem to be the inverse of g_list_nth\n");
101 for (i
= 0; i
< 10; i
++)
102 list
= g_list_insert_sorted (list
, &more_nums
[i
], my_list_compare_one
);
106 g_list_foreach (list, my_list_print, NULL);
109 for (i
= 0; i
< 10; i
++)
111 GList
*t
= g_list_nth (list
, i
);
112 if (*((gint
*) t
->data
) != i
)
113 g_error ("Sorted insert failed");
119 for (i
= 0; i
< 10; i
++)
120 list
= g_list_insert_sorted (list
, &more_nums
[i
], my_list_compare_two
);
124 g_list_foreach (list, my_list_print, NULL);
127 for (i
= 0; i
< 10; i
++)
129 GList
*t
= g_list_nth (list
, i
);
130 if (*((gint
*) t
->data
) != (9 - i
))
131 g_error ("Sorted insert failed");
137 for (i
= 0; i
< 10; i
++)
138 list
= g_list_prepend (list
, &more_nums
[i
]);
140 list
= g_list_sort (list
, my_list_compare_two
);
144 g_list_foreach (list, my_list_print, NULL);
147 for (i
= 0; i
< 10; i
++)
149 GList
*t
= g_list_nth (list
, i
);
150 if (*((gint
*) t
->data
) != (9 - i
))
151 g_error ("Merge sort failed");
160 GSList
*slist
= NULL
;
163 for (i
= 0; i
< 10; i
++)
164 slist
= g_slist_append (slist
, &test_nums
[i
]);
165 slist
= g_slist_reverse (slist
);
167 for (i
= 0; i
< 10; i
++)
169 GSList
*st
= g_slist_nth (slist
, i
);
170 if (*((gint
*) st
->data
) != (9 - i
))
174 g_slist_free (slist
);
177 for (i
= 0; i
< 10; i
++)
178 slist
= g_slist_insert_sorted (slist
, &more_nums
[i
], my_list_compare_one
);
182 g_slist_foreach (slist, my_list_print, NULL);
185 for (i
= 0; i
< 10; i
++)
187 GSList
*st
= g_slist_nth (slist
, i
);
188 if (*((gint
*) st
->data
) != i
)
189 g_error ("Sorted insert failed");
192 g_slist_free (slist
);
195 for (i
= 0; i
< 10; i
++)
196 slist
= g_slist_insert_sorted (slist
, &more_nums
[i
], my_list_compare_two
);
200 g_slist_foreach (slist, my_list_print, NULL);
203 for (i
= 0; i
< 10; i
++)
205 GSList
*st
= g_slist_nth (slist
, i
);
206 if (*((gint
*) st
->data
) != (9 - i
))
207 g_error("Sorted insert failed");
213 for (i
= 0; i
< 10; i
++)
214 slist
= g_slist_prepend (slist
, &more_nums
[i
]);
216 slist
= g_slist_sort (slist
, my_list_compare_two
);
220 g_slist_foreach (slist, my_list_print, NULL);
223 for (i
= 0; i
< 10; i
++)
225 GSList
*st
= g_slist_nth (slist
, i
);
226 if (*((gint
*) st
->data
) != (9 - i
))
227 g_error("Sorted insert failed");
234 node_build_string (GNode
*node
,
241 c
[0] = ((gchar
) ((gintptr
) (node
->data
)));
243 string
= g_strconcat (*p
? *p
: "", c
, NULL
);
253 #define C2P(c) ((gpointer) ((long) (c)))
254 #define P2C(p) ((gchar) ((gintptr) (p)))
262 gchar
*tstring
, *cstring
;
264 root
= g_node_new (C2P ('A'));
265 g_assert (g_node_depth (root
) == 1 && g_node_max_height (root
) == 1);
267 node_B
= g_node_new (C2P ('B'));
268 g_node_append (root
, node_B
);
269 g_assert (root
->children
== node_B
);
271 g_node_append_data (node_B
, C2P ('E'));
272 g_node_prepend_data (node_B
, C2P ('C'));
273 g_node_insert (node_B
, 1, g_node_new (C2P ('D')));
275 node_F
= g_node_new (C2P ('F'));
276 g_node_append (root
, node_F
);
277 g_assert (root
->children
->next
== node_F
);
279 node_G
= g_node_new (C2P ('G'));
280 g_node_append (node_F
, node_G
);
281 node_J
= g_node_new (C2P ('J'));
282 g_node_prepend (node_G
, node_J
);
283 g_node_insert (node_G
, 42, g_node_new (C2P ('K')));
284 g_node_insert_data (node_G
, 0, C2P ('H'));
285 g_node_insert (node_G
, 1, g_node_new (C2P ('I')));
287 g_assert (g_node_depth (root
) == 1);
288 g_assert (g_node_max_height (root
) == 4);
289 g_assert (g_node_depth (node_G
->children
->next
) == 4);
290 g_assert (g_node_n_nodes (root
, G_TRAVERSE_LEAFS
) == 7);
291 g_assert (g_node_n_nodes (root
, G_TRAVERSE_NON_LEAFS
) == 4);
292 g_assert (g_node_n_nodes (root
, G_TRAVERSE_ALL
) == 11);
293 g_assert (g_node_max_height (node_F
) == 3);
294 g_assert (g_node_n_children (node_G
) == 4);
295 g_assert (g_node_find_child (root
, G_TRAVERSE_ALL
, C2P ('F')) == node_F
);
296 g_assert (g_node_find (root
, G_LEVEL_ORDER
, G_TRAVERSE_NON_LEAFS
, C2P ('I')) == NULL
);
297 g_assert (g_node_find (root
, G_IN_ORDER
, G_TRAVERSE_LEAFS
, C2P ('J')) == node_J
);
299 for (i
= 0; i
< g_node_n_children (node_B
); i
++)
301 node
= g_node_nth_child (node_B
, i
);
302 g_assert (P2C (node
->data
) == ('C' + i
));
305 for (i
= 0; i
< g_node_n_children (node_G
); i
++)
306 g_assert (g_node_child_position (node_G
, g_node_nth_child (node_G
, i
)) == i
);
316 * for in-order traversal, 'G' is considered to be the "left"
317 * child of 'F', which will cause 'F' to be the last node visited.
321 g_node_traverse (root
, G_PRE_ORDER
, G_TRAVERSE_ALL
, -1, node_build_string
, &tstring
);
322 g_assert_cmpstr (tstring
, ==, "ABCDEFGHIJK");
323 g_free (tstring
); tstring
= NULL
;
324 g_node_traverse (root
, G_POST_ORDER
, G_TRAVERSE_ALL
, -1, node_build_string
, &tstring
);
325 g_assert_cmpstr (tstring
, ==, "CDEBHIJKGFA");
326 g_free (tstring
); tstring
= NULL
;
327 g_node_traverse (root
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1, node_build_string
, &tstring
);
328 g_assert_cmpstr (tstring
, ==, "CBDEAHGIJKF");
329 g_free (tstring
); tstring
= NULL
;
330 g_node_traverse (root
, G_LEVEL_ORDER
, G_TRAVERSE_ALL
, -1, node_build_string
, &tstring
);
331 g_assert_cmpstr (tstring
, ==, "ABFCDEGHIJK");
332 g_free (tstring
); tstring
= NULL
;
334 g_node_traverse (root
, G_LEVEL_ORDER
, G_TRAVERSE_LEAFS
, -1, node_build_string
, &tstring
);
335 g_assert_cmpstr (tstring
, ==, "CDEHIJK");
336 g_free (tstring
); tstring
= NULL
;
337 g_node_traverse (root
, G_PRE_ORDER
, G_TRAVERSE_NON_LEAFS
, -1, node_build_string
, &tstring
);
338 g_assert_cmpstr (tstring
, ==, "ABFG");
339 g_free (tstring
); tstring
= NULL
;
341 g_node_reverse_children (node_B
);
342 g_node_reverse_children (node_G
);
344 g_node_traverse (root
, G_LEVEL_ORDER
, G_TRAVERSE_ALL
, -1, node_build_string
, &tstring
);
345 g_assert_cmpstr (tstring
, ==, "ABFEDCGKJIH");
346 g_free (tstring
); tstring
= NULL
;
349 node
= g_node_copy (root
);
350 g_assert (g_node_n_nodes (root
, G_TRAVERSE_ALL
) == g_node_n_nodes (node
, G_TRAVERSE_ALL
));
351 g_assert (g_node_max_height (root
) == g_node_max_height (node
));
352 g_node_traverse (root
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1, node_build_string
, &tstring
);
353 g_node_traverse (node
, G_IN_ORDER
, G_TRAVERSE_ALL
, -1, node_build_string
, &cstring
);
354 g_assert_cmpstr (tstring
, ==, cstring
);
355 g_free (tstring
); tstring
= NULL
;
356 g_free (cstring
); cstring
= NULL
;
357 g_node_destroy (node
);
359 g_node_destroy (root
);
361 /* allocation tests */
363 root
= g_node_new (NULL
);
366 for (i
= 0; i
< 2048; i
++)
368 g_node_append (node
, g_node_new (NULL
));
370 node
= node
->children
->next
;
372 g_assert (g_node_max_height (root
) > 100);
373 g_assert (g_node_n_nodes (root
, G_TRAVERSE_ALL
) == 1 + 2048);
375 g_node_destroy (root
);
381 my_compare (gconstpointer a
,
391 my_traverse (gpointer key
,
396 g_printerr ("%c ", *ch
);
401 binary_tree_test (void)
407 tree
= g_tree_new (my_compare
);
409 for (j
= 0; j
< 10; j
++, i
++)
412 g_tree_insert (tree
, &chars
[i
], &chars
[i
]);
414 for (j
= 0; j
< 26; j
++, i
++)
417 g_tree_insert (tree
, &chars
[i
], &chars
[i
]);
419 for (j
= 0; j
< 26; j
++, i
++)
422 g_tree_insert (tree
, &chars
[i
], &chars
[i
]);
425 g_assert_cmpint (g_tree_nnodes (tree
), ==, 10 + 26 + 26);
426 g_assert_cmpint (g_tree_height (tree
), ==, 6);
428 if (g_test_verbose())
430 g_printerr ("tree: ");
431 g_tree_foreach (tree
, my_traverse
, NULL
);
435 for (i
= 0; i
< 10; i
++)
436 g_tree_remove (tree
, &chars
[i
]);
438 g_assert_cmpint (g_tree_nnodes (tree
), ==, 26 + 26);
439 g_assert_cmpint (g_tree_height (tree
), ==, 6);
441 if (g_test_verbose())
443 g_printerr ("tree: ");
444 g_tree_foreach (tree
, my_traverse
, NULL
);
452 my_hash_callback_remove (gpointer key
,
465 my_hash_callback_remove_test (gpointer key
,
472 g_error ("hash table entry %d should have been removed already\n", *d
);
476 my_hash_callback (gpointer key
,
485 my_hash (gconstpointer key
)
487 return (guint
) *((const gint
*) key
);
491 my_hash_equal (gconstpointer a
,
494 return *((const gint
*) a
) == *((const gint
*) b
);
498 find_first_that(gpointer key
,
503 gint
*test
= user_data
;
504 return (*v
== *test
);
508 test_g_parse_debug_string (void)
516 guint n_keys
= G_N_ELEMENTS (keys
);
519 result
= g_parse_debug_string ("bar:foo:blubb", keys
, n_keys
);
520 g_assert (result
== 3);
522 result
= g_parse_debug_string (":baz::_E@~!_::", keys
, n_keys
);
523 g_assert (result
== 4);
525 result
= g_parse_debug_string ("", keys
, n_keys
);
526 g_assert (result
== 0);
528 result
= g_parse_debug_string (" : ", keys
, n_keys
);
529 g_assert (result
== 0);
531 result
= g_parse_debug_string ("all", keys
, n_keys
);
532 g_assert_cmpuint (result
, ==, (1 << n_keys
) - 1);
534 /* Test subtracting debug flags from "all" */
535 result
= g_parse_debug_string ("all:foo", keys
, n_keys
);
536 g_assert_cmpuint (result
, ==, 2 | 4 | 8);
538 result
= g_parse_debug_string ("foo baz,all", keys
, n_keys
);
539 g_assert_cmpuint (result
, ==, 2 | 8);
541 result
= g_parse_debug_string ("all,fooo,baz", keys
, n_keys
);
542 g_assert_cmpuint (result
, ==, 1 | 2 | 8);
544 result
= g_parse_debug_string ("all:weird", keys
, n_keys
);
545 g_assert_cmpuint (result
, ==, 1 | 2 | 4);
549 log_warning_error_tests (void)
551 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_MESSAGE
,
552 "*is a g_message test*");
553 g_message ("this is a g_message test.");
554 g_test_assert_expected_messages ();
556 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_MESSAGE
,
557 "*non-printable UTF-8*");
558 g_message ("non-printable UTF-8: \"\xc3\xa4\xda\x85\"");
559 g_test_assert_expected_messages ();
561 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_MESSAGE
,
563 g_message ("unsafe chars: \"\x10\x11\x12\n\t\x7f\x81\x82\x83\"");
564 g_test_assert_expected_messages ();
566 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_WARNING
,
567 "*harmless warning*");
568 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
569 g_test_assert_expected_messages ();
571 g_test_expect_message (G_LOG_DOMAIN
, G_LOG_LEVEL_CRITICAL
,
572 "*g_print*assertion*failed*");
574 g_test_assert_expected_messages ();
580 GTimer
*timer
, *timer2
;
584 timer
= g_timer_new ();
585 g_timer_start (timer
);
586 elapsed
= g_timer_elapsed (timer
, NULL
);
587 g_timer_stop (timer
);
588 g_assert_cmpfloat (elapsed
, <=, g_timer_elapsed (timer
, NULL
));
589 g_timer_destroy (timer
);
593 if (g_test_verbose())
594 g_printerr ("checking timers...\n");
595 timer
= g_timer_new ();
596 if (g_test_verbose())
597 g_printerr (" spinning for 3 seconds...\n");
598 g_timer_start (timer
);
599 while (g_timer_elapsed (timer
, NULL
) < 3)
601 g_timer_stop (timer
);
602 g_timer_destroy (timer
);
603 if (g_test_verbose())
609 gulong elapsed_usecs
;
610 if (g_test_verbose())
611 g_printerr ("checking g_timer_continue...\n");
612 timer2
= g_timer_new ();
613 if (g_test_verbose())
614 g_printerr ("\trun for 1 second...\n");
615 timer
= g_timer_new();
616 g_usleep (G_USEC_PER_SEC
); /* run timer for 1 second */
617 g_timer_stop (timer
);
618 if (g_test_verbose())
619 g_printerr ("\tstop for 1 second...\n");
620 g_usleep (G_USEC_PER_SEC
); /* wait for 1 second */
621 if (g_test_verbose())
622 g_printerr ("\trun for 2 seconds...\n");
623 g_timer_continue (timer
);
624 g_usleep (2 * G_USEC_PER_SEC
); /* run timer for 2 seconds */
626 if (g_test_verbose())
627 g_printerr ("\tstop for 1.5 seconds...\n");
628 g_usleep ((3 * G_USEC_PER_SEC
) / 2); /* wait for 1.5 seconds */
629 if (g_test_verbose())
630 g_printerr ("\trun for 0.2 seconds...\n");
631 g_timer_continue (timer
);
632 g_usleep (G_USEC_PER_SEC
/ 5); /* run timer for 0.2 seconds */
633 g_timer_stop (timer
);
634 if (g_test_verbose())
635 g_printerr ("\tstop for 4 seconds...\n");
636 g_usleep (4 * G_USEC_PER_SEC
); /* wait for 4 seconds */
637 if (g_test_verbose())
638 g_printerr ("\trun for 5.8 seconds...\n");
639 g_timer_continue (timer
);
640 g_usleep ((29 * G_USEC_PER_SEC
) / 5); /* run timer for 5.8 seconds */
642 elapsed
= g_timer_elapsed (timer
, &elapsed_usecs
);
643 if (g_test_verbose())
644 g_printerr ("\t=> timer = %.6f = %d.%06ld (should be: 9.000000) (%.6f off)\n", elapsed
, (int) elapsed
, elapsed_usecs
, ABS (elapsed
- 9.));
645 g_assert_cmpfloat (elapsed
, >, 8.8);
646 g_assert_cmpfloat (elapsed
, <, 9.2);
647 if (g_test_verbose())
648 g_printerr ("g_timer_continue ... ok\n\n");
649 g_timer_stop (timer2
);
650 elapsed
= g_timer_elapsed (timer2
, &elapsed_usecs
);
651 if (g_test_verbose())
652 g_printerr ("\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)));
653 g_assert_cmpfloat (elapsed
, >, 8.8 + 6.5);
654 g_assert_cmpfloat (elapsed
, <, 9.2 + 6.5);
655 if (g_test_verbose())
656 g_printerr ("timer2 ... ok\n\n");
657 g_timer_destroy (timer
);
658 g_timer_destroy (timer2
);
665 guint16 gu16t1
= 0x44afU
, gu16t2
= 0xaf44U
;
666 guint32 gu32t1
= 0x02a7f109U
, gu32t2
= 0x09f1a702U
;
667 guint64 gu64t1
= G_GINT64_CONSTANT(0x1d636b02300a7aa7U
),
668 gu64t2
= G_GINT64_CONSTANT(0xa77a0a30026b631dU
);
670 g_assert_cmpint (sizeof (gint8
), ==, 1);
671 g_assert_cmpint (sizeof (gint16
), ==, 2);
672 g_assert_cmpint (sizeof (gint32
), ==, 4);
673 g_assert_cmpint (sizeof (gint64
), ==, 8);
675 if (g_test_verbose())
676 g_printerr ("checking endian macros (host is %s)...\n",
677 G_BYTE_ORDER
== G_BIG_ENDIAN
? "big endian" : "little endian");
678 g_assert (GUINT16_SWAP_LE_BE (gu16t1
) == gu16t2
);
679 g_assert (GUINT32_SWAP_LE_BE (gu32t1
) == gu32t2
);
680 g_assert (GUINT64_SWAP_LE_BE (gu64t1
) == gu64t2
);
686 const gchar
*un
, *rn
, *hn
;
687 const gchar
*tmpdir
, *homedir
, *userdatadir
, *uconfdir
, *ucachedir
;
688 const gchar
*uddesktop
, *udddocs
, *uddpubshare
, *uruntimedir
;
689 gchar
**sv
, *cwd
, *sdatadirs
, *sconfdirs
, *langnames
;
690 const gchar
*charset
;
691 gboolean charset_is_utf8
;
692 if (g_test_verbose())
693 g_printerr ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
700 cwd
= g_get_current_dir ();
701 un
= g_get_user_name();
702 rn
= g_get_real_name();
703 hn
= g_get_host_name();
704 if (g_test_verbose())
706 g_printerr ("cwd: %s\n", cwd
);
707 g_printerr ("user: %s\n", un
);
708 g_printerr ("real: %s\n", rn
);
709 g_printerr ("host: %s\n", hn
);
713 /* reload, just for fun */
714 g_reload_user_special_dirs_cache ();
715 g_reload_user_special_dirs_cache ();
717 tmpdir
= g_get_tmp_dir();
718 g_assert (tmpdir
!= NULL
);
719 homedir
= g_get_home_dir ();
720 g_assert (homedir
!= NULL
);
721 userdatadir
= g_get_user_data_dir ();
722 g_assert (userdatadir
!= NULL
);
723 uconfdir
= g_get_user_config_dir ();
724 g_assert (uconfdir
!= NULL
);
725 ucachedir
= g_get_user_cache_dir ();
726 g_assert (ucachedir
!= NULL
);
728 uddesktop
= g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP
);
729 g_assert (uddesktop
!= NULL
);
730 udddocs
= g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS
);
731 uddpubshare
= g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE
);
732 uruntimedir
= g_get_user_runtime_dir ();
733 g_assert (uruntimedir
!= NULL
);
735 sv
= (gchar
**) g_get_system_data_dirs ();
736 sdatadirs
= g_strjoinv (G_SEARCHPATH_SEPARATOR_S
, sv
);
737 sv
= (gchar
**) g_get_system_config_dirs ();
738 sconfdirs
= g_strjoinv (G_SEARCHPATH_SEPARATOR_S
, sv
);
739 sv
= (gchar
**) g_get_language_names ();
740 langnames
= g_strjoinv (":", sv
);
742 if (g_test_verbose())
744 g_printerr ("tmp-dir: %s\n", tmpdir
);
745 g_printerr ("home: %s\n", homedir
);
746 g_printerr ("user_data: %s\n", userdatadir
);
747 g_printerr ("user_config: %s\n", uconfdir
);
748 g_printerr ("user_cache: %s\n", ucachedir
);
749 g_printerr ("user_runtime: %s\n", uruntimedir
);
750 g_printerr ("system_data: %s\n", sdatadirs
);
751 g_printerr ("system_config: %s\n", sconfdirs
);
752 g_printerr ("languages: %s\n", langnames
);
753 g_printerr ("user_special[DESKTOP]: %s\n", uddesktop
);
754 g_printerr ("user_special[DOCUMENTS]: %s\n", udddocs
);
755 g_printerr ("user_special[PUBLIC_SHARE]: %s\n", uddpubshare
);
761 charset_is_utf8
= g_get_charset ((const char**)&charset
);
763 if (g_test_verbose())
766 g_printerr ("current charset is UTF-8: %s\n", charset
);
768 g_printerr ("current charset is not UTF-8: %s\n", charset
);
771 if (g_test_verbose())
773 #ifdef G_PLATFORM_WIN32
774 g_printerr ("current locale: %s\n", g_win32_getlocale ());
776 g_printerr ("found more.com as %s\n", g_find_program_in_path ("more.com"));
777 g_printerr ("found regedit as %s\n", g_find_program_in_path ("regedit"));
779 g_printerr ("a Win32 error message: %s\n", g_win32_error_message (2));
790 } dirname_checks
[] = {
801 { ".\\\\\\\\", "." },
803 { "..\\\\\\\\", ".." },
811 { "//server/share///x", "//server/share" },
817 const guint n_dirname_checks
= G_N_ELEMENTS (dirname_checks
);
821 } skip_root_checks
[] = {
830 { "\\\\server\\foo", "" },
831 { "\\\\server\\foo\\bar", "bar" },
835 { "//server/share///x", "//x" },
840 const guint n_skip_root_checks
= G_N_ELEMENTS (skip_root_checks
);
843 if (g_test_verbose())
844 g_printerr ("checking g_path_get_basename()...");
845 string
= g_path_get_basename (G_DIR_SEPARATOR_S
"foo" G_DIR_SEPARATOR_S
"dir" G_DIR_SEPARATOR_S
);
846 g_assert (strcmp (string
, "dir") == 0);
848 string
= g_path_get_basename (G_DIR_SEPARATOR_S
"foo" G_DIR_SEPARATOR_S
"file");
849 g_assert (strcmp (string
, "file") == 0);
851 if (g_test_verbose())
855 string
= g_path_get_basename ("/foo/dir/");
856 g_assert (strcmp (string
, "dir") == 0);
858 string
= g_path_get_basename ("/foo/file");
859 g_assert (strcmp (string
, "file") == 0);
863 if (g_test_verbose())
864 g_printerr ("checking g_path_get_dirname()...");
865 for (i
= 0; i
< n_dirname_checks
; i
++)
867 gchar
*dirname
= g_path_get_dirname (dirname_checks
[i
].filename
);
868 if (strcmp (dirname
, dirname_checks
[i
].dirname
) != 0)
870 g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
871 dirname_checks
[i
].filename
,
872 dirname_checks
[i
].dirname
,
877 if (g_test_verbose())
880 if (g_test_verbose())
881 g_printerr ("checking g_path_skip_root()...");
882 for (i
= 0; i
< n_skip_root_checks
; i
++)
884 const gchar
*skipped
= g_path_skip_root (skip_root_checks
[i
].filename
);
885 if ((skipped
&& !skip_root_checks
[i
].without_root
) ||
886 (!skipped
&& skip_root_checks
[i
].without_root
) ||
887 ((skipped
&& skip_root_checks
[i
].without_root
) &&
888 strcmp (skipped
, skip_root_checks
[i
].without_root
)))
890 g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
891 skip_root_checks
[i
].filename
,
892 (skip_root_checks
[i
].without_root
?
893 skip_root_checks
[i
].without_root
: "<NULL>"),
894 (skipped
? skipped
: "<NULL>"));
897 if (g_test_verbose())
902 test_file_functions (void)
904 const char hello
[] = "Hello, World";
905 const int hellolen
= sizeof (hello
) - 1;
908 char *name_used
, chars
[62];
911 strcpy (template, "foobar");
912 fd
= g_mkstemp (template);
913 if (g_test_verbose() && fd
!= -1)
914 g_printerr ("g_mkstemp works even if template doesn't end in XXXXXX\n");
917 strcpy (template, "fooXXXXXX");
918 fd
= g_mkstemp (template);
920 g_error ("g_mkstemp didn't work for template %s\n", template);
921 n
= write (fd
, hello
, hellolen
);
923 g_error ("write() failed: %s\n", g_strerror (errno
));
924 else if (n
!= hellolen
)
925 g_error ("write() should have written %d bytes, wrote %d\n", hellolen
, n
);
928 n
= read (fd
, chars
, sizeof (chars
));
930 g_error ("read() failed: %s\n", g_strerror (errno
));
931 else if (n
!= hellolen
)
932 g_error ("read() should have read %d bytes, got %d\n", hellolen
, n
);
935 if (strcmp (chars
, hello
) != 0)
936 g_error ("wrote '%s', but got '%s'\n", hello
, chars
);
943 strcpy (template, "zap" G_DIR_SEPARATOR_S
"barXXXXXX");
944 fd
= g_file_open_tmp (template, &name_used
, &error
);
945 if (g_test_verbose())
948 g_printerr ("g_file_open_tmp works even if template contains '%s'\n", G_DIR_SEPARATOR_S
);
950 g_printerr ("g_file_open_tmp correctly returns error: %s\n", error
->message
);
954 g_clear_error (&error
);
959 strcpy (template, "zap/barXXXXXX");
960 fd
= g_file_open_tmp (template, &name_used
, &error
);
961 if (g_test_verbose())
964 g_printerr ("g_file_open_tmp works even if template contains '/'\n");
966 g_printerr ("g_file_open_tmp correctly returns error: %s\n", error
->message
);
970 g_clear_error (&error
);
974 strcpy (template, "zapXXXXXX");
976 fd
= g_file_open_tmp (template, &name_used
, &error
);
978 g_error ("g_file_open_tmp didn't work for template '%s': %s\n", template, error
->message
);
979 else if (g_test_verbose())
980 g_printerr ("g_file_open_tmp for template '%s' used name '%s'\n", template, name_used
);
983 g_clear_error (&error
);
988 fd
= g_file_open_tmp (NULL
, &name_used
, &error
);
990 g_error ("g_file_open_tmp didn't work for a NULL template: %s\n", error
->message
);
993 g_clear_error (&error
);
1001 GByteArray
*gbarray
;
1006 gparray
= g_ptr_array_new ();
1007 for (i
= 0; i
< 10000; i
++)
1008 g_ptr_array_add (gparray
, GINT_TO_POINTER (i
));
1009 for (i
= 0; i
< 10000; i
++)
1010 if (g_ptr_array_index (gparray
, i
) != GINT_TO_POINTER (i
))
1011 g_error ("array fails: %p ( %p )\n", g_ptr_array_index (gparray
, i
), GINT_TO_POINTER (i
));
1012 g_ptr_array_free (gparray
, TRUE
);
1014 gbarray
= g_byte_array_new ();
1015 for (i
= 0; i
< 10000; i
++)
1016 g_byte_array_append (gbarray
, (guint8
*) "abcd", 4);
1017 for (i
= 0; i
< 10000; i
++)
1019 g_assert (gbarray
->data
[4*i
] == 'a');
1020 g_assert (gbarray
->data
[4*i
+1] == 'b');
1021 g_assert (gbarray
->data
[4*i
+2] == 'c');
1022 g_assert (gbarray
->data
[4*i
+3] == 'd');
1024 g_byte_array_free (gbarray
, TRUE
);
1026 garray
= g_array_new (FALSE
, FALSE
, sizeof (gint
));
1027 for (i
= 0; i
< 10000; i
++)
1028 g_array_append_val (garray
, i
);
1029 for (i
= 0; i
< 10000; i
++)
1030 if (g_array_index (garray
, gint
, i
) != i
)
1031 g_error ("failure: %d ( %d )\n", g_array_index (garray
, gint
, i
), i
);
1032 g_array_free (garray
, TRUE
);
1034 garray
= g_array_new (FALSE
, FALSE
, sizeof (gint
));
1035 for (i
= 0; i
< 100; i
++)
1036 g_array_prepend_val (garray
, i
);
1037 for (i
= 0; i
< 100; i
++)
1038 if (g_array_index (garray
, gint
, i
) != (100 - i
- 1))
1039 g_error ("failure: %d ( %d )\n", g_array_index (garray
, gint
, i
), 100 - i
- 1);
1040 g_array_free (garray
, TRUE
);
1044 hash_table_tests (void)
1046 GHashTable
*hash_table
;
1048 gint
*pvalue
= NULL
;
1052 hash_table
= g_hash_table_new (my_hash
, my_hash_equal
);
1053 for (i
= 0; i
< 10000; i
++)
1056 g_hash_table_insert (hash_table
, &array
[i
], &array
[i
]);
1058 pvalue
= g_hash_table_find (hash_table
, find_first_that
, &value
);
1059 if (*pvalue
!= value
)
1060 g_error ("g_hash_table_find failed");
1061 g_hash_table_foreach (hash_table
, my_hash_callback
, NULL
);
1062 for (i
= 0; i
< 10000; i
++)
1064 g_error ("hashtable-test: wrong value: %d\n", i
);
1065 for (i
= 0; i
< 10000; i
++)
1066 g_hash_table_remove (hash_table
, &array
[i
]);
1067 for (i
= 0; i
< 10000; i
++)
1070 g_hash_table_insert (hash_table
, &array
[i
], &array
[i
]);
1072 if (g_hash_table_foreach_remove (hash_table
, my_hash_callback_remove
, NULL
) != 5000 ||
1073 g_hash_table_size (hash_table
) != 5000)
1074 g_error ("hashtable removal failed\n");
1075 g_hash_table_foreach (hash_table
, my_hash_callback_remove_test
, NULL
);
1076 g_hash_table_destroy (hash_table
);
1079 #ifndef G_DISABLE_DEPRECATED
1081 relation_test (void)
1083 GRelation
*relation
= g_relation_new (2);
1088 g_relation_index (relation
, 0, g_int_hash
, g_int_equal
);
1089 g_relation_index (relation
, 1, g_int_hash
, g_int_equal
);
1091 for (i
= 0; i
< 1024; i
+= 1)
1094 for (i
= 1; i
< 1023; i
+= 1)
1096 g_relation_insert (relation
, data
+ i
, data
+ i
+ 1);
1097 g_relation_insert (relation
, data
+ i
, data
+ i
- 1);
1100 for (i
= 2; i
< 1022; i
+= 1)
1102 g_assert (! g_relation_exists (relation
, data
+ i
, data
+ i
));
1103 g_assert (! g_relation_exists (relation
, data
+ i
, data
+ i
+ 2));
1104 g_assert (! g_relation_exists (relation
, data
+ i
, data
+ i
- 2));
1107 for (i
= 1; i
< 1023; i
+= 1)
1109 g_assert (g_relation_exists (relation
, data
+ i
, data
+ i
+ 1));
1110 g_assert (g_relation_exists (relation
, data
+ i
, data
+ i
- 1));
1113 for (i
= 2; i
< 1022; i
+= 1)
1115 g_assert (g_relation_count (relation
, data
+ i
, 0) == 2);
1116 g_assert (g_relation_count (relation
, data
+ i
, 1) == 2);
1119 g_assert (g_relation_count (relation
, data
, 0) == 0);
1121 g_assert (g_relation_count (relation
, data
+ 42, 0) == 2);
1122 g_assert (g_relation_count (relation
, data
+ 43, 1) == 2);
1123 g_assert (g_relation_count (relation
, data
+ 41, 1) == 2);
1124 g_relation_delete (relation
, data
+ 42, 0);
1125 g_assert (g_relation_count (relation
, data
+ 42, 0) == 0);
1126 g_assert (g_relation_count (relation
, data
+ 43, 1) == 1);
1127 g_assert (g_relation_count (relation
, data
+ 41, 1) == 1);
1129 tuples
= g_relation_select (relation
, data
+ 200, 0);
1131 g_assert (tuples
->len
== 2);
1134 for (i
= 0; i
< tuples
->len
; i
+= 1)
1137 *(gint
*) g_tuples_index (tuples
, i
, 0),
1138 *(gint
*) g_tuples_index (tuples
, i
, 1));
1142 g_assert (g_relation_exists (relation
, data
+ 300, data
+ 301));
1143 g_relation_delete (relation
, data
+ 300, 0);
1144 g_assert (!g_relation_exists (relation
, data
+ 300, data
+ 301));
1146 g_tuples_destroy (tuples
);
1148 g_relation_destroy (relation
);
1155 gstring_tests (void)
1157 GString
*string1
, *string2
;
1160 if (g_test_verbose())
1161 g_printerr ("test GString basics\n");
1163 string1
= g_string_new ("hi pete!");
1164 string2
= g_string_new ("");
1166 g_assert (strcmp ("hi pete!", string1
->str
) == 0);
1168 for (i
= 0; i
< 10000; i
++)
1169 g_string_append_c (string1
, 'a'+(i
%26));
1172 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
1173 the %10000.10000f format... */
1174 g_string_printf (string2
, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
1175 "this pete guy sure is a wuss, like he's the number ",
1177 " wuss. everyone agrees.\n",
1179 10, 666, 15, 15, 666.666666666, 666.666666666);
1181 g_string_printf (string2
, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
1182 "this pete guy sure is a wuss, like he's the number ",
1184 " wuss. everyone agrees.\n",
1186 10, 666, 15, 15, 666.666666666, 666.666666666);
1189 if (g_test_verbose())
1190 g_printerr ("string2 length = %lu...\n", (gulong
)string2
->len
);
1191 string2
->str
[70] = '\0';
1192 if (g_test_verbose())
1193 g_printerr ("first 70 chars:\n%s\n", string2
->str
);
1194 string2
->str
[141] = '\0';
1195 if (g_test_verbose())
1196 g_printerr ("next 70 chars:\n%s\n", string2
->str
+71);
1197 string2
->str
[212] = '\0';
1198 if (g_test_verbose())
1199 g_printerr ("and next 70:\n%s\n", string2
->str
+142);
1200 if (g_test_verbose())
1201 g_printerr ("last 70 chars:\n%s\n", string2
->str
+string2
->len
- 70);
1203 g_string_free (string1
, TRUE
);
1204 g_string_free (string2
, TRUE
);
1207 string1
= g_string_new ("firsthalf");
1208 g_string_append (string1
, "lasthalf");
1209 g_assert (strcmp (string1
->str
, "firsthalflasthalf") == 0);
1210 g_string_free (string1
, TRUE
);
1213 string1
= g_string_new ("firsthalf");
1214 g_string_append_len (string1
, "lasthalfjunkjunk", strlen ("lasthalf"));
1215 g_assert (strcmp (string1
->str
, "firsthalflasthalf") == 0);
1216 g_string_free (string1
, TRUE
);
1219 string1
= g_string_new ("lasthalf");
1220 g_string_prepend (string1
, "firsthalf");
1221 g_assert (strcmp (string1
->str
, "firsthalflasthalf") == 0);
1222 g_string_free (string1
, TRUE
);
1225 string1
= g_string_new ("lasthalf");
1226 g_string_prepend_len (string1
, "firsthalfjunkjunk", strlen ("firsthalf"));
1227 g_assert (strcmp (string1
->str
, "firsthalflasthalf") == 0);
1228 g_string_free (string1
, TRUE
);
1231 string1
= g_string_new ("firstlast");
1232 g_string_insert (string1
, 5, "middle");
1233 g_assert (strcmp (string1
->str
, "firstmiddlelast") == 0);
1234 g_string_free (string1
, TRUE
);
1236 /* insert with pos == end of the string */
1237 string1
= g_string_new ("firstmiddle");
1238 g_string_insert (string1
, strlen ("firstmiddle"), "last");
1239 g_assert (strcmp (string1
->str
, "firstmiddlelast") == 0);
1240 g_string_free (string1
, TRUE
);
1243 string1
= g_string_new ("firstlast");
1244 g_string_insert_len (string1
, 5, "middlejunkjunk", strlen ("middle"));
1245 g_assert (strcmp (string1
->str
, "firstmiddlelast") == 0);
1246 g_string_free (string1
, TRUE
);
1248 /* insert_len with magic -1 pos for append */
1249 string1
= g_string_new ("first");
1250 g_string_insert_len (string1
, -1, "lastjunkjunk", strlen ("last"));
1251 g_assert (strcmp (string1
->str
, "firstlast") == 0);
1252 g_string_free (string1
, TRUE
);
1254 /* insert_len with magic -1 len for strlen-the-string */
1255 string1
= g_string_new ("first");
1256 g_string_insert_len (string1
, 5, "last", -1);
1257 g_assert (strcmp (string1
->str
, "firstlast") == 0);
1258 g_string_free (string1
, TRUE
);
1260 /* g_string_equal */
1261 string1
= g_string_new ("test");
1262 string2
= g_string_new ("te");
1263 g_assert (! g_string_equal(string1
, string2
));
1264 g_string_append (string2
, "st");
1265 g_assert (g_string_equal(string1
, string2
));
1266 g_string_free (string1
, TRUE
);
1267 g_string_free (string2
, TRUE
);
1269 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
1270 if (g_test_verbose())
1271 g_printerr ("test embedded ASCII 0 (NUL) characters in GString\n");
1272 string1
= g_string_new ("fiddle");
1273 string2
= g_string_new ("fiddle");
1274 g_assert (g_string_equal(string1
, string2
));
1275 g_string_append_c(string1
, '\0');
1276 g_assert (! g_string_equal(string1
, string2
));
1277 g_string_append_c(string2
, '\0');
1278 g_assert (g_string_equal(string1
, string2
));
1279 g_string_append_c(string1
, 'x');
1280 g_string_append_c(string2
, 'y');
1281 g_assert (! g_string_equal(string1
, string2
));
1282 g_assert (string1
->len
== 8);
1283 g_string_append(string1
, "yzzy");
1284 g_assert (string1
->len
== 12);
1285 g_assert ( memcmp(string1
->str
, "fiddle\0xyzzy", 13) == 0);
1286 g_string_insert(string1
, 1, "QED");
1287 g_assert ( memcmp(string1
->str
, "fQEDiddle\0xyzzy", 16) == 0);
1288 g_string_free (string1
, TRUE
);
1289 g_string_free (string2
, TRUE
);
1293 various_string_tests (void)
1295 GStringChunk
*string_chunk
;
1296 GTimeVal ref_date
, date
;
1297 gchar
*tmp_string
= NULL
, *tmp_string_2
, *string
, *date_str
;
1301 if (g_test_verbose())
1302 g_printerr ("checking string chunks...");
1303 string_chunk
= g_string_chunk_new (1024);
1304 for (i
= 0; i
< 100000; i
++)
1306 tmp_string
= g_string_chunk_insert (string_chunk
, "hi pete");
1307 if (strcmp ("hi pete", tmp_string
) != 0)
1308 g_error ("string chunks are broken.\n");
1310 tmp_string_2
= g_string_chunk_insert_const (string_chunk
, tmp_string
);
1311 g_assert (tmp_string_2
!= tmp_string
&& strcmp (tmp_string_2
, tmp_string
) == 0);
1312 tmp_string
= g_string_chunk_insert_const (string_chunk
, tmp_string
);
1313 g_assert (tmp_string_2
== tmp_string
);
1314 g_string_chunk_free (string_chunk
);
1316 if (g_test_verbose())
1317 g_printerr ("test positional printf formats (not supported):");
1318 string
= g_strdup_printf ("%.*s%s", 5, "a", "b");
1319 tmp_string
= g_strdup_printf ("%2$*1$s", 5, "c");
1320 if (g_test_verbose())
1321 g_printerr ("%s%s\n", string
, tmp_string
);
1322 g_free (tmp_string
);
1325 #define REF_INVALID1 "Wed Dec 19 17:20:20 GMT 2007"
1326 #define REF_INVALID2 "1980-02-22T10:36:00Zulu"
1327 #define REF_INVALID3 "1980-02-22T"
1328 #define REF_SEC_UTC 320063760
1329 #define REF_STR_UTC "1980-02-22T10:36:00Z"
1330 #define REF_STR_LOCAL "1980-02-22T13:36:00"
1331 #define REF_STR_CEST "1980-02-22T12:36:00+02:00"
1332 #define REF_STR_EST "19800222T053600-0500"
1333 #define REF_STR_NST "1980-02-22T07:06:00-03:30"
1334 #define REF_USEC_UTC 50000
1335 #define REF_STR_USEC_UTC "1980-02-22T10:36:00.050000Z"
1336 #define REF_STR_USEC_CEST "19800222T123600.050000000+0200"
1337 #define REF_STR_USEC_EST "1980-02-22T05:36:00,05-05:00"
1338 #define REF_STR_USEC_NST "19800222T070600,0500-0330"
1339 #define REF_STR_DATE_ONLY "1980-02-22"
1341 if (g_test_verbose())
1342 g_printerr ("checking g_time_val_from_iso8601...\n");
1343 ref_date
.tv_sec
= REF_SEC_UTC
;
1344 ref_date
.tv_usec
= 0;
1345 g_assert (g_time_val_from_iso8601 (REF_INVALID1
, &date
) == FALSE
);
1346 g_assert (g_time_val_from_iso8601 (REF_INVALID2
, &date
) == FALSE
);
1347 g_assert (g_time_val_from_iso8601 (REF_INVALID3
, &date
) == FALSE
);
1348 g_assert (g_time_val_from_iso8601 (REF_STR_DATE_ONLY
, &date
) == FALSE
);
1349 g_assert (g_time_val_from_iso8601 (REF_STR_UTC
, &date
) != FALSE
);
1350 if (g_test_verbose())
1351 g_printerr ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1352 date
.tv_sec
, date
.tv_usec
, ref_date
.tv_sec
, ref_date
.tv_usec
,
1353 date
.tv_sec
- ref_date
.tv_sec
, date
.tv_usec
- ref_date
.tv_usec
);
1354 g_assert (date
.tv_sec
== ref_date
.tv_sec
&& date
.tv_usec
== ref_date
.tv_usec
);
1356 /* predefine time zone */
1357 tz
= g_getenv("TZ");
1358 g_setenv("TZ", "UTC-03:00", 1);
1361 g_assert (g_time_val_from_iso8601 (REF_STR_LOCAL
, &date
) != FALSE
);
1362 if (g_test_verbose())
1363 g_printerr ("\t=> LOCAL stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1364 date
.tv_sec
, date
.tv_usec
, ref_date
.tv_sec
, ref_date
.tv_usec
,
1365 date
.tv_sec
- ref_date
.tv_sec
, date
.tv_usec
- ref_date
.tv_usec
);
1366 g_assert (date
.tv_sec
== ref_date
.tv_sec
&& date
.tv_usec
== ref_date
.tv_usec
);
1368 /* revert back user defined time zone */
1370 g_setenv("TZ", tz
, TRUE
);
1375 g_assert (g_time_val_from_iso8601 (REF_STR_CEST
, &date
) != FALSE
);
1376 if (g_test_verbose())
1377 g_printerr ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1378 date
.tv_sec
, date
.tv_usec
, ref_date
.tv_sec
, ref_date
.tv_usec
,
1379 date
.tv_sec
- ref_date
.tv_sec
, date
.tv_usec
- ref_date
.tv_usec
);
1380 g_assert (date
.tv_sec
== ref_date
.tv_sec
&& date
.tv_usec
== ref_date
.tv_usec
);
1382 g_assert (g_time_val_from_iso8601 (REF_STR_EST
, &date
) != FALSE
);
1383 if (g_test_verbose())
1384 g_printerr ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1385 date
.tv_sec
, date
.tv_usec
, ref_date
.tv_sec
, ref_date
.tv_usec
,
1386 date
.tv_sec
- ref_date
.tv_sec
, date
.tv_usec
- ref_date
.tv_usec
);
1387 g_assert (date
.tv_sec
== ref_date
.tv_sec
&& date
.tv_usec
== ref_date
.tv_usec
);
1389 g_assert (g_time_val_from_iso8601 (REF_STR_NST
, &date
) != FALSE
);
1390 if (g_test_verbose())
1391 g_printerr ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1392 date
.tv_sec
, date
.tv_usec
, ref_date
.tv_sec
, ref_date
.tv_usec
,
1393 date
.tv_sec
- ref_date
.tv_sec
, date
.tv_usec
- ref_date
.tv_usec
);
1394 g_assert (date
.tv_sec
== ref_date
.tv_sec
&& date
.tv_usec
== ref_date
.tv_usec
);
1396 ref_date
.tv_usec
= REF_USEC_UTC
;
1397 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_UTC
, &date
) != FALSE
);
1398 if (g_test_verbose())
1399 g_printerr ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1400 date
.tv_sec
, date
.tv_usec
, ref_date
.tv_sec
, ref_date
.tv_usec
,
1401 date
.tv_sec
- ref_date
.tv_sec
, date
.tv_usec
- ref_date
.tv_usec
);
1402 g_assert (date
.tv_sec
== ref_date
.tv_sec
&& date
.tv_usec
== ref_date
.tv_usec
);
1404 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_CEST
, &date
) != FALSE
);
1405 if (g_test_verbose())
1406 g_printerr ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1407 date
.tv_sec
, date
.tv_usec
, ref_date
.tv_sec
, ref_date
.tv_usec
,
1408 date
.tv_sec
- ref_date
.tv_sec
, date
.tv_usec
- ref_date
.tv_usec
);
1409 g_assert (date
.tv_sec
== ref_date
.tv_sec
&& date
.tv_usec
== ref_date
.tv_usec
);
1411 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_EST
, &date
) != FALSE
);
1412 if (g_test_verbose())
1413 g_printerr ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1414 date
.tv_sec
, date
.tv_usec
, ref_date
.tv_sec
, ref_date
.tv_usec
,
1415 date
.tv_sec
- ref_date
.tv_sec
, date
.tv_usec
- ref_date
.tv_usec
);
1416 g_assert (date
.tv_sec
== ref_date
.tv_sec
&& date
.tv_usec
== ref_date
.tv_usec
);
1418 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_NST
, &date
) != FALSE
);
1419 if (g_test_verbose())
1420 g_printerr ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1421 date
.tv_sec
, date
.tv_usec
, ref_date
.tv_sec
, ref_date
.tv_usec
,
1422 date
.tv_sec
- ref_date
.tv_sec
, date
.tv_usec
- ref_date
.tv_usec
);
1423 g_assert (date
.tv_sec
== ref_date
.tv_sec
&& date
.tv_usec
== ref_date
.tv_usec
);
1425 if (g_test_verbose())
1426 g_printerr ("checking g_time_val_to_iso8601...\n");
1427 ref_date
.tv_sec
= REF_SEC_UTC
;
1428 ref_date
.tv_usec
= 0;
1429 date_str
= g_time_val_to_iso8601 (&ref_date
);
1430 g_assert (date_str
!= NULL
);
1431 if (g_test_verbose())
1432 g_printerr ("\t=> date string = %s (should be: %s)\n", date_str
, REF_STR_UTC
);
1433 g_assert (strcmp (date_str
, REF_STR_UTC
) == 0);
1436 ref_date
.tv_usec
= REF_USEC_UTC
;
1437 date_str
= g_time_val_to_iso8601 (&ref_date
);
1438 g_assert (date_str
!= NULL
);
1439 if (g_test_verbose())
1440 g_printerr ("\t=> date string = %s (should be: %s)\n", date_str
, REF_STR_USEC_UTC
);
1441 g_assert (strcmp (date_str
, REF_STR_USEC_UTC
) == 0);
1444 if (g_test_verbose())
1445 g_printerr ("checking g_ascii_strcasecmp...");
1446 g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
1447 g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
1448 g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
1449 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1450 g_assert (g_ascii_strcasecmp ("", "") == 0);
1451 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
1452 g_assert (g_ascii_strcasecmp ("a", "b") < 0);
1453 g_assert (g_ascii_strcasecmp ("a", "B") < 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 ("b", "a") > 0);
1457 g_assert (g_ascii_strcasecmp ("b", "A") > 0);
1458 g_assert (g_ascii_strcasecmp ("B", "a") > 0);
1459 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1461 if (g_test_verbose())
1462 g_printerr ("checking g_strdup...\n");
1463 g_assert (g_strdup (NULL
) == NULL
);
1464 string
= g_strdup (GLIB_TEST_STRING
);
1465 g_assert (string
!= NULL
);
1466 g_assert (strcmp(string
, GLIB_TEST_STRING
) == 0);
1469 if (g_test_verbose())
1470 g_printerr ("checking g_strconcat...\n");
1471 string
= g_strconcat (GLIB_TEST_STRING
, NULL
);
1472 g_assert (string
!= NULL
);
1473 g_assert (strcmp (string
, GLIB_TEST_STRING
) == 0);
1475 string
= g_strconcat (GLIB_TEST_STRING
, GLIB_TEST_STRING
,
1476 GLIB_TEST_STRING
, NULL
);
1477 g_assert (string
!= NULL
);
1478 g_assert (strcmp (string
, GLIB_TEST_STRING GLIB_TEST_STRING
1479 GLIB_TEST_STRING
) == 0);
1482 if (g_test_verbose())
1483 g_printerr ("checking g_strlcpy/g_strlcat...");
1484 /* The following is a torture test for strlcpy/strlcat, with lots of
1485 * checking; normal users wouldn't use them this way!
1487 string
= g_malloc (6);
1488 *(string
+ 5) = 'Z'; /* guard value, shouldn't change during test */
1490 g_assert (g_strlcpy(string
, "" , 5) == 0);
1491 g_assert ( *string
== '\0' );
1493 g_assert (g_strlcpy(string
, "abc" , 5) == 3);
1494 g_assert ( *(string
+ 3) == '\0' );
1495 g_assert (g_str_equal(string
, "abc"));
1496 g_assert (g_strlcpy(string
, "abcd" , 5) == 4);
1497 g_assert ( *(string
+ 4) == '\0' );
1498 g_assert ( *(string
+ 5) == 'Z' );
1499 g_assert (g_str_equal(string
, "abcd"));
1500 g_assert (g_strlcpy(string
, "abcde" , 5) == 5);
1501 g_assert ( *(string
+ 4) == '\0' );
1502 g_assert ( *(string
+ 5) == 'Z' );
1503 g_assert (g_str_equal(string
, "abcd"));
1504 g_assert (g_strlcpy(string
, "abcdef" , 5) == 6);
1505 g_assert ( *(string
+ 4) == '\0' );
1506 g_assert ( *(string
+ 5) == 'Z' );
1507 g_assert (g_str_equal(string
, "abcd"));
1509 *(string
+ 1)= '\0';
1510 g_assert (g_strlcpy(string
, "Hello" , 0) == 5);
1511 g_assert (*string
== 'Y');
1513 g_assert (g_strlcat(string
, "123" , 5) == 3);
1514 g_assert ( *(string
+ 3) == '\0' );
1515 g_assert (g_str_equal(string
, "123"));
1516 g_assert (g_strlcat(string
, "" , 5) == 3);
1517 g_assert ( *(string
+ 3) == '\0' );
1518 g_assert (g_str_equal(string
, "123"));
1519 g_assert (g_strlcat(string
, "4", 5) == 4);
1520 g_assert (g_str_equal(string
, "1234"));
1521 g_assert (g_strlcat(string
, "5", 5) == 5);
1522 g_assert ( *(string
+ 4) == '\0' );
1523 g_assert (g_str_equal(string
, "1234"));
1524 g_assert ( *(string
+ 5) == 'Z' );
1526 *(string
+ 1)= '\0';
1527 g_assert (g_strlcat(string
, "123" , 0) == 3);
1528 g_assert (*string
== 'Y');
1530 /* A few more tests, demonstrating more "normal" use */
1531 g_assert (g_strlcpy(string
, "hi", 5) == 2);
1532 g_assert (g_str_equal(string
, "hi"));
1533 g_assert (g_strlcat(string
, "t", 5) == 3);
1534 g_assert (g_str_equal(string
, "hit"));
1537 if (g_test_verbose())
1538 g_printerr ("checking g_strdup_printf...\n");
1539 string
= g_strdup_printf ("%05d %-5s", 21, "test");
1540 g_assert (string
!= NULL
);
1541 g_assert (strcmp(string
, "00021 test ") == 0);
1544 /* g_debug (argv[0]); */
1547 #ifndef G_DISABLE_DEPRECATED
1549 test_mem_chunks (void)
1551 GMemChunk
*mem_chunk
= g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE
);
1554 for (i
= 0; i
< 10000; i
++)
1557 mem
[i
] = g_chunk_new (gchar
, mem_chunk
);
1558 for (j
= 0; j
< 50; j
++)
1561 for (i
= 0; i
< 10000; i
++)
1562 g_mem_chunk_free (mem_chunk
, mem
[i
]);
1564 g_mem_chunk_destroy (mem_chunk
);
1572 g_test_init (&argc
, &argv
, NULL
);
1574 g_test_add_func ("/testglib/Infos", test_info
);
1575 g_test_add_func ("/testglib/Types Sizes", type_sizes
);
1576 g_test_add_func ("/testglib/GStrings", gstring_tests
);
1577 g_test_add_func ("/testglib/Various Strings", various_string_tests
);
1578 g_test_add_func ("/testglib/GList", glist_test
);
1579 g_test_add_func ("/testglib/GSList", gslist_test
);
1580 g_test_add_func ("/testglib/GNode", gnode_test
);
1581 g_test_add_func ("/testglib/GTree", binary_tree_test
);
1582 g_test_add_func ("/testglib/Arrays", test_arrays
);
1583 g_test_add_func ("/testglib/GHashTable", hash_table_tests
);
1584 #ifndef G_DISABLE_DEPRECATED
1585 g_test_add_func ("/testglib/Relation (deprecated)", relation_test
);
1587 g_test_add_func ("/testglib/File Paths", test_paths
);
1588 g_test_add_func ("/testglib/File Functions", test_file_functions
);
1589 g_test_add_func ("/testglib/Parse Debug Strings", test_g_parse_debug_string
);
1590 #ifndef G_DISABLE_DEPRECATED
1591 g_test_add_func ("/testglib/GMemChunk (deprecated)", test_mem_chunks
);
1593 g_test_add_func ("/testglib/Warnings & Errors", log_warning_error_tests
);
1594 g_test_add_func ("/testglib/Timers (slow)", timer_tests
);
1596 return g_test_run();