Don't show the special G_OPTION_REMAINING entry. (#161934, Matthew F.
[glib.git] / tests / testglib.c
blobe8f9f8aa176f416ce9f24404519da1184caaf857
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU 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.
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.
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.
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/.
27 #include "config.h"
29 #undef G_DISABLE_ASSERT
30 #undef G_LOG_DOMAIN
32 #ifdef GLIB_COMPILATION
33 #undef GLIB_COMPILATION
34 #endif
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
40 #include "glib.h"
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
46 #ifdef G_OS_WIN32
47 #include <io.h> /* For read(), write() etc */
48 #endif
50 static int array[10000];
51 static gboolean failed = FALSE;
53 /* We write (m ? m : "") even in the m != NULL case to suppress a warning with GCC-3.1
55 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
56 if (failed) \
57 { if (!m) \
58 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
59 else \
60 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)(m ? m : "")); \
61 } \
62 else \
63 g_print ("."); fflush (stdout); \
64 } G_STMT_END
66 #define C2P(c) ((gpointer) ((long) (c)))
67 #define P2C(p) ((gchar) ((long) (p)))
69 #define GLIB_TEST_STRING "el dorado "
70 #define GLIB_TEST_STRING_5 "el do"
72 static gboolean
73 node_build_string (GNode *node,
74 gpointer data)
76 gchar **p = data;
77 gchar *string;
78 gchar c[2] = "_";
80 c[0] = P2C (node->data);
82 string = g_strconcat (*p ? *p : "", c, NULL);
83 g_free (*p);
84 *p = string;
86 return FALSE;
89 static void
90 g_node_test (void)
92 GNode *root;
93 GNode *node;
94 GNode *node_B;
95 GNode *node_F;
96 GNode *node_G;
97 GNode *node_J;
98 guint i;
99 gchar *tstring, *cstring;
101 g_print ("checking n-way trees: ");
102 failed = FALSE;
104 root = g_node_new (C2P ('A'));
105 TEST (NULL, g_node_depth (root) == 1 && g_node_max_height (root) == 1);
107 node_B = g_node_new (C2P ('B'));
108 g_node_append (root, node_B);
109 TEST (NULL, root->children == node_B);
111 g_node_append_data (node_B, C2P ('E'));
112 g_node_prepend_data (node_B, C2P ('C'));
113 g_node_insert (node_B, 1, g_node_new (C2P ('D')));
115 node_F = g_node_new (C2P ('F'));
116 g_node_append (root, node_F);
117 TEST (NULL, root->children->next == node_F);
119 node_G = g_node_new (C2P ('G'));
120 g_node_append (node_F, node_G);
121 node_J = g_node_new (C2P ('J'));
122 g_node_prepend (node_G, node_J);
123 g_node_insert (node_G, 42, g_node_new (C2P ('K')));
124 g_node_insert_data (node_G, 0, C2P ('H'));
125 g_node_insert (node_G, 1, g_node_new (C2P ('I')));
127 TEST (NULL, g_node_depth (root) == 1);
128 TEST (NULL, g_node_max_height (root) == 4);
129 TEST (NULL, g_node_depth (node_G->children->next) == 4);
130 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_LEAFS) == 7);
131 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_NON_LEAFS) == 4);
132 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 11);
133 TEST (NULL, g_node_max_height (node_F) == 3);
134 TEST (NULL, g_node_n_children (node_G) == 4);
135 TEST (NULL, g_node_find_child (root, G_TRAVERSE_ALL, C2P ('F')) == node_F);
136 TEST (NULL, g_node_find (root, G_LEVEL_ORDER, G_TRAVERSE_NON_LEAFS, C2P ('I')) == NULL);
137 TEST (NULL, g_node_find (root, G_IN_ORDER, G_TRAVERSE_LEAFS, C2P ('J')) == node_J);
139 for (i = 0; i < g_node_n_children (node_B); i++)
141 node = g_node_nth_child (node_B, i);
142 TEST (NULL, P2C (node->data) == ('C' + i));
145 for (i = 0; i < g_node_n_children (node_G); i++)
146 TEST (NULL, g_node_child_position (node_G, g_node_nth_child (node_G, i)) == i);
148 /* we have built: A
149 * / \
150 * B F
151 * / | \ \
152 * C D E G
153 * / /\ \
154 * H I J K
156 * for in-order traversal, 'G' is considered to be the "left"
157 * child of 'F', which will cause 'F' to be the last node visited.
160 tstring = NULL;
161 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
162 TEST (tstring, strcmp (tstring, "ABCDEFGHIJK") == 0);
163 g_free (tstring); tstring = NULL;
164 g_node_traverse (root, G_POST_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
165 TEST (tstring, strcmp (tstring, "CDEBHIJKGFA") == 0);
166 g_free (tstring); tstring = NULL;
167 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
168 TEST (tstring, strcmp (tstring, "CBDEAHGIJKF") == 0);
169 g_free (tstring); tstring = NULL;
170 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
171 TEST (tstring, strcmp (tstring, "ABFCDEGHIJK") == 0);
172 g_free (tstring); tstring = NULL;
174 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_LEAFS, -1, node_build_string, &tstring);
175 TEST (tstring, strcmp (tstring, "CDEHIJK") == 0);
176 g_free (tstring); tstring = NULL;
177 g_node_traverse (root, G_PRE_ORDER, G_TRAVERSE_NON_LEAFS, -1, node_build_string, &tstring);
178 TEST (tstring, strcmp (tstring, "ABFG") == 0);
179 g_free (tstring); tstring = NULL;
181 g_node_reverse_children (node_B);
182 g_node_reverse_children (node_G);
184 g_node_traverse (root, G_LEVEL_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
185 TEST (tstring, strcmp (tstring, "ABFEDCGKJIH") == 0);
186 g_free (tstring); tstring = NULL;
188 cstring = NULL;
189 node = g_node_copy (root);
190 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == g_node_n_nodes (node, G_TRAVERSE_ALL));
191 TEST (NULL, g_node_max_height (root) == g_node_max_height (node));
192 g_node_traverse (root, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &tstring);
193 g_node_traverse (node, G_IN_ORDER, G_TRAVERSE_ALL, -1, node_build_string, &cstring);
194 TEST (cstring, strcmp (tstring, cstring) == 0);
195 g_free (tstring); tstring = NULL;
196 g_free (cstring); cstring = NULL;
197 g_node_destroy (node);
199 g_node_destroy (root);
201 /* allocation tests */
203 root = g_node_new (NULL);
204 node = root;
206 for (i = 0; i < 2048; i++)
208 g_node_append (node, g_node_new (NULL));
209 if ((i%5) == 4)
210 node = node->children->next;
212 TEST (NULL, g_node_max_height (root) > 100);
213 TEST (NULL, g_node_n_nodes (root, G_TRAVERSE_ALL) == 1 + 2048);
215 g_node_destroy (root);
217 if (!failed)
218 g_print ("ok\n");
221 static gboolean
222 my_hash_callback_remove (gpointer key,
223 gpointer value,
224 gpointer user_data)
226 int *d = value;
228 if ((*d) % 2)
229 return TRUE;
231 return FALSE;
234 static void
235 my_hash_callback_remove_test (gpointer key,
236 gpointer value,
237 gpointer user_data)
239 int *d = value;
241 if ((*d) % 2)
242 g_print ("bad!\n");
245 static void
246 my_hash_callback (gpointer key,
247 gpointer value,
248 gpointer user_data)
250 int *d = value;
251 *d = 1;
254 static guint
255 my_hash (gconstpointer key)
257 return (guint) *((const gint*) key);
260 static gboolean
261 my_hash_equal (gconstpointer a,
262 gconstpointer b)
264 return *((const gint*) a) == *((const gint*) b);
267 static gint
268 my_list_compare_one (gconstpointer a, gconstpointer b)
270 gint one = *((const gint*)a);
271 gint two = *((const gint*)b);
272 return one-two;
275 static gint
276 my_list_compare_two (gconstpointer a, gconstpointer b)
278 gint one = *((const gint*)a);
279 gint two = *((const gint*)b);
280 return two-one;
283 /* static void
284 my_list_print (gpointer a, gpointer b)
286 gint three = *((gint*)a);
287 g_print("%d", three);
288 }; */
290 static gint
291 my_compare (gconstpointer a,
292 gconstpointer b)
294 const char *cha = a;
295 const char *chb = b;
297 return *cha - *chb;
300 static gint
301 my_traverse (gpointer key,
302 gpointer value,
303 gpointer data)
305 char *ch = key;
306 g_print ("%c ", *ch);
307 return FALSE;
310 static gboolean
311 find_first_that(gpointer key,
312 gpointer value,
313 gpointer user_data)
315 gint *v = value;
316 gint *test = user_data;
317 return (*v == *test);
322 main (int argc,
323 char *argv[])
325 const gchar *s;
326 gchar **sv;
327 GList *list, *t;
328 GSList *slist, *st;
329 GHashTable *hash_table;
330 GMemChunk *mem_chunk;
331 GStringChunk *string_chunk;
332 GTimer *timer, *timer2;
333 gint nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
334 gint morenums[10] = { 8, 9, 7, 0, 3, 2, 5, 1, 4, 6};
335 gchar *string;
336 gint value = 120;
337 gint *pvalue=NULL;
339 gchar *mem[10000], *tmp_string = NULL, *tmp_string_2;
340 gint i, j;
341 GArray *garray;
342 GPtrArray *gparray;
343 GByteArray *gbarray;
344 GString *string1, *string2;
345 const gchar *charset;
346 GTree *tree;
347 char chars[62];
348 GRelation *relation;
349 GTuples *tuples;
350 gint data [1024];
351 struct {
352 gchar *filename;
353 gchar *dirname;
354 } dirname_checks[] = {
355 { "/", "/" },
356 { "////", "/" },
357 { ".////", "." },
358 { "../", ".." },
359 { "..////", ".." },
360 { "a/b", "a" },
361 { "a/b/", "a/b" },
362 { "c///", "c" },
363 #ifdef G_OS_WIN32
364 { "\\", "\\" },
365 { ".\\\\\\\\", "." },
366 { "..\\", ".." },
367 { "..\\\\\\\\", ".." },
368 { "a\\b", "a" },
369 { "a\\b/", "a\\b" },
370 { "a/b\\", "a/b" },
371 { "c\\\\/", "c" },
372 { "//\\", "/" },
373 #endif
374 #ifdef G_WITH_CYGWIN
375 { "//server/share///x", "//server/share" },
376 #endif
377 { ".", "." },
378 { "..", "." },
379 { "", "." },
381 guint n_dirname_checks = G_N_ELEMENTS (dirname_checks);
383 struct {
384 gchar *filename;
385 gchar *without_root;
386 } skip_root_checks[] = {
387 { "/", "" },
388 { "//", "" },
389 { "/foo", "foo" },
390 { "//foo", "foo" },
391 { "a/b", NULL },
392 #ifdef G_OS_WIN32
393 { "\\", "" },
394 { "\\foo", "foo" },
395 { "\\\\server\\foo", "" },
396 { "\\\\server\\foo\\bar", "bar" },
397 { "a\\b", NULL },
398 #endif
399 #ifdef G_WITH_CYGWIN
400 { "//server/share///x", "//x" },
401 #endif
402 { ".", NULL },
403 { "", NULL },
405 guint n_skip_root_checks = G_N_ELEMENTS (skip_root_checks);
407 #ifndef G_DISABLE_ASSERT
408 guint16 gu16t1 = 0x44afU, gu16t2 = 0xaf44U;
409 guint32 gu32t1 = 0x02a7f109U, gu32t2 = 0x09f1a702U;
410 guint64 gu64t1 = G_GINT64_CONSTANT(0x1d636b02300a7aa7U),
411 gu64t2 = G_GINT64_CONSTANT(0xa77a0a30026b631dU);
412 #endif
413 const char hello[] = "Hello, World";
414 const int hellolen = sizeof (hello) - 1;
415 int fd;
416 char template[32];
417 GError *error;
418 char *name_used;
419 #ifdef G_OS_WIN32
420 /* Can't calculate GLib DLL name at runtime. */
421 gchar *glib_dll = "libglib-2.0-0.dll";
422 #endif
423 #ifdef G_WITH_CYGWIN
424 gchar *glib_dll = "cygglib-2.0-0.dll";
425 #endif
427 g_print ("TestGLib v%u.%u.%u (i:%u b:%u)\n",
428 glib_major_version,
429 glib_minor_version,
430 glib_micro_version,
431 glib_interface_age,
432 glib_binary_age);
434 string = g_get_current_dir ();
435 g_print ("cwd: %s\n", string);
436 g_free (string);
437 g_print ("user: %s\n", g_get_user_name ());
438 g_print ("real: %s\n", g_get_real_name ());
439 s = g_get_home_dir ();
440 g_print ("home: %s\n", s ? s : "NULL!");
441 s = g_get_home_dir ();
442 g_print ("home: %s\n", s ? s : "NULL!");
443 s = g_get_user_data_dir ();
444 g_print ("user_data: %s\n", s ? s : "NULL!");
445 s = g_get_user_config_dir ();
446 g_print ("user_config: %s\n", s ? s : "NULL!");
447 s = g_get_user_cache_dir ();
448 g_print ("user_cache: %s\n", s ? s : "NULL!");
449 sv = (gchar **) g_get_system_data_dirs ();
450 g_print ("system_data: %s\n", sv ? g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv) : "NULL!");
451 sv = (gchar **) g_get_system_config_dirs ();
452 g_print ("system_config: %s\n", s ? g_strjoinv (G_SEARCHPATH_SEPARATOR_S, sv) : "NULL!");
453 g_print ("tmp-dir: %s\n", g_get_tmp_dir ());
454 sv = (gchar **) g_get_language_names ();
455 g_print ("languages: %s\n", s ? g_strjoinv (":", sv) : "NULL!");
457 /* type sizes */
458 g_print ("checking size of gint8: %" G_GSIZE_FORMAT, sizeof (gint8));
459 TEST (NULL, sizeof (gint8) == 1);
460 g_print ("\nchecking size of gint16: %" G_GSIZE_FORMAT, sizeof (gint16));
461 TEST (NULL, sizeof (gint16) == 2);
462 g_print ("\nchecking size of gint32: %" G_GSIZE_FORMAT, sizeof (gint32));
463 TEST (NULL, sizeof (gint32) == 4);
464 g_print ("\nchecking size of gsize: %" G_GSIZE_FORMAT, sizeof (gsize));
465 g_print ("\nchecking size of gint64: %" G_GSIZE_FORMAT, sizeof (gint64));
466 TEST (NULL, sizeof (gint64) == 8);
467 g_print ("\n");
469 g_print ("checking g_path_get_basename()...");
470 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "dir" G_DIR_SEPARATOR_S);
471 g_assert (strcmp (string, "dir") == 0);
472 g_free (string);
473 string = g_path_get_basename (G_DIR_SEPARATOR_S "foo" G_DIR_SEPARATOR_S "file");
474 g_assert (strcmp (string, "file") == 0);
475 g_free (string);
476 g_print ("ok\n");
477 #ifdef G_OS_WIN32
478 string = g_path_get_basename ("/foo/dir/");
479 g_assert (strcmp (string, "dir") == 0);
480 g_free (string);
481 string = g_path_get_basename ("/foo/file");
482 g_assert (strcmp (string, "file") == 0);
483 g_free (string);
484 g_print ("ok\n");
485 #endif
487 g_print ("checking g_path_get_dirname()...");
488 for (i = 0; i < n_dirname_checks; i++)
490 gchar *dirname;
492 dirname = g_path_get_dirname (dirname_checks[i].filename);
493 if (strcmp (dirname, dirname_checks[i].dirname) != 0)
495 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
496 dirname_checks[i].filename,
497 dirname_checks[i].dirname,
498 dirname);
499 n_dirname_checks = 0;
501 g_free (dirname);
503 if (n_dirname_checks)
504 g_print ("ok\n");
506 g_print ("checking g_path_skip_root()...");
507 for (i = 0; i < n_skip_root_checks; i++)
509 const gchar *skipped;
511 skipped = g_path_skip_root (skip_root_checks[i].filename);
512 if ((skipped && !skip_root_checks[i].without_root) ||
513 (!skipped && skip_root_checks[i].without_root) ||
514 ((skipped && skip_root_checks[i].without_root) &&
515 strcmp (skipped, skip_root_checks[i].without_root)))
517 g_print ("\nfailed for \"%s\"==\"%s\" (returned: \"%s\")\n",
518 skip_root_checks[i].filename,
519 (skip_root_checks[i].without_root ?
520 skip_root_checks[i].without_root : "<NULL>"),
521 (skipped ? skipped : "<NULL>"));
522 n_skip_root_checks = 0;
525 if (n_skip_root_checks)
526 g_print ("ok\n");
528 g_print ("checking doubly linked lists...");
530 list = NULL;
531 for (i = 0; i < 10; i++)
532 list = g_list_append (list, &nums[i]);
533 list = g_list_reverse (list);
535 for (i = 0; i < 10; i++)
537 t = g_list_nth (list, i);
538 if (*((gint*) t->data) != (9 - i))
539 g_error ("Regular insert failed");
542 for (i = 0; i < 10; i++)
543 if(g_list_position(list, g_list_nth (list, i)) != i)
544 g_error("g_list_position does not seem to be the inverse of g_list_nth\n");
546 g_list_free (list);
547 list = NULL;
549 for (i = 0; i < 10; i++)
550 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_one);
553 g_print("\n");
554 g_list_foreach (list, my_list_print, NULL);
557 for (i = 0; i < 10; i++)
559 t = g_list_nth (list, i);
560 if (*((gint*) t->data) != i)
561 g_error ("Sorted insert failed");
564 g_list_free (list);
565 list = NULL;
567 for (i = 0; i < 10; i++)
568 list = g_list_insert_sorted (list, &morenums[i], my_list_compare_two);
571 g_print("\n");
572 g_list_foreach (list, my_list_print, NULL);
575 for (i = 0; i < 10; i++)
577 t = g_list_nth (list, i);
578 if (*((gint*) t->data) != (9 - i))
579 g_error ("Sorted insert failed");
582 g_list_free (list);
583 list = NULL;
585 for (i = 0; i < 10; i++)
586 list = g_list_prepend (list, &morenums[i]);
588 list = g_list_sort (list, my_list_compare_two);
591 g_print("\n");
592 g_list_foreach (list, my_list_print, NULL);
595 for (i = 0; i < 10; i++)
597 t = g_list_nth (list, i);
598 if (*((gint*) t->data) != (9 - i))
599 g_error ("Merge sort failed");
602 g_list_free (list);
604 g_print ("ok\n");
607 g_print ("checking singly linked lists...");
609 slist = NULL;
610 for (i = 0; i < 10; i++)
611 slist = g_slist_append (slist, &nums[i]);
612 slist = g_slist_reverse (slist);
614 for (i = 0; i < 10; i++)
616 st = g_slist_nth (slist, i);
617 if (*((gint*) st->data) != (9 - i))
618 g_error ("failed");
621 g_slist_free (slist);
622 slist = NULL;
624 for (i = 0; i < 10; i++)
625 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_one);
628 g_print("\n");
629 g_slist_foreach (slist, my_list_print, NULL);
632 for (i = 0; i < 10; i++)
634 st = g_slist_nth (slist, i);
635 if (*((gint*) st->data) != i)
636 g_error ("Sorted insert failed");
639 g_slist_free(slist);
640 slist = NULL;
642 for (i = 0; i < 10; i++)
643 slist = g_slist_insert_sorted (slist, &morenums[i], my_list_compare_two);
646 g_print("\n");
647 g_slist_foreach (slist, my_list_print, NULL);
650 for (i = 0; i < 10; i++)
652 st = g_slist_nth (slist, i);
653 if (*((gint*) st->data) != (9 - i))
654 g_error("Sorted insert failed");
657 g_slist_free(slist);
658 slist = NULL;
660 for (i = 0; i < 10; i++)
661 slist = g_slist_prepend (slist, &morenums[i]);
663 slist = g_slist_sort (slist, my_list_compare_two);
666 g_print("\n");
667 g_slist_foreach (slist, my_list_print, NULL);
670 for (i = 0; i < 10; i++)
672 st = g_slist_nth (slist, i);
673 if (*((gint*) st->data) != (9 - i))
674 g_error("Sorted insert failed");
677 g_slist_free(slist);
679 g_print ("ok\n");
682 g_print ("checking binary trees...\n");
684 tree = g_tree_new (my_compare);
685 i = 0;
686 for (j = 0; j < 10; j++, i++)
688 chars[i] = '0' + j;
689 g_tree_insert (tree, &chars[i], &chars[i]);
691 for (j = 0; j < 26; j++, i++)
693 chars[i] = 'A' + j;
694 g_tree_insert (tree, &chars[i], &chars[i]);
696 for (j = 0; j < 26; j++, i++)
698 chars[i] = 'a' + j;
699 g_tree_insert (tree, &chars[i], &chars[i]);
702 g_print ("tree height: %d\n", g_tree_height (tree));
703 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
705 g_print ("tree: ");
706 g_tree_foreach (tree, my_traverse, NULL);
707 g_print ("\n");
709 for (i = 0; i < 10; i++)
710 g_tree_remove (tree, &chars[i]);
712 g_print ("tree height: %d\n", g_tree_height (tree));
713 g_print ("tree nnodes: %d\n", g_tree_nnodes (tree));
715 g_print ("tree: ");
716 g_tree_foreach (tree, my_traverse, NULL);
717 g_print ("\n");
719 g_print ("ok\n");
722 /* check n-way trees */
723 g_node_test ();
725 g_print ("checking mem chunks...");
727 mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
729 for (i = 0; i < 10000; i++)
731 mem[i] = g_chunk_new (gchar, mem_chunk);
733 for (j = 0; j < 50; j++)
734 mem[i][j] = i * j;
737 for (i = 0; i < 10000; i++)
739 g_mem_chunk_free (mem_chunk, mem[i]);
742 g_print ("ok\n");
745 g_print ("checking hash tables...");
747 hash_table = g_hash_table_new (my_hash, my_hash_equal);
748 for (i = 0; i < 10000; i++)
750 array[i] = i;
751 g_hash_table_insert (hash_table, &array[i], &array[i]);
753 pvalue = g_hash_table_find (hash_table, find_first_that, &value);
754 if (*pvalue != value)
755 g_print("g_hash_table_find failed");
757 g_hash_table_foreach (hash_table, my_hash_callback, NULL);
759 for (i = 0; i < 10000; i++)
760 if (array[i] == 0)
761 g_print ("%d\n", i);
763 for (i = 0; i < 10000; i++)
764 g_hash_table_remove (hash_table, &array[i]);
766 for (i = 0; i < 10000; i++)
768 array[i] = i;
769 g_hash_table_insert (hash_table, &array[i], &array[i]);
772 if (g_hash_table_foreach_remove (hash_table, my_hash_callback_remove, NULL) != 5000 ||
773 g_hash_table_size (hash_table) != 5000)
774 g_print ("bad!\n");
776 g_hash_table_foreach (hash_table, my_hash_callback_remove_test, NULL);
779 g_hash_table_destroy (hash_table);
781 g_print ("ok\n");
784 g_print ("checking string chunks...");
786 string_chunk = g_string_chunk_new (1024);
788 for (i = 0; i < 100000; i ++)
790 tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
792 if (strcmp ("hi pete", tmp_string) != 0)
793 g_error ("string chunks are broken.\n");
796 tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
798 g_assert (tmp_string_2 != tmp_string &&
799 strcmp(tmp_string_2, tmp_string) == 0);
801 tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
803 g_assert (tmp_string_2 == tmp_string);
805 g_string_chunk_free (string_chunk);
807 g_print ("ok\n");
810 g_print ("checking arrays...");
812 garray = g_array_new (FALSE, FALSE, sizeof (gint));
813 for (i = 0; i < 10000; i++)
814 g_array_append_val (garray, i);
816 for (i = 0; i < 10000; i++)
817 if (g_array_index (garray, gint, i) != i)
818 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), i);
820 g_array_free (garray, TRUE);
822 garray = g_array_new (FALSE, FALSE, sizeof (gint));
823 for (i = 0; i < 100; i++)
824 g_array_prepend_val (garray, i);
826 for (i = 0; i < 100; i++)
827 if (g_array_index (garray, gint, i) != (100 - i - 1))
828 g_print ("uh oh: %d ( %d )\n", g_array_index (garray, gint, i), 100 - i - 1);
830 g_array_free (garray, TRUE);
832 g_print ("ok\n");
835 g_print ("checking strings...");
837 string1 = g_string_new ("hi pete!");
838 string2 = g_string_new ("");
840 g_assert (strcmp ("hi pete!", string1->str) == 0);
842 for (i = 0; i < 10000; i++)
843 g_string_append_c (string1, 'a'+(i%26));
845 #ifndef G_OS_WIN32
846 /* MSVC, mingw32 and LCC use the same run-time C library, which doesn't like
847 the %10000.10000f format... */
848 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%10000.10000f",
849 "this pete guy sure is a wuss, like he's the number ",
851 " wuss. everyone agrees.\n",
852 string1->str,
853 10, 666, 15, 15, 666.666666666, 666.666666666);
854 #else
855 g_string_printf (string2, "%s|%0100d|%s|%s|%0*d|%*.*f|%100.100f",
856 "this pete guy sure is a wuss, like he's the number ",
858 " wuss. everyone agrees.\n",
859 string1->str,
860 10, 666, 15, 15, 666.666666666, 666.666666666);
861 #endif
863 g_print ("string2 length = %lu...\n", (gulong)string2->len);
864 string2->str[70] = '\0';
865 g_print ("first 70 chars:\n%s\n", string2->str);
866 string2->str[141] = '\0';
867 g_print ("next 70 chars:\n%s\n", string2->str+71);
868 string2->str[212] = '\0';
869 g_print ("and next 70:\n%s\n", string2->str+142);
870 g_print ("last 70 chars:\n%s\n", string2->str+string2->len - 70);
872 g_string_free (string1, TRUE);
873 g_string_free (string2, TRUE);
875 /* append */
876 string1 = g_string_new ("firsthalf");
877 g_string_append (string1, "lasthalf");
878 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
879 g_string_free (string1, TRUE);
881 /* append_len */
883 string1 = g_string_new ("firsthalf");
884 g_string_append_len (string1, "lasthalfjunkjunk", strlen ("lasthalf"));
885 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
886 g_string_free (string1, TRUE);
888 /* prepend */
889 string1 = g_string_new ("lasthalf");
890 g_string_prepend (string1, "firsthalf");
891 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
892 g_string_free (string1, TRUE);
894 /* prepend_len */
895 string1 = g_string_new ("lasthalf");
896 g_string_prepend_len (string1, "firsthalfjunkjunk", strlen ("firsthalf"));
897 g_assert (strcmp (string1->str, "firsthalflasthalf") == 0);
898 g_string_free (string1, TRUE);
900 /* insert */
901 string1 = g_string_new ("firstlast");
902 g_string_insert (string1, 5, "middle");
903 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
904 g_string_free (string1, TRUE);
906 /* insert with pos == end of the string */
907 string1 = g_string_new ("firstmiddle");
908 g_string_insert (string1, strlen ("firstmiddle"), "last");
909 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
910 g_string_free (string1, TRUE);
912 /* insert_len */
914 string1 = g_string_new ("firstlast");
915 g_string_insert_len (string1, 5, "middlejunkjunk", strlen ("middle"));
916 g_assert (strcmp (string1->str, "firstmiddlelast") == 0);
917 g_string_free (string1, TRUE);
919 /* insert_len with magic -1 pos for append */
920 string1 = g_string_new ("first");
921 g_string_insert_len (string1, -1, "lastjunkjunk", strlen ("last"));
922 g_assert (strcmp (string1->str, "firstlast") == 0);
923 g_string_free (string1, TRUE);
925 /* insert_len with magic -1 len for strlen-the-string */
926 string1 = g_string_new ("first");
927 g_string_insert_len (string1, 5, "last", -1);
928 g_assert (strcmp (string1->str, "firstlast") == 0);
929 g_string_free (string1, TRUE);
931 g_print ("ok\n");
933 /* g_string_equal */
934 string1 = g_string_new ("test");
935 string2 = g_string_new ("te");
936 g_assert (! g_string_equal(string1, string2));
937 g_string_append (string2, "st");
938 g_assert (g_string_equal(string1, string2));
939 g_string_free (string1, TRUE);
940 g_string_free (string2, TRUE);
942 /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
943 string1 = g_string_new ("fiddle");
944 string2 = g_string_new ("fiddle");
945 g_assert (g_string_equal(string1, string2));
946 g_string_append_c(string1, '\0');
947 g_assert (! g_string_equal(string1, string2));
948 g_string_append_c(string2, '\0');
949 g_assert (g_string_equal(string1, string2));
950 g_string_append_c(string1, 'x');
951 g_string_append_c(string2, 'y');
952 g_assert (! g_string_equal(string1, string2));
953 g_assert (string1->len == 8);
954 g_string_append(string1, "yzzy");
955 g_assert (string1->len == 12);
956 g_assert ( memcmp(string1->str, "fiddle\0xyzzy", 13) == 0);
957 g_string_insert(string1, 1, "QED");
958 g_assert ( memcmp(string1->str, "fQEDiddle\0xyzzy", 16) == 0);
959 g_string_free (string1, TRUE);
960 g_string_free (string2, TRUE);
962 g_print ("test positional printf formats (not supported): ");
963 string = g_strdup_printf ("%.*s%s", 5, "a", "b");
964 tmp_string = g_strdup_printf ("%2$*1$s", 5, "c");
965 g_print ("%s%s\n", string, tmp_string);
966 g_free (tmp_string);
967 g_free (string);
969 g_print ("checking timers...\n");
971 timer = g_timer_new ();
972 g_print (" spinning for 3 seconds...\n");
974 g_timer_start (timer);
975 while (g_timer_elapsed (timer, NULL) < 3)
978 g_timer_stop (timer);
979 g_timer_destroy (timer);
981 g_print ("ok\n");
983 g_print ("checking g_timer_continue...\n");
985 timer2 = g_timer_new ();
987 g_print ("\trun for 1 second...\n");
988 timer = g_timer_new();
989 g_usleep(G_USEC_PER_SEC); /* run timer for 1 second */
990 g_timer_stop(timer);
992 g_print ("\tstop for 1 second...\n");
993 g_usleep(G_USEC_PER_SEC); /* wait for 1 second */
994 g_print ("\trun for 2 seconds...\n");
996 g_timer_continue(timer);
997 g_usleep(2*G_USEC_PER_SEC); /* run timer for 2 seconds */
998 g_timer_stop(timer);
1000 g_print ("\tstop for 1.5 seconds...\n");
1001 g_usleep((3*G_USEC_PER_SEC)/2); /* wait for 1.5 seconds */
1002 g_print ("\trun for 0.2 seconds...\n");
1004 g_timer_continue(timer);
1005 g_usleep(G_USEC_PER_SEC/5); /* run timer for 0.2 seconds */
1006 g_timer_stop(timer);
1008 g_print ("\tstop for 4 seconds...\n");
1009 g_usleep(4*G_USEC_PER_SEC); /* wait for 4 seconds */
1010 g_print ("\trun for 5.8 seconds...\n");
1012 g_timer_continue(timer);
1013 g_usleep((29*G_USEC_PER_SEC)/5); /* run timer for 5.8 seconds */
1014 g_timer_stop(timer);
1016 g_print ("\t=> total elapsed = %.2f seconds (should be: 9.00 seconds)\n\n", g_timer_elapsed(timer, NULL));
1018 if (g_timer_elapsed(timer, NULL) > 8.8 && g_timer_elapsed(timer, NULL) < 9.2)
1019 g_print ("g_timer_continue ... ok\n\n");
1020 else
1021 g_print ("g_timer_continue ... ***** FAILED *****\n\n");
1023 g_timer_stop(timer2);
1025 if (g_timer_elapsed(timer2, NULL) > (8.8+6.5) && g_timer_elapsed(timer2, NULL) < (9.2+6.5))
1026 g_print ("timer2 ... ok\n\n");
1027 else
1028 g_print ("timer2 ... ***** FAILED *****\n\n");
1030 g_timer_destroy(timer);
1031 g_timer_destroy(timer2);
1033 g_print ("checking g_ascii_strcasecmp...");
1034 g_assert (g_ascii_strcasecmp ("FroboZZ", "frobozz") == 0);
1035 g_assert (g_ascii_strcasecmp ("frobozz", "frobozz") == 0);
1036 g_assert (g_ascii_strcasecmp ("frobozz", "FROBOZZ") == 0);
1037 g_assert (g_ascii_strcasecmp ("FROBOZZ", "froboz") > 0);
1038 g_assert (g_ascii_strcasecmp ("", "") == 0);
1039 g_assert (g_ascii_strcasecmp ("!#%&/()", "!#%&/()") == 0);
1040 g_assert (g_ascii_strcasecmp ("a", "b") < 0);
1041 g_assert (g_ascii_strcasecmp ("a", "B") < 0);
1042 g_assert (g_ascii_strcasecmp ("A", "b") < 0);
1043 g_assert (g_ascii_strcasecmp ("A", "B") < 0);
1044 g_assert (g_ascii_strcasecmp ("b", "a") > 0);
1045 g_assert (g_ascii_strcasecmp ("b", "A") > 0);
1046 g_assert (g_ascii_strcasecmp ("B", "a") > 0);
1047 g_assert (g_ascii_strcasecmp ("B", "A") > 0);
1049 g_print ("ok\n");
1051 g_print ("checking g_strdup...");
1052 g_assert(g_strdup(NULL) == NULL);
1053 string = g_strdup(GLIB_TEST_STRING);
1054 g_assert(string != NULL);
1055 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
1056 g_free(string);
1058 g_print ("ok\n");
1060 g_print ("checking g_strconcat...");
1061 string = g_strconcat(GLIB_TEST_STRING, NULL);
1062 g_assert(string != NULL);
1063 g_assert(strcmp(string, GLIB_TEST_STRING) == 0);
1064 g_free(string);
1065 string = g_strconcat(GLIB_TEST_STRING, GLIB_TEST_STRING,
1066 GLIB_TEST_STRING, NULL);
1067 g_assert(string != NULL);
1068 g_assert(strcmp(string, GLIB_TEST_STRING GLIB_TEST_STRING
1069 GLIB_TEST_STRING) == 0);
1070 g_free(string);
1071 g_print ("ok\n");
1074 g_print("checking g_strlcpy/g_strlcat...");
1075 /* The following is a torture test for strlcpy/strlcat, with lots of
1076 * checking; normal users wouldn't use them this way!
1078 string = g_malloc (6);
1079 *(string + 5) = 'Z'; /* guard value, shouldn't change during test */
1080 *string = 'q';
1081 g_assert (g_strlcpy(string, "" , 5) == 0);
1082 g_assert ( *string == '\0' );
1083 *string = 'q';
1084 g_assert (g_strlcpy(string, "abc" , 5) == 3);
1085 g_assert ( *(string + 3) == '\0' );
1086 g_assert (g_str_equal(string, "abc"));
1087 g_assert (g_strlcpy(string, "abcd" , 5) == 4);
1088 g_assert ( *(string + 4) == '\0' );
1089 g_assert ( *(string + 5) == 'Z' );
1090 g_assert (g_str_equal(string, "abcd"));
1091 g_assert (g_strlcpy(string, "abcde" , 5) == 5);
1092 g_assert ( *(string + 4) == '\0' );
1093 g_assert ( *(string + 5) == 'Z' );
1094 g_assert (g_str_equal(string, "abcd"));
1095 g_assert (g_strlcpy(string, "abcdef" , 5) == 6);
1096 g_assert ( *(string + 4) == '\0' );
1097 g_assert ( *(string + 5) == 'Z' );
1098 g_assert (g_str_equal(string, "abcd"));
1099 *string = 'Y';
1100 *(string + 1)= '\0';
1101 g_assert (g_strlcpy(string, "Hello" , 0) == 5);
1102 g_assert (*string == 'Y');
1103 *string = '\0';
1104 g_assert (g_strlcat(string, "123" , 5) == 3);
1105 g_assert ( *(string + 3) == '\0' );
1106 g_assert (g_str_equal(string, "123"));
1107 g_assert (g_strlcat(string, "" , 5) == 3);
1108 g_assert ( *(string + 3) == '\0' );
1109 g_assert (g_str_equal(string, "123"));
1110 g_assert (g_strlcat(string, "4", 5) == 4);
1111 g_assert (g_str_equal(string, "1234"));
1112 g_assert (g_strlcat(string, "5", 5) == 5);
1113 g_assert ( *(string + 4) == '\0' );
1114 g_assert (g_str_equal(string, "1234"));
1115 g_assert ( *(string + 5) == 'Z' );
1116 *string = 'Y';
1117 *(string + 1)= '\0';
1118 g_assert (g_strlcat(string, "123" , 0) == 3);
1119 g_assert (*string == 'Y');
1121 /* A few more tests, demonstrating more "normal" use */
1122 g_assert (g_strlcpy(string, "hi", 5) == 2);
1123 g_assert (g_str_equal(string, "hi"));
1124 g_assert (g_strlcat(string, "t", 5) == 3);
1125 g_assert (g_str_equal(string, "hit"));
1126 g_free(string);
1128 g_print ("ok\n");
1131 g_print ("checking g_strdup_printf...");
1132 string = g_strdup_printf ("%05d %-5s", 21, "test");
1133 g_assert (string != NULL);
1134 g_assert (strcmp(string, "00021 test ") == 0);
1135 g_free (string);
1137 g_print ("ok\n");
1139 /* g_debug (argv[0]); */
1141 /* Relation tests */
1143 g_print ("checking relations...");
1145 relation = g_relation_new (2);
1147 g_relation_index (relation, 0, g_int_hash, g_int_equal);
1148 g_relation_index (relation, 1, g_int_hash, g_int_equal);
1150 for (i = 0; i < 1024; i += 1)
1151 data[i] = i;
1153 for (i = 1; i < 1023; i += 1)
1155 g_relation_insert (relation, data + i, data + i + 1);
1156 g_relation_insert (relation, data + i, data + i - 1);
1159 for (i = 2; i < 1022; i += 1)
1161 g_assert (! g_relation_exists (relation, data + i, data + i));
1162 g_assert (! g_relation_exists (relation, data + i, data + i + 2));
1163 g_assert (! g_relation_exists (relation, data + i, data + i - 2));
1166 for (i = 1; i < 1023; i += 1)
1168 g_assert (g_relation_exists (relation, data + i, data + i + 1));
1169 g_assert (g_relation_exists (relation, data + i, data + i - 1));
1172 for (i = 2; i < 1022; i += 1)
1174 g_assert (g_relation_count (relation, data + i, 0) == 2);
1175 g_assert (g_relation_count (relation, data + i, 1) == 2);
1178 g_assert (g_relation_count (relation, data, 0) == 0);
1180 g_assert (g_relation_count (relation, data + 42, 0) == 2);
1181 g_assert (g_relation_count (relation, data + 43, 1) == 2);
1182 g_assert (g_relation_count (relation, data + 41, 1) == 2);
1183 g_relation_delete (relation, data + 42, 0);
1184 g_assert (g_relation_count (relation, data + 42, 0) == 0);
1185 g_assert (g_relation_count (relation, data + 43, 1) == 1);
1186 g_assert (g_relation_count (relation, data + 41, 1) == 1);
1188 tuples = g_relation_select (relation, data + 200, 0);
1190 g_assert (tuples->len == 2);
1192 #if 0
1193 for (i = 0; i < tuples->len; i += 1)
1195 printf ("%d %d\n",
1196 *(gint*) g_tuples_index (tuples, i, 0),
1197 *(gint*) g_tuples_index (tuples, i, 1));
1199 #endif
1201 g_assert (g_relation_exists (relation, data + 300, data + 301));
1202 g_relation_delete (relation, data + 300, 0);
1203 g_assert (!g_relation_exists (relation, data + 300, data + 301));
1205 g_tuples_destroy (tuples);
1207 g_relation_destroy (relation);
1209 relation = NULL;
1211 g_print ("ok\n");
1213 g_print ("checking pointer arrays...");
1215 gparray = g_ptr_array_new ();
1216 for (i = 0; i < 10000; i++)
1217 g_ptr_array_add (gparray, GINT_TO_POINTER (i));
1219 for (i = 0; i < 10000; i++)
1220 if (g_ptr_array_index (gparray, i) != GINT_TO_POINTER (i))
1221 g_print ("array fails: %p ( %p )\n", g_ptr_array_index (gparray, i), GINT_TO_POINTER (i));
1223 g_ptr_array_free (gparray, TRUE);
1225 g_print ("ok\n");
1228 g_print ("checking byte arrays...");
1230 gbarray = g_byte_array_new ();
1231 for (i = 0; i < 10000; i++)
1232 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1234 for (i = 0; i < 10000; i++)
1236 g_assert (gbarray->data[4*i] == 'a');
1237 g_assert (gbarray->data[4*i+1] == 'b');
1238 g_assert (gbarray->data[4*i+2] == 'c');
1239 g_assert (gbarray->data[4*i+3] == 'd');
1242 g_byte_array_free (gbarray, TRUE);
1243 g_print ("ok\n");
1245 g_printerr ("g_log tests:");
1246 g_warning ("harmless warning with parameters: %d %s %#x", 42, "Boo", 12345);
1247 g_message ("the next warning is a test:");
1248 string = NULL;
1249 g_print (string);
1250 g_message ("non-printable UTF-8: \"\xc3\xa4\xda\x85\"");
1251 g_message ("unsafe chars: \"\x10\x11\x12\n\t\x7f\x81\x82\x83\"");
1253 g_print ("checking endian macros (host is ");
1254 #if G_BYTE_ORDER == G_BIG_ENDIAN
1255 g_print ("big endian)...");
1256 #else
1257 g_print ("little endian)...");
1258 #endif
1259 g_assert (GUINT16_SWAP_LE_BE (gu16t1) == gu16t2);
1260 g_assert (GUINT32_SWAP_LE_BE (gu32t1) == gu32t2);
1261 g_assert (GUINT64_SWAP_LE_BE (gu64t1) == gu64t2);
1263 g_print ("ok\n");
1265 if (g_get_charset ((G_CONST_RETURN char**)&charset))
1266 g_print ("current charset is UTF-8: %s\n", charset);
1267 else
1268 g_print ("current charset is not UTF-8: %s\n", charset);
1270 #ifdef G_PLATFORM_WIN32
1271 g_print ("current locale: %s\n", g_win32_getlocale ());
1272 g_print ("GLib DLL name tested for: %s\n", glib_dll);
1274 g_print ("GLib installation directory, from Registry entry for %s if available: %s\n",
1275 GETTEXT_PACKAGE,
1276 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, NULL));
1277 g_print ("Ditto, or from GLib DLL name: %s\n",
1278 g_win32_get_package_installation_directory (GETTEXT_PACKAGE, glib_dll));
1279 g_print ("Ditto, only from GLib DLL name: %s\n",
1280 g_win32_get_package_installation_directory (NULL, glib_dll));
1281 g_print ("locale subdirectory of GLib installation directory: %s\n",
1282 g_win32_get_package_installation_subdirectory (NULL, glib_dll, "lib\\locale"));
1283 g_print ("GTK+ 2.0 installation directory, if available: %s\n",
1284 g_win32_get_package_installation_directory ("gtk20", NULL));
1286 g_print ("found more.com as %s\n", g_find_program_in_path ("more.com"));
1287 g_print ("found regedit as %s\n", g_find_program_in_path ("regedit"));
1289 #endif
1291 g_print ("checking file functions...\n");
1293 strcpy (template, "foobar");
1294 fd = g_mkstemp (template);
1295 if (fd != -1)
1296 g_print ("g_mkstemp works even if template doesn't end in XXXXXX\n");
1297 close (fd);
1298 strcpy (template, "fooXXXXXX");
1299 fd = g_mkstemp (template);
1300 if (fd == -1)
1301 g_print ("g_mkstemp didn't work for template %s\n", template);
1302 i = write (fd, hello, hellolen);
1303 if (i == -1)
1304 g_print ("write() failed: %s\n", g_strerror (errno));
1305 else if (i != hellolen)
1306 g_print ("write() should have written %d bytes, wrote %d\n", hellolen, i);
1308 lseek (fd, 0, 0);
1309 i = read (fd, chars, sizeof (chars));
1310 if (i == -1)
1311 g_print ("read() failed: %s\n", g_strerror (errno));
1312 else if (i != hellolen)
1313 g_print ("read() should have read %d bytes, got %d\n", hellolen, i);
1315 chars[i] = 0;
1316 if (strcmp (chars, hello) != 0)
1317 g_print ("wrote '%s', but got '%s'\n", hello, chars);
1319 close (fd);
1320 remove (template);
1322 error = NULL;
1323 strcpy (template, "zap" G_DIR_SEPARATOR_S "barXXXXXX");
1324 fd = g_file_open_tmp (template, &name_used, &error);
1325 if (fd != -1)
1326 g_print ("g_file_open_tmp works even if template contains '%s'\n",
1327 G_DIR_SEPARATOR_S);
1328 else
1329 g_print ("g_file_open_tmp correctly returns error: %s\n",
1330 error->message);
1331 close (fd);
1332 g_clear_error (&error);
1334 #ifdef G_OS_WIN32
1335 strcpy (template, "zap/barXXXXXX");
1336 fd = g_file_open_tmp (template, &name_used, &error);
1337 if (fd != -1)
1338 g_print ("g_file_open_tmp works even if template contains '/'\n");
1339 else
1340 g_print ("g_file_open_tmp correctly returns error: %s\n",
1341 error->message);
1342 close (fd);
1343 g_clear_error (&error);
1344 #endif
1346 strcpy (template, "zapXXXXXX");
1347 fd = g_file_open_tmp (template, &name_used, &error);
1348 if (fd == -1)
1349 g_print ("g_file_open_tmp didn't work for template '%s': %s\n",
1350 template, error->message);
1351 else
1352 g_print ("g_file_open_tmp for template '%s' used name '%s'\n",
1353 template, name_used);
1354 close (fd);
1355 g_clear_error (&error);
1356 remove (name_used);
1358 fd = g_file_open_tmp (NULL, &name_used, &error);
1359 if (fd == -1)
1360 g_print ("g_file_open_tmp didn't work for a NULL template: %s\n",
1361 error->message);
1362 else
1363 g_print ("g_file_open_tmp for NULL template used name '%s'\n",
1364 name_used);
1365 close (fd);
1366 g_clear_error (&error);
1367 remove (name_used);
1369 return 0;