Initial revision
[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
20 #include <stdio.h>
21 #include <string.h>
22 #include "glib.h"
23
24 int array[10000];
25
26 void
27 my_hash_callback (gpointer key,
28                   gpointer value,
29                   gpointer user_data)
30 {
31   int *d = value;
32   *d = 1;
33 }
34
35 guint
36 my_hash (gconstpointer key)
37 {
38   return (guint) *((const gint*) key);
39 }
40
41 gint
42 my_hash_compare (gconstpointer a,
43                  gconstpointer b)
44 {
45   return *((const gint*) a) == *((const gint*) b);
46 }
47
48 gint 
49 my_list_compare_one (gconstpointer a, gconstpointer b)
50 {
51   gint one = *((const gint*)a);
52   gint two = *((const gint*)b);
53   return one-two;
54 }
55
56 gint 
57 my_list_compare_two (gconstpointer a, gconstpointer b)
58 {
59   gint one = *((const gint*)a);
60   gint two = *((const gint*)b);
61   return two-one;
62 }
63
64 /* void
65 my_list_print (gpointer a, gpointer b)
66 {
67   gint three = *((gint*)a);
68   g_print("%d", three);
69 }; */
70
71 gint
72 my_compare (gconstpointer a,
73             gconstpointer b)
74 {
75   const char *cha = a;
76   const char *chb = b;
77
78   return *cha - *chb;
79 }
80
81 gint
82 my_traverse (gpointer key,
83              gpointer value,
84              gpointer data)
85 {
86   char *ch = key;
87   g_print ("%c ", *ch);
88   return FALSE;
89 }
90
91 int
92 main (int   argc,
93       char *argv[])
94 {
95   GList *list, *t;
96   GSList *slist, *st;
97   GHashTable *hash_table;
98   GMemChunk *mem_chunk;
99   GStringChunk *string_chunk;
100   GTimer *timer;
101   gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
102   gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
103
104   gchar *mem[10000], *tmp_string, *tmp_string_2;
105   gint i, j;
106   GArray *garray;
107   GString *string1, *string2;
108   GTree *tree;
109   char chars[62];
110
111   g_print ("checking size of gint8...%ld (should be 1)\n", (glong)sizeof (gint8));
112   g_print ("checking size of gint16...%ld (should be 2)\n", (glong)sizeof (gint16));
113   g_print ("checking size of gint32...%ld (should be 4)\n", (glong)sizeof (gint32));
114
115   g_print ("checking doubly linked lists...");
116
117   list = NULL;
118   for (i = 0; i < 10; i++)
119     list = g_list_append (list, &nums[i]);
120   list = g_list_reverse (list);
121
122   for (i = 0; i < 10; i++)
123     {
124       t = g_list_nth (list, i);
125       if (*((gint*) t->data) != (9 - i))
126         g_error ("Regular insert failed");
127     }
128
129   for (i = 0; i < 10; i++)
130     if(g_list_position(list, g_list_nth (list, i)) != i)
131       g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
132
133   g_list_free (list);
134   list = NULL;
135   
136   for (i = 0; i < 10; i++)
137     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
138
139   /*
140   g_print("\n");
141   g_list_foreach (list, my_list_print, NULL);
142   */
143
144   for (i = 0; i < 10; i++)
145     {
146       t = g_list_nth (list, i);
147       if (*((gint*) t->data) != i)
148          g_error ("Sorted insert failed");
149     }
150     
151   g_list_free (list);
152   list = NULL;
153   
154   for (i = 0; i < 10; i++)
155     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
156
157   /*
158   g_print("\n");
159   g_list_foreach (list, my_list_print, NULL);
160   */
161
162   for (i = 0; i < 10; i++)
163     {
164       t = g_list_nth (list, i);
165       if (*((gint*) t->data) != (9 - i))
166          g_error ("Sorted insert failed");
167     }
168     
169   g_list_free (list);
170
171   g_print ("ok\n");
172
173
174   g_print ("checking singly linked lists...");
175
176   slist = NULL;
177   for (i = 0; i < 10; i++)
178     slist = g_slist_append (slist, &nums[i]);
179   slist = g_slist_reverse (slist);
180
181   for (i = 0; i < 10; i++)
182     {
183       st = g_slist_nth (slist, i);
184       if (*((gint*) st->data) != (9 - i))
185         g_error ("failed");
186     }
187
188   g_slist_free (slist);
189   slist = NULL;
190
191   for (i = 0; i < 10; i++)
192     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
193
194   /*
195   g_print("\n");
196   g_slist_foreach (slist, my_list_print, NULL);
197   */
198
199   for (i = 0; i < 10; i++)
200     {
201       st = g_slist_nth (slist, i);
202       if (*((gint*) st->data) != i)
203          g_error ("Sorted insert failed");
204     }
205      
206   g_slist_free(slist);
207   slist = NULL;
208    
209   for (i = 0; i < 10; i++)
210     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
211
212   /*
213   g_print("\n");
214   g_slist_foreach (slist, my_list_print, NULL);
215   */
216
217   for (i = 0; i < 10; i++)
218     {
219       st = g_slist_nth (slist, i);
220       if (*((gint*) st->data) != (9 - i))
221          g_error("Sorted insert failed");
222     }
223     
224   g_slist_free(slist);
225
226   g_print ("ok\n");
227
228
229   g_print ("checking trees...\n");
230
231   tree = g_tree_new (my_compare);
232   i = 0;
233   for (j = 0; j < 10; j++, i++)
234     {
235       chars[i] = '0' + j;
236       g_tree_insert (tree, &chars[i], &chars[i]);
237     }
238   for (j = 0; j < 26; j++, i++)
239     {
240       chars[i] = 'A' + j;
241       g_tree_insert (tree, &chars[i], &chars[i]);
242     }
243   for (j = 0; j < 26; j++, i++)
244     {
245       chars[i] = 'a' + j;
246       g_tree_insert (tree, &chars[i], &chars[i]);
247     }
248
249   g_print ("tree height: %d\n", g_tree_height (tree));
250   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
251
252   g_print ("tree: ");
253   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
254   g_print ("\n");
255
256   for (i = 0; i < 10; i++)
257     g_tree_remove (tree, &chars[i]);
258
259   g_print ("tree height: %d\n", g_tree_height (tree));
260   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
261
262   g_print ("tree: ");
263   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
264   g_print ("\n");
265
266   g_print ("ok\n");
267
268
269   g_print ("checking mem chunks...");
270
271   mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
272
273   for (i = 0; i < 10000; i++)
274     {
275       mem[i] = g_chunk_new (gchar, mem_chunk);
276
277       for (j = 0; j < 50; j++)
278         mem[i][j] = i * j;
279     }
280
281   for (i = 0; i < 10000; i++)
282     {
283       g_mem_chunk_free (mem_chunk, mem[i]);
284     }
285
286   g_print ("ok\n");
287
288
289   g_print ("checking hash tables...");
290
291   hash_table = g_hash_table_new (my_hash, my_hash_compare);
292   for (i = 0; i < 10000; i++)
293     {
294       array[i] = i;
295       g_hash_table_insert (hash_table, &array[i], &array[i]);
296     }
297   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
298
299   for (i = 0; i < 10000; i++)
300     if (array[i] == 0)
301       g_print ("%d\n", i);
302
303   for (i = 0; i < 10000; i++)
304     g_hash_table_remove (hash_table, &array[i]);
305
306   g_hash_table_destroy (hash_table);
307
308   g_print ("ok\n");
309
310
311   g_print ("checking string chunks...");
312
313   string_chunk = g_string_chunk_new (1024);
314
315   for (i = 0; i < 100000; i ++)
316     {
317       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
318
319       if (strcmp ("hi pete", tmp_string) != 0)
320         g_error ("string chunks are broken.\n");
321     }
322
323   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
324
325   g_assert (tmp_string_2 != tmp_string &&
326             strcmp(tmp_string_2, tmp_string) == 0);
327
328   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
329
330   g_assert (tmp_string_2 == tmp_string);
331
332   g_string_chunk_free (string_chunk);
333
334   g_print ("ok\n");
335
336
337   g_print ("checking arrays...");
338
339   garray = g_array_new (FALSE);
340   for (i = 0; i < 10000; i++)
341     g_array_append_val (garray, gint, i);
342
343   for (i = 0; i < 10000; i++)
344     if (g_array_index (garray, gint, i) != i)
345       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
346
347   g_array_free (garray, TRUE);
348
349   garray = g_array_new (FALSE);
350   for (i = 0; i < 10000; i++)
351     g_array_prepend_val (garray, gint, i);
352
353   for (i = 0; i < 10000; i++)
354     if (g_array_index (garray, gint, i) != (10000 - i - 1))
355       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 10000 - i - 1);
356
357   g_array_free (garray, TRUE);
358
359   g_print ("ok\n");
360
361
362   g_print ("checking strings...");
363
364   string1 = g_string_new ("hi pete!");
365   string2 = g_string_new ("");
366
367   g_assert (strcmp ("hi pete!", string1->str) == 0);
368
369   for (i = 0; i < 10000; i++)
370     g_string_append_c (string1, 'a'+(i%26));
371
372   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
373                     "this pete guy sure is a wuss, like he's the number ",
374                     1,
375                     " wuss.  everyone agrees.\n",
376                     string1->str,
377                     10, 666, 15, 15, 666.666666666, 666.666666666);
378
379   g_print ("ok\n");
380
381   g_print ("checking timers...\n");
382
383   timer = g_timer_new ();
384   g_print ("  spinning for 3 seconds...\n");
385
386   g_timer_start (timer);
387   while (g_timer_elapsed (timer, NULL) < 3)
388     ;
389
390   g_timer_stop (timer);
391   g_timer_destroy (timer);
392
393   g_print ("ok\n");
394
395   g_print ("checking g_strcasecmp...\n");
396
397   /* g_debug (argv[0]); */
398
399
400   return 0;
401 }