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