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