=== Released 2.3.0 ===
[platform/upstream/glib.git] / tests / testglib.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 #undef G_DISABLE_ASSERT
30 #undef G_LOG_DOMAIN
31
32 #ifdef GLIB_COMPILATION
33 #undef GLIB_COMPILATION
34 #endif
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39
40 #include "glib.h"
41
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45
46 #ifdef G_OS_WIN32
47 #include <io.h>                 /* For read(), write() etc */
48 #endif
49
50 static int array[10000];
51 static gboolean failed = FALSE;
52
53 /* We write (m ? m : "") even in the m != NULL case to suppress a warning with GCC-3.1
54  */
55 #define TEST(m,cond)    G_STMT_START { failed = !(cond); \
56 if (failed) \
57   { if (!m) \
58       g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
59     else \
60       g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)(m ? m : "")); \
61   } \
62 else \
63   g_print ("."); fflush (stdout); \
64 } G_STMT_END
65
66 #define C2P(c)          ((gpointer) ((long) (c)))
67 #define P2C(p)          ((gchar) ((long) (p)))
68
69 #define GLIB_TEST_STRING "el dorado "
70 #define GLIB_TEST_STRING_5 "el do"
71
72 static gboolean
73 node_build_string (GNode    *node,
74                    gpointer  data)
75 {
76   gchar **p = data;
77   gchar *string;
78   gchar c[2] = "_";
79
80   c[0] = P2C (node->data);
81
82   string = g_strconcat (*p ? *p : "", c, NULL);
83   g_free (*p);
84   *p = string;
85
86   return FALSE;
87 }
88
89 static void
90 g_node_test (void)
91 {
92   GNode *root;
93   GNode *node;
94   GNode *node_B;
95   GNode *node_F;
96   GNode *node_G;
97   GNode *node_J;
98   guint i;
99   gchar *tstring, *cstring;
100
101   g_print ("checking n-way trees: ");
102   failed = FALSE;
103
104   root = g_node_new (C2P ('A'));
105   TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
106
107   node_B = g_node_new (C2P ('B'));
108   g_node_append (root, node_B);
109   TEST (NULL, root->children == node_B);
110
111   g_node_append_data (node_B, C2P ('E'));
112   g_node_prepend_data (node_B, C2P ('C'));
113   g_node_insert (node_B, 1, g_node_new (C2P ('D')));
114
115   node_F = g_node_new (C2P ('F'));
116   g_node_append (root, node_F);
117   TEST (NULL, root->children->next == node_F);
118
119   node_G = g_node_new (C2P ('G'));
120   g_node_append (node_F, node_G);
121   node_J = g_node_new (C2P ('J'));
122   g_node_prepend (node_G, node_J);
123   g_node_insert (node_G, 42, g_node_new (C2P ('K')));
124   g_node_insert_data (node_G, 0, C2P ('H'));
125   g_node_insert (node_G, 1, g_node_new (C2P ('I')));
126
127   TEST (NULL, g_node_depth (root) == 1);
128   TEST (NULL, g_node_max_height (root) == 4);
129   TEST (NULL, g_node_depth (node_G->children->next) == 4);
130   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
131   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
132   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
133   TEST (NULL, g_node_max_height (node_F) == 3);
134   TEST (NULL, g_node_n_children (node_G) == 4);
135   TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
136   TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
137   TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
138
139   for (i = 0; i < g_node_n_children (node_B); i++)
140     {
141       node = g_node_nth_child (node_B, i);
142       TEST (NULL, P2C (node->data) == ('C' + i));
143     }
144   
145   for (i = 0; i < g_node_n_children (node_G); i++)
146     TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
147
148   /* we have built:                    A
149    *                                 /   \
150    *                               B       F
151    *                             / | \       \
152    *                           C   D   E       G
153    *                                         / /\ \
154    *                                       H  I  J  K
155    *
156    * for in-order traversal, 'G' is considered to be the "left"
157    * child of 'F', which will cause 'F' to be the last node visited.
158    */
159
160   tstring = NULL;
161   g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
162   TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
163   g_free (tstring); tstring = NULL;
164   g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
165   TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
166   g_free (tstring); tstring = NULL;
167   g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
168   TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
169   g_free (tstring); tstring = NULL;
170   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
171   TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
172   g_free (tstring); tstring = NULL;
173   
174   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
175   TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
176   g_free (tstring); tstring = NULL;
177   g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
178   TEST (tstring, strcmp (tstring, "ABFG") == 0);
179   g_free (tstring); tstring = NULL;
180
181   g_node_reverse_children (node_B);
182   g_node_reverse_children (node_G);
183
184   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
185   TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
186   g_free (tstring); tstring = NULL;
187
188   cstring = NULL;
189   node = g_node_copy (root);
190   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
191   TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
192   g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
193   g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
194   TEST (cstring, strcmp (tstring, cstring) == 0);
195   g_free (tstring); tstring = NULL;
196   g_free (cstring); cstring = NULL;
197   g_node_destroy (node);
198
199   g_node_destroy (root);
200
201   /* allocation tests */
202
203   root = g_node_new (NULL);
204   node = root;
205
206   for (i = 0; i < 2048; i++)
207     {
208       g_node_append (node, g_node_new (NULL));
209       if ((i%5) == 4)
210         node = node->children->next;
211     }
212   TEST (NULL, g_node_max_height (root) > 100);
213   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
214
215   g_node_destroy (root);
216   
217   if (!failed)
218     g_print ("ok\n");
219 }
220
221 static gboolean
222 my_hash_callback_remove (gpointer key,
223                          gpointer value,
224                          gpointer user_data)
225 {
226   int *d = value;
227
228   if ((*d) % 2)
229     return TRUE;
230
231   return FALSE;
232 }
233
234 static void
235 my_hash_callback_remove_test (gpointer key,
236                               gpointer value,
237                               gpointer user_data)
238 {
239   int *d = value;
240
241   if ((*d) % 2)
242     g_print ("bad!\n");
243 }
244
245 static void
246 my_hash_callback (gpointer key,
247                   gpointer value,
248                   gpointer user_data)
249 {
250   int *d = value;
251   *d = 1;
252 }
253
254 static guint
255 my_hash (gconstpointer key)
256 {
257   return (guint) *((const gint*) key);
258 }
259
260 static gboolean
261 my_hash_equal (gconstpointer a,
262                gconstpointer b)
263 {
264   return *((const gint*) a) == *((const gint*) b);
265 }
266
267 static gint
268 my_list_compare_one (gconstpointer a, gconstpointer b)
269 {
270   gint one = *((const gint*)a);
271   gint two = *((const gint*)b);
272   return one-two;
273 }
274
275 static gint
276 my_list_compare_two (gconstpointer a, gconstpointer b)
277 {
278   gint one = *((const gint*)a);
279   gint two = *((const gint*)b);
280   return two-one;
281 }
282
283 /* static void
284 my_list_print (gpointer a, gpointer b)
285 {
286   gint three = *((gint*)a);
287   g_print("%d", three);
288 }; */
289
290 static gint
291 my_compare (gconstpointer a,
292             gconstpointer b)
293 {
294   const char *cha = a;
295   const char *chb = b;
296
297   return *cha - *chb;
298 }
299
300 static gint
301 my_traverse (gpointer key,
302              gpointer value,
303              gpointer data)
304 {
305   char *ch = key;
306   g_print ("%c ", *ch);
307   return FALSE;
308 }
309
310 int
311 main (int   argc,
312       char *argv[])
313 {
314   GList *list, *t;
315   GSList *slist, *st;
316   GHashTable *hash_table;
317   GMemChunk *mem_chunk;
318   GStringChunk *string_chunk;
319   GTimer *timer;
320   gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
321   gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
322   gchar *string;
323
324   gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
325   gint i, j;
326   GArray *garray;
327   GPtrArray *gparray;
328   GByteArray *gbarray;
329   GString *string1, *string2;
330   const gchar *charset;
331   GTree *tree;
332   char chars[62];
333   GRelation *relation;
334   GTuples *tuples;
335   gint data [1024];
336   struct {
337     gchar *filename;
338     gchar *dirname;
339   } dirname_checks[] = {
340     { "/", "/" },
341     { "////", "/" },
342     { ".////", "." },
343     { "../", ".." },
344     { "..////", ".." },
345     { "a/b", "a" },
346     { "a/b/", "a/b" },
347     { "c///", "c" },
348 #ifdef G_OS_WIN32
349     { "\\", "\\" },
350     { ".\\\\\\\\", "." },
351     { "..\\", ".." },
352     { "..\\\\\\\\", ".." },
353     { "a\\b", "a" },
354     { "a\\b/", "a\\b" },
355     { "a/b\\", "a/b" },
356     { "c\\\\/", "c" },
357     { "//\\", "/" },
358 #endif
359 #ifdef G_WITH_CYGWIN
360     { "//server/share///x", "//server/share" },
361 #endif
362     { ".", "." },
363     { "..", "." },
364     { "", "." },
365   };
366   guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
367
368   struct {
369     gchar *filename;
370     gchar *without_root;
371   } skip_root_checks[] = {
372     { "/", "" },
373     { "//", "" },
374     { "/foo", "foo" },
375     { "//foo", "foo" },
376     { "a/b", NULL },
377 #ifdef G_OS_WIN32
378     { "\\", "" },
379     { "\\foo", "foo" },
380     { "\\\\server\\foo", "" },
381     { "\\\\server\\foo\\bar", "bar" },
382     { "a\\b", NULL },
383 #endif
384 #ifdef G_WITH_CYGWIN
385     { "//server/share///x", "//x" },
386 #endif
387     { ".", NULL },
388     { "", NULL },
389   };
390   guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
391
392 #ifndef G_DISABLE_ASSERT
393   guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
394   guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
395   guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
396           gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
397 #endif
398   const char hello[] = "Hello, World";
399   const int hellolen = sizeof (hello) - 1;
400   int fd;
401   char template[32];
402   GError *error;
403   char *name_used;
404 #ifdef G_OS_WIN32
405   /* Can't calculate GLib DLL name at runtime. */
406   gchar *glib_dll = "libglib-2.0-0.dll";
407 #endif
408 #ifdef G_WITH_CYGWIN
409   gchar *glib_dll = "cygglib-2.0-0.dll";
410 #endif
411
412   g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
413            glib_major_version,
414            glib_minor_version,
415            glib_micro_version,
416            glib_interface_age,
417            glib_binary_age);
418
419   string = g_get_current_dir ();
420   g_print ("cwd: %s\n", string);
421   g_free (string);
422   g_print ("user: %s\n", g_get_user_name ());
423   g_print ("real: %s\n", g_get_real_name ());
424   g_print ("home: %s\n", g_get_home_dir ());
425   g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
426
427   /* type sizes */
428   g_print ("checking size of gint8: %d", (int)sizeof (gint8));
429   TEST (NULL, sizeof (gint8) == 1);
430   g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
431   TEST (NULL, sizeof (gint16) == 2);
432   g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
433   TEST (NULL, sizeof (gint32) == 4);
434   g_print ("\nchecking size of gsize: %d", (int)sizeof (gsize));
435   g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
436   TEST (NULL, sizeof (gint64) == 8);
437   g_print ("\n");
438
439   g_print ("checking g_path_get_basename()...");
440   string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
441   g_assert (strcmp (string, "dir") == 0);
442   g_free (string);
443   string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
444   g_assert (strcmp (string, "file") == 0);
445   g_free (string);
446   g_print ("ok\n");
447 #ifdef G_OS_WIN32
448   string = g_path_get_basename ("/foo/dir/");
449   g_assert (strcmp (string, "dir") == 0);
450   g_free (string);
451   string = g_path_get_basename ("/foo/file");
452   g_assert (strcmp (string, "file") == 0);
453   g_free (string);
454   g_print ("ok\n");
455 #endif
456
457   g_print ("checking g_path_get_dirname()...");
458   for (i = 0; i < n_dirname_checks; i++)
459     {
460       gchar *dirname;
461
462       dirname = g_path_get_dirname (dirname_checks[i].filename);
463       if (strcmp (dirname, dirname_checks[i].dirname) != 0)
464         {
465           g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
466                    dirname_checks[i].filename,
467                    dirname_checks[i].dirname,
468                    dirname);
469           n_dirname_checks = 0;
470         }
471       g_free (dirname);
472     }
473   if (n_dirname_checks)
474     g_print ("ok\n");
475
476   g_print ("checking g_path_skip_root()...");
477   for (i = 0; i < n_skip_root_checks; i++)
478     {
479       const gchar *skipped;
480
481       skipped = g_path_skip_root (skip_root_checks[i].filename);
482       if ((skipped && !skip_root_checks[i].without_root) ||
483           (!skipped && skip_root_checks[i].without_root) ||
484           ((skipped && skip_root_checks[i].without_root) &&
485            strcmp (skipped, skip_root_checks[i].without_root)))
486         {
487           g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
488                    skip_root_checks[i].filename,
489                    (skip_root_checks[i].without_root ?
490                     skip_root_checks[i].without_root : "<NULL>"),
491                    (skipped ? skipped : "<NULL>"));
492           n_skip_root_checks = 0;
493         }
494     }
495   if (n_skip_root_checks)
496     g_print ("ok\n");
497
498   g_print ("checking doubly linked lists...");
499
500   list = NULL;
501   for (i = 0; i < 10; i++)
502     list = g_list_append (list, &nums[i]);
503   list = g_list_reverse (list);
504
505   for (i = 0; i < 10; i++)
506     {
507       t = g_list_nth (list, i);
508       if (*((gint*) t->data) != (9 - i))
509         g_error ("Regular insert failed");
510     }
511
512   for (i = 0; i < 10; i++)
513     if(g_list_position(list, g_list_nth (list, i)) != i)
514       g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
515
516   g_list_free (list);
517   list = NULL;
518
519   for (i = 0; i < 10; i++)
520     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
521
522   /*
523   g_print("\n");
524   g_list_foreach (list, my_list_print, NULL);
525   */
526
527   for (i = 0; i < 10; i++)
528     {
529       t = g_list_nth (list, i);
530       if (*((gint*) t->data) != i)
531          g_error ("Sorted insert failed");
532     }
533
534   g_list_free (list);
535   list = NULL;
536
537   for (i = 0; i < 10; i++)
538     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
539
540   /*
541   g_print("\n");
542   g_list_foreach (list, my_list_print, NULL);
543   */
544
545   for (i = 0; i < 10; i++)
546     {
547       t = g_list_nth (list, i);
548       if (*((gint*) t->data) != (9 - i))
549          g_error ("Sorted insert failed");
550     }
551
552   g_list_free (list);
553   list = NULL;
554
555   for (i = 0; i < 10; i++)
556     list = g_list_prepend (list, &morenums[i]);
557
558   list = g_list_sort (list, my_list_compare_two);
559
560   /*
561   g_print("\n");
562   g_list_foreach (list, my_list_print, NULL);
563   */
564
565   for (i = 0; i < 10; i++)
566     {
567       t = g_list_nth (list, i);
568       if (*((gint*) t->data) != (9 - i))
569          g_error ("Merge sort failed");
570     }
571
572   g_list_free (list);
573
574   g_print ("ok\n");
575
576
577   g_print ("checking singly linked lists...");
578
579   slist = NULL;
580   for (i = 0; i < 10; i++)
581     slist = g_slist_append (slist, &nums[i]);
582   slist = g_slist_reverse (slist);
583
584   for (i = 0; i < 10; i++)
585     {
586       st = g_slist_nth (slist, i);
587       if (*((gint*) st->data) != (9 - i))
588         g_error ("failed");
589     }
590
591   g_slist_free (slist);
592   slist = NULL;
593
594   for (i = 0; i < 10; i++)
595     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
596
597   /*
598   g_print("\n");
599   g_slist_foreach (slist, my_list_print, NULL);
600   */
601
602   for (i = 0; i < 10; i++)
603     {
604       st = g_slist_nth (slist, i);
605       if (*((gint*) st->data) != i)
606          g_error ("Sorted insert failed");
607     }
608
609   g_slist_free(slist);
610   slist = NULL;
611
612   for (i = 0; i < 10; i++)
613     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
614
615   /*
616   g_print("\n");
617   g_slist_foreach (slist, my_list_print, NULL);
618   */
619
620   for (i = 0; i < 10; i++)
621     {
622       st = g_slist_nth (slist, i);
623       if (*((gint*) st->data) != (9 - i))
624          g_error("Sorted insert failed");
625     }
626
627   g_slist_free(slist);
628   slist = NULL;
629
630   for (i = 0; i < 10; i++)
631     slist = g_slist_prepend (slist, &morenums[i]);
632
633   slist = g_slist_sort (slist, my_list_compare_two);
634
635   /*
636   g_print("\n");
637   g_slist_foreach (slist, my_list_print, NULL);
638   */
639
640   for (i = 0; i < 10; i++)
641     {
642       st = g_slist_nth (slist, i);
643       if (*((gint*) st->data) != (9 - i))
644          g_error("Sorted insert failed");
645     }
646
647   g_slist_free(slist);
648
649   g_print ("ok\n");
650
651
652   g_print ("checking binary trees...\n");
653
654   tree = g_tree_new (my_compare);
655   i = 0;
656   for (j = 0; j < 10; j++, i++)
657     {
658       chars[i] = '0' + j;
659       g_tree_insert (tree, &chars[i], &chars[i]);
660     }
661   for (j = 0; j < 26; j++, i++)
662     {
663       chars[i] = 'A' + j;
664       g_tree_insert (tree, &chars[i], &chars[i]);
665     }
666   for (j = 0; j < 26; j++, i++)
667     {
668       chars[i] = 'a' + j;
669       g_tree_insert (tree, &chars[i], &chars[i]);
670     }
671
672   g_print ("tree height: %d\n", g_tree_height (tree));
673   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
674
675   g_print ("tree: ");
676   g_tree_foreach (tree, my_traverse, NULL);
677   g_print ("\n");
678
679   for (i = 0; i < 10; i++)
680     g_tree_remove (tree, &chars[i]);
681
682   g_print ("tree height: %d\n", g_tree_height (tree));
683   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
684
685   g_print ("tree: ");
686   g_tree_foreach (tree, my_traverse, NULL);
687   g_print ("\n");
688
689   g_print ("ok\n");
690
691
692   /* check n-way trees */
693   g_node_test ();
694
695   g_print ("checking mem chunks...");
696
697   mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
698
699   for (i = 0; i < 10000; i++)
700     {
701       mem[i] = g_chunk_new (gchar, mem_chunk);
702
703       for (j = 0; j < 50; j++)
704         mem[i][j] = i * j;
705     }
706
707   for (i = 0; i < 10000; i++)
708     {
709       g_mem_chunk_free (mem_chunk, mem[i]);
710     }
711
712   g_print ("ok\n");
713
714
715   g_print ("checking hash tables...");
716
717   hash_table = g_hash_table_new (my_hash, my_hash_equal);
718   for (i = 0; i < 10000; i++)
719     {
720       array[i] = i;
721       g_hash_table_insert (hash_table, &array[i], &array[i]);
722     }
723   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
724
725   for (i = 0; i < 10000; i++)
726     if (array[i] == 0)
727       g_print ("%d\n", i);
728
729   for (i = 0; i < 10000; i++)
730     g_hash_table_remove (hash_table, &array[i]);
731
732   for (i = 0; i < 10000; i++)
733     {
734       array[i] = i;
735       g_hash_table_insert (hash_table, &array[i], &array[i]);
736     }
737
738   if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
739       g_hash_table_size (hash_table) != 5000)
740     g_print ("bad!\n");
741
742   g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
743
744
745   g_hash_table_destroy (hash_table);
746
747   g_print ("ok\n");
748
749
750   g_print ("checking string chunks...");
751
752   string_chunk = g_string_chunk_new (1024);
753
754   for (i = 0; i < 100000; i ++)
755     {
756       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
757
758       if (strcmp ("hi pete", tmp_string) != 0)
759         g_error ("string chunks are broken.\n");
760     }
761
762   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
763
764   g_assert (tmp_string_2 != tmp_string &&
765             strcmp(tmp_string_2, tmp_string) == 0);
766
767   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
768
769   g_assert (tmp_string_2 == tmp_string);
770
771   g_string_chunk_free (string_chunk);
772
773   g_print ("ok\n");
774
775
776   g_print ("checking arrays...");
777
778   garray = g_array_new (FALSE, FALSE, sizeof (gint));
779   for (i = 0; i < 10000; i++)
780     g_array_append_val (garray, i);
781
782   for (i = 0; i < 10000; i++)
783     if (g_array_index (garray, gint, i) != i)
784       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
785
786   g_array_free (garray, TRUE);
787
788   garray = g_array_new (FALSE, FALSE, sizeof (gint));
789   for (i = 0; i < 100; i++)
790     g_array_prepend_val (garray, i);
791
792   for (i = 0; i < 100; i++)
793     if (g_array_index (garray, gint, i) != (100 - i - 1))
794       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
795
796   g_array_free (garray, TRUE);
797
798   g_print ("ok\n");
799
800
801   g_print ("checking strings...");
802
803   string1 = g_string_new ("hi pete!");
804   string2 = g_string_new ("");
805
806   g_assert (strcmp ("hi pete!", string1->str) == 0);
807
808   for (i = 0; i < 10000; i++)
809     g_string_append_c (string1, 'a'+(i%26));
810
811 #ifndef G_OS_WIN32
812   /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
813      the %10000.10000f format... */
814   g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
815                    "this pete guy sure is a wuss, like he's the number ",
816                    1,
817                    " wuss.  everyone agrees.\n",
818                    string1->str,
819                    10, 666, 15, 15, 666.666666666, 666.666666666);
820 #else
821   g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
822                    "this pete guy sure is a wuss, like he's the number ",
823                    1,
824                    " wuss.  everyone agrees.\n",
825                    string1->str,
826                    10, 666, 15, 15, 666.666666666, 666.666666666);
827 #endif
828
829   g_print ("string2 length = %lu...\n", (gulong)string2->len);
830   string2->str[70] = '\0';
831   g_print ("first 70 chars:\n%s\n", string2->str);
832   string2->str[141] = '\0';
833   g_print ("next 70 chars:\n%s\n", string2->str+71);
834   string2->str[212] = '\0';
835   g_print ("and next 70:\n%s\n", string2->str+142);
836   g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
837
838   g_string_free (string1, TRUE);
839   g_string_free (string2, TRUE);
840
841   /* append */
842   string1 = g_string_new ("firsthalf");
843   g_string_append (string1, "lasthalf");
844   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
845   g_string_free (string1, TRUE);
846
847   /* append_len */
848
849   string1 = g_string_new ("firsthalf");
850   g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
851   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
852   g_string_free (string1, TRUE);  
853   
854   /* prepend */
855   string1 = g_string_new ("lasthalf");
856   g_string_prepend (string1, "firsthalf");
857   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
858   g_string_free (string1, TRUE);
859
860   /* prepend_len */
861   string1 = g_string_new ("lasthalf");
862   g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
863   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
864   g_string_free (string1, TRUE);
865   
866   /* insert */
867   string1 = g_string_new ("firstlast");
868   g_string_insert (string1, 5, "middle");
869   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
870   g_string_free (string1, TRUE);
871
872   /* insert with pos == end of the string */
873   string1 = g_string_new ("firstmiddle");
874   g_string_insert (string1, strlen ("firstmiddle"), "last");
875   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
876   g_string_free (string1, TRUE);
877   
878   /* insert_len */
879
880   string1 = g_string_new ("firstlast");
881   g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
882   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
883   g_string_free (string1, TRUE);
884
885   /* insert_len with magic -1 pos for append */
886   string1 = g_string_new ("first");
887   g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
888   g_assert (strcmp (string1->str, "firstlast") == 0);
889   g_string_free (string1, TRUE);
890   
891   /* insert_len with magic -1 len for strlen-the-string */
892   string1 = g_string_new ("first");
893   g_string_insert_len (string1, 5, "last", -1);
894   g_assert (strcmp (string1->str, "firstlast") == 0);
895   g_string_free (string1, TRUE);
896   
897   g_print ("ok\n");
898
899   /* g_string_equal */
900   string1 = g_string_new ("test");
901   string2 = g_string_new ("te");
902   g_assert (! g_string_equal(string1, string2));
903   g_string_append (string2, "st");
904   g_assert (g_string_equal(string1, string2));
905   g_string_free (string1, TRUE);
906   g_string_free (string2, TRUE);
907   
908   /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
909   string1 = g_string_new ("fiddle");
910   string2 = g_string_new ("fiddle");
911   g_assert (g_string_equal(string1, string2));
912   g_string_append_c(string1, '\0');
913   g_assert (! g_string_equal(string1, string2));
914   g_string_append_c(string2, '\0');
915   g_assert (g_string_equal(string1, string2));
916   g_string_append_c(string1, 'x');
917   g_string_append_c(string2, 'y');
918   g_assert (! g_string_equal(string1, string2));
919   g_assert (string1->len == 8);
920   g_string_append(string1, "yzzy");
921   g_assert (string1->len == 12);
922   g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
923   g_string_insert(string1, 1, "QED");
924   g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
925   g_string_free (string1, TRUE);
926   g_string_free (string2, TRUE);
927   
928   g_print ("test positional printf formats (not supported): ");
929   string = g_strdup_printf ("%.*s%s", 5, "a", "b");
930   tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
931   g_print ("%s%s\n", string, tmp_string);
932   g_free (tmp_string);
933   g_free (string);
934
935   g_print ("checking timers...\n");
936   
937   timer = g_timer_new ();
938   g_print ("  spinning for 3 seconds...\n");
939
940   g_timer_start (timer);
941   while (g_timer_elapsed (timer, NULL) < 3)
942     ;
943
944   g_timer_stop (timer);
945   g_timer_destroy (timer);
946
947   g_print ("ok\n");
948
949   g_print ("checking g_ascii_strcasecmp...");
950   g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
951   g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
952   g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
953   g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
954   g_assert (g_ascii_strcasecmp ("", "") == 0);
955   g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
956   g_assert (g_ascii_strcasecmp ("a", "b") < 0);
957   g_assert (g_ascii_strcasecmp ("a", "B") < 0);
958   g_assert (g_ascii_strcasecmp ("A", "b") < 0);
959   g_assert (g_ascii_strcasecmp ("A", "B") < 0);
960   g_assert (g_ascii_strcasecmp ("b", "a") > 0);
961   g_assert (g_ascii_strcasecmp ("b", "A") > 0);
962   g_assert (g_ascii_strcasecmp ("B", "a") > 0);
963   g_assert (g_ascii_strcasecmp ("B", "A") > 0);
964
965   g_print ("ok\n");
966
967   g_print ("checking g_strdup...");
968   g_assert(g_strdup(NULL) == NULL);
969   string = g_strdup(GLIB_TEST_STRING);
970   g_assert(string != NULL);
971   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
972   g_free(string);
973
974   g_print ("ok\n");
975
976   g_print ("checking g_strconcat...");
977   string = g_strconcat(GLIB_TEST_STRING, NULL);
978   g_assert(string != NULL);
979   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
980   g_free(string);
981   string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING, 
982                        GLIB_TEST_STRING, NULL);
983   g_assert(string != NULL);
984   g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
985                           GLIB_TEST_STRING) == 0);
986   g_free(string);
987   g_print ("ok\n");
988   
989
990   g_print("checking g_strlcpy/g_strlcat...");
991   /* The following is a torture test for strlcpy/strlcat, with lots of
992    * checking; normal users wouldn't use them this way!
993    */
994   string = g_malloc (6);
995   *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
996   *string = 'q';
997   g_assert (g_strlcpy(string, "" , 5) == 0);
998   g_assert ( *string == '\0' );
999   *string = 'q';
1000   g_assert (g_strlcpy(string, "abc" , 5) == 3);
1001   g_assert ( *(string + 3) == '\0' );
1002   g_assert (g_str_equal(string, "abc"));
1003   g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1004   g_assert ( *(string + 4) == '\0' );
1005   g_assert ( *(string + 5) == 'Z' );
1006   g_assert (g_str_equal(string, "abcd"));
1007   g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1008   g_assert ( *(string + 4) == '\0' );
1009   g_assert ( *(string + 5) == 'Z' );
1010   g_assert (g_str_equal(string, "abcd"));
1011   g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1012   g_assert ( *(string + 4) == '\0' );
1013   g_assert ( *(string + 5) == 'Z' );
1014   g_assert (g_str_equal(string, "abcd"));
1015   *string = 'Y';
1016   *(string + 1)= '\0';
1017   g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1018   g_assert (*string == 'Y');
1019   *string = '\0';
1020   g_assert (g_strlcat(string, "123" , 5) == 3);
1021   g_assert ( *(string + 3) == '\0' );
1022   g_assert (g_str_equal(string, "123"));
1023   g_assert (g_strlcat(string, "" , 5) == 3);
1024   g_assert ( *(string + 3) == '\0' );
1025   g_assert (g_str_equal(string, "123"));
1026   g_assert (g_strlcat(string, "4", 5) == 4);
1027   g_assert (g_str_equal(string, "1234"));
1028   g_assert (g_strlcat(string, "5", 5) == 5);
1029   g_assert ( *(string + 4) == '\0' );
1030   g_assert (g_str_equal(string, "1234"));
1031   g_assert ( *(string + 5) == 'Z' );
1032   *string = 'Y';
1033   *(string + 1)= '\0';
1034   g_assert (g_strlcat(string, "123" , 0) == 3);
1035   g_assert (*string == 'Y');
1036   
1037   /* A few more tests, demonstrating more "normal" use  */
1038   g_assert (g_strlcpy(string, "hi", 5) == 2);
1039   g_assert (g_str_equal(string, "hi"));
1040   g_assert (g_strlcat(string, "t", 5) == 3);
1041   g_assert (g_str_equal(string, "hit"));
1042   g_free(string);
1043
1044   g_print ("ok\n");
1045   
1046   
1047   g_print ("checking g_strdup_printf...");
1048   string = g_strdup_printf ("%05d %-5s", 21, "test");
1049   g_assert (string != NULL);
1050   g_assert (strcmp(string, "00021 test ") == 0);
1051   g_free (string);
1052
1053   g_print ("ok\n");
1054
1055   /* g_debug (argv[0]); */
1056
1057   /* Relation tests */
1058
1059   g_print ("checking relations...");
1060
1061   relation = g_relation_new (2);
1062
1063   g_relation_index (relation, 0, g_int_hash, g_int_equal);
1064   g_relation_index (relation, 1, g_int_hash, g_int_equal);
1065
1066   for (i = 0; i < 1024; i += 1)
1067     data[i] = i;
1068
1069   for (i = 1; i < 1023; i += 1)
1070     {
1071       g_relation_insert (relation, data + i, data + i + 1);
1072       g_relation_insert (relation, data + i, data + i - 1);
1073     }
1074
1075   for (i = 2; i < 1022; i += 1)
1076     {
1077       g_assert (! g_relation_exists (relation, data + i, data + i));
1078       g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1079       g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1080     }
1081
1082   for (i = 1; i < 1023; i += 1)
1083     {
1084       g_assert (g_relation_exists (relation, data + i, data + i + 1));
1085       g_assert (g_relation_exists (relation, data + i, data + i - 1));
1086     }
1087
1088   for (i = 2; i < 1022; i += 1)
1089     {
1090       g_assert (g_relation_count (relation, data + i, 0) == 2);
1091       g_assert (g_relation_count (relation, data + i, 1) == 2);
1092     }
1093
1094   g_assert (g_relation_count (relation, data, 0) == 0);
1095
1096   g_assert (g_relation_count (relation, data + 42, 0) == 2);
1097   g_assert (g_relation_count (relation, data + 43, 1) == 2);
1098   g_assert (g_relation_count (relation, data + 41, 1) == 2);
1099   g_relation_delete (relation, data + 42, 0);
1100   g_assert (g_relation_count (relation, data + 42, 0) == 0);
1101   g_assert (g_relation_count (relation, data + 43, 1) == 1);
1102   g_assert (g_relation_count (relation, data + 41, 1) == 1);
1103
1104   tuples = g_relation_select (relation, data + 200, 0);
1105
1106   g_assert (tuples->len == 2);
1107
1108 #if 0
1109   for (i = 0; i < tuples->len; i += 1)
1110     {
1111       printf ("%d %d\n",
1112               *(gint*) g_tuples_index (tuples, i, 0),
1113               *(gint*) g_tuples_index (tuples, i, 1));
1114     }
1115 #endif
1116
1117   g_assert (g_relation_exists (relation, data + 300, data + 301));
1118   g_relation_delete (relation, data + 300, 0);
1119   g_assert (!g_relation_exists (relation, data + 300, data + 301));
1120
1121   g_tuples_destroy (tuples);
1122
1123   g_relation_destroy (relation);
1124
1125   relation = NULL;
1126
1127   g_print ("ok\n");
1128
1129   g_print ("checking pointer arrays...");
1130
1131   gparray = g_ptr_array_new ();
1132   for (i = 0; i < 10000; i++)
1133     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1134
1135   for (i = 0; i < 10000; i++)
1136     if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1137       g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1138
1139   g_ptr_array_free (gparray, TRUE);
1140
1141   g_print ("ok\n");
1142
1143
1144   g_print ("checking byte arrays...");
1145
1146   gbarray = g_byte_array_new ();
1147   for (i = 0; i < 10000; i++)
1148     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1149
1150   for (i = 0; i < 10000; i++)
1151     {
1152       g_assert (gbarray->data[4*i] == 'a');
1153       g_assert (gbarray->data[4*i+1] == 'b');
1154       g_assert (gbarray->data[4*i+2] == 'c');
1155       g_assert (gbarray->data[4*i+3] == 'd');
1156     }
1157
1158   g_byte_array_free (gbarray, TRUE);
1159   g_print ("ok\n");
1160
1161   g_printerr ("g_log tests:");
1162   g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1163   g_message ("the next warning is a test:");
1164   string = NULL;
1165   g_print (string);
1166
1167   g_print ("checking endian macros (host is ");
1168 #if G_BYTE_ORDER == G_BIG_ENDIAN
1169   g_print ("big endian)...");
1170 #else
1171   g_print ("little endian)...");
1172 #endif
1173   g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);  
1174   g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);  
1175   g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);  
1176
1177   g_print ("ok\n");
1178
1179   if (g_get_charset (&charset))
1180     g_print ("current charset is UTF-8: %s\n", charset);
1181   else
1182     g_print ("current charset is not UTF-8: %s\n", charset);
1183
1184 #ifdef G_PLATFORM_WIN32
1185   g_print ("current locale: %s\n", g_win32_getlocale ());
1186   g_print ("GLib DLL name tested for: %s\n", glib_dll);
1187
1188   g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
1189            GETTEXT_PACKAGE,
1190            g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
1191   g_print ("Ditto, or from GLib DLL name: %s\n",
1192            g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
1193   g_print ("Ditto, only from GLib DLL name: %s\n",
1194            g_win32_get_package_installation_directory (NULL, glib_dll));
1195   g_print ("locale subdirectory of GLib installation directory: %s\n",
1196            g_win32_get_package_installation_subdirectory (NULL, glib_dll, "lib\\locale"));
1197   g_print ("GTK+ 2.0 installation directory, if available: %s\n",
1198            g_win32_get_package_installation_directory ("gtk20", NULL));
1199
1200   g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
1201   g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
1202
1203 #endif
1204
1205   g_print ("checking file functions...\n");
1206
1207   strcpy (template, "foobar");
1208   fd = g_mkstemp (template);
1209   if (fd != -1)
1210     g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
1211   close (fd);
1212   strcpy (template, "fooXXXXXX");
1213   fd = g_mkstemp (template);
1214   if (fd == -1)
1215     g_print ("g_mkstemp didn't work for template %s\n", template);
1216   i = write (fd, hello, hellolen);
1217   if (i == -1)
1218     g_print ("write() failed: %s\n", g_strerror (errno));
1219   else if (i != hellolen)
1220     g_print ("write() should have written %d bytes, wrote %d\n", hellolen, i);
1221
1222   lseek (fd, 0, 0);
1223   i = read (fd, chars, sizeof (chars));
1224   if (i == -1)
1225     g_print ("read() failed: %s\n", g_strerror (errno));
1226   else if (i != hellolen)
1227     g_print ("read() should have read %d bytes, got %d\n", hellolen, i);
1228
1229   chars[i] = 0;
1230   if (strcmp (chars, hello) != 0)
1231     g_print ("wrote '%s', but got '%s'\n", hello, chars);
1232
1233   close (fd);
1234   remove (template);
1235
1236   error = NULL;
1237   strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1238   fd = g_file_open_tmp (template, &name_used, &error);
1239   if (fd != -1)
1240     g_print ("g_file_open_tmp works even if template contains '%s'\n",
1241              G_DIR_SEPARATOR_S);
1242   else
1243     g_print ("g_file_open_tmp correctly returns error: %s\n",
1244              error->message);
1245   close (fd);
1246   g_clear_error (&error);
1247
1248 #ifdef G_OS_WIN32
1249   strcpy (template, "zap/barXXXXXX");
1250   fd = g_file_open_tmp (template, &name_used, &error);
1251   if (fd != -1)
1252     g_print ("g_file_open_tmp works even if template contains '/'\n");
1253   else
1254     g_print ("g_file_open_tmp correctly returns error: %s\n",
1255              error->message);
1256   close (fd);
1257   g_clear_error (&error);
1258 #endif
1259
1260   strcpy (template, "zapXXXXXX");
1261   fd = g_file_open_tmp (template, &name_used, &error);
1262   if (fd == -1)
1263     g_print ("g_file_open_tmp didn't work for template '%s': %s\n",
1264              template, error->message);
1265   else
1266     g_print ("g_file_open_tmp for template '%s' used name '%s'\n",
1267              template, name_used);
1268   close (fd);
1269   g_clear_error (&error);
1270   remove (name_used);
1271
1272   fd = g_file_open_tmp (NULL, &name_used, &error);
1273   if (fd == -1)
1274     g_print ("g_file_open_tmp didn't work for a NULL template: %s\n",
1275              error->message);
1276   else
1277     g_print ("g_file_open_tmp for NULL template used name '%s'\n",
1278              name_used);
1279   close (fd);
1280   g_clear_error (&error);
1281   remove (name_used);
1282   
1283   return 0;
1284 }
1285