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