Mark the functions g_basename and g_dirname deprecated. They will issue an
[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 Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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-1999.  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 #ifdef  G_HAVE_GINT64
376   g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
377   TEST (NULL, sizeof (gint64) == 8);
378 #endif  /* G_HAVE_GINT64 */
379   g_print ("\n");
380
381   g_print ("checking g_path_get_dirname()...");
382   for (i = 0; i < n_dirname_checks; i++)
383     {
384       gchar *dirname;
385
386       dirname = g_path_get_dirname (dirname_checks[i].filename);
387       if (strcmp (dirname, dirname_checks[i].dirname) != 0)
388         {
389           g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
390                    dirname_checks[i].filename,
391                    dirname_checks[i].dirname,
392                    dirname);
393           n_dirname_checks = 0;
394         }
395       g_free (dirname);
396     }
397   if (n_dirname_checks)
398     g_print ("ok\n");
399
400   g_print ("checking doubly linked lists...");
401
402   list = NULL;
403   for (i = 0; i < 10; i++)
404     list = g_list_append (list, &nums[i]);
405   list = g_list_reverse (list);
406
407   for (i = 0; i < 10; i++)
408     {
409       t = g_list_nth (list, i);
410       if (*((gint*) t->data) != (9 - i))
411         g_error ("Regular insert failed");
412     }
413
414   for (i = 0; i < 10; i++)
415     if(g_list_position(list, g_list_nth (list, i)) != i)
416       g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
417
418   g_list_free (list);
419   list = NULL;
420
421   for (i = 0; i < 10; i++)
422     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
423
424   /*
425   g_print("\n");
426   g_list_foreach (list, my_list_print, NULL);
427   */
428
429   for (i = 0; i < 10; i++)
430     {
431       t = g_list_nth (list, i);
432       if (*((gint*) t->data) != i)
433          g_error ("Sorted insert failed");
434     }
435
436   g_list_free (list);
437   list = NULL;
438
439   for (i = 0; i < 10; i++)
440     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
441
442   /*
443   g_print("\n");
444   g_list_foreach (list, my_list_print, NULL);
445   */
446
447   for (i = 0; i < 10; i++)
448     {
449       t = g_list_nth (list, i);
450       if (*((gint*) t->data) != (9 - i))
451          g_error ("Sorted insert failed");
452     }
453
454   g_list_free (list);
455   list = NULL;
456
457   for (i = 0; i < 10; i++)
458     list = g_list_prepend (list, &morenums[i]);
459
460   list = g_list_sort (list, my_list_compare_two);
461
462   /*
463   g_print("\n");
464   g_list_foreach (list, my_list_print, NULL);
465   */
466
467   for (i = 0; i < 10; i++)
468     {
469       t = g_list_nth (list, i);
470       if (*((gint*) t->data) != (9 - i))
471          g_error ("Merge sort failed");
472     }
473
474   g_list_free (list);
475
476   g_print ("ok\n");
477
478
479   g_print ("checking singly linked lists...");
480
481   slist = NULL;
482   for (i = 0; i < 10; i++)
483     slist = g_slist_append (slist, &nums[i]);
484   slist = g_slist_reverse (slist);
485
486   for (i = 0; i < 10; i++)
487     {
488       st = g_slist_nth (slist, i);
489       if (*((gint*) st->data) != (9 - i))
490         g_error ("failed");
491     }
492
493   g_slist_free (slist);
494   slist = NULL;
495
496   for (i = 0; i < 10; i++)
497     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
498
499   /*
500   g_print("\n");
501   g_slist_foreach (slist, my_list_print, NULL);
502   */
503
504   for (i = 0; i < 10; i++)
505     {
506       st = g_slist_nth (slist, i);
507       if (*((gint*) st->data) != i)
508          g_error ("Sorted insert failed");
509     }
510
511   g_slist_free(slist);
512   slist = NULL;
513
514   for (i = 0; i < 10; i++)
515     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
516
517   /*
518   g_print("\n");
519   g_slist_foreach (slist, my_list_print, NULL);
520   */
521
522   for (i = 0; i < 10; i++)
523     {
524       st = g_slist_nth (slist, i);
525       if (*((gint*) st->data) != (9 - i))
526          g_error("Sorted insert failed");
527     }
528
529   g_slist_free(slist);
530   slist = NULL;
531
532   for (i = 0; i < 10; i++)
533     slist = g_slist_prepend (slist, &morenums[i]);
534
535   slist = g_slist_sort (slist, my_list_compare_two);
536
537   /*
538   g_print("\n");
539   g_slist_foreach (slist, my_list_print, NULL);
540   */
541
542   for (i = 0; i < 10; i++)
543     {
544       st = g_slist_nth (slist, i);
545       if (*((gint*) st->data) != (9 - i))
546          g_error("Sorted insert failed");
547     }
548
549   g_slist_free(slist);
550
551   g_print ("ok\n");
552
553
554   g_print ("checking binary trees...\n");
555
556   tree = g_tree_new (my_compare);
557   i = 0;
558   for (j = 0; j < 10; j++, i++)
559     {
560       chars[i] = '0' + j;
561       g_tree_insert (tree, &chars[i], &chars[i]);
562     }
563   for (j = 0; j < 26; j++, i++)
564     {
565       chars[i] = 'A' + j;
566       g_tree_insert (tree, &chars[i], &chars[i]);
567     }
568   for (j = 0; j < 26; j++, i++)
569     {
570       chars[i] = 'a' + j;
571       g_tree_insert (tree, &chars[i], &chars[i]);
572     }
573
574   g_print ("tree height: %d\n", g_tree_height (tree));
575   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
576
577   g_print ("tree: ");
578   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
579   g_print ("\n");
580
581   for (i = 0; i < 10; i++)
582     g_tree_remove (tree, &chars[i]);
583
584   g_print ("tree height: %d\n", g_tree_height (tree));
585   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
586
587   g_print ("tree: ");
588   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
589   g_print ("\n");
590
591   g_print ("ok\n");
592
593
594   /* check n-way trees */
595   g_node_test ();
596
597   g_print ("checking mem chunks...");
598
599   mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
600
601   for (i = 0; i < 10000; i++)
602     {
603       mem[i] = g_chunk_new (gchar, mem_chunk);
604
605       for (j = 0; j < 50; j++)
606         mem[i][j] = i * j;
607     }
608
609   for (i = 0; i < 10000; i++)
610     {
611       g_mem_chunk_free (mem_chunk, mem[i]);
612     }
613
614   g_print ("ok\n");
615
616
617   g_print ("checking hash tables...");
618
619   hash_table = g_hash_table_new (my_hash, my_hash_compare);
620   for (i = 0; i < 10000; i++)
621     {
622       array[i] = i;
623       g_hash_table_insert (hash_table, &array[i], &array[i]);
624     }
625   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
626
627   for (i = 0; i < 10000; i++)
628     if (array[i] == 0)
629       g_print ("%d\n", i);
630
631   for (i = 0; i < 10000; i++)
632     g_hash_table_remove (hash_table, &array[i]);
633
634   for (i = 0; i < 10000; i++)
635     {
636       array[i] = i;
637       g_hash_table_insert (hash_table, &array[i], &array[i]);
638     }
639
640   if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
641       g_hash_table_size (hash_table) != 5000)
642     g_print ("bad!\n");
643
644   g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
645
646
647   g_hash_table_destroy (hash_table);
648
649   g_print ("ok\n");
650
651
652   g_print ("checking string chunks...");
653
654   string_chunk = g_string_chunk_new (1024);
655
656   for (i = 0; i < 100000; i ++)
657     {
658       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
659
660       if (strcmp ("hi pete", tmp_string) != 0)
661         g_error ("string chunks are broken.\n");
662     }
663
664   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
665
666   g_assert (tmp_string_2 != tmp_string &&
667             strcmp(tmp_string_2, tmp_string) == 0);
668
669   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
670
671   g_assert (tmp_string_2 == tmp_string);
672
673   g_string_chunk_free (string_chunk);
674
675   g_print ("ok\n");
676
677
678   g_print ("checking arrays...");
679
680   garray = g_array_new (FALSE, FALSE, sizeof (gint));
681   for (i = 0; i < 10000; i++)
682     g_array_append_val (garray, i);
683
684   for (i = 0; i < 10000; i++)
685     if (g_array_index (garray, gint, i) != i)
686       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
687
688   g_array_free (garray, TRUE);
689
690   garray = g_array_new (FALSE, FALSE, sizeof (gint));
691   for (i = 0; i < 100; i++)
692     g_array_prepend_val (garray, i);
693
694   for (i = 0; i < 100; i++)
695     if (g_array_index (garray, gint, i) != (100 - i - 1))
696       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
697
698   g_array_free (garray, TRUE);
699
700   g_print ("ok\n");
701
702
703   g_print ("checking strings...");
704
705   string1 = g_string_new ("hi pete!");
706   string2 = g_string_new ("");
707
708   g_assert (strcmp ("hi pete!", string1->str) == 0);
709
710   for (i = 0; i < 10000; i++)
711     g_string_append_c (string1, 'a'+(i%26));
712
713 #ifndef G_OS_WIN32
714   /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
715      the %10000.10000f format... */
716   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
717                     "this pete guy sure is a wuss, like he's the number ",
718                     1,
719                     " wuss.  everyone agrees.\n",
720                     string1->str,
721                     10, 666, 15, 15, 666.666666666, 666.666666666);
722 #else
723   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
724                     "this pete guy sure is a wuss, like he's the number ",
725                     1,
726                     " wuss.  everyone agrees.\n",
727                     string1->str,
728                     10, 666, 15, 15, 666.666666666, 666.666666666);
729 #endif
730
731   g_print ("string2 length = %d...\n", string2->len);
732   string2->str[70] = '\0';
733   g_print ("first 70 chars:\n%s\n", string2->str);
734   string2->str[141] = '\0';
735   g_print ("next 70 chars:\n%s\n", string2->str+71);
736   string2->str[212] = '\0';
737   g_print ("and next 70:\n%s\n", string2->str+142);
738   g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
739
740   g_string_free (string1, TRUE);
741   g_string_free (string2, TRUE);
742
743   /* append */
744   string1 = g_string_new ("firsthalf");
745   g_string_append (string1, "lasthalf");
746   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
747   g_string_free (string1, TRUE);
748
749   /* append_len */
750
751   string1 = g_string_new ("firsthalf");
752   g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
753   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
754   g_string_free (string1, TRUE);  
755   
756   /* prepend */
757   string1 = g_string_new ("lasthalf");
758   g_string_prepend (string1, "firsthalf");
759   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
760   g_string_free (string1, TRUE);
761
762   /* prepend_len */
763   string1 = g_string_new ("lasthalf");
764   g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
765   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
766   g_string_free (string1, TRUE);
767   
768   /* insert */
769   string1 = g_string_new ("firstlast");
770   g_string_insert (string1, 5, "middle");
771   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
772   g_string_free (string1, TRUE);
773
774   /* insert with pos == end of the string */
775   string1 = g_string_new ("firstmiddle");
776   g_string_insert (string1, strlen ("firstmiddle"), "last");
777   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
778   g_string_free (string1, TRUE);
779   
780   /* insert_len */
781
782   string1 = g_string_new ("firstlast");
783   g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
784   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
785   g_string_free (string1, TRUE);
786
787   /* insert_len with magic -1 pos for append */
788   string1 = g_string_new ("first");
789   g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
790   g_assert (strcmp (string1->str, "firstlast") == 0);
791   g_string_free (string1, TRUE);
792   
793   /* insert_len with magic -1 len for strlen-the-string */
794   string1 = g_string_new ("first");
795   g_string_insert_len (string1, 5, "last", -1);
796   g_assert (strcmp (string1->str, "firstlast") == 0);
797   g_string_free (string1, TRUE);
798   
799   g_print ("ok\n");
800
801   /* g_string_equal */
802   string1 = g_string_new ("test");
803   string2 = g_string_new ("te");
804   g_assert (! g_string_equal(string1, string2));
805   g_string_append (string2, "st");
806   g_assert (g_string_equal(string1, string2));
807   g_string_free (string1, TRUE);
808   g_string_free (string2, TRUE);
809   
810   /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
811   string1 = g_string_new ("fiddle");
812   string2 = g_string_new ("fiddle");
813   g_assert (g_string_equal(string1, string2));
814   g_string_append_c(string1, '\0');
815   g_assert (! g_string_equal(string1, string2));
816   g_string_append_c(string2, '\0');
817   g_assert (g_string_equal(string1, string2));
818   g_string_append_c(string1, 'x');
819   g_string_append_c(string2, 'y');
820   g_assert (! g_string_equal(string1, string2));
821   g_assert (string1->len == 8);
822   g_string_append(string1, "yzzy");
823   g_assert (string1->len == 12);
824   g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
825   g_string_insert(string1, 1, "QED");
826   g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
827   g_string_free (string1, TRUE);
828   g_string_free (string2, TRUE);
829   
830   g_print ("checking timers...\n");
831   
832   timer = g_timer_new ();
833   g_print ("  spinning for 3 seconds...\n");
834
835   g_timer_start (timer);
836   while (g_timer_elapsed (timer, NULL) < 3)
837     ;
838
839   g_timer_stop (timer);
840   g_timer_destroy (timer);
841
842   g_print ("ok\n");
843
844   g_print ("checking g_strcasecmp...");
845   g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0);
846   g_assert (g_strcasecmp ("frobozz", "frobozz") == 0);
847   g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0);
848   g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0);
849   g_assert (g_strcasecmp ("", "") == 0);
850   g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0);
851   g_assert (g_strcasecmp ("a", "b") < 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 ("b", "a") > 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
860   g_print ("ok\n");
861
862   g_print ("checking g_strdup...");
863   g_assert(g_strdup(NULL) == NULL);
864   string = g_strdup(GLIB_TEST_STRING);
865   g_assert(string != NULL);
866   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
867   g_free(string);
868
869   g_print ("ok\n");
870
871   g_print ("checking g_strconcat...");
872   string = g_strconcat(GLIB_TEST_STRING, NULL);
873   g_assert(string != NULL);
874   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
875   g_free(string);
876   string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING, 
877                        GLIB_TEST_STRING, NULL);
878   g_assert(string != NULL);
879   g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
880                           GLIB_TEST_STRING) == 0);
881   g_free(string);
882   
883   g_print ("ok\n");
884
885   g_print ("checking g_strdup_printf...");
886   string = g_strdup_printf ("%05d %-5s", 21, "test");
887   g_assert (string != NULL);
888   g_assert (strcmp(string, "00021 test ") == 0);
889   g_free (string);
890
891   g_print ("ok\n");
892
893   /* g_debug (argv[0]); */
894
895   /* Relation tests */
896
897   g_print ("checking relations...");
898
899   relation = g_relation_new (2);
900
901   g_relation_index (relation, 0, g_int_hash, g_int_equal);
902   g_relation_index (relation, 1, g_int_hash, g_int_equal);
903
904   for (i = 0; i < 1024; i += 1)
905     data[i] = i;
906
907   for (i = 1; i < 1023; i += 1)
908     {
909       g_relation_insert (relation, data + i, data + i + 1);
910       g_relation_insert (relation, data + i, data + i - 1);
911     }
912
913   for (i = 2; i < 1022; i += 1)
914     {
915       g_assert (! g_relation_exists (relation, data + i, data + i));
916       g_assert (! g_relation_exists (relation, data + i, data + i + 2));
917       g_assert (! g_relation_exists (relation, data + i, data + i - 2));
918     }
919
920   for (i = 1; i < 1023; i += 1)
921     {
922       g_assert (g_relation_exists (relation, data + i, data + i + 1));
923       g_assert (g_relation_exists (relation, data + i, data + i - 1));
924     }
925
926   for (i = 2; i < 1022; i += 1)
927     {
928       g_assert (g_relation_count (relation, data + i, 0) == 2);
929       g_assert (g_relation_count (relation, data + i, 1) == 2);
930     }
931
932   g_assert (g_relation_count (relation, data, 0) == 0);
933
934   g_assert (g_relation_count (relation, data + 42, 0) == 2);
935   g_assert (g_relation_count (relation, data + 43, 1) == 2);
936   g_assert (g_relation_count (relation, data + 41, 1) == 2);
937   g_relation_delete (relation, data + 42, 0);
938   g_assert (g_relation_count (relation, data + 42, 0) == 0);
939   g_assert (g_relation_count (relation, data + 43, 1) == 1);
940   g_assert (g_relation_count (relation, data + 41, 1) == 1);
941
942   tuples = g_relation_select (relation, data + 200, 0);
943
944   g_assert (tuples->len == 2);
945
946 #if 0
947   for (i = 0; i < tuples->len; i += 1)
948     {
949       printf ("%d %d\n",
950               *(gint*) g_tuples_index (tuples, i, 0),
951               *(gint*) g_tuples_index (tuples, i, 1));
952     }
953 #endif
954
955   g_assert (g_relation_exists (relation, data + 300, data + 301));
956   g_relation_delete (relation, data + 300, 0);
957   g_assert (!g_relation_exists (relation, data + 300, data + 301));
958
959   g_tuples_destroy (tuples);
960
961   g_relation_destroy (relation);
962
963   relation = NULL;
964
965   g_print ("ok\n");
966
967   g_print ("checking pointer arrays...");
968
969   gparray = g_ptr_array_new ();
970   for (i = 0; i < 10000; i++)
971     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
972
973   for (i = 0; i < 10000; i++)
974     if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
975       g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
976
977   g_ptr_array_free (gparray, TRUE);
978
979   g_print ("ok\n");
980
981
982   g_print ("checking byte arrays...");
983
984   gbarray = g_byte_array_new ();
985   for (i = 0; i < 10000; i++)
986     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
987
988   for (i = 0; i < 10000; i++)
989     {
990       g_assert (gbarray->data[4*i] == 'a');
991       g_assert (gbarray->data[4*i+1] == 'b');
992       g_assert (gbarray->data[4*i+2] == 'c');
993       g_assert (gbarray->data[4*i+3] == 'd');
994     }
995
996   g_byte_array_free (gbarray, TRUE);
997   g_print ("ok\n");
998
999   g_printerr ("g_log tests:");
1000   g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1001   g_message ("the next warning is a test:");
1002   string = NULL;
1003   g_print (string);
1004
1005   g_print ("checking endian macros (host is ");
1006 #if G_BYTE_ORDER == G_BIG_ENDIAN
1007   g_print ("big endian)...");
1008 #else
1009   g_print ("little endian)...");
1010 #endif
1011   g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);  
1012   g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);  
1013 #ifdef G_HAVE_GINT64
1014   g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);  
1015 #endif
1016
1017   g_print ("ok\n");
1018
1019 #ifdef G_OS_WIN32
1020   g_print ("current locale: %s\n", g_win32_getlocale ());
1021 #endif
1022
1023   return 0;
1024 }
1025