added a GNode test.
[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
20 #include <stdio.h>
21 #include <string.h>
22 #include "glib.h"
23
24 int array[10000];
25 gboolean failed = FALSE;
26
27 #define TEST(m,cond)    G_STMT_START { failed = !(cond); \
28 if (failed) \
29   { if (!m) \
30       g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
31     else \
32       g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
33   } \
34 else \
35   g_print ("."); fflush (stdout); \
36 } G_STMT_END
37
38 #define C2P(c)          ((gpointer) ((long) (c)))
39 #define P2C(p)          ((gchar) ((long) (p)))
40
41 static gboolean
42 node_build_string (GNode    *node,
43                    gpointer  data)
44 {
45   gchar **p = data;
46   gchar *string;
47   gchar c[2] = "_";
48
49   c[0] = P2C (node->data);
50
51   string = g_strconcat (*p ? *p : "", c, NULL);
52   g_free (*p);
53   *p = string;
54
55   return FALSE;
56 }
57
58 static void
59 g_node_test (void)
60 {
61   GNode *root;
62   GNode *node;
63   GNode *node_B;
64   GNode *node_F;
65   GNode *node_G;
66   GNode *node_J;
67   guint i;
68   gchar *tstring;
69
70   g_print ("checking n-way trees: ");
71   failed = FALSE;
72
73   root = g_node_new (C2P ('A'));
74   TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
75
76   node_B = g_node_new (C2P ('B'));
77   g_node_append (root, node_B);
78   TEST (NULL, root->children == node_B);
79
80   g_node_append (node_B, g_node_new (C2P ('E')));
81   g_node_prepend (node_B, g_node_new (C2P ('C')));
82   g_node_insert (node_B, 1, g_node_new (C2P ('D')));
83
84   node_F = g_node_new (C2P ('F'));
85   g_node_append (root, node_F);
86   TEST (NULL, root->children->next == node_F);
87
88   node_G = g_node_new (C2P ('G'));
89   g_node_append (node_F, node_G);
90   node_J = g_node_new (C2P ('J'));
91   g_node_insert (node_G, -1, node_J);
92   g_node_insert (node_G, 42, g_node_new (C2P ('K')));
93   g_node_insert (node_G, 0, g_node_new (C2P ('H')));
94   g_node_insert (node_G, 1, g_node_new (C2P ('I')));
95
96   TEST (NULL, g_node_depth (root) == 1);
97   TEST (NULL, g_node_max_height (root) == 4);
98   TEST (NULL, g_node_depth (node_G->children->next) == 4);
99   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
100   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
101   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
102   TEST (NULL, g_node_max_height (node_F) == 3);
103   TEST (NULL, g_node_n_children (node_G) == 4);
104   TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
105   TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
106   TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
107
108   for (i = 0; i < g_node_n_children (node_B); i++)
109     {
110       node = g_node_nth_child (node_B, i);
111       TEST (NULL, P2C (node->data) == ('C' + i));
112     }
113   
114   for (i = 0; i < g_node_n_children (node_G); i++)
115     TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
116
117   /* we have built:                    A
118    *                                 /   \
119    *                               B       F
120    *                             / | \       \
121    *                           C   D   E       G
122    *                                         / /\ \
123    *                                       H  I  J  K
124    *
125    * for in-order traversal, 'G' is considered to be the "left"
126    * child of 'F', which will cause 'F' to be the last node visited.
127    */
128
129   tstring = NULL;
130   g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
131   TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
132   g_free (tstring); tstring = NULL;
133   g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
134   TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
135   g_free (tstring); tstring = NULL;
136   g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
137   TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
138   g_free (tstring); tstring = NULL;
139   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
140   TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
141   g_free (tstring); tstring = NULL;
142   
143   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
144   TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
145   g_free (tstring); tstring = NULL;
146   g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
147   TEST (tstring, strcmp (tstring, "ABFG") == 0);
148   g_free (tstring); tstring = NULL;
149
150   g_node_reverse_children (node_B);
151   g_node_reverse_children (node_G);
152
153   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
154   TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
155   g_free (tstring); tstring = NULL;
156   
157   g_node_destroy (root);
158
159   /* allocation tests */
160
161   root = g_node_new (NULL);
162   node = root;
163
164   for (i = 0; i < 2048; i++)
165     {
166       g_node_append (node, g_node_new (NULL));
167       if ((i%5) == 4)
168         node = node->children->next;
169     }
170   TEST (NULL, g_node_max_height (root) > 100);
171   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
172
173   g_node_destroy (root);
174   
175   if (!failed)
176     g_print ("ok\n");
177 }
178
179 void
180 my_hash_callback (gpointer key,
181                   gpointer value,
182                   gpointer user_data)
183 {
184   int *d = value;
185   *d = 1;
186 }
187
188 guint
189 my_hash (gconstpointer key)
190 {
191   return (guint) *((const gint*) key);
192 }
193
194 gint
195 my_hash_compare (gconstpointer a,
196                  gconstpointer b)
197 {
198   return *((const gint*) a) == *((const gint*) b);
199 }
200
201 gint 
202 my_list_compare_one (gconstpointer a, gconstpointer b)
203 {
204   gint one = *((const gint*)a);
205   gint two = *((const gint*)b);
206   return one-two;
207 }
208
209 gint 
210 my_list_compare_two (gconstpointer a, gconstpointer b)
211 {
212   gint one = *((const gint*)a);
213   gint two = *((const gint*)b);
214   return two-one;
215 }
216
217 /* void
218 my_list_print (gpointer a, gpointer b)
219 {
220   gint three = *((gint*)a);
221   g_print("%d", three);
222 }; */
223
224 gint
225 my_compare (gconstpointer a,
226             gconstpointer b)
227 {
228   const char *cha = a;
229   const char *chb = b;
230
231   return *cha - *chb;
232 }
233
234 gint
235 my_traverse (gpointer key,
236              gpointer value,
237              gpointer data)
238 {
239   char *ch = key;
240   g_print ("%c ", *ch);
241   return FALSE;
242 }
243
244 int
245 main (int   argc,
246       char *argv[])
247 {
248   GList *list, *t;
249   GSList *slist, *st;
250   GHashTable *hash_table;
251   GMemChunk *mem_chunk;
252   GStringChunk *string_chunk;
253   GTimer *timer;
254   gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
255   gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
256
257   gchar *mem[10000], *tmp_string, *tmp_string_2;
258   gint i, j;
259   GArray *garray;
260   GPtrArray *gparray;
261   GByteArray *gbarray;
262   GString *string1, *string2;
263   GTree *tree;
264   char chars[62];
265   GRelation *relation;
266   GTuples *tuples;
267   gint data [1024];
268   struct {
269     gchar *filename;
270     gchar *dirname;
271   } dirname_checks[] = {
272     { "/", "/" },
273     { "////", "/" },
274     { ".////", "." },
275     { ".", "." },
276     { "..", "." },
277     { "../", ".." },
278     { "..////", ".." },
279     { "", "." },
280     { "a/b", "a" },
281     { "a/b/", "a/b" },
282     { "c///", "c" },
283   };
284   guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
285
286   
287   g_print ("checking size of gint8...%ld (should be 1)\n", (glong)sizeof (gint8));
288   g_print ("checking size of gint16...%ld (should be 2)\n", (glong)sizeof (gint16));
289   g_print ("checking size of gint32...%ld (should be 4)\n", (glong)sizeof (gint32));
290
291   g_print ("checking g_dirname()...");
292   for (i = 0; i < n_dirname_checks; i++)
293     {
294       gchar *dirname;
295
296       dirname = g_dirname (dirname_checks[i].filename);
297       if (strcmp (dirname, dirname_checks[i].dirname) != 0)
298         {
299           g_print ("failed for \"%s\"==\"%s\" (returned: \"%s\")\n",
300                    dirname_checks[i].filename,
301                    dirname_checks[i].dirname,
302                    dirname);
303           n_dirname_checks = 0;
304         }
305       g_free (dirname);
306     }
307   if (n_dirname_checks)
308     g_print ("ok\n");
309
310   g_print ("checking doubly linked lists...");
311
312   list = NULL;
313   for (i = 0; i < 10; i++)
314     list = g_list_append (list, &nums[i]);
315   list = g_list_reverse (list);
316
317   for (i = 0; i < 10; i++)
318     {
319       t = g_list_nth (list, i);
320       if (*((gint*) t->data) != (9 - i))
321         g_error ("Regular insert failed");
322     }
323
324   for (i = 0; i < 10; i++)
325     if(g_list_position(list, g_list_nth (list, i)) != i)
326       g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
327
328   g_list_free (list);
329   list = NULL;
330   
331   for (i = 0; i < 10; i++)
332     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
333
334   /*
335   g_print("\n");
336   g_list_foreach (list, my_list_print, NULL);
337   */
338
339   for (i = 0; i < 10; i++)
340     {
341       t = g_list_nth (list, i);
342       if (*((gint*) t->data) != i)
343          g_error ("Sorted insert failed");
344     }
345     
346   g_list_free (list);
347   list = NULL;
348   
349   for (i = 0; i < 10; i++)
350     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
351
352   /*
353   g_print("\n");
354   g_list_foreach (list, my_list_print, NULL);
355   */
356
357   for (i = 0; i < 10; i++)
358     {
359       t = g_list_nth (list, i);
360       if (*((gint*) t->data) != (9 - i))
361          g_error ("Sorted insert failed");
362     }
363     
364   g_list_free (list);
365
366   g_print ("ok\n");
367
368
369   g_print ("checking singly linked lists...");
370
371   slist = NULL;
372   for (i = 0; i < 10; i++)
373     slist = g_slist_append (slist, &nums[i]);
374   slist = g_slist_reverse (slist);
375
376   for (i = 0; i < 10; i++)
377     {
378       st = g_slist_nth (slist, i);
379       if (*((gint*) st->data) != (9 - i))
380         g_error ("failed");
381     }
382
383   g_slist_free (slist);
384   slist = NULL;
385
386   for (i = 0; i < 10; i++)
387     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
388
389   /*
390   g_print("\n");
391   g_slist_foreach (slist, my_list_print, NULL);
392   */
393
394   for (i = 0; i < 10; i++)
395     {
396       st = g_slist_nth (slist, i);
397       if (*((gint*) st->data) != i)
398          g_error ("Sorted insert failed");
399     }
400      
401   g_slist_free(slist);
402   slist = NULL;
403    
404   for (i = 0; i < 10; i++)
405     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
406
407   /*
408   g_print("\n");
409   g_slist_foreach (slist, my_list_print, NULL);
410   */
411
412   for (i = 0; i < 10; i++)
413     {
414       st = g_slist_nth (slist, i);
415       if (*((gint*) st->data) != (9 - i))
416          g_error("Sorted insert failed");
417     }
418     
419   g_slist_free(slist);
420
421   g_print ("ok\n");
422
423
424   g_print ("checking binary trees...\n");
425
426   tree = g_tree_new (my_compare);
427   i = 0;
428   for (j = 0; j < 10; j++, i++)
429     {
430       chars[i] = '0' + j;
431       g_tree_insert (tree, &chars[i], &chars[i]);
432     }
433   for (j = 0; j < 26; j++, i++)
434     {
435       chars[i] = 'A' + j;
436       g_tree_insert (tree, &chars[i], &chars[i]);
437     }
438   for (j = 0; j < 26; j++, i++)
439     {
440       chars[i] = 'a' + j;
441       g_tree_insert (tree, &chars[i], &chars[i]);
442     }
443
444   g_print ("tree height: %d\n", g_tree_height (tree));
445   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
446
447   g_print ("tree: ");
448   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
449   g_print ("\n");
450
451   for (i = 0; i < 10; i++)
452     g_tree_remove (tree, &chars[i]);
453
454   g_print ("tree height: %d\n", g_tree_height (tree));
455   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
456
457   g_print ("tree: ");
458   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
459   g_print ("\n");
460
461   g_print ("ok\n");
462
463
464   /* check n-way trees */
465   g_node_test ();
466
467   g_print ("checking mem chunks...");
468
469   mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
470
471   for (i = 0; i < 10000; i++)
472     {
473       mem[i] = g_chunk_new (gchar, mem_chunk);
474
475       for (j = 0; j < 50; j++)
476         mem[i][j] = i * j;
477     }
478
479   for (i = 0; i < 10000; i++)
480     {
481       g_mem_chunk_free (mem_chunk, mem[i]);
482     }
483
484   g_print ("ok\n");
485
486
487   g_print ("checking hash tables...");
488
489   hash_table = g_hash_table_new (my_hash, my_hash_compare);
490   for (i = 0; i < 10000; i++)
491     {
492       array[i] = i;
493       g_hash_table_insert (hash_table, &array[i], &array[i]);
494     }
495   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
496
497   for (i = 0; i < 10000; i++)
498     if (array[i] == 0)
499       g_print ("%d\n", i);
500
501   for (i = 0; i < 10000; i++)
502     g_hash_table_remove (hash_table, &array[i]);
503
504   g_hash_table_destroy (hash_table);
505
506   g_print ("ok\n");
507
508
509   g_print ("checking string chunks...");
510
511   string_chunk = g_string_chunk_new (1024);
512
513   for (i = 0; i < 100000; i ++)
514     {
515       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
516
517       if (strcmp ("hi pete", tmp_string) != 0)
518         g_error ("string chunks are broken.\n");
519     }
520
521   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
522
523   g_assert (tmp_string_2 != tmp_string &&
524             strcmp(tmp_string_2, tmp_string) == 0);
525
526   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
527
528   g_assert (tmp_string_2 == tmp_string);
529
530   g_string_chunk_free (string_chunk);
531
532   g_print ("ok\n");
533
534
535   g_print ("checking arrays...");
536
537   garray = g_array_new (FALSE);
538   for (i = 0; i < 10000; i++)
539     g_array_append_val (garray, gint, i);
540
541   for (i = 0; i < 10000; i++)
542     if (g_array_index (garray, gint, i) != i)
543       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
544
545   g_array_free (garray, TRUE);
546
547   garray = g_array_new (FALSE);
548   for (i = 0; i < 10000; i++)
549     g_array_prepend_val (garray, gint, i);
550
551   for (i = 0; i < 10000; i++)
552     if (g_array_index (garray, gint, i) != (10000 - i - 1))
553       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 10000 - i - 1);
554
555   g_array_free (garray, TRUE);
556
557   g_print ("ok\n");
558
559
560   g_print ("checking strings...");
561
562   string1 = g_string_new ("hi pete!");
563   string2 = g_string_new ("");
564
565   g_assert (strcmp ("hi pete!", string1->str) == 0);
566
567   for (i = 0; i < 10000; i++)
568     g_string_append_c (string1, 'a'+(i%26));
569
570   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
571                     "this pete guy sure is a wuss, like he's the number ",
572                     1,
573                     " wuss.  everyone agrees.\n",
574                     string1->str,
575                     10, 666, 15, 15, 666.666666666, 666.666666666);
576
577   g_print ("ok\n");
578
579   g_print ("checking timers...\n");
580
581   timer = g_timer_new ();
582   g_print ("  spinning for 3 seconds...\n");
583
584   g_timer_start (timer);
585   while (g_timer_elapsed (timer, NULL) < 3)
586     ;
587
588   g_timer_stop (timer);
589   g_timer_destroy (timer);
590
591   g_print ("ok\n");
592
593   g_print ("checking g_strcasecmp...\n");
594
595   /* g_debug (argv[0]); */
596
597   /* Relation tests */
598
599   g_print ("checking relations...");
600
601   relation = g_relation_new (2);
602
603   g_relation_index (relation, 0, g_int_hash, g_int_equal);
604   g_relation_index (relation, 1, g_int_hash, g_int_equal);
605
606   for (i = 0; i < 1024; i += 1)
607     data[i] = i;
608
609   for (i = 1; i < 1023; i += 1)
610     {
611       g_relation_insert (relation, data + i, data + i + 1);
612       g_relation_insert (relation, data + i, data + i - 1);
613     }
614
615   for (i = 2; i < 1022; i += 1)
616     {
617       g_assert (! g_relation_exists (relation, data + i, data + i));
618       g_assert (! g_relation_exists (relation, data + i, data + i + 2));
619       g_assert (! g_relation_exists (relation, data + i, data + i - 2));
620     }
621
622   for (i = 1; i < 1023; i += 1)
623     {
624       g_assert (g_relation_exists (relation, data + i, data + i + 1));
625       g_assert (g_relation_exists (relation, data + i, data + i - 1));
626     }
627
628   for (i = 2; i < 1022; i += 1)
629     {
630       g_assert (g_relation_count (relation, data + i, 0) == 2);
631       g_assert (g_relation_count (relation, data + i, 1) == 2);
632     }
633
634   g_assert (g_relation_count (relation, data, 0) == 0);
635
636   g_assert (g_relation_count (relation, data + 42, 0) == 2);
637   g_assert (g_relation_count (relation, data + 43, 1) == 2);
638   g_assert (g_relation_count (relation, data + 41, 1) == 2);
639   g_relation_delete (relation, data + 42, 0);
640   g_assert (g_relation_count (relation, data + 42, 0) == 0);
641   g_assert (g_relation_count (relation, data + 43, 1) == 1);
642   g_assert (g_relation_count (relation, data + 41, 1) == 1);
643
644   tuples = g_relation_select (relation, data + 200, 0);
645
646   g_assert (tuples->len == 2);
647
648 #if 0
649   for (i = 0; i < tuples->len; i += 1)
650     {
651       printf ("%d %d\n",
652               *(gint*) g_tuples_index (tuples, i, 0),
653               *(gint*) g_tuples_index (tuples, i, 1));
654     }
655 #endif
656
657   g_assert (g_relation_exists (relation, data + 300, data + 301));
658   g_relation_delete (relation, data + 300, 0);
659   g_assert (!g_relation_exists (relation, data + 300, data + 301));
660
661   g_tuples_destroy (tuples);
662
663   g_relation_destroy (relation);
664
665   relation = NULL;
666
667   g_print ("ok\n");
668
669   g_print ("checking pointer arrays...");
670
671   gparray = g_ptr_array_new ();
672   for (i = 0; i < 10000; i++)
673     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
674
675   for (i = 0; i < 10000; i++)
676     if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
677       g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
678
679   g_ptr_array_free (gparray, TRUE);
680
681   g_print ("ok\n");
682
683
684   g_print ("checking byte arrays...");
685
686   gbarray = g_byte_array_new ();
687   for (i = 0; i < 10000; i++)
688     g_byte_array_append (gbarray, "abcd", 4);
689
690   for (i = 0; i < 10000; i++)
691     {
692       g_assert (gbarray->data[4*i] == 'a');
693       g_assert (gbarray->data[4*i+1] == 'b');
694       g_assert (gbarray->data[4*i+2] == 'c');
695       g_assert (gbarray->data[4*i+3] == 'd');
696     }
697
698   g_byte_array_free (gbarray, TRUE);
699
700   g_print ("ok\n");
701
702   return 0;
703 }