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