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