[Changes from josh to sync with his glib stuff -Yosh]
[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
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   GPtrArray *gparray;
108   GByteArray *gbarray;
109   GString *string1, *string2;
110   GTree *tree;
111   char chars[62];
112   GRelation *relation;
113   GTuples *tuples;
114   gint data [1024];
115
116   g_print ("checking size of gint8...%ld (should be 1)\n", (glong)sizeof (gint8));
117   g_print ("checking size of gint16...%ld (should be 2)\n", (glong)sizeof (gint16));
118   g_print ("checking size of gint32...%ld (should be 4)\n", (glong)sizeof (gint32));
119
120   g_print ("checking doubly linked lists...");
121
122   list = NULL;
123   for (i = 0; i < 10; i++)
124     list = g_list_append (list, &nums[i]);
125   list = g_list_reverse (list);
126
127   for (i = 0; i < 10; i++)
128     {
129       t = g_list_nth (list, i);
130       if (*((gint*) t->data) != (9 - i))
131         g_error ("Regular insert failed");
132     }
133
134   for (i = 0; i < 10; i++)
135     if(g_list_position(list, g_list_nth (list, i)) != i)
136       g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
137
138   g_list_free (list);
139   list = NULL;
140   
141   for (i = 0; i < 10; i++)
142     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
143
144   /*
145   g_print("\n");
146   g_list_foreach (list, my_list_print, NULL);
147   */
148
149   for (i = 0; i < 10; i++)
150     {
151       t = g_list_nth (list, i);
152       if (*((gint*) t->data) != i)
153          g_error ("Sorted insert failed");
154     }
155     
156   g_list_free (list);
157   list = NULL;
158   
159   for (i = 0; i < 10; i++)
160     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
161
162   /*
163   g_print("\n");
164   g_list_foreach (list, my_list_print, NULL);
165   */
166
167   for (i = 0; i < 10; i++)
168     {
169       t = g_list_nth (list, i);
170       if (*((gint*) t->data) != (9 - i))
171          g_error ("Sorted insert failed");
172     }
173     
174   g_list_free (list);
175
176   g_print ("ok\n");
177
178
179   g_print ("checking singly linked lists...");
180
181   slist = NULL;
182   for (i = 0; i < 10; i++)
183     slist = g_slist_append (slist, &nums[i]);
184   slist = g_slist_reverse (slist);
185
186   for (i = 0; i < 10; i++)
187     {
188       st = g_slist_nth (slist, i);
189       if (*((gint*) st->data) != (9 - i))
190         g_error ("failed");
191     }
192
193   g_slist_free (slist);
194   slist = NULL;
195
196   for (i = 0; i < 10; i++)
197     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
198
199   /*
200   g_print("\n");
201   g_slist_foreach (slist, my_list_print, NULL);
202   */
203
204   for (i = 0; i < 10; i++)
205     {
206       st = g_slist_nth (slist, i);
207       if (*((gint*) st->data) != i)
208          g_error ("Sorted insert failed");
209     }
210      
211   g_slist_free(slist);
212   slist = NULL;
213    
214   for (i = 0; i < 10; i++)
215     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
216
217   /*
218   g_print("\n");
219   g_slist_foreach (slist, my_list_print, NULL);
220   */
221
222   for (i = 0; i < 10; i++)
223     {
224       st = g_slist_nth (slist, i);
225       if (*((gint*) st->data) != (9 - i))
226          g_error("Sorted insert failed");
227     }
228     
229   g_slist_free(slist);
230
231   g_print ("ok\n");
232
233
234   g_print ("checking trees...\n");
235
236   tree = g_tree_new (my_compare);
237   i = 0;
238   for (j = 0; j < 10; j++, i++)
239     {
240       chars[i] = '0' + 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   for (j = 0; j < 26; j++, i++)
249     {
250       chars[i] = 'a' + j;
251       g_tree_insert (tree, &chars[i], &chars[i]);
252     }
253
254   g_print ("tree height: %d\n", g_tree_height (tree));
255   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
256
257   g_print ("tree: ");
258   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
259   g_print ("\n");
260
261   for (i = 0; i < 10; i++)
262     g_tree_remove (tree, &chars[i]);
263
264   g_print ("tree height: %d\n", g_tree_height (tree));
265   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
266
267   g_print ("tree: ");
268   g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
269   g_print ("\n");
270
271   g_print ("ok\n");
272
273
274   g_print ("checking mem chunks...");
275
276   mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
277
278   for (i = 0; i < 10000; i++)
279     {
280       mem[i] = g_chunk_new (gchar, mem_chunk);
281
282       for (j = 0; j < 50; j++)
283         mem[i][j] = i * j;
284     }
285
286   for (i = 0; i < 10000; i++)
287     {
288       g_mem_chunk_free (mem_chunk, mem[i]);
289     }
290
291   g_print ("ok\n");
292
293
294   g_print ("checking hash tables...");
295
296   hash_table = g_hash_table_new (my_hash, my_hash_compare);
297   for (i = 0; i < 10000; i++)
298     {
299       array[i] = i;
300       g_hash_table_insert (hash_table, &array[i], &array[i]);
301     }
302   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
303
304   for (i = 0; i < 10000; i++)
305     if (array[i] == 0)
306       g_print ("%d\n", i);
307
308   for (i = 0; i < 10000; i++)
309     g_hash_table_remove (hash_table, &array[i]);
310
311   g_hash_table_destroy (hash_table);
312
313   g_print ("ok\n");
314
315
316   g_print ("checking string chunks...");
317
318   string_chunk = g_string_chunk_new (1024);
319
320   for (i = 0; i < 100000; i ++)
321     {
322       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
323
324       if (strcmp ("hi pete", tmp_string) != 0)
325         g_error ("string chunks are broken.\n");
326     }
327
328   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
329
330   g_assert (tmp_string_2 != tmp_string &&
331             strcmp(tmp_string_2, tmp_string) == 0);
332
333   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
334
335   g_assert (tmp_string_2 == tmp_string);
336
337   g_string_chunk_free (string_chunk);
338
339   g_print ("ok\n");
340
341
342   g_print ("checking arrays...");
343
344   garray = g_array_new (FALSE);
345   for (i = 0; i < 10000; i++)
346     g_array_append_val (garray, gint, i);
347
348   for (i = 0; i < 10000; i++)
349     if (g_array_index (garray, gint, i) != i)
350       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
351
352   g_array_free (garray, TRUE);
353
354   garray = g_array_new (FALSE);
355   for (i = 0; i < 10000; i++)
356     g_array_prepend_val (garray, gint, i);
357
358   for (i = 0; i < 10000; i++)
359     if (g_array_index (garray, gint, i) != (10000 - i - 1))
360       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 10000 - i - 1);
361
362   g_array_free (garray, TRUE);
363
364   g_print ("ok\n");
365
366
367   g_print ("checking strings...");
368
369   string1 = g_string_new ("hi pete!");
370   string2 = g_string_new ("");
371
372   g_assert (strcmp ("hi pete!", string1->str) == 0);
373
374   for (i = 0; i < 10000; i++)
375     g_string_append_c (string1, 'a'+(i%26));
376
377   g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
378                     "this pete guy sure is a wuss, like he's the number ",
379                     1,
380                     " wuss.  everyone agrees.\n",
381                     string1->str,
382                     10, 666, 15, 15, 666.666666666, 666.666666666);
383
384   g_print ("ok\n");
385
386   g_print ("checking timers...\n");
387
388   timer = g_timer_new ();
389   g_print ("  spinning for 3 seconds...\n");
390
391   g_timer_start (timer);
392   while (g_timer_elapsed (timer, NULL) < 3)
393     ;
394
395   g_timer_stop (timer);
396   g_timer_destroy (timer);
397
398   g_print ("ok\n");
399
400   g_print ("checking g_strcasecmp...\n");
401
402   /* g_debug (argv[0]); */
403
404   /* Relation tests */
405
406   g_print ("checking relations...");
407
408   relation = g_relation_new (2);
409
410   g_relation_index (relation, 0, g_int_hash, g_int_equal);
411   g_relation_index (relation, 1, g_int_hash, g_int_equal);
412
413   for (i = 0; i < 1024; i += 1)
414     data[i] = i;
415
416   for (i = 1; i < 1023; i += 1)
417     {
418       g_relation_insert (relation, data + i, data + i + 1);
419       g_relation_insert (relation, data + i, data + i - 1);
420     }
421
422   for (i = 2; i < 1022; i += 1)
423     {
424       g_assert (! g_relation_exists (relation, data + i, data + i));
425       g_assert (! g_relation_exists (relation, data + i, data + i + 2));
426       g_assert (! g_relation_exists (relation, data + i, data + i - 2));
427     }
428
429   for (i = 1; i < 1023; i += 1)
430     {
431       g_assert (g_relation_exists (relation, data + i, data + i + 1));
432       g_assert (g_relation_exists (relation, data + i, data + i - 1));
433     }
434
435   for (i = 2; i < 1022; i += 1)
436     {
437       g_assert (g_relation_count (relation, data + i, 0) == 2);
438       g_assert (g_relation_count (relation, data + i, 1) == 2);
439     }
440
441   g_assert (g_relation_count (relation, data, 0) == 0);
442
443   g_assert (g_relation_count (relation, data + 42, 0) == 2);
444   g_assert (g_relation_count (relation, data + 43, 1) == 2);
445   g_assert (g_relation_count (relation, data + 41, 1) == 2);
446   g_relation_delete (relation, data + 42, 0);
447   g_assert (g_relation_count (relation, data + 42, 0) == 0);
448   g_assert (g_relation_count (relation, data + 43, 1) == 1);
449   g_assert (g_relation_count (relation, data + 41, 1) == 1);
450
451   tuples = g_relation_select (relation, data + 200, 0);
452
453   g_assert (tuples->len == 2);
454
455 #if 0
456   for (i = 0; i < tuples->len; i += 1)
457     {
458       printf ("%d %d\n",
459               *(gint*) g_tuples_index (tuples, i, 0),
460               *(gint*) g_tuples_index (tuples, i, 1));
461     }
462 #endif
463
464   g_assert (g_relation_exists (relation, data + 300, data + 301));
465   g_relation_delete (relation, data + 300, 0);
466   g_assert (!g_relation_exists (relation, data + 300, data + 301));
467
468   g_tuples_destroy (tuples);
469
470   g_relation_destroy (relation);
471
472   relation = NULL;
473
474   g_print ("ok\n");
475
476   g_print ("checking pointer arrays...");
477
478   gparray = g_ptr_array_new ();
479   for (i = 0; i < 10000; i++)
480     g_ptr_array_add (gparray, (void*)i);
481
482   for (i = 0; i < 10000; i++)
483     if (g_ptr_array_index (gparray, i) != (void*)i)
484       g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), (void*)i);
485
486   g_ptr_array_free (gparray, TRUE);
487
488   g_print ("ok\n");
489
490
491   g_print ("checking byte arrays...");
492
493   gbarray = g_byte_array_new ();
494   for (i = 0; i < 10000; i++)
495     g_byte_array_append (gbarray, "abcd", 4);
496
497   for (i = 0; i < 10000; i++)
498     {
499       g_assert (gbarray->data[4*i] == 'a');
500       g_assert (gbarray->data[4*i+1] == 'b');
501       g_assert (gbarray->data[4*i+2] == 'c');
502       g_assert (gbarray->data[4*i+3] == 'd');
503     }
504
505   g_byte_array_free (gbarray, TRUE);
506
507   g_print ("ok\n");
508
509   return 0;
510 }