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