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