Don't test user directories for being non-null. (#517084, Yevgen Muntyan)
[platform/upstream/glib.git] / tests / testglib.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 #include "config.h"
28
29 #undef GLIB_COMPILATION
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "glib.h"
36 #include "gstdio.h"
37
38 #include <stdlib.h>
39
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43
44 #ifdef G_OS_WIN32
45 #include <io.h>                 /* For read(), write() etc */
46 #endif
47
48
49 #define GLIB_TEST_STRING "el dorado "
50 #define GLIB_TEST_STRING_5 "el do"
51
52
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};
56
57 /* --- functions --- */
58 static gint
59 my_list_compare_one (gconstpointer a, gconstpointer b)
60 {
61   gint one = *((const gint*)a);
62   gint two = *((const gint*)b);
63   return one-two;
64 }
65
66 static gint
67 my_list_compare_two (gconstpointer a, gconstpointer b)
68 {
69   gint one = *((const gint*)a);
70   gint two = *((const gint*)b);
71   return two-one;
72 }
73
74 /* static void
75 my_list_print (gpointer a, gpointer b)
76 {
77   gint three = *((gint*)a);
78   g_print("%d", three);
79 }; */
80
81 static void
82 glist_test (void)
83 {
84   GList *list = NULL;
85   guint i;
86
87   for (i = 0; i < 10; i++)
88     list = g_list_append (list, &test_nums[i]);
89   list = g_list_reverse (list);
90
91   for (i = 0; i < 10; i++)
92     {
93       GList *t = g_list_nth (list, i);
94       if (*((gint*) t->data) != (9 - i))
95         g_error ("Regular insert failed");
96     }
97
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");
101
102   g_list_free (list);
103   list = NULL;
104
105   for (i = 0; i < 10; i++)
106     list = g_list_insert_sorted (list, &more_nums[i], my_list_compare_one);
107
108   /*
109   g_print("\n");
110   g_list_foreach (list, my_list_print, NULL);
111   */
112
113   for (i = 0; i < 10; i++)
114     {
115       GList *t = g_list_nth (list, i);
116       if (*((gint*) t->data) != i)
117          g_error ("Sorted insert failed");
118     }
119
120   g_list_free (list);
121   list = NULL;
122
123   for (i = 0; i < 10; i++)
124     list = g_list_insert_sorted (list, &more_nums[i], my_list_compare_two);
125
126   /*
127   g_print("\n");
128   g_list_foreach (list, my_list_print, NULL);
129   */
130
131   for (i = 0; i < 10; i++)
132     {
133       GList *t = g_list_nth (list, i);
134       if (*((gint*) t->data) != (9 - i))
135          g_error ("Sorted insert failed");
136     }
137
138   g_list_free (list);
139   list = NULL;
140
141   for (i = 0; i < 10; i++)
142     list = g_list_prepend (list, &more_nums[i]);
143
144   list = g_list_sort (list, my_list_compare_two);
145
146   /*
147   g_print("\n");
148   g_list_foreach (list, my_list_print, NULL);
149   */
150
151   for (i = 0; i < 10; i++)
152     {
153       GList *t = g_list_nth (list, i);
154       if (*((gint*) t->data) != (9 - i))
155          g_error ("Merge sort failed");
156     }
157
158   g_list_free (list);
159 }
160
161 static void
162 gslist_test (void)
163 {
164   GSList *slist = NULL;
165   guint i;
166
167   for (i = 0; i < 10; i++)
168     slist = g_slist_append (slist, &test_nums[i]);
169   slist = g_slist_reverse (slist);
170
171   for (i = 0; i < 10; i++)
172     {
173       GSList *st = g_slist_nth (slist, i);
174       if (*((gint*) st->data) != (9 - i))
175         g_error ("failed");
176     }
177
178   g_slist_free (slist);
179   slist = NULL;
180
181   for (i = 0; i < 10; i++)
182     slist = g_slist_insert_sorted (slist, &more_nums[i], my_list_compare_one);
183
184   /*
185   g_print("\n");
186   g_slist_foreach (slist, my_list_print, NULL);
187   */
188
189   for (i = 0; i < 10; i++)
190     {
191       GSList *st = g_slist_nth (slist, i);
192       if (*((gint*) st->data) != i)
193         g_error ("Sorted insert failed");
194     }
195
196   g_slist_free (slist);
197   slist = NULL;
198
199   for (i = 0; i < 10; i++)
200     slist = g_slist_insert_sorted (slist, &more_nums[i], my_list_compare_two);
201
202   /*
203   g_print("\n");
204   g_slist_foreach (slist, my_list_print, NULL);
205   */
206
207   for (i = 0; i < 10; i++)
208     {
209       GSList *st = g_slist_nth (slist, i);
210       if (*((gint*) st->data) != (9 - i))
211         g_error("Sorted insert failed");
212     }
213
214   g_slist_free(slist);
215   slist = NULL;
216
217   for (i = 0; i < 10; i++)
218     slist = g_slist_prepend (slist, &more_nums[i]);
219
220   slist = g_slist_sort (slist, my_list_compare_two);
221
222   /*
223   g_print("\n");
224   g_slist_foreach (slist, my_list_print, NULL);
225   */
226
227   for (i = 0; i < 10; i++)
228     {
229       GSList *st = g_slist_nth (slist, i);
230       if (*((gint*) st->data) != (9 - i))
231         g_error("Sorted insert failed");
232     }
233
234   g_slist_free(slist);
235 }
236
237 static gboolean
238 node_build_string (GNode    *node,
239                    gpointer  data)
240 {
241   gchar **p = data;
242   gchar *string;
243   gchar c[2] = "_";
244
245   c[0] = ((gchar) ((long) (node->data)));
246
247   string = g_strconcat (*p ? *p : "", c, NULL);
248   g_free (*p);
249   *p = string;
250
251   return FALSE;
252 }
253
254 static void
255 gnode_test (void)
256 {
257 #define C2P(c)          ((gpointer) ((long) (c)))
258 #define P2C(p)          ((gchar) ((long) (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;
267
268   root = g_node_new (C2P ('A'));
269   g_assert (g_node_depth (root) == 1 && g_node_max_height (root) == 1);
270
271   node_B = g_node_new (C2P ('B'));
272   g_node_append (root, node_B);
273   g_assert (root->children == node_B);
274
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')));
278
279   node_F = g_node_new (C2P ('F'));
280   g_node_append (root, node_F);
281   g_assert (root->children->next == node_F);
282
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')));
290
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);
302
303   for (i = 0; i < g_node_n_children (node_B); i++)
304     {
305       node = g_node_nth_child (node_B, i);
306       g_assert (P2C (node->data) == ('C' + i));
307     }
308   
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);
311
312   /* we have built:                    A
313    *                                 /   \
314    *                               B       F
315    *                             / | \       \
316    *                           C   D   E       G
317    *                                         / /\ \
318    *                                       H  I  J  K
319    *
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.
322    */
323
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;
337   
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;
344
345   g_node_reverse_children (node_B);
346   g_node_reverse_children (node_G);
347
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;
351
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);
362
363   g_node_destroy (root);
364
365   /* allocation tests */
366
367   root = g_node_new (NULL);
368   node = root;
369
370   for (i = 0; i < 2048; i++)
371     {
372       g_node_append (node, g_node_new (NULL));
373       if ((i%5) == 4)
374         node = node->children->next;
375     }
376   g_assert (g_node_max_height (root) > 100);
377   g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
378
379   g_node_destroy (root);
380 #undef C2P
381 #undef P2C
382 }
383
384 static gint
385 my_compare (gconstpointer a,
386             gconstpointer b)
387 {
388   const char *cha = a;
389   const char *chb = b;
390
391   return *cha - *chb;
392 }
393
394 static gint
395 my_traverse (gpointer key,
396              gpointer value,
397              gpointer data)
398 {
399   char *ch = key;
400   g_print ("%c ", *ch);
401   return FALSE;
402 }
403
404 static void
405 binary_tree_test (void)
406 {
407   GTree *tree;
408   char chars[62];
409   guint i, j;
410
411   tree = g_tree_new (my_compare);
412   i = 0;
413   for (j = 0; j < 10; j++, i++)
414     {
415       chars[i] = '0' + j;
416       g_tree_insert (tree, &chars[i], &chars[i]);
417     }
418   for (j = 0; j < 26; j++, i++)
419     {
420       chars[i] = 'A' + j;
421       g_tree_insert (tree, &chars[i], &chars[i]);
422     }
423   for (j = 0; j < 26; j++, i++)
424     {
425       chars[i] = 'a' + j;
426       g_tree_insert (tree, &chars[i], &chars[i]);
427     }
428
429   g_assert_cmpint (g_tree_nnodes (tree), ==, 10 + 26 + 26);
430   g_assert_cmpint (g_tree_height (tree), ==, 6);
431
432   if (g_test_verbose())
433     {
434       g_print ("tree: ");
435       g_tree_foreach (tree, my_traverse, NULL);
436       g_print ("\n");
437     }
438
439   for (i = 0; i < 10; i++)
440     g_tree_remove (tree, &chars[i]);
441
442   g_assert_cmpint (g_tree_nnodes (tree), ==, 26 + 26);
443   g_assert_cmpint (g_tree_height (tree), ==, 6);
444
445   if (g_test_verbose())
446     {
447       g_print ("tree: ");
448       g_tree_foreach (tree, my_traverse, NULL);
449       g_print ("\n");
450     }
451 }
452
453 static gboolean
454 my_hash_callback_remove (gpointer key,
455                          gpointer value,
456                          gpointer user_data)
457 {
458   int *d = value;
459
460   if ((*d) % 2)
461     return TRUE;
462
463   return FALSE;
464 }
465
466 static void
467 my_hash_callback_remove_test (gpointer key,
468                               gpointer value,
469                               gpointer user_data)
470 {
471   int *d = value;
472
473   if ((*d) % 2)
474     g_print ("bad!\n");
475 }
476
477 static void
478 my_hash_callback (gpointer key,
479                   gpointer value,
480                   gpointer user_data)
481 {
482   int *d = value;
483   *d = 1;
484 }
485
486 static guint
487 my_hash (gconstpointer key)
488 {
489   return (guint) *((const gint*) key);
490 }
491
492 static gboolean
493 my_hash_equal (gconstpointer a,
494                gconstpointer b)
495 {
496   return *((const gint*) a) == *((const gint*) b);
497 }
498
499 static gboolean 
500 find_first_that(gpointer key, 
501                 gpointer value, 
502                 gpointer user_data)
503 {
504   gint *v = value;
505   gint *test = user_data;
506   return (*v == *test);
507 }
508
509
510 static void
511 test_g_mkdir_with_parents_1 (const gchar *base)
512 {
513   char *p0 = g_build_filename (base, "fum", NULL);
514   char *p1 = g_build_filename (p0, "tem", NULL);
515   char *p2 = g_build_filename (p1, "zap", NULL);
516   FILE *f;
517
518   g_remove (p2);
519   g_remove (p1);
520   g_remove (p0);
521
522   if (g_file_test (p0, G_FILE_TEST_EXISTS))
523     g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p0);
524
525   if (g_file_test (p1, G_FILE_TEST_EXISTS))
526     g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p1);
527
528   if (g_file_test (p2, G_FILE_TEST_EXISTS))
529     g_error ("failed, %s exists, cannot test g_mkdir_with_parents\n", p2);
530
531   if (g_mkdir_with_parents (p2, 0777) == -1)
532     g_error ("failed, g_mkdir_with_parents(%s) failed: %s\n", p2, g_strerror (errno));
533
534   if (!g_file_test (p2, G_FILE_TEST_IS_DIR))
535     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p2);
536
537   if (!g_file_test (p1, G_FILE_TEST_IS_DIR))
538     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p1);
539
540   if (!g_file_test (p0, G_FILE_TEST_IS_DIR))
541     g_error ("failed, g_mkdir_with_parents(%s) succeeded, but %s is not a directory\n", p2, p0);
542
543   g_rmdir (p2);
544   if (g_file_test (p2, G_FILE_TEST_EXISTS))
545     g_error ("failed, did g_rmdir(%s), but %s is still there\n", p2, p2);
546
547   g_rmdir (p1);
548   if (g_file_test (p1, G_FILE_TEST_EXISTS))
549     g_error ("failed, did g_rmdir(%s), but %s is still there\n", p1, p1);
550
551   f = g_fopen (p1, "w");
552   if (f == NULL)
553     g_error ("failed, couldn't create file %s\n", p1);
554   fclose (f);
555
556   if (g_mkdir_with_parents (p1, 0666) == 0)
557     g_error ("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p1, p1);
558
559   if (g_mkdir_with_parents (p2, 0666) == 0)
560     g_error("failed, g_mkdir_with_parents(%s) succeeded, even if %s is a file\n", p2, p1);
561
562   g_remove (p2);
563   g_remove (p1);
564   g_remove (p0);
565 }
566
567 static void
568 test_g_mkdir_with_parents (void)
569 {
570   gchar *cwd;
571   if (g_test_verbose())
572     g_print ("checking g_mkdir_with_parents() in subdir ./hum/");
573   test_g_mkdir_with_parents_1 ("hum");
574   g_remove ("hum");
575   if (g_test_verbose())
576     g_print ("checking g_mkdir_with_parents() in subdir ./hii///haa/hee/");
577   test_g_mkdir_with_parents_1 ("hii///haa/hee");
578   g_remove ("hii/haa/hee");
579   g_remove ("hii/haa");
580   g_remove ("hii");
581   cwd = g_get_current_dir ();
582   if (g_test_verbose())
583     g_print ("checking g_mkdir_with_parents() in cwd: %s", cwd);
584   test_g_mkdir_with_parents_1 (cwd);
585   g_free (cwd);
586 }
587
588 static void
589 test_g_parse_debug_string (void)
590 {
591   GDebugKey keys[3] = { 
592     { "foo", 1 },
593     { "bar", 2 },
594     { "baz", 4 }
595   };
596   guint n_keys = 3;
597   guint result;
598   
599   result = g_parse_debug_string ("bar:foo:blubb", keys, n_keys);
600   g_assert (result == 3);
601
602   result = g_parse_debug_string (":baz::_E@~!_::", keys, n_keys);
603   g_assert (result == 4);
604
605   result = g_parse_debug_string ("", keys, n_keys);
606   g_assert (result == 0);
607
608   result = g_parse_debug_string (" : ", keys, n_keys);
609   g_assert (result == 0);
610 }
611
612 static void
613 log_warning_error_tests (void)
614 {
615   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
616     {
617       g_message ("this is a g_message test.");
618       g_message ("non-printable UTF-8: \"\xc3\xa4\xda\x85\"");
619       g_message ("unsafe chars: \"\x10\x11\x12\n\t\x7f\x81\x82\x83\"");
620       exit (0);
621     }
622   g_test_trap_assert_passed();
623   g_test_trap_assert_stderr ("*is a g_message test*");
624   g_test_trap_assert_stderr ("*non-printable UTF-8*");
625   g_test_trap_assert_stderr ("*unsafe chars*");
626   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
627     {
628       g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
629       exit (0);
630     }
631   g_test_trap_assert_failed(); /* we have fatal-warnings enabled */
632   g_test_trap_assert_stderr ("*harmless warning*");
633   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
634     {
635       g_print (NULL);
636       exit (0);
637     }
638   g_test_trap_assert_failed(); /* we have fatal-warnings enabled */
639   g_test_trap_assert_stderr ("*g_print*assertion*failed*");
640   g_test_trap_assert_stderr ("*NULL*");
641 }
642
643 static void
644 timer_tests (void)
645 {
646   GTimer *timer, *timer2;
647   gdouble elapsed;
648
649   /* basic testing */
650   timer = g_timer_new ();
651   g_timer_start (timer);
652   elapsed = g_timer_elapsed (timer, NULL);
653   g_timer_stop (timer);
654   g_assert_cmpfloat (elapsed, <=, g_timer_elapsed (timer, NULL));
655   g_timer_destroy (timer);
656
657   if (g_test_slow())
658     {
659       if (g_test_verbose())
660         g_print ("checking timers...\n");
661       timer = g_timer_new ();
662       if (g_test_verbose())
663         g_print ("  spinning for 3 seconds...\n");
664       g_timer_start (timer);
665       while (g_timer_elapsed (timer, NULL) < 3)
666         ;
667       g_timer_stop (timer);
668       g_timer_destroy (timer);
669       if (g_test_verbose())
670         g_print ("ok\n");
671     }
672
673   if (g_test_slow())
674     {
675       gulong elapsed_usecs;
676       if (g_test_verbose())
677         g_print ("checking g_timer_continue...\n");
678       timer2 = g_timer_new ();
679       if (g_test_verbose())
680         g_print ("\trun for 1 second...\n");
681       timer = g_timer_new();
682       g_usleep (G_USEC_PER_SEC); /* run timer for 1 second */
683       g_timer_stop (timer);
684       if (g_test_verbose())
685         g_print ("\tstop for 1 second...\n");
686       g_usleep (G_USEC_PER_SEC); /* wait for 1 second */
687       if (g_test_verbose())
688         g_print ("\trun for 2 seconds...\n");
689       g_timer_continue (timer);
690       g_usleep (2 * G_USEC_PER_SEC); /* run timer for 2 seconds */
691       g_timer_stop(timer);
692       if (g_test_verbose())
693         g_print ("\tstop for 1.5 seconds...\n");
694       g_usleep ((3 * G_USEC_PER_SEC) / 2); /* wait for 1.5 seconds */
695       if (g_test_verbose())
696         g_print ("\trun for 0.2 seconds...\n");
697       g_timer_continue (timer);
698       g_usleep (G_USEC_PER_SEC / 5); /* run timer for 0.2 seconds */
699       g_timer_stop (timer);
700       if (g_test_verbose())
701         g_print ("\tstop for 4 seconds...\n");
702       g_usleep (4 * G_USEC_PER_SEC); /* wait for 4 seconds */
703       if (g_test_verbose())
704         g_print ("\trun for 5.8 seconds...\n");
705       g_timer_continue (timer);
706       g_usleep ((29 * G_USEC_PER_SEC) / 5); /* run timer for 5.8 seconds */
707       g_timer_stop(timer);
708       elapsed = g_timer_elapsed (timer, &elapsed_usecs);
709       if (g_test_verbose())
710         g_print ("\t=> timer = %.6f = %d.%06ld (should be: 9.000000) (%.6f off)\n", elapsed, (int) elapsed, elapsed_usecs, ABS (elapsed - 9.));
711       g_assert_cmpfloat (elapsed, >, 8.8);
712       g_assert_cmpfloat (elapsed, <, 9.2);
713       if (g_test_verbose())
714         g_print ("g_timer_continue ... ok\n\n");
715       g_timer_stop (timer2);
716       elapsed = g_timer_elapsed (timer2, &elapsed_usecs);
717       if (g_test_verbose())
718         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)));
719       g_assert_cmpfloat (elapsed, >, 8.8 + 6.5);
720       g_assert_cmpfloat (elapsed, <, 9.2 + 6.5);
721       if (g_test_verbose())
722         g_print ("timer2 ... ok\n\n");
723       g_timer_destroy (timer);
724       g_timer_destroy (timer2);
725     }
726 }
727
728 static void
729 type_sizes (void)
730 {
731   guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
732   guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
733   guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
734           gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
735   /* type sizes */
736   g_assert_cmpint (sizeof (gint8), ==, 1);
737   g_assert_cmpint (sizeof (gint16), ==, 2);
738   g_assert_cmpint (sizeof (gint32), ==, 4);
739   g_assert_cmpint (sizeof (gint64), ==, 8);
740   /* endian macros */
741   if (g_test_verbose())
742     g_print ("checking endian macros (host is %s)...\n",
743              G_BYTE_ORDER == G_BIG_ENDIAN ? "big endian" : "little endian");
744   g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
745   g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
746   g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
747 }
748
749 static void
750 test_info (void)
751 {
752   const gchar *un, *rn, *hn;
753   const gchar *tmpdir, *homedir, *userdatadir, *uconfdir, *ucachedir;
754   const gchar *uddesktop, *udddocs, *uddpubshare;
755   gchar **sv, *cwd, *sdatadirs, *sconfdirs, *langnames;
756   if (g_test_verbose())
757     g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
758              glib_major_version,
759              glib_minor_version,
760              glib_micro_version,
761              glib_interface_age,
762              glib_binary_age);
763
764   cwd = g_get_current_dir ();
765   un = g_get_user_name();
766   rn = g_get_real_name();
767   hn = g_get_host_name();
768   if (g_test_verbose())
769     {
770       g_print ("cwd: %s\n", cwd);
771       g_print ("user: %s\n", un);
772       g_print ("real: %s\n", rn);
773       g_print ("host: %s\n", hn);
774     }
775   g_free (cwd);
776
777   tmpdir = g_get_tmp_dir();
778   g_assert (tmpdir != NULL);
779   homedir = g_get_home_dir ();
780   g_assert (homedir != NULL);
781   userdatadir = g_get_user_data_dir ();
782   g_assert (userdatadir != NULL);
783   uconfdir = g_get_user_config_dir ();
784   g_assert (uconfdir != NULL);
785   ucachedir = g_get_user_cache_dir ();
786   g_assert (ucachedir != NULL);
787
788   uddesktop = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
789   g_assert (uddesktop != NULL);
790
791   sv = (gchar **) g_get_system_data_dirs ();
792   sdatadirs = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv);
793   sv = (gchar **) g_get_system_config_dirs ();
794   sconfdirs = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv);
795   sv = (gchar **) g_get_language_names ();
796   langnames = g_strjoinv (":", sv);
797
798   if (g_test_verbose())
799     {
800       g_print ("tmp-dir: %s\n", tmpdir);
801       g_print ("home: %s\n", homedir);
802       g_print ("user_data: %s\n", userdatadir);
803       g_print ("user_config: %s\n", uconfdir);
804       g_print ("user_cache: %s\n", ucachedir);
805       g_print ("system_data: %s\n", sdatadirs);
806       g_print ("system_config: %s\n", sconfdirs);
807       g_print ("languages: %s\n", langnames);
808       g_print ("user_special[DESKTOP]: %s\n", uddesktop);
809       g_print ("user_special[DOCUMENTS]: %s\n", udddocs);
810       g_print ("user_special[PUBLIC_SHARE]: %s\n", uddpubshare);
811     }
812   g_free (sdatadirs);
813   g_free (sconfdirs);
814   g_free (langnames);
815   
816   if (g_test_verbose())
817     {
818 #ifdef G_PLATFORM_WIN32
819       gchar *glib_dll;
820 #endif
821       const gchar *charset;
822       if (g_get_charset ((G_CONST_RETURN char**)&charset))
823         g_print ("current charset is UTF-8: %s\n", charset);
824       else
825         g_print ("current charset is not UTF-8: %s\n", charset);
826
827 #ifdef G_PLATFORM_WIN32
828 #ifdef G_OS_WIN32
829       /* Can't calculate GLib DLL name at runtime. */
830       glib_dll = "libglib-2.0-0.dll";
831 #endif
832 #ifdef G_WITH_CYGWIN
833       glib_dll = "cygglib-2.0-0.dll";
834 #endif
835
836       g_print ("current locale: %s\n", g_win32_getlocale ());
837       g_print ("GLib DLL name tested for: %s\n", glib_dll);
838
839       g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
840                GETTEXT_PACKAGE,
841                g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
842       g_print ("Ditto, or from GLib DLL name: %s\n",
843                g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
844       g_print ("Ditto, only from GLib DLL name: %s\n",
845                g_win32_get_package_installation_directory (NULL, glib_dll));
846       g_print ("locale subdirectory of GLib installation directory: %s\n",
847                g_win32_get_package_installation_subdirectory (NULL, glib_dll, "lib\\locale"));
848       g_print ("GTK+ 2.0 installation directory, if available: %s\n",
849                g_win32_get_package_installation_directory ("gtk20", NULL));
850
851       g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
852       g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
853
854       g_print ("a Win32 error message: %s\n", g_win32_error_message (2));
855 #endif
856     }
857 }
858
859 static void
860 test_paths (void)
861 {
862   struct {
863     gchar *filename;
864     gchar *dirname;
865   } dirname_checks[] = {
866     { "/", "/" },
867     { "////", "/" },
868     { ".////", "." },
869     { "../", ".." },
870     { "..////", ".." },
871     { "a/b", "a" },
872     { "a/b/", "a/b" },
873     { "c///", "c" },
874 #ifdef G_OS_WIN32
875     { "\\", "\\" },
876     { ".\\\\\\\\", "." },
877     { "..\\", ".." },
878     { "..\\\\\\\\", ".." },
879     { "a\\b", "a" },
880     { "a\\b/", "a\\b" },
881     { "a/b\\", "a/b" },
882     { "c\\\\/", "c" },
883     { "//\\", "/" },
884 #endif
885 #ifdef G_WITH_CYGWIN
886     { "//server/share///x", "//server/share" },
887 #endif
888     { ".", "." },
889     { "..", "." },
890     { "", "." },
891   };
892   const guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
893   struct {
894     gchar *filename;
895     gchar *without_root;
896   } skip_root_checks[] = {
897     { "/", "" },
898     { "//", "" },
899     { "/foo", "foo" },
900     { "//foo", "foo" },
901     { "a/b", NULL },
902 #ifdef G_OS_WIN32
903     { "\\", "" },
904     { "\\foo", "foo" },
905     { "\\\\server\\foo", "" },
906     { "\\\\server\\foo\\bar", "bar" },
907     { "a\\b", NULL },
908 #endif
909 #ifdef G_WITH_CYGWIN
910     { "//server/share///x", "//x" },
911 #endif
912     { ".", NULL },
913     { "", NULL },
914   };
915   const guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
916   gchar *string;
917   guint i;
918   if (g_test_verbose())
919     g_print ("checking g_path_get_basename()...");
920   string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
921   g_assert (strcmp (string, "dir") == 0);
922   g_free (string);
923   string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
924   g_assert (strcmp (string, "file") == 0);
925   g_free (string);
926   if (g_test_verbose())
927     g_print ("ok\n");
928
929 #ifdef G_OS_WIN32
930   string = g_path_get_basename ("/foo/dir/");
931   g_assert (strcmp (string, "dir") == 0);
932   g_free (string);
933   string = g_path_get_basename ("/foo/file");
934   g_assert (strcmp (string, "file") == 0);
935   g_free (string);
936 #endif
937
938   if (g_test_verbose())
939     g_print ("checking g_path_get_dirname()...");
940   for (i = 0; i < n_dirname_checks; i++)
941     {
942       gchar *dirname = g_path_get_dirname (dirname_checks[i].filename);
943       if (strcmp (dirname, dirname_checks[i].dirname) != 0)
944         {
945           g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
946                    dirname_checks[i].filename,
947                    dirname_checks[i].dirname,
948                    dirname);
949         }
950       g_free (dirname);
951     }
952   if (g_test_verbose())
953     g_print ("ok\n");
954
955   if (g_test_verbose())
956     g_print ("checking g_path_skip_root()...");
957   for (i = 0; i < n_skip_root_checks; i++)
958     {
959       const gchar *skipped = g_path_skip_root (skip_root_checks[i].filename);
960       if ((skipped && !skip_root_checks[i].without_root) ||
961           (!skipped && skip_root_checks[i].without_root) ||
962           ((skipped && skip_root_checks[i].without_root) &&
963            strcmp (skipped, skip_root_checks[i].without_root)))
964         {
965           g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
966                    skip_root_checks[i].filename,
967                    (skip_root_checks[i].without_root ?
968                     skip_root_checks[i].without_root : "<NULL>"),
969                    (skipped ? skipped : "<NULL>"));
970         }
971     }
972   if (g_test_verbose())
973     g_print ("ok\n");
974 }
975
976 static void
977 test_file_functions (void)
978 {
979   const char hello[] = "Hello, World";
980   const int hellolen = sizeof (hello) - 1;
981   GError *error;
982   char template[32];
983   char *name_used, chars[62];
984   gint fd, n;
985   
986   strcpy (template, "foobar");
987   fd = g_mkstemp (template);
988   if (g_test_verbose() && fd != -1)
989     g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
990   close (fd);
991   strcpy (template, "fooXXXXXX");
992   fd = g_mkstemp (template);
993   if (fd == -1)
994     g_error ("g_mkstemp didn't work for template %s\n", template);
995   n = write (fd, hello, hellolen);
996   if (n == -1)
997     g_error ("write() failed: %s\n", g_strerror (errno));
998   else if (n != hellolen)
999     g_error ("write() should have written %d bytes, wrote %d\n", hellolen, n);
1000
1001   lseek (fd, 0, 0);
1002   n = read (fd, chars, sizeof (chars));
1003   if (n == -1)
1004     g_error ("read() failed: %s\n", g_strerror (errno));
1005   else if (n != hellolen)
1006     g_error ("read() should have read %d bytes, got %d\n", hellolen, n);
1007
1008   chars[n] = 0;
1009   if (strcmp (chars, hello) != 0)
1010     g_error ("wrote '%s', but got '%s'\n", hello, chars);
1011
1012   close (fd);
1013   remove (template);
1014
1015   error = NULL;
1016   strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1017   fd = g_file_open_tmp (template, &name_used, &error);
1018   if (g_test_verbose())
1019     {
1020       if (fd != -1)
1021         g_print ("g_file_open_tmp works even if template contains '%s'\n", G_DIR_SEPARATOR_S);
1022       else
1023         g_print ("g_file_open_tmp correctly returns error: %s\n", error->message);
1024     }
1025   close (fd);
1026   g_clear_error (&error);
1027
1028 #ifdef G_OS_WIN32
1029   strcpy (template, "zap/barXXXXXX");
1030   fd = g_file_open_tmp (template, &name_used, &error);
1031   if (g_test_verbose())
1032     {
1033       if (fd != -1)
1034         g_print ("g_file_open_tmp works even if template contains '/'\n");
1035       else
1036         g_print ("g_file_open_tmp correctly returns error: %s\n", error->message);
1037     }
1038   close (fd);
1039   g_clear_error (&error);
1040 #endif
1041
1042   strcpy (template, "zapXXXXXX");
1043   fd = g_file_open_tmp (template, &name_used, &error);
1044   if (fd == -1)
1045     g_error ("g_file_open_tmp didn't work for template '%s': %s\n", template, error->message);
1046   else if (g_test_verbose())
1047     g_print ("g_file_open_tmp for template '%s' used name '%s'\n", template, name_used);
1048   close (fd);
1049   g_clear_error (&error);
1050   remove (name_used);
1051
1052   fd = g_file_open_tmp (NULL, &name_used, &error);
1053   if (fd == -1)
1054     g_error ("g_file_open_tmp didn't work for a NULL template: %s\n", error->message);
1055   close (fd);
1056   g_clear_error (&error);
1057   remove (name_used);
1058 }
1059
1060 static void
1061 test_arrays (void)
1062 {
1063   GByteArray *gbarray;
1064   GPtrArray *gparray;
1065   GArray *garray;
1066   guint i;
1067
1068   gparray = g_ptr_array_new ();
1069   for (i = 0; i < 10000; i++)
1070     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1071   for (i = 0; i < 10000; i++)
1072     if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1073       g_error ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1074   g_ptr_array_free (gparray, TRUE);
1075
1076   gbarray = g_byte_array_new ();
1077   for (i = 0; i < 10000; i++)
1078     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1079   for (i = 0; i < 10000; i++)
1080     {
1081       g_assert (gbarray->data[4*i] == 'a');
1082       g_assert (gbarray->data[4*i+1] == 'b');
1083       g_assert (gbarray->data[4*i+2] == 'c');
1084       g_assert (gbarray->data[4*i+3] == 'd');
1085     }
1086   g_byte_array_free (gbarray, TRUE);
1087
1088   garray = g_array_new (FALSE, FALSE, sizeof (gint));
1089   for (i = 0; i < 10000; i++)
1090     g_array_append_val (garray, i);
1091   for (i = 0; i < 10000; i++)
1092     if (g_array_index (garray, gint, i) != i)
1093       g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), i);
1094   g_array_free (garray, TRUE);
1095
1096   garray = g_array_new (FALSE, FALSE, sizeof (gint));
1097   for (i = 0; i < 100; i++)
1098     g_array_prepend_val (garray, i);
1099   for (i = 0; i < 100; i++)
1100     if (g_array_index (garray, gint, i) != (100 - i - 1))
1101       g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
1102   g_array_free (garray, TRUE);
1103 }
1104
1105 static void
1106 hash_table_tests (void)
1107 {
1108   GHashTable *hash_table;
1109   int array[10000];
1110   gint *pvalue = NULL;
1111   gint value = 120;
1112   guint i;
1113
1114   hash_table = g_hash_table_new (my_hash, my_hash_equal);
1115   for (i = 0; i < 10000; i++)
1116     {
1117       array[i] = i;
1118       g_hash_table_insert (hash_table, &array[i], &array[i]);
1119     }
1120   pvalue = g_hash_table_find (hash_table, find_first_that, &value);
1121   if (*pvalue != value)
1122     g_error ("g_hash_table_find failed");
1123   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
1124   for (i = 0; i < 10000; i++)
1125     if (array[i] == 0)
1126       g_error ("hashtable-test: wrong value: %d\n", i);
1127   for (i = 0; i < 10000; i++)
1128     g_hash_table_remove (hash_table, &array[i]);
1129   for (i = 0; i < 10000; i++)
1130     {
1131       array[i] = i;
1132       g_hash_table_insert (hash_table, &array[i], &array[i]);
1133     }
1134   if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
1135       g_hash_table_size (hash_table) != 5000)
1136     g_error ("hashtable removal failed\n");
1137   g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
1138   g_hash_table_destroy (hash_table);
1139 }
1140
1141 static void
1142 relation_test (void)
1143 {
1144   GRelation *relation = g_relation_new (2);
1145   GTuples *tuples;
1146   gint data [1024];
1147   guint i;
1148
1149   g_relation_index (relation, 0, g_int_hash, g_int_equal);
1150   g_relation_index (relation, 1, g_int_hash, g_int_equal);
1151
1152   for (i = 0; i < 1024; i += 1)
1153     data[i] = i;
1154
1155   for (i = 1; i < 1023; i += 1)
1156     {
1157       g_relation_insert (relation, data + i, data + i + 1);
1158       g_relation_insert (relation, data + i, data + i - 1);
1159     }
1160
1161   for (i = 2; i < 1022; i += 1)
1162     {
1163       g_assert (! g_relation_exists (relation, data + i, data + i));
1164       g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1165       g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1166     }
1167
1168   for (i = 1; i < 1023; i += 1)
1169     {
1170       g_assert (g_relation_exists (relation, data + i, data + i + 1));
1171       g_assert (g_relation_exists (relation, data + i, data + i - 1));
1172     }
1173
1174   for (i = 2; i < 1022; i += 1)
1175     {
1176       g_assert (g_relation_count (relation, data + i, 0) == 2);
1177       g_assert (g_relation_count (relation, data + i, 1) == 2);
1178     }
1179
1180   g_assert (g_relation_count (relation, data, 0) == 0);
1181
1182   g_assert (g_relation_count (relation, data + 42, 0) == 2);
1183   g_assert (g_relation_count (relation, data + 43, 1) == 2);
1184   g_assert (g_relation_count (relation, data + 41, 1) == 2);
1185   g_relation_delete (relation, data + 42, 0);
1186   g_assert (g_relation_count (relation, data + 42, 0) == 0);
1187   g_assert (g_relation_count (relation, data + 43, 1) == 1);
1188   g_assert (g_relation_count (relation, data + 41, 1) == 1);
1189
1190   tuples = g_relation_select (relation, data + 200, 0);
1191
1192   g_assert (tuples->len == 2);
1193
1194 #if 0
1195   for (i = 0; i < tuples->len; i += 1)
1196     {
1197       printf ("%d %d\n",
1198               *(gint*) g_tuples_index (tuples, i, 0),
1199               *(gint*) g_tuples_index (tuples, i, 1));
1200     }
1201 #endif
1202
1203   g_assert (g_relation_exists (relation, data + 300, data + 301));
1204   g_relation_delete (relation, data + 300, 0);
1205   g_assert (!g_relation_exists (relation, data + 300, data + 301));
1206
1207   g_tuples_destroy (tuples);
1208
1209   g_relation_destroy (relation);
1210
1211   relation = NULL;
1212 }
1213
1214 static void
1215 gstring_tests (void)
1216 {
1217   GString *string1, *string2;
1218   guint i;
1219
1220   if (g_test_verbose())
1221     g_print ("test GString basics\n");
1222
1223   string1 = g_string_new ("hi pete!");
1224   string2 = g_string_new ("");
1225
1226   g_assert (strcmp ("hi pete!", string1->str) == 0);
1227
1228   for (i = 0; i < 10000; i++)
1229     g_string_append_c (string1, 'a'+(i%26));
1230
1231 #ifndef G_OS_WIN32
1232   /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
1233      the %10000.10000f format... */
1234   g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
1235                    "this pete guy sure is a wuss, like he's the number ",
1236                    1,
1237                    " wuss.  everyone agrees.\n",
1238                    string1->str,
1239                    10, 666, 15, 15, 666.666666666, 666.666666666);
1240 #else
1241   g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
1242                    "this pete guy sure is a wuss, like he's the number ",
1243                    1,
1244                    " wuss.  everyone agrees.\n",
1245                    string1->str,
1246                    10, 666, 15, 15, 666.666666666, 666.666666666);
1247 #endif
1248
1249   if (g_test_verbose())
1250     g_print ("string2 length = %lu...\n", (gulong)string2->len);
1251   string2->str[70] = '\0';
1252   if (g_test_verbose())
1253     g_print ("first 70 chars:\n%s\n", string2->str);
1254   string2->str[141] = '\0';
1255   if (g_test_verbose())
1256     g_print ("next 70 chars:\n%s\n", string2->str+71);
1257   string2->str[212] = '\0';
1258   if (g_test_verbose())
1259     g_print ("and next 70:\n%s\n", string2->str+142);
1260   if (g_test_verbose())
1261     g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
1262
1263   g_string_free (string1, TRUE);
1264   g_string_free (string2, TRUE);
1265
1266   /* append */
1267   string1 = g_string_new ("firsthalf");
1268   g_string_append (string1, "lasthalf");
1269   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1270   g_string_free (string1, TRUE);
1271
1272   /* append_len */
1273   string1 = g_string_new ("firsthalf");
1274   g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
1275   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1276   g_string_free (string1, TRUE);
1277
1278   /* prepend */
1279   string1 = g_string_new ("lasthalf");
1280   g_string_prepend (string1, "firsthalf");
1281   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1282   g_string_free (string1, TRUE);
1283
1284   /* prepend_len */
1285   string1 = g_string_new ("lasthalf");
1286   g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
1287   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1288   g_string_free (string1, TRUE);
1289
1290   /* insert */
1291   string1 = g_string_new ("firstlast");
1292   g_string_insert (string1, 5, "middle");
1293   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1294   g_string_free (string1, TRUE);
1295
1296   /* insert with pos == end of the string */
1297   string1 = g_string_new ("firstmiddle");
1298   g_string_insert (string1, strlen ("firstmiddle"), "last");
1299   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1300   g_string_free (string1, TRUE);
1301
1302   /* insert_len */
1303   string1 = g_string_new ("firstlast");
1304   g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
1305   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1306   g_string_free (string1, TRUE);
1307
1308   /* insert_len with magic -1 pos for append */
1309   string1 = g_string_new ("first");
1310   g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
1311   g_assert (strcmp (string1->str, "firstlast") == 0);
1312   g_string_free (string1, TRUE);
1313
1314   /* insert_len with magic -1 len for strlen-the-string */
1315   string1 = g_string_new ("first");
1316   g_string_insert_len (string1, 5, "last", -1);
1317   g_assert (strcmp (string1->str, "firstlast") == 0);
1318   g_string_free (string1, TRUE);
1319
1320   /* g_string_equal */
1321   string1 = g_string_new ("test");
1322   string2 = g_string_new ("te");
1323   g_assert (! g_string_equal(string1, string2));
1324   g_string_append (string2, "st");
1325   g_assert (g_string_equal(string1, string2));
1326   g_string_free (string1, TRUE);
1327   g_string_free (string2, TRUE);
1328
1329   /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
1330   if (g_test_verbose())
1331     g_print ("test embedded ASCII 0 (NUL) characters in GString\n");
1332   string1 = g_string_new ("fiddle");
1333   string2 = g_string_new ("fiddle");
1334   g_assert (g_string_equal(string1, string2));
1335   g_string_append_c(string1, '\0');
1336   g_assert (! g_string_equal(string1, string2));
1337   g_string_append_c(string2, '\0');
1338   g_assert (g_string_equal(string1, string2));
1339   g_string_append_c(string1, 'x');
1340   g_string_append_c(string2, 'y');
1341   g_assert (! g_string_equal(string1, string2));
1342   g_assert (string1->len == 8);
1343   g_string_append(string1, "yzzy");
1344   g_assert (string1->len == 12);
1345   g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
1346   g_string_insert(string1, 1, "QED");
1347   g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
1348   g_string_free (string1, TRUE);
1349   g_string_free (string2, TRUE);
1350 }
1351
1352 static void
1353 various_string_tests (void)
1354 {
1355   GStringChunk *string_chunk;
1356   GTimeVal ref_date, date;
1357   gchar *tmp_string = NULL, *tmp_string_2, *string, *date_str;
1358   guint i;
1359
1360   if (g_test_verbose())
1361     g_print ("checking string chunks...");
1362   string_chunk = g_string_chunk_new (1024);
1363   for (i = 0; i < 100000; i ++)
1364     {
1365       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
1366       if (strcmp ("hi pete", tmp_string) != 0)
1367         g_error ("string chunks are broken.\n");
1368     }
1369   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
1370   g_assert (tmp_string_2 != tmp_string && strcmp (tmp_string_2, tmp_string) == 0);
1371   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
1372   g_assert (tmp_string_2 == tmp_string);
1373   g_string_chunk_free (string_chunk);
1374
1375   if (g_test_verbose())
1376     g_print ("test positional printf formats (not supported):");
1377   string = g_strdup_printf ("%.*s%s", 5, "a", "b");
1378   tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
1379   if (g_test_verbose())
1380     g_print ("%s%s\n", string, tmp_string);
1381   g_free (tmp_string);
1382   g_free (string);
1383
1384 #define REF_INVALID  "Wed Dec 19 17:20:20 GMT 2007"
1385 #define REF_SEC_UTC  320063760
1386 #define REF_STR_UTC  "1980-02-22T10:36:00Z"
1387 #define REF_STR_CEST "1980-02-22T12:36:00+02:00"
1388 #define REF_STR_EST  "1980-02-22T05:36:00-05:00"
1389
1390   if (g_test_verbose())
1391     g_print ("checking g_time_val_from_iso8601...\n");
1392   ref_date.tv_sec = REF_SEC_UTC;
1393   ref_date.tv_usec = 0;
1394   g_assert (g_time_val_from_iso8601 (REF_INVALID, &date) == FALSE);
1395   g_assert (g_time_val_from_iso8601 (REF_STR_UTC, &date) != FALSE);
1396   if (g_test_verbose())
1397     g_print ("\t=> UTC stamp = %ld (should be: %ld) (%ld off)\n", date.tv_sec, ref_date.tv_sec, date.tv_sec - ref_date.tv_sec);
1398   g_assert (date.tv_sec == ref_date.tv_sec);
1399
1400   g_assert (g_time_val_from_iso8601 (REF_STR_CEST, &date) != FALSE);
1401   if (g_test_verbose())
1402     g_print ("\t=> CEST stamp = %ld (should be: %ld) (%ld off)\n", date.tv_sec, ref_date.tv_sec, date.tv_sec - ref_date.tv_sec);
1403   g_assert (date.tv_sec == ref_date.tv_sec);
1404
1405   g_assert (g_time_val_from_iso8601 (REF_STR_EST, &date) != FALSE);
1406   if (g_test_verbose())
1407     g_print ("\t=> EST stamp = %ld (should be: %ld) (%ld off)\n", date.tv_sec, ref_date.tv_sec, date.tv_sec - ref_date.tv_sec);
1408   g_assert (date.tv_sec == ref_date.tv_sec);
1409
1410   if (g_test_verbose())
1411     g_print ("checking g_time_val_to_iso8601...\n");
1412   ref_date.tv_sec = REF_SEC_UTC;
1413   ref_date.tv_usec = 1;
1414   date_str = g_time_val_to_iso8601 (&ref_date);
1415   g_assert (date_str != NULL);
1416   if (g_test_verbose())
1417     g_print ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_UTC);
1418   g_assert (strcmp (date_str, REF_STR_UTC) == 0);
1419   g_free (date_str);
1420
1421   if (g_test_verbose())
1422     g_print ("checking g_ascii_strcasecmp...");
1423   g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
1424   g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
1425   g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
1426   g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1427   g_assert (g_ascii_strcasecmp ("", "") == 0);
1428   g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
1429   g_assert (g_ascii_strcasecmp ("a", "b") < 0);
1430   g_assert (g_ascii_strcasecmp ("a", "B") < 0);
1431   g_assert (g_ascii_strcasecmp ("A", "b") < 0);
1432   g_assert (g_ascii_strcasecmp ("A", "B") < 0);
1433   g_assert (g_ascii_strcasecmp ("b", "a") > 0);
1434   g_assert (g_ascii_strcasecmp ("b", "A") > 0);
1435   g_assert (g_ascii_strcasecmp ("B", "a") > 0);
1436   g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1437
1438   if (g_test_verbose())
1439     g_print ("checking g_strdup...\n");
1440   g_assert (g_strdup (NULL) == NULL);
1441   string = g_strdup (GLIB_TEST_STRING);
1442   g_assert (string != NULL);
1443   g_assert (strcmp(string, GLIB_TEST_STRING) == 0);
1444   g_free (string);
1445
1446   if (g_test_verbose())
1447     g_print ("checking g_strconcat...\n");
1448   string = g_strconcat (GLIB_TEST_STRING, NULL);
1449   g_assert (string != NULL);
1450   g_assert (strcmp (string, GLIB_TEST_STRING) == 0);
1451   g_free (string);
1452   string = g_strconcat (GLIB_TEST_STRING, GLIB_TEST_STRING, 
1453                         GLIB_TEST_STRING, NULL);
1454   g_assert (string != NULL);
1455   g_assert (strcmp (string, GLIB_TEST_STRING GLIB_TEST_STRING
1456                     GLIB_TEST_STRING) == 0);
1457   g_free (string);
1458
1459   if (g_test_verbose())
1460     g_print ("checking g_strlcpy/g_strlcat...");
1461   /* The following is a torture test for strlcpy/strlcat, with lots of
1462    * checking; normal users wouldn't use them this way!
1463    */
1464   string = g_malloc (6);
1465   *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
1466   *string = 'q';
1467   g_assert (g_strlcpy(string, "" , 5) == 0);
1468   g_assert ( *string == '\0' );
1469   *string = 'q';
1470   g_assert (g_strlcpy(string, "abc" , 5) == 3);
1471   g_assert ( *(string + 3) == '\0' );
1472   g_assert (g_str_equal(string, "abc"));
1473   g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1474   g_assert ( *(string + 4) == '\0' );
1475   g_assert ( *(string + 5) == 'Z' );
1476   g_assert (g_str_equal(string, "abcd"));
1477   g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1478   g_assert ( *(string + 4) == '\0' );
1479   g_assert ( *(string + 5) == 'Z' );
1480   g_assert (g_str_equal(string, "abcd"));
1481   g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1482   g_assert ( *(string + 4) == '\0' );
1483   g_assert ( *(string + 5) == 'Z' );
1484   g_assert (g_str_equal(string, "abcd"));
1485   *string = 'Y';
1486   *(string + 1)= '\0';
1487   g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1488   g_assert (*string == 'Y');
1489   *string = '\0';
1490   g_assert (g_strlcat(string, "123" , 5) == 3);
1491   g_assert ( *(string + 3) == '\0' );
1492   g_assert (g_str_equal(string, "123"));
1493   g_assert (g_strlcat(string, "" , 5) == 3);
1494   g_assert ( *(string + 3) == '\0' );
1495   g_assert (g_str_equal(string, "123"));
1496   g_assert (g_strlcat(string, "4", 5) == 4);
1497   g_assert (g_str_equal(string, "1234"));
1498   g_assert (g_strlcat(string, "5", 5) == 5);
1499   g_assert ( *(string + 4) == '\0' );
1500   g_assert (g_str_equal(string, "1234"));
1501   g_assert ( *(string + 5) == 'Z' );
1502   *string = 'Y';
1503   *(string + 1)= '\0';
1504   g_assert (g_strlcat(string, "123" , 0) == 3);
1505   g_assert (*string == 'Y');
1506
1507   /* A few more tests, demonstrating more "normal" use  */
1508   g_assert (g_strlcpy(string, "hi", 5) == 2);
1509   g_assert (g_str_equal(string, "hi"));
1510   g_assert (g_strlcat(string, "t", 5) == 3);
1511   g_assert (g_str_equal(string, "hit"));
1512   g_free(string);
1513
1514   if (g_test_verbose())
1515     g_print ("checking g_strdup_printf...\n");
1516   string = g_strdup_printf ("%05d %-5s", 21, "test");
1517   g_assert (string != NULL);
1518   g_assert (strcmp(string, "00021 test ") == 0);
1519   g_free (string);
1520
1521   /* g_debug (argv[0]); */
1522 }
1523
1524 static void
1525 test_mem_chunks (void)
1526 {
1527   GMemChunk *mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
1528   gchar *mem[10000];
1529   guint i;
1530   for (i = 0; i < 10000; i++)
1531     {
1532       guint j;
1533       mem[i] = g_chunk_new (gchar, mem_chunk);
1534       for (j = 0; j < 50; j++)
1535         mem[i][j] = i * j;
1536     }
1537   for (i = 0; i < 10000; i++)
1538     g_mem_chunk_free (mem_chunk, mem[i]);
1539 }
1540
1541 int
1542 main (int   argc,
1543       char *argv[])
1544 {
1545   g_test_init (&argc, &argv, NULL);
1546
1547   g_test_add_func ("/testglib/Infos", test_info);
1548   g_test_add_func ("/testglib/Types Sizes", type_sizes);
1549   g_test_add_func ("/testglib/GStrings", gstring_tests);
1550   g_test_add_func ("/testglib/Various Strings", various_string_tests);
1551   g_test_add_func ("/testglib/GList", glist_test);
1552   g_test_add_func ("/testglib/GSList", gslist_test);
1553   g_test_add_func ("/testglib/GNode", gnode_test);
1554   g_test_add_func ("/testglib/GTree", binary_tree_test);
1555   g_test_add_func ("/testglib/Arrays", test_arrays);
1556   g_test_add_func ("/testglib/GHashTable", hash_table_tests);
1557   g_test_add_func ("/testglib/Relation", relation_test);
1558   g_test_add_func ("/testglib/File Paths", test_paths);
1559   g_test_add_func ("/testglib/File Functions", test_file_functions);
1560   g_test_add_func ("/testglib/Mkdir", test_g_mkdir_with_parents);
1561   g_test_add_func ("/testglib/Parse Debug Strings", test_g_parse_debug_string);
1562   g_test_add_func ("/testglib/GMemChunk (deprecated)", test_mem_chunks);
1563   g_test_add_func ("/testglib/Warnings & Errors", log_warning_error_tests);
1564   g_test_add_func ("/testglib/Timers (slow)", timer_tests);
1565
1566   return g_test_run();
1567 }