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