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