changed g_str_hash() to a 31 bit version based on a submission by Karl
[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;
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   g_node_destroy (root);
170
171   /* allocation tests */
172
173   root = g_node_new (NULL);
174   node = root;
175
176   for (i = 0; i < 2048; i++)
177     {
178       g_node_append (node, g_node_new (NULL));
179       if ((i%5) == 4)
180         node = node->children->next;
181     }
182   TEST (NULL, g_node_max_height (root) > 100);
183   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
184
185   g_node_destroy (root);
186   
187   if (!failed)
188     g_print ("ok\n");
189 }
190
191 static gboolean
192 my_hash_callback_remove (gpointer key,
193                          gpointer value,
194                          gpointer user_data)
195 {
196   int *d = value;
197
198   if ((*d) % 2)
199     return TRUE;
200
201   return FALSE;
202 }
203
204 static void
205 my_hash_callback_remove_test (gpointer key,
206                               gpointer value,
207                               gpointer user_data)
208 {
209   int *d = value;
210
211   if ((*d) % 2)
212     g_print ("bad!\n");
213 }
214
215 static void
216 my_hash_callback (gpointer key,
217                   gpointer value,
218                   gpointer user_data)
219 {
220   int *d = value;
221   *d = 1;
222 }
223
224 static guint
225 my_hash (gconstpointer key)
226 {
227   return (guint) *((const gint*) key);
228 }
229
230 static gint
231 my_hash_compare (gconstpointer a,
232                  gconstpointer b)
233 {
234   return *((const gint*) a) == *((const gint*) b);
235 }
236
237 static gint
238 my_list_compare_one (gconstpointer a, gconstpointer b)
239 {
240   gint one = *((const gint*)a);
241   gint two = *((const gint*)b);
242   return one-two;
243 }
244
245 static gint
246 my_list_compare_two (gconstpointer a, gconstpointer b)
247 {
248   gint one = *((const gint*)a);
249   gint two = *((const gint*)b);
250   return two-one;
251 }
252
253 /* static void
254 my_list_print (gpointer a, gpointer b)
255 {
256   gint three = *((gint*)a);
257   g_print("%d", three);
258 }; */
259
260 static gint
261 my_compare (gconstpointer a,
262             gconstpointer b)
263 {
264   const char *cha = a;
265   const char *chb = b;
266
267   return *cha - *chb;
268 }
269
270 static gint
271 my_traverse (gpointer key,
272              gpointer value,
273              gpointer data)
274 {
275   char *ch = key;
276   g_print ("%c ", *ch);
277   return FALSE;
278 }
279
280 int
281 main (int   argc,
282       char *argv[])
283 {
284   GList *list, *t;
285   GSList *slist, *st;
286   GHashTable *hash_table;
287   GMemChunk *mem_chunk;
288   GStringChunk *string_chunk;
289   GTimer *timer;
290   gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
291   gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
292   gchar *string;
293
294   gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
295   gint i, j;
296   GArray *garray;
297   GPtrArray *gparray;
298   GByteArray *gbarray;
299   GString *string1, *string2;
300   GTree *tree;
301   char chars[62];
302   GRelation *relation;
303   GTuples *tuples;
304   gint data [1024];
305   struct {
306     gchar *filename;
307     gchar *dirname;
308   } dirname_checks[] = {
309 #ifndef G_OS_WIN32
310     { "/", "/" },
311     { "////", "/" },
312     { ".////", "." },
313     { ".", "." },
314     { "..", "." },
315     { "../", ".." },
316     { "..////", ".." },
317     { "", "." },
318     { "a/b", "a" },
319     { "a/b/", "a/b" },
320     { "c///", "c" },
321 #else
322     { "\\", "\\" },
323     { ".\\\\\\\\", "." },
324     { ".", "." },
325     { "..", "." },
326     { "..\\", ".." },
327     { "..\\\\\\\\", ".." },
328     { "", "." },
329     { "a\\b", "a" },
330     { "a\\b\\", "a\\b" },
331     { "c\\\\\\", "c" },
332 #endif
333   };
334   guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
335   guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
336   guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
337 #ifdef G_HAVE_GINT64
338   guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
339           gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
340 #endif
341
342   g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
343            glib_major_version,
344            glib_minor_version,
345            glib_micro_version,
346            glib_interface_age,
347            glib_binary_age);
348
349   string = g_get_current_dir ();
350   g_print ("cwd: %s\n", string);
351   g_free (string);
352   g_print ("user: %s\n", g_get_user_name ());
353   g_print ("real: %s\n", g_get_real_name ());
354   g_print ("home: %s\n", g_get_home_dir ());
355   g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
356
357   /* type sizes */
358   g_print ("checking size of gint8: %d", (int)sizeof (gint8));
359   TEST (NULL, sizeof (gint8) == 1);
360   g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
361   TEST (NULL, sizeof (gint16) == 2);
362   g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
363   TEST (NULL, sizeof (gint32) == 4);
364 #ifdef  G_HAVE_GINT64
365   g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
366   TEST (NULL, sizeof (gint64) == 8);
367 #endif  /* G_HAVE_GINT64 */
368   g_print ("\n");
369
370   g_print ("checking g_dirname()...");
371   for (i = 0; i < n_dirname_checks; i++)
372     {
373       gchar *dirname;
374
375       dirname = g_dirname (dirname_checks[i].filename);
376       if (strcmp (dirname, dirname_checks[i].dirname) != 0)
377         {
378           g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
379                    dirname_checks[i].filename,
380                    dirname_checks[i].dirname,
381                    dirname);
382           n_dirname_checks = 0;
383         }
384       g_free (dirname);
385     }
386   if (n_dirname_checks)
387     g_print ("ok\n");
388
389   g_print ("checking doubly linked lists...");
390
391   list = NULL;
392   for (i = 0; i < 10; i++)
393     list = g_list_append (list, &nums[i]);
394   list = g_list_reverse (list);
395
396   for (i = 0; i < 10; i++)
397     {
398       t = g_list_nth (list, i);
399       if (*((gint*) t->data) != (9 - i))
400         g_error ("Regular insert failed");
401     }
402
403   for (i = 0; i < 10; i++)
404     if(g_list_position(list, g_list_nth (list, i)) != i)
405       g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
406
407   g_list_free (list);
408   list = NULL;
409
410   for (i = 0; i < 10; i++)
411     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
412
413   /*
414   g_print("\n");
415   g_list_foreach (list, my_list_print, NULL);
416   */
417
418   for (i = 0; i < 10; i++)
419     {
420       t = g_list_nth (list, i);
421       if (*((gint*) t->data) != i)
422          g_error ("Sorted insert failed");
423     }
424
425   g_list_free (list);
426   list = NULL;
427
428   for (i = 0; i < 10; i++)
429     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
430
431   /*
432   g_print("\n");
433   g_list_foreach (list, my_list_print, NULL);
434   */
435
436   for (i = 0; i < 10; i++)
437     {
438       t = g_list_nth (list, i);
439       if (*((gint*) t->data) != (9 - i))
440          g_error ("Sorted insert failed");
441     }
442
443   g_list_free (list);
444   list = NULL;
445
446   for (i = 0; i < 10; i++)
447     list = g_list_prepend (list, &morenums[i]);
448
449   list = g_list_sort (list, my_list_compare_two);
450
451   /*
452   g_print("\n");
453   g_list_foreach (list, my_list_print, NULL);
454   */
455
456   for (i = 0; i < 10; i++)
457     {
458       t = g_list_nth (list, i);
459       if (*((gint*) t->data) != (9 - i))
460          g_error ("Merge sort failed");
461     }
462
463   g_list_free (list);
464
465   g_print ("ok\n");
466
467
468   g_print ("checking singly linked lists...");
469
470   slist = NULL;
471   for (i = 0; i < 10; i++)
472     slist = g_slist_append (slist, &nums[i]);
473   slist = g_slist_reverse (slist);
474
475   for (i = 0; i < 10; i++)
476     {
477       st = g_slist_nth (slist, i);
478       if (*((gint*) st->data) != (9 - i))
479         g_error ("failed");
480     }
481
482   g_slist_free (slist);
483   slist = NULL;
484
485   for (i = 0; i < 10; i++)
486     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
487
488   /*
489   g_print("\n");
490   g_slist_foreach (slist, my_list_print, NULL);
491   */
492
493   for (i = 0; i < 10; i++)
494     {
495       st = g_slist_nth (slist, i);
496       if (*((gint*) st->data) != i)
497          g_error ("Sorted insert failed");
498     }
499
500   g_slist_free(slist);
501   slist = NULL;
502
503   for (i = 0; i < 10; i++)
504     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
505
506   /*
507   g_print("\n");
508   g_slist_foreach (slist, my_list_print, NULL);
509   */
510
511   for (i = 0; i < 10; i++)
512     {
513       st = g_slist_nth (slist, i);
514       if (*((gint*) st->data) != (9 - i))
515          g_error("Sorted insert failed");
516     }
517
518   g_slist_free(slist);
519   slist = NULL;
520
521   for (i = 0; i < 10; i++)
522     slist = g_slist_prepend (slist, &morenums[i]);
523
524   slist = g_slist_sort (slist, my_list_compare_two);
525
526   /*
527   g_print("\n");
528   g_slist_foreach (slist, my_list_print, NULL);
529   */
530
531   for (i = 0; i < 10; i++)
532     {
533       st = g_slist_nth (slist, i);
534       if (*((gint*) st->data) != (9 - i))
535          g_error("Sorted insert failed");
536     }
537
538   g_slist_free(slist);
539
540   g_print ("ok\n");
541
542
543   g_print ("checking binary trees...\n");
544
545   tree = g_tree_new (my_compare);
546   i = 0;
547   for (j = 0; j < 10; j++, i++)
548     {
549       chars[i] = '0' + j;
550       g_tree_insert (tree, &chars[i], &chars[i]);
551     }
552   for (j = 0; j < 26; j++, i++)
553     {
554       chars[i] = 'A' + j;
555       g_tree_insert (tree, &chars[i], &chars[i]);
556     }
557   for (j = 0; j < 26; j++, i++)
558     {
559       chars[i] = 'a' + j;
560       g_tree_insert (tree, &chars[i], &chars[i]);
561     }
562
563   g_print ("tree height: %d\n", g_tree_height (tree));
564   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
565
566   g_print ("tree: ");
567   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
568   g_print ("\n");
569
570   for (i = 0; i < 10; i++)
571     g_tree_remove (tree, &chars[i]);
572
573   g_print ("tree height: %d\n", g_tree_height (tree));
574   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
575
576   g_print ("tree: ");
577   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
578   g_print ("\n");
579
580   g_print ("ok\n");
581
582
583   /* check n-way trees */
584   g_node_test ();
585
586   g_print ("checking mem chunks...");
587
588   mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
589
590   for (i = 0; i < 10000; i++)
591     {
592       mem[i] = g_chunk_new (gchar, mem_chunk);
593
594       for (j = 0; j < 50; j++)
595         mem[i][j] = i * j;
596     }
597
598   for (i = 0; i < 10000; i++)
599     {
600       g_mem_chunk_free (mem_chunk, mem[i]);
601     }
602
603   g_print ("ok\n");
604
605
606   g_print ("checking hash tables...");
607
608   hash_table = g_hash_table_new (my_hash, my_hash_compare);
609   for (i = 0; i < 10000; i++)
610     {
611       array[i] = i;
612       g_hash_table_insert (hash_table, &array[i], &array[i]);
613     }
614   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
615
616   for (i = 0; i < 10000; i++)
617     if (array[i] == 0)
618       g_print ("%d\n", i);
619
620   for (i = 0; i < 10000; i++)
621     g_hash_table_remove (hash_table, &array[i]);
622
623   for (i = 0; i < 10000; i++)
624     {
625       array[i] = i;
626       g_hash_table_insert (hash_table, &array[i], &array[i]);
627     }
628
629   if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
630       g_hash_table_size (hash_table) != 5000)
631     g_print ("bad!\n");
632
633   g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
634
635
636   g_hash_table_destroy (hash_table);
637
638   g_print ("ok\n");
639
640
641   g_print ("checking string chunks...");
642
643   string_chunk = g_string_chunk_new (1024);
644
645   for (i = 0; i < 100000; i ++)
646     {
647       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
648
649       if (strcmp ("hi pete", tmp_string) != 0)
650         g_error ("string chunks are broken.\n");
651     }
652
653   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
654
655   g_assert (tmp_string_2 != tmp_string &&
656             strcmp(tmp_string_2, tmp_string) == 0);
657
658   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
659
660   g_assert (tmp_string_2 == tmp_string);
661
662   g_string_chunk_free (string_chunk);
663
664   g_print ("ok\n");
665
666
667   g_print ("checking arrays...");
668
669   garray = g_array_new (FALSE, FALSE, sizeof (gint));
670   for (i = 0; i < 10000; i++)
671     g_array_append_val (garray, i);
672
673   for (i = 0; i < 10000; i++)
674     if (g_array_index (garray, gint, i) != i)
675       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
676
677   g_array_free (garray, TRUE);
678
679   garray = g_array_new (FALSE, FALSE, sizeof (gint));
680   for (i = 0; i < 100; i++)
681     g_array_prepend_val (garray, i);
682
683   for (i = 0; i < 100; i++)
684     if (g_array_index (garray, gint, i) != (100 - i - 1))
685       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
686
687   g_array_free (garray, TRUE);
688
689   g_print ("ok\n");
690
691
692   g_print ("checking strings...");
693
694   string1 = g_string_new ("hi pete!");
695   string2 = g_string_new ("");
696
697   g_assert (strcmp ("hi pete!", string1->str) == 0);
698
699   for (i = 0; i < 10000; i++)
700     g_string_append_c (string1, 'a'+(i%26));
701
702 #ifndef G_OS_WIN32
703   /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
704      the %10000.10000f format... */
705   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
706                     "this pete guy sure is a wuss, like he's the number ",
707                     1,
708                     " wuss.  everyone agrees.\n",
709                     string1->str,
710                     10, 666, 15, 15, 666.666666666, 666.666666666);
711 #else
712   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
713                     "this pete guy sure is a wuss, like he's the number ",
714                     1,
715                     " wuss.  everyone agrees.\n",
716                     string1->str,
717                     10, 666, 15, 15, 666.666666666, 666.666666666);
718 #endif
719
720   g_print ("string2 length = %d...\n", string2->len);
721   string2->str[70] = '\0';
722   g_print ("first 70 chars:\n%s\n", string2->str);
723   string2->str[141] = '\0';
724   g_print ("next 70 chars:\n%s\n", string2->str+71);
725   string2->str[212] = '\0';
726   g_print ("and next 70:\n%s\n", string2->str+142);
727   g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
728
729   g_string_free (string1, TRUE);
730   g_string_free (string2, TRUE);
731
732   /* append */
733   string1 = g_string_new ("firsthalf");
734   g_string_append (string1, "lasthalf");
735   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
736   g_string_free (string1, TRUE);
737
738   /* append_len */
739
740   string1 = g_string_new ("firsthalf");
741   g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
742   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
743   g_string_free (string1, TRUE);  
744   
745   /* prepend */
746   string1 = g_string_new ("lasthalf");
747   g_string_prepend (string1, "firsthalf");
748   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
749   g_string_free (string1, TRUE);
750
751   /* prepend_len */
752   string1 = g_string_new ("lasthalf");
753   g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
754   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
755   g_string_free (string1, TRUE);
756   
757   /* insert */
758   string1 = g_string_new ("firstlast");
759   g_string_insert (string1, 5, "middle");
760   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
761   g_string_free (string1, TRUE);
762
763   /* insert with pos == end of the string */
764   string1 = g_string_new ("firstmiddle");
765   g_string_insert (string1, strlen ("firstmiddle"), "last");
766   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
767   g_string_free (string1, TRUE);
768   
769   /* insert_len */
770
771   string1 = g_string_new ("firstlast");
772   g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
773   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
774   g_string_free (string1, TRUE);
775
776   /* insert_len with magic -1 pos for append */
777   string1 = g_string_new ("first");
778   g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
779   g_assert (strcmp (string1->str, "firstlast") == 0);
780   g_string_free (string1, TRUE);
781   
782   /* insert_len with magic -1 len for strlen-the-string */
783   string1 = g_string_new ("first");
784   g_string_insert_len (string1, 5, "last", -1);
785   g_assert (strcmp (string1->str, "firstlast") == 0);
786   g_string_free (string1, TRUE);
787   
788   g_print ("ok\n");
789
790   g_print ("checking timers...\n");
791
792   timer = g_timer_new ();
793   g_print ("  spinning for 3 seconds...\n");
794
795   g_timer_start (timer);
796   while (g_timer_elapsed (timer, NULL) < 3)
797     ;
798
799   g_timer_stop (timer);
800   g_timer_destroy (timer);
801
802   g_print ("ok\n");
803
804   g_print ("checking g_strcasecmp...");
805   g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0);
806   g_assert (g_strcasecmp ("frobozz", "frobozz") == 0);
807   g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0);
808   g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0);
809   g_assert (g_strcasecmp ("", "") == 0);
810   g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0);
811   g_assert (g_strcasecmp ("a", "b") < 0);
812   g_assert (g_strcasecmp ("a", "B") < 0);
813   g_assert (g_strcasecmp ("A", "b") < 0);
814   g_assert (g_strcasecmp ("A", "B") < 0);
815   g_assert (g_strcasecmp ("b", "a") > 0);
816   g_assert (g_strcasecmp ("b", "A") > 0);
817   g_assert (g_strcasecmp ("B", "a") > 0);
818   g_assert (g_strcasecmp ("B", "A") > 0);
819
820   g_print ("ok\n");
821
822   g_print ("checking g_strdup...");
823   g_assert(g_strdup(NULL) == NULL);
824   string = g_strdup(GLIB_TEST_STRING);
825   g_assert(string != NULL);
826   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
827   g_free(string);
828
829   g_print ("ok\n");
830
831   g_print ("checking g_strconcat...");
832   string = g_strconcat(GLIB_TEST_STRING, NULL);
833   g_assert(string != NULL);
834   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
835   g_free(string);
836   string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING, 
837                        GLIB_TEST_STRING, NULL);
838   g_assert(string != NULL);
839   g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
840                           GLIB_TEST_STRING) == 0);
841   g_free(string);
842   
843   g_print ("ok\n");
844
845   g_print ("checking g_strdup_printf...");
846   string = g_strdup_printf ("%05d %-5s", 21, "test");
847   g_assert (string != NULL);
848   g_assert (strcmp(string, "00021 test ") == 0);
849   g_free (string);
850
851   g_print ("ok\n");
852
853   /* g_debug (argv[0]); */
854
855   /* Relation tests */
856
857   g_print ("checking relations...");
858
859   relation = g_relation_new (2);
860
861   g_relation_index (relation, 0, g_int_hash, g_int_equal);
862   g_relation_index (relation, 1, g_int_hash, g_int_equal);
863
864   for (i = 0; i < 1024; i += 1)
865     data[i] = i;
866
867   for (i = 1; i < 1023; i += 1)
868     {
869       g_relation_insert (relation, data + i, data + i + 1);
870       g_relation_insert (relation, data + i, data + i - 1);
871     }
872
873   for (i = 2; i < 1022; i += 1)
874     {
875       g_assert (! g_relation_exists (relation, data + i, data + i));
876       g_assert (! g_relation_exists (relation, data + i, data + i + 2));
877       g_assert (! g_relation_exists (relation, data + i, data + i - 2));
878     }
879
880   for (i = 1; i < 1023; i += 1)
881     {
882       g_assert (g_relation_exists (relation, data + i, data + i + 1));
883       g_assert (g_relation_exists (relation, data + i, data + i - 1));
884     }
885
886   for (i = 2; i < 1022; i += 1)
887     {
888       g_assert (g_relation_count (relation, data + i, 0) == 2);
889       g_assert (g_relation_count (relation, data + i, 1) == 2);
890     }
891
892   g_assert (g_relation_count (relation, data, 0) == 0);
893
894   g_assert (g_relation_count (relation, data + 42, 0) == 2);
895   g_assert (g_relation_count (relation, data + 43, 1) == 2);
896   g_assert (g_relation_count (relation, data + 41, 1) == 2);
897   g_relation_delete (relation, data + 42, 0);
898   g_assert (g_relation_count (relation, data + 42, 0) == 0);
899   g_assert (g_relation_count (relation, data + 43, 1) == 1);
900   g_assert (g_relation_count (relation, data + 41, 1) == 1);
901
902   tuples = g_relation_select (relation, data + 200, 0);
903
904   g_assert (tuples->len == 2);
905
906 #if 0
907   for (i = 0; i < tuples->len; i += 1)
908     {
909       printf ("%d %d\n",
910               *(gint*) g_tuples_index (tuples, i, 0),
911               *(gint*) g_tuples_index (tuples, i, 1));
912     }
913 #endif
914
915   g_assert (g_relation_exists (relation, data + 300, data + 301));
916   g_relation_delete (relation, data + 300, 0);
917   g_assert (!g_relation_exists (relation, data + 300, data + 301));
918
919   g_tuples_destroy (tuples);
920
921   g_relation_destroy (relation);
922
923   relation = NULL;
924
925   g_print ("ok\n");
926
927   g_print ("checking pointer arrays...");
928
929   gparray = g_ptr_array_new ();
930   for (i = 0; i < 10000; i++)
931     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
932
933   for (i = 0; i < 10000; i++)
934     if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
935       g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
936
937   g_ptr_array_free (gparray, TRUE);
938
939   g_print ("ok\n");
940
941
942   g_print ("checking byte arrays...");
943
944   gbarray = g_byte_array_new ();
945   for (i = 0; i < 10000; i++)
946     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
947
948   for (i = 0; i < 10000; i++)
949     {
950       g_assert (gbarray->data[4*i] == 'a');
951       g_assert (gbarray->data[4*i+1] == 'b');
952       g_assert (gbarray->data[4*i+2] == 'c');
953       g_assert (gbarray->data[4*i+3] == 'd');
954     }
955
956   g_byte_array_free (gbarray, TRUE);
957   g_print ("ok\n");
958
959   g_printerr ("g_log tests:");
960   g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
961   g_message ("the next warning is a test:");
962   string = NULL;
963   g_print (string);
964
965   g_print ("checking endian macros (host is ");
966 #if G_BYTE_ORDER == G_BIG_ENDIAN
967   g_print ("big endian)...");
968 #else
969   g_print ("little endian)...");
970 #endif
971   g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);  
972   g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);  
973 #ifdef G_HAVE_GINT64
974   g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);  
975 #endif
976
977   g_print ("ok\n");
978
979 #ifdef G_OS_WIN32
980   g_print ("current locale: %s\n", g_win32_getlocale ());
981 #endif
982
983   return 0;
984 }
985