Match also UNC paths on Win32.
[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 int array[10000];
50 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     { ".", "." },
355     { "..", "." },
356     { "", "." },
357   };
358   guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
359
360   struct {
361     gchar *filename;
362     gchar *without_root;
363   } skip_root_checks[] = {
364 #ifndef G_OS_WIN32
365     { "/", "" },
366     { "//", "" },
367     { "/foo", "foo" },
368     { "//foo", "foo" },
369     { "a/b", NULL },
370 #else
371     { "\\", "" },
372     { "\\foo", "foo" },
373     { "\\\\server\\foo", "" },
374     { "\\\\server\\foo\\bar", "bar" },
375     { "a\\b", NULL },
376 #endif
377     { ".", NULL },
378     { "", NULL },
379   };
380   guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
381
382   guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
383   guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
384 #ifdef G_HAVE_GINT64
385   guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
386           gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
387 #endif
388   const char hello[] = "Hello, World";
389   const int hellolen = sizeof (hello) - 1;
390   int fd;
391   char template[10];
392   GError *error;
393   char *name_used;
394   gchar *p;
395 #ifdef G_OS_WIN32
396   gchar *glib_dll = g_strdup_printf ("glib-%d.%d.dll",
397                                      GLIB_MAJOR_VERSION,
398                                      GLIB_MINOR_VERSION);
399 #endif
400
401   g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
402            glib_major_version,
403            glib_minor_version,
404            glib_micro_version,
405            glib_interface_age,
406            glib_binary_age);
407
408   string = g_get_current_dir ();
409   g_print ("cwd: %s\n", string);
410   g_free (string);
411   g_print ("user: %s\n", g_get_user_name ());
412   g_print ("real: %s\n", g_get_real_name ());
413   g_print ("home: %s\n", g_get_home_dir ());
414   g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
415
416   /* type sizes */
417   g_print ("checking size of gint8: %d", (int)sizeof (gint8));
418   TEST (NULL, sizeof (gint8) == 1);
419   g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
420   TEST (NULL, sizeof (gint16) == 2);
421   g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
422   TEST (NULL, sizeof (gint32) == 4);
423   g_print ("\nchecking size of gsize: %d", (int)sizeof (gsize));
424 #ifdef  G_HAVE_GINT64
425   g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
426   TEST (NULL, sizeof (gint64) == 8);
427 #endif  /* G_HAVE_GINT64 */
428   g_print ("\n");
429
430   g_print ("checking g_path_get_dirname()...");
431   for (i = 0; i < n_dirname_checks; i++)
432     {
433       gchar *dirname;
434
435       dirname = g_path_get_dirname (dirname_checks[i].filename);
436       if (strcmp (dirname, dirname_checks[i].dirname) != 0)
437         {
438           g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
439                    dirname_checks[i].filename,
440                    dirname_checks[i].dirname,
441                    dirname);
442           n_dirname_checks = 0;
443         }
444       g_free (dirname);
445     }
446   if (n_dirname_checks)
447     g_print ("ok\n");
448
449   g_print ("checking g_path_skip_root()...");
450   for (i = 0; i < n_skip_root_checks; i++)
451     {
452       const gchar *skipped;
453
454       skipped = g_path_skip_root (skip_root_checks[i].filename);
455       if ((skipped && !skip_root_checks[i].without_root) ||
456           (!skipped && skip_root_checks[i].without_root) ||
457           ((skipped && skip_root_checks[i].without_root) &&
458            strcmp (skipped, skip_root_checks[i].without_root)))
459         {
460           g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
461                    skip_root_checks[i].filename,
462                    (skip_root_checks[i].without_root ?
463                     skip_root_checks[i].without_root : "<NULL>"),
464                    (skipped ? skipped : "<NULL>"));
465           n_skip_root_checks = 0;
466         }
467     }
468   if (n_skip_root_checks)
469     g_print ("ok\n");
470
471   g_print ("checking doubly linked lists...");
472
473   list = NULL;
474   for (i = 0; i < 10; i++)
475     list = g_list_append (list, &nums[i]);
476   list = g_list_reverse (list);
477
478   for (i = 0; i < 10; i++)
479     {
480       t = g_list_nth (list, i);
481       if (*((gint*) t->data) != (9 - i))
482         g_error ("Regular insert failed");
483     }
484
485   for (i = 0; i < 10; i++)
486     if(g_list_position(list, g_list_nth (list, i)) != i)
487       g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
488
489   g_list_free (list);
490   list = NULL;
491
492   for (i = 0; i < 10; i++)
493     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
494
495   /*
496   g_print("\n");
497   g_list_foreach (list, my_list_print, NULL);
498   */
499
500   for (i = 0; i < 10; i++)
501     {
502       t = g_list_nth (list, i);
503       if (*((gint*) t->data) != i)
504          g_error ("Sorted insert failed");
505     }
506
507   g_list_free (list);
508   list = NULL;
509
510   for (i = 0; i < 10; i++)
511     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
512
513   /*
514   g_print("\n");
515   g_list_foreach (list, my_list_print, NULL);
516   */
517
518   for (i = 0; i < 10; i++)
519     {
520       t = g_list_nth (list, i);
521       if (*((gint*) t->data) != (9 - i))
522          g_error ("Sorted insert failed");
523     }
524
525   g_list_free (list);
526   list = NULL;
527
528   for (i = 0; i < 10; i++)
529     list = g_list_prepend (list, &morenums[i]);
530
531   list = g_list_sort (list, my_list_compare_two);
532
533   /*
534   g_print("\n");
535   g_list_foreach (list, my_list_print, NULL);
536   */
537
538   for (i = 0; i < 10; i++)
539     {
540       t = g_list_nth (list, i);
541       if (*((gint*) t->data) != (9 - i))
542          g_error ("Merge sort failed");
543     }
544
545   g_list_free (list);
546
547   g_print ("ok\n");
548
549
550   g_print ("checking singly linked lists...");
551
552   slist = NULL;
553   for (i = 0; i < 10; i++)
554     slist = g_slist_append (slist, &nums[i]);
555   slist = g_slist_reverse (slist);
556
557   for (i = 0; i < 10; i++)
558     {
559       st = g_slist_nth (slist, i);
560       if (*((gint*) st->data) != (9 - i))
561         g_error ("failed");
562     }
563
564   g_slist_free (slist);
565   slist = NULL;
566
567   for (i = 0; i < 10; i++)
568     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
569
570   /*
571   g_print("\n");
572   g_slist_foreach (slist, my_list_print, NULL);
573   */
574
575   for (i = 0; i < 10; i++)
576     {
577       st = g_slist_nth (slist, i);
578       if (*((gint*) st->data) != i)
579          g_error ("Sorted insert failed");
580     }
581
582   g_slist_free(slist);
583   slist = NULL;
584
585   for (i = 0; i < 10; i++)
586     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
587
588   /*
589   g_print("\n");
590   g_slist_foreach (slist, my_list_print, NULL);
591   */
592
593   for (i = 0; i < 10; i++)
594     {
595       st = g_slist_nth (slist, i);
596       if (*((gint*) st->data) != (9 - i))
597          g_error("Sorted insert failed");
598     }
599
600   g_slist_free(slist);
601   slist = NULL;
602
603   for (i = 0; i < 10; i++)
604     slist = g_slist_prepend (slist, &morenums[i]);
605
606   slist = g_slist_sort (slist, my_list_compare_two);
607
608   /*
609   g_print("\n");
610   g_slist_foreach (slist, my_list_print, NULL);
611   */
612
613   for (i = 0; i < 10; i++)
614     {
615       st = g_slist_nth (slist, i);
616       if (*((gint*) st->data) != (9 - i))
617          g_error("Sorted insert failed");
618     }
619
620   g_slist_free(slist);
621
622   g_print ("ok\n");
623
624
625   g_print ("checking binary trees...\n");
626
627   tree = g_tree_new (my_compare);
628   i = 0;
629   for (j = 0; j < 10; j++, i++)
630     {
631       chars[i] = '0' + j;
632       g_tree_insert (tree, &chars[i], &chars[i]);
633     }
634   for (j = 0; j < 26; j++, i++)
635     {
636       chars[i] = 'A' + j;
637       g_tree_insert (tree, &chars[i], &chars[i]);
638     }
639   for (j = 0; j < 26; j++, i++)
640     {
641       chars[i] = 'a' + j;
642       g_tree_insert (tree, &chars[i], &chars[i]);
643     }
644
645   g_print ("tree height: %d\n", g_tree_height (tree));
646   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
647
648   g_print ("tree: ");
649   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
650   g_print ("\n");
651
652   for (i = 0; i < 10; i++)
653     g_tree_remove (tree, &chars[i]);
654
655   g_print ("tree height: %d\n", g_tree_height (tree));
656   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
657
658   g_print ("tree: ");
659   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
660   g_print ("\n");
661
662   g_print ("ok\n");
663
664
665   /* check n-way trees */
666   g_node_test ();
667
668   g_print ("checking mem chunks...");
669
670   mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
671
672   for (i = 0; i < 10000; i++)
673     {
674       mem[i] = g_chunk_new (gchar, mem_chunk);
675
676       for (j = 0; j < 50; j++)
677         mem[i][j] = i * j;
678     }
679
680   for (i = 0; i < 10000; i++)
681     {
682       g_mem_chunk_free (mem_chunk, mem[i]);
683     }
684
685   g_print ("ok\n");
686
687
688   g_print ("checking hash tables...");
689
690   hash_table = g_hash_table_new (my_hash, my_hash_equal);
691   for (i = 0; i < 10000; i++)
692     {
693       array[i] = i;
694       g_hash_table_insert (hash_table, &array[i], &array[i]);
695     }
696   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
697
698   for (i = 0; i < 10000; i++)
699     if (array[i] == 0)
700       g_print ("%d\n", i);
701
702   for (i = 0; i < 10000; i++)
703     g_hash_table_remove (hash_table, &array[i]);
704
705   for (i = 0; i < 10000; i++)
706     {
707       array[i] = i;
708       g_hash_table_insert (hash_table, &array[i], &array[i]);
709     }
710
711   if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
712       g_hash_table_size (hash_table) != 5000)
713     g_print ("bad!\n");
714
715   g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
716
717
718   g_hash_table_destroy (hash_table);
719
720   g_print ("ok\n");
721
722
723   g_print ("checking string chunks...");
724
725   string_chunk = g_string_chunk_new (1024);
726
727   for (i = 0; i < 100000; i ++)
728     {
729       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
730
731       if (strcmp ("hi pete", tmp_string) != 0)
732         g_error ("string chunks are broken.\n");
733     }
734
735   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
736
737   g_assert (tmp_string_2 != tmp_string &&
738             strcmp(tmp_string_2, tmp_string) == 0);
739
740   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
741
742   g_assert (tmp_string_2 == tmp_string);
743
744   g_string_chunk_free (string_chunk);
745
746   g_print ("ok\n");
747
748
749   g_print ("checking arrays...");
750
751   garray = g_array_new (FALSE, FALSE, sizeof (gint));
752   for (i = 0; i < 10000; i++)
753     g_array_append_val (garray, i);
754
755   for (i = 0; i < 10000; i++)
756     if (g_array_index (garray, gint, i) != i)
757       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
758
759   g_array_free (garray, TRUE);
760
761   garray = g_array_new (FALSE, FALSE, sizeof (gint));
762   for (i = 0; i < 100; i++)
763     g_array_prepend_val (garray, i);
764
765   for (i = 0; i < 100; i++)
766     if (g_array_index (garray, gint, i) != (100 - i - 1))
767       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
768
769   g_array_free (garray, TRUE);
770
771   g_print ("ok\n");
772
773
774   g_print ("checking strings...");
775
776   string1 = g_string_new ("hi pete!");
777   string2 = g_string_new ("");
778
779   g_assert (strcmp ("hi pete!", string1->str) == 0);
780
781   for (i = 0; i < 10000; i++)
782     g_string_append_c (string1, 'a'+(i%26));
783
784 #ifndef G_OS_WIN32
785   /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
786      the %10000.10000f format... */
787   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
788                     "this pete guy sure is a wuss, like he's the number ",
789                     1,
790                     " wuss.  everyone agrees.\n",
791                     string1->str,
792                     10, 666, 15, 15, 666.666666666, 666.666666666);
793 #else
794   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
795                     "this pete guy sure is a wuss, like he's the number ",
796                     1,
797                     " wuss.  everyone agrees.\n",
798                     string1->str,
799                     10, 666, 15, 15, 666.666666666, 666.666666666);
800 #endif
801
802   g_print ("string2 length = %d...\n", string2->len);
803   string2->str[70] = '\0';
804   g_print ("first 70 chars:\n%s\n", string2->str);
805   string2->str[141] = '\0';
806   g_print ("next 70 chars:\n%s\n", string2->str+71);
807   string2->str[212] = '\0';
808   g_print ("and next 70:\n%s\n", string2->str+142);
809   g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
810
811   g_string_free (string1, TRUE);
812   g_string_free (string2, TRUE);
813
814   /* append */
815   string1 = g_string_new ("firsthalf");
816   g_string_append (string1, "lasthalf");
817   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
818   g_string_free (string1, TRUE);
819
820   /* append_len */
821
822   string1 = g_string_new ("firsthalf");
823   g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
824   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
825   g_string_free (string1, TRUE);  
826   
827   /* prepend */
828   string1 = g_string_new ("lasthalf");
829   g_string_prepend (string1, "firsthalf");
830   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
831   g_string_free (string1, TRUE);
832
833   /* prepend_len */
834   string1 = g_string_new ("lasthalf");
835   g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
836   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
837   g_string_free (string1, TRUE);
838   
839   /* insert */
840   string1 = g_string_new ("firstlast");
841   g_string_insert (string1, 5, "middle");
842   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
843   g_string_free (string1, TRUE);
844
845   /* insert with pos == end of the string */
846   string1 = g_string_new ("firstmiddle");
847   g_string_insert (string1, strlen ("firstmiddle"), "last");
848   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
849   g_string_free (string1, TRUE);
850   
851   /* insert_len */
852
853   string1 = g_string_new ("firstlast");
854   g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
855   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
856   g_string_free (string1, TRUE);
857
858   /* insert_len with magic -1 pos for append */
859   string1 = g_string_new ("first");
860   g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
861   g_assert (strcmp (string1->str, "firstlast") == 0);
862   g_string_free (string1, TRUE);
863   
864   /* insert_len with magic -1 len for strlen-the-string */
865   string1 = g_string_new ("first");
866   g_string_insert_len (string1, 5, "last", -1);
867   g_assert (strcmp (string1->str, "firstlast") == 0);
868   g_string_free (string1, TRUE);
869   
870   g_print ("ok\n");
871
872   /* g_string_equal */
873   string1 = g_string_new ("test");
874   string2 = g_string_new ("te");
875   g_assert (! g_string_equal(string1, string2));
876   g_string_append (string2, "st");
877   g_assert (g_string_equal(string1, string2));
878   g_string_free (string1, TRUE);
879   g_string_free (string2, TRUE);
880   
881   /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
882   string1 = g_string_new ("fiddle");
883   string2 = g_string_new ("fiddle");
884   g_assert (g_string_equal(string1, string2));
885   g_string_append_c(string1, '\0');
886   g_assert (! g_string_equal(string1, string2));
887   g_string_append_c(string2, '\0');
888   g_assert (g_string_equal(string1, string2));
889   g_string_append_c(string1, 'x');
890   g_string_append_c(string2, 'y');
891   g_assert (! g_string_equal(string1, string2));
892   g_assert (string1->len == 8);
893   g_string_append(string1, "yzzy");
894   g_assert (string1->len == 12);
895   g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
896   g_string_insert(string1, 1, "QED");
897   g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
898   g_string_free (string1, TRUE);
899   g_string_free (string2, TRUE);
900   
901   g_print ("test positional printf formats (not supported): ");
902   string = g_strdup_printf ("%.*s%s", 5, "a", "b");
903   tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
904   g_print ("%s%s\n", string, tmp_string);
905   g_free (tmp_string);
906   g_free (string);
907
908   g_print ("checking timers...\n");
909   
910   timer = g_timer_new ();
911   g_print ("  spinning for 3 seconds...\n");
912
913   g_timer_start (timer);
914   while (g_timer_elapsed (timer, NULL) < 3)
915     ;
916
917   g_timer_stop (timer);
918   g_timer_destroy (timer);
919
920   g_print ("ok\n");
921
922   g_print ("checking g_strcasecmp...");
923   g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0);
924   g_assert (g_strcasecmp ("frobozz", "frobozz") == 0);
925   g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0);
926   g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0);
927   g_assert (g_strcasecmp ("", "") == 0);
928   g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0);
929   g_assert (g_strcasecmp ("a", "b") < 0);
930   g_assert (g_strcasecmp ("a", "B") < 0);
931   g_assert (g_strcasecmp ("A", "b") < 0);
932   g_assert (g_strcasecmp ("A", "B") < 0);
933   g_assert (g_strcasecmp ("b", "a") > 0);
934   g_assert (g_strcasecmp ("b", "A") > 0);
935   g_assert (g_strcasecmp ("B", "a") > 0);
936   g_assert (g_strcasecmp ("B", "A") > 0);
937
938   g_print ("ok\n");
939
940   g_print ("checking g_strdup...");
941   g_assert(g_strdup(NULL) == NULL);
942   string = g_strdup(GLIB_TEST_STRING);
943   g_assert(string != NULL);
944   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
945   g_free(string);
946
947   g_print ("ok\n");
948
949   g_print ("checking g_strconcat...");
950   string = g_strconcat(GLIB_TEST_STRING, NULL);
951   g_assert(string != NULL);
952   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
953   g_free(string);
954   string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING, 
955                        GLIB_TEST_STRING, NULL);
956   g_assert(string != NULL);
957   g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
958                           GLIB_TEST_STRING) == 0);
959   g_free(string);
960   g_print ("ok\n");
961   
962
963   g_print("checking g_strlcpy/g_strlcat...");
964   /* The following is a torture test for strlcpy/strlcat, with lots of
965    * checking; normal users wouldn't use them this way!
966    */
967   string = g_malloc (6);
968   *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
969   *string = 'q';
970   g_assert (g_strlcpy(string, "" , 5) == 0);
971   g_assert ( *string == '\0' );
972   *string = 'q';
973   g_assert (g_strlcpy(string, "abc" , 5) == 3);
974   g_assert ( *(string + 3) == '\0' );
975   g_assert (g_str_equal(string, "abc"));
976   g_assert (g_strlcpy(string, "abcd" , 5) == 4);
977   g_assert ( *(string + 4) == '\0' );
978   g_assert ( *(string + 5) == 'Z' );
979   g_assert (g_str_equal(string, "abcd"));
980   g_assert (g_strlcpy(string, "abcde" , 5) == 5);
981   g_assert ( *(string + 4) == '\0' );
982   g_assert ( *(string + 5) == 'Z' );
983   g_assert (g_str_equal(string, "abcd"));
984   g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
985   g_assert ( *(string + 4) == '\0' );
986   g_assert ( *(string + 5) == 'Z' );
987   g_assert (g_str_equal(string, "abcd"));
988   *string = 'Y';
989   *(string + 1)= '\0';
990   g_assert (g_strlcpy(string, "Hello" , 0) == 5);
991   g_assert (*string == 'Y');
992   *string = '\0';
993   g_assert (g_strlcat(string, "123" , 5) == 3);
994   g_assert ( *(string + 3) == '\0' );
995   g_assert (g_str_equal(string, "123"));
996   g_assert (g_strlcat(string, "" , 5) == 3);
997   g_assert ( *(string + 3) == '\0' );
998   g_assert (g_str_equal(string, "123"));
999   g_assert (g_strlcat(string, "4", 5) == 4);
1000   g_assert (g_str_equal(string, "1234"));
1001   g_assert (g_strlcat(string, "5", 5) == 5);
1002   g_assert ( *(string + 4) == '\0' );
1003   g_assert (g_str_equal(string, "1234"));
1004   g_assert ( *(string + 5) == 'Z' );
1005   *string = 'Y';
1006   *(string + 1)= '\0';
1007   g_assert (g_strlcat(string, "123" , 0) == 3);
1008   g_assert (*string == 'Y');
1009   
1010   /* A few more tests, demonstrating more "normal" use  */
1011   g_assert (g_strlcpy(string, "hi", 5) == 2);
1012   g_assert (g_str_equal(string, "hi"));
1013   g_assert (g_strlcat(string, "t", 5) == 3);
1014   g_assert (g_str_equal(string, "hit"));
1015   g_free(string);
1016
1017   g_print ("ok\n");
1018   
1019   
1020   g_print ("checking g_strdup_printf...");
1021   string = g_strdup_printf ("%05d %-5s", 21, "test");
1022   g_assert (string != NULL);
1023   g_assert (strcmp(string, "00021 test ") == 0);
1024   g_free (string);
1025
1026   g_print ("ok\n");
1027
1028   /* g_debug (argv[0]); */
1029
1030   /* Relation tests */
1031
1032   g_print ("checking relations...");
1033
1034   relation = g_relation_new (2);
1035
1036   g_relation_index (relation, 0, g_int_hash, g_int_equal);
1037   g_relation_index (relation, 1, g_int_hash, g_int_equal);
1038
1039   for (i = 0; i < 1024; i += 1)
1040     data[i] = i;
1041
1042   for (i = 1; i < 1023; i += 1)
1043     {
1044       g_relation_insert (relation, data + i, data + i + 1);
1045       g_relation_insert (relation, data + i, data + i - 1);
1046     }
1047
1048   for (i = 2; i < 1022; i += 1)
1049     {
1050       g_assert (! g_relation_exists (relation, data + i, data + i));
1051       g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1052       g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1053     }
1054
1055   for (i = 1; i < 1023; i += 1)
1056     {
1057       g_assert (g_relation_exists (relation, data + i, data + i + 1));
1058       g_assert (g_relation_exists (relation, data + i, data + i - 1));
1059     }
1060
1061   for (i = 2; i < 1022; i += 1)
1062     {
1063       g_assert (g_relation_count (relation, data + i, 0) == 2);
1064       g_assert (g_relation_count (relation, data + i, 1) == 2);
1065     }
1066
1067   g_assert (g_relation_count (relation, data, 0) == 0);
1068
1069   g_assert (g_relation_count (relation, data + 42, 0) == 2);
1070   g_assert (g_relation_count (relation, data + 43, 1) == 2);
1071   g_assert (g_relation_count (relation, data + 41, 1) == 2);
1072   g_relation_delete (relation, data + 42, 0);
1073   g_assert (g_relation_count (relation, data + 42, 0) == 0);
1074   g_assert (g_relation_count (relation, data + 43, 1) == 1);
1075   g_assert (g_relation_count (relation, data + 41, 1) == 1);
1076
1077   tuples = g_relation_select (relation, data + 200, 0);
1078
1079   g_assert (tuples->len == 2);
1080
1081 #if 0
1082   for (i = 0; i < tuples->len; i += 1)
1083     {
1084       printf ("%d %d\n",
1085               *(gint*) g_tuples_index (tuples, i, 0),
1086               *(gint*) g_tuples_index (tuples, i, 1));
1087     }
1088 #endif
1089
1090   g_assert (g_relation_exists (relation, data + 300, data + 301));
1091   g_relation_delete (relation, data + 300, 0);
1092   g_assert (!g_relation_exists (relation, data + 300, data + 301));
1093
1094   g_tuples_destroy (tuples);
1095
1096   g_relation_destroy (relation);
1097
1098   relation = NULL;
1099
1100   g_print ("ok\n");
1101
1102   g_print ("checking pointer arrays...");
1103
1104   gparray = g_ptr_array_new ();
1105   for (i = 0; i < 10000; i++)
1106     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1107
1108   for (i = 0; i < 10000; i++)
1109     if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1110       g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1111
1112   g_ptr_array_free (gparray, TRUE);
1113
1114   g_print ("ok\n");
1115
1116
1117   g_print ("checking byte arrays...");
1118
1119   gbarray = g_byte_array_new ();
1120   for (i = 0; i < 10000; i++)
1121     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1122
1123   for (i = 0; i < 10000; i++)
1124     {
1125       g_assert (gbarray->data[4*i] == 'a');
1126       g_assert (gbarray->data[4*i+1] == 'b');
1127       g_assert (gbarray->data[4*i+2] == 'c');
1128       g_assert (gbarray->data[4*i+3] == 'd');
1129     }
1130
1131   g_byte_array_free (gbarray, TRUE);
1132   g_print ("ok\n");
1133
1134   g_printerr ("g_log tests:");
1135   g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1136   g_message ("the next warning is a test:");
1137   string = NULL;
1138   g_print (string);
1139
1140   g_print ("checking endian macros (host is ");
1141 #if G_BYTE_ORDER == G_BIG_ENDIAN
1142   g_print ("big endian)...");
1143 #else
1144   g_print ("little endian)...");
1145 #endif
1146   g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);  
1147   g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);  
1148 #ifdef G_HAVE_GINT64
1149   g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);  
1150 #endif
1151
1152   g_print ("ok\n");
1153
1154 #ifdef G_OS_WIN32
1155   g_print ("current locale: %s\n", g_win32_getlocale ());
1156
1157   g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
1158            GETTEXT_PACKAGE,
1159            g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
1160   g_print ("Ditto, or from GLib DLL name: %s\n",
1161            g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
1162   g_print ("Ditto, only from GLib DLL name: %s\n",
1163            g_win32_get_package_installation_directory (NULL, glib_dll));
1164   g_print ("locale subdirectory of GLib installation directory: %s\n",
1165            g_win32_get_package_installation_subdirectory (NULL, glib_dll, "locale"));
1166   g_print ("GTK+ 2.0 installation directory, if available: %s\n",
1167            g_win32_get_package_installation_directory ("gtk20", NULL));
1168 #endif
1169
1170   g_print ("checking file functions...\n");
1171
1172   strcpy (template, "foobar");
1173   fd = g_mkstemp (template);
1174   if (fd != -1)
1175     g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
1176   close (fd);
1177   strcpy (template, "fooXXXXXX");
1178   fd = g_mkstemp (template);
1179   if (fd == -1)
1180     g_print ("g_mkstemp didn't work for template %s\n", template);
1181   i = write (fd, hello, hellolen);
1182   if (i == -1)
1183     g_print ("write() failed: %s\n", g_strerror (errno));
1184   else if (i != hellolen)
1185     g_print ("write() should have written %d bytes, wrote %d\n", hellolen, i);
1186
1187   lseek (fd, 0, 0);
1188   i = read (fd, chars, sizeof (chars));
1189   if (i == -1)
1190     g_print ("read() failed: %s\n", g_strerror (errno));
1191   else if (i != hellolen)
1192     g_print ("read() should have read %d bytes, got %d\n", hellolen, i);
1193
1194   chars[i] = 0;
1195   if (strcmp (chars, hello) != 0)
1196     g_print ("wrote '%s', but got '%s'\n", hello, chars);
1197
1198   close (fd);
1199   remove (template);
1200
1201   strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1202   fd = g_file_open_tmp (template, &name_used, &error);
1203   if (fd != -1)
1204     g_print ("g_file_open_tmp works even if template contains '%s'\n",
1205              G_DIR_SEPARATOR_S);
1206   else
1207     g_print ("g_file_open_tmp correctly returns error: %s\n",
1208              error->message);
1209   close (fd);
1210   g_clear_error (&error);
1211
1212   strcpy (template, "zapXXXXXX");
1213   fd = g_file_open_tmp (template, &name_used, &error);
1214   if (fd == -1)
1215     g_print ("g_file_open_tmp didn't work for template '%s': %s\n",
1216              template, error->message);
1217   else
1218     g_print ("g_file_open_tmp for template '%s' used name '%s'\n",
1219              template, name_used);
1220   close (fd);
1221   g_clear_error (&error);
1222   remove (name_used);
1223
1224   fd = g_file_open_tmp (NULL, &name_used, &error);
1225   if (fd == -1)
1226     g_print ("g_file_open_tmp didn't work for a NULL template: %s\n",
1227              error->message);
1228   else
1229     g_print ("g_file_open_tmp for NULL template used name '%s'\n",
1230              name_used);
1231   close (fd);
1232   g_clear_error (&error);
1233   remove (name_used);
1234   
1235   return 0;
1236 }
1237