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