Added the missing POSIX_NO_YIELD and POSIX_NO_PRIORITIES warning messages.
[glib.git] / testglib.c
blob940851bfeb302fe0197ed777c1d49df40944688a
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
21 * Modified by the GLib Team and others 1997-1999. 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/.
27 #undef G_LOG_DOMAIN
29 #include <stdio.h>
30 #include <string.h>
31 #include "glib.h"
33 int array[10000];
34 gboolean failed = FALSE;
36 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
37 if (failed) \
38 { if (!m) \
39 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
40 else \
41 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
42 } \
43 else \
44 g_print ("."); fflush (stdout); \
45 } G_STMT_END
47 #define C2P(c) ((gpointer) ((long) (c)))
48 #define P2C(p) ((gchar) ((long) (p)))
50 #define GLIB_TEST_STRING "el dorado "
51 #define GLIB_TEST_STRING_5 "el do"
53 static gboolean
54 node_build_string (GNode *node,
55 gpointer data)
57 gchar **p = data;
58 gchar *string;
59 gchar c[2] = "_";
61 c[0] = P2C (node->data);
63 string = g_strconcat (*p ? *p : "", c, NULL);
64 g_free (*p);
65 *p = string;
67 return FALSE;
70 static void
71 g_node_test (void)
73 GNode *root;
74 GNode *node;
75 GNode *node_B;
76 GNode *node_F;
77 GNode *node_G;
78 GNode *node_J;
79 guint i;
80 gchar *tstring, *cstring;
82 g_print ("checking n-way trees: ");
83 failed = FALSE;
85 root = g_node_new (C2P ('A'));
86 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
88 node_B = g_node_new (C2P ('B'));
89 g_node_append (root, node_B);
90 TEST (NULL, root->children == node_B);
92 g_node_append_data (node_B, C2P ('E'));
93 g_node_prepend_data (node_B, C2P ('C'));
94 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
96 node_F = g_node_new (C2P ('F'));
97 g_node_append (root, node_F);
98 TEST (NULL, root->children->next == node_F);
100 node_G = g_node_new (C2P ('G'));
101 g_node_append (node_F, node_G);
102 node_J = g_node_new (C2P ('J'));
103 g_node_prepend (node_G, node_J);
104 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
105 g_node_insert_data (node_G, 0, C2P ('H'));
106 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
108 TEST (NULL, g_node_depth (root) == 1);
109 TEST (NULL, g_node_max_height (root) == 4);
110 TEST (NULL, g_node_depth (node_G->children->next) == 4);
111 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
112 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
113 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
114 TEST (NULL, g_node_max_height (node_F) == 3);
115 TEST (NULL, g_node_n_children (node_G) == 4);
116 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
117 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
118 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
120 for (i = 0; i < g_node_n_children (node_B); i++)
122 node = g_node_nth_child (node_B, i);
123 TEST (NULL, P2C (node->data) == ('C' + i));
126 for (i = 0; i < g_node_n_children (node_G); i++)
127 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
129 /* we have built: A
130 * / \
131 * B F
132 * / | \ \
133 * C D E G
134 * / /\ \
135 * H I J K
137 * for in-order traversal, 'G' is considered to be the "left"
138 * child of 'F', which will cause 'F' to be the last node visited.
141 tstring = NULL;
142 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
143 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
144 g_free (tstring); tstring = NULL;
145 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
146 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
147 g_free (tstring); tstring = NULL;
148 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
149 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
150 g_free (tstring); tstring = NULL;
151 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
152 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
153 g_free (tstring); tstring = NULL;
155 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
156 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
157 g_free (tstring); tstring = NULL;
158 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
159 TEST (tstring, strcmp (tstring, "ABFG") == 0);
160 g_free (tstring); tstring = NULL;
162 g_node_reverse_children (node_B);
163 g_node_reverse_children (node_G);
165 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
166 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
167 g_free (tstring); tstring = NULL;
169 cstring = NULL;
170 node = g_node_copy (root);
171 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
172 TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
173 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
174 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
175 TEST (cstring, strcmp (tstring, cstring) == 0);
176 g_free (tstring); tstring = NULL;
177 g_free (cstring); cstring = NULL;
178 g_node_destroy (node);
180 g_node_destroy (root);
182 /* allocation tests */
184 root = g_node_new (NULL);
185 node = root;
187 for (i = 0; i < 2048; i++)
189 g_node_append (node, g_node_new (NULL));
190 if ((i%5) == 4)
191 node = node->children->next;
193 TEST (NULL, g_node_max_height (root) > 100);
194 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
196 g_node_destroy (root);
198 if (!failed)
199 g_print ("ok\n");
202 static gboolean
203 my_hash_callback_remove (gpointer key,
204 gpointer value,
205 gpointer user_data)
207 int *d = value;
209 if ((*d) % 2)
210 return TRUE;
212 return FALSE;
215 static void
216 my_hash_callback_remove_test (gpointer key,
217 gpointer value,
218 gpointer user_data)
220 int *d = value;
222 if ((*d) % 2)
223 g_print ("bad!\n");
226 static void
227 my_hash_callback (gpointer key,
228 gpointer value,
229 gpointer user_data)
231 int *d = value;
232 *d = 1;
235 static guint
236 my_hash (gconstpointer key)
238 return (guint) *((const gint*) key);
241 static gint
242 my_hash_compare (gconstpointer a,
243 gconstpointer b)
245 return *((const gint*) a) == *((const gint*) b);
248 static gint
249 my_list_compare_one (gconstpointer a, gconstpointer b)
251 gint one = *((const gint*)a);
252 gint two = *((const gint*)b);
253 return one-two;
256 static gint
257 my_list_compare_two (gconstpointer a, gconstpointer b)
259 gint one = *((const gint*)a);
260 gint two = *((const gint*)b);
261 return two-one;
264 /* static void
265 my_list_print (gpointer a, gpointer b)
267 gint three = *((gint*)a);
268 g_print("%d", three);
269 }; */
271 static gint
272 my_compare (gconstpointer a,
273 gconstpointer b)
275 const char *cha = a;
276 const char *chb = b;
278 return *cha - *chb;
281 static gint
282 my_traverse (gpointer key,
283 gpointer value,
284 gpointer data)
286 char *ch = key;
287 g_print ("%c ", *ch);
288 return FALSE;
292 main (int argc,
293 char *argv[])
295 GList *list, *t;
296 GSList *slist, *st;
297 GHashTable *hash_table;
298 GMemChunk *mem_chunk;
299 GStringChunk *string_chunk;
300 GTimer *timer;
301 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
302 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
303 gchar *string;
305 gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
306 gint i, j;
307 GArray *garray;
308 GPtrArray *gparray;
309 GByteArray *gbarray;
310 GString *string1, *string2;
311 GTree *tree;
312 char chars[62];
313 GRelation *relation;
314 GTuples *tuples;
315 gint data [1024];
316 struct {
317 gchar *filename;
318 gchar *dirname;
319 } dirname_checks[] = {
320 #ifndef G_OS_WIN32
321 { "/", "/" },
322 { "////", "/" },
323 { ".////", "." },
324 { ".", "." },
325 { "..", "." },
326 { "../", ".." },
327 { "..////", ".." },
328 { "", "." },
329 { "a/b", "a" },
330 { "a/b/", "a/b" },
331 { "c///", "c" },
332 #else
333 { "\\", "\\" },
334 { ".\\\\\\\\", "." },
335 { ".", "." },
336 { "..", "." },
337 { "..\\", ".." },
338 { "..\\\\\\\\", ".." },
339 { "", "." },
340 { "a\\b", "a" },
341 { "a\\b\\", "a\\b" },
342 { "c\\\\\\", "c" },
343 #endif
345 guint n_dirname_checks = sizeof (dirname_checks) / sizeof (dirname_checks[0]);
346 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
347 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
348 #ifdef G_HAVE_GINT64
349 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
350 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
351 #endif
353 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
354 glib_major_version,
355 glib_minor_version,
356 glib_micro_version,
357 glib_interface_age,
358 glib_binary_age);
360 string = g_get_current_dir ();
361 g_print ("cwd: %s\n", string);
362 g_free (string);
363 g_print ("user: %s\n", g_get_user_name ());
364 g_print ("real: %s\n", g_get_real_name ());
365 g_print ("home: %s\n", g_get_home_dir ());
366 g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
368 /* type sizes */
369 g_print ("checking size of gint8: %d", (int)sizeof (gint8));
370 TEST (NULL, sizeof (gint8) == 1);
371 g_print ("\nchecking size of gint16: %d", (int)sizeof (gint16));
372 TEST (NULL, sizeof (gint16) == 2);
373 g_print ("\nchecking size of gint32: %d", (int)sizeof (gint32));
374 TEST (NULL, sizeof (gint32) == 4);
375 #ifdef G_HAVE_GINT64
376 g_print ("\nchecking size of gint64: %d", (int)sizeof (gint64));
377 TEST (NULL, sizeof (gint64) == 8);
378 #endif /* G_HAVE_GINT64 */
379 g_print ("\n");
381 g_print ("checking g_dirname()...");
382 for (i = 0; i < n_dirname_checks; i++)
384 gchar *dirname;
386 dirname = g_dirname (dirname_checks[i].filename);
387 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
389 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
390 dirname_checks[i].filename,
391 dirname_checks[i].dirname,
392 dirname);
393 n_dirname_checks = 0;
395 g_free (dirname);
397 if (n_dirname_checks)
398 g_print ("ok\n");
400 g_print ("checking doubly linked lists...");
402 list = NULL;
403 for (i = 0; i < 10; i++)
404 list = g_list_append (list, &nums[i]);
405 list = g_list_reverse (list);
407 for (i = 0; i < 10; i++)
409 t = g_list_nth (list, i);
410 if (*((gint*) t->data) != (9 - i))
411 g_error ("Regular insert failed");
414 for (i = 0; i < 10; i++)
415 if(g_list_position(list, g_list_nth (list, i)) != i)
416 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
418 g_list_free (list);
419 list = NULL;
421 for (i = 0; i < 10; i++)
422 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
425 g_print("\n");
426 g_list_foreach (list, my_list_print, NULL);
429 for (i = 0; i < 10; i++)
431 t = g_list_nth (list, i);
432 if (*((gint*) t->data) != i)
433 g_error ("Sorted insert failed");
436 g_list_free (list);
437 list = NULL;
439 for (i = 0; i < 10; i++)
440 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
443 g_print("\n");
444 g_list_foreach (list, my_list_print, NULL);
447 for (i = 0; i < 10; i++)
449 t = g_list_nth (list, i);
450 if (*((gint*) t->data) != (9 - i))
451 g_error ("Sorted insert failed");
454 g_list_free (list);
455 list = NULL;
457 for (i = 0; i < 10; i++)
458 list = g_list_prepend (list, &morenums[i]);
460 list = g_list_sort (list, my_list_compare_two);
463 g_print("\n");
464 g_list_foreach (list, my_list_print, NULL);
467 for (i = 0; i < 10; i++)
469 t = g_list_nth (list, i);
470 if (*((gint*) t->data) != (9 - i))
471 g_error ("Merge sort failed");
474 g_list_free (list);
476 g_print ("ok\n");
479 g_print ("checking singly linked lists...");
481 slist = NULL;
482 for (i = 0; i < 10; i++)
483 slist = g_slist_append (slist, &nums[i]);
484 slist = g_slist_reverse (slist);
486 for (i = 0; i < 10; i++)
488 st = g_slist_nth (slist, i);
489 if (*((gint*) st->data) != (9 - i))
490 g_error ("failed");
493 g_slist_free (slist);
494 slist = NULL;
496 for (i = 0; i < 10; i++)
497 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
500 g_print("\n");
501 g_slist_foreach (slist, my_list_print, NULL);
504 for (i = 0; i < 10; i++)
506 st = g_slist_nth (slist, i);
507 if (*((gint*) st->data) != i)
508 g_error ("Sorted insert failed");
511 g_slist_free(slist);
512 slist = NULL;
514 for (i = 0; i < 10; i++)
515 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
518 g_print("\n");
519 g_slist_foreach (slist, my_list_print, NULL);
522 for (i = 0; i < 10; i++)
524 st = g_slist_nth (slist, i);
525 if (*((gint*) st->data) != (9 - i))
526 g_error("Sorted insert failed");
529 g_slist_free(slist);
530 slist = NULL;
532 for (i = 0; i < 10; i++)
533 slist = g_slist_prepend (slist, &morenums[i]);
535 slist = g_slist_sort (slist, my_list_compare_two);
538 g_print("\n");
539 g_slist_foreach (slist, my_list_print, NULL);
542 for (i = 0; i < 10; i++)
544 st = g_slist_nth (slist, i);
545 if (*((gint*) st->data) != (9 - i))
546 g_error("Sorted insert failed");
549 g_slist_free(slist);
551 g_print ("ok\n");
554 g_print ("checking binary trees...\n");
556 tree = g_tree_new (my_compare);
557 i = 0;
558 for (j = 0; j < 10; j++, i++)
560 chars[i] = '0' + j;
561 g_tree_insert (tree, &chars[i], &chars[i]);
563 for (j = 0; j < 26; j++, i++)
565 chars[i] = 'A' + j;
566 g_tree_insert (tree, &chars[i], &chars[i]);
568 for (j = 0; j < 26; j++, i++)
570 chars[i] = 'a' + j;
571 g_tree_insert (tree, &chars[i], &chars[i]);
574 g_print ("tree height: %d\n", g_tree_height (tree));
575 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
577 g_print ("tree: ");
578 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
579 g_print ("\n");
581 for (i = 0; i < 10; i++)
582 g_tree_remove (tree, &chars[i]);
584 g_print ("tree height: %d\n", g_tree_height (tree));
585 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
587 g_print ("tree: ");
588 g_tree_traverse (tree, my_traverse, G_IN_ORDER, NULL);
589 g_print ("\n");
591 g_print ("ok\n");
594 /* check n-way trees */
595 g_node_test ();
597 g_print ("checking mem chunks...");
599 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
601 for (i = 0; i < 10000; i++)
603 mem[i] = g_chunk_new (gchar, mem_chunk);
605 for (j = 0; j < 50; j++)
606 mem[i][j] = i * j;
609 for (i = 0; i < 10000; i++)
611 g_mem_chunk_free (mem_chunk, mem[i]);
614 g_print ("ok\n");
617 g_print ("checking hash tables...");
619 hash_table = g_hash_table_new (my_hash, my_hash_compare);
620 for (i = 0; i < 10000; i++)
622 array[i] = i;
623 g_hash_table_insert (hash_table, &array[i], &array[i]);
625 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
627 for (i = 0; i < 10000; i++)
628 if (array[i] == 0)
629 g_print ("%d\n", i);
631 for (i = 0; i < 10000; i++)
632 g_hash_table_remove (hash_table, &array[i]);
634 for (i = 0; i < 10000; i++)
636 array[i] = i;
637 g_hash_table_insert (hash_table, &array[i], &array[i]);
640 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
641 g_hash_table_size (hash_table) != 5000)
642 g_print ("bad!\n");
644 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
647 g_hash_table_destroy (hash_table);
649 g_print ("ok\n");
652 g_print ("checking string chunks...");
654 string_chunk = g_string_chunk_new (1024);
656 for (i = 0; i < 100000; i ++)
658 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
660 if (strcmp ("hi pete", tmp_string) != 0)
661 g_error ("string chunks are broken.\n");
664 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
666 g_assert (tmp_string_2 != tmp_string &&
667 strcmp(tmp_string_2, tmp_string) == 0);
669 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
671 g_assert (tmp_string_2 == tmp_string);
673 g_string_chunk_free (string_chunk);
675 g_print ("ok\n");
678 g_print ("checking arrays...");
680 garray = g_array_new (FALSE, FALSE, sizeof (gint));
681 for (i = 0; i < 10000; i++)
682 g_array_append_val (garray, i);
684 for (i = 0; i < 10000; i++)
685 if (g_array_index (garray, gint, i) != i)
686 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
688 g_array_free (garray, TRUE);
690 garray = g_array_new (FALSE, FALSE, sizeof (gint));
691 for (i = 0; i < 100; i++)
692 g_array_prepend_val (garray, i);
694 for (i = 0; i < 100; i++)
695 if (g_array_index (garray, gint, i) != (100 - i - 1))
696 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
698 g_array_free (garray, TRUE);
700 g_print ("ok\n");
703 g_print ("checking strings...");
705 string1 = g_string_new ("hi pete!");
706 string2 = g_string_new ("");
708 g_assert (strcmp ("hi pete!", string1->str) == 0);
710 for (i = 0; i < 10000; i++)
711 g_string_append_c (string1, 'a'+(i%26));
713 #ifndef G_OS_WIN32
714 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
715 the %10000.10000f format... */
716 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
717 "this pete guy sure is a wuss, like he's the number ",
719 " wuss. everyone agrees.\n",
720 string1->str,
721 10, 666, 15, 15, 666.666666666, 666.666666666);
722 #else
723 g_string_sprintf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
724 "this pete guy sure is a wuss, like he's the number ",
726 " wuss. everyone agrees.\n",
727 string1->str,
728 10, 666, 15, 15, 666.666666666, 666.666666666);
729 #endif
731 g_print ("string2 length = %d...\n", string2->len);
732 string2->str[70] = '\0';
733 g_print ("first 70 chars:\n%s\n", string2->str);
734 string2->str[141] = '\0';
735 g_print ("next 70 chars:\n%s\n", string2->str+71);
736 string2->str[212] = '\0';
737 g_print ("and next 70:\n%s\n", string2->str+142);
738 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
740 g_string_free (string1, TRUE);
741 g_string_free (string2, TRUE);
743 /* append */
744 string1 = g_string_new ("firsthalf");
745 g_string_append (string1, "lasthalf");
746 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
747 g_string_free (string1, TRUE);
749 /* append_len */
751 string1 = g_string_new ("firsthalf");
752 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
753 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
754 g_string_free (string1, TRUE);
756 /* prepend */
757 string1 = g_string_new ("lasthalf");
758 g_string_prepend (string1, "firsthalf");
759 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
760 g_string_free (string1, TRUE);
762 /* prepend_len */
763 string1 = g_string_new ("lasthalf");
764 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
765 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
766 g_string_free (string1, TRUE);
768 /* insert */
769 string1 = g_string_new ("firstlast");
770 g_string_insert (string1, 5, "middle");
771 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
772 g_string_free (string1, TRUE);
774 /* insert with pos == end of the string */
775 string1 = g_string_new ("firstmiddle");
776 g_string_insert (string1, strlen ("firstmiddle"), "last");
777 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
778 g_string_free (string1, TRUE);
780 /* insert_len */
782 string1 = g_string_new ("firstlast");
783 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
784 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
785 g_string_free (string1, TRUE);
787 /* insert_len with magic -1 pos for append */
788 string1 = g_string_new ("first");
789 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
790 g_assert (strcmp (string1->str, "firstlast") == 0);
791 g_string_free (string1, TRUE);
793 /* insert_len with magic -1 len for strlen-the-string */
794 string1 = g_string_new ("first");
795 g_string_insert_len (string1, 5, "last", -1);
796 g_assert (strcmp (string1->str, "firstlast") == 0);
797 g_string_free (string1, TRUE);
799 g_print ("ok\n");
801 g_print ("checking timers...\n");
803 timer = g_timer_new ();
804 g_print (" spinning for 3 seconds...\n");
806 g_timer_start (timer);
807 while (g_timer_elapsed (timer, NULL) < 3)
810 g_timer_stop (timer);
811 g_timer_destroy (timer);
813 g_print ("ok\n");
815 g_print ("checking g_strcasecmp...");
816 g_assert (g_strcasecmp ("FroboZZ", "frobozz") == 0);
817 g_assert (g_strcasecmp ("frobozz", "frobozz") == 0);
818 g_assert (g_strcasecmp ("frobozz", "FROBOZZ") == 0);
819 g_assert (g_strcasecmp ("FROBOZZ", "froboz") != 0);
820 g_assert (g_strcasecmp ("", "") == 0);
821 g_assert (g_strcasecmp ("!#%&/()", "!#%&/()") == 0);
822 g_assert (g_strcasecmp ("a", "b") < 0);
823 g_assert (g_strcasecmp ("a", "B") < 0);
824 g_assert (g_strcasecmp ("A", "b") < 0);
825 g_assert (g_strcasecmp ("A", "B") < 0);
826 g_assert (g_strcasecmp ("b", "a") > 0);
827 g_assert (g_strcasecmp ("b", "A") > 0);
828 g_assert (g_strcasecmp ("B", "a") > 0);
829 g_assert (g_strcasecmp ("B", "A") > 0);
831 g_print ("ok\n");
833 g_print ("checking g_strdup...");
834 g_assert(g_strdup(NULL) == NULL);
835 string = g_strdup(GLIB_TEST_STRING);
836 g_assert(string != NULL);
837 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
838 g_free(string);
840 g_print ("ok\n");
842 g_print ("checking g_strconcat...");
843 string = g_strconcat(GLIB_TEST_STRING, NULL);
844 g_assert(string != NULL);
845 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
846 g_free(string);
847 string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
848 GLIB_TEST_STRING, NULL);
849 g_assert(string != NULL);
850 g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
851 GLIB_TEST_STRING) == 0);
852 g_free(string);
854 g_print ("ok\n");
856 g_print ("checking g_strdup_printf...");
857 string = g_strdup_printf ("%05d %-5s", 21, "test");
858 g_assert (string != NULL);
859 g_assert (strcmp(string, "00021 test ") == 0);
860 g_free (string);
862 g_print ("ok\n");
864 /* g_debug (argv[0]); */
866 /* Relation tests */
868 g_print ("checking relations...");
870 relation = g_relation_new (2);
872 g_relation_index (relation, 0, g_int_hash, g_int_equal);
873 g_relation_index (relation, 1, g_int_hash, g_int_equal);
875 for (i = 0; i < 1024; i += 1)
876 data[i] = i;
878 for (i = 1; i < 1023; i += 1)
880 g_relation_insert (relation, data + i, data + i + 1);
881 g_relation_insert (relation, data + i, data + i - 1);
884 for (i = 2; i < 1022; i += 1)
886 g_assert (! g_relation_exists (relation, data + i, data + i));
887 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
888 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
891 for (i = 1; i < 1023; i += 1)
893 g_assert (g_relation_exists (relation, data + i, data + i + 1));
894 g_assert (g_relation_exists (relation, data + i, data + i - 1));
897 for (i = 2; i < 1022; i += 1)
899 g_assert (g_relation_count (relation, data + i, 0) == 2);
900 g_assert (g_relation_count (relation, data + i, 1) == 2);
903 g_assert (g_relation_count (relation, data, 0) == 0);
905 g_assert (g_relation_count (relation, data + 42, 0) == 2);
906 g_assert (g_relation_count (relation, data + 43, 1) == 2);
907 g_assert (g_relation_count (relation, data + 41, 1) == 2);
908 g_relation_delete (relation, data + 42, 0);
909 g_assert (g_relation_count (relation, data + 42, 0) == 0);
910 g_assert (g_relation_count (relation, data + 43, 1) == 1);
911 g_assert (g_relation_count (relation, data + 41, 1) == 1);
913 tuples = g_relation_select (relation, data + 200, 0);
915 g_assert (tuples->len == 2);
917 #if 0
918 for (i = 0; i < tuples->len; i += 1)
920 printf ("%d %d\n",
921 *(gint*) g_tuples_index (tuples, i, 0),
922 *(gint*) g_tuples_index (tuples, i, 1));
924 #endif
926 g_assert (g_relation_exists (relation, data + 300, data + 301));
927 g_relation_delete (relation, data + 300, 0);
928 g_assert (!g_relation_exists (relation, data + 300, data + 301));
930 g_tuples_destroy (tuples);
932 g_relation_destroy (relation);
934 relation = NULL;
936 g_print ("ok\n");
938 g_print ("checking pointer arrays...");
940 gparray = g_ptr_array_new ();
941 for (i = 0; i < 10000; i++)
942 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
944 for (i = 0; i < 10000; i++)
945 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
946 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
948 g_ptr_array_free (gparray, TRUE);
950 g_print ("ok\n");
953 g_print ("checking byte arrays...");
955 gbarray = g_byte_array_new ();
956 for (i = 0; i < 10000; i++)
957 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
959 for (i = 0; i < 10000; i++)
961 g_assert (gbarray->data[4*i] == 'a');
962 g_assert (gbarray->data[4*i+1] == 'b');
963 g_assert (gbarray->data[4*i+2] == 'c');
964 g_assert (gbarray->data[4*i+3] == 'd');
967 g_byte_array_free (gbarray, TRUE);
968 g_print ("ok\n");
970 g_printerr ("g_log tests:");
971 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
972 g_message ("the next warning is a test:");
973 string = NULL;
974 g_print (string);
976 g_print ("checking endian macros (host is ");
977 #if G_BYTE_ORDER == G_BIG_ENDIAN
978 g_print ("big endian)...");
979 #else
980 g_print ("little endian)...");
981 #endif
982 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
983 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
984 #ifdef G_HAVE_GINT64
985 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
986 #endif
988 g_print ("ok\n");
990 #ifdef G_OS_WIN32
991 g_print ("current locale: %s\n", g_win32_getlocale ());
992 #endif
994 return 0;