Tizen 2.1 base
[platform/upstream/glib2.0.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 <glib/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) ((gintptr) (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) ((gintptr) (p)))
259   GNode *root;
260   GNode *node;
261   GNode *node_B;
262   GNode *node_F;
263   GNode *node_G;
264   GNode *node_J;
265   guint i;
266   gchar *tstring, *cstring;
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   g_tree_unref (tree);
453 }
454
455 static gboolean
456 my_hash_callback_remove (gpointer key,
457                          gpointer value,
458                          gpointer user_data)
459 {
460   int *d = value;
461
462   if ((*d) % 2)
463     return TRUE;
464
465   return FALSE;
466 }
467
468 static void
469 my_hash_callback_remove_test (gpointer key,
470                               gpointer value,
471                               gpointer user_data)
472 {
473   int *d = value;
474
475   if ((*d) % 2)
476     g_print ("bad!\n");
477 }
478
479 static void
480 my_hash_callback (gpointer key,
481                   gpointer value,
482                   gpointer user_data)
483 {
484   int *d = value;
485   *d = 1;
486 }
487
488 static guint
489 my_hash (gconstpointer key)
490 {
491   return (guint) *((const gint*) key);
492 }
493
494 static gboolean
495 my_hash_equal (gconstpointer a,
496                gconstpointer b)
497 {
498   return *((const gint*) a) == *((const gint*) b);
499 }
500
501 static gboolean 
502 find_first_that(gpointer key, 
503                 gpointer value, 
504                 gpointer user_data)
505 {
506   gint *v = value;
507   gint *test = user_data;
508   return (*v == *test);
509 }
510
511 static void
512 test_g_parse_debug_string (void)
513 {
514   GDebugKey keys[] = { 
515     { "foo", 1 },
516     { "bar", 2 },
517     { "baz", 4 },
518     { "weird", 8 },
519   };
520   guint n_keys = G_N_ELEMENTS (keys);
521   guint result;
522   
523   result = g_parse_debug_string ("bar:foo:blubb", keys, n_keys);
524   g_assert (result == 3);
525
526   result = g_parse_debug_string (":baz::_E@~!_::", keys, n_keys);
527   g_assert (result == 4);
528
529   result = g_parse_debug_string ("", keys, n_keys);
530   g_assert (result == 0);
531
532   result = g_parse_debug_string (" : ", keys, n_keys);
533   g_assert (result == 0);
534
535   result = g_parse_debug_string ("all", keys, n_keys);
536   g_assert_cmpuint (result, ==, (1 << n_keys) - 1);
537
538   /* Test subtracting debug flags from "all" */
539   result = g_parse_debug_string ("all:foo", keys, n_keys);
540   g_assert_cmpuint (result, ==, 2 | 4 | 8);
541
542   result = g_parse_debug_string ("foo baz,all", keys, n_keys);
543   g_assert_cmpuint (result, ==, 2 | 8);
544
545   result = g_parse_debug_string ("all,fooo,baz", keys, n_keys);
546   g_assert_cmpuint (result, ==, 1 | 2 | 8);
547
548   result = g_parse_debug_string ("all:weird", keys, n_keys);
549   g_assert_cmpuint (result, ==, 1 | 2 | 4);
550 }
551
552 static void
553 log_warning_error_tests (void)
554 {
555   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
556     {
557       g_message ("this is a g_message test.");
558       g_message ("non-printable UTF-8: \"\xc3\xa4\xda\x85\"");
559       g_message ("unsafe chars: \"\x10\x11\x12\n\t\x7f\x81\x82\x83\"");
560       exit (0);
561     }
562   g_test_trap_assert_passed();
563   g_test_trap_assert_stderr ("*is a g_message test*");
564   g_test_trap_assert_stderr ("*non-printable UTF-8*");
565   g_test_trap_assert_stderr ("*unsafe chars*");
566   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
567     {
568       g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
569       exit (0);
570     }
571   g_test_trap_assert_failed(); /* we have fatal-warnings enabled */
572   g_test_trap_assert_stderr ("*harmless warning*");
573   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
574     {
575       g_print (NULL);
576       exit (0);
577     }
578   g_test_trap_assert_failed(); /* we have fatal-warnings enabled */
579   g_test_trap_assert_stderr ("*g_print*assertion*failed*");
580   g_test_trap_assert_stderr ("*NULL*");
581 }
582
583 static void
584 timer_tests (void)
585 {
586   GTimer *timer, *timer2;
587   gdouble elapsed;
588
589   /* basic testing */
590   timer = g_timer_new ();
591   g_timer_start (timer);
592   elapsed = g_timer_elapsed (timer, NULL);
593   g_timer_stop (timer);
594   g_assert_cmpfloat (elapsed, <=, g_timer_elapsed (timer, NULL));
595   g_timer_destroy (timer);
596
597   if (g_test_slow())
598     {
599       if (g_test_verbose())
600         g_print ("checking timers...\n");
601       timer = g_timer_new ();
602       if (g_test_verbose())
603         g_print ("  spinning for 3 seconds...\n");
604       g_timer_start (timer);
605       while (g_timer_elapsed (timer, NULL) < 3)
606         ;
607       g_timer_stop (timer);
608       g_timer_destroy (timer);
609       if (g_test_verbose())
610         g_print ("ok\n");
611     }
612
613   if (g_test_slow())
614     {
615       gulong elapsed_usecs;
616       if (g_test_verbose())
617         g_print ("checking g_timer_continue...\n");
618       timer2 = g_timer_new ();
619       if (g_test_verbose())
620         g_print ("\trun for 1 second...\n");
621       timer = g_timer_new();
622       g_usleep (G_USEC_PER_SEC); /* run timer for 1 second */
623       g_timer_stop (timer);
624       if (g_test_verbose())
625         g_print ("\tstop for 1 second...\n");
626       g_usleep (G_USEC_PER_SEC); /* wait for 1 second */
627       if (g_test_verbose())
628         g_print ("\trun for 2 seconds...\n");
629       g_timer_continue (timer);
630       g_usleep (2 * G_USEC_PER_SEC); /* run timer for 2 seconds */
631       g_timer_stop(timer);
632       if (g_test_verbose())
633         g_print ("\tstop for 1.5 seconds...\n");
634       g_usleep ((3 * G_USEC_PER_SEC) / 2); /* wait for 1.5 seconds */
635       if (g_test_verbose())
636         g_print ("\trun for 0.2 seconds...\n");
637       g_timer_continue (timer);
638       g_usleep (G_USEC_PER_SEC / 5); /* run timer for 0.2 seconds */
639       g_timer_stop (timer);
640       if (g_test_verbose())
641         g_print ("\tstop for 4 seconds...\n");
642       g_usleep (4 * G_USEC_PER_SEC); /* wait for 4 seconds */
643       if (g_test_verbose())
644         g_print ("\trun for 5.8 seconds...\n");
645       g_timer_continue (timer);
646       g_usleep ((29 * G_USEC_PER_SEC) / 5); /* run timer for 5.8 seconds */
647       g_timer_stop(timer);
648       elapsed = g_timer_elapsed (timer, &elapsed_usecs);
649       if (g_test_verbose())
650         g_print ("\t=> timer = %.6f = %d.%06ld (should be: 9.000000) (%.6f off)\n", elapsed, (int) elapsed, elapsed_usecs, ABS (elapsed - 9.));
651       g_assert_cmpfloat (elapsed, >, 8.8);
652       g_assert_cmpfloat (elapsed, <, 9.2);
653       if (g_test_verbose())
654         g_print ("g_timer_continue ... ok\n\n");
655       g_timer_stop (timer2);
656       elapsed = g_timer_elapsed (timer2, &elapsed_usecs);
657       if (g_test_verbose())
658         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)));
659       g_assert_cmpfloat (elapsed, >, 8.8 + 6.5);
660       g_assert_cmpfloat (elapsed, <, 9.2 + 6.5);
661       if (g_test_verbose())
662         g_print ("timer2 ... ok\n\n");
663       g_timer_destroy (timer);
664       g_timer_destroy (timer2);
665     }
666 }
667
668 static void
669 type_sizes (void)
670 {
671   guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
672   guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
673   guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
674           gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
675   /* type sizes */
676   g_assert_cmpint (sizeof (gint8), ==, 1);
677   g_assert_cmpint (sizeof (gint16), ==, 2);
678   g_assert_cmpint (sizeof (gint32), ==, 4);
679   g_assert_cmpint (sizeof (gint64), ==, 8);
680   /* endian macros */
681   if (g_test_verbose())
682     g_print ("checking endian macros (host is %s)...\n",
683              G_BYTE_ORDER == G_BIG_ENDIAN ? "big endian" : "little endian");
684   g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
685   g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
686   g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
687 }
688
689 static void
690 test_info (void)
691 {
692   const gchar *un, *rn, *hn;
693   const gchar *tmpdir, *homedir, *userdatadir, *uconfdir, *ucachedir;
694   const gchar *uddesktop, *udddocs, *uddpubshare, *uruntimedir;
695   gchar **sv, *cwd, *sdatadirs, *sconfdirs, *langnames;
696   const gchar *charset;
697   gboolean charset_is_utf8;
698   if (g_test_verbose())
699     g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
700              glib_major_version,
701              glib_minor_version,
702              glib_micro_version,
703              glib_interface_age,
704              glib_binary_age);
705
706   cwd = g_get_current_dir ();
707   un = g_get_user_name();
708   rn = g_get_real_name();
709   hn = g_get_host_name();
710   if (g_test_verbose())
711     {
712       g_print ("cwd: %s\n", cwd);
713       g_print ("user: %s\n", un);
714       g_print ("real: %s\n", rn);
715       g_print ("host: %s\n", hn);
716     }
717   g_free (cwd);
718
719   /* reload, just for fun */
720   g_reload_user_special_dirs_cache ();
721   g_reload_user_special_dirs_cache ();
722
723   tmpdir = g_get_tmp_dir();
724   g_assert (tmpdir != NULL);
725   homedir = g_get_home_dir ();
726   g_assert (homedir != NULL);
727   userdatadir = g_get_user_data_dir ();
728   g_assert (userdatadir != NULL);
729   uconfdir = g_get_user_config_dir ();
730   g_assert (uconfdir != NULL);
731   ucachedir = g_get_user_cache_dir ();
732   g_assert (ucachedir != NULL);
733
734   uddesktop = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
735   g_assert (uddesktop != NULL);
736   udddocs = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
737   uddpubshare = g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE);
738   uruntimedir = g_get_user_runtime_dir ();
739   g_assert (uruntimedir != NULL);
740
741   sv = (gchar **) g_get_system_data_dirs ();
742   sdatadirs = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv);
743   sv = (gchar **) g_get_system_config_dirs ();
744   sconfdirs = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv);
745   sv = (gchar **) g_get_language_names ();
746   langnames = g_strjoinv (":", sv);
747
748   if (g_test_verbose())
749     {
750       g_print ("tmp-dir: %s\n", tmpdir);
751       g_print ("home: %s\n", homedir);
752       g_print ("user_data: %s\n", userdatadir);
753       g_print ("user_config: %s\n", uconfdir);
754       g_print ("user_cache: %s\n", ucachedir);
755       g_print ("user_runtime: %s\n", uruntimedir);
756       g_print ("system_data: %s\n", sdatadirs);
757       g_print ("system_config: %s\n", sconfdirs);
758       g_print ("languages: %s\n", langnames);
759       g_print ("user_special[DESKTOP]: %s\n", uddesktop);
760       g_print ("user_special[DOCUMENTS]: %s\n", udddocs);
761       g_print ("user_special[PUBLIC_SHARE]: %s\n", uddpubshare);
762     }
763   g_free (sdatadirs);
764   g_free (sconfdirs);
765   g_free (langnames);
766
767   charset_is_utf8 = g_get_charset ((const char**)&charset);
768
769   if (g_test_verbose())
770     {
771       if (charset_is_utf8)
772         g_print ("current charset is UTF-8: %s\n", charset);
773       else
774         g_print ("current charset is not UTF-8: %s\n", charset);
775     }
776
777   if (g_test_verbose())
778     {
779 #ifdef G_PLATFORM_WIN32
780       g_print ("current locale: %s\n", g_win32_getlocale ());
781
782       g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
783       g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
784
785       g_print ("a Win32 error message: %s\n", g_win32_error_message (2));
786 #endif
787     }
788 }
789
790 static void
791 test_paths (void)
792 {
793   struct {
794     gchar *filename;
795     gchar *dirname;
796   } dirname_checks[] = {
797     { "/", "/" },
798     { "////", "/" },
799     { ".////", "." },
800     { "../", ".." },
801     { "..////", ".." },
802     { "a/b", "a" },
803     { "a/b/", "a/b" },
804     { "c///", "c" },
805 #ifdef G_OS_WIN32
806     { "\\", "\\" },
807     { ".\\\\\\\\", "." },
808     { "..\\", ".." },
809     { "..\\\\\\\\", ".." },
810     { "a\\b", "a" },
811     { "a\\b/", "a\\b" },
812     { "a/b\\", "a/b" },
813     { "c\\\\/", "c" },
814     { "//\\", "/" },
815 #endif
816 #ifdef G_WITH_CYGWIN
817     { "//server/share///x", "//server/share" },
818 #endif
819     { ".", "." },
820     { "..", "." },
821     { "", "." },
822   };
823   const guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
824   struct {
825     gchar *filename;
826     gchar *without_root;
827   } skip_root_checks[] = {
828     { "/", "" },
829     { "//", "" },
830     { "/foo", "foo" },
831     { "//foo", "foo" },
832     { "a/b", NULL },
833 #ifdef G_OS_WIN32
834     { "\\", "" },
835     { "\\foo", "foo" },
836     { "\\\\server\\foo", "" },
837     { "\\\\server\\foo\\bar", "bar" },
838     { "a\\b", NULL },
839 #endif
840 #ifdef G_WITH_CYGWIN
841     { "//server/share///x", "//x" },
842 #endif
843     { ".", NULL },
844     { "", NULL },
845   };
846   const guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
847   gchar *string;
848   guint i;
849   if (g_test_verbose())
850     g_print ("checking g_path_get_basename()...");
851   string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
852   g_assert (strcmp (string, "dir") == 0);
853   g_free (string);
854   string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
855   g_assert (strcmp (string, "file") == 0);
856   g_free (string);
857   if (g_test_verbose())
858     g_print ("ok\n");
859
860 #ifdef G_OS_WIN32
861   string = g_path_get_basename ("/foo/dir/");
862   g_assert (strcmp (string, "dir") == 0);
863   g_free (string);
864   string = g_path_get_basename ("/foo/file");
865   g_assert (strcmp (string, "file") == 0);
866   g_free (string);
867 #endif
868
869   if (g_test_verbose())
870     g_print ("checking g_path_get_dirname()...");
871   for (i = 0; i < n_dirname_checks; i++)
872     {
873       gchar *dirname = g_path_get_dirname (dirname_checks[i].filename);
874       if (strcmp (dirname, dirname_checks[i].dirname) != 0)
875         {
876           g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
877                    dirname_checks[i].filename,
878                    dirname_checks[i].dirname,
879                    dirname);
880         }
881       g_free (dirname);
882     }
883   if (g_test_verbose())
884     g_print ("ok\n");
885
886   if (g_test_verbose())
887     g_print ("checking g_path_skip_root()...");
888   for (i = 0; i < n_skip_root_checks; i++)
889     {
890       const gchar *skipped = g_path_skip_root (skip_root_checks[i].filename);
891       if ((skipped && !skip_root_checks[i].without_root) ||
892           (!skipped && skip_root_checks[i].without_root) ||
893           ((skipped && skip_root_checks[i].without_root) &&
894            strcmp (skipped, skip_root_checks[i].without_root)))
895         {
896           g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
897                    skip_root_checks[i].filename,
898                    (skip_root_checks[i].without_root ?
899                     skip_root_checks[i].without_root : "<NULL>"),
900                    (skipped ? skipped : "<NULL>"));
901         }
902     }
903   if (g_test_verbose())
904     g_print ("ok\n");
905 }
906
907 static void
908 test_file_functions (void)
909 {
910   const char hello[] = "Hello, World";
911   const int hellolen = sizeof (hello) - 1;
912   GError *error;
913   char template[32];
914   char *name_used, chars[62];
915   gint fd, n;
916   
917   strcpy (template, "foobar");
918   fd = g_mkstemp (template);
919   if (g_test_verbose() && fd != -1)
920     g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
921   if (fd != -1)
922     close (fd);
923   strcpy (template, "fooXXXXXX");
924   fd = g_mkstemp (template);
925   if (fd == -1)
926     g_error ("g_mkstemp didn't work for template %s\n", template);
927   n = write (fd, hello, hellolen);
928   if (n == -1)
929     g_error ("write() failed: %s\n", g_strerror (errno));
930   else if (n != hellolen)
931     g_error ("write() should have written %d bytes, wrote %d\n", hellolen, n);
932
933   lseek (fd, 0, 0);
934   n = read (fd, chars, sizeof (chars));
935   if (n == -1)
936     g_error ("read() failed: %s\n", g_strerror (errno));
937   else if (n != hellolen)
938     g_error ("read() should have read %d bytes, got %d\n", hellolen, n);
939
940   chars[n] = 0;
941   if (strcmp (chars, hello) != 0)
942     g_error ("wrote '%s', but got '%s'\n", hello, chars);
943
944   close (fd);
945   remove (template);
946
947   error = NULL;
948   name_used = NULL;
949   strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
950   fd = g_file_open_tmp (template, &name_used, &error);
951   if (g_test_verbose())
952     {
953       if (fd != -1)
954         g_print ("g_file_open_tmp works even if template contains '%s'\n", G_DIR_SEPARATOR_S);
955       else
956         g_print ("g_file_open_tmp correctly returns error: %s\n", error->message);
957     }
958   if (fd != -1)
959     close (fd);
960   g_clear_error (&error);
961   g_free (name_used);
962
963 #ifdef G_OS_WIN32
964   name_used = NULL;
965   strcpy (template, "zap/barXXXXXX");
966   fd = g_file_open_tmp (template, &name_used, &error);
967   if (g_test_verbose())
968     {
969       if (fd != -1)
970         g_print ("g_file_open_tmp works even if template contains '/'\n");
971       else
972         g_print ("g_file_open_tmp correctly returns error: %s\n", error->message);
973     }
974   close (fd);
975   g_clear_error (&error);
976   g_free (name_used);
977 #endif
978
979   strcpy (template, "zapXXXXXX");
980   name_used = NULL;
981   fd = g_file_open_tmp (template, &name_used, &error);
982   if (fd == -1)
983     g_error ("g_file_open_tmp didn't work for template '%s': %s\n", template, error->message);
984   else if (g_test_verbose())
985     g_print ("g_file_open_tmp for template '%s' used name '%s'\n", template, name_used);
986   close (fd);
987   g_clear_error (&error);
988   remove (name_used);
989   g_free (name_used);
990
991   name_used = NULL;
992   fd = g_file_open_tmp (NULL, &name_used, &error);
993   if (fd == -1)
994     g_error ("g_file_open_tmp didn't work for a NULL template: %s\n", error->message);
995   close (fd);
996   g_clear_error (&error);
997   remove (name_used);
998   g_free (name_used);
999 }
1000
1001 static void
1002 test_arrays (void)
1003 {
1004   GByteArray *gbarray;
1005   GPtrArray *gparray;
1006   GArray *garray;
1007   guint i;
1008
1009   gparray = g_ptr_array_new ();
1010   for (i = 0; i < 10000; i++)
1011     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1012   for (i = 0; i < 10000; i++)
1013     if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1014       g_error ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1015   g_ptr_array_free (gparray, TRUE);
1016
1017   gbarray = g_byte_array_new ();
1018   for (i = 0; i < 10000; i++)
1019     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1020   for (i = 0; i < 10000; i++)
1021     {
1022       g_assert (gbarray->data[4*i] == 'a');
1023       g_assert (gbarray->data[4*i+1] == 'b');
1024       g_assert (gbarray->data[4*i+2] == 'c');
1025       g_assert (gbarray->data[4*i+3] == 'd');
1026     }
1027   g_byte_array_free (gbarray, TRUE);
1028
1029   garray = g_array_new (FALSE, FALSE, sizeof (gint));
1030   for (i = 0; i < 10000; i++)
1031     g_array_append_val (garray, i);
1032   for (i = 0; i < 10000; i++)
1033     if (g_array_index (garray, gint, i) != i)
1034       g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), i);
1035   g_array_free (garray, TRUE);
1036
1037   garray = g_array_new (FALSE, FALSE, sizeof (gint));
1038   for (i = 0; i < 100; i++)
1039     g_array_prepend_val (garray, i);
1040   for (i = 0; i < 100; i++)
1041     if (g_array_index (garray, gint, i) != (100 - i - 1))
1042       g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
1043   g_array_free (garray, TRUE);
1044 }
1045
1046 static void
1047 hash_table_tests (void)
1048 {
1049   GHashTable *hash_table;
1050   int array[10000];
1051   gint *pvalue = NULL;
1052   gint value = 120;
1053   guint i;
1054
1055   hash_table = g_hash_table_new (my_hash, my_hash_equal);
1056   for (i = 0; i < 10000; i++)
1057     {
1058       array[i] = i;
1059       g_hash_table_insert (hash_table, &array[i], &array[i]);
1060     }
1061   pvalue = g_hash_table_find (hash_table, find_first_that, &value);
1062   if (*pvalue != value)
1063     g_error ("g_hash_table_find failed");
1064   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
1065   for (i = 0; i < 10000; i++)
1066     if (array[i] == 0)
1067       g_error ("hashtable-test: wrong value: %d\n", i);
1068   for (i = 0; i < 10000; i++)
1069     g_hash_table_remove (hash_table, &array[i]);
1070   for (i = 0; i < 10000; i++)
1071     {
1072       array[i] = i;
1073       g_hash_table_insert (hash_table, &array[i], &array[i]);
1074     }
1075   if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
1076       g_hash_table_size (hash_table) != 5000)
1077     g_error ("hashtable removal failed\n");
1078   g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
1079   g_hash_table_destroy (hash_table);
1080 }
1081
1082 #ifndef G_DISABLE_DEPRECATED
1083 static void
1084 relation_test (void)
1085 {
1086   GRelation *relation = g_relation_new (2);
1087   GTuples *tuples;
1088   gint data [1024];
1089   guint i;
1090
1091   g_relation_index (relation, 0, g_int_hash, g_int_equal);
1092   g_relation_index (relation, 1, g_int_hash, g_int_equal);
1093
1094   for (i = 0; i < 1024; i += 1)
1095     data[i] = i;
1096
1097   for (i = 1; i < 1023; i += 1)
1098     {
1099       g_relation_insert (relation, data + i, data + i + 1);
1100       g_relation_insert (relation, data + i, data + i - 1);
1101     }
1102
1103   for (i = 2; i < 1022; i += 1)
1104     {
1105       g_assert (! g_relation_exists (relation, data + i, data + i));
1106       g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1107       g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1108     }
1109
1110   for (i = 1; i < 1023; i += 1)
1111     {
1112       g_assert (g_relation_exists (relation, data + i, data + i + 1));
1113       g_assert (g_relation_exists (relation, data + i, data + i - 1));
1114     }
1115
1116   for (i = 2; i < 1022; i += 1)
1117     {
1118       g_assert (g_relation_count (relation, data + i, 0) == 2);
1119       g_assert (g_relation_count (relation, data + i, 1) == 2);
1120     }
1121
1122   g_assert (g_relation_count (relation, data, 0) == 0);
1123
1124   g_assert (g_relation_count (relation, data + 42, 0) == 2);
1125   g_assert (g_relation_count (relation, data + 43, 1) == 2);
1126   g_assert (g_relation_count (relation, data + 41, 1) == 2);
1127   g_relation_delete (relation, data + 42, 0);
1128   g_assert (g_relation_count (relation, data + 42, 0) == 0);
1129   g_assert (g_relation_count (relation, data + 43, 1) == 1);
1130   g_assert (g_relation_count (relation, data + 41, 1) == 1);
1131
1132   tuples = g_relation_select (relation, data + 200, 0);
1133
1134   g_assert (tuples->len == 2);
1135
1136 #if 0
1137   for (i = 0; i < tuples->len; i += 1)
1138     {
1139       printf ("%d %d\n",
1140               *(gint*) g_tuples_index (tuples, i, 0),
1141               *(gint*) g_tuples_index (tuples, i, 1));
1142     }
1143 #endif
1144
1145   g_assert (g_relation_exists (relation, data + 300, data + 301));
1146   g_relation_delete (relation, data + 300, 0);
1147   g_assert (!g_relation_exists (relation, data + 300, data + 301));
1148
1149   g_tuples_destroy (tuples);
1150
1151   g_relation_destroy (relation);
1152
1153   relation = NULL;
1154 }
1155 #endif
1156
1157 static void
1158 gstring_tests (void)
1159 {
1160   GString *string1, *string2;
1161   guint i;
1162
1163   if (g_test_verbose())
1164     g_print ("test GString basics\n");
1165
1166   string1 = g_string_new ("hi pete!");
1167   string2 = g_string_new ("");
1168
1169   g_assert (strcmp ("hi pete!", string1->str) == 0);
1170
1171   for (i = 0; i < 10000; i++)
1172     g_string_append_c (string1, 'a'+(i%26));
1173
1174 #ifndef G_OS_WIN32
1175   /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
1176      the %10000.10000f format... */
1177   g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
1178                    "this pete guy sure is a wuss, like he's the number ",
1179                    1,
1180                    " wuss.  everyone agrees.\n",
1181                    string1->str,
1182                    10, 666, 15, 15, 666.666666666, 666.666666666);
1183 #else
1184   g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
1185                    "this pete guy sure is a wuss, like he's the number ",
1186                    1,
1187                    " wuss.  everyone agrees.\n",
1188                    string1->str,
1189                    10, 666, 15, 15, 666.666666666, 666.666666666);
1190 #endif
1191
1192   if (g_test_verbose())
1193     g_print ("string2 length = %lu...\n", (gulong)string2->len);
1194   string2->str[70] = '\0';
1195   if (g_test_verbose())
1196     g_print ("first 70 chars:\n%s\n", string2->str);
1197   string2->str[141] = '\0';
1198   if (g_test_verbose())
1199     g_print ("next 70 chars:\n%s\n", string2->str+71);
1200   string2->str[212] = '\0';
1201   if (g_test_verbose())
1202     g_print ("and next 70:\n%s\n", string2->str+142);
1203   if (g_test_verbose())
1204     g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
1205
1206   g_string_free (string1, TRUE);
1207   g_string_free (string2, TRUE);
1208
1209   /* append */
1210   string1 = g_string_new ("firsthalf");
1211   g_string_append (string1, "lasthalf");
1212   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1213   g_string_free (string1, TRUE);
1214
1215   /* append_len */
1216   string1 = g_string_new ("firsthalf");
1217   g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
1218   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1219   g_string_free (string1, TRUE);
1220
1221   /* prepend */
1222   string1 = g_string_new ("lasthalf");
1223   g_string_prepend (string1, "firsthalf");
1224   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1225   g_string_free (string1, TRUE);
1226
1227   /* prepend_len */
1228   string1 = g_string_new ("lasthalf");
1229   g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
1230   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1231   g_string_free (string1, TRUE);
1232
1233   /* insert */
1234   string1 = g_string_new ("firstlast");
1235   g_string_insert (string1, 5, "middle");
1236   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1237   g_string_free (string1, TRUE);
1238
1239   /* insert with pos == end of the string */
1240   string1 = g_string_new ("firstmiddle");
1241   g_string_insert (string1, strlen ("firstmiddle"), "last");
1242   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1243   g_string_free (string1, TRUE);
1244
1245   /* insert_len */
1246   string1 = g_string_new ("firstlast");
1247   g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
1248   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1249   g_string_free (string1, TRUE);
1250
1251   /* insert_len with magic -1 pos for append */
1252   string1 = g_string_new ("first");
1253   g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
1254   g_assert (strcmp (string1->str, "firstlast") == 0);
1255   g_string_free (string1, TRUE);
1256
1257   /* insert_len with magic -1 len for strlen-the-string */
1258   string1 = g_string_new ("first");
1259   g_string_insert_len (string1, 5, "last", -1);
1260   g_assert (strcmp (string1->str, "firstlast") == 0);
1261   g_string_free (string1, TRUE);
1262
1263   /* g_string_equal */
1264   string1 = g_string_new ("test");
1265   string2 = g_string_new ("te");
1266   g_assert (! g_string_equal(string1, string2));
1267   g_string_append (string2, "st");
1268   g_assert (g_string_equal(string1, string2));
1269   g_string_free (string1, TRUE);
1270   g_string_free (string2, TRUE);
1271
1272   /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
1273   if (g_test_verbose())
1274     g_print ("test embedded ASCII 0 (NUL) characters in GString\n");
1275   string1 = g_string_new ("fiddle");
1276   string2 = g_string_new ("fiddle");
1277   g_assert (g_string_equal(string1, string2));
1278   g_string_append_c(string1, '\0');
1279   g_assert (! g_string_equal(string1, string2));
1280   g_string_append_c(string2, '\0');
1281   g_assert (g_string_equal(string1, string2));
1282   g_string_append_c(string1, 'x');
1283   g_string_append_c(string2, 'y');
1284   g_assert (! g_string_equal(string1, string2));
1285   g_assert (string1->len == 8);
1286   g_string_append(string1, "yzzy");
1287   g_assert (string1->len == 12);
1288   g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
1289   g_string_insert(string1, 1, "QED");
1290   g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
1291   g_string_free (string1, TRUE);
1292   g_string_free (string2, TRUE);
1293 }
1294
1295 static void
1296 various_string_tests (void)
1297 {
1298   GStringChunk *string_chunk;
1299   GTimeVal ref_date, date;
1300   gchar *tmp_string = NULL, *tmp_string_2, *string, *date_str;
1301   guint i;
1302   const gchar *tz;
1303
1304   if (g_test_verbose())
1305     g_print ("checking string chunks...");
1306   string_chunk = g_string_chunk_new (1024);
1307   for (i = 0; i < 100000; i ++)
1308     {
1309       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
1310       if (strcmp ("hi pete", tmp_string) != 0)
1311         g_error ("string chunks are broken.\n");
1312     }
1313   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
1314   g_assert (tmp_string_2 != tmp_string && strcmp (tmp_string_2, tmp_string) == 0);
1315   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
1316   g_assert (tmp_string_2 == tmp_string);
1317   g_string_chunk_free (string_chunk);
1318
1319   if (g_test_verbose())
1320     g_print ("test positional printf formats (not supported):");
1321   string = g_strdup_printf ("%.*s%s", 5, "a", "b");
1322   tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
1323   if (g_test_verbose())
1324     g_print ("%s%s\n", string, tmp_string);
1325   g_free (tmp_string);
1326   g_free (string);
1327
1328 #define REF_INVALID1      "Wed Dec 19 17:20:20 GMT 2007"
1329 #define REF_INVALID2      "1980-02-22T10:36:00Zulu"
1330 #define REF_INVALID3      "1980-02-22T"
1331 #define REF_SEC_UTC       320063760
1332 #define REF_STR_UTC       "1980-02-22T10:36:00Z"
1333 #define REF_STR_LOCAL     "1980-02-22T13:36:00"
1334 #define REF_STR_CEST      "1980-02-22T12:36:00+02:00"
1335 #define REF_STR_EST       "19800222T053600-0500"
1336 #define REF_STR_NST       "1980-02-22T07:06:00-03:30"
1337 #define REF_USEC_UTC      50000
1338 #define REF_STR_USEC_UTC  "1980-02-22T10:36:00.050000Z"
1339 #define REF_STR_USEC_CEST "19800222T123600.050000000+0200"
1340 #define REF_STR_USEC_EST  "1980-02-22T05:36:00,05-05:00"
1341 #define REF_STR_USEC_NST  "19800222T070600,0500-0330"
1342 #define REF_STR_DATE_ONLY "1980-02-22"
1343
1344   if (g_test_verbose())
1345     g_print ("checking g_time_val_from_iso8601...\n");
1346   ref_date.tv_sec = REF_SEC_UTC;
1347   ref_date.tv_usec = 0;
1348   g_assert (g_time_val_from_iso8601 (REF_INVALID1, &date) == FALSE);
1349   g_assert (g_time_val_from_iso8601 (REF_INVALID2, &date) == FALSE);
1350   g_assert (g_time_val_from_iso8601 (REF_INVALID3, &date) == FALSE);
1351   g_assert (g_time_val_from_iso8601 (REF_STR_DATE_ONLY, &date) != FALSE);
1352   g_assert (g_time_val_from_iso8601 (REF_STR_UTC, &date) != FALSE);
1353   if (g_test_verbose())
1354     g_print ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1355              date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1356              date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1357   g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1358
1359   /* predefine time zone */
1360   tz = g_getenv("TZ");
1361   g_setenv("TZ", "UTC-03:00", 1);
1362   tzset();
1363
1364   g_assert (g_time_val_from_iso8601 (REF_STR_LOCAL, &date) != FALSE);
1365   if (g_test_verbose())
1366     g_print ("\t=> LOCAL stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1367              date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1368              date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1369   g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1370
1371   /* revert back user defined time zone */
1372   if (tz)
1373     g_setenv("TZ", tz, TRUE);
1374   else
1375     g_unsetenv("TZ");
1376   tzset();
1377
1378   g_assert (g_time_val_from_iso8601 (REF_STR_CEST, &date) != FALSE);
1379   if (g_test_verbose())
1380     g_print ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1381              date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1382              date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1383   g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1384
1385   g_assert (g_time_val_from_iso8601 (REF_STR_EST, &date) != FALSE);
1386   if (g_test_verbose())
1387     g_print ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1388              date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1389              date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1390   g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1391
1392   g_assert (g_time_val_from_iso8601 (REF_STR_NST, &date) != FALSE);
1393   if (g_test_verbose())
1394     g_print ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1395              date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1396              date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1397   g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1398
1399   ref_date.tv_usec = REF_USEC_UTC;
1400   g_assert (g_time_val_from_iso8601 (REF_STR_USEC_UTC, &date) != FALSE);
1401   if (g_test_verbose())
1402     g_print ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1403              date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1404              date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1405   g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1406
1407   g_assert (g_time_val_from_iso8601 (REF_STR_USEC_CEST, &date) != FALSE);
1408   if (g_test_verbose())
1409     g_print ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1410              date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1411              date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1412   g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1413
1414   g_assert (g_time_val_from_iso8601 (REF_STR_USEC_EST, &date) != FALSE);
1415   if (g_test_verbose())
1416     g_print ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1417              date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1418              date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1419   g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1420
1421   g_assert (g_time_val_from_iso8601 (REF_STR_USEC_NST, &date) != FALSE);
1422   if (g_test_verbose())
1423     g_print ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1424              date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1425              date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1426   g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1427
1428   if (g_test_verbose())
1429     g_print ("checking g_time_val_to_iso8601...\n");
1430   ref_date.tv_sec = REF_SEC_UTC;
1431   ref_date.tv_usec = 0;
1432   date_str = g_time_val_to_iso8601 (&ref_date);
1433   g_assert (date_str != NULL);
1434   if (g_test_verbose())
1435     g_print ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_UTC);
1436   g_assert (strcmp (date_str, REF_STR_UTC) == 0);
1437   g_free (date_str);
1438
1439   ref_date.tv_usec = REF_USEC_UTC;
1440   date_str = g_time_val_to_iso8601 (&ref_date);
1441   g_assert (date_str != NULL);
1442   if (g_test_verbose())
1443     g_print ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_USEC_UTC);
1444   g_assert (strcmp (date_str, REF_STR_USEC_UTC) == 0);
1445   g_free (date_str);
1446
1447   if (g_test_verbose())
1448     g_print ("checking g_ascii_strcasecmp...");
1449   g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
1450   g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
1451   g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
1452   g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1453   g_assert (g_ascii_strcasecmp ("", "") == 0);
1454   g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
1455   g_assert (g_ascii_strcasecmp ("a", "b") < 0);
1456   g_assert (g_ascii_strcasecmp ("a", "B") < 0);
1457   g_assert (g_ascii_strcasecmp ("A", "b") < 0);
1458   g_assert (g_ascii_strcasecmp ("A", "B") < 0);
1459   g_assert (g_ascii_strcasecmp ("b", "a") > 0);
1460   g_assert (g_ascii_strcasecmp ("b", "A") > 0);
1461   g_assert (g_ascii_strcasecmp ("B", "a") > 0);
1462   g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1463
1464   if (g_test_verbose())
1465     g_print ("checking g_strdup...\n");
1466   g_assert (g_strdup (NULL) == NULL);
1467   string = g_strdup (GLIB_TEST_STRING);
1468   g_assert (string != NULL);
1469   g_assert (strcmp(string, GLIB_TEST_STRING) == 0);
1470   g_free (string);
1471
1472   if (g_test_verbose())
1473     g_print ("checking g_strconcat...\n");
1474   string = g_strconcat (GLIB_TEST_STRING, NULL);
1475   g_assert (string != NULL);
1476   g_assert (strcmp (string, GLIB_TEST_STRING) == 0);
1477   g_free (string);
1478   string = g_strconcat (GLIB_TEST_STRING, GLIB_TEST_STRING, 
1479                         GLIB_TEST_STRING, NULL);
1480   g_assert (string != NULL);
1481   g_assert (strcmp (string, GLIB_TEST_STRING GLIB_TEST_STRING
1482                     GLIB_TEST_STRING) == 0);
1483   g_free (string);
1484
1485   if (g_test_verbose())
1486     g_print ("checking g_strlcpy/g_strlcat...");
1487   /* The following is a torture test for strlcpy/strlcat, with lots of
1488    * checking; normal users wouldn't use them this way!
1489    */
1490   string = g_malloc (6);
1491   *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
1492   *string = 'q';
1493   g_assert (g_strlcpy(string, "" , 5) == 0);
1494   g_assert ( *string == '\0' );
1495   *string = 'q';
1496   g_assert (g_strlcpy(string, "abc" , 5) == 3);
1497   g_assert ( *(string + 3) == '\0' );
1498   g_assert (g_str_equal(string, "abc"));
1499   g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1500   g_assert ( *(string + 4) == '\0' );
1501   g_assert ( *(string + 5) == 'Z' );
1502   g_assert (g_str_equal(string, "abcd"));
1503   g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1504   g_assert ( *(string + 4) == '\0' );
1505   g_assert ( *(string + 5) == 'Z' );
1506   g_assert (g_str_equal(string, "abcd"));
1507   g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1508   g_assert ( *(string + 4) == '\0' );
1509   g_assert ( *(string + 5) == 'Z' );
1510   g_assert (g_str_equal(string, "abcd"));
1511   *string = 'Y';
1512   *(string + 1)= '\0';
1513   g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1514   g_assert (*string == 'Y');
1515   *string = '\0';
1516   g_assert (g_strlcat(string, "123" , 5) == 3);
1517   g_assert ( *(string + 3) == '\0' );
1518   g_assert (g_str_equal(string, "123"));
1519   g_assert (g_strlcat(string, "" , 5) == 3);
1520   g_assert ( *(string + 3) == '\0' );
1521   g_assert (g_str_equal(string, "123"));
1522   g_assert (g_strlcat(string, "4", 5) == 4);
1523   g_assert (g_str_equal(string, "1234"));
1524   g_assert (g_strlcat(string, "5", 5) == 5);
1525   g_assert ( *(string + 4) == '\0' );
1526   g_assert (g_str_equal(string, "1234"));
1527   g_assert ( *(string + 5) == 'Z' );
1528   *string = 'Y';
1529   *(string + 1)= '\0';
1530   g_assert (g_strlcat(string, "123" , 0) == 3);
1531   g_assert (*string == 'Y');
1532
1533   /* A few more tests, demonstrating more "normal" use  */
1534   g_assert (g_strlcpy(string, "hi", 5) == 2);
1535   g_assert (g_str_equal(string, "hi"));
1536   g_assert (g_strlcat(string, "t", 5) == 3);
1537   g_assert (g_str_equal(string, "hit"));
1538   g_free(string);
1539
1540   if (g_test_verbose())
1541     g_print ("checking g_strdup_printf...\n");
1542   string = g_strdup_printf ("%05d %-5s", 21, "test");
1543   g_assert (string != NULL);
1544   g_assert (strcmp(string, "00021 test ") == 0);
1545   g_free (string);
1546
1547   /* g_debug (argv[0]); */
1548 }
1549
1550 #ifndef G_DISABLE_DEPRECATED
1551 static void
1552 test_mem_chunks (void)
1553 {
1554   GMemChunk *mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
1555   gchar *mem[10000];
1556   guint i;
1557   for (i = 0; i < 10000; i++)
1558     {
1559       guint j;
1560       mem[i] = g_chunk_new (gchar, mem_chunk);
1561       for (j = 0; j < 50; j++)
1562         mem[i][j] = i * j;
1563     }
1564   for (i = 0; i < 10000; i++)
1565     g_mem_chunk_free (mem_chunk, mem[i]);
1566
1567   g_mem_chunk_destroy (mem_chunk);
1568 }
1569 #endif
1570
1571 int
1572 main (int   argc,
1573       char *argv[])
1574 {
1575   g_test_init (&argc, &argv, NULL);
1576
1577   g_test_add_func ("/testglib/Infos", test_info);
1578   g_test_add_func ("/testglib/Types Sizes", type_sizes);
1579   g_test_add_func ("/testglib/GStrings", gstring_tests);
1580   g_test_add_func ("/testglib/Various Strings", various_string_tests);
1581   g_test_add_func ("/testglib/GList", glist_test);
1582   g_test_add_func ("/testglib/GSList", gslist_test);
1583   g_test_add_func ("/testglib/GNode", gnode_test);
1584   g_test_add_func ("/testglib/GTree", binary_tree_test);
1585   g_test_add_func ("/testglib/Arrays", test_arrays);
1586   g_test_add_func ("/testglib/GHashTable", hash_table_tests);
1587 #ifndef G_DISABLE_DEPRECATED
1588   g_test_add_func ("/testglib/Relation (deprecated)", relation_test);
1589 #endif
1590   g_test_add_func ("/testglib/File Paths", test_paths);
1591   g_test_add_func ("/testglib/File Functions", test_file_functions);
1592   g_test_add_func ("/testglib/Parse Debug Strings", test_g_parse_debug_string);
1593 #ifndef G_DISABLE_DEPRECATED
1594   g_test_add_func ("/testglib/GMemChunk (deprecated)", test_mem_chunks);
1595 #endif
1596   g_test_add_func ("/testglib/Warnings & Errors", log_warning_error_tests);
1597   g_test_add_func ("/testglib/Timers (slow)", timer_tests);
1598
1599   return g_test_run();
1600 }