Update Friulian translation
[glib.git] / tests / testglib.c
blobb5a833178efdf82e5eaee2805fdbe812696f314b
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.1 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
27 #include <stdio.h>
28 #include <string.h>
29 #include <errno.h>
31 #include "glib.h"
32 #include <glib/gstdio.h>
34 #include <stdlib.h>
36 #ifdef G_OS_UNIX
37 #include <unistd.h>
38 #endif
40 #ifdef G_OS_WIN32
41 #include <io.h> /* For read(), write() etc */
42 #endif
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 --- */
54 static gint
55 my_list_compare_one (gconstpointer a, gconstpointer b)
57 gint one = *((const gint*)a);
58 gint two = *((const gint*)b);
59 return one-two;
62 static gint
63 my_list_compare_two (gconstpointer a, gconstpointer b)
65 gint one = *((const gint*)a);
66 gint two = *((const gint*)b);
67 return two-one;
70 /* static void
71 my_list_print (gpointer a, gpointer b)
73 gint three = *((gint*)a);
74 g_printerr ("%d", three);
75 }; */
77 static void
78 glist_test (void)
80 GList *list = NULL;
81 guint i;
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");
98 g_list_free (list);
99 list = NULL;
101 for (i = 0; i < 10; i++)
102 list = g_list_insert_sorted (list, &more_nums[i], my_list_compare_one);
105 g_printerr ("\n");
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");
116 g_list_free (list);
117 list = NULL;
119 for (i = 0; i < 10; i++)
120 list = g_list_insert_sorted (list, &more_nums[i], my_list_compare_two);
123 g_printerr ("\n");
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");
134 g_list_free (list);
135 list = NULL;
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);
143 g_printerr ("\n");
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");
154 g_list_free (list);
157 static void
158 gslist_test (void)
160 GSList *slist = NULL;
161 guint i;
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))
171 g_error ("failed");
174 g_slist_free (slist);
175 slist = NULL;
177 for (i = 0; i < 10; i++)
178 slist = g_slist_insert_sorted (slist, &more_nums[i], my_list_compare_one);
181 g_printerr ("\n");
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);
193 slist = NULL;
195 for (i = 0; i < 10; i++)
196 slist = g_slist_insert_sorted (slist, &more_nums[i], my_list_compare_two);
199 g_printerr ("\n");
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");
210 g_slist_free(slist);
211 slist = NULL;
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);
219 g_printerr ("\n");
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");
230 g_slist_free(slist);
233 static gboolean
234 node_build_string (GNode *node,
235 gpointer data)
237 gchar **p = data;
238 gchar *string;
239 gchar c[2] = "_";
241 c[0] = ((gchar) ((gintptr) (node->data)));
243 string = g_strconcat (*p ? *p : "", c, NULL);
244 g_free (*p);
245 *p = string;
247 return FALSE;
250 static void
251 gnode_test (void)
253 #define C2P(c) ((gpointer) ((long) (c)))
254 #define P2C(p) ((gchar) ((gintptr) (p)))
255 GNode *root;
256 GNode *node;
257 GNode *node_B;
258 GNode *node_F;
259 GNode *node_G;
260 GNode *node_J;
261 guint i;
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);
308 /* we have built: A
309 * / \
310 * B F
311 * / | \ \
312 * C D E G
313 * / /\ \
314 * H I J K
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.
320 tstring = NULL;
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;
348 cstring = 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);
364 node = root;
366 for (i = 0; i < 2048; i++)
368 g_node_append (node, g_node_new (NULL));
369 if ((i%5) == 4)
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);
376 #undef C2P
377 #undef P2C
380 static gint
381 my_compare (gconstpointer a,
382 gconstpointer b)
384 const char *cha = a;
385 const char *chb = b;
387 return *cha - *chb;
390 static gint
391 my_traverse (gpointer key,
392 gpointer value,
393 gpointer data)
395 char *ch = key;
396 g_printerr ("%c ", *ch);
397 return FALSE;
400 static void
401 binary_tree_test (void)
403 GTree *tree;
404 char chars[62];
405 guint i, j;
407 tree = g_tree_new (my_compare);
408 i = 0;
409 for (j = 0; j < 10; j++, i++)
411 chars[i] = '0' + j;
412 g_tree_insert (tree, &chars[i], &chars[i]);
414 for (j = 0; j < 26; j++, i++)
416 chars[i] = 'A' + j;
417 g_tree_insert (tree, &chars[i], &chars[i]);
419 for (j = 0; j < 26; j++, i++)
421 chars[i] = 'a' + j;
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);
432 g_printerr ("\n");
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);
445 g_printerr ("\n");
448 g_tree_unref (tree);
451 static gboolean
452 my_hash_callback_remove (gpointer key,
453 gpointer value,
454 gpointer user_data)
456 int *d = value;
458 if ((*d) % 2)
459 return TRUE;
461 return FALSE;
464 static void
465 my_hash_callback_remove_test (gpointer key,
466 gpointer value,
467 gpointer user_data)
469 int *d = value;
471 if ((*d) % 2)
472 g_error ("hash table entry %d should have been removed already\n", *d);
475 static void
476 my_hash_callback (gpointer key,
477 gpointer value,
478 gpointer user_data)
480 int *d = value;
481 *d = 1;
484 static guint
485 my_hash (gconstpointer key)
487 return (guint) *((const gint*) key);
490 static gboolean
491 my_hash_equal (gconstpointer a,
492 gconstpointer b)
494 return *((const gint*) a) == *((const gint*) b);
497 static gboolean
498 find_first_that(gpointer key,
499 gpointer value,
500 gpointer user_data)
502 gint *v = value;
503 gint *test = user_data;
504 return (*v == *test);
507 static void
508 test_g_parse_debug_string (void)
510 GDebugKey keys[] = {
511 { "foo", 1 },
512 { "bar", 2 },
513 { "baz", 4 },
514 { "weird", 8 },
516 guint n_keys = G_N_ELEMENTS (keys);
517 guint result;
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);
548 static void
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,
562 "*unsafe chars*");
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*");
573 g_print (NULL);
574 g_test_assert_expected_messages ();
577 static void
578 timer_tests (void)
580 GTimer *timer, *timer2;
581 gdouble elapsed;
583 /* basic testing */
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);
591 if (g_test_slow())
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())
604 g_printerr ("ok\n");
607 if (g_test_slow())
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 */
625 g_timer_stop(timer);
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 */
641 g_timer_stop(timer);
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);
662 static void
663 type_sizes (void)
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);
669 /* type sizes */
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);
674 /* endian macros */
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);
683 static void
684 test_info (void)
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",
694 glib_major_version,
695 glib_minor_version,
696 glib_micro_version,
697 glib_interface_age,
698 glib_binary_age);
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);
711 g_free (cwd);
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);
757 g_free (sdatadirs);
758 g_free (sconfdirs);
759 g_free (langnames);
761 charset_is_utf8 = g_get_charset ((const char**)&charset);
763 if (g_test_verbose())
765 if (charset_is_utf8)
766 g_printerr ("current charset is UTF-8: %s\n", charset);
767 else
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));
780 #endif
784 static void
785 test_paths (void)
787 struct {
788 gchar *filename;
789 gchar *dirname;
790 } dirname_checks[] = {
791 { "/", "/" },
792 { "////", "/" },
793 { ".////", "." },
794 { "../", ".." },
795 { "..////", ".." },
796 { "a/b", "a" },
797 { "a/b/", "a/b" },
798 { "c///", "c" },
799 #ifdef G_OS_WIN32
800 { "\\", "\\" },
801 { ".\\\\\\\\", "." },
802 { "..\\", ".." },
803 { "..\\\\\\\\", ".." },
804 { "a\\b", "a" },
805 { "a\\b/", "a\\b" },
806 { "a/b\\", "a/b" },
807 { "c\\\\/", "c" },
808 { "//\\", "/" },
809 #endif
810 #ifdef G_WITH_CYGWIN
811 { "//server/share///x", "//server/share" },
812 #endif
813 { ".", "." },
814 { "..", "." },
815 { "", "." },
817 const guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
818 struct {
819 gchar *filename;
820 gchar *without_root;
821 } skip_root_checks[] = {
822 { "/", "" },
823 { "//", "" },
824 { "/foo", "foo" },
825 { "//foo", "foo" },
826 { "a/b", NULL },
827 #ifdef G_OS_WIN32
828 { "\\", "" },
829 { "\\foo", "foo" },
830 { "\\\\server\\foo", "" },
831 { "\\\\server\\foo\\bar", "bar" },
832 { "a\\b", NULL },
833 #endif
834 #ifdef G_WITH_CYGWIN
835 { "//server/share///x", "//x" },
836 #endif
837 { ".", NULL },
838 { "", NULL },
840 const guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
841 gchar *string;
842 guint i;
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);
847 g_free (string);
848 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
849 g_assert (strcmp (string, "file") == 0);
850 g_free (string);
851 if (g_test_verbose())
852 g_printerr ("ok\n");
854 #ifdef G_OS_WIN32
855 string = g_path_get_basename ("/foo/dir/");
856 g_assert (strcmp (string, "dir") == 0);
857 g_free (string);
858 string = g_path_get_basename ("/foo/file");
859 g_assert (strcmp (string, "file") == 0);
860 g_free (string);
861 #endif
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,
873 dirname);
875 g_free (dirname);
877 if (g_test_verbose())
878 g_printerr ("ok\n");
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())
898 g_printerr ("ok\n");
901 static void
902 test_file_functions (void)
904 const char hello[] = "Hello, World";
905 const int hellolen = sizeof (hello) - 1;
906 GError *error;
907 char template[32];
908 char *name_used, chars[62];
909 gint fd, n;
910 int errsv;
912 strcpy (template, "foobar");
913 fd = g_mkstemp (template);
914 if (g_test_verbose() && fd != -1)
915 g_printerr ("g_mkstemp works even if template doesn't end in XXXXXX\n");
916 if (fd != -1)
917 close (fd);
918 strcpy (template, "fooXXXXXX");
919 fd = g_mkstemp (template);
920 if (fd == -1)
921 g_error ("g_mkstemp didn't work for template %s\n", template);
922 n = write (fd, hello, hellolen);
923 errsv = errno;
924 if (n == -1)
925 g_error ("write() failed: %s\n", g_strerror (errsv));
926 else if (n != hellolen)
927 g_error ("write() should have written %d bytes, wrote %d\n", hellolen, n);
929 lseek (fd, 0, 0);
930 n = read (fd, chars, sizeof (chars));
931 errsv = errno;
932 if (n == -1)
933 g_error ("read() failed: %s\n", g_strerror (errsv));
934 else if (n != hellolen)
935 g_error ("read() should have read %d bytes, got %d\n", hellolen, n);
937 chars[n] = 0;
938 if (strcmp (chars, hello) != 0)
939 g_error ("wrote '%s', but got '%s'\n", hello, chars);
940 if (fd != -1)
941 close (fd);
942 remove (template);
944 error = NULL;
945 name_used = NULL;
946 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
947 fd = g_file_open_tmp (template, &name_used, &error);
948 if (g_test_verbose())
950 if (fd != -1)
951 g_printerr ("g_file_open_tmp works even if template contains '%s'\n", G_DIR_SEPARATOR_S);
952 else
953 g_printerr ("g_file_open_tmp correctly returns error: %s\n", error->message);
955 if (fd != -1)
956 close (fd);
957 g_clear_error (&error);
958 g_free (name_used);
960 #ifdef G_OS_WIN32
961 name_used = NULL;
962 strcpy (template, "zap/barXXXXXX");
963 fd = g_file_open_tmp (template, &name_used, &error);
964 if (g_test_verbose())
966 if (fd != -1)
967 g_printerr ("g_file_open_tmp works even if template contains '/'\n");
968 else
969 g_printerr ("g_file_open_tmp correctly returns error: %s\n", error->message);
971 if (fd != -1)
972 close (fd);
973 g_clear_error (&error);
974 g_free (name_used);
975 #endif
977 strcpy (template, "zapXXXXXX");
978 name_used = NULL;
979 fd = g_file_open_tmp (template, &name_used, &error);
980 if (fd == -1)
981 g_error ("g_file_open_tmp didn't work for template '%s': %s\n", template, error->message);
982 else if (g_test_verbose())
983 g_printerr ("g_file_open_tmp for template '%s' used name '%s'\n", template, name_used);
984 if (fd != -1)
985 close (fd);
986 g_clear_error (&error);
987 remove (name_used);
988 g_free (name_used);
990 name_used = NULL;
991 fd = g_file_open_tmp (NULL, &name_used, &error);
992 if (fd == -1)
993 g_error ("g_file_open_tmp didn't work for a NULL template: %s\n", error->message);
994 else
995 close (fd);
996 g_clear_error (&error);
997 remove (name_used);
998 g_free (name_used);
1001 static void
1002 test_arrays (void)
1004 GByteArray *gbarray;
1005 GPtrArray *gparray;
1006 GArray *garray;
1007 guint i;
1009 gparray = g_ptr_array_new ();
1010 for (i = 0; i < 10000; i++)
1011 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1012 for (i = 0; i < 10000; i++)
1013 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1014 g_error ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1015 g_ptr_array_free (gparray, TRUE);
1017 gbarray = g_byte_array_new ();
1018 for (i = 0; i < 10000; i++)
1019 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1020 for (i = 0; i < 10000; i++)
1022 g_assert (gbarray->data[4*i] == 'a');
1023 g_assert (gbarray->data[4*i+1] == 'b');
1024 g_assert (gbarray->data[4*i+2] == 'c');
1025 g_assert (gbarray->data[4*i+3] == 'd');
1027 g_byte_array_free (gbarray, TRUE);
1029 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1030 for (i = 0; i < 10000; i++)
1031 g_array_append_val (garray, i);
1032 for (i = 0; i < 10000; i++)
1033 if (g_array_index (garray, gint, i) != i)
1034 g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), i);
1035 g_array_free (garray, TRUE);
1037 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1038 for (i = 0; i < 100; i++)
1039 g_array_prepend_val (garray, i);
1040 for (i = 0; i < 100; i++)
1041 if (g_array_index (garray, gint, i) != (100 - i - 1))
1042 g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
1043 g_array_free (garray, TRUE);
1046 static void
1047 hash_table_tests (void)
1049 GHashTable *hash_table;
1050 int array[10000];
1051 gint *pvalue = NULL;
1052 gint value = 120;
1053 guint i;
1055 hash_table = g_hash_table_new (my_hash, my_hash_equal);
1056 for (i = 0; i < 10000; i++)
1058 array[i] = i;
1059 g_hash_table_insert (hash_table, &array[i], &array[i]);
1061 pvalue = g_hash_table_find (hash_table, find_first_that, &value);
1062 if (*pvalue != value)
1063 g_error ("g_hash_table_find failed");
1064 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
1065 for (i = 0; i < 10000; i++)
1066 if (array[i] == 0)
1067 g_error ("hashtable-test: wrong value: %d\n", i);
1068 for (i = 0; i < 10000; i++)
1069 g_hash_table_remove (hash_table, &array[i]);
1070 for (i = 0; i < 10000; i++)
1072 array[i] = i;
1073 g_hash_table_insert (hash_table, &array[i], &array[i]);
1075 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
1076 g_hash_table_size (hash_table) != 5000)
1077 g_error ("hashtable removal failed\n");
1078 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
1079 g_hash_table_destroy (hash_table);
1082 #ifndef G_DISABLE_DEPRECATED
1083 static void
1084 relation_test (void)
1086 GRelation *relation = g_relation_new (2);
1087 GTuples *tuples;
1088 gint data [1024];
1089 guint i;
1091 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1092 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1094 for (i = 0; i < 1024; i += 1)
1095 data[i] = i;
1097 for (i = 1; i < 1023; i += 1)
1099 g_relation_insert (relation, data + i, data + i + 1);
1100 g_relation_insert (relation, data + i, data + i - 1);
1103 for (i = 2; i < 1022; i += 1)
1105 g_assert (! g_relation_exists (relation, data + i, data + i));
1106 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1107 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1110 for (i = 1; i < 1023; i += 1)
1112 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1113 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1116 for (i = 2; i < 1022; i += 1)
1118 g_assert (g_relation_count (relation, data + i, 0) == 2);
1119 g_assert (g_relation_count (relation, data + i, 1) == 2);
1122 g_assert (g_relation_count (relation, data, 0) == 0);
1124 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1125 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1126 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1127 g_relation_delete (relation, data + 42, 0);
1128 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1129 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1130 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1132 tuples = g_relation_select (relation, data + 200, 0);
1134 g_assert (tuples->len == 2);
1136 #if 0
1137 for (i = 0; i < tuples->len; i += 1)
1139 printf ("%d %d\n",
1140 *(gint*) g_tuples_index (tuples, i, 0),
1141 *(gint*) g_tuples_index (tuples, i, 1));
1143 #endif
1145 g_assert (g_relation_exists (relation, data + 300, data + 301));
1146 g_relation_delete (relation, data + 300, 0);
1147 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1149 g_tuples_destroy (tuples);
1151 g_relation_destroy (relation);
1153 relation = NULL;
1155 #endif
1157 static void
1158 gstring_tests (void)
1160 GString *string1, *string2;
1161 guint i;
1163 if (g_test_verbose())
1164 g_printerr ("test GString basics\n");
1166 string1 = g_string_new ("hi pete!");
1167 string2 = g_string_new ("");
1169 g_assert (strcmp ("hi pete!", string1->str) == 0);
1171 for (i = 0; i < 10000; i++)
1172 g_string_append_c (string1, 'a'+(i%26));
1174 #ifndef G_OS_WIN32
1175 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
1176 the %10000.10000f format... */
1177 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
1178 "this pete guy sure is a wuss, like he's the number ",
1180 " wuss. everyone agrees.\n",
1181 string1->str,
1182 10, 666, 15, 15, 666.666666666, 666.666666666);
1183 #else
1184 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
1185 "this pete guy sure is a wuss, like he's the number ",
1187 " wuss. everyone agrees.\n",
1188 string1->str,
1189 10, 666, 15, 15, 666.666666666, 666.666666666);
1190 #endif
1192 if (g_test_verbose())
1193 g_printerr ("string2 length = %lu...\n", (gulong)string2->len);
1194 string2->str[70] = '\0';
1195 if (g_test_verbose())
1196 g_printerr ("first 70 chars:\n%s\n", string2->str);
1197 string2->str[141] = '\0';
1198 if (g_test_verbose())
1199 g_printerr ("next 70 chars:\n%s\n", string2->str+71);
1200 string2->str[212] = '\0';
1201 if (g_test_verbose())
1202 g_printerr ("and next 70:\n%s\n", string2->str+142);
1203 if (g_test_verbose())
1204 g_printerr ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
1206 g_string_free (string1, TRUE);
1207 g_string_free (string2, TRUE);
1209 /* append */
1210 string1 = g_string_new ("firsthalf");
1211 g_string_append (string1, "lasthalf");
1212 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1213 g_string_free (string1, TRUE);
1215 /* append_len */
1216 string1 = g_string_new ("firsthalf");
1217 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
1218 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1219 g_string_free (string1, TRUE);
1221 /* prepend */
1222 string1 = g_string_new ("lasthalf");
1223 g_string_prepend (string1, "firsthalf");
1224 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1225 g_string_free (string1, TRUE);
1227 /* prepend_len */
1228 string1 = g_string_new ("lasthalf");
1229 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
1230 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1231 g_string_free (string1, TRUE);
1233 /* insert */
1234 string1 = g_string_new ("firstlast");
1235 g_string_insert (string1, 5, "middle");
1236 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1237 g_string_free (string1, TRUE);
1239 /* insert with pos == end of the string */
1240 string1 = g_string_new ("firstmiddle");
1241 g_string_insert (string1, strlen ("firstmiddle"), "last");
1242 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1243 g_string_free (string1, TRUE);
1245 /* insert_len */
1246 string1 = g_string_new ("firstlast");
1247 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
1248 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1249 g_string_free (string1, TRUE);
1251 /* insert_len with magic -1 pos for append */
1252 string1 = g_string_new ("first");
1253 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
1254 g_assert (strcmp (string1->str, "firstlast") == 0);
1255 g_string_free (string1, TRUE);
1257 /* insert_len with magic -1 len for strlen-the-string */
1258 string1 = g_string_new ("first");
1259 g_string_insert_len (string1, 5, "last", -1);
1260 g_assert (strcmp (string1->str, "firstlast") == 0);
1261 g_string_free (string1, TRUE);
1263 /* g_string_equal */
1264 string1 = g_string_new ("test");
1265 string2 = g_string_new ("te");
1266 g_assert (! g_string_equal(string1, string2));
1267 g_string_append (string2, "st");
1268 g_assert (g_string_equal(string1, string2));
1269 g_string_free (string1, TRUE);
1270 g_string_free (string2, TRUE);
1272 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
1273 if (g_test_verbose())
1274 g_printerr ("test embedded ASCII 0 (NUL) characters in GString\n");
1275 string1 = g_string_new ("fiddle");
1276 string2 = g_string_new ("fiddle");
1277 g_assert (g_string_equal(string1, string2));
1278 g_string_append_c(string1, '\0');
1279 g_assert (! g_string_equal(string1, string2));
1280 g_string_append_c(string2, '\0');
1281 g_assert (g_string_equal(string1, string2));
1282 g_string_append_c(string1, 'x');
1283 g_string_append_c(string2, 'y');
1284 g_assert (! g_string_equal(string1, string2));
1285 g_assert (string1->len == 8);
1286 g_string_append(string1, "yzzy");
1287 g_assert (string1->len == 12);
1288 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
1289 g_string_insert(string1, 1, "QED");
1290 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
1291 g_string_free (string1, TRUE);
1292 g_string_free (string2, TRUE);
1295 static void
1296 various_string_tests (void)
1298 GStringChunk *string_chunk;
1299 GTimeVal ref_date, date;
1300 gchar *tmp_string = NULL, *tmp_string_2, *string, *date_str;
1301 guint i;
1302 const gchar *tz;
1304 if (g_test_verbose())
1305 g_printerr ("checking string chunks...");
1306 string_chunk = g_string_chunk_new (1024);
1307 for (i = 0; i < 100000; i ++)
1309 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
1310 if (strcmp ("hi pete", tmp_string) != 0)
1311 g_error ("string chunks are broken.\n");
1313 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
1314 g_assert (tmp_string_2 != tmp_string && strcmp (tmp_string_2, tmp_string) == 0);
1315 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
1316 g_assert (tmp_string_2 == tmp_string);
1317 g_string_chunk_free (string_chunk);
1319 if (g_test_verbose())
1320 g_printerr ("test positional printf formats (not supported):");
1321 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
1322 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
1323 if (g_test_verbose())
1324 g_printerr ("%s%s\n", string, tmp_string);
1325 g_free (tmp_string);
1326 g_free (string);
1328 #define REF_INVALID1 "Wed Dec 19 17:20:20 GMT 2007"
1329 #define REF_INVALID2 "1980-02-22T10:36:00Zulu"
1330 #define REF_INVALID3 "1980-02-22T"
1331 #define REF_SEC_UTC 320063760
1332 #define REF_STR_UTC "1980-02-22T10:36:00Z"
1333 #define REF_STR_LOCAL "1980-02-22T13:36:00"
1334 #define REF_STR_CEST "1980-02-22T12:36:00+02:00"
1335 #define REF_STR_EST "19800222T053600-0500"
1336 #define REF_STR_NST "1980-02-22T07:06:00-03:30"
1337 #define REF_USEC_UTC 50000
1338 #define REF_STR_USEC_UTC "1980-02-22T10:36:00.050000Z"
1339 #define REF_STR_USEC_CEST "19800222T123600.050000000+0200"
1340 #define REF_STR_USEC_EST "1980-02-22T05:36:00,05-05:00"
1341 #define REF_STR_USEC_NST "19800222T070600,0500-0330"
1342 #define REF_STR_DATE_ONLY "1980-02-22"
1344 if (g_test_verbose())
1345 g_printerr ("checking g_time_val_from_iso8601...\n");
1346 ref_date.tv_sec = REF_SEC_UTC;
1347 ref_date.tv_usec = 0;
1348 g_assert (g_time_val_from_iso8601 (REF_INVALID1, &date) == FALSE);
1349 g_assert (g_time_val_from_iso8601 (REF_INVALID2, &date) == FALSE);
1350 g_assert (g_time_val_from_iso8601 (REF_INVALID3, &date) == FALSE);
1351 g_assert (g_time_val_from_iso8601 (REF_STR_DATE_ONLY, &date) == FALSE);
1352 g_assert (g_time_val_from_iso8601 (REF_STR_UTC, &date) != FALSE);
1353 if (g_test_verbose())
1354 g_printerr ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1355 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1356 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1357 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1359 /* predefine time zone */
1360 tz = g_getenv("TZ");
1361 g_setenv("TZ", "UTC-03:00", 1);
1362 tzset();
1364 g_assert (g_time_val_from_iso8601 (REF_STR_LOCAL, &date) != FALSE);
1365 if (g_test_verbose())
1366 g_printerr ("\t=> LOCAL 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 /* revert back user defined time zone */
1372 if (tz)
1373 g_setenv("TZ", tz, TRUE);
1374 else
1375 g_unsetenv("TZ");
1376 tzset();
1378 g_assert (g_time_val_from_iso8601 (REF_STR_CEST, &date) != FALSE);
1379 if (g_test_verbose())
1380 g_printerr ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1381 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1382 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1383 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1385 g_assert (g_time_val_from_iso8601 (REF_STR_EST, &date) != FALSE);
1386 if (g_test_verbose())
1387 g_printerr ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1388 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1389 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1390 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1392 g_assert (g_time_val_from_iso8601 (REF_STR_NST, &date) != FALSE);
1393 if (g_test_verbose())
1394 g_printerr ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1395 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1396 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1397 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1399 ref_date.tv_usec = REF_USEC_UTC;
1400 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_UTC, &date) != FALSE);
1401 if (g_test_verbose())
1402 g_printerr ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1403 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1404 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1405 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1407 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_CEST, &date) != FALSE);
1408 if (g_test_verbose())
1409 g_printerr ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1410 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1411 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1412 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1414 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_EST, &date) != FALSE);
1415 if (g_test_verbose())
1416 g_printerr ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1417 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1418 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1419 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1421 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_NST, &date) != FALSE);
1422 if (g_test_verbose())
1423 g_printerr ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1424 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1425 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1426 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1428 if (g_test_verbose())
1429 g_printerr ("checking g_time_val_to_iso8601...\n");
1430 ref_date.tv_sec = REF_SEC_UTC;
1431 ref_date.tv_usec = 0;
1432 date_str = g_time_val_to_iso8601 (&ref_date);
1433 g_assert (date_str != NULL);
1434 if (g_test_verbose())
1435 g_printerr ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_UTC);
1436 g_assert (strcmp (date_str, REF_STR_UTC) == 0);
1437 g_free (date_str);
1439 ref_date.tv_usec = REF_USEC_UTC;
1440 date_str = g_time_val_to_iso8601 (&ref_date);
1441 g_assert (date_str != NULL);
1442 if (g_test_verbose())
1443 g_printerr ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_USEC_UTC);
1444 g_assert (strcmp (date_str, REF_STR_USEC_UTC) == 0);
1445 g_free (date_str);
1447 if (g_test_verbose())
1448 g_printerr ("checking g_ascii_strcasecmp...");
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", "FROBOZZ") == 0);
1452 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1453 g_assert (g_ascii_strcasecmp ("", "") == 0);
1454 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 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 ("A", "B") < 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);
1462 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1464 if (g_test_verbose())
1465 g_printerr ("checking g_strdup...\n");
1466 g_assert (g_strdup (NULL) == NULL);
1467 string = g_strdup (GLIB_TEST_STRING);
1468 g_assert (string != NULL);
1469 g_assert (strcmp(string, GLIB_TEST_STRING) == 0);
1470 g_free (string);
1472 if (g_test_verbose())
1473 g_printerr ("checking g_strconcat...\n");
1474 string = g_strconcat (GLIB_TEST_STRING, NULL);
1475 g_assert (string != NULL);
1476 g_assert (strcmp (string, GLIB_TEST_STRING) == 0);
1477 g_free (string);
1478 string = g_strconcat (GLIB_TEST_STRING, GLIB_TEST_STRING,
1479 GLIB_TEST_STRING, NULL);
1480 g_assert (string != NULL);
1481 g_assert (strcmp (string, GLIB_TEST_STRING GLIB_TEST_STRING
1482 GLIB_TEST_STRING) == 0);
1483 g_free (string);
1485 if (g_test_verbose())
1486 g_printerr ("checking g_strlcpy/g_strlcat...");
1487 /* The following is a torture test for strlcpy/strlcat, with lots of
1488 * checking; normal users wouldn't use them this way!
1490 string = g_malloc (6);
1491 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
1492 *string = 'q';
1493 g_assert (g_strlcpy(string, "" , 5) == 0);
1494 g_assert ( *string == '\0' );
1495 *string = 'q';
1496 g_assert (g_strlcpy(string, "abc" , 5) == 3);
1497 g_assert ( *(string + 3) == '\0' );
1498 g_assert (g_str_equal(string, "abc"));
1499 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1500 g_assert ( *(string + 4) == '\0' );
1501 g_assert ( *(string + 5) == 'Z' );
1502 g_assert (g_str_equal(string, "abcd"));
1503 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1504 g_assert ( *(string + 4) == '\0' );
1505 g_assert ( *(string + 5) == 'Z' );
1506 g_assert (g_str_equal(string, "abcd"));
1507 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1508 g_assert ( *(string + 4) == '\0' );
1509 g_assert ( *(string + 5) == 'Z' );
1510 g_assert (g_str_equal(string, "abcd"));
1511 *string = 'Y';
1512 *(string + 1)= '\0';
1513 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1514 g_assert (*string == 'Y');
1515 *string = '\0';
1516 g_assert (g_strlcat(string, "123" , 5) == 3);
1517 g_assert ( *(string + 3) == '\0' );
1518 g_assert (g_str_equal(string, "123"));
1519 g_assert (g_strlcat(string, "" , 5) == 3);
1520 g_assert ( *(string + 3) == '\0' );
1521 g_assert (g_str_equal(string, "123"));
1522 g_assert (g_strlcat(string, "4", 5) == 4);
1523 g_assert (g_str_equal(string, "1234"));
1524 g_assert (g_strlcat(string, "5", 5) == 5);
1525 g_assert ( *(string + 4) == '\0' );
1526 g_assert (g_str_equal(string, "1234"));
1527 g_assert ( *(string + 5) == 'Z' );
1528 *string = 'Y';
1529 *(string + 1)= '\0';
1530 g_assert (g_strlcat(string, "123" , 0) == 3);
1531 g_assert (*string == 'Y');
1533 /* A few more tests, demonstrating more "normal" use */
1534 g_assert (g_strlcpy(string, "hi", 5) == 2);
1535 g_assert (g_str_equal(string, "hi"));
1536 g_assert (g_strlcat(string, "t", 5) == 3);
1537 g_assert (g_str_equal(string, "hit"));
1538 g_free(string);
1540 if (g_test_verbose())
1541 g_printerr ("checking g_strdup_printf...\n");
1542 string = g_strdup_printf ("%05d %-5s", 21, "test");
1543 g_assert (string != NULL);
1544 g_assert (strcmp(string, "00021 test ") == 0);
1545 g_free (string);
1547 /* g_debug (argv[0]); */
1550 #ifndef G_DISABLE_DEPRECATED
1551 static void
1552 test_mem_chunks (void)
1554 GMemChunk *mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
1555 gchar *mem[10000];
1556 guint i;
1557 for (i = 0; i < 10000; i++)
1559 guint j;
1560 mem[i] = g_chunk_new (gchar, mem_chunk);
1561 for (j = 0; j < 50; j++)
1562 mem[i][j] = i * j;
1564 for (i = 0; i < 10000; i++)
1565 g_mem_chunk_free (mem_chunk, mem[i]);
1567 g_mem_chunk_destroy (mem_chunk);
1569 #endif
1572 main (int argc,
1573 char *argv[])
1575 g_test_init (&argc, &argv, NULL);
1577 g_test_add_func ("/testglib/Infos", test_info);
1578 g_test_add_func ("/testglib/Types Sizes", type_sizes);
1579 g_test_add_func ("/testglib/GStrings", gstring_tests);
1580 g_test_add_func ("/testglib/Various Strings", various_string_tests);
1581 g_test_add_func ("/testglib/GList", glist_test);
1582 g_test_add_func ("/testglib/GSList", gslist_test);
1583 g_test_add_func ("/testglib/GNode", gnode_test);
1584 g_test_add_func ("/testglib/GTree", binary_tree_test);
1585 g_test_add_func ("/testglib/Arrays", test_arrays);
1586 g_test_add_func ("/testglib/GHashTable", hash_table_tests);
1587 #ifndef G_DISABLE_DEPRECATED
1588 g_test_add_func ("/testglib/Relation (deprecated)", relation_test);
1589 #endif
1590 g_test_add_func ("/testglib/File Paths", test_paths);
1591 g_test_add_func ("/testglib/File Functions", test_file_functions);
1592 g_test_add_func ("/testglib/Parse Debug Strings", test_g_parse_debug_string);
1593 #ifndef G_DISABLE_DEPRECATED
1594 g_test_add_func ("/testglib/GMemChunk (deprecated)", test_mem_chunks);
1595 #endif
1596 g_test_add_func ("/testglib/Warnings & Errors", log_warning_error_tests);
1597 g_test_add_func ("/testglib/Timers (slow)", timer_tests);
1599 return g_test_run();