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