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