Don't read past the end of the string. (#334471, Morten Welinder)
[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   
480   gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
481   gint i, j;
482   GArray *garray;
483   GPtrArray *gparray;
484   GByteArray *gbarray;
485   GString *string1, *string2;
486   const gchar *charset;
487   GTree *tree;
488   char chars[62];
489   GRelation *relation;
490   GTuples *tuples;
491   gint data [1024];
492   struct {
493     gchar *filename;
494     gchar *dirname;
495   } dirname_checks[] = {
496     { "/", "/" },
497     { "////", "/" },
498     { ".////", "." },
499     { "../", ".." },
500     { "..////", ".." },
501     { "a/b", "a" },
502     { "a/b/", "a/b" },
503     { "c///", "c" },
504 #ifdef G_OS_WIN32
505     { "\\", "\\" },
506     { ".\\\\\\\\", "." },
507     { "..\\", ".." },
508     { "..\\\\\\\\", ".." },
509     { "a\\b", "a" },
510     { "a\\b/", "a\\b" },
511     { "a/b\\", "a/b" },
512     { "c\\\\/", "c" },
513     { "//\\", "/" },
514 #endif
515 #ifdef G_WITH_CYGWIN
516     { "//server/share///x", "//server/share" },
517 #endif
518     { ".", "." },
519     { "..", "." },
520     { "", "." },
521   };
522   guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
523
524   struct {
525     gchar *filename;
526     gchar *without_root;
527   } skip_root_checks[] = {
528     { "/", "" },
529     { "//", "" },
530     { "/foo", "foo" },
531     { "//foo", "foo" },
532     { "a/b", NULL },
533 #ifdef G_OS_WIN32
534     { "\\", "" },
535     { "\\foo", "foo" },
536     { "\\\\server\\foo", "" },
537     { "\\\\server\\foo\\bar", "bar" },
538     { "a\\b", NULL },
539 #endif
540 #ifdef G_WITH_CYGWIN
541     { "//server/share///x", "//x" },
542 #endif
543     { ".", NULL },
544     { "", NULL },
545   };
546   guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
547
548 #ifndef G_DISABLE_ASSERT
549   guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
550   guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
551   guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
552           gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
553 #endif
554   const char hello[] = "Hello, World";
555   const int hellolen = sizeof (hello) - 1;
556   int fd;
557   char template[32];
558   GError *error;
559   char *name_used;
560 #ifdef G_OS_WIN32
561   /* Can't calculate GLib DLL name at runtime. */
562   gchar *glib_dll = "libglib-2.0-0.dll";
563 #endif
564 #ifdef G_WITH_CYGWIN
565   gchar *glib_dll = "cygglib-2.0-0.dll";
566 #endif
567
568   g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
569            glib_major_version,
570            glib_minor_version,
571            glib_micro_version,
572            glib_interface_age,
573            glib_binary_age);
574
575   string = g_get_current_dir ();
576   g_print ("cwd: %s\n", string);
577   g_free (string);
578   g_print ("user: %s\n", g_get_user_name ());
579   g_print ("real: %s\n", g_get_real_name ());
580   g_print ("host: %s\n", g_get_host_name ());
581   s = g_get_home_dir ();
582   g_print ("home: %s\n", s ? s : "NULL!");
583   s = g_get_user_data_dir ();
584   g_print ("user_data: %s\n", s ? s : "NULL!");
585   s = g_get_user_config_dir ();
586   g_print ("user_config: %s\n", s ? s : "NULL!");
587   s = g_get_user_cache_dir ();
588   g_print ("user_cache: %s\n", s ? s : "NULL!");
589   sv = (gchar **) g_get_system_data_dirs ();
590   g_print ("system_data: %s\n", sv ? g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv) : "NULL!");
591   sv = (gchar **) g_get_system_config_dirs ();
592   g_print ("system_config: %s\n", s ? g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv) : "NULL!");
593   g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
594   sv = (gchar **) g_get_language_names ();
595   g_print ("languages: %s\n", s ? g_strjoinv (":", sv) : "NULL!");
596
597   /* type sizes */
598   g_print ("checking size of gint8: %"    G_GSIZE_FORMAT, sizeof (gint8));
599   TEST (NULL, sizeof (gint8) == 1);
600   g_print ("\nchecking size of gint16: %" G_GSIZE_FORMAT, sizeof (gint16));
601   TEST (NULL, sizeof (gint16) == 2);
602   g_print ("\nchecking size of gint32: %" G_GSIZE_FORMAT, sizeof (gint32));
603   TEST (NULL, sizeof (gint32) == 4);
604   g_print ("\nchecking size of gsize: %"  G_GSIZE_FORMAT, sizeof (gsize));
605   g_print ("\nchecking size of gint64: %" G_GSIZE_FORMAT, sizeof (gint64));
606   TEST (NULL, sizeof (gint64) == 8);
607   g_print ("\n");
608
609   g_print ("checking g_path_get_basename()...");
610   string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
611   g_assert (strcmp (string, "dir") == 0);
612   g_free (string);
613   string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
614   g_assert (strcmp (string, "file") == 0);
615   g_free (string);
616   g_print ("ok\n");
617 #ifdef G_OS_WIN32
618   string = g_path_get_basename ("/foo/dir/");
619   g_assert (strcmp (string, "dir") == 0);
620   g_free (string);
621   string = g_path_get_basename ("/foo/file");
622   g_assert (strcmp (string, "file") == 0);
623   g_free (string);
624 #endif
625
626   g_print ("checking g_path_get_dirname()...");
627   for (i = 0; i < n_dirname_checks; i++)
628     {
629       gchar *dirname;
630
631       dirname = g_path_get_dirname (dirname_checks[i].filename);
632       if (strcmp (dirname, dirname_checks[i].dirname) != 0)
633         {
634           g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
635                    dirname_checks[i].filename,
636                    dirname_checks[i].dirname,
637                    dirname);
638           n_dirname_checks = 0;
639         }
640       g_free (dirname);
641     }
642   if (n_dirname_checks)
643     g_print ("ok\n");
644
645   g_print ("checking g_path_skip_root()...");
646   for (i = 0; i < n_skip_root_checks; i++)
647     {
648       const gchar *skipped;
649
650       skipped = g_path_skip_root (skip_root_checks[i].filename);
651       if ((skipped && !skip_root_checks[i].without_root) ||
652           (!skipped && skip_root_checks[i].without_root) ||
653           ((skipped && skip_root_checks[i].without_root) &&
654            strcmp (skipped, skip_root_checks[i].without_root)))
655         {
656           g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
657                    skip_root_checks[i].filename,
658                    (skip_root_checks[i].without_root ?
659                     skip_root_checks[i].without_root : "<NULL>"),
660                    (skipped ? skipped : "<NULL>"));
661           n_skip_root_checks = 0;
662         }
663     }
664   if (n_skip_root_checks)
665     g_print ("ok\n");
666
667   if (test_g_mkdir_with_parents ())
668     g_print ("ok\n");
669
670   g_print ("checking doubly linked lists...");
671
672   list = NULL;
673   for (i = 0; i < 10; i++)
674     list = g_list_append (list, &nums[i]);
675   list = g_list_reverse (list);
676
677   for (i = 0; i < 10; i++)
678     {
679       t = g_list_nth (list, i);
680       if (*((gint*) t->data) != (9 - i))
681         g_error ("Regular insert failed");
682     }
683
684   for (i = 0; i < 10; i++)
685     if(g_list_position(list, g_list_nth (list, i)) != i)
686       g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
687
688   g_list_free (list);
689   list = NULL;
690
691   for (i = 0; i < 10; i++)
692     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
693
694   /*
695   g_print("\n");
696   g_list_foreach (list, my_list_print, NULL);
697   */
698
699   for (i = 0; i < 10; i++)
700     {
701       t = g_list_nth (list, i);
702       if (*((gint*) t->data) != i)
703          g_error ("Sorted insert failed");
704     }
705
706   g_list_free (list);
707   list = NULL;
708
709   for (i = 0; i < 10; i++)
710     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
711
712   /*
713   g_print("\n");
714   g_list_foreach (list, my_list_print, NULL);
715   */
716
717   for (i = 0; i < 10; i++)
718     {
719       t = g_list_nth (list, i);
720       if (*((gint*) t->data) != (9 - i))
721          g_error ("Sorted insert failed");
722     }
723
724   g_list_free (list);
725   list = NULL;
726
727   for (i = 0; i < 10; i++)
728     list = g_list_prepend (list, &morenums[i]);
729
730   list = g_list_sort (list, my_list_compare_two);
731
732   /*
733   g_print("\n");
734   g_list_foreach (list, my_list_print, NULL);
735   */
736
737   for (i = 0; i < 10; i++)
738     {
739       t = g_list_nth (list, i);
740       if (*((gint*) t->data) != (9 - i))
741          g_error ("Merge sort failed");
742     }
743
744   g_list_free (list);
745
746   g_print ("ok\n");
747
748
749   g_print ("checking singly linked lists...");
750
751   slist = NULL;
752   for (i = 0; i < 10; i++)
753     slist = g_slist_append (slist, &nums[i]);
754   slist = g_slist_reverse (slist);
755
756   for (i = 0; i < 10; i++)
757     {
758       st = g_slist_nth (slist, i);
759       if (*((gint*) st->data) != (9 - i))
760         g_error ("failed");
761     }
762
763   g_slist_free (slist);
764   slist = NULL;
765
766   for (i = 0; i < 10; i++)
767     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
768
769   /*
770   g_print("\n");
771   g_slist_foreach (slist, my_list_print, NULL);
772   */
773
774   for (i = 0; i < 10; i++)
775     {
776       st = g_slist_nth (slist, i);
777       if (*((gint*) st->data) != i)
778          g_error ("Sorted insert failed");
779     }
780
781   g_slist_free(slist);
782   slist = NULL;
783
784   for (i = 0; i < 10; i++)
785     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
786
787   /*
788   g_print("\n");
789   g_slist_foreach (slist, my_list_print, NULL);
790   */
791
792   for (i = 0; i < 10; i++)
793     {
794       st = g_slist_nth (slist, i);
795       if (*((gint*) st->data) != (9 - i))
796          g_error("Sorted insert failed");
797     }
798
799   g_slist_free(slist);
800   slist = NULL;
801
802   for (i = 0; i < 10; i++)
803     slist = g_slist_prepend (slist, &morenums[i]);
804
805   slist = g_slist_sort (slist, my_list_compare_two);
806
807   /*
808   g_print("\n");
809   g_slist_foreach (slist, my_list_print, NULL);
810   */
811
812   for (i = 0; i < 10; i++)
813     {
814       st = g_slist_nth (slist, i);
815       if (*((gint*) st->data) != (9 - i))
816          g_error("Sorted insert failed");
817     }
818
819   g_slist_free(slist);
820
821   g_print ("ok\n");
822
823
824   g_print ("checking binary trees...\n");
825
826   tree = g_tree_new (my_compare);
827   i = 0;
828   for (j = 0; j < 10; j++, i++)
829     {
830       chars[i] = '0' + j;
831       g_tree_insert (tree, &chars[i], &chars[i]);
832     }
833   for (j = 0; j < 26; j++, i++)
834     {
835       chars[i] = 'A' + j;
836       g_tree_insert (tree, &chars[i], &chars[i]);
837     }
838   for (j = 0; j < 26; j++, i++)
839     {
840       chars[i] = 'a' + j;
841       g_tree_insert (tree, &chars[i], &chars[i]);
842     }
843
844   g_print ("tree height: %d\n", g_tree_height (tree));
845   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
846
847   g_print ("tree: ");
848   g_tree_foreach (tree, my_traverse, NULL);
849   g_print ("\n");
850
851   for (i = 0; i < 10; i++)
852     g_tree_remove (tree, &chars[i]);
853
854   g_print ("tree height: %d\n", g_tree_height (tree));
855   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
856
857   g_print ("tree: ");
858   g_tree_foreach (tree, my_traverse, NULL);
859   g_print ("\n");
860
861   g_print ("ok\n");
862
863
864   /* check n-way trees */
865   g_node_test ();
866
867   g_print ("checking mem chunks...");
868
869   mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
870
871   for (i = 0; i < 10000; i++)
872     {
873       mem[i] = g_chunk_new (gchar, mem_chunk);
874
875       for (j = 0; j < 50; j++)
876         mem[i][j] = i * j;
877     }
878
879   for (i = 0; i < 10000; i++)
880     {
881       g_mem_chunk_free (mem_chunk, mem[i]);
882     }
883
884   g_print ("ok\n");
885
886
887   g_print ("checking hash tables...");
888
889   hash_table = g_hash_table_new (my_hash, my_hash_equal);
890   for (i = 0; i < 10000; i++)
891     {
892       array[i] = i;
893       g_hash_table_insert (hash_table, &array[i], &array[i]);
894     }
895   pvalue = g_hash_table_find (hash_table, find_first_that, &value);
896   if (*pvalue != value)
897           g_print("g_hash_table_find failed");
898   
899   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
900
901   for (i = 0; i < 10000; i++)
902     if (array[i] == 0)
903       g_print ("%d\n", i);
904
905   for (i = 0; i < 10000; i++)
906     g_hash_table_remove (hash_table, &array[i]);
907
908   for (i = 0; i < 10000; i++)
909     {
910       array[i] = i;
911       g_hash_table_insert (hash_table, &array[i], &array[i]);
912     }
913
914   if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
915       g_hash_table_size (hash_table) != 5000)
916     g_print ("bad!\n");
917
918   g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
919
920
921   g_hash_table_destroy (hash_table);
922
923   g_print ("ok\n");
924
925
926   g_print ("checking string chunks...");
927
928   string_chunk = g_string_chunk_new (1024);
929
930   for (i = 0; i < 100000; i ++)
931     {
932       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
933
934       if (strcmp ("hi pete", tmp_string) != 0)
935         g_error ("string chunks are broken.\n");
936     }
937
938   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
939
940   g_assert (tmp_string_2 != tmp_string &&
941             strcmp(tmp_string_2, tmp_string) == 0);
942
943   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
944
945   g_assert (tmp_string_2 == tmp_string);
946
947   g_string_chunk_free (string_chunk);
948
949   g_print ("ok\n");
950
951
952   g_print ("checking arrays...");
953
954   garray = g_array_new (FALSE, FALSE, sizeof (gint));
955   for (i = 0; i < 10000; i++)
956     g_array_append_val (garray, i);
957
958   for (i = 0; i < 10000; i++)
959     if (g_array_index (garray, gint, i) != i)
960       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
961
962   g_array_free (garray, TRUE);
963
964   garray = g_array_new (FALSE, FALSE, sizeof (gint));
965   for (i = 0; i < 100; i++)
966     g_array_prepend_val (garray, i);
967
968   for (i = 0; i < 100; i++)
969     if (g_array_index (garray, gint, i) != (100 - i - 1))
970       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
971
972   g_array_free (garray, TRUE);
973
974   g_print ("ok\n");
975
976
977   g_print ("checking strings...");
978
979   string1 = g_string_new ("hi pete!");
980   string2 = g_string_new ("");
981
982   g_assert (strcmp ("hi pete!", string1->str) == 0);
983
984   for (i = 0; i < 10000; i++)
985     g_string_append_c (string1, 'a'+(i%26));
986
987 #ifndef G_OS_WIN32
988   /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
989      the %10000.10000f format... */
990   g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
991                    "this pete guy sure is a wuss, like he's the number ",
992                    1,
993                    " wuss.  everyone agrees.\n",
994                    string1->str,
995                    10, 666, 15, 15, 666.666666666, 666.666666666);
996 #else
997   g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
998                    "this pete guy sure is a wuss, like he's the number ",
999                    1,
1000                    " wuss.  everyone agrees.\n",
1001                    string1->str,
1002                    10, 666, 15, 15, 666.666666666, 666.666666666);
1003 #endif
1004
1005   g_print ("string2 length = %lu...\n", (gulong)string2->len);
1006   string2->str[70] = '\0';
1007   g_print ("first 70 chars:\n%s\n", string2->str);
1008   string2->str[141] = '\0';
1009   g_print ("next 70 chars:\n%s\n", string2->str+71);
1010   string2->str[212] = '\0';
1011   g_print ("and next 70:\n%s\n", string2->str+142);
1012   g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
1013
1014   g_string_free (string1, TRUE);
1015   g_string_free (string2, TRUE);
1016
1017   /* append */
1018   string1 = g_string_new ("firsthalf");
1019   g_string_append (string1, "lasthalf");
1020   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1021   g_string_free (string1, TRUE);
1022
1023   /* append_len */
1024
1025   string1 = g_string_new ("firsthalf");
1026   g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
1027   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1028   g_string_free (string1, TRUE);  
1029   
1030   /* prepend */
1031   string1 = g_string_new ("lasthalf");
1032   g_string_prepend (string1, "firsthalf");
1033   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1034   g_string_free (string1, TRUE);
1035
1036   /* prepend_len */
1037   string1 = g_string_new ("lasthalf");
1038   g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
1039   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1040   g_string_free (string1, TRUE);
1041   
1042   /* insert */
1043   string1 = g_string_new ("firstlast");
1044   g_string_insert (string1, 5, "middle");
1045   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1046   g_string_free (string1, TRUE);
1047
1048   /* insert with pos == end of the string */
1049   string1 = g_string_new ("firstmiddle");
1050   g_string_insert (string1, strlen ("firstmiddle"), "last");
1051   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1052   g_string_free (string1, TRUE);
1053   
1054   /* insert_len */
1055
1056   string1 = g_string_new ("firstlast");
1057   g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
1058   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1059   g_string_free (string1, TRUE);
1060
1061   /* insert_len with magic -1 pos for append */
1062   string1 = g_string_new ("first");
1063   g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
1064   g_assert (strcmp (string1->str, "firstlast") == 0);
1065   g_string_free (string1, TRUE);
1066   
1067   /* insert_len with magic -1 len for strlen-the-string */
1068   string1 = g_string_new ("first");
1069   g_string_insert_len (string1, 5, "last", -1);
1070   g_assert (strcmp (string1->str, "firstlast") == 0);
1071   g_string_free (string1, TRUE);
1072   
1073   g_print ("ok\n");
1074
1075   /* g_string_equal */
1076   string1 = g_string_new ("test");
1077   string2 = g_string_new ("te");
1078   g_assert (! g_string_equal(string1, string2));
1079   g_string_append (string2, "st");
1080   g_assert (g_string_equal(string1, string2));
1081   g_string_free (string1, TRUE);
1082   g_string_free (string2, TRUE);
1083   
1084   /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
1085   string1 = g_string_new ("fiddle");
1086   string2 = g_string_new ("fiddle");
1087   g_assert (g_string_equal(string1, string2));
1088   g_string_append_c(string1, '\0');
1089   g_assert (! g_string_equal(string1, string2));
1090   g_string_append_c(string2, '\0');
1091   g_assert (g_string_equal(string1, string2));
1092   g_string_append_c(string1, 'x');
1093   g_string_append_c(string2, 'y');
1094   g_assert (! g_string_equal(string1, string2));
1095   g_assert (string1->len == 8);
1096   g_string_append(string1, "yzzy");
1097   g_assert (string1->len == 12);
1098   g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
1099   g_string_insert(string1, 1, "QED");
1100   g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
1101   g_string_free (string1, TRUE);
1102   g_string_free (string2, TRUE);
1103   
1104   g_print ("test positional printf formats (not supported): ");
1105   string = g_strdup_printf ("%.*s%s", 5, "a", "b");
1106   tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
1107   g_print ("%s%s\n", string, tmp_string);
1108   g_free (tmp_string);
1109   g_free (string);
1110
1111   g_print ("checking timers...\n");
1112   
1113   timer = g_timer_new ();
1114   g_print ("  spinning for 3 seconds...\n");
1115
1116   g_timer_start (timer);
1117   while (g_timer_elapsed (timer, NULL) < 3)
1118     ;
1119
1120   g_timer_stop (timer);
1121   g_timer_destroy (timer);
1122
1123   g_print ("ok\n");
1124
1125   g_print ("checking g_timer_continue...\n");
1126
1127   timer2 = g_timer_new ();
1128
1129   g_print ("\trun for 1 second...\n");
1130   timer = g_timer_new();
1131   g_usleep(G_USEC_PER_SEC); /* run timer for 1 second */
1132   g_timer_stop(timer);
1133
1134   g_print ("\tstop for 1 second...\n");
1135   g_usleep(G_USEC_PER_SEC); /* wait for 1 second */
1136   g_print ("\trun for 2 seconds...\n");
1137
1138   g_timer_continue(timer);
1139   g_usleep(2*G_USEC_PER_SEC); /* run timer for 2 seconds */
1140   g_timer_stop(timer);
1141
1142   g_print ("\tstop for 1.5 seconds...\n");
1143   g_usleep((3*G_USEC_PER_SEC)/2); /* wait for 1.5 seconds */
1144   g_print ("\trun for 0.2 seconds...\n");
1145
1146   g_timer_continue(timer);
1147   g_usleep(G_USEC_PER_SEC/5); /* run timer for 0.2 seconds */
1148   g_timer_stop(timer);
1149
1150   g_print ("\tstop for 4 seconds...\n");
1151   g_usleep(4*G_USEC_PER_SEC); /* wait for 4 seconds */
1152   g_print ("\trun for 5.8 seconds...\n");
1153
1154   g_timer_continue(timer);
1155   g_usleep((29*G_USEC_PER_SEC)/5); /* run timer for 5.8 seconds */
1156   g_timer_stop(timer);
1157
1158   elapsed = g_timer_elapsed (timer, &elapsed_usecs);
1159   g_print ("\t=> timer = %.6f = %d.%06ld (should be: 9.000000) (%.6f off)\n", elapsed, (int) elapsed, elapsed_usecs, ABS (elapsed - 9.));
1160
1161   if (elapsed > 8.8 && elapsed < 9.2)
1162     g_print ("g_timer_continue ... ok\n\n");
1163   else
1164     g_print ("g_timer_continue ... ***** FAILED *****\n\n");
1165
1166   g_timer_stop(timer2);
1167
1168   elapsed = g_timer_elapsed(timer2, &elapsed_usecs);
1169   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)));
1170
1171   if (elapsed > (8.8+6.5) && elapsed < (9.2+6.5))
1172     g_print ("timer2 ... ok\n\n");
1173   else
1174     g_print ("timer2 ... ***** FAILED *****\n\n");
1175
1176   g_timer_destroy(timer);
1177   g_timer_destroy(timer2);
1178
1179   g_print ("checking g_ascii_strcasecmp...");
1180   g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
1181   g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
1182   g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
1183   g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1184   g_assert (g_ascii_strcasecmp ("", "") == 0);
1185   g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
1186   g_assert (g_ascii_strcasecmp ("a", "b") < 0);
1187   g_assert (g_ascii_strcasecmp ("a", "B") < 0);
1188   g_assert (g_ascii_strcasecmp ("A", "b") < 0);
1189   g_assert (g_ascii_strcasecmp ("A", "B") < 0);
1190   g_assert (g_ascii_strcasecmp ("b", "a") > 0);
1191   g_assert (g_ascii_strcasecmp ("b", "A") > 0);
1192   g_assert (g_ascii_strcasecmp ("B", "a") > 0);
1193   g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1194
1195   g_print ("ok\n");
1196
1197   g_print ("checking g_strdup...");
1198   g_assert(g_strdup(NULL) == NULL);
1199   string = g_strdup(GLIB_TEST_STRING);
1200   g_assert(string != NULL);
1201   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
1202   g_free(string);
1203
1204   g_print ("ok\n");
1205
1206   g_print ("checking g_strconcat...");
1207   string = g_strconcat(GLIB_TEST_STRING, NULL);
1208   g_assert(string != NULL);
1209   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
1210   g_free(string);
1211   string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING, 
1212                        GLIB_TEST_STRING, NULL);
1213   g_assert(string != NULL);
1214   g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
1215                           GLIB_TEST_STRING) == 0);
1216   g_free(string);
1217   g_print ("ok\n");
1218   
1219
1220   g_print("checking g_strlcpy/g_strlcat...");
1221   /* The following is a torture test for strlcpy/strlcat, with lots of
1222    * checking; normal users wouldn't use them this way!
1223    */
1224   string = g_malloc (6);
1225   *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
1226   *string = 'q';
1227   g_assert (g_strlcpy(string, "" , 5) == 0);
1228   g_assert ( *string == '\0' );
1229   *string = 'q';
1230   g_assert (g_strlcpy(string, "abc" , 5) == 3);
1231   g_assert ( *(string + 3) == '\0' );
1232   g_assert (g_str_equal(string, "abc"));
1233   g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1234   g_assert ( *(string + 4) == '\0' );
1235   g_assert ( *(string + 5) == 'Z' );
1236   g_assert (g_str_equal(string, "abcd"));
1237   g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1238   g_assert ( *(string + 4) == '\0' );
1239   g_assert ( *(string + 5) == 'Z' );
1240   g_assert (g_str_equal(string, "abcd"));
1241   g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1242   g_assert ( *(string + 4) == '\0' );
1243   g_assert ( *(string + 5) == 'Z' );
1244   g_assert (g_str_equal(string, "abcd"));
1245   *string = 'Y';
1246   *(string + 1)= '\0';
1247   g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1248   g_assert (*string == 'Y');
1249   *string = '\0';
1250   g_assert (g_strlcat(string, "123" , 5) == 3);
1251   g_assert ( *(string + 3) == '\0' );
1252   g_assert (g_str_equal(string, "123"));
1253   g_assert (g_strlcat(string, "" , 5) == 3);
1254   g_assert ( *(string + 3) == '\0' );
1255   g_assert (g_str_equal(string, "123"));
1256   g_assert (g_strlcat(string, "4", 5) == 4);
1257   g_assert (g_str_equal(string, "1234"));
1258   g_assert (g_strlcat(string, "5", 5) == 5);
1259   g_assert ( *(string + 4) == '\0' );
1260   g_assert (g_str_equal(string, "1234"));
1261   g_assert ( *(string + 5) == 'Z' );
1262   *string = 'Y';
1263   *(string + 1)= '\0';
1264   g_assert (g_strlcat(string, "123" , 0) == 3);
1265   g_assert (*string == 'Y');
1266   
1267   /* A few more tests, demonstrating more "normal" use  */
1268   g_assert (g_strlcpy(string, "hi", 5) == 2);
1269   g_assert (g_str_equal(string, "hi"));
1270   g_assert (g_strlcat(string, "t", 5) == 3);
1271   g_assert (g_str_equal(string, "hit"));
1272   g_free(string);
1273
1274   g_print ("ok\n");
1275   
1276   
1277   g_print ("checking g_strdup_printf...");
1278   string = g_strdup_printf ("%05d %-5s", 21, "test");
1279   g_assert (string != NULL);
1280   g_assert (strcmp(string, "00021 test ") == 0);
1281   g_free (string);
1282
1283   g_print ("ok\n");
1284
1285   /* g_debug (argv[0]); */
1286
1287   /* Relation tests */
1288
1289   g_print ("checking relations...");
1290
1291   relation = g_relation_new (2);
1292
1293   g_relation_index (relation, 0, g_int_hash, g_int_equal);
1294   g_relation_index (relation, 1, g_int_hash, g_int_equal);
1295
1296   for (i = 0; i < 1024; i += 1)
1297     data[i] = i;
1298
1299   for (i = 1; i < 1023; i += 1)
1300     {
1301       g_relation_insert (relation, data + i, data + i + 1);
1302       g_relation_insert (relation, data + i, data + i - 1);
1303     }
1304
1305   for (i = 2; i < 1022; i += 1)
1306     {
1307       g_assert (! g_relation_exists (relation, data + i, data + i));
1308       g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1309       g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1310     }
1311
1312   for (i = 1; i < 1023; i += 1)
1313     {
1314       g_assert (g_relation_exists (relation, data + i, data + i + 1));
1315       g_assert (g_relation_exists (relation, data + i, data + i - 1));
1316     }
1317
1318   for (i = 2; i < 1022; i += 1)
1319     {
1320       g_assert (g_relation_count (relation, data + i, 0) == 2);
1321       g_assert (g_relation_count (relation, data + i, 1) == 2);
1322     }
1323
1324   g_assert (g_relation_count (relation, data, 0) == 0);
1325
1326   g_assert (g_relation_count (relation, data + 42, 0) == 2);
1327   g_assert (g_relation_count (relation, data + 43, 1) == 2);
1328   g_assert (g_relation_count (relation, data + 41, 1) == 2);
1329   g_relation_delete (relation, data + 42, 0);
1330   g_assert (g_relation_count (relation, data + 42, 0) == 0);
1331   g_assert (g_relation_count (relation, data + 43, 1) == 1);
1332   g_assert (g_relation_count (relation, data + 41, 1) == 1);
1333
1334   tuples = g_relation_select (relation, data + 200, 0);
1335
1336   g_assert (tuples->len == 2);
1337
1338 #if 0
1339   for (i = 0; i < tuples->len; i += 1)
1340     {
1341       printf ("%d %d\n",
1342               *(gint*) g_tuples_index (tuples, i, 0),
1343               *(gint*) g_tuples_index (tuples, i, 1));
1344     }
1345 #endif
1346
1347   g_assert (g_relation_exists (relation, data + 300, data + 301));
1348   g_relation_delete (relation, data + 300, 0);
1349   g_assert (!g_relation_exists (relation, data + 300, data + 301));
1350
1351   g_tuples_destroy (tuples);
1352
1353   g_relation_destroy (relation);
1354
1355   relation = NULL;
1356
1357   g_print ("ok\n");
1358
1359   g_print ("checking pointer arrays...");
1360
1361   gparray = g_ptr_array_new ();
1362   for (i = 0; i < 10000; i++)
1363     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1364
1365   for (i = 0; i < 10000; i++)
1366     if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1367       g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1368
1369   g_ptr_array_free (gparray, TRUE);
1370
1371   g_print ("ok\n");
1372
1373
1374   g_print ("checking byte arrays...");
1375
1376   gbarray = g_byte_array_new ();
1377   for (i = 0; i < 10000; i++)
1378     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1379
1380   for (i = 0; i < 10000; i++)
1381     {
1382       g_assert (gbarray->data[4*i] == 'a');
1383       g_assert (gbarray->data[4*i+1] == 'b');
1384       g_assert (gbarray->data[4*i+2] == 'c');
1385       g_assert (gbarray->data[4*i+3] == 'd');
1386     }
1387
1388   g_byte_array_free (gbarray, TRUE);
1389   g_print ("ok\n");
1390
1391   g_printerr ("g_log tests:");
1392   g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1393   g_message ("the next warning is a test:");
1394   string = NULL;
1395   g_print (string);
1396   g_message ("non-printable UTF-8: \"\xc3\xa4\xda\x85\"");
1397   g_message ("unsafe chars: \"\x10\x11\x12\n\t\x7f\x81\x82\x83\"");
1398
1399   g_print ("checking endian macros (host is ");
1400 #if G_BYTE_ORDER == G_BIG_ENDIAN
1401   g_print ("big endian)...");
1402 #else
1403   g_print ("little endian)...");
1404 #endif
1405   g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);  
1406   g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);  
1407   g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);  
1408
1409   g_print ("ok\n");
1410
1411   if (g_get_charset ((G_CONST_RETURN char**)&charset))
1412     g_print ("current charset is UTF-8: %s\n", charset);
1413   else
1414     g_print ("current charset is not UTF-8: %s\n", charset);
1415
1416 #ifdef G_PLATFORM_WIN32
1417   g_print ("current locale: %s\n", g_win32_getlocale ());
1418   g_print ("GLib DLL name tested for: %s\n", glib_dll);
1419
1420   g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
1421            GETTEXT_PACKAGE,
1422            g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
1423   g_print ("Ditto, or from GLib DLL name: %s\n",
1424            g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
1425   g_print ("Ditto, only from GLib DLL name: %s\n",
1426            g_win32_get_package_installation_directory (NULL, glib_dll));
1427   g_print ("locale subdirectory of GLib installation directory: %s\n",
1428            g_win32_get_package_installation_subdirectory (NULL, glib_dll, "lib\\locale"));
1429   g_print ("GTK+ 2.0 installation directory, if available: %s\n",
1430            g_win32_get_package_installation_directory ("gtk20", NULL));
1431
1432   g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
1433   g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
1434
1435   g_print ("a Win32 error message: %s\n", g_win32_error_message (2));
1436
1437 #endif
1438
1439   g_print ("checking file functions...\n");
1440
1441   strcpy (template, "foobar");
1442   fd = g_mkstemp (template);
1443   if (fd != -1)
1444     g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
1445   close (fd);
1446   strcpy (template, "fooXXXXXX");
1447   fd = g_mkstemp (template);
1448   if (fd == -1)
1449     g_print ("g_mkstemp didn't work for template %s\n", template);
1450   i = write (fd, hello, hellolen);
1451   if (i == -1)
1452     g_print ("write() failed: %s\n", g_strerror (errno));
1453   else if (i != hellolen)
1454     g_print ("write() should have written %d bytes, wrote %d\n", hellolen, i);
1455
1456   lseek (fd, 0, 0);
1457   i = read (fd, chars, sizeof (chars));
1458   if (i == -1)
1459     g_print ("read() failed: %s\n", g_strerror (errno));
1460   else if (i != hellolen)
1461     g_print ("read() should have read %d bytes, got %d\n", hellolen, i);
1462
1463   chars[i] = 0;
1464   if (strcmp (chars, hello) != 0)
1465     g_print ("wrote '%s', but got '%s'\n", hello, chars);
1466
1467   close (fd);
1468   remove (template);
1469
1470   error = NULL;
1471   strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1472   fd = g_file_open_tmp (template, &name_used, &error);
1473   if (fd != -1)
1474     g_print ("g_file_open_tmp works even if template contains '%s'\n",
1475              G_DIR_SEPARATOR_S);
1476   else
1477     g_print ("g_file_open_tmp correctly returns error: %s\n",
1478              error->message);
1479   close (fd);
1480   g_clear_error (&error);
1481
1482 #ifdef G_OS_WIN32
1483   strcpy (template, "zap/barXXXXXX");
1484   fd = g_file_open_tmp (template, &name_used, &error);
1485   if (fd != -1)
1486     g_print ("g_file_open_tmp works even if template contains '/'\n");
1487   else
1488     g_print ("g_file_open_tmp correctly returns error: %s\n",
1489              error->message);
1490   close (fd);
1491   g_clear_error (&error);
1492 #endif
1493
1494   strcpy (template, "zapXXXXXX");
1495   fd = g_file_open_tmp (template, &name_used, &error);
1496   if (fd == -1)
1497     g_print ("g_file_open_tmp didn't work for template '%s': %s\n",
1498              template, error->message);
1499   else
1500     g_print ("g_file_open_tmp for template '%s' used name '%s'\n",
1501              template, name_used);
1502   close (fd);
1503   g_clear_error (&error);
1504   remove (name_used);
1505
1506   fd = g_file_open_tmp (NULL, &name_used, &error);
1507   if (fd == -1)
1508     g_print ("g_file_open_tmp didn't work for a NULL template: %s\n",
1509              error->message);
1510   else
1511     g_print ("g_file_open_tmp for NULL template used name '%s'\n",
1512              name_used);
1513   close (fd);
1514   g_clear_error (&error);
1515   remove (name_used);
1516
1517   test_g_parse_debug_string ();
1518
1519   return 0;
1520 }
1521