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 GLIB_COMPILATION
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <errno.h>
34
35 #include "glib.h"
36 #include <glib/gstdio.h>
37
38 #include <stdlib.h>
39
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43
44 #ifdef G_OS_WIN32
45 #include <io.h> /* For read(), write() etc */
46 #endif
47
48
49 #define GLIB_TEST_STRING "el dorado "
50 #define GLIB_TEST_STRING_5 "el do"
51
52
53 /* --- variables --- */
54 static gint test_nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
55 static gint more_nums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
56
57 /* --- functions --- */
58 static gint
my_list_compare_one(gconstpointer a,gconstpointer b)59 my_list_compare_one (gconstpointer a, gconstpointer b)
60 {
61 gint one = *((const gint*)a);
62 gint two = *((const gint*)b);
63 return one-two;
64 }
65
66 static gint
my_list_compare_two(gconstpointer a,gconstpointer b)67 my_list_compare_two (gconstpointer a, gconstpointer b)
68 {
69 gint one = *((const gint*)a);
70 gint two = *((const gint*)b);
71 return two-one;
72 }
73
74 /* static void
75 my_list_print (gpointer a, gpointer b)
76 {
77 gint three = *((gint*)a);
78 g_print("%d", three);
79 }; */
80
81 static void
glist_test(void)82 glist_test (void)
83 {
84 GList *list = NULL;
85 guint i;
86
87 for (i = 0; i < 10; i++)
88 list = g_list_append (list, &test_nums[i]);
89 list = g_list_reverse (list);
90
91 for (i = 0; i < 10; i++)
92 {
93 GList *t = g_list_nth (list, i);
94 if (*((gint*) t->data) != (9 - i))
95 g_error ("Regular insert failed");
96 }
97
98 for (i = 0; i < 10; i++)
99 if (g_list_position (list, g_list_nth (list, i)) != i)
100 g_error ("g_list_position does not seem to be the inverse of g_list_nth\n");
101
102 g_list_free (list);
103 list = NULL;
104
105 for (i = 0; i < 10; i++)
106 list = g_list_insert_sorted (list, &more_nums[i], my_list_compare_one);
107
108 /*
109 g_print("\n");
110 g_list_foreach (list, my_list_print, NULL);
111 */
112
113 for (i = 0; i < 10; i++)
114 {
115 GList *t = g_list_nth (list, i);
116 if (*((gint*) t->data) != i)
117 g_error ("Sorted insert failed");
118 }
119
120 g_list_free (list);
121 list = NULL;
122
123 for (i = 0; i < 10; i++)
124 list = g_list_insert_sorted (list, &more_nums[i], my_list_compare_two);
125
126 /*
127 g_print("\n");
128 g_list_foreach (list, my_list_print, NULL);
129 */
130
131 for (i = 0; i < 10; i++)
132 {
133 GList *t = g_list_nth (list, i);
134 if (*((gint*) t->data) != (9 - i))
135 g_error ("Sorted insert failed");
136 }
137
138 g_list_free (list);
139 list = NULL;
140
141 for (i = 0; i < 10; i++)
142 list = g_list_prepend (list, &more_nums[i]);
143
144 list = g_list_sort (list, my_list_compare_two);
145
146 /*
147 g_print("\n");
148 g_list_foreach (list, my_list_print, NULL);
149 */
150
151 for (i = 0; i < 10; i++)
152 {
153 GList *t = g_list_nth (list, i);
154 if (*((gint*) t->data) != (9 - i))
155 g_error ("Merge sort failed");
156 }
157
158 g_list_free (list);
159 }
160
161 static void
gslist_test(void)162 gslist_test (void)
163 {
164 GSList *slist = NULL;
165 guint i;
166
167 for (i = 0; i < 10; i++)
168 slist = g_slist_append (slist, &test_nums[i]);
169 slist = g_slist_reverse (slist);
170
171 for (i = 0; i < 10; i++)
172 {
173 GSList *st = g_slist_nth (slist, i);
174 if (*((gint*) st->data) != (9 - i))
175 g_error ("failed");
176 }
177
178 g_slist_free (slist);
179 slist = NULL;
180
181 for (i = 0; i < 10; i++)
182 slist = g_slist_insert_sorted (slist, &more_nums[i], my_list_compare_one);
183
184 /*
185 g_print("\n");
186 g_slist_foreach (slist, my_list_print, NULL);
187 */
188
189 for (i = 0; i < 10; i++)
190 {
191 GSList *st = g_slist_nth (slist, i);
192 if (*((gint*) st->data) != i)
193 g_error ("Sorted insert failed");
194 }
195
196 g_slist_free (slist);
197 slist = NULL;
198
199 for (i = 0; i < 10; i++)
200 slist = g_slist_insert_sorted (slist, &more_nums[i], my_list_compare_two);
201
202 /*
203 g_print("\n");
204 g_slist_foreach (slist, my_list_print, NULL);
205 */
206
207 for (i = 0; i < 10; i++)
208 {
209 GSList *st = g_slist_nth (slist, i);
210 if (*((gint*) st->data) != (9 - i))
211 g_error("Sorted insert failed");
212 }
213
214 g_slist_free(slist);
215 slist = NULL;
216
217 for (i = 0; i < 10; i++)
218 slist = g_slist_prepend (slist, &more_nums[i]);
219
220 slist = g_slist_sort (slist, my_list_compare_two);
221
222 /*
223 g_print("\n");
224 g_slist_foreach (slist, my_list_print, NULL);
225 */
226
227 for (i = 0; i < 10; i++)
228 {
229 GSList *st = g_slist_nth (slist, i);
230 if (*((gint*) st->data) != (9 - i))
231 g_error("Sorted insert failed");
232 }
233
234 g_slist_free(slist);
235 }
236
237 static gboolean
node_build_string(GNode * node,gpointer data)238 node_build_string (GNode *node,
239 gpointer data)
240 {
241 gchar **p = data;
242 gchar *string;
243 gchar c[2] = "_";
244
245 c[0] = ((gchar) ((gintptr) (node->data)));
246
247 string = g_strconcat (*p ? *p : "", c, NULL);
248 g_free (*p);
249 *p = string;
250
251 return FALSE;
252 }
253
254 static void
gnode_test(void)255 gnode_test (void)
256 {
257 #define C2P(c) ((gpointer) ((long) (c)))
258 #define P2C(p) ((gchar) ((gintptr) (p)))
259 GNode *root;
260 GNode *node;
261 GNode *node_B;
262 GNode *node_F;
263 GNode *node_G;
264 GNode *node_J;
265 guint i;
266 gchar *tstring, *cstring;
267
268 root = g_node_new (C2P ('A'));
269 g_assert (g_node_depth (root) == 1 && g_node_max_height (root) == 1);
270
271 node_B = g_node_new (C2P ('B'));
272 g_node_append (root, node_B);
273 g_assert (root->children == node_B);
274
275 g_node_append_data (node_B, C2P ('E'));
276 g_node_prepend_data (node_B, C2P ('C'));
277 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
278
279 node_F = g_node_new (C2P ('F'));
280 g_node_append (root, node_F);
281 g_assert (root->children->next == node_F);
282
283 node_G = g_node_new (C2P ('G'));
284 g_node_append (node_F, node_G);
285 node_J = g_node_new (C2P ('J'));
286 g_node_prepend (node_G, node_J);
287 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
288 g_node_insert_data (node_G, 0, C2P ('H'));
289 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
290
291 g_assert (g_node_depth (root) == 1);
292 g_assert (g_node_max_height (root) == 4);
293 g_assert (g_node_depth (node_G->children->next) == 4);
294 g_assert (g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
295 g_assert (g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
296 g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
297 g_assert (g_node_max_height (node_F) == 3);
298 g_assert (g_node_n_children (node_G) == 4);
299 g_assert (g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
300 g_assert (g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
301 g_assert (g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
302
303 for (i = 0; i < g_node_n_children (node_B); i++)
304 {
305 node = g_node_nth_child (node_B, i);
306 g_assert (P2C (node->data) == ('C' + i));
307 }
308
309 for (i = 0; i < g_node_n_children (node_G); i++)
310 g_assert (g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
311
312 /* we have built: A
313 * / \
314 * B F
315 * / | \ \
316 * C D E G
317 * / /\ \
318 * H I J K
319 *
320 * for in-order traversal, 'G' is considered to be the "left"
321 * child of 'F', which will cause 'F' to be the last node visited.
322 */
323
324 tstring = NULL;
325 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
326 g_assert_cmpstr (tstring, ==, "ABCDEFGHIJK");
327 g_free (tstring); tstring = NULL;
328 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
329 g_assert_cmpstr (tstring, ==, "CDEBHIJKGFA");
330 g_free (tstring); tstring = NULL;
331 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
332 g_assert_cmpstr (tstring, ==, "CBDEAHGIJKF");
333 g_free (tstring); tstring = NULL;
334 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
335 g_assert_cmpstr (tstring, ==, "ABFCDEGHIJK");
336 g_free (tstring); tstring = NULL;
337
338 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
339 g_assert_cmpstr (tstring, ==, "CDEHIJK");
340 g_free (tstring); tstring = NULL;
341 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
342 g_assert_cmpstr (tstring, ==, "ABFG");
343 g_free (tstring); tstring = NULL;
344
345 g_node_reverse_children (node_B);
346 g_node_reverse_children (node_G);
347
348 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
349 g_assert_cmpstr (tstring, ==, "ABFEDCGKJIH");
350 g_free (tstring); tstring = NULL;
351
352 cstring = NULL;
353 node = g_node_copy (root);
354 g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
355 g_assert (g_node_max_height (root) == g_node_max_height (node));
356 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
357 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
358 g_assert_cmpstr (tstring, ==, cstring);
359 g_free (tstring); tstring = NULL;
360 g_free (cstring); cstring = NULL;
361 g_node_destroy (node);
362
363 g_node_destroy (root);
364
365 /* allocation tests */
366
367 root = g_node_new (NULL);
368 node = root;
369
370 for (i = 0; i < 2048; i++)
371 {
372 g_node_append (node, g_node_new (NULL));
373 if ((i%5) == 4)
374 node = node->children->next;
375 }
376 g_assert (g_node_max_height (root) > 100);
377 g_assert (g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
378
379 g_node_destroy (root);
380 #undef C2P
381 #undef P2C
382 }
383
384 static gint
my_compare(gconstpointer a,gconstpointer b)385 my_compare (gconstpointer a,
386 gconstpointer b)
387 {
388 const char *cha = a;
389 const char *chb = b;
390
391 return *cha - *chb;
392 }
393
394 static gint
my_traverse(gpointer key,gpointer value,gpointer data)395 my_traverse (gpointer key,
396 gpointer value,
397 gpointer data)
398 {
399 char *ch = key;
400 g_print ("%c ", *ch);
401 return FALSE;
402 }
403
404 static void
binary_tree_test(void)405 binary_tree_test (void)
406 {
407 GTree *tree;
408 char chars[62];
409 guint i, j;
410
411 tree = g_tree_new (my_compare);
412 i = 0;
413 for (j = 0; j < 10; j++, i++)
414 {
415 chars[i] = '0' + j;
416 g_tree_insert (tree, &chars[i], &chars[i]);
417 }
418 for (j = 0; j < 26; j++, i++)
419 {
420 chars[i] = 'A' + j;
421 g_tree_insert (tree, &chars[i], &chars[i]);
422 }
423 for (j = 0; j < 26; j++, i++)
424 {
425 chars[i] = 'a' + j;
426 g_tree_insert (tree, &chars[i], &chars[i]);
427 }
428
429 g_assert_cmpint (g_tree_nnodes (tree), ==, 10 + 26 + 26);
430 g_assert_cmpint (g_tree_height (tree), ==, 6);
431
432 if (g_test_verbose())
433 {
434 g_print ("tree: ");
435 g_tree_foreach (tree, my_traverse, NULL);
436 g_print ("\n");
437 }
438
439 for (i = 0; i < 10; i++)
440 g_tree_remove (tree, &chars[i]);
441
442 g_assert_cmpint (g_tree_nnodes (tree), ==, 26 + 26);
443 g_assert_cmpint (g_tree_height (tree), ==, 6);
444
445 if (g_test_verbose())
446 {
447 g_print ("tree: ");
448 g_tree_foreach (tree, my_traverse, NULL);
449 g_print ("\n");
450 }
451 }
452
453 static gboolean
my_hash_callback_remove(gpointer key,gpointer value,gpointer user_data)454 my_hash_callback_remove (gpointer key,
455 gpointer value,
456 gpointer user_data)
457 {
458 int *d = value;
459
460 if ((*d) % 2)
461 return TRUE;
462
463 return FALSE;
464 }
465
466 static void
my_hash_callback_remove_test(gpointer key,gpointer value,gpointer user_data)467 my_hash_callback_remove_test (gpointer key,
468 gpointer value,
469 gpointer user_data)
470 {
471 int *d = value;
472
473 if ((*d) % 2)
474 g_print ("bad!\n");
475 }
476
477 static void
my_hash_callback(gpointer key,gpointer value,gpointer user_data)478 my_hash_callback (gpointer key,
479 gpointer value,
480 gpointer user_data)
481 {
482 int *d = value;
483 *d = 1;
484 }
485
486 static guint
my_hash(gconstpointer key)487 my_hash (gconstpointer key)
488 {
489 return (guint) *((const gint*) key);
490 }
491
492 static gboolean
my_hash_equal(gconstpointer a,gconstpointer b)493 my_hash_equal (gconstpointer a,
494 gconstpointer b)
495 {
496 return *((const gint*) a) == *((const gint*) b);
497 }
498
499 static gboolean
find_first_that(gpointer key,gpointer value,gpointer user_data)500 find_first_that(gpointer key,
501 gpointer value,
502 gpointer user_data)
503 {
504 gint *v = value;
505 gint *test = user_data;
506 return (*v == *test);
507 }
508
509 static void
test_g_parse_debug_string(void)510 test_g_parse_debug_string (void)
511 {
512 GDebugKey keys[3] = {
513 { "foo", 1 },
514 { "bar", 2 },
515 { "baz", 4 }
516 };
517 guint n_keys = 3;
518 guint result;
519
520 result = g_parse_debug_string ("bar:foo:blubb", keys, n_keys);
521 g_assert (result == 3);
522
523 result = g_parse_debug_string (":baz::_E@~!_::", keys, n_keys);
524 g_assert (result == 4);
525
526 result = g_parse_debug_string ("", keys, n_keys);
527 g_assert (result == 0);
528
529 result = g_parse_debug_string (" : ", keys, n_keys);
530 g_assert (result == 0);
531 }
532
533 static void
log_warning_error_tests(void)534 log_warning_error_tests (void)
535 {
536 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
537 {
538 g_message ("this is a g_message test.");
539 g_message ("non-printable UTF-8: \"\xc3\xa4\xda\x85\"");
540 g_message ("unsafe chars: \"\x10\x11\x12\n\t\x7f\x81\x82\x83\"");
541 exit (0);
542 }
543 g_test_trap_assert_passed();
544 g_test_trap_assert_stderr ("*is a g_message test*");
545 g_test_trap_assert_stderr ("*non-printable UTF-8*");
546 g_test_trap_assert_stderr ("*unsafe chars*");
547 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
548 {
549 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
550 exit (0);
551 }
552 g_test_trap_assert_failed(); /* we have fatal-warnings enabled */
553 g_test_trap_assert_stderr ("*harmless warning*");
554 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
555 {
556 g_print (NULL);
557 exit (0);
558 }
559 g_test_trap_assert_failed(); /* we have fatal-warnings enabled */
560 g_test_trap_assert_stderr ("*g_print*assertion*failed*");
561 g_test_trap_assert_stderr ("*NULL*");
562 }
563
564 static void
timer_tests(void)565 timer_tests (void)
566 {
567 GTimer *timer, *timer2;
568 gdouble elapsed;
569
570 /* basic testing */
571 timer = g_timer_new ();
572 g_timer_start (timer);
573 elapsed = g_timer_elapsed (timer, NULL);
574 g_timer_stop (timer);
575 g_assert_cmpfloat (elapsed, <=, g_timer_elapsed (timer, NULL));
576 g_timer_destroy (timer);
577
578 if (g_test_slow())
579 {
580 if (g_test_verbose())
581 g_print ("checking timers...\n");
582 timer = g_timer_new ();
583 if (g_test_verbose())
584 g_print (" spinning for 3 seconds...\n");
585 g_timer_start (timer);
586 while (g_timer_elapsed (timer, NULL) < 3)
587 ;
588 g_timer_stop (timer);
589 g_timer_destroy (timer);
590 if (g_test_verbose())
591 g_print ("ok\n");
592 }
593
594 if (g_test_slow())
595 {
596 gulong elapsed_usecs;
597 if (g_test_verbose())
598 g_print ("checking g_timer_continue...\n");
599 timer2 = g_timer_new ();
600 if (g_test_verbose())
601 g_print ("\trun for 1 second...\n");
602 timer = g_timer_new();
603 g_usleep (G_USEC_PER_SEC); /* run timer for 1 second */
604 g_timer_stop (timer);
605 if (g_test_verbose())
606 g_print ("\tstop for 1 second...\n");
607 g_usleep (G_USEC_PER_SEC); /* wait for 1 second */
608 if (g_test_verbose())
609 g_print ("\trun for 2 seconds...\n");
610 g_timer_continue (timer);
611 g_usleep (2 * G_USEC_PER_SEC); /* run timer for 2 seconds */
612 g_timer_stop(timer);
613 if (g_test_verbose())
614 g_print ("\tstop for 1.5 seconds...\n");
615 g_usleep ((3 * G_USEC_PER_SEC) / 2); /* wait for 1.5 seconds */
616 if (g_test_verbose())
617 g_print ("\trun for 0.2 seconds...\n");
618 g_timer_continue (timer);
619 g_usleep (G_USEC_PER_SEC / 5); /* run timer for 0.2 seconds */
620 g_timer_stop (timer);
621 if (g_test_verbose())
622 g_print ("\tstop for 4 seconds...\n");
623 g_usleep (4 * G_USEC_PER_SEC); /* wait for 4 seconds */
624 if (g_test_verbose())
625 g_print ("\trun for 5.8 seconds...\n");
626 g_timer_continue (timer);
627 g_usleep ((29 * G_USEC_PER_SEC) / 5); /* run timer for 5.8 seconds */
628 g_timer_stop(timer);
629 elapsed = g_timer_elapsed (timer, &elapsed_usecs);
630 if (g_test_verbose())
631 g_print ("\t=> timer = %.6f = %d.%06ld (should be: 9.000000) (%.6f off)\n", elapsed, (int) elapsed, elapsed_usecs, ABS (elapsed - 9.));
632 g_assert_cmpfloat (elapsed, >, 8.8);
633 g_assert_cmpfloat (elapsed, <, 9.2);
634 if (g_test_verbose())
635 g_print ("g_timer_continue ... ok\n\n");
636 g_timer_stop (timer2);
637 elapsed = g_timer_elapsed (timer2, &elapsed_usecs);
638 if (g_test_verbose())
639 g_print ("\t=> timer2 = %.6f = %d.%06ld (should be: %.6f) (%.6f off)\n\n", elapsed, (int) elapsed, elapsed_usecs, 9.+6.5, ABS (elapsed - (9.+6.5)));
640 g_assert_cmpfloat (elapsed, >, 8.8 + 6.5);
641 g_assert_cmpfloat (elapsed, <, 9.2 + 6.5);
642 if (g_test_verbose())
643 g_print ("timer2 ... ok\n\n");
644 g_timer_destroy (timer);
645 g_timer_destroy (timer2);
646 }
647 }
648
649 static void
type_sizes(void)650 type_sizes (void)
651 {
652 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
653 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
654 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
655 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
656 /* type sizes */
657 g_assert_cmpint (sizeof (gint8), ==, 1);
658 g_assert_cmpint (sizeof (gint16), ==, 2);
659 g_assert_cmpint (sizeof (gint32), ==, 4);
660 g_assert_cmpint (sizeof (gint64), ==, 8);
661 /* endian macros */
662 if (g_test_verbose())
663 g_print ("checking endian macros (host is %s)...\n",
664 G_BYTE_ORDER == G_BIG_ENDIAN ? "big endian" : "little endian");
665 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
666 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
667 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
668 }
669
670 static void
test_info(void)671 test_info (void)
672 {
673 const gchar *un, *rn, *hn;
674 const gchar *tmpdir, *homedir, *userdatadir, *uconfdir, *ucachedir;
675 const gchar *uddesktop, *udddocs, *uddpubshare;
676 gchar **sv, *cwd, *sdatadirs, *sconfdirs, *langnames;
677 if (g_test_verbose())
678 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
679 glib_major_version,
680 glib_minor_version,
681 glib_micro_version,
682 glib_interface_age,
683 glib_binary_age);
684
685 cwd = g_get_current_dir ();
686 un = g_get_user_name();
687 rn = g_get_real_name();
688 hn = g_get_host_name();
689 if (g_test_verbose())
690 {
691 g_print ("cwd: %s\n", cwd);
692 g_print ("user: %s\n", un);
693 g_print ("real: %s\n", rn);
694 g_print ("host: %s\n", hn);
695 }
696 g_free (cwd);
697
698 tmpdir = g_get_tmp_dir();
699 g_assert (tmpdir != NULL);
700 homedir = g_get_home_dir ();
701 g_assert (homedir != NULL);
702 userdatadir = g_get_user_data_dir ();
703 g_assert (userdatadir != NULL);
704 uconfdir = g_get_user_config_dir ();
705 g_assert (uconfdir != NULL);
706 ucachedir = g_get_user_cache_dir ();
707 g_assert (ucachedir != NULL);
708
709 uddesktop = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
710 g_assert (uddesktop != NULL);
711 udddocs = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
712 uddpubshare = g_get_user_special_dir (G_USER_DIRECTORY_PUBLIC_SHARE);
713
714 sv = (gchar **) g_get_system_data_dirs ();
715 sdatadirs = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv);
716 sv = (gchar **) g_get_system_config_dirs ();
717 sconfdirs = g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv);
718 sv = (gchar **) g_get_language_names ();
719 langnames = g_strjoinv (":", sv);
720
721 if (g_test_verbose())
722 {
723 g_print ("tmp-dir: %s\n", tmpdir);
724 g_print ("home: %s\n", homedir);
725 g_print ("user_data: %s\n", userdatadir);
726 g_print ("user_config: %s\n", uconfdir);
727 g_print ("user_cache: %s\n", ucachedir);
728 g_print ("system_data: %s\n", sdatadirs);
729 g_print ("system_config: %s\n", sconfdirs);
730 g_print ("languages: %s\n", langnames);
731 g_print ("user_special[DESKTOP]: %s\n", uddesktop);
732 g_print ("user_special[DOCUMENTS]: %s\n", udddocs);
733 g_print ("user_special[PUBLIC_SHARE]: %s\n", uddpubshare);
734 }
735 g_free (sdatadirs);
736 g_free (sconfdirs);
737 g_free (langnames);
738
739 if (g_test_verbose())
740 {
741 #ifdef G_PLATFORM_WIN32
742 gchar *glib_dll;
743 #endif
744 const gchar *charset;
745 if (g_get_charset ((G_CONST_RETURN char**)&charset))
746 g_print ("current charset is UTF-8: %s\n", charset);
747 else
748 g_print ("current charset is not UTF-8: %s\n", charset);
749
750 #ifdef G_PLATFORM_WIN32
751 #ifdef G_OS_WIN32
752 /* Can't calculate GLib DLL name at runtime. */
753 glib_dll = "libglib-2.0-0.dll";
754 #endif
755 #ifdef G_WITH_CYGWIN
756 glib_dll = "cygglib-2.0-0.dll";
757 #endif
758
759 g_print ("current locale: %s\n", g_win32_getlocale ());
760 g_print ("GLib DLL name tested for: %s\n", glib_dll);
761
762 g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
763 GETTEXT_PACKAGE,
764 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
765 g_print ("Ditto, or from GLib DLL name: %s\n",
766 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
767 g_print ("Ditto, only from GLib DLL name: %s\n",
768 g_win32_get_package_installation_directory (NULL, glib_dll));
769 g_print ("locale subdirectory of GLib installation directory: %s\n",
770 g_win32_get_package_installation_subdirectory (NULL, glib_dll, "lib\\locale"));
771 g_print ("GTK+ 2.0 installation directory, if available: %s\n",
772 g_win32_get_package_installation_directory ("gtk20", NULL));
773
774 g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
775 g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
776
777 g_print ("a Win32 error message: %s\n", g_win32_error_message (2));
778 #endif
779 }
780 }
781
782 static void
test_paths(void)783 test_paths (void)
784 {
785 struct {
786 gchar *filename;
787 gchar *dirname;
788 } dirname_checks[] = {
789 { "/", "/" },
790 { "////", "/" },
791 { ".////", "." },
792 { "../", ".." },
793 { "..////", ".." },
794 { "a/b", "a" },
795 { "a/b/", "a/b" },
796 { "c///", "c" },
797 #ifdef G_OS_WIN32
798 { "\\", "\\" },
799 { ".\\\\\\\\", "." },
800 { "..\\", ".." },
801 { "..\\\\\\\\", ".." },
802 { "a\\b", "a" },
803 { "a\\b/", "a\\b" },
804 { "a/b\\", "a/b" },
805 { "c\\\\/", "c" },
806 { "//\\", "/" },
807 #endif
808 #ifdef G_WITH_CYGWIN
809 { "//server/share///x", "//server/share" },
810 #endif
811 { ".", "." },
812 { "..", "." },
813 { "", "." },
814 };
815 const guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
816 struct {
817 gchar *filename;
818 gchar *without_root;
819 } skip_root_checks[] = {
820 { "/", "" },
821 { "//", "" },
822 { "/foo", "foo" },
823 { "//foo", "foo" },
824 { "a/b", NULL },
825 #ifdef G_OS_WIN32
826 { "\\", "" },
827 { "\\foo", "foo" },
828 { "\\\\server\\foo", "" },
829 { "\\\\server\\foo\\bar", "bar" },
830 { "a\\b", NULL },
831 #endif
832 #ifdef G_WITH_CYGWIN
833 { "//server/share///x", "//x" },
834 #endif
835 { ".", NULL },
836 { "", NULL },
837 };
838 const guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
839 gchar *string;
840 guint i;
841 if (g_test_verbose())
842 g_print ("checking g_path_get_basename()...");
843 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
844 g_assert (strcmp (string, "dir") == 0);
845 g_free (string);
846 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
847 g_assert (strcmp (string, "file") == 0);
848 g_free (string);
849 if (g_test_verbose())
850 g_print ("ok\n");
851
852 #ifdef G_OS_WIN32
853 string = g_path_get_basename ("/foo/dir/");
854 g_assert (strcmp (string, "dir") == 0);
855 g_free (string);
856 string = g_path_get_basename ("/foo/file");
857 g_assert (strcmp (string, "file") == 0);
858 g_free (string);
859 #endif
860
861 if (g_test_verbose())
862 g_print ("checking g_path_get_dirname()...");
863 for (i = 0; i < n_dirname_checks; i++)
864 {
865 gchar *dirname = g_path_get_dirname (dirname_checks[i].filename);
866 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
867 {
868 g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
869 dirname_checks[i].filename,
870 dirname_checks[i].dirname,
871 dirname);
872 }
873 g_free (dirname);
874 }
875 if (g_test_verbose())
876 g_print ("ok\n");
877
878 if (g_test_verbose())
879 g_print ("checking g_path_skip_root()...");
880 for (i = 0; i < n_skip_root_checks; i++)
881 {
882 const gchar *skipped = g_path_skip_root (skip_root_checks[i].filename);
883 if ((skipped && !skip_root_checks[i].without_root) ||
884 (!skipped && skip_root_checks[i].without_root) ||
885 ((skipped && skip_root_checks[i].without_root) &&
886 strcmp (skipped, skip_root_checks[i].without_root)))
887 {
888 g_error ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
889 skip_root_checks[i].filename,
890 (skip_root_checks[i].without_root ?
891 skip_root_checks[i].without_root : "<NULL>"),
892 (skipped ? skipped : "<NULL>"));
893 }
894 }
895 if (g_test_verbose())
896 g_print ("ok\n");
897 }
898
899 static void
test_file_functions(void)900 test_file_functions (void)
901 {
902 const char hello[] = "Hello, World";
903 const int hellolen = sizeof (hello) - 1;
904 GError *error;
905 char template[32];
906 char *name_used, chars[62];
907 gint fd, n;
908
909 strcpy (template, "foobar");
910 fd = g_mkstemp (template);
911 if (g_test_verbose() && fd != -1)
912 g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
913 close (fd);
914 strcpy (template, "fooXXXXXX");
915 fd = g_mkstemp (template);
916 if (fd == -1)
917 g_error ("g_mkstemp didn't work for template %s\n", template);
918 n = write (fd, hello, hellolen);
919 if (n == -1)
920 g_error ("write() failed: %s\n", g_strerror (errno));
921 else if (n != hellolen)
922 g_error ("write() should have written %d bytes, wrote %d\n", hellolen, n);
923
924 lseek (fd, 0, 0);
925 n = read (fd, chars, sizeof (chars));
926 if (n == -1)
927 g_error ("read() failed: %s\n", g_strerror (errno));
928 else if (n != hellolen)
929 g_error ("read() should have read %d bytes, got %d\n", hellolen, n);
930
931 chars[n] = 0;
932 if (strcmp (chars, hello) != 0)
933 g_error ("wrote '%s', but got '%s'\n", hello, chars);
934
935 close (fd);
936 remove (template);
937
938 error = NULL;
939 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
940 fd = g_file_open_tmp (template, &name_used, &error);
941 if (g_test_verbose())
942 {
943 if (fd != -1)
944 g_print ("g_file_open_tmp works even if template contains '%s'\n", G_DIR_SEPARATOR_S);
945 else
946 g_print ("g_file_open_tmp correctly returns error: %s\n", error->message);
947 }
948 close (fd);
949 g_clear_error (&error);
950
951 #ifdef G_OS_WIN32
952 strcpy (template, "zap/barXXXXXX");
953 fd = g_file_open_tmp (template, &name_used, &error);
954 if (g_test_verbose())
955 {
956 if (fd != -1)
957 g_print ("g_file_open_tmp works even if template contains '/'\n");
958 else
959 g_print ("g_file_open_tmp correctly returns error: %s\n", error->message);
960 }
961 close (fd);
962 g_clear_error (&error);
963 #endif
964
965 strcpy (template, "zapXXXXXX");
966 fd = g_file_open_tmp (template, &name_used, &error);
967 if (fd == -1)
968 g_error ("g_file_open_tmp didn't work for template '%s': %s\n", template, error->message);
969 else if (g_test_verbose())
970 g_print ("g_file_open_tmp for template '%s' used name '%s'\n", template, name_used);
971 close (fd);
972 g_clear_error (&error);
973 remove (name_used);
974
975 fd = g_file_open_tmp (NULL, &name_used, &error);
976 if (fd == -1)
977 g_error ("g_file_open_tmp didn't work for a NULL template: %s\n", error->message);
978 close (fd);
979 g_clear_error (&error);
980 remove (name_used);
981 }
982
983 static void
test_arrays(void)984 test_arrays (void)
985 {
986 GByteArray *gbarray;
987 GPtrArray *gparray;
988 GArray *garray;
989 guint i;
990
991 gparray = g_ptr_array_new ();
992 for (i = 0; i < 10000; i++)
993 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
994 for (i = 0; i < 10000; i++)
995 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
996 g_error ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
997 g_ptr_array_free (gparray, TRUE);
998
999 gbarray = g_byte_array_new ();
1000 for (i = 0; i < 10000; i++)
1001 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1002 for (i = 0; i < 10000; i++)
1003 {
1004 g_assert (gbarray->data[4*i] == 'a');
1005 g_assert (gbarray->data[4*i+1] == 'b');
1006 g_assert (gbarray->data[4*i+2] == 'c');
1007 g_assert (gbarray->data[4*i+3] == 'd');
1008 }
1009 g_byte_array_free (gbarray, TRUE);
1010
1011 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1012 for (i = 0; i < 10000; i++)
1013 g_array_append_val (garray, i);
1014 for (i = 0; i < 10000; i++)
1015 if (g_array_index (garray, gint, i) != i)
1016 g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), i);
1017 g_array_free (garray, TRUE);
1018
1019 garray = g_array_new (FALSE, FALSE, sizeof (gint));
1020 for (i = 0; i < 100; i++)
1021 g_array_prepend_val (garray, i);
1022 for (i = 0; i < 100; i++)
1023 if (g_array_index (garray, gint, i) != (100 - i - 1))
1024 g_error ("failure: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
1025 g_array_free (garray, TRUE);
1026 }
1027
1028 static void
hash_table_tests(void)1029 hash_table_tests (void)
1030 {
1031 GHashTable *hash_table;
1032 int array[10000];
1033 gint *pvalue = NULL;
1034 gint value = 120;
1035 guint i;
1036
1037 hash_table = g_hash_table_new (my_hash, my_hash_equal);
1038 for (i = 0; i < 10000; i++)
1039 {
1040 array[i] = i;
1041 g_hash_table_insert (hash_table, &array[i], &array[i]);
1042 }
1043 pvalue = g_hash_table_find (hash_table, find_first_that, &value);
1044 if (*pvalue != value)
1045 g_error ("g_hash_table_find failed");
1046 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
1047 for (i = 0; i < 10000; i++)
1048 if (array[i] == 0)
1049 g_error ("hashtable-test: wrong value: %d\n", i);
1050 for (i = 0; i < 10000; i++)
1051 g_hash_table_remove (hash_table, &array[i]);
1052 for (i = 0; i < 10000; i++)
1053 {
1054 array[i] = i;
1055 g_hash_table_insert (hash_table, &array[i], &array[i]);
1056 }
1057 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
1058 g_hash_table_size (hash_table) != 5000)
1059 g_error ("hashtable removal failed\n");
1060 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
1061 g_hash_table_destroy (hash_table);
1062 }
1063
1064 static void
relation_test(void)1065 relation_test (void)
1066 {
1067 GRelation *relation = g_relation_new (2);
1068 GTuples *tuples;
1069 gint data [1024];
1070 guint i;
1071
1072 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1073 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1074
1075 for (i = 0; i < 1024; i += 1)
1076 data[i] = i;
1077
1078 for (i = 1; i < 1023; i += 1)
1079 {
1080 g_relation_insert (relation, data + i, data + i + 1);
1081 g_relation_insert (relation, data + i, data + i - 1);
1082 }
1083
1084 for (i = 2; i < 1022; i += 1)
1085 {
1086 g_assert (! g_relation_exists (relation, data + i, data + i));
1087 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1088 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1089 }
1090
1091 for (i = 1; i < 1023; i += 1)
1092 {
1093 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1094 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1095 }
1096
1097 for (i = 2; i < 1022; i += 1)
1098 {
1099 g_assert (g_relation_count (relation, data + i, 0) == 2);
1100 g_assert (g_relation_count (relation, data + i, 1) == 2);
1101 }
1102
1103 g_assert (g_relation_count (relation, data, 0) == 0);
1104
1105 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1106 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1107 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1108 g_relation_delete (relation, data + 42, 0);
1109 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1110 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1111 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1112
1113 tuples = g_relation_select (relation, data + 200, 0);
1114
1115 g_assert (tuples->len == 2);
1116
1117 #if 0
1118 for (i = 0; i < tuples->len; i += 1)
1119 {
1120 printf ("%d %d\n",
1121 *(gint*) g_tuples_index (tuples, i, 0),
1122 *(gint*) g_tuples_index (tuples, i, 1));
1123 }
1124 #endif
1125
1126 g_assert (g_relation_exists (relation, data + 300, data + 301));
1127 g_relation_delete (relation, data + 300, 0);
1128 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1129
1130 g_tuples_destroy (tuples);
1131
1132 g_relation_destroy (relation);
1133
1134 relation = NULL;
1135 }
1136
1137 static void
gstring_tests(void)1138 gstring_tests (void)
1139 {
1140 GString *string1, *string2;
1141 guint i;
1142
1143 if (g_test_verbose())
1144 g_print ("test GString basics\n");
1145
1146 string1 = g_string_new ("hi pete!");
1147 string2 = g_string_new ("");
1148
1149 g_assert (strcmp ("hi pete!", string1->str) == 0);
1150
1151 for (i = 0; i < 10000; i++)
1152 g_string_append_c (string1, 'a'+(i%26));
1153
1154 #ifndef G_OS_WIN32
1155 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
1156 the %10000.10000f format... */
1157 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
1158 "this pete guy sure is a wuss, like he's the number ",
1159 1,
1160 " wuss. everyone agrees.\n",
1161 string1->str,
1162 10, 666, 15, 15, 666.666666666, 666.666666666);
1163 #else
1164 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
1165 "this pete guy sure is a wuss, like he's the number ",
1166 1,
1167 " wuss. everyone agrees.\n",
1168 string1->str,
1169 10, 666, 15, 15, 666.666666666, 666.666666666);
1170 #endif
1171
1172 if (g_test_verbose())
1173 g_print ("string2 length = %lu...\n", (gulong)string2->len);
1174 string2->str[70] = '\0';
1175 if (g_test_verbose())
1176 g_print ("first 70 chars:\n%s\n", string2->str);
1177 string2->str[141] = '\0';
1178 if (g_test_verbose())
1179 g_print ("next 70 chars:\n%s\n", string2->str+71);
1180 string2->str[212] = '\0';
1181 if (g_test_verbose())
1182 g_print ("and next 70:\n%s\n", string2->str+142);
1183 if (g_test_verbose())
1184 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
1185
1186 g_string_free (string1, TRUE);
1187 g_string_free (string2, TRUE);
1188
1189 /* append */
1190 string1 = g_string_new ("firsthalf");
1191 g_string_append (string1, "lasthalf");
1192 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1193 g_string_free (string1, TRUE);
1194
1195 /* append_len */
1196 string1 = g_string_new ("firsthalf");
1197 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
1198 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1199 g_string_free (string1, TRUE);
1200
1201 /* prepend */
1202 string1 = g_string_new ("lasthalf");
1203 g_string_prepend (string1, "firsthalf");
1204 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1205 g_string_free (string1, TRUE);
1206
1207 /* prepend_len */
1208 string1 = g_string_new ("lasthalf");
1209 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
1210 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
1211 g_string_free (string1, TRUE);
1212
1213 /* insert */
1214 string1 = g_string_new ("firstlast");
1215 g_string_insert (string1, 5, "middle");
1216 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1217 g_string_free (string1, TRUE);
1218
1219 /* insert with pos == end of the string */
1220 string1 = g_string_new ("firstmiddle");
1221 g_string_insert (string1, strlen ("firstmiddle"), "last");
1222 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1223 g_string_free (string1, TRUE);
1224
1225 /* insert_len */
1226 string1 = g_string_new ("firstlast");
1227 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
1228 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
1229 g_string_free (string1, TRUE);
1230
1231 /* insert_len with magic -1 pos for append */
1232 string1 = g_string_new ("first");
1233 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
1234 g_assert (strcmp (string1->str, "firstlast") == 0);
1235 g_string_free (string1, TRUE);
1236
1237 /* insert_len with magic -1 len for strlen-the-string */
1238 string1 = g_string_new ("first");
1239 g_string_insert_len (string1, 5, "last", -1);
1240 g_assert (strcmp (string1->str, "firstlast") == 0);
1241 g_string_free (string1, TRUE);
1242
1243 /* g_string_equal */
1244 string1 = g_string_new ("test");
1245 string2 = g_string_new ("te");
1246 g_assert (! g_string_equal(string1, string2));
1247 g_string_append (string2, "st");
1248 g_assert (g_string_equal(string1, string2));
1249 g_string_free (string1, TRUE);
1250 g_string_free (string2, TRUE);
1251
1252 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
1253 if (g_test_verbose())
1254 g_print ("test embedded ASCII 0 (NUL) characters in GString\n");
1255 string1 = g_string_new ("fiddle");
1256 string2 = g_string_new ("fiddle");
1257 g_assert (g_string_equal(string1, string2));
1258 g_string_append_c(string1, '\0');
1259 g_assert (! g_string_equal(string1, string2));
1260 g_string_append_c(string2, '\0');
1261 g_assert (g_string_equal(string1, string2));
1262 g_string_append_c(string1, 'x');
1263 g_string_append_c(string2, 'y');
1264 g_assert (! g_string_equal(string1, string2));
1265 g_assert (string1->len == 8);
1266 g_string_append(string1, "yzzy");
1267 g_assert (string1->len == 12);
1268 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
1269 g_string_insert(string1, 1, "QED");
1270 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
1271 g_string_free (string1, TRUE);
1272 g_string_free (string2, TRUE);
1273 }
1274
1275 static void
various_string_tests(void)1276 various_string_tests (void)
1277 {
1278 GStringChunk *string_chunk;
1279 GTimeVal ref_date, date;
1280 gchar *tmp_string = NULL, *tmp_string_2, *string, *date_str;
1281 guint i;
1282
1283 if (g_test_verbose())
1284 g_print ("checking string chunks...");
1285 string_chunk = g_string_chunk_new (1024);
1286 for (i = 0; i < 100000; i ++)
1287 {
1288 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
1289 if (strcmp ("hi pete", tmp_string) != 0)
1290 g_error ("string chunks are broken.\n");
1291 }
1292 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
1293 g_assert (tmp_string_2 != tmp_string && strcmp (tmp_string_2, tmp_string) == 0);
1294 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
1295 g_assert (tmp_string_2 == tmp_string);
1296 g_string_chunk_free (string_chunk);
1297
1298 if (g_test_verbose())
1299 g_print ("test positional printf formats (not supported):");
1300 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
1301 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
1302 if (g_test_verbose())
1303 g_print ("%s%s\n", string, tmp_string);
1304 g_free (tmp_string);
1305 g_free (string);
1306
1307 #define REF_INVALID1 "Wed Dec 19 17:20:20 GMT 2007"
1308 #define REF_INVALID2 "1980-02-22T10:36:00Zulu"
1309 #define REF_SEC_UTC 320063760
1310 #define REF_STR_UTC "1980-02-22T10:36:00Z"
1311 #define REF_STR_CEST "1980-02-22T12:36:00+02:00"
1312 #define REF_STR_EST "19800222T053600-0500"
1313 #define REF_STR_NST "1980-02-22T07:06:00-03:30"
1314 #define REF_USEC_UTC 50000
1315 #define REF_STR_USEC_UTC "1980-02-22T10:36:00.050000Z"
1316 #define REF_STR_USEC_CEST "19800222T123600.050000000+0200"
1317 #define REF_STR_USEC_EST "1980-02-22T05:36:00,05-05:00"
1318 #define REF_STR_USEC_NST "19800222T070600,0500-0330"
1319
1320 if (g_test_verbose())
1321 g_print ("checking g_time_val_from_iso8601...\n");
1322 ref_date.tv_sec = REF_SEC_UTC;
1323 ref_date.tv_usec = 0;
1324 g_assert (g_time_val_from_iso8601 (REF_INVALID1, &date) == FALSE);
1325 g_assert (g_time_val_from_iso8601 (REF_INVALID2, &date) == FALSE);
1326 g_assert (g_time_val_from_iso8601 (REF_STR_UTC, &date) != FALSE);
1327 if (g_test_verbose())
1328 g_print ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1329 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1330 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1331 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1332
1333 g_assert (g_time_val_from_iso8601 (REF_STR_CEST, &date) != FALSE);
1334 if (g_test_verbose())
1335 g_print ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1336 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1337 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1338 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1339
1340 g_assert (g_time_val_from_iso8601 (REF_STR_EST, &date) != FALSE);
1341 if (g_test_verbose())
1342 g_print ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1343 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1344 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1345 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1346
1347 g_assert (g_time_val_from_iso8601 (REF_STR_NST, &date) != FALSE);
1348 if (g_test_verbose())
1349 g_print ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1350 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1351 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1352 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1353
1354 ref_date.tv_usec = REF_USEC_UTC;
1355 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_UTC, &date) != FALSE);
1356 if (g_test_verbose())
1357 g_print ("\t=> UTC stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1358 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1359 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1360 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1361
1362 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_CEST, &date) != FALSE);
1363 if (g_test_verbose())
1364 g_print ("\t=> CEST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1365 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1366 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1367 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1368
1369 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_EST, &date) != FALSE);
1370 if (g_test_verbose())
1371 g_print ("\t=> EST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1372 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1373 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1374 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1375
1376 g_assert (g_time_val_from_iso8601 (REF_STR_USEC_NST, &date) != FALSE);
1377 if (g_test_verbose())
1378 g_print ("\t=> NST stamp = %ld.%06ld (should be: %ld.%06ld) (%ld.%06ld off)\n",
1379 date.tv_sec, date.tv_usec, ref_date.tv_sec, ref_date.tv_usec,
1380 date.tv_sec - ref_date.tv_sec, date.tv_usec - ref_date.tv_usec);
1381 g_assert (date.tv_sec == ref_date.tv_sec && date.tv_usec == ref_date.tv_usec);
1382
1383 if (g_test_verbose())
1384 g_print ("checking g_time_val_to_iso8601...\n");
1385 ref_date.tv_sec = REF_SEC_UTC;
1386 ref_date.tv_usec = 0;
1387 date_str = g_time_val_to_iso8601 (&ref_date);
1388 g_assert (date_str != NULL);
1389 if (g_test_verbose())
1390 g_print ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_UTC);
1391 g_assert (strcmp (date_str, REF_STR_UTC) == 0);
1392 g_free (date_str);
1393
1394 ref_date.tv_usec = REF_USEC_UTC;
1395 date_str = g_time_val_to_iso8601 (&ref_date);
1396 g_assert (date_str != NULL);
1397 if (g_test_verbose())
1398 g_print ("\t=> date string = %s (should be: %s)\n", date_str, REF_STR_USEC_UTC);
1399 g_assert (strcmp (date_str, REF_STR_USEC_UTC) == 0);
1400 g_free (date_str);
1401
1402 if (g_test_verbose())
1403 g_print ("checking g_ascii_strcasecmp...");
1404 g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
1405 g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
1406 g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
1407 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1408 g_assert (g_ascii_strcasecmp ("", "") == 0);
1409 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
1410 g_assert (g_ascii_strcasecmp ("a", "b") < 0);
1411 g_assert (g_ascii_strcasecmp ("a", "B") < 0);
1412 g_assert (g_ascii_strcasecmp ("A", "b") < 0);
1413 g_assert (g_ascii_strcasecmp ("A", "B") < 0);
1414 g_assert (g_ascii_strcasecmp ("b", "a") > 0);
1415 g_assert (g_ascii_strcasecmp ("b", "A") > 0);
1416 g_assert (g_ascii_strcasecmp ("B", "a") > 0);
1417 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1418
1419 if (g_test_verbose())
1420 g_print ("checking g_strdup...\n");
1421 g_assert (g_strdup (NULL) == NULL);
1422 string = g_strdup (GLIB_TEST_STRING);
1423 g_assert (string != NULL);
1424 g_assert (strcmp(string, GLIB_TEST_STRING) == 0);
1425 g_free (string);
1426
1427 if (g_test_verbose())
1428 g_print ("checking g_strconcat...\n");
1429 string = g_strconcat (GLIB_TEST_STRING, NULL);
1430 g_assert (string != NULL);
1431 g_assert (strcmp (string, GLIB_TEST_STRING) == 0);
1432 g_free (string);
1433 string = g_strconcat (GLIB_TEST_STRING, GLIB_TEST_STRING,
1434 GLIB_TEST_STRING, NULL);
1435 g_assert (string != NULL);
1436 g_assert (strcmp (string, GLIB_TEST_STRING GLIB_TEST_STRING
1437 GLIB_TEST_STRING) == 0);
1438 g_free (string);
1439
1440 if (g_test_verbose())
1441 g_print ("checking g_strlcpy/g_strlcat...");
1442 /* The following is a torture test for strlcpy/strlcat, with lots of
1443 * checking; normal users wouldn't use them this way!
1444 */
1445 string = g_malloc (6);
1446 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
1447 *string = 'q';
1448 g_assert (g_strlcpy(string, "" , 5) == 0);
1449 g_assert ( *string == '\0' );
1450 *string = 'q';
1451 g_assert (g_strlcpy(string, "abc" , 5) == 3);
1452 g_assert ( *(string + 3) == '\0' );
1453 g_assert (g_str_equal(string, "abc"));
1454 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1455 g_assert ( *(string + 4) == '\0' );
1456 g_assert ( *(string + 5) == 'Z' );
1457 g_assert (g_str_equal(string, "abcd"));
1458 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1459 g_assert ( *(string + 4) == '\0' );
1460 g_assert ( *(string + 5) == 'Z' );
1461 g_assert (g_str_equal(string, "abcd"));
1462 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1463 g_assert ( *(string + 4) == '\0' );
1464 g_assert ( *(string + 5) == 'Z' );
1465 g_assert (g_str_equal(string, "abcd"));
1466 *string = 'Y';
1467 *(string + 1)= '\0';
1468 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1469 g_assert (*string == 'Y');
1470 *string = '\0';
1471 g_assert (g_strlcat(string, "123" , 5) == 3);
1472 g_assert ( *(string + 3) == '\0' );
1473 g_assert (g_str_equal(string, "123"));
1474 g_assert (g_strlcat(string, "" , 5) == 3);
1475 g_assert ( *(string + 3) == '\0' );
1476 g_assert (g_str_equal(string, "123"));
1477 g_assert (g_strlcat(string, "4", 5) == 4);
1478 g_assert (g_str_equal(string, "1234"));
1479 g_assert (g_strlcat(string, "5", 5) == 5);
1480 g_assert ( *(string + 4) == '\0' );
1481 g_assert (g_str_equal(string, "1234"));
1482 g_assert ( *(string + 5) == 'Z' );
1483 *string = 'Y';
1484 *(string + 1)= '\0';
1485 g_assert (g_strlcat(string, "123" , 0) == 3);
1486 g_assert (*string == 'Y');
1487
1488 /* A few more tests, demonstrating more "normal" use */
1489 g_assert (g_strlcpy(string, "hi", 5) == 2);
1490 g_assert (g_str_equal(string, "hi"));
1491 g_assert (g_strlcat(string, "t", 5) == 3);
1492 g_assert (g_str_equal(string, "hit"));
1493 g_free(string);
1494
1495 if (g_test_verbose())
1496 g_print ("checking g_strdup_printf...\n");
1497 string = g_strdup_printf ("%05d %-5s", 21, "test");
1498 g_assert (string != NULL);
1499 g_assert (strcmp(string, "00021 test ") == 0);
1500 g_free (string);
1501
1502 /* g_debug (argv[0]); */
1503 }
1504
1505 #ifndef G_DISABLE_DEPRECATED
1506 static void
test_mem_chunks(void)1507 test_mem_chunks (void)
1508 {
1509 GMemChunk *mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
1510 gchar *mem[10000];
1511 guint i;
1512 for (i = 0; i < 10000; i++)
1513 {
1514 guint j;
1515 mem[i] = g_chunk_new (gchar, mem_chunk);
1516 for (j = 0; j < 50; j++)
1517 mem[i][j] = i * j;
1518 }
1519 for (i = 0; i < 10000; i++)
1520 g_mem_chunk_free (mem_chunk, mem[i]);
1521 }
1522 #endif
1523
1524 int
main(int argc,char * argv[])1525 main (int argc,
1526 char *argv[])
1527 {
1528 g_test_init (&argc, &argv, NULL);
1529
1530 g_test_add_func ("/testglib/Infos", test_info);
1531 g_test_add_func ("/testglib/Types Sizes", type_sizes);
1532 g_test_add_func ("/testglib/GStrings", gstring_tests);
1533 g_test_add_func ("/testglib/Various Strings", various_string_tests);
1534 g_test_add_func ("/testglib/GList", glist_test);
1535 g_test_add_func ("/testglib/GSList", gslist_test);
1536 g_test_add_func ("/testglib/GNode", gnode_test);
1537 g_test_add_func ("/testglib/GTree", binary_tree_test);
1538 g_test_add_func ("/testglib/Arrays", test_arrays);
1539 g_test_add_func ("/testglib/GHashTable", hash_table_tests);
1540 g_test_add_func ("/testglib/Relation", relation_test);
1541 g_test_add_func ("/testglib/File Paths", test_paths);
1542 g_test_add_func ("/testglib/File Functions", test_file_functions);
1543 g_test_add_func ("/testglib/Parse Debug Strings", test_g_parse_debug_string);
1544 #ifndef G_DISABLE_DEPRECATED
1545 g_test_add_func ("/testglib/GMemChunk (deprecated)", test_mem_chunks);
1546 #endif
1547 g_test_add_func ("/testglib/Warnings & Errors", log_warning_error_tests);
1548 g_test_add_func ("/testglib/Timers (slow)", timer_tests);
1549
1550 return g_test_run();
1551 }
1552