version bump to 1.1.1, binary age 1, interface age 0. updates. updates.
[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_data (node_B, C2P ('E'));
81   g_node_prepend_data (node_B, 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_prepend (node_G, node_J);
92   g_node_insert (node_G, 42, g_node_new (C2P ('K')));
93   g_node_insert_data (node_G, 0, 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   gchar *string;
257
258   gchar *mem[10000], *tmp_string, *tmp_string_2;
259   gint i, j;
260   GArray *garray;
261   GPtrArray *gparray;
262   GByteArray *gbarray;
263   GString *string1, *string2;
264   GTree *tree;
265   char chars[62];
266   GRelation *relation;
267   GTuples *tuples;
268   gint data [1024];
269   struct {
270     gchar *filename;
271     gchar *dirname;
272   } dirname_checks[] = {
273     { "/", "/" },
274     { "////", "/" },
275     { ".////", "." },
276     { ".", "." },
277     { "..", "." },
278     { "../", ".." },
279     { "..////", ".." },
280     { "", "." },
281     { "a/b", "a" },
282     { "a/b/", "a/b" },
283     { "c///", "c" },
284   };
285   guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
286
287   g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
288            glib_major_version,
289            glib_minor_version,
290            glib_micro_version,
291            glib_interface_age,
292            glib_binary_age);
293
294   string = g_get_current_dir ();
295   g_print ("cwd: %s\n", string);
296   g_free (string);
297
298   /* type sizes */
299   g_print ("checking size of gint8: %d", sizeof (gint8));
300   TEST (NULL, sizeof (gint8) == 1);
301   g_print ("\nchecking size of gint16: %d", sizeof (gint16));
302   TEST (NULL, sizeof (gint16) == 2);
303   g_print ("\nchecking size of gint32: %d", sizeof (gint32));
304   TEST (NULL, sizeof (gint32) == 4);
305 #ifdef  HAVE_GINT64
306   g_print ("\nchecking size of gint64: %d", sizeof (gint64));
307   TEST (NULL, sizeof (gint64) == 8);
308 #endif  /* HAVE_GINT64 */
309   g_print ("\n");
310
311   g_print ("checking g_dirname()...");
312   for (i = 0; i < n_dirname_checks; i++)
313     {
314       gchar *dirname;
315
316       dirname = g_dirname (dirname_checks[i].filename);
317       if (strcmp (dirname, dirname_checks[i].dirname) != 0)
318         {
319           g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
320                    dirname_checks[i].filename,
321                    dirname_checks[i].dirname,
322                    dirname);
323           n_dirname_checks = 0;
324         }
325       g_free (dirname);
326     }
327   if (n_dirname_checks)
328     g_print ("ok\n");
329
330   g_print ("checking doubly linked lists...");
331
332   list = NULL;
333   for (i = 0; i < 10; i++)
334     list = g_list_append (list, &nums[i]);
335   list = g_list_reverse (list);
336
337   for (i = 0; i < 10; i++)
338     {
339       t = g_list_nth (list, i);
340       if (*((gint*) t->data) != (9 - i))
341         g_error ("Regular insert failed");
342     }
343
344   for (i = 0; i < 10; i++)
345     if(g_list_position(list, g_list_nth (list, i)) != i)
346       g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
347
348   g_list_free (list);
349   list = NULL;
350   
351   for (i = 0; i < 10; i++)
352     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
353
354   /*
355   g_print("\n");
356   g_list_foreach (list, my_list_print, NULL);
357   */
358
359   for (i = 0; i < 10; i++)
360     {
361       t = g_list_nth (list, i);
362       if (*((gint*) t->data) != i)
363          g_error ("Sorted insert failed");
364     }
365     
366   g_list_free (list);
367   list = NULL;
368   
369   for (i = 0; i < 10; i++)
370     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
371
372   /*
373   g_print("\n");
374   g_list_foreach (list, my_list_print, NULL);
375   */
376
377   for (i = 0; i < 10; i++)
378     {
379       t = g_list_nth (list, i);
380       if (*((gint*) t->data) != (9 - i))
381          g_error ("Sorted insert failed");
382     }
383     
384   g_list_free (list);
385
386   g_print ("ok\n");
387
388
389   g_print ("checking singly linked lists...");
390
391   slist = NULL;
392   for (i = 0; i < 10; i++)
393     slist = g_slist_append (slist, &nums[i]);
394   slist = g_slist_reverse (slist);
395
396   for (i = 0; i < 10; i++)
397     {
398       st = g_slist_nth (slist, i);
399       if (*((gint*) st->data) != (9 - i))
400         g_error ("failed");
401     }
402
403   g_slist_free (slist);
404   slist = NULL;
405
406   for (i = 0; i < 10; i++)
407     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
408
409   /*
410   g_print("\n");
411   g_slist_foreach (slist, my_list_print, NULL);
412   */
413
414   for (i = 0; i < 10; i++)
415     {
416       st = g_slist_nth (slist, i);
417       if (*((gint*) st->data) != i)
418          g_error ("Sorted insert failed");
419     }
420      
421   g_slist_free(slist);
422   slist = NULL;
423    
424   for (i = 0; i < 10; i++)
425     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
426
427   /*
428   g_print("\n");
429   g_slist_foreach (slist, my_list_print, NULL);
430   */
431
432   for (i = 0; i < 10; i++)
433     {
434       st = g_slist_nth (slist, i);
435       if (*((gint*) st->data) != (9 - i))
436          g_error("Sorted insert failed");
437     }
438     
439   g_slist_free(slist);
440
441   g_print ("ok\n");
442
443
444   g_print ("checking binary trees...\n");
445
446   tree = g_tree_new (my_compare);
447   i = 0;
448   for (j = 0; j < 10; j++, i++)
449     {
450       chars[i] = '0' + j;
451       g_tree_insert (tree, &chars[i], &chars[i]);
452     }
453   for (j = 0; j < 26; j++, i++)
454     {
455       chars[i] = 'A' + j;
456       g_tree_insert (tree, &chars[i], &chars[i]);
457     }
458   for (j = 0; j < 26; j++, i++)
459     {
460       chars[i] = 'a' + j;
461       g_tree_insert (tree, &chars[i], &chars[i]);
462     }
463
464   g_print ("tree height: %d\n", g_tree_height (tree));
465   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
466
467   g_print ("tree: ");
468   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
469   g_print ("\n");
470
471   for (i = 0; i < 10; i++)
472     g_tree_remove (tree, &chars[i]);
473
474   g_print ("tree height: %d\n", g_tree_height (tree));
475   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
476
477   g_print ("tree: ");
478   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
479   g_print ("\n");
480
481   g_print ("ok\n");
482
483
484   /* check n-way trees */
485   g_node_test ();
486
487   g_print ("checking mem chunks...");
488
489   mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
490
491   for (i = 0; i < 10000; i++)
492     {
493       mem[i] = g_chunk_new (gchar, mem_chunk);
494
495       for (j = 0; j < 50; j++)
496         mem[i][j] = i * j;
497     }
498
499   for (i = 0; i < 10000; i++)
500     {
501       g_mem_chunk_free (mem_chunk, mem[i]);
502     }
503
504   g_print ("ok\n");
505
506
507   g_print ("checking hash tables...");
508
509   hash_table = g_hash_table_new (my_hash, my_hash_compare);
510   for (i = 0; i < 10000; i++)
511     {
512       array[i] = i;
513       g_hash_table_insert (hash_table, &array[i], &array[i]);
514     }
515   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
516
517   for (i = 0; i < 10000; i++)
518     if (array[i] == 0)
519       g_print ("%d\n", i);
520
521   for (i = 0; i < 10000; i++)
522     g_hash_table_remove (hash_table, &array[i]);
523
524   g_hash_table_destroy (hash_table);
525
526   g_print ("ok\n");
527
528
529   g_print ("checking string chunks...");
530
531   string_chunk = g_string_chunk_new (1024);
532
533   for (i = 0; i < 100000; i ++)
534     {
535       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
536
537       if (strcmp ("hi pete", tmp_string) != 0)
538         g_error ("string chunks are broken.\n");
539     }
540
541   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
542
543   g_assert (tmp_string_2 != tmp_string &&
544             strcmp(tmp_string_2, tmp_string) == 0);
545
546   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
547
548   g_assert (tmp_string_2 == tmp_string);
549
550   g_string_chunk_free (string_chunk);
551
552   g_print ("ok\n");
553
554
555   g_print ("checking arrays...");
556
557   garray = g_array_new (FALSE);
558   for (i = 0; i < 10000; i++)
559     g_array_append_val (garray, gint, i);
560
561   for (i = 0; i < 10000; i++)
562     if (g_array_index (garray, gint, i) != i)
563       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
564
565   g_array_free (garray, TRUE);
566
567   garray = g_array_new (FALSE);
568   for (i = 0; i < 10000; i++)
569     g_array_prepend_val (garray, gint, i);
570
571   for (i = 0; i < 10000; i++)
572     if (g_array_index (garray, gint, i) != (10000 - i - 1))
573       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 10000 - i - 1);
574
575   g_array_free (garray, TRUE);
576
577   g_print ("ok\n");
578
579
580   g_print ("checking strings...");
581
582   string1 = g_string_new ("hi pete!");
583   string2 = g_string_new ("");
584
585   g_assert (strcmp ("hi pete!", string1->str) == 0);
586
587   for (i = 0; i < 10000; i++)
588     g_string_append_c (string1, 'a'+(i%26));
589
590   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
591                     "this pete guy sure is a wuss, like he's the number ",
592                     1,
593                     " wuss.  everyone agrees.\n",
594                     string1->str,
595                     10, 666, 15, 15, 666.666666666, 666.666666666);
596
597   g_print ("ok\n");
598
599   g_print ("checking timers...\n");
600
601   timer = g_timer_new ();
602   g_print ("  spinning for 3 seconds...\n");
603
604   g_timer_start (timer);
605   while (g_timer_elapsed (timer, NULL) < 3)
606     ;
607
608   g_timer_stop (timer);
609   g_timer_destroy (timer);
610
611   g_print ("ok\n");
612
613   g_print ("checking g_strcasecmp...\n");
614
615   /* g_debug (argv[0]); */
616
617   /* Relation tests */
618
619   g_print ("checking relations...");
620
621   relation = g_relation_new (2);
622
623   g_relation_index (relation, 0, g_int_hash, g_int_equal);
624   g_relation_index (relation, 1, g_int_hash, g_int_equal);
625
626   for (i = 0; i < 1024; i += 1)
627     data[i] = i;
628
629   for (i = 1; i < 1023; i += 1)
630     {
631       g_relation_insert (relation, data + i, data + i + 1);
632       g_relation_insert (relation, data + i, data + i - 1);
633     }
634
635   for (i = 2; i < 1022; i += 1)
636     {
637       g_assert (! g_relation_exists (relation, data + i, data + i));
638       g_assert (! g_relation_exists (relation, data + i, data + i + 2));
639       g_assert (! g_relation_exists (relation, data + i, data + i - 2));
640     }
641
642   for (i = 1; i < 1023; i += 1)
643     {
644       g_assert (g_relation_exists (relation, data + i, data + i + 1));
645       g_assert (g_relation_exists (relation, data + i, data + i - 1));
646     }
647
648   for (i = 2; i < 1022; i += 1)
649     {
650       g_assert (g_relation_count (relation, data + i, 0) == 2);
651       g_assert (g_relation_count (relation, data + i, 1) == 2);
652     }
653
654   g_assert (g_relation_count (relation, data, 0) == 0);
655
656   g_assert (g_relation_count (relation, data + 42, 0) == 2);
657   g_assert (g_relation_count (relation, data + 43, 1) == 2);
658   g_assert (g_relation_count (relation, data + 41, 1) == 2);
659   g_relation_delete (relation, data + 42, 0);
660   g_assert (g_relation_count (relation, data + 42, 0) == 0);
661   g_assert (g_relation_count (relation, data + 43, 1) == 1);
662   g_assert (g_relation_count (relation, data + 41, 1) == 1);
663
664   tuples = g_relation_select (relation, data + 200, 0);
665
666   g_assert (tuples->len == 2);
667
668 #if 0
669   for (i = 0; i < tuples->len; i += 1)
670     {
671       printf ("%d %d\n",
672               *(gint*) g_tuples_index (tuples, i, 0),
673               *(gint*) g_tuples_index (tuples, i, 1));
674     }
675 #endif
676
677   g_assert (g_relation_exists (relation, data + 300, data + 301));
678   g_relation_delete (relation, data + 300, 0);
679   g_assert (!g_relation_exists (relation, data + 300, data + 301));
680
681   g_tuples_destroy (tuples);
682
683   g_relation_destroy (relation);
684
685   relation = NULL;
686
687   g_print ("ok\n");
688
689   g_print ("checking pointer arrays...");
690
691   gparray = g_ptr_array_new ();
692   for (i = 0; i < 10000; i++)
693     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
694
695   for (i = 0; i < 10000; i++)
696     if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
697       g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
698
699   g_ptr_array_free (gparray, TRUE);
700
701   g_print ("ok\n");
702
703
704   g_print ("checking byte arrays...");
705
706   gbarray = g_byte_array_new ();
707   for (i = 0; i < 10000; i++)
708     g_byte_array_append (gbarray, "abcd", 4);
709
710   for (i = 0; i < 10000; i++)
711     {
712       g_assert (gbarray->data[4*i] == 'a');
713       g_assert (gbarray->data[4*i+1] == 'b');
714       g_assert (gbarray->data[4*i+2] == 'c');
715       g_assert (gbarray->data[4*i+3] == 'd');
716     }
717
718   g_byte_array_free (gbarray, TRUE);
719
720   g_print ("ok\n");
721
722   return 0;
723 }