Whitespace fixes
[glib.git] / tests / testglib.c
blob1a06d101cc83225b7a3d86b434bd4f887d4cc4a9
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 #include "config.h"
29 #undef GLIB_COMPILATION
31 #include <stdio.h>
32 #include <string.h>
33 #include <errno.h>
35 #include "glib.h"
36 #include <glib/gstdio.h>
38 #include <stdlib.h>
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
44 #ifdef G_OS_WIN32
45 #include <io.h> /* For read(), write() etc */
46 #endif
49 #define GLIB_TEST_STRING "el dorado "
50 #define GLIB_TEST_STRING_5 "el do"
53 /* --- variables --- */
54 static gint test_nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
55 static gint more_nums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
57 /* --- functions --- */
58 static gint
59 my_list_compare_one (gconstpointer a, gconstpointer b)
61 gint one = *((const gint*)a);
62 gint two = *((const gint*)b);
63 return one-two;
66 static gint
67 my_list_compare_two (gconstpointer a, gconstpointer b)
69 gint one = *((const gint*)a);
70 gint two = *((const gint*)b);
71 return two-one;
74 /* static void
75 my_list_print (gpointer a, gpointer b)
77 gint three = *((gint*)a);
78 g_print("%d", three);
79 }; */
81 static void
82 glist_test (void)
84 GList *list = NULL;
85 guint i;
87 for (i = 0; i < 10; i++)
88 list = g_list_append (list, &test_nums[i]);
89 list = g_list_reverse (list);
91 for (i = 0; i < 10; i++)
93 GList *t = g_list_nth (list, i);
94 if (*((gint*) t->data) != (9 - i))
95 g_error ("Regular insert failed");
98 for (i = 0; i < 10; i++)
99 if (g_list_position (list, g_list_nth (list, i)) != i)
100 g_error ("g_list_position does not seem to be the inverse of g_list_nth\n");
102 g_list_free (list);
103 list = NULL;
105 for (i = 0; i < 10; i++)
106 list = g_list_insert_sorted (list, &more_nums[i], my_list_compare_one);
109 g_print("\n");
110 g_list_foreach (list, my_list_print, NULL);
113 for (i = 0; i < 10; i++)
115 GList *t = g_list_nth (list, i);
116 if (*((gint*) t->data) != i)
117 g_error ("Sorted insert failed");
120 g_list_free (list);
121 list = NULL;
123 for (i = 0; i < 10; i++)
124 list = g_list_insert_sorted (list, &more_nums[i], my_list_compare_two);
127 g_print("\n");
128 g_list_foreach (list, my_list_print, NULL);
131 for (i = 0; i < 10; i++)
133 GList *t = g_list_nth (list, i);
134 if (*((gint*) t->data) != (9 - i))
135 g_error ("Sorted insert failed");
138 g_list_free (list);
139 list = NULL;
141 for (i = 0; i < 10; i++)
142 list = g_list_prepend (list, &more_nums[i]);
144 list = g_list_sort (list, my_list_compare_two);
147 g_print("\n");
148 g_list_foreach (list, my_list_print, NULL);
151 for (i = 0; i < 10; i++)
153 GList *t = g_list_nth (list, i);
154 if (*((gint*) t->data) != (9 - i))
155 g_error ("Merge sort failed");
158 g_list_free (list);
161 static void
162 gslist_test (void)
164 GSList *slist = NULL;
165 guint i;
167 for (i = 0; i < 10; i++)
168 slist = g_slist_append (slist, &test_nums[i]);
169 slist = g_slist_reverse (slist);
171 for (i = 0; i < 10; i++)
173 GSList *st = g_slist_nth (slist, i);
174 if (*((gint*) st->data) != (9 - i))
175 g_error ("failed");
178 g_slist_free (slist);
179 slist = NULL;
181 for (i = 0; i < 10; i++)
182 slist = g_slist_insert_sorted (slist, &more_nums[i], my_list_compare_one);
185 g_print("\n");
186 g_slist_foreach (slist, my_list_print, NULL);
189 for (i = 0; i < 10; i++)
191 GSList *st = g_slist_nth (slist, i);
192 if (*((gint*) st->data) != i)
193 g_error ("Sorted insert failed");
196 g_slist_free (slist);
197 slist = NULL;
199 for (i = 0; i < 10; i++)
200 slist = g_slist_insert_sorted (slist, &more_nums[i], my_list_compare_two);
203 g_print("\n");
204 g_slist_foreach (slist, my_list_print, NULL);
207 for (i = 0; i < 10; i++)
209 GSList *st = g_slist_nth (slist, i);
210 if (*((gint*) st->data) != (9 - i))
211 g_error("Sorted insert failed");
214 g_slist_free(slist);
215 slist = NULL;
217 for (i = 0; i < 10; i++)
218 slist = g_slist_prepend (slist, &more_nums[i]);
220 slist = g_slist_sort (slist, my_list_compare_two);
223 g_print("\n");
224 g_slist_foreach (slist, my_list_print, NULL);
227 for (i = 0; i < 10; i++)
229 GSList *st = g_slist_nth (slist, i);
230 if (*((gint*) st->data) != (9 - i))
231 g_error("Sorted insert failed");
234 g_slist_free(slist);
237 static gboolean
238 node_build_string (GNode *node,
239 gpointer data)
241 gchar **p = data;
242 gchar *string;
243 gchar c[2] = "_";
245 c[0] = ((gchar) ((gintptr) (node->data)));
247 string = g_strconcat (*p ? *p : "", c, NULL);
248 g_free (*p);
249 *p = string;
251 return FALSE;
254 static void
255 gnode_test (void)
257 #define C2P(c) ((gpointer) ((long) (c)))
258 #define P2C(p) ((gchar) ((gintptr) (p)))
259 GNode *root;
260 GNode *node;
261 GNode *node_B;
262 GNode *node_F;
263 GNode *node_G;
264 GNode *node_J;
265 guint i;
266 gchar *tstring, *cstring;
268 root = g_node_new (C2P ('A'));
269 g_assert (g_node_depth (root) == 1 && g_node_max_height (root) == 1);
271 node_B = g_node_new (C2P ('B'));
272 g_node_append (root, node_B);
273 g_assert (root->children == node_B);
275 g_node_append_data (node_B, C2P ('E'));
276 g_node_prepend_data (node_B, C2P ('C'));
277 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
279 node_F = g_node_new (C2P ('F'));
280 g_node_append (root, node_F);
281 g_assert (root->children->next == node_F);
283 node_G = g_node_new (C2P ('G'));
284 g_node_append (node_F, node_G);
285 node_J = g_node_new (C2P ('J'));
286 g_node_prepend (node_G, node_J);
287 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
288 g_node_insert_data (node_G, 0, C2P ('H'));
289 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
291 g_assert (g_node_depth (root) == 1);
292 g_assert (g_node_max_height (root) == 4);
293 g_assert (g_node_depth (node_G->children->next) == 4);
294 g_assert (g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
295 g_assert (g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
296 g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
297 g_assert (g_node_max_height (node_F) == 3);
298 g_assert (g_node_n_children (node_G) == 4);
299 g_assert (g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
300 g_assert (g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
301 g_assert (g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
303 for (i = 0; i < g_node_n_children (node_B); i++)
305 node = g_node_nth_child (node_B, i);
306 g_assert (P2C (node->data) == ('C' + i));
309 for (i = 0; i < g_node_n_children (node_G); i++)
310 g_assert (g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
312 /* we have built: A
313 * / \
314 * B F
315 * / | \ \
316 * C D E G
317 * / /\ \
318 * H I J K
320 * for in-order traversal, 'G' is considered to be the "left"
321 * child of 'F', which will cause 'F' to be the last node visited.
324 tstring = NULL;
325 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
326 g_assert_cmpstr (tstring, ==, "ABCDEFGHIJK");
327 g_free (tstring); tstring = NULL;
328 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
329 g_assert_cmpstr (tstring, ==, "CDEBHIJKGFA");
330 g_free (tstring); tstring = NULL;
331 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
332 g_assert_cmpstr (tstring, ==, "CBDEAHGIJKF");
333 g_free (tstring); tstring = NULL;
334 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
335 g_assert_cmpstr (tstring, ==, "ABFCDEGHIJK");
336 g_free (tstring); tstring = NULL;
338 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
339 g_assert_cmpstr (tstring, ==, "CDEHIJK");
340 g_free (tstring); tstring = NULL;
341 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
342 g_assert_cmpstr (tstring, ==, "ABFG");
343 g_free (tstring); tstring = NULL;
345 g_node_reverse_children (node_B);
346 g_node_reverse_children (node_G);
348 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
349 g_assert_cmpstr (tstring, ==, "ABFEDCGKJIH");
350 g_free (tstring); tstring = NULL;
352 cstring = NULL;
353 node = g_node_copy (root);
354 g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
355 g_assert (g_node_max_height (root) == g_node_max_height (node));
356 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
357 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
358 g_assert_cmpstr (tstring, ==, cstring);
359 g_free (tstring); tstring = NULL;
360 g_free (cstring); cstring = NULL;
361 g_node_destroy (node);
363 g_node_destroy (root);
365 /* allocation tests */
367 root = g_node_new (NULL);
368 node = root;
370 for (i = 0; i < 2048; i++)
372 g_node_append (node, g_node_new (NULL));
373 if ((i%5) == 4)
374 node = node->children->next;
376 g_assert (g_node_max_height (root) > 100);
377 g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
379 g_node_destroy (root);
380 #undef C2P
381 #undef P2C
384 static gint
385 my_compare (gconstpointer a,
386 gconstpointer b)
388 const char *cha = a;
389 const char *chb = b;
391 return *cha - *chb;
394 static gint
395 my_traverse (gpointer key,
396 gpointer value,
397 gpointer data)
399 char *ch = key;
400 g_print ("%c ", *ch);
401 return FALSE;
404 static void
405 binary_tree_test (void)
407 GTree *tree;
408 char chars[62];
409 guint i, j;
411 tree = g_tree_new (my_compare);
412 i = 0;
413 for (j = 0; j < 10; j++, i++)
415 chars[i] = '0' + j;
416 g_tree_insert (tree, &chars[i], &chars[i]);
418 for (j = 0; j < 26; j++, i++)
420 chars[i] = 'A' + j;
421 g_tree_insert (tree, &chars[i], &chars[i]);
423 for (j = 0; j < 26; j++, i++)
425 chars[i] = 'a' + j;
426 g_tree_insert (tree, &chars[i], &chars[i]);
429 g_assert_cmpint (g_tree_nnodes (tree), ==, 10 + 26 + 26);
430 g_assert_cmpint (g_tree_height (tree), ==, 6);
432 if (g_test_verbose())
434 g_print ("tree: ");
435 g_tree_foreach (tree, my_traverse, NULL);
436 g_print ("\n");
439 for (i = 0; i < 10; i++)
440 g_tree_remove (tree, &chars[i]);
442 g_assert_cmpint (g_tree_nnodes (tree), ==, 26 + 26);
443 g_assert_cmpint (g_tree_height (tree), ==, 6);
445 if (g_test_verbose())
447 g_print ("tree: ");
448 g_tree_foreach (tree, my_traverse, NULL);
449 g_print ("\n");
453 static gboolean
454 my_hash_callback_remove (gpointer key,
455 gpointer value,
456 gpointer user_data)
458 int *d = value;
460 if ((*d) % 2)
461 return TRUE;
463 return FALSE;
466 static void
467 my_hash_callback_remove_test (gpointer key,
468 gpointer value,
469 gpointer user_data)
471 int *d = value;
473 if ((*d) % 2)
474 g_print ("bad!\n");
477 static void
478 my_hash_callback (gpointer key,
479 gpointer value,
480 gpointer user_data)
482 int *d = value;
483 *d = 1;
486 static guint
487 my_hash (gconstpointer key)
489 return (guint) *((const gint*) key);
492 static gboolean
493 my_hash_equal (gconstpointer a,
494 gconstpointer b)
496 return *((const gint*) a) == *((const gint*) b);
499 static gboolean
500 find_first_that(gpointer key,
501 gpointer value,
502 gpointer user_data)
504 gint *v = value;
505 gint *test = user_data;
506 return (*v == *test);
509 static void
510 test_g_parse_debug_string (void)
512 GDebugKey keys[] = {
513 { "foo", 1 },
514 { "bar", 2 },
515 { "baz", 4 },
516 { "weird", 8 },
518 guint n_keys = G_N_ELEMENTS (keys);
519 guint result;
521 result = g_parse_debug_string ("bar:foo:blubb", keys, n_keys);
522 g_assert (result == 3);
524 result = g_parse_debug_string (":baz::_E@~!_::", keys, n_keys);
525 g_assert (result == 4);
527 result = g_parse_debug_string ("", keys, n_keys);
528 g_assert (result == 0);
530 result = g_parse_debug_string (" : ", keys, n_keys);
531 g_assert (result == 0);
533 result = g_parse_debug_string ("all", keys, n_keys);
534 g_assert_cmpuint (result, ==, (1 << n_keys) - 1);
536 /* Test subtracting debug flags from "all" */
537 result = g_parse_debug_string ("all:foo", keys, n_keys);
538 g_assert_cmpuint (result, ==, 2 | 4 | 8);
540 result = g_parse_debug_string ("foo baz,all", keys, n_keys);
541 g_assert_cmpuint (result, ==, 2 | 8);
543 result = g_parse_debug_string ("all,fooo,baz", keys, n_keys);
544 g_assert_cmpuint (result, ==, 1 | 2 | 8);
546 result = g_parse_debug_string ("all:weird", keys, n_keys);
547 g_assert_cmpuint (result, ==, 1 | 2 | 4);
550 static void
551 log_warning_error_tests (void)
553 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
555 g_message ("this is a g_message test.");
556 g_message ("non-printable UTF-8: \"\xc3\xa4\xda\x85\"");
557 g_message ("unsafe chars: \"\x10\x11\x12\n\t\x7f\x81\x82\x83\"");
558 exit (0);
560 g_test_trap_assert_passed();
561 g_test_trap_assert_stderr ("*is a g_message test*");
562 g_test_trap_assert_stderr ("*non-printable UTF-8*");
563 g_test_trap_assert_stderr ("*unsafe chars*");
564 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
566 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
567 exit (0);
569 g_test_trap_assert_failed(); /* we have fatal-warnings enabled */
570 g_test_trap_assert_stderr ("*harmless warning*");
571 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
573 g_print (NULL);
574 exit (0);
576 g_test_trap_assert_failed(); /* we have fatal-warnings enabled */
577 g_test_trap_assert_stderr ("*g_print*assertion*failed*");
578 g_test_trap_assert_stderr ("*NULL*");
581 static void
582 timer_tests (void)
584 GTimer *timer, *timer2;
585 gdouble elapsed;
587 /* basic testing */
588 timer = g_timer_new ();
589 g_timer_start (timer);
590 elapsed = g_timer_elapsed (timer, NULL);
591 g_timer_stop (timer);
592 g_assert_cmpfloat (elapsed, <=, g_timer_elapsed (timer, NULL));
593 g_timer_destroy (timer);
595 if (g_test_slow())
597 if (g_test_verbose())
598 g_print ("checking timers...\n");
599 timer = g_timer_new ();
600 if (g_test_verbose())
601 g_print (" spinning for 3 seconds...\n");
602 g_timer_start (timer);
603 while (g_timer_elapsed (timer, NULL) < 3)
605 g_timer_stop (timer);
606 g_timer_destroy (timer);
607 if (g_test_verbose())
608 g_print ("ok\n");
611 if (g_test_slow())
613 gulong elapsed_usecs;
614 if (g_test_verbose())
615 g_print ("checking g_timer_continue...\n");
616 timer2 = g_timer_new ();
617 if (g_test_verbose())
618 g_print ("\trun for 1 second...\n");
619 timer = g_timer_new();
620 g_usleep (G_USEC_PER_SEC); /* run timer for 1 second */
621 g_timer_stop (timer);
622 if (g_test_verbose())
623 g_print ("\tstop for 1 second...\n");
624 g_usleep (G_USEC_PER_SEC); /* wait for 1 second */
625 if (g_test_verbose())
626 g_print ("\trun for 2 seconds...\n");
627 g_timer_continue (timer);
628 g_usleep (2 * G_USEC_PER_SEC); /* run timer for 2 seconds */
629 g_timer_stop(timer);
630 if (g_test_verbose())
631 g_print ("\tstop for 1.5 seconds...\n");
632 g_usleep ((3 * G_USEC_PER_SEC) / 2); /* wait for 1.5 seconds */
633 if (g_test_verbose())
634 g_print ("\trun for 0.2 seconds...\n");
635 g_timer_continue (timer);
636 g_usleep (G_USEC_PER_SEC / 5); /* run timer for 0.2 seconds */
637 g_timer_stop (timer);
638 if (g_test_verbose())
639 g_print ("\tstop for 4 seconds...\n");
640 g_usleep (4 * G_USEC_PER_SEC); /* wait for 4 seconds */
641 if (g_test_verbose())
642 g_print ("\trun for 5.8 seconds...\n");
643 g_timer_continue (timer);
644 g_usleep ((29 * G_USEC_PER_SEC) / 5); /* run timer for 5.8 seconds */
645 g_timer_stop(timer);
646 elapsed = g_timer_elapsed (timer, &elapsed_usecs);
647 if (g_test_verbose())
648 g_print ("\t=> timer = %.6f = %d.%06ld (should be: 9.000000) (%.6f off)\n", elapsed, (int) elapsed, elapsed_usecs, ABS (elapsed - 9.));
649 g_assert_cmpfloat (elapsed, >, 8.8);
650 g_assert_cmpfloat (elapsed, <, 9.2);
651 if (g_test_verbose())
652 g_print ("g_timer_continue ... ok\n\n");
653 g_timer_stop (timer2);
654 elapsed = g_timer_elapsed (timer2, &elapsed_usecs);
655 if (g_test_verbose())
656 g_print ("\t=> timer2 = %.6f = %d.%06ld (should be: %.6f) (%.6f off)\n\n", elapsed, (int) elapsed, elapsed_usecs, 9.+6.5, ABS (elapsed - (9.+6.5)));
657 g_assert_cmpfloat (elapsed, >, 8.8 + 6.5);
658 g_assert_cmpfloat (elapsed, <, 9.2 + 6.5);
659 if (g_test_verbose())
660 g_print ("timer2 ... ok\n\n");
661 g_timer_destroy (timer);
662 g_timer_destroy (timer2);
666 static void
667 type_sizes (void)
669 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
670 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
671 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
672 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
673 /* type sizes */
674 g_assert_cmpint (sizeof (gint8), ==, 1);
675 g_assert_cmpint (sizeof (gint16), ==, 2);
676 g_assert_cmpint (sizeof (gint32), ==, 4);
677 g_assert_cmpint (sizeof (gint64), ==, 8);
678 /* endian macros */
679 if (g_test_verbose())
680 g_print ("checking endian macros (host is %s)...\n",
681 G_BYTE_ORDER == G_BIG_ENDIAN ? "big endian" : "little endian");
682 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
683 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
684 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
687 static void
688 test_info (void)
690 const gchar *un, *rn, *hn;
691 const gchar *tmpdir, *homedir, *userdatadir, *uconfdir, *ucachedir;
692 const gchar *uddesktop, *udddocs, *uddpubshare, *uruntimedir;
693 gchar **sv, *cwd, *sdatadirs, *sconfdirs, *langnames;
694 const gchar *charset;
695 gboolean charset_is_utf8;
696 if (g_test_verbose())
697 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
698 glib_major_version,
699 glib_minor_version,
700 glib_micro_version,
701 glib_interface_age,
702 glib_binary_age);
704 cwd = g_get_current_dir ();
705 un = g_get_user_name();
706 rn = g_get_real_name();
707 hn = g_get_host_name();
708 if (g_test_verbose())
710 g_print ("cwd: %s\n", cwd);
711 g_print ("user: %s\n", un);
712 g_print ("real: %s\n", rn);
713 g_print ("host: %s\n", hn);
715 g_free (cwd);
717 /* reload, just for fun */
718 g_reload_user_special_dirs_cache ();
719 g_reload_user_special_dirs_cache ();
721 tmpdir = g_get_tmp_dir();
722 g_assert (tmpdir != NULL);
723 homedir = g_get_home_dir ();
724 g_assert (homedir != NULL);
725 userdatadir = g_get_user_data_dir ();
726 g_assert (userdatadir != NULL);
727 uconfdir = g_get_user_config_dir ();
728 g_assert (uconfdir != NULL);
729 ucachedir = g_get_user_cache_dir ();
730 g_assert (ucachedir != NULL);
732 uddesktop = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
733 g_assert (uddesktop != NULL);
734 udddocs = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
735 uddpubshare = g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE);
736 uruntimedir = g_get_user_runtime_dir ();
737 g_assert (uruntimedir != NULL);
739 sv = (gchar **) g_get_system_data_dirs ();
740 sdatadirs = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv);
741 sv = (gchar **) g_get_system_config_dirs ();
742 sconfdirs = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv);
743 sv = (gchar **) g_get_language_names ();
744 langnames = g_strjoinv (":", sv);
746 if (g_test_verbose())
748 g_print ("tmp-dir: %s\n", tmpdir);
749 g_print ("home: %s\n", homedir);
750 g_print ("user_data: %s\n", userdatadir);
751 g_print ("user_config: %s\n", uconfdir);
752 g_print ("user_cache: %s\n", ucachedir);
753 g_print ("user_runtime: %s\n", uruntimedir);
754 g_print ("system_data: %s\n", sdatadirs);
755 g_print ("system_config: %s\n", sconfdirs);
756 g_print ("languages: %s\n", langnames);
757 g_print ("user_special[DESKTOP]: %s\n", uddesktop);
758 g_print ("user_special[DOCUMENTS]: %s\n", udddocs);
759 g_print ("user_special[PUBLIC_SHARE]: %s\n", uddpubshare);
761 g_free (sdatadirs);
762 g_free (sconfdirs);
763 g_free (langnames);
765 charset_is_utf8 = g_get_charset ((const char**)&charset);
767 if (g_test_verbose())
769 if (charset_is_utf8)
770 g_print ("current charset is UTF-8: %s\n", charset);
771 else
772 g_print ("current charset is not UTF-8: %s\n", charset);
775 if (g_test_verbose())
777 #ifdef G_PLATFORM_WIN32
778 g_print ("current locale: %s\n", g_win32_getlocale ());
780 g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
781 g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
783 g_print ("a Win32 error message: %s\n", g_win32_error_message (2));
784 #endif
788 static void
789 test_paths (void)
791 struct {
792 gchar *filename;
793 gchar *dirname;
794 } dirname_checks[] = {
795 { "/", "/" },
796 { "////", "/" },
797 { ".////", "." },
798 { "../", ".." },
799 { "..////", ".." },
800 { "a/b", "a" },
801 { "a/b/", "a/b" },
802 { "c///", "c" },
803 #ifdef G_OS_WIN32
804 { "\\", "\\" },
805 { ".\\\\\\\\", "." },
806 { "..\\", ".." },
807 { "..\\\\\\\\", ".." },
808 { "a\\b", "a" },
809 { "a\\b/", "a\\b" },
810 { "a/b\\", "a/b" },
811 { "c\\\\/", "c" },
812 { "//\\", "/" },
813 #endif
814 #ifdef G_WITH_CYGWIN
815 { "//server/share///x", "//server/share" },
816 #endif
817 { ".", "." },
818 { "..", "." },
819 { "", "." },
821 const guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
822 struct {
823 gchar *filename;
824 gchar *without_root;
825 } skip_root_checks[] = {
826 { "/", "" },
827 { "//", "" },
828 { "/foo", "foo" },
829 { "//foo", "foo" },
830 { "a/b", NULL },
831 #ifdef G_OS_WIN32
832 { "\\", "" },
833 { "\\foo", "foo" },
834 { "\\\\server\\foo", "" },
835 { "\\\\server\\foo\\bar", "bar" },
836 { "a\\b", NULL },
837 #endif
838 #ifdef G_WITH_CYGWIN
839 { "//server/share///x", "//x" },
840 #endif
841 { ".", NULL },
842 { "", NULL },
844 const guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
845 gchar *string;
846 guint i;
847 if (g_test_verbose())
848 g_print ("checking g_path_get_basename()...");
849 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
850 g_assert (strcmp (string, "dir") == 0);
851 g_free (string);
852 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
853 g_assert (strcmp (string, "file") == 0);
854 g_free (string);
855 if (g_test_verbose())
856 g_print ("ok\n");
858 #ifdef G_OS_WIN32
859 string = g_path_get_basename ("/foo/dir/");
860 g_assert (strcmp (string, "dir") == 0);
861 g_free (string);
862 string = g_path_get_basename ("/foo/file");
863 g_assert (strcmp (string, "file") == 0);
864 g_free (string);
865 #endif
867 if (g_test_verbose())
868 g_print ("checking g_path_get_dirname()...");
869 for (i = 0; i < n_dirname_checks; i++)
871 gchar *dirname = g_path_get_dirname (dirname_checks[i].filename);
872 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
874 g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
875 dirname_checks[i].filename,
876 dirname_checks[i].dirname,
877 dirname);
879 g_free (dirname);
881 if (g_test_verbose())
882 g_print ("ok\n");
884 if (g_test_verbose())
885 g_print ("checking g_path_skip_root()...");
886 for (i = 0; i < n_skip_root_checks; i++)
888 const gchar *skipped = g_path_skip_root (skip_root_checks[i].filename);
889 if ((skipped && !skip_root_checks[i].without_root) ||
890 (!skipped && skip_root_checks[i].without_root) ||
891 ((skipped && skip_root_checks[i].without_root) &&
892 strcmp (skipped, skip_root_checks[i].without_root)))
894 g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
895 skip_root_checks[i].filename,
896 (skip_root_checks[i].without_root ?
897 skip_root_checks[i].without_root : "<NULL>"),
898 (skipped ? skipped : "<NULL>"));
901 if (g_test_verbose())
902 g_print ("ok\n");
905 static void
906 test_file_functions (void)
908 const char hello[] = "Hello, World";
909 const int hellolen = sizeof (hello) - 1;
910 GError *error;
911 char template[32];
912 char *name_used, chars[62];
913 gint fd, n;
915 strcpy (template, "foobar");
916 fd = g_mkstemp (template);
917 if (g_test_verbose() && fd != -1)
918 g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
919 if (fd != -1)
920 close (fd);
921 strcpy (template, "fooXXXXXX");
922 fd = g_mkstemp (template);
923 if (fd == -1)
924 g_error ("g_mkstemp didn't work for template %s\n", template);
925 n = write (fd, hello, hellolen);
926 if (n == -1)
927 g_error ("write() failed: %s\n", g_strerror (errno));
928 else if (n != hellolen)
929 g_error ("write() should have written %d bytes, wrote %d\n", hellolen, n);
931 lseek (fd, 0, 0);
932 n = read (fd, chars, sizeof (chars));
933 if (n == -1)
934 g_error ("read() failed: %s\n", g_strerror (errno));
935 else if (n != hellolen)
936 g_error ("read() should have read %d bytes, got %d\n", hellolen, n);
938 chars[n] = 0;
939 if (strcmp (chars, hello) != 0)
940 g_error ("wrote '%s', but got '%s'\n", hello, chars);
942 close (fd);
943 remove (template);
945 error = 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_print ("g_file_open_tmp works even if template contains '%s'\n", G_DIR_SEPARATOR_S);
952 else
953 g_print ("g_file_open_tmp correctly returns error: %s\n", error->message);
955 close (fd);
956 g_clear_error (&error);
958 #ifdef G_OS_WIN32
959 strcpy (template, "zap/barXXXXXX");
960 fd = g_file_open_tmp (template, &name_used, &error);
961 if (g_test_verbose())
963 if (fd != -1)
964 g_print ("g_file_open_tmp works even if template contains '/'\n");
965 else
966 g_print ("g_file_open_tmp correctly returns error: %s\n", error->message);
968 close (fd);
969 g_clear_error (&error);
970 #endif
972 strcpy (template, "zapXXXXXX");
973 fd = g_file_open_tmp (template, &name_used, &error);
974 if (fd == -1)
975 g_error ("g_file_open_tmp didn't work for template '%s': %s\n", template, error->message);
976 else if (g_test_verbose())
977 g_print ("g_file_open_tmp for template '%s' used name '%s'\n", template, name_used);
978 close (fd);
979 g_clear_error (&error);
980 remove (name_used);
982 fd = g_file_open_tmp (NULL, &name_used, &error);
983 if (fd == -1)
984 g_error ("g_file_open_tmp didn't work for a NULL template: %s\n", error->message);
985 close (fd);
986 g_clear_error (&error);
987 remove (name_used);
990 static void
991 test_arrays (void)
993 GByteArray *gbarray;
994 GPtrArray *gparray;
995 GArray *garray;
996 guint i;
998 gparray = g_ptr_array_new ();
999 for (i = 0; i < 10000; i++)
1000 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1001 for (i = 0; i < 10000; i++)
1002 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1003 g_error ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1004 g_ptr_array_free (gparray, TRUE);
1006 gbarray = g_byte_array_new ();
1007 for (i = 0; i < 10000; i++)
1008 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1009 for (i = 0; i < 10000; i++)
1011 g_assert (gbarray->data[4*i] == 'a');
1012 g_assert (gbarray->data[4*i+1] == 'b');
1013 g_assert (gbarray->data[4*i+2] == 'c');
1014 g_assert (gbarray->data[4*i+3] == 'd');
1016 g_byte_array_free (gbarray, TRUE);
1018 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1019 for (i = 0; i < 10000; i++)
1020 g_array_append_val (garray, i);
1021 for (i = 0; i < 10000; i++)
1022 if (g_array_index (garray, gint, i) != i)
1023 g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), i);
1024 g_array_free (garray, TRUE);
1026 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1027 for (i = 0; i < 100; i++)
1028 g_array_prepend_val (garray, i);
1029 for (i = 0; i < 100; i++)
1030 if (g_array_index (garray, gint, i) != (100 - i - 1))
1031 g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
1032 g_array_free (garray, TRUE);
1035 static void
1036 hash_table_tests (void)
1038 GHashTable *hash_table;
1039 int array[10000];
1040 gint *pvalue = NULL;
1041 gint value = 120;
1042 guint i;
1044 hash_table = g_hash_table_new (my_hash, my_hash_equal);
1045 for (i = 0; i < 10000; i++)
1047 array[i] = i;
1048 g_hash_table_insert (hash_table, &array[i], &array[i]);
1050 pvalue = g_hash_table_find (hash_table, find_first_that, &value);
1051 if (*pvalue != value)
1052 g_error ("g_hash_table_find failed");
1053 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
1054 for (i = 0; i < 10000; i++)
1055 if (array[i] == 0)
1056 g_error ("hashtable-test: wrong value: %d\n", i);
1057 for (i = 0; i < 10000; i++)
1058 g_hash_table_remove (hash_table, &array[i]);
1059 for (i = 0; i < 10000; i++)
1061 array[i] = i;
1062 g_hash_table_insert (hash_table, &array[i], &array[i]);
1064 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
1065 g_hash_table_size (hash_table) != 5000)
1066 g_error ("hashtable removal failed\n");
1067 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
1068 g_hash_table_destroy (hash_table);
1071 #ifndef G_DISABLE_DEPRECATED
1072 static void
1073 relation_test (void)
1075 GRelation *relation = g_relation_new (2);
1076 GTuples *tuples;
1077 gint data [1024];
1078 guint i;
1080 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1081 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1083 for (i = 0; i < 1024; i += 1)
1084 data[i] = i;
1086 for (i = 1; i < 1023; i += 1)
1088 g_relation_insert (relation, data + i, data + i + 1);
1089 g_relation_insert (relation, data + i, data + i - 1);
1092 for (i = 2; i < 1022; i += 1)
1094 g_assert (! g_relation_exists (relation, data + i, data + i));
1095 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1096 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1099 for (i = 1; i < 1023; i += 1)
1101 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1102 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1105 for (i = 2; i < 1022; i += 1)
1107 g_assert (g_relation_count (relation, data + i, 0) == 2);
1108 g_assert (g_relation_count (relation, data + i, 1) == 2);
1111 g_assert (g_relation_count (relation, data, 0) == 0);
1113 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1114 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1115 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1116 g_relation_delete (relation, data + 42, 0);
1117 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1118 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1119 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1121 tuples = g_relation_select (relation, data + 200, 0);
1123 g_assert (tuples->len == 2);
1125 #if 0
1126 for (i = 0; i < tuples->len; i += 1)
1128 printf ("%d %d\n",
1129 *(gint*) g_tuples_index (tuples, i, 0),
1130 *(gint*) g_tuples_index (tuples, i, 1));
1132 #endif
1134 g_assert (g_relation_exists (relation, data + 300, data + 301));
1135 g_relation_delete (relation, data + 300, 0);
1136 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1138 g_tuples_destroy (tuples);
1140 g_relation_destroy (relation);
1142 relation = NULL;
1144 #endif
1146 static void
1147 gstring_tests (void)
1149 GString *string1, *string2;
1150 guint i;
1152 if (g_test_verbose())
1153 g_print ("test GString basics\n");
1155 string1 = g_string_new ("hi pete!");
1156 string2 = g_string_new ("");
1158 g_assert (strcmp ("hi pete!", string1->str) == 0);
1160 for (i = 0; i < 10000; i++)
1161 g_string_append_c (string1, 'a'+(i%26));
1163 #ifndef G_OS_WIN32
1164 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
1165 the %10000.10000f format... */
1166 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
1167 "this pete guy sure is a wuss, like he's the number ",
1169 " wuss. everyone agrees.\n",
1170 string1->str,
1171 10, 666, 15, 15, 666.666666666, 666.666666666);
1172 #else
1173 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
1174 "this pete guy sure is a wuss, like he's the number ",
1176 " wuss. everyone agrees.\n",
1177 string1->str,
1178 10, 666, 15, 15, 666.666666666, 666.666666666);
1179 #endif
1181 if (g_test_verbose())
1182 g_print ("string2 length = %lu...\n", (gulong)string2->len);
1183 string2->str[70] = '\0';
1184 if (g_test_verbose())
1185 g_print ("first 70 chars:\n%s\n", string2->str);
1186 string2->str[141] = '\0';
1187 if (g_test_verbose())
1188 g_print ("next 70 chars:\n%s\n", string2->str+71);
1189 string2->str[212] = '\0';
1190 if (g_test_verbose())
1191 g_print ("and next 70:\n%s\n", string2->str+142);
1192 if (g_test_verbose())
1193 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
1195 g_string_free (string1, TRUE);
1196 g_string_free (string2, TRUE);
1198 /* append */
1199 string1 = g_string_new ("firsthalf");
1200 g_string_append (string1, "lasthalf");
1201 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1202 g_string_free (string1, TRUE);
1204 /* append_len */
1205 string1 = g_string_new ("firsthalf");
1206 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
1207 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1208 g_string_free (string1, TRUE);
1210 /* prepend */
1211 string1 = g_string_new ("lasthalf");
1212 g_string_prepend (string1, "firsthalf");
1213 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1214 g_string_free (string1, TRUE);
1216 /* prepend_len */
1217 string1 = g_string_new ("lasthalf");
1218 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
1219 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1220 g_string_free (string1, TRUE);
1222 /* insert */
1223 string1 = g_string_new ("firstlast");
1224 g_string_insert (string1, 5, "middle");
1225 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1226 g_string_free (string1, TRUE);
1228 /* insert with pos == end of the string */
1229 string1 = g_string_new ("firstmiddle");
1230 g_string_insert (string1, strlen ("firstmiddle"), "last");
1231 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1232 g_string_free (string1, TRUE);
1234 /* insert_len */
1235 string1 = g_string_new ("firstlast");
1236 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
1237 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1238 g_string_free (string1, TRUE);
1240 /* insert_len with magic -1 pos for append */
1241 string1 = g_string_new ("first");
1242 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
1243 g_assert (strcmp (string1->str, "firstlast") == 0);
1244 g_string_free (string1, TRUE);
1246 /* insert_len with magic -1 len for strlen-the-string */
1247 string1 = g_string_new ("first");
1248 g_string_insert_len (string1, 5, "last", -1);
1249 g_assert (strcmp (string1->str, "firstlast") == 0);
1250 g_string_free (string1, TRUE);
1252 /* g_string_equal */
1253 string1 = g_string_new ("test");
1254 string2 = g_string_new ("te");
1255 g_assert (! g_string_equal(string1, string2));
1256 g_string_append (string2, "st");
1257 g_assert (g_string_equal(string1, string2));
1258 g_string_free (string1, TRUE);
1259 g_string_free (string2, TRUE);
1261 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
1262 if (g_test_verbose())
1263 g_print ("test embedded ASCII 0 (NUL) characters in GString\n");
1264 string1 = g_string_new ("fiddle");
1265 string2 = g_string_new ("fiddle");
1266 g_assert (g_string_equal(string1, string2));
1267 g_string_append_c(string1, '\0');
1268 g_assert (! g_string_equal(string1, string2));
1269 g_string_append_c(string2, '\0');
1270 g_assert (g_string_equal(string1, string2));
1271 g_string_append_c(string1, 'x');
1272 g_string_append_c(string2, 'y');
1273 g_assert (! g_string_equal(string1, string2));
1274 g_assert (string1->len == 8);
1275 g_string_append(string1, "yzzy");
1276 g_assert (string1->len == 12);
1277 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
1278 g_string_insert(string1, 1, "QED");
1279 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
1280 g_string_free (string1, TRUE);
1281 g_string_free (string2, TRUE);
1284 static void
1285 various_string_tests (void)
1287 GStringChunk *string_chunk;
1288 GTimeVal ref_date, date;
1289 gchar *tmp_string = NULL, *tmp_string_2, *string, *date_str;
1290 guint i;
1291 const gchar *tz;
1293 if (g_test_verbose())
1294 g_print ("checking string chunks...");
1295 string_chunk = g_string_chunk_new (1024);
1296 for (i = 0; i < 100000; i ++)
1298 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
1299 if (strcmp ("hi pete", tmp_string) != 0)
1300 g_error ("string chunks are broken.\n");
1302 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
1303 g_assert (tmp_string_2 != tmp_string && strcmp (tmp_string_2, tmp_string) == 0);
1304 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
1305 g_assert (tmp_string_2 == tmp_string);
1306 g_string_chunk_free (string_chunk);
1308 if (g_test_verbose())
1309 g_print ("test positional printf formats (not supported):");
1310 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
1311 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
1312 if (g_test_verbose())
1313 g_print ("%s%s\n", string, tmp_string);
1314 g_free (tmp_string);
1315 g_free (string);
1317 #define REF_INVALID1 "Wed Dec 19 17:20:20 GMT 2007"
1318 #define REF_INVALID2 "1980-02-22T10:36:00Zulu"
1319 #define REF_INVALID3 "1980-02-22T"
1320 #define REF_SEC_UTC 320063760
1321 #define REF_STR_UTC "1980-02-22T10:36:00Z"
1322 #define REF_STR_LOCAL "1980-02-22T13:36:00"
1323 #define REF_STR_CEST "1980-02-22T12:36:00+02:00"
1324 #define REF_STR_EST "19800222T053600-0500"
1325 #define REF_STR_NST "1980-02-22T07:06:00-03:30"
1326 #define REF_USEC_UTC 50000
1327 #define REF_STR_USEC_UTC "1980-02-22T10:36:00.050000Z"
1328 #define REF_STR_USEC_CEST "19800222T123600.050000000+0200"
1329 #define REF_STR_USEC_EST "1980-02-22T05:36:00,05-05:00"
1330 #define REF_STR_USEC_NST "19800222T070600,0500-0330"
1331 #define REF_STR_DATE_ONLY "1980-02-22"
1333 if (g_test_verbose())
1334 g_print ("checking g_time_val_from_iso8601...\n");
1335 ref_date.tv_sec = REF_SEC_UTC;
1336 ref_date.tv_usec = 0;
1337 g_assert (g_time_val_from_iso8601 (REF_INVALID1, &date) == FALSE);
1338 g_assert (g_time_val_from_iso8601 (REF_INVALID2, &date) == FALSE);
1339 g_assert (g_time_val_from_iso8601 (REF_INVALID3, &date) == FALSE);
1340 g_assert (g_time_val_from_iso8601 (REF_STR_DATE_ONLY, &date) != FALSE);
1341 g_assert (g_time_val_from_iso8601 (REF_STR_UTC, &date) != FALSE);
1342 if (g_test_verbose())
1343 g_print ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1344 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1345 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1346 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1348 /* predefine time zone */
1349 tz = g_getenv("TZ");
1350 g_setenv("TZ", "UTC-03:00", 1);
1351 tzset();
1353 g_assert (g_time_val_from_iso8601 (REF_STR_LOCAL, &date) != FALSE);
1354 if (g_test_verbose())
1355 g_print ("\t=> LOCAL stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1356 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1357 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1358 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1360 /* revert back user defined time zone */
1361 if (tz)
1362 g_setenv("TZ", tz, TRUE);
1363 else
1364 g_unsetenv("TZ");
1365 tzset();
1367 g_assert (g_time_val_from_iso8601 (REF_STR_CEST, &date) != FALSE);
1368 if (g_test_verbose())
1369 g_print ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1370 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1371 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1372 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1374 g_assert (g_time_val_from_iso8601 (REF_STR_EST, &date) != FALSE);
1375 if (g_test_verbose())
1376 g_print ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1377 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1378 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1379 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1381 g_assert (g_time_val_from_iso8601 (REF_STR_NST, &date) != FALSE);
1382 if (g_test_verbose())
1383 g_print ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1384 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1385 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1386 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1388 ref_date.tv_usec = REF_USEC_UTC;
1389 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_UTC, &date) != FALSE);
1390 if (g_test_verbose())
1391 g_print ("\t=> UTC 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 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_CEST, &date) != FALSE);
1397 if (g_test_verbose())
1398 g_print ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1399 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1400 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1401 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1403 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_EST, &date) != FALSE);
1404 if (g_test_verbose())
1405 g_print ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1406 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1407 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1408 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1410 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_NST, &date) != FALSE);
1411 if (g_test_verbose())
1412 g_print ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1413 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1414 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1415 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1417 if (g_test_verbose())
1418 g_print ("checking g_time_val_to_iso8601...\n");
1419 ref_date.tv_sec = REF_SEC_UTC;
1420 ref_date.tv_usec = 0;
1421 date_str = g_time_val_to_iso8601 (&ref_date);
1422 g_assert (date_str != NULL);
1423 if (g_test_verbose())
1424 g_print ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_UTC);
1425 g_assert (strcmp (date_str, REF_STR_UTC) == 0);
1426 g_free (date_str);
1428 ref_date.tv_usec = REF_USEC_UTC;
1429 date_str = g_time_val_to_iso8601 (&ref_date);
1430 g_assert (date_str != NULL);
1431 if (g_test_verbose())
1432 g_print ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_USEC_UTC);
1433 g_assert (strcmp (date_str, REF_STR_USEC_UTC) == 0);
1434 g_free (date_str);
1436 if (g_test_verbose())
1437 g_print ("checking g_ascii_strcasecmp...");
1438 g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
1439 g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
1440 g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
1441 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1442 g_assert (g_ascii_strcasecmp ("", "") == 0);
1443 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
1444 g_assert (g_ascii_strcasecmp ("a", "b") < 0);
1445 g_assert (g_ascii_strcasecmp ("a", "B") < 0);
1446 g_assert (g_ascii_strcasecmp ("A", "b") < 0);
1447 g_assert (g_ascii_strcasecmp ("A", "B") < 0);
1448 g_assert (g_ascii_strcasecmp ("b", "a") > 0);
1449 g_assert (g_ascii_strcasecmp ("b", "A") > 0);
1450 g_assert (g_ascii_strcasecmp ("B", "a") > 0);
1451 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1453 if (g_test_verbose())
1454 g_print ("checking g_strdup...\n");
1455 g_assert (g_strdup (NULL) == NULL);
1456 string = g_strdup (GLIB_TEST_STRING);
1457 g_assert (string != NULL);
1458 g_assert (strcmp(string, GLIB_TEST_STRING) == 0);
1459 g_free (string);
1461 if (g_test_verbose())
1462 g_print ("checking g_strconcat...\n");
1463 string = g_strconcat (GLIB_TEST_STRING, NULL);
1464 g_assert (string != NULL);
1465 g_assert (strcmp (string, GLIB_TEST_STRING) == 0);
1466 g_free (string);
1467 string = g_strconcat (GLIB_TEST_STRING, GLIB_TEST_STRING,
1468 GLIB_TEST_STRING, NULL);
1469 g_assert (string != NULL);
1470 g_assert (strcmp (string, GLIB_TEST_STRING GLIB_TEST_STRING
1471 GLIB_TEST_STRING) == 0);
1472 g_free (string);
1474 if (g_test_verbose())
1475 g_print ("checking g_strlcpy/g_strlcat...");
1476 /* The following is a torture test for strlcpy/strlcat, with lots of
1477 * checking; normal users wouldn't use them this way!
1479 string = g_malloc (6);
1480 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
1481 *string = 'q';
1482 g_assert (g_strlcpy(string, "" , 5) == 0);
1483 g_assert ( *string == '\0' );
1484 *string = 'q';
1485 g_assert (g_strlcpy(string, "abc" , 5) == 3);
1486 g_assert ( *(string + 3) == '\0' );
1487 g_assert (g_str_equal(string, "abc"));
1488 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1489 g_assert ( *(string + 4) == '\0' );
1490 g_assert ( *(string + 5) == 'Z' );
1491 g_assert (g_str_equal(string, "abcd"));
1492 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1493 g_assert ( *(string + 4) == '\0' );
1494 g_assert ( *(string + 5) == 'Z' );
1495 g_assert (g_str_equal(string, "abcd"));
1496 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1497 g_assert ( *(string + 4) == '\0' );
1498 g_assert ( *(string + 5) == 'Z' );
1499 g_assert (g_str_equal(string, "abcd"));
1500 *string = 'Y';
1501 *(string + 1)= '\0';
1502 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1503 g_assert (*string == 'Y');
1504 *string = '\0';
1505 g_assert (g_strlcat(string, "123" , 5) == 3);
1506 g_assert ( *(string + 3) == '\0' );
1507 g_assert (g_str_equal(string, "123"));
1508 g_assert (g_strlcat(string, "" , 5) == 3);
1509 g_assert ( *(string + 3) == '\0' );
1510 g_assert (g_str_equal(string, "123"));
1511 g_assert (g_strlcat(string, "4", 5) == 4);
1512 g_assert (g_str_equal(string, "1234"));
1513 g_assert (g_strlcat(string, "5", 5) == 5);
1514 g_assert ( *(string + 4) == '\0' );
1515 g_assert (g_str_equal(string, "1234"));
1516 g_assert ( *(string + 5) == 'Z' );
1517 *string = 'Y';
1518 *(string + 1)= '\0';
1519 g_assert (g_strlcat(string, "123" , 0) == 3);
1520 g_assert (*string == 'Y');
1522 /* A few more tests, demonstrating more "normal" use */
1523 g_assert (g_strlcpy(string, "hi", 5) == 2);
1524 g_assert (g_str_equal(string, "hi"));
1525 g_assert (g_strlcat(string, "t", 5) == 3);
1526 g_assert (g_str_equal(string, "hit"));
1527 g_free(string);
1529 if (g_test_verbose())
1530 g_print ("checking g_strdup_printf...\n");
1531 string = g_strdup_printf ("%05d %-5s", 21, "test");
1532 g_assert (string != NULL);
1533 g_assert (strcmp(string, "00021 test ") == 0);
1534 g_free (string);
1536 /* g_debug (argv[0]); */
1539 #ifndef G_DISABLE_DEPRECATED
1540 static void
1541 test_mem_chunks (void)
1543 GMemChunk *mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
1544 gchar *mem[10000];
1545 guint i;
1546 for (i = 0; i < 10000; i++)
1548 guint j;
1549 mem[i] = g_chunk_new (gchar, mem_chunk);
1550 for (j = 0; j < 50; j++)
1551 mem[i][j] = i * j;
1553 for (i = 0; i < 10000; i++)
1554 g_mem_chunk_free (mem_chunk, mem[i]);
1556 #endif
1559 main (int argc,
1560 char *argv[])
1562 g_test_init (&argc, &argv, NULL);
1564 g_test_add_func ("/testglib/Infos", test_info);
1565 g_test_add_func ("/testglib/Types Sizes", type_sizes);
1566 g_test_add_func ("/testglib/GStrings", gstring_tests);
1567 g_test_add_func ("/testglib/Various Strings", various_string_tests);
1568 g_test_add_func ("/testglib/GList", glist_test);
1569 g_test_add_func ("/testglib/GSList", gslist_test);
1570 g_test_add_func ("/testglib/GNode", gnode_test);
1571 g_test_add_func ("/testglib/GTree", binary_tree_test);
1572 g_test_add_func ("/testglib/Arrays", test_arrays);
1573 g_test_add_func ("/testglib/GHashTable", hash_table_tests);
1574 #ifndef G_DISABLE_DEPRECATED
1575 g_test_add_func ("/testglib/Relation (deprecated)", relation_test);
1576 #endif
1577 g_test_add_func ("/testglib/File Paths", test_paths);
1578 g_test_add_func ("/testglib/File Functions", test_file_functions);
1579 g_test_add_func ("/testglib/Parse Debug Strings", test_g_parse_debug_string);
1580 #ifndef G_DISABLE_DEPRECATED
1581 g_test_add_func ("/testglib/GMemChunk (deprecated)", test_mem_chunks);
1582 #endif
1583 g_test_add_func ("/testglib/Warnings & Errors", log_warning_error_tests);
1584 g_test_add_func ("/testglib/Timers (slow)", timer_tests);
1586 return g_test_run();