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