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