add -DG_DISABLED_DEPRECATED
[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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser 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 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 #undef G_DISABLE_ASSERT
30 #undef G_LOG_DOMAIN
31
32 #ifdef GLIB_COMPILATION
33 #undef GLIB_COMPILATION
34 #endif
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39
40 #include "glib.h"
41
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45
46 #ifdef G_OS_WIN32
47 #include <io.h>                 /* For read(), write() etc */
48 #endif
49
50 static int array[10000];
51 static gboolean failed = FALSE;
52
53 /* We write (m ? m : "") even in the m != NULL case to suppress a warning with GCC-3.1
54  */
55 #define TEST(m,cond)    G_STMT_START { failed = !(cond); \
56 if (failed) \
57   { if (!m) \
58       g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
59     else \
60       g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)(m ? m : "")); \
61   } \
62 else \
63   g_print ("."); fflush (stdout); \
64 } G_STMT_END
65
66 #define C2P(c)          ((gpointer) ((long) (c)))
67 #define P2C(p)          ((gchar) ((long) (p)))
68
69 #define GLIB_TEST_STRING "el dorado "
70 #define GLIB_TEST_STRING_5 "el do"
71
72 static gboolean
73 node_build_string (GNode    *node,
74                    gpointer  data)
75 {
76   gchar **p = data;
77   gchar *string;
78   gchar c[2] = "_";
79
80   c[0] = P2C (node->data);
81
82   string = g_strconcat (*p ? *p : "", c, NULL);
83   g_free (*p);
84   *p = string;
85
86   return FALSE;
87 }
88
89 static void
90 g_node_test (void)
91 {
92   GNode *root;
93   GNode *node;
94   GNode *node_B;
95   GNode *node_F;
96   GNode *node_G;
97   GNode *node_J;
98   guint i;
99   gchar *tstring, *cstring;
100
101   g_print ("checking n-way trees: ");
102   failed = FALSE;
103
104   root = g_node_new (C2P ('A'));
105   TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
106
107   node_B = g_node_new (C2P ('B'));
108   g_node_append (root, node_B);
109   TEST (NULL, root->children == node_B);
110
111   g_node_append_data (node_B, C2P ('E'));
112   g_node_prepend_data (node_B, C2P ('C'));
113   g_node_insert (node_B, 1, g_node_new (C2P ('D')));
114
115   node_F = g_node_new (C2P ('F'));
116   g_node_append (root, node_F);
117   TEST (NULL, root->children->next == node_F);
118
119   node_G = g_node_new (C2P ('G'));
120   g_node_append (node_F, node_G);
121   node_J = g_node_new (C2P ('J'));
122   g_node_prepend (node_G, node_J);
123   g_node_insert (node_G, 42, g_node_new (C2P ('K')));
124   g_node_insert_data (node_G, 0, C2P ('H'));
125   g_node_insert (node_G, 1, g_node_new (C2P ('I')));
126
127   TEST (NULL, g_node_depth (root) == 1);
128   TEST (NULL, g_node_max_height (root) == 4);
129   TEST (NULL, g_node_depth (node_G->children->next) == 4);
130   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
131   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
132   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
133   TEST (NULL, g_node_max_height (node_F) == 3);
134   TEST (NULL, g_node_n_children (node_G) == 4);
135   TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
136   TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
137   TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
138
139   for (i = 0; i < g_node_n_children (node_B); i++)
140     {
141       node = g_node_nth_child (node_B, i);
142       TEST (NULL, P2C (node->data) == ('C' + i));
143     }
144   
145   for (i = 0; i < g_node_n_children (node_G); i++)
146     TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
147
148   /* we have built:                    A
149    *                                 /   \
150    *                               B       F
151    *                             / | \       \
152    *                           C   D   E       G
153    *                                         / /\ \
154    *                                       H  I  J  K
155    *
156    * for in-order traversal, 'G' is considered to be the "left"
157    * child of 'F', which will cause 'F' to be the last node visited.
158    */
159
160   tstring = NULL;
161   g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
162   TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
163   g_free (tstring); tstring = NULL;
164   g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
165   TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
166   g_free (tstring); tstring = NULL;
167   g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
168   TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
169   g_free (tstring); tstring = NULL;
170   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
171   TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
172   g_free (tstring); tstring = NULL;
173   
174   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
175   TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
176   g_free (tstring); tstring = NULL;
177   g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
178   TEST (tstring, strcmp (tstring, "ABFG") == 0);
179   g_free (tstring); tstring = NULL;
180
181   g_node_reverse_children (node_B);
182   g_node_reverse_children (node_G);
183
184   g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
185   TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
186   g_free (tstring); tstring = NULL;
187
188   cstring = NULL;
189   node = g_node_copy (root);
190   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
191   TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
192   g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
193   g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
194   TEST (cstring, strcmp (tstring, cstring) == 0);
195   g_free (tstring); tstring = NULL;
196   g_free (cstring); cstring = NULL;
197   g_node_destroy (node);
198
199   g_node_destroy (root);
200
201   /* allocation tests */
202
203   root = g_node_new (NULL);
204   node = root;
205
206   for (i = 0; i < 2048; i++)
207     {
208       g_node_append (node, g_node_new (NULL));
209       if ((i%5) == 4)
210         node = node->children->next;
211     }
212   TEST (NULL, g_node_max_height (root) > 100);
213   TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
214
215   g_node_destroy (root);
216   
217   if (!failed)
218     g_print ("ok\n");
219 }
220
221 static gboolean
222 my_hash_callback_remove (gpointer key,
223                          gpointer value,
224                          gpointer user_data)
225 {
226   int *d = value;
227
228   if ((*d) % 2)
229     return TRUE;
230
231   return FALSE;
232 }
233
234 static void
235 my_hash_callback_remove_test (gpointer key,
236                               gpointer value,
237                               gpointer user_data)
238 {
239   int *d = value;
240
241   if ((*d) % 2)
242     g_print ("bad!\n");
243 }
244
245 static void
246 my_hash_callback (gpointer key,
247                   gpointer value,
248                   gpointer user_data)
249 {
250   int *d = value;
251   *d = 1;
252 }
253
254 static guint
255 my_hash (gconstpointer key)
256 {
257   return (guint) *((const gint*) key);
258 }
259
260 static gboolean
261 my_hash_equal (gconstpointer a,
262                gconstpointer b)
263 {
264   return *((const gint*) a) == *((const gint*) b);
265 }
266
267 static gint
268 my_list_compare_one (gconstpointer a, gconstpointer b)
269 {
270   gint one = *((const gint*)a);
271   gint two = *((const gint*)b);
272   return one-two;
273 }
274
275 static gint
276 my_list_compare_two (gconstpointer a, gconstpointer b)
277 {
278   gint one = *((const gint*)a);
279   gint two = *((const gint*)b);
280   return two-one;
281 }
282
283 /* static void
284 my_list_print (gpointer a, gpointer b)
285 {
286   gint three = *((gint*)a);
287   g_print("%d", three);
288 }; */
289
290 static gint
291 my_compare (gconstpointer a,
292             gconstpointer b)
293 {
294   const char *cha = a;
295   const char *chb = b;
296
297   return *cha - *chb;
298 }
299
300 static gint
301 my_traverse (gpointer key,
302              gpointer value,
303              gpointer data)
304 {
305   char *ch = key;
306   g_print ("%c ", *ch);
307   return FALSE;
308 }
309
310 int
311 main (int   argc,
312       char *argv[])
313 {
314   GList *list, *t;
315   GSList *slist, *st;
316   GHashTable *hash_table;
317   GMemChunk *mem_chunk;
318   GStringChunk *string_chunk;
319   GTimer *timer;
320   gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
321   gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
322   gchar *string;
323
324   gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
325   gint i, j;
326   GArray *garray;
327   GPtrArray *gparray;
328   GByteArray *gbarray;
329   GString *string1, *string2;
330   GTree *tree;
331   char chars[62];
332   GRelation *relation;
333   GTuples *tuples;
334   gint data [1024];
335   struct {
336     gchar *filename;
337     gchar *dirname;
338   } dirname_checks[] = {
339 #ifndef G_OS_WIN32
340     { "/", "/" },
341     { "////", "/" },
342     { ".////", "." },
343     { "../", ".." },
344     { "..////", ".." },
345     { "a/b", "a" },
346     { "a/b/", "a/b" },
347     { "c///", "c" },
348 #else
349     { "\\", "\\" },
350     { ".\\\\\\\\", "." },
351     { "..\\", ".." },
352     { "..\\\\\\\\", ".." },
353     { "a\\b", "a" },
354     { "a\\b\\", "a\\b" },
355     { "c\\\\\\", "c" },
356 #endif
357 #ifdef G_WITH_CYGWIN
358     { "//server/share///x", "//server/share" },
359 #endif
360     { ".", "." },
361     { "..", "." },
362     { "", "." },
363   };
364   guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
365
366   struct {
367     gchar *filename;
368     gchar *without_root;
369   } skip_root_checks[] = {
370 #ifndef G_OS_WIN32
371     { "/", "" },
372     { "//", "" },
373     { "/foo", "foo" },
374     { "//foo", "foo" },
375     { "a/b", NULL },
376 #else
377     { "\\", "" },
378     { "\\foo", "foo" },
379     { "\\\\server\\foo", "" },
380     { "\\\\server\\foo\\bar", "bar" },
381     { "a\\b", NULL },
382 #endif
383 #ifdef G_WITH_CYGWIN
384     { "//server/share///x", "//x" },
385 #endif
386     { ".", NULL },
387     { "", NULL },
388   };
389   guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
390
391 #ifndef G_DISABLE_ASSERT
392   guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
393   guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
394 #ifdef G_HAVE_GINT64
395   guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
396           gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
397 #endif
398 #endif
399   const char hello[] = "Hello, World";
400   const int hellolen = sizeof (hello) - 1;
401   int fd;
402   char template[32];
403   GError *error;
404   char *name_used;
405 #ifdef G_OS_WIN32
406   gchar *glib_dll = g_strdup_printf ("libglib-%d.%d-%d.dll",
407                                      GLIB_MAJOR_VERSION,
408                                      GLIB_MINOR_VERSION,
409                                      GLIB_MICRO_VERSION - GLIB_BINARY_AGE);
410 #endif
411 #ifdef G_WITH_CYGWIN
412   gchar *glib_dll = g_strdup_printf ("cygglib-%d.%d-%d.dll",
413                                      GLIB_MAJOR_VERSION,
414                                      GLIB_MINOR_VERSION,
415                                      GLIB_MICRO_VERSION - GLIB_BINARY_AGE);
416 #endif
417
418   g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
419            glib_major_version,
420            glib_minor_version,
421            glib_micro_version,
422            glib_interface_age,
423            glib_binary_age);
424
425   string = g_get_current_dir ();
426   g_print ("cwd: %s\n", string);
427   g_free (string);
428   g_print ("user: %s\n", g_get_user_name ());
429   g_print ("real: %s\n", g_get_real_name ());
430   g_print ("home: %s\n", g_get_home_dir ());
431   g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
432
433   /* type sizes */
434   g_print ("checking size of gint8: %d", (int)sizeof (gint8));
435   TEST (NULL, sizeof (gint8) == 1);
436   g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
437   TEST (NULL, sizeof (gint16) == 2);
438   g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
439   TEST (NULL, sizeof (gint32) == 4);
440   g_print ("\nchecking size of gsize: %d", (int)sizeof (gsize));
441 #ifdef  G_HAVE_GINT64
442   g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
443   TEST (NULL, sizeof (gint64) == 8);
444 #endif  /* G_HAVE_GINT64 */
445   g_print ("\n");
446
447   g_print ("checking g_path_get_basename()...");
448   string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
449   g_assert (strcmp (string, "dir") == 0);
450   g_free (string);
451   string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
452   g_assert (strcmp (string, "file") == 0);
453   g_free (string);
454   g_print ("ok\n");
455
456   g_print ("checking g_path_get_dirname()...");
457   for (i = 0; i < n_dirname_checks; i++)
458     {
459       gchar *dirname;
460
461       dirname = g_path_get_dirname (dirname_checks[i].filename);
462       if (strcmp (dirname, dirname_checks[i].dirname) != 0)
463         {
464           g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
465                    dirname_checks[i].filename,
466                    dirname_checks[i].dirname,
467                    dirname);
468           n_dirname_checks = 0;
469         }
470       g_free (dirname);
471     }
472   if (n_dirname_checks)
473     g_print ("ok\n");
474
475   g_print ("checking g_path_skip_root()...");
476   for (i = 0; i < n_skip_root_checks; i++)
477     {
478       const gchar *skipped;
479
480       skipped = g_path_skip_root (skip_root_checks[i].filename);
481       if ((skipped && !skip_root_checks[i].without_root) ||
482           (!skipped && skip_root_checks[i].without_root) ||
483           ((skipped && skip_root_checks[i].without_root) &&
484            strcmp (skipped, skip_root_checks[i].without_root)))
485         {
486           g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
487                    skip_root_checks[i].filename,
488                    (skip_root_checks[i].without_root ?
489                     skip_root_checks[i].without_root : "<NULL>"),
490                    (skipped ? skipped : "<NULL>"));
491           n_skip_root_checks = 0;
492         }
493     }
494   if (n_skip_root_checks)
495     g_print ("ok\n");
496
497   g_print ("checking doubly linked lists...");
498
499   list = NULL;
500   for (i = 0; i < 10; i++)
501     list = g_list_append (list, &nums[i]);
502   list = g_list_reverse (list);
503
504   for (i = 0; i < 10; i++)
505     {
506       t = g_list_nth (list, i);
507       if (*((gint*) t->data) != (9 - i))
508         g_error ("Regular insert failed");
509     }
510
511   for (i = 0; i < 10; i++)
512     if(g_list_position(list, g_list_nth (list, i)) != i)
513       g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
514
515   g_list_free (list);
516   list = NULL;
517
518   for (i = 0; i < 10; i++)
519     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
520
521   /*
522   g_print("\n");
523   g_list_foreach (list, my_list_print, NULL);
524   */
525
526   for (i = 0; i < 10; i++)
527     {
528       t = g_list_nth (list, i);
529       if (*((gint*) t->data) != i)
530          g_error ("Sorted insert failed");
531     }
532
533   g_list_free (list);
534   list = NULL;
535
536   for (i = 0; i < 10; i++)
537     list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
538
539   /*
540   g_print("\n");
541   g_list_foreach (list, my_list_print, NULL);
542   */
543
544   for (i = 0; i < 10; i++)
545     {
546       t = g_list_nth (list, i);
547       if (*((gint*) t->data) != (9 - i))
548          g_error ("Sorted insert failed");
549     }
550
551   g_list_free (list);
552   list = NULL;
553
554   for (i = 0; i < 10; i++)
555     list = g_list_prepend (list, &morenums[i]);
556
557   list = g_list_sort (list, my_list_compare_two);
558
559   /*
560   g_print("\n");
561   g_list_foreach (list, my_list_print, NULL);
562   */
563
564   for (i = 0; i < 10; i++)
565     {
566       t = g_list_nth (list, i);
567       if (*((gint*) t->data) != (9 - i))
568          g_error ("Merge sort failed");
569     }
570
571   g_list_free (list);
572
573   g_print ("ok\n");
574
575
576   g_print ("checking singly linked lists...");
577
578   slist = NULL;
579   for (i = 0; i < 10; i++)
580     slist = g_slist_append (slist, &nums[i]);
581   slist = g_slist_reverse (slist);
582
583   for (i = 0; i < 10; i++)
584     {
585       st = g_slist_nth (slist, i);
586       if (*((gint*) st->data) != (9 - i))
587         g_error ("failed");
588     }
589
590   g_slist_free (slist);
591   slist = NULL;
592
593   for (i = 0; i < 10; i++)
594     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
595
596   /*
597   g_print("\n");
598   g_slist_foreach (slist, my_list_print, NULL);
599   */
600
601   for (i = 0; i < 10; i++)
602     {
603       st = g_slist_nth (slist, i);
604       if (*((gint*) st->data) != i)
605          g_error ("Sorted insert failed");
606     }
607
608   g_slist_free(slist);
609   slist = NULL;
610
611   for (i = 0; i < 10; i++)
612     slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
613
614   /*
615   g_print("\n");
616   g_slist_foreach (slist, my_list_print, NULL);
617   */
618
619   for (i = 0; i < 10; i++)
620     {
621       st = g_slist_nth (slist, i);
622       if (*((gint*) st->data) != (9 - i))
623          g_error("Sorted insert failed");
624     }
625
626   g_slist_free(slist);
627   slist = NULL;
628
629   for (i = 0; i < 10; i++)
630     slist = g_slist_prepend (slist, &morenums[i]);
631
632   slist = g_slist_sort (slist, my_list_compare_two);
633
634   /*
635   g_print("\n");
636   g_slist_foreach (slist, my_list_print, NULL);
637   */
638
639   for (i = 0; i < 10; i++)
640     {
641       st = g_slist_nth (slist, i);
642       if (*((gint*) st->data) != (9 - i))
643          g_error("Sorted insert failed");
644     }
645
646   g_slist_free(slist);
647
648   g_print ("ok\n");
649
650
651   g_print ("checking binary trees...\n");
652
653   tree = g_tree_new (my_compare);
654   i = 0;
655   for (j = 0; j < 10; j++, i++)
656     {
657       chars[i] = '0' + j;
658       g_tree_insert (tree, &chars[i], &chars[i]);
659     }
660   for (j = 0; j < 26; j++, i++)
661     {
662       chars[i] = 'A' + j;
663       g_tree_insert (tree, &chars[i], &chars[i]);
664     }
665   for (j = 0; j < 26; j++, i++)
666     {
667       chars[i] = 'a' + j;
668       g_tree_insert (tree, &chars[i], &chars[i]);
669     }
670
671   g_print ("tree height: %d\n", g_tree_height (tree));
672   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
673
674   g_print ("tree: ");
675   g_tree_foreach (tree, my_traverse, NULL);
676   g_print ("\n");
677
678   for (i = 0; i < 10; i++)
679     g_tree_remove (tree, &chars[i]);
680
681   g_print ("tree height: %d\n", g_tree_height (tree));
682   g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
683
684   g_print ("tree: ");
685   g_tree_foreach (tree, my_traverse, NULL);
686   g_print ("\n");
687
688   g_print ("ok\n");
689
690
691   /* check n-way trees */
692   g_node_test ();
693
694   g_print ("checking mem chunks...");
695
696   mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
697
698   for (i = 0; i < 10000; i++)
699     {
700       mem[i] = g_chunk_new (gchar, mem_chunk);
701
702       for (j = 0; j < 50; j++)
703         mem[i][j] = i * j;
704     }
705
706   for (i = 0; i < 10000; i++)
707     {
708       g_mem_chunk_free (mem_chunk, mem[i]);
709     }
710
711   g_print ("ok\n");
712
713
714   g_print ("checking hash tables...");
715
716   hash_table = g_hash_table_new (my_hash, my_hash_equal);
717   for (i = 0; i < 10000; i++)
718     {
719       array[i] = i;
720       g_hash_table_insert (hash_table, &array[i], &array[i]);
721     }
722   g_hash_table_foreach (hash_table, my_hash_callback, NULL);
723
724   for (i = 0; i < 10000; i++)
725     if (array[i] == 0)
726       g_print ("%d\n", i);
727
728   for (i = 0; i < 10000; i++)
729     g_hash_table_remove (hash_table, &array[i]);
730
731   for (i = 0; i < 10000; i++)
732     {
733       array[i] = i;
734       g_hash_table_insert (hash_table, &array[i], &array[i]);
735     }
736
737   if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
738       g_hash_table_size (hash_table) != 5000)
739     g_print ("bad!\n");
740
741   g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
742
743
744   g_hash_table_destroy (hash_table);
745
746   g_print ("ok\n");
747
748
749   g_print ("checking string chunks...");
750
751   string_chunk = g_string_chunk_new (1024);
752
753   for (i = 0; i < 100000; i ++)
754     {
755       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
756
757       if (strcmp ("hi pete", tmp_string) != 0)
758         g_error ("string chunks are broken.\n");
759     }
760
761   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
762
763   g_assert (tmp_string_2 != tmp_string &&
764             strcmp(tmp_string_2, tmp_string) == 0);
765
766   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
767
768   g_assert (tmp_string_2 == tmp_string);
769
770   g_string_chunk_free (string_chunk);
771
772   g_print ("ok\n");
773
774
775   g_print ("checking arrays...");
776
777   garray = g_array_new (FALSE, FALSE, sizeof (gint));
778   for (i = 0; i < 10000; i++)
779     g_array_append_val (garray, i);
780
781   for (i = 0; i < 10000; i++)
782     if (g_array_index (garray, gint, i) != i)
783       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
784
785   g_array_free (garray, TRUE);
786
787   garray = g_array_new (FALSE, FALSE, sizeof (gint));
788   for (i = 0; i < 100; i++)
789     g_array_prepend_val (garray, i);
790
791   for (i = 0; i < 100; i++)
792     if (g_array_index (garray, gint, i) != (100 - i - 1))
793       g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
794
795   g_array_free (garray, TRUE);
796
797   g_print ("ok\n");
798
799
800   g_print ("checking strings...");
801
802   string1 = g_string_new ("hi pete!");
803   string2 = g_string_new ("");
804
805   g_assert (strcmp ("hi pete!", string1->str) == 0);
806
807   for (i = 0; i < 10000; i++)
808     g_string_append_c (string1, 'a'+(i%26));
809
810 #ifndef G_OS_WIN32
811   /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
812      the %10000.10000f format... */
813   g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
814                    "this pete guy sure is a wuss, like he's the number ",
815                    1,
816                    " wuss.  everyone agrees.\n",
817                    string1->str,
818                    10, 666, 15, 15, 666.666666666, 666.666666666);
819 #else
820   g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
821                    "this pete guy sure is a wuss, like he's the number ",
822                    1,
823                    " wuss.  everyone agrees.\n",
824                    string1->str,
825                    10, 666, 15, 15, 666.666666666, 666.666666666);
826 #endif
827
828   g_print ("string2 length = %lu...\n", (gulong)string2->len);
829   string2->str[70] = '\0';
830   g_print ("first 70 chars:\n%s\n", string2->str);
831   string2->str[141] = '\0';
832   g_print ("next 70 chars:\n%s\n", string2->str+71);
833   string2->str[212] = '\0';
834   g_print ("and next 70:\n%s\n", string2->str+142);
835   g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
836
837   g_string_free (string1, TRUE);
838   g_string_free (string2, TRUE);
839
840   /* append */
841   string1 = g_string_new ("firsthalf");
842   g_string_append (string1, "lasthalf");
843   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
844   g_string_free (string1, TRUE);
845
846   /* append_len */
847
848   string1 = g_string_new ("firsthalf");
849   g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
850   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
851   g_string_free (string1, TRUE);  
852   
853   /* prepend */
854   string1 = g_string_new ("lasthalf");
855   g_string_prepend (string1, "firsthalf");
856   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
857   g_string_free (string1, TRUE);
858
859   /* prepend_len */
860   string1 = g_string_new ("lasthalf");
861   g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
862   g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
863   g_string_free (string1, TRUE);
864   
865   /* insert */
866   string1 = g_string_new ("firstlast");
867   g_string_insert (string1, 5, "middle");
868   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
869   g_string_free (string1, TRUE);
870
871   /* insert with pos == end of the string */
872   string1 = g_string_new ("firstmiddle");
873   g_string_insert (string1, strlen ("firstmiddle"), "last");
874   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
875   g_string_free (string1, TRUE);
876   
877   /* insert_len */
878
879   string1 = g_string_new ("firstlast");
880   g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
881   g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
882   g_string_free (string1, TRUE);
883
884   /* insert_len with magic -1 pos for append */
885   string1 = g_string_new ("first");
886   g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
887   g_assert (strcmp (string1->str, "firstlast") == 0);
888   g_string_free (string1, TRUE);
889   
890   /* insert_len with magic -1 len for strlen-the-string */
891   string1 = g_string_new ("first");
892   g_string_insert_len (string1, 5, "last", -1);
893   g_assert (strcmp (string1->str, "firstlast") == 0);
894   g_string_free (string1, TRUE);
895   
896   g_print ("ok\n");
897
898   /* g_string_equal */
899   string1 = g_string_new ("test");
900   string2 = g_string_new ("te");
901   g_assert (! g_string_equal(string1, string2));
902   g_string_append (string2, "st");
903   g_assert (g_string_equal(string1, string2));
904   g_string_free (string1, TRUE);
905   g_string_free (string2, TRUE);
906   
907   /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
908   string1 = g_string_new ("fiddle");
909   string2 = g_string_new ("fiddle");
910   g_assert (g_string_equal(string1, string2));
911   g_string_append_c(string1, '\0');
912   g_assert (! g_string_equal(string1, string2));
913   g_string_append_c(string2, '\0');
914   g_assert (g_string_equal(string1, string2));
915   g_string_append_c(string1, 'x');
916   g_string_append_c(string2, 'y');
917   g_assert (! g_string_equal(string1, string2));
918   g_assert (string1->len == 8);
919   g_string_append(string1, "yzzy");
920   g_assert (string1->len == 12);
921   g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
922   g_string_insert(string1, 1, "QED");
923   g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
924   g_string_free (string1, TRUE);
925   g_string_free (string2, TRUE);
926   
927   g_print ("test positional printf formats (not supported): ");
928   string = g_strdup_printf ("%.*s%s", 5, "a", "b");
929   tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
930   g_print ("%s%s\n", string, tmp_string);
931   g_free (tmp_string);
932   g_free (string);
933
934   g_print ("checking timers...\n");
935   
936   timer = g_timer_new ();
937   g_print ("  spinning for 3 seconds...\n");
938
939   g_timer_start (timer);
940   while (g_timer_elapsed (timer, NULL) < 3)
941     ;
942
943   g_timer_stop (timer);
944   g_timer_destroy (timer);
945
946   g_print ("ok\n");
947
948   g_print ("checking g_ascii_strcasecmp...");
949   g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
950   g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
951   g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
952   g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
953   g_assert (g_ascii_strcasecmp ("", "") == 0);
954   g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
955   g_assert (g_ascii_strcasecmp ("a", "b") < 0);
956   g_assert (g_ascii_strcasecmp ("a", "B") < 0);
957   g_assert (g_ascii_strcasecmp ("A", "b") < 0);
958   g_assert (g_ascii_strcasecmp ("A", "B") < 0);
959   g_assert (g_ascii_strcasecmp ("b", "a") > 0);
960   g_assert (g_ascii_strcasecmp ("b", "A") > 0);
961   g_assert (g_ascii_strcasecmp ("B", "a") > 0);
962   g_assert (g_ascii_strcasecmp ("B", "A") > 0);
963
964   g_print ("ok\n");
965
966   g_print ("checking g_strdup...");
967   g_assert(g_strdup(NULL) == NULL);
968   string = g_strdup(GLIB_TEST_STRING);
969   g_assert(string != NULL);
970   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
971   g_free(string);
972
973   g_print ("ok\n");
974
975   g_print ("checking g_strconcat...");
976   string = g_strconcat(GLIB_TEST_STRING, NULL);
977   g_assert(string != NULL);
978   g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
979   g_free(string);
980   string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING, 
981                        GLIB_TEST_STRING, NULL);
982   g_assert(string != NULL);
983   g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
984                           GLIB_TEST_STRING) == 0);
985   g_free(string);
986   g_print ("ok\n");
987   
988
989   g_print("checking g_strlcpy/g_strlcat...");
990   /* The following is a torture test for strlcpy/strlcat, with lots of
991    * checking; normal users wouldn't use them this way!
992    */
993   string = g_malloc (6);
994   *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
995   *string = 'q';
996   g_assert (g_strlcpy(string, "" , 5) == 0);
997   g_assert ( *string == '\0' );
998   *string = 'q';
999   g_assert (g_strlcpy(string, "abc" , 5) == 3);
1000   g_assert ( *(string + 3) == '\0' );
1001   g_assert (g_str_equal(string, "abc"));
1002   g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1003   g_assert ( *(string + 4) == '\0' );
1004   g_assert ( *(string + 5) == 'Z' );
1005   g_assert (g_str_equal(string, "abcd"));
1006   g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1007   g_assert ( *(string + 4) == '\0' );
1008   g_assert ( *(string + 5) == 'Z' );
1009   g_assert (g_str_equal(string, "abcd"));
1010   g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1011   g_assert ( *(string + 4) == '\0' );
1012   g_assert ( *(string + 5) == 'Z' );
1013   g_assert (g_str_equal(string, "abcd"));
1014   *string = 'Y';
1015   *(string + 1)= '\0';
1016   g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1017   g_assert (*string == 'Y');
1018   *string = '\0';
1019   g_assert (g_strlcat(string, "123" , 5) == 3);
1020   g_assert ( *(string + 3) == '\0' );
1021   g_assert (g_str_equal(string, "123"));
1022   g_assert (g_strlcat(string, "" , 5) == 3);
1023   g_assert ( *(string + 3) == '\0' );
1024   g_assert (g_str_equal(string, "123"));
1025   g_assert (g_strlcat(string, "4", 5) == 4);
1026   g_assert (g_str_equal(string, "1234"));
1027   g_assert (g_strlcat(string, "5", 5) == 5);
1028   g_assert ( *(string + 4) == '\0' );
1029   g_assert (g_str_equal(string, "1234"));
1030   g_assert ( *(string + 5) == 'Z' );
1031   *string = 'Y';
1032   *(string + 1)= '\0';
1033   g_assert (g_strlcat(string, "123" , 0) == 3);
1034   g_assert (*string == 'Y');
1035   
1036   /* A few more tests, demonstrating more "normal" use  */
1037   g_assert (g_strlcpy(string, "hi", 5) == 2);
1038   g_assert (g_str_equal(string, "hi"));
1039   g_assert (g_strlcat(string, "t", 5) == 3);
1040   g_assert (g_str_equal(string, "hit"));
1041   g_free(string);
1042
1043   g_print ("ok\n");
1044   
1045   
1046   g_print ("checking g_strdup_printf...");
1047   string = g_strdup_printf ("%05d %-5s", 21, "test");
1048   g_assert (string != NULL);
1049   g_assert (strcmp(string, "00021 test ") == 0);
1050   g_free (string);
1051
1052   g_print ("ok\n");
1053
1054   /* g_debug (argv[0]); */
1055
1056   /* Relation tests */
1057
1058   g_print ("checking relations...");
1059
1060   relation = g_relation_new (2);
1061
1062   g_relation_index (relation, 0, g_int_hash, g_int_equal);
1063   g_relation_index (relation, 1, g_int_hash, g_int_equal);
1064
1065   for (i = 0; i < 1024; i += 1)
1066     data[i] = i;
1067
1068   for (i = 1; i < 1023; i += 1)
1069     {
1070       g_relation_insert (relation, data + i, data + i + 1);
1071       g_relation_insert (relation, data + i, data + i - 1);
1072     }
1073
1074   for (i = 2; i < 1022; i += 1)
1075     {
1076       g_assert (! g_relation_exists (relation, data + i, data + i));
1077       g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1078       g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1079     }
1080
1081   for (i = 1; i < 1023; i += 1)
1082     {
1083       g_assert (g_relation_exists (relation, data + i, data + i + 1));
1084       g_assert (g_relation_exists (relation, data + i, data + i - 1));
1085     }
1086
1087   for (i = 2; i < 1022; i += 1)
1088     {
1089       g_assert (g_relation_count (relation, data + i, 0) == 2);
1090       g_assert (g_relation_count (relation, data + i, 1) == 2);
1091     }
1092
1093   g_assert (g_relation_count (relation, data, 0) == 0);
1094
1095   g_assert (g_relation_count (relation, data + 42, 0) == 2);
1096   g_assert (g_relation_count (relation, data + 43, 1) == 2);
1097   g_assert (g_relation_count (relation, data + 41, 1) == 2);
1098   g_relation_delete (relation, data + 42, 0);
1099   g_assert (g_relation_count (relation, data + 42, 0) == 0);
1100   g_assert (g_relation_count (relation, data + 43, 1) == 1);
1101   g_assert (g_relation_count (relation, data + 41, 1) == 1);
1102
1103   tuples = g_relation_select (relation, data + 200, 0);
1104
1105   g_assert (tuples->len == 2);
1106
1107 #if 0
1108   for (i = 0; i < tuples->len; i += 1)
1109     {
1110       printf ("%d %d\n",
1111               *(gint*) g_tuples_index (tuples, i, 0),
1112               *(gint*) g_tuples_index (tuples, i, 1));
1113     }
1114 #endif
1115
1116   g_assert (g_relation_exists (relation, data + 300, data + 301));
1117   g_relation_delete (relation, data + 300, 0);
1118   g_assert (!g_relation_exists (relation, data + 300, data + 301));
1119
1120   g_tuples_destroy (tuples);
1121
1122   g_relation_destroy (relation);
1123
1124   relation = NULL;
1125
1126   g_print ("ok\n");
1127
1128   g_print ("checking pointer arrays...");
1129
1130   gparray = g_ptr_array_new ();
1131   for (i = 0; i < 10000; i++)
1132     g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1133
1134   for (i = 0; i < 10000; i++)
1135     if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1136       g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1137
1138   g_ptr_array_free (gparray, TRUE);
1139
1140   g_print ("ok\n");
1141
1142
1143   g_print ("checking byte arrays...");
1144
1145   gbarray = g_byte_array_new ();
1146   for (i = 0; i < 10000; i++)
1147     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1148
1149   for (i = 0; i < 10000; i++)
1150     {
1151       g_assert (gbarray->data[4*i] == 'a');
1152       g_assert (gbarray->data[4*i+1] == 'b');
1153       g_assert (gbarray->data[4*i+2] == 'c');
1154       g_assert (gbarray->data[4*i+3] == 'd');
1155     }
1156
1157   g_byte_array_free (gbarray, TRUE);
1158   g_print ("ok\n");
1159
1160   g_printerr ("g_log tests:");
1161   g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1162   g_message ("the next warning is a test:");
1163   string = NULL;
1164   g_print (string);
1165
1166   g_print ("checking endian macros (host is ");
1167 #if G_BYTE_ORDER == G_BIG_ENDIAN
1168   g_print ("big endian)...");
1169 #else
1170   g_print ("little endian)...");
1171 #endif
1172   g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);  
1173   g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);  
1174 #ifdef G_HAVE_GINT64
1175   g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);  
1176 #endif
1177
1178   g_print ("ok\n");
1179
1180 #ifdef G_PLATFORM_WIN32
1181   g_print ("current locale: %s\n", g_win32_getlocale ());
1182   g_print ("GLib DLL name tested for: %s\n", glib_dll);
1183
1184   g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
1185            GETTEXT_PACKAGE,
1186            g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
1187   g_print ("Ditto, or from GLib DLL name: %s\n",
1188            g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
1189   g_print ("Ditto, only from GLib DLL name: %s\n",
1190            g_win32_get_package_installation_directory (NULL, glib_dll));
1191   g_print ("locale subdirectory of GLib installation directory: %s\n",
1192            g_win32_get_package_installation_subdirectory (NULL, glib_dll, "lib\\locale"));
1193   g_print ("GTK+ 2.0 installation directory, if available: %s\n",
1194            g_win32_get_package_installation_directory ("gtk20", NULL));
1195
1196   g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
1197   g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
1198
1199 #endif
1200
1201   g_print ("checking file functions...\n");
1202
1203   strcpy (template, "foobar");
1204   fd = g_mkstemp (template);
1205   if (fd != -1)
1206     g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
1207   close (fd);
1208   strcpy (template, "fooXXXXXX");
1209   fd = g_mkstemp (template);
1210   if (fd == -1)
1211     g_print ("g_mkstemp didn't work for template %s\n", template);
1212   i = write (fd, hello, hellolen);
1213   if (i == -1)
1214     g_print ("write() failed: %s\n", g_strerror (errno));
1215   else if (i != hellolen)
1216     g_print ("write() should have written %d bytes, wrote %d\n", hellolen, i);
1217
1218   lseek (fd, 0, 0);
1219   i = read (fd, chars, sizeof (chars));
1220   if (i == -1)
1221     g_print ("read() failed: %s\n", g_strerror (errno));
1222   else if (i != hellolen)
1223     g_print ("read() should have read %d bytes, got %d\n", hellolen, i);
1224
1225   chars[i] = 0;
1226   if (strcmp (chars, hello) != 0)
1227     g_print ("wrote '%s', but got '%s'\n", hello, chars);
1228
1229   close (fd);
1230   remove (template);
1231
1232   error = NULL;
1233   strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1234   fd = g_file_open_tmp (template, &name_used, &error);
1235   if (fd != -1)
1236     g_print ("g_file_open_tmp works even if template contains '%s'\n",
1237              G_DIR_SEPARATOR_S);
1238   else
1239     g_print ("g_file_open_tmp correctly returns error: %s\n",
1240              error->message);
1241   close (fd);
1242   g_clear_error (&error);
1243
1244   strcpy (template, "zapXXXXXX");
1245   fd = g_file_open_tmp (template, &name_used, &error);
1246   if (fd == -1)
1247     g_print ("g_file_open_tmp didn't work for template '%s': %s\n",
1248              template, error->message);
1249   else
1250     g_print ("g_file_open_tmp for template '%s' used name '%s'\n",
1251              template, name_used);
1252   close (fd);
1253   g_clear_error (&error);
1254   remove (name_used);
1255
1256   fd = g_file_open_tmp (NULL, &name_used, &error);
1257   if (fd == -1)
1258     g_print ("g_file_open_tmp didn't work for a NULL template: %s\n",
1259              error->message);
1260   else
1261     g_print ("g_file_open_tmp for NULL template used name '%s'\n",
1262              name_used);
1263   close (fd);
1264   g_clear_error (&error);
1265   remove (name_used);
1266   
1267   return 0;
1268 }
1269