tests: Mark refcount/properties2 test as slow
[glib.git] / tests / testglib.c
blobf29bbc6641e4e0e0c3b8f90979fc43c04c04b685
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");
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", *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 struct {
842 gchar *cwd;
843 gchar *relative_path;
844 gchar *canonical_path;
845 } canonicalize_filename_checks[] = {
846 { "/etc", "../usr/share", "/usr/share" },
847 { "/", "/foo/bar", "/foo/bar" },
848 { "/usr/bin", "../../foo/bar", "/foo/bar" },
849 { "/", "../../foo/bar", "/foo/bar" },
850 { "/double//dash", "../../foo/bar", "/foo/bar" },
851 { "/usr/share/foo", ".././././bar", "/usr/share/bar" },
852 { "/foo/bar", "../bar/./.././bar", "/foo/bar" },
853 { "/test///dir", "../../././foo/bar", "/foo/bar" },
854 { "/test///dir", "../../././/foo///bar", "/foo/bar" },
855 { "/etc", "///triple/slash", "/triple/slash" },
856 { "/etc", "//double/slash", "//double/slash" },
857 { "///triple/slash", ".", "/triple/slash" },
858 { "//double/slash", ".", "//double/slash" },
859 { "/cwd/../with/./complexities/", "./hello", "/with/complexities/hello" },
860 #ifdef G_OS_WIN32
861 { "\\etc", "..\\usr\\share", "\\usr\\share" },
862 { "\\", "\\foo\\bar", "\\foo\\bar" },
863 { "\\usr\\bin", "..\\..\\foo\\bar", "\\foo\\bar" },
864 { "\\", "..\\..\\foo\\bar", "\\foo\\bar" },
865 { "\\double\\\\dash", "..\\..\\foo\\bar", "\\foo\\bar" },
866 { "\\usr\\share\\foo", "..\\.\\.\\.\\bar", "\\usr\\share\\bar" },
867 { "\\foo\\bar", "..\\bar\\.\\..\\.\\bar", "\\foo\\bar" },
868 { "\\test\\\\\\dir", "..\\..\\.\\.\\foo\\bar", "\\foo\\bar" },
869 { "\\test\\\\\\dir", "..\\..\\.\\.\\\\foo\\\\\\bar", "\\foo\\bar" },
870 { "\\etc", "\\\\\\triple\\slash", "\\triple\\slash" },
871 { "\\etc", "\\\\double\\slash", "\\\\double\\slash" },
872 { "\\\\\\triple\\slash", ".", "\\triple\\slash" },
873 { "\\\\double\\slash", ".", "\\\\double\\slash" },
874 { "\\cwd\\..\\with\\.\\complexities\\", ".\\hello", "\\cwd\\with\\complexities\\hello" },
875 #endif
877 const guint n_canonicalize_filename_checks = G_N_ELEMENTS (canonicalize_filename_checks);
878 gchar *string;
879 guint i;
880 if (g_test_verbose())
881 g_printerr ("checking g_path_get_basename()...");
882 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
883 g_assert (strcmp (string, "dir") == 0);
884 g_free (string);
885 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
886 g_assert (strcmp (string, "file") == 0);
887 g_free (string);
888 if (g_test_verbose())
889 g_printerr ("ok\n");
891 #ifdef G_OS_WIN32
892 string = g_path_get_basename ("/foo/dir/");
893 g_assert (strcmp (string, "dir") == 0);
894 g_free (string);
895 string = g_path_get_basename ("/foo/file");
896 g_assert (strcmp (string, "file") == 0);
897 g_free (string);
898 #endif
900 if (g_test_verbose())
901 g_printerr ("checking g_path_get_dirname()...");
902 for (i = 0; i < n_dirname_checks; i++)
904 gchar *dirname = g_path_get_dirname (dirname_checks[i].filename);
905 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
907 g_error ("failed for \"%s\"==\"%s\" (returned: \"%s\")",
908 dirname_checks[i].filename,
909 dirname_checks[i].dirname,
910 dirname);
912 g_free (dirname);
914 if (g_test_verbose())
915 g_printerr ("ok\n");
917 if (g_test_verbose())
918 g_printerr ("checking g_path_skip_root()...");
919 for (i = 0; i < n_skip_root_checks; i++)
921 const gchar *skipped = g_path_skip_root (skip_root_checks[i].filename);
922 if ((skipped && !skip_root_checks[i].without_root) ||
923 (!skipped && skip_root_checks[i].without_root) ||
924 ((skipped && skip_root_checks[i].without_root) &&
925 strcmp (skipped, skip_root_checks[i].without_root)))
927 g_error ("failed for \"%s\"==\"%s\" (returned: \"%s\")",
928 skip_root_checks[i].filename,
929 (skip_root_checks[i].without_root ?
930 skip_root_checks[i].without_root : "<NULL>"),
931 (skipped ? skipped : "<NULL>"));
934 if (g_test_verbose())
935 g_printerr ("ok\n");
937 if (g_test_verbose ())
938 g_printerr ("checking g_canonicalize_filename()...");
939 for (i = 0; i < n_canonicalize_filename_checks; i++)
941 gchar *canonical_path = g_canonicalize_filename (canonicalize_filename_checks[i].relative_path,
942 canonicalize_filename_checks[i].cwd);
943 if (g_strcmp0 (canonical_path, canonicalize_filename_checks[i].canonical_path) != 0)
945 g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
946 canonicalize_filename_checks[i].relative_path,
947 canonicalize_filename_checks[i].canonical_path,
948 canonical_path);
950 g_free (canonical_path);
952 if (g_test_verbose ())
953 g_printerr ("ok\n");
955 if (g_test_verbose ())
956 g_printerr ("checking g_canonicalize_filename() supports NULL...");
959 const gchar *relative_path = "./";
960 gchar *canonical_path = g_canonicalize_filename (relative_path, NULL);
961 gchar *cwd = g_get_current_dir ();
962 if (g_strcmp0 (canonical_path, cwd) != 0)
964 g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
965 relative_path, cwd, canonical_path);
967 g_free (cwd);
968 g_free (canonical_path);
971 if (g_test_verbose ())
972 g_printerr ("ok\n");
975 static void
976 test_file_functions (void)
978 const char hello[] = "Hello, World";
979 const int hellolen = sizeof (hello) - 1;
980 GError *error;
981 char template[32];
982 char *name_used, chars[62];
983 gint fd, n;
984 int errsv;
986 strcpy (template, "foobar");
987 fd = g_mkstemp (template);
988 if (g_test_verbose() && fd != -1)
989 g_printerr ("g_mkstemp works even if template doesn't end in XXXXXX\n");
990 if (fd != -1)
991 close (fd);
992 strcpy (template, "fooXXXXXX");
993 fd = g_mkstemp (template);
994 if (fd == -1)
995 g_error ("g_mkstemp didn't work for template %s", template);
996 n = write (fd, hello, hellolen);
997 errsv = errno;
998 if (n == -1)
999 g_error ("write() failed: %s", g_strerror (errsv));
1000 else if (n != hellolen)
1001 g_error ("write() should have written %d bytes, wrote %d", hellolen, n);
1003 lseek (fd, 0, 0);
1004 n = read (fd, chars, sizeof (chars));
1005 errsv = errno;
1006 if (n == -1)
1007 g_error ("read() failed: %s", g_strerror (errsv));
1008 else if (n != hellolen)
1009 g_error ("read() should have read %d bytes, got %d", hellolen, n);
1011 chars[n] = 0;
1012 if (strcmp (chars, hello) != 0)
1013 g_error ("wrote '%s', but got '%s'", hello, chars);
1014 if (fd != -1)
1015 close (fd);
1016 remove (template);
1018 error = NULL;
1019 name_used = NULL;
1020 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1021 fd = g_file_open_tmp (template, &name_used, &error);
1022 if (g_test_verbose())
1024 if (fd != -1)
1025 g_printerr ("g_file_open_tmp works even if template contains '%s'\n", G_DIR_SEPARATOR_S);
1026 else
1027 g_printerr ("g_file_open_tmp correctly returns error: %s\n", error->message);
1029 if (fd != -1)
1030 close (fd);
1031 g_clear_error (&error);
1032 g_free (name_used);
1034 #ifdef G_OS_WIN32
1035 name_used = NULL;
1036 strcpy (template, "zap/barXXXXXX");
1037 fd = g_file_open_tmp (template, &name_used, &error);
1038 if (g_test_verbose())
1040 if (fd != -1)
1041 g_printerr ("g_file_open_tmp works even if template contains '/'\n");
1042 else
1043 g_printerr ("g_file_open_tmp correctly returns error: %s\n", error->message);
1045 if (fd != -1)
1046 close (fd);
1047 g_clear_error (&error);
1048 g_free (name_used);
1049 #endif
1051 strcpy (template, "zapXXXXXX");
1052 name_used = NULL;
1053 fd = g_file_open_tmp (template, &name_used, &error);
1054 if (fd == -1)
1055 g_error ("g_file_open_tmp didn't work for template '%s': %s", template, error->message);
1056 else if (g_test_verbose())
1057 g_printerr ("g_file_open_tmp for template '%s' used name '%s'\n", template, name_used);
1058 if (fd != -1)
1059 close (fd);
1060 g_clear_error (&error);
1061 remove (name_used);
1062 g_free (name_used);
1064 name_used = NULL;
1065 fd = g_file_open_tmp (NULL, &name_used, &error);
1066 if (fd == -1)
1067 g_error ("g_file_open_tmp didn't work for a NULL template: %s", error->message);
1068 else
1069 close (fd);
1070 g_clear_error (&error);
1071 remove (name_used);
1072 g_free (name_used);
1075 static void
1076 test_arrays (void)
1078 GByteArray *gbarray;
1079 GPtrArray *gparray;
1080 GArray *garray;
1081 guint i;
1083 gparray = g_ptr_array_new ();
1084 for (i = 0; i < 10000; i++)
1085 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1086 for (i = 0; i < 10000; i++)
1087 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1088 g_error ("array fails: %p ( %p )", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1089 g_ptr_array_free (gparray, TRUE);
1091 gbarray = g_byte_array_new ();
1092 for (i = 0; i < 10000; i++)
1093 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1094 for (i = 0; i < 10000; i++)
1096 g_assert (gbarray->data[4*i] == 'a');
1097 g_assert (gbarray->data[4*i+1] == 'b');
1098 g_assert (gbarray->data[4*i+2] == 'c');
1099 g_assert (gbarray->data[4*i+3] == 'd');
1101 g_byte_array_free (gbarray, TRUE);
1103 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1104 for (i = 0; i < 10000; i++)
1105 g_array_append_val (garray, i);
1106 for (i = 0; i < 10000; i++)
1107 if (g_array_index (garray, gint, i) != i)
1108 g_error ("failure: %d ( %d )", g_array_index (garray, gint, i), i);
1109 g_array_free (garray, TRUE);
1111 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1112 for (i = 0; i < 100; i++)
1113 g_array_prepend_val (garray, i);
1114 for (i = 0; i < 100; i++)
1115 if (g_array_index (garray, gint, i) != (100 - i - 1))
1116 g_error ("failure: %d ( %d )", g_array_index (garray, gint, i), 100 - i - 1);
1117 g_array_free (garray, TRUE);
1120 static void
1121 hash_table_tests (void)
1123 GHashTable *hash_table;
1124 int array[10000];
1125 gint *pvalue = NULL;
1126 gint value = 120;
1127 guint i;
1129 hash_table = g_hash_table_new (my_hash, my_hash_equal);
1130 for (i = 0; i < 10000; i++)
1132 array[i] = i;
1133 g_hash_table_insert (hash_table, &array[i], &array[i]);
1135 pvalue = g_hash_table_find (hash_table, find_first_that, &value);
1136 if (*pvalue != value)
1137 g_error ("g_hash_table_find failed");
1138 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
1139 for (i = 0; i < 10000; i++)
1140 if (array[i] == 0)
1141 g_error ("hashtable-test: wrong value: %d", i);
1142 for (i = 0; i < 10000; i++)
1143 g_hash_table_remove (hash_table, &array[i]);
1144 for (i = 0; i < 10000; i++)
1146 array[i] = i;
1147 g_hash_table_insert (hash_table, &array[i], &array[i]);
1149 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
1150 g_hash_table_size (hash_table) != 5000)
1151 g_error ("hashtable removal failed");
1152 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
1153 g_hash_table_destroy (hash_table);
1156 #ifndef G_DISABLE_DEPRECATED
1157 static void
1158 relation_test (void)
1160 GRelation *relation = g_relation_new (2);
1161 GTuples *tuples;
1162 gint data [1024];
1163 guint i;
1165 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1166 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1168 for (i = 0; i < 1024; i += 1)
1169 data[i] = i;
1171 for (i = 1; i < 1023; i += 1)
1173 g_relation_insert (relation, data + i, data + i + 1);
1174 g_relation_insert (relation, data + i, data + i - 1);
1177 for (i = 2; i < 1022; i += 1)
1179 g_assert (! g_relation_exists (relation, data + i, data + i));
1180 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1181 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1184 for (i = 1; i < 1023; i += 1)
1186 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1187 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1190 for (i = 2; i < 1022; i += 1)
1192 g_assert (g_relation_count (relation, data + i, 0) == 2);
1193 g_assert (g_relation_count (relation, data + i, 1) == 2);
1196 g_assert (g_relation_count (relation, data, 0) == 0);
1198 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1199 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1200 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1201 g_relation_delete (relation, data + 42, 0);
1202 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1203 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1204 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1206 tuples = g_relation_select (relation, data + 200, 0);
1208 g_assert (tuples->len == 2);
1210 #if 0
1211 for (i = 0; i < tuples->len; i += 1)
1213 printf ("%d %d\n",
1214 *(gint*) g_tuples_index (tuples, i, 0),
1215 *(gint*) g_tuples_index (tuples, i, 1));
1217 #endif
1219 g_assert (g_relation_exists (relation, data + 300, data + 301));
1220 g_relation_delete (relation, data + 300, 0);
1221 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1223 g_tuples_destroy (tuples);
1225 g_relation_destroy (relation);
1227 relation = NULL;
1229 #endif
1231 static void
1232 gstring_tests (void)
1234 GString *string1, *string2;
1235 guint i;
1237 if (g_test_verbose())
1238 g_printerr ("test GString basics\n");
1240 string1 = g_string_new ("hi pete!");
1241 string2 = g_string_new ("");
1243 g_assert (strcmp ("hi pete!", string1->str) == 0);
1245 for (i = 0; i < 10000; i++)
1246 g_string_append_c (string1, 'a'+(i%26));
1248 #ifndef G_OS_WIN32
1249 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
1250 the %10000.10000f format... */
1251 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
1252 "this pete guy sure is a wuss, like he's the number ",
1254 " wuss. everyone agrees.\n",
1255 string1->str,
1256 10, 666, 15, 15, 666.666666666, 666.666666666);
1257 #else
1258 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
1259 "this pete guy sure is a wuss, like he's the number ",
1261 " wuss. everyone agrees.\n",
1262 string1->str,
1263 10, 666, 15, 15, 666.666666666, 666.666666666);
1264 #endif
1266 if (g_test_verbose())
1267 g_printerr ("string2 length = %lu...\n", (gulong)string2->len);
1268 string2->str[70] = '\0';
1269 if (g_test_verbose())
1270 g_printerr ("first 70 chars:\n%s\n", string2->str);
1271 string2->str[141] = '\0';
1272 if (g_test_verbose())
1273 g_printerr ("next 70 chars:\n%s\n", string2->str+71);
1274 string2->str[212] = '\0';
1275 if (g_test_verbose())
1276 g_printerr ("and next 70:\n%s\n", string2->str+142);
1277 if (g_test_verbose())
1278 g_printerr ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
1280 g_string_free (string1, TRUE);
1281 g_string_free (string2, TRUE);
1283 /* append */
1284 string1 = g_string_new ("firsthalf");
1285 g_string_append (string1, "lasthalf");
1286 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1287 g_string_free (string1, TRUE);
1289 /* append_len */
1290 string1 = g_string_new ("firsthalf");
1291 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
1292 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1293 g_string_free (string1, TRUE);
1295 /* prepend */
1296 string1 = g_string_new ("lasthalf");
1297 g_string_prepend (string1, "firsthalf");
1298 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1299 g_string_free (string1, TRUE);
1301 /* prepend_len */
1302 string1 = g_string_new ("lasthalf");
1303 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
1304 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1305 g_string_free (string1, TRUE);
1307 /* insert */
1308 string1 = g_string_new ("firstlast");
1309 g_string_insert (string1, 5, "middle");
1310 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1311 g_string_free (string1, TRUE);
1313 /* insert with pos == end of the string */
1314 string1 = g_string_new ("firstmiddle");
1315 g_string_insert (string1, strlen ("firstmiddle"), "last");
1316 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1317 g_string_free (string1, TRUE);
1319 /* insert_len */
1320 string1 = g_string_new ("firstlast");
1321 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
1322 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1323 g_string_free (string1, TRUE);
1325 /* insert_len with magic -1 pos for append */
1326 string1 = g_string_new ("first");
1327 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
1328 g_assert (strcmp (string1->str, "firstlast") == 0);
1329 g_string_free (string1, TRUE);
1331 /* insert_len with magic -1 len for strlen-the-string */
1332 string1 = g_string_new ("first");
1333 g_string_insert_len (string1, 5, "last", -1);
1334 g_assert (strcmp (string1->str, "firstlast") == 0);
1335 g_string_free (string1, TRUE);
1337 /* g_string_equal */
1338 string1 = g_string_new ("test");
1339 string2 = g_string_new ("te");
1340 g_assert (! g_string_equal(string1, string2));
1341 g_string_append (string2, "st");
1342 g_assert (g_string_equal(string1, string2));
1343 g_string_free (string1, TRUE);
1344 g_string_free (string2, TRUE);
1346 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
1347 if (g_test_verbose())
1348 g_printerr ("test embedded ASCII 0 (NUL) characters in GString\n");
1349 string1 = g_string_new ("fiddle");
1350 string2 = g_string_new ("fiddle");
1351 g_assert (g_string_equal(string1, string2));
1352 g_string_append_c(string1, '\0');
1353 g_assert (! g_string_equal(string1, string2));
1354 g_string_append_c(string2, '\0');
1355 g_assert (g_string_equal(string1, string2));
1356 g_string_append_c(string1, 'x');
1357 g_string_append_c(string2, 'y');
1358 g_assert (! g_string_equal(string1, string2));
1359 g_assert (string1->len == 8);
1360 g_string_append(string1, "yzzy");
1361 g_assert (string1->len == 12);
1362 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
1363 g_string_insert(string1, 1, "QED");
1364 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
1365 g_string_free (string1, TRUE);
1366 g_string_free (string2, TRUE);
1369 static void
1370 various_string_tests (void)
1372 GStringChunk *string_chunk;
1373 GTimeVal ref_date, date;
1374 gchar *tmp_string = NULL, *tmp_string_2, *string, *date_str;
1375 guint i;
1376 const gchar *tz;
1378 if (g_test_verbose())
1379 g_printerr ("checking string chunks...");
1380 string_chunk = g_string_chunk_new (1024);
1381 for (i = 0; i < 100000; i ++)
1383 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
1384 if (strcmp ("hi pete", tmp_string) != 0)
1385 g_error ("string chunks are broken.");
1387 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
1388 g_assert (tmp_string_2 != tmp_string && strcmp (tmp_string_2, tmp_string) == 0);
1389 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
1390 g_assert (tmp_string_2 == tmp_string);
1391 g_string_chunk_free (string_chunk);
1393 if (g_test_verbose())
1394 g_printerr ("test positional printf formats (not supported):");
1395 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
1396 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
1397 if (g_test_verbose())
1398 g_printerr ("%s%s\n", string, tmp_string);
1399 g_free (tmp_string);
1400 g_free (string);
1402 #define REF_INVALID1 "Wed Dec 19 17:20:20 GMT 2007"
1403 #define REF_INVALID2 "1980-02-22T10:36:00Zulu"
1404 #define REF_INVALID3 "1980-02-22T"
1405 #define REF_SEC_UTC 320063760
1406 #define REF_STR_UTC "1980-02-22T10:36:00Z"
1407 #define REF_STR_LOCAL "1980-02-22T13:36:00"
1408 #define REF_STR_CEST "1980-02-22T12:36:00+02:00"
1409 #define REF_STR_EST "19800222T053600-0500"
1410 #define REF_STR_NST "1980-02-22T07:06:00-03:30"
1411 #define REF_USEC_UTC 50000
1412 #define REF_STR_USEC_UTC "1980-02-22T10:36:00.050000Z"
1413 #define REF_STR_USEC_CEST "19800222T123600.050000000+0200"
1414 #define REF_STR_USEC_EST "1980-02-22T05:36:00,05-05:00"
1415 #define REF_STR_USEC_NST "19800222T070600,0500-0330"
1416 #define REF_STR_DATE_ONLY "1980-02-22"
1418 if (g_test_verbose())
1419 g_printerr ("checking g_time_val_from_iso8601...\n");
1420 ref_date.tv_sec = REF_SEC_UTC;
1421 ref_date.tv_usec = 0;
1422 g_assert (g_time_val_from_iso8601 (REF_INVALID1, &date) == FALSE);
1423 g_assert (g_time_val_from_iso8601 (REF_INVALID2, &date) == FALSE);
1424 g_assert (g_time_val_from_iso8601 (REF_INVALID3, &date) == FALSE);
1425 g_assert (g_time_val_from_iso8601 (REF_STR_DATE_ONLY, &date) == FALSE);
1426 g_assert (g_time_val_from_iso8601 (REF_STR_UTC, &date) != FALSE);
1427 if (g_test_verbose())
1428 g_printerr ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1429 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1430 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1431 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1433 /* predefine time zone */
1434 tz = g_getenv("TZ");
1435 g_setenv("TZ", "UTC-03:00", 1);
1436 tzset();
1438 g_assert (g_time_val_from_iso8601 (REF_STR_LOCAL, &date) != FALSE);
1439 if (g_test_verbose())
1440 g_printerr ("\t=> LOCAL stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1441 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1442 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1443 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1445 /* revert back user defined time zone */
1446 if (tz)
1447 g_setenv("TZ", tz, TRUE);
1448 else
1449 g_unsetenv("TZ");
1450 tzset();
1452 g_assert (g_time_val_from_iso8601 (REF_STR_CEST, &date) != FALSE);
1453 if (g_test_verbose())
1454 g_printerr ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1455 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1456 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1457 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1459 g_assert (g_time_val_from_iso8601 (REF_STR_EST, &date) != FALSE);
1460 if (g_test_verbose())
1461 g_printerr ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1462 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1463 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1464 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1466 g_assert (g_time_val_from_iso8601 (REF_STR_NST, &date) != FALSE);
1467 if (g_test_verbose())
1468 g_printerr ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1469 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1470 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1471 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1473 ref_date.tv_usec = REF_USEC_UTC;
1474 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_UTC, &date) != FALSE);
1475 if (g_test_verbose())
1476 g_printerr ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1477 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1478 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1479 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1481 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_CEST, &date) != FALSE);
1482 if (g_test_verbose())
1483 g_printerr ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1484 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1485 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1486 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1488 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_EST, &date) != FALSE);
1489 if (g_test_verbose())
1490 g_printerr ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1491 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1492 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1493 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1495 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_NST, &date) != FALSE);
1496 if (g_test_verbose())
1497 g_printerr ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1498 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1499 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1500 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1502 if (g_test_verbose())
1503 g_printerr ("checking g_time_val_to_iso8601...\n");
1504 ref_date.tv_sec = REF_SEC_UTC;
1505 ref_date.tv_usec = 0;
1506 date_str = g_time_val_to_iso8601 (&ref_date);
1507 g_assert (date_str != NULL);
1508 if (g_test_verbose())
1509 g_printerr ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_UTC);
1510 g_assert (strcmp (date_str, REF_STR_UTC) == 0);
1511 g_free (date_str);
1513 ref_date.tv_usec = REF_USEC_UTC;
1514 date_str = g_time_val_to_iso8601 (&ref_date);
1515 g_assert (date_str != NULL);
1516 if (g_test_verbose())
1517 g_printerr ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_USEC_UTC);
1518 g_assert (strcmp (date_str, REF_STR_USEC_UTC) == 0);
1519 g_free (date_str);
1521 if (g_test_verbose())
1522 g_printerr ("checking g_ascii_strcasecmp...");
1523 g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
1524 g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
1525 g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
1526 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1527 g_assert (g_ascii_strcasecmp ("", "") == 0);
1528 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
1529 g_assert (g_ascii_strcasecmp ("a", "b") < 0);
1530 g_assert (g_ascii_strcasecmp ("a", "B") < 0);
1531 g_assert (g_ascii_strcasecmp ("A", "b") < 0);
1532 g_assert (g_ascii_strcasecmp ("A", "B") < 0);
1533 g_assert (g_ascii_strcasecmp ("b", "a") > 0);
1534 g_assert (g_ascii_strcasecmp ("b", "A") > 0);
1535 g_assert (g_ascii_strcasecmp ("B", "a") > 0);
1536 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1538 if (g_test_verbose())
1539 g_printerr ("checking g_strdup...\n");
1540 g_assert (g_strdup (NULL) == NULL);
1541 string = g_strdup (GLIB_TEST_STRING);
1542 g_assert (string != NULL);
1543 g_assert (strcmp(string, GLIB_TEST_STRING) == 0);
1544 g_free (string);
1546 if (g_test_verbose())
1547 g_printerr ("checking g_strconcat...\n");
1548 string = g_strconcat (GLIB_TEST_STRING, NULL);
1549 g_assert (string != NULL);
1550 g_assert (strcmp (string, GLIB_TEST_STRING) == 0);
1551 g_free (string);
1552 string = g_strconcat (GLIB_TEST_STRING, GLIB_TEST_STRING,
1553 GLIB_TEST_STRING, NULL);
1554 g_assert (string != NULL);
1555 g_assert (strcmp (string, GLIB_TEST_STRING GLIB_TEST_STRING
1556 GLIB_TEST_STRING) == 0);
1557 g_free (string);
1559 if (g_test_verbose())
1560 g_printerr ("checking g_strlcpy/g_strlcat...");
1561 /* The following is a torture test for strlcpy/strlcat, with lots of
1562 * checking; normal users wouldn't use them this way!
1564 string = g_malloc (6);
1565 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
1566 *string = 'q';
1567 g_assert (g_strlcpy(string, "" , 5) == 0);
1568 g_assert ( *string == '\0' );
1569 *string = 'q';
1570 g_assert (g_strlcpy(string, "abc" , 5) == 3);
1571 g_assert ( *(string + 3) == '\0' );
1572 g_assert (g_str_equal(string, "abc"));
1573 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1574 g_assert ( *(string + 4) == '\0' );
1575 g_assert ( *(string + 5) == 'Z' );
1576 g_assert (g_str_equal(string, "abcd"));
1577 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1578 g_assert ( *(string + 4) == '\0' );
1579 g_assert ( *(string + 5) == 'Z' );
1580 g_assert (g_str_equal(string, "abcd"));
1581 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1582 g_assert ( *(string + 4) == '\0' );
1583 g_assert ( *(string + 5) == 'Z' );
1584 g_assert (g_str_equal(string, "abcd"));
1585 *string = 'Y';
1586 *(string + 1)= '\0';
1587 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1588 g_assert (*string == 'Y');
1589 *string = '\0';
1590 g_assert (g_strlcat(string, "123" , 5) == 3);
1591 g_assert ( *(string + 3) == '\0' );
1592 g_assert (g_str_equal(string, "123"));
1593 g_assert (g_strlcat(string, "" , 5) == 3);
1594 g_assert ( *(string + 3) == '\0' );
1595 g_assert (g_str_equal(string, "123"));
1596 g_assert (g_strlcat(string, "4", 5) == 4);
1597 g_assert (g_str_equal(string, "1234"));
1598 g_assert (g_strlcat(string, "5", 5) == 5);
1599 g_assert ( *(string + 4) == '\0' );
1600 g_assert (g_str_equal(string, "1234"));
1601 g_assert ( *(string + 5) == 'Z' );
1602 *string = 'Y';
1603 *(string + 1)= '\0';
1604 g_assert (g_strlcat(string, "123" , 0) == 3);
1605 g_assert (*string == 'Y');
1607 /* A few more tests, demonstrating more "normal" use */
1608 g_assert (g_strlcpy(string, "hi", 5) == 2);
1609 g_assert (g_str_equal(string, "hi"));
1610 g_assert (g_strlcat(string, "t", 5) == 3);
1611 g_assert (g_str_equal(string, "hit"));
1612 g_free(string);
1614 if (g_test_verbose())
1615 g_printerr ("checking g_strdup_printf...\n");
1616 string = g_strdup_printf ("%05d %-5s", 21, "test");
1617 g_assert (string != NULL);
1618 g_assert (strcmp(string, "00021 test ") == 0);
1619 g_free (string);
1621 /* g_debug (argv[0]); */
1624 #ifndef G_DISABLE_DEPRECATED
1625 static void
1626 test_mem_chunks (void)
1628 GMemChunk *mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
1629 gchar *mem[10000];
1630 guint i;
1631 for (i = 0; i < 10000; i++)
1633 guint j;
1634 mem[i] = g_chunk_new (gchar, mem_chunk);
1635 for (j = 0; j < 50; j++)
1636 mem[i][j] = i * j;
1638 for (i = 0; i < 10000; i++)
1639 g_mem_chunk_free (mem_chunk, mem[i]);
1641 g_mem_chunk_destroy (mem_chunk);
1643 #endif
1646 main (int argc,
1647 char *argv[])
1649 g_test_init (&argc, &argv, NULL);
1651 g_test_add_func ("/testglib/Infos", test_info);
1652 g_test_add_func ("/testglib/Types Sizes", type_sizes);
1653 g_test_add_func ("/testglib/GStrings", gstring_tests);
1654 g_test_add_func ("/testglib/Various Strings", various_string_tests);
1655 g_test_add_func ("/testglib/GList", glist_test);
1656 g_test_add_func ("/testglib/GSList", gslist_test);
1657 g_test_add_func ("/testglib/GNode", gnode_test);
1658 g_test_add_func ("/testglib/GTree", binary_tree_test);
1659 g_test_add_func ("/testglib/Arrays", test_arrays);
1660 g_test_add_func ("/testglib/GHashTable", hash_table_tests);
1661 #ifndef G_DISABLE_DEPRECATED
1662 g_test_add_func ("/testglib/Relation (deprecated)", relation_test);
1663 #endif
1664 g_test_add_func ("/testglib/File Paths", test_paths);
1665 g_test_add_func ("/testglib/File Functions", test_file_functions);
1666 g_test_add_func ("/testglib/Parse Debug Strings", test_g_parse_debug_string);
1667 #ifndef G_DISABLE_DEPRECATED
1668 g_test_add_func ("/testglib/GMemChunk (deprecated)", test_mem_chunks);
1669 #endif
1670 g_test_add_func ("/testglib/Warnings & Errors", log_warning_error_tests);
1671 g_test_add_func ("/testglib/Timers (slow)", timer_tests);
1673 return g_test_run();