Bug 550433 – g_test_init doesn't recognize --help
[platform/upstream/glib.git] / glib / gtestutils.c
1 /* GLib testing utilities
2  * Copyright (C) 2007 Imendio AB
3  * Authors: Tim Janik, Sven Herzberg
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 #include "config.h"
21 #include "gtestutils.h"
22 #include "galias.h"
23 #include <sys/types.h>
24 #ifdef G_OS_UNIX
25 #include <sys/wait.h>
26 #include <sys/time.h>
27 #include <fcntl.h>
28 #endif
29 #include <string.h>
30 #include <stdlib.h>
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #ifdef G_OS_WIN32
35 #include <io.h>
36 #endif
37 #include <errno.h>
38 #include <signal.h>
39 #ifdef HAVE_SYS_SELECT_H
40 #include <sys/select.h>
41 #endif /* HAVE_SYS_SELECT_H */
42
43 /* --- structures --- */
44 struct GTestCase
45 {
46   gchar  *name;
47   guint   fixture_size;
48   void   (*fixture_setup)    (void*, gconstpointer);
49   void   (*fixture_test)     (void*, gconstpointer);
50   void   (*fixture_teardown) (void*, gconstpointer);
51   gpointer test_data;
52 };
53 struct GTestSuite
54 {
55   gchar  *name;
56   GSList *suites;
57   GSList *cases;
58 };
59 typedef struct DestroyEntry DestroyEntry;
60 struct DestroyEntry
61 {
62   DestroyEntry *next;
63   GDestroyNotify destroy_func;
64   gpointer       destroy_data;
65 };
66
67 /* --- prototypes --- */
68 static void     test_run_seed                   (const gchar *rseed);
69 static void     test_trap_clear                 (void);
70 static guint8*  g_test_log_dump                 (GTestLogMsg *msg,
71                                                  guint       *len);
72 static void     gtest_default_log_handler       (const gchar    *log_domain,
73                                                  GLogLevelFlags  log_level,
74                                                  const gchar    *message,
75                                                  gpointer        unused_data);
76
77
78 /* --- variables --- */
79 static int         test_log_fd = -1;
80 static gboolean    test_mode_fatal = TRUE;
81 static gboolean    g_test_run_once = TRUE;
82 static gboolean    test_run_list = FALSE;
83 static gchar      *test_run_seedstr = NULL;
84 static GRand      *test_run_rand = NULL;
85 static gchar      *test_run_name = "";
86 static guint       test_run_forks = 0;
87 static guint       test_run_count = 0;
88 static guint       test_skip_count = 0;
89 static GTimer     *test_user_timer = NULL;
90 static double      test_user_stamp = 0;
91 static GSList     *test_paths = NULL;
92 static GTestSuite *test_suite_root = NULL;
93 static int         test_trap_last_status = 0;
94 static int         test_trap_last_pid = 0;
95 static char       *test_trap_last_stdout = NULL;
96 static char       *test_trap_last_stderr = NULL;
97 static char       *test_uri_base = NULL;
98 static gboolean    test_debug_log = FALSE;
99 static DestroyEntry *test_destroy_queue = NULL;
100 static GTestConfig mutable_test_config_vars = {
101   FALSE,        /* test_initialized */
102   TRUE,         /* test_quick */
103   FALSE,        /* test_perf */
104   FALSE,        /* test_verbose */
105   FALSE,        /* test_quiet */
106 };
107 const GTestConfig * const g_test_config_vars = &mutable_test_config_vars;
108
109 /* --- functions --- */
110 const char*
111 g_test_log_type_name (GTestLogType log_type)
112 {
113   switch (log_type)
114     {
115     case G_TEST_LOG_NONE:               return "none";
116     case G_TEST_LOG_ERROR:              return "error";
117     case G_TEST_LOG_START_BINARY:       return "binary";
118     case G_TEST_LOG_LIST_CASE:          return "list";
119     case G_TEST_LOG_SKIP_CASE:          return "skip";
120     case G_TEST_LOG_START_CASE:         return "start";
121     case G_TEST_LOG_STOP_CASE:          return "stop";
122     case G_TEST_LOG_MIN_RESULT:         return "minperf";
123     case G_TEST_LOG_MAX_RESULT:         return "maxperf";
124     case G_TEST_LOG_MESSAGE:            return "message";
125     }
126   return "???";
127 }
128
129 static void
130 g_test_log_send (guint         n_bytes,
131                  const guint8 *buffer)
132 {
133   if (test_log_fd >= 0)
134     {
135       int r;
136       do
137         r = write (test_log_fd, buffer, n_bytes);
138       while (r < 0 && errno == EINTR);
139     }
140   if (test_debug_log)
141     {
142       GTestLogBuffer *lbuffer = g_test_log_buffer_new();
143       GTestLogMsg *msg;
144       guint ui;
145       g_test_log_buffer_push (lbuffer, n_bytes, buffer);
146       msg = g_test_log_buffer_pop (lbuffer);
147       g_assert (msg != NULL); /* FIXME: should be g_awrn_if_fail */
148       g_assert (lbuffer->data->len == 0); /* FIXME: should be g_awrn_if_fail */
149       g_test_log_buffer_free (lbuffer);
150       /* print message */
151       g_printerr ("{*LOG(%s)", g_test_log_type_name (msg->log_type));
152       for (ui = 0; ui < msg->n_strings; ui++)
153         g_printerr (":{%s}", msg->strings[ui]);
154       if (msg->n_nums)
155         {
156           g_printerr (":(");
157           for (ui = 0; ui < msg->n_nums; ui++)
158             g_printerr ("%s%.16Lg", ui ? ";" : "", msg->nums[ui]);
159           g_printerr (")");
160         }
161       g_printerr (":LOG*}\n");
162       g_test_log_msg_free (msg);
163     }
164 }
165
166 static void
167 g_test_log (GTestLogType lbit,
168             const gchar *string1,
169             const gchar *string2,
170             guint        n_args,
171             long double *largs)
172 {
173   gboolean fail = lbit == G_TEST_LOG_STOP_CASE && largs[0] != 0;
174   GTestLogMsg msg;
175   gchar *astrings[3] = { NULL, NULL, NULL };
176   guint8 *dbuffer;
177   guint32 dbufferlen;
178
179   switch (lbit)
180     {
181     case G_TEST_LOG_START_BINARY:
182       if (g_test_verbose())
183         g_print ("GTest: random seed: %s\n", string2);
184       break;
185     case G_TEST_LOG_STOP_CASE:
186       if (g_test_verbose())
187         g_print ("GTest: result: %s\n", fail ? "FAIL" : "OK");
188       else if (!g_test_quiet())
189         g_print ("%s\n", fail ? "FAIL" : "OK");
190       if (fail && test_mode_fatal)
191         abort();
192       break;
193     case G_TEST_LOG_MIN_RESULT:
194       if (g_test_verbose())
195         g_print ("(MINPERF:%s)\n", string1);
196       break;
197     case G_TEST_LOG_MAX_RESULT:
198       if (g_test_verbose())
199         g_print ("(MAXPERF:%s)\n", string1);
200       break;
201     case G_TEST_LOG_MESSAGE:
202       if (g_test_verbose())
203         g_print ("(MSG: %s)\n", string1);
204       break;
205     default: ;
206     }
207
208   msg.log_type = lbit;
209   msg.n_strings = (string1 != NULL) + (string1 && string2);
210   msg.strings = astrings;
211   astrings[0] = (gchar*) string1;
212   astrings[1] = astrings[0] ? (gchar*) string2 : NULL;
213   msg.n_nums = n_args;
214   msg.nums = largs;
215   dbuffer = g_test_log_dump (&msg, &dbufferlen);
216   g_test_log_send (dbufferlen, dbuffer);
217   g_free (dbuffer);
218
219   switch (lbit)
220     {
221     case G_TEST_LOG_START_CASE:
222       if (g_test_verbose())
223         g_print ("GTest: run: %s\n", string1);
224       else if (!g_test_quiet())
225         g_print ("%s: ", string1);
226       break;
227     default: ;
228     }
229 }
230
231 /* We intentionally parse the command line without GOptionContext
232  * because otherwise you would never be able to test it.
233  */
234 static void
235 parse_args (gint    *argc_p,
236             gchar ***argv_p)
237 {
238   guint argc = *argc_p;
239   gchar **argv = *argv_p;
240   guint i, e;
241   /* parse known args */
242   for (i = 1; i < argc; i++)
243     {
244       if (strcmp (argv[i], "--g-fatal-warnings") == 0)
245         {
246           GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
247           fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
248           g_log_set_always_fatal (fatal_mask);
249           argv[i] = NULL;
250         }
251       else if (strcmp (argv[i], "--keep-going") == 0 ||
252                strcmp (argv[i], "-k") == 0)
253         {
254           test_mode_fatal = FALSE;
255           argv[i] = NULL;
256         }
257       else if (strcmp (argv[i], "--debug-log") == 0)
258         {
259           test_debug_log = TRUE;
260           argv[i] = NULL;
261         }
262       else if (strcmp ("--GTestLogFD", argv[i]) == 0 || strncmp ("--GTestLogFD=", argv[i], 13) == 0)
263         {
264           gchar *equal = argv[i] + 12;
265           if (*equal == '=')
266             test_log_fd = g_ascii_strtoull (equal + 1, NULL, 0);
267           else if (i + 1 < argc)
268             {
269               argv[i++] = NULL;
270               test_log_fd = g_ascii_strtoull (argv[i], NULL, 0);
271             }
272           argv[i] = NULL;
273         }
274       else if (strcmp ("--GTestSkipCount", argv[i]) == 0 || strncmp ("--GTestSkipCount=", argv[i], 17) == 0)
275         {
276           gchar *equal = argv[i] + 16;
277           if (*equal == '=')
278             test_skip_count = g_ascii_strtoull (equal + 1, NULL, 0);
279           else if (i + 1 < argc)
280             {
281               argv[i++] = NULL;
282               test_skip_count = g_ascii_strtoull (argv[i], NULL, 0);
283             }
284           argv[i] = NULL;
285         }
286       else if (strcmp ("-p", argv[i]) == 0 || strncmp ("-p=", argv[i], 3) == 0)
287         {
288           gchar *equal = argv[i] + 2;
289           if (*equal == '=')
290             test_paths = g_slist_prepend (test_paths, equal + 1);
291           else if (i + 1 < argc)
292             {
293               argv[i++] = NULL;
294               test_paths = g_slist_prepend (test_paths, argv[i]);
295             }
296           argv[i] = NULL;
297         }
298       else if (strcmp ("-m", argv[i]) == 0 || strncmp ("-m=", argv[i], 3) == 0)
299         {
300           gchar *equal = argv[i] + 2;
301           const gchar *mode = "";
302           if (*equal == '=')
303             mode = equal + 1;
304           else if (i + 1 < argc)
305             {
306               argv[i++] = NULL;
307               mode = argv[i];
308             }
309           if (strcmp (mode, "perf") == 0)
310             mutable_test_config_vars.test_perf = TRUE;
311           else if (strcmp (mode, "slow") == 0)
312             mutable_test_config_vars.test_quick = FALSE;
313           else if (strcmp (mode, "thorough") == 0)
314             mutable_test_config_vars.test_quick = FALSE;
315           else if (strcmp (mode, "quick") == 0)
316             {
317               mutable_test_config_vars.test_quick = TRUE;
318               mutable_test_config_vars.test_perf = FALSE;
319             }
320           else
321             g_error ("unknown test mode: -m %s", mode);
322           argv[i] = NULL;
323         }
324       else if (strcmp ("-q", argv[i]) == 0 || strcmp ("--quiet", argv[i]) == 0)
325         {
326           mutable_test_config_vars.test_quiet = TRUE;
327           mutable_test_config_vars.test_verbose = FALSE;
328           argv[i] = NULL;
329         }
330       else if (strcmp ("--verbose", argv[i]) == 0)
331         {
332           mutable_test_config_vars.test_quiet = FALSE;
333           mutable_test_config_vars.test_verbose = TRUE;
334           argv[i] = NULL;
335         }
336       else if (strcmp ("-l", argv[i]) == 0)
337         {
338           test_run_list = TRUE;
339           argv[i] = NULL;
340         }
341       else if (strcmp ("--seed", argv[i]) == 0 || strncmp ("--seed=", argv[i], 7) == 0)
342         {
343           gchar *equal = argv[i] + 6;
344           if (*equal == '=')
345             test_run_seedstr = equal + 1;
346           else if (i + 1 < argc)
347             {
348               argv[i++] = NULL;
349               test_run_seedstr = argv[i];
350             }
351           argv[i] = NULL;
352         }
353       else if (strcmp ("-?", argv[i]) == 0 || strcmp ("--help", argv[i]) == 0)
354         {
355           printf ("Usage:\n"
356                   "  %s [OPTION...]\n\n"
357                   "Help Options:\n"
358                   "  -?, --help                     Show help options\n"
359                   "Test Options:\n"
360                   "  -l                             List test cases available in a test executable\n"
361                   "  -seed=RANDOMSEED               Provide a random seed to reproduce test\n"
362                   "                                 runs using random numbers\n"
363                   "  --verbose                      Run tests verbosely\n"
364                   "  -q, --quiet                    Run tests quietly\n"
365                   "  -p TESTPATH                    execute all tests matching TESTPATH\n"
366                   "  -m {perf|slow|thorough|quick}  Execute tests according modes\n"
367                   "  --debug-log                    debug test logging output\n"
368                   "  -k, --keep-going               gtester-specific argument\n"
369                   "  --GTestLogFD=N                 gtester-specific argument\n"
370                   "  --GTestSkipCount=N             gtester-specific argument\n",
371                   argv[0]);
372           exit (0);
373         }
374     }
375   /* collapse argv */
376   e = 1;
377   for (i = 1; i < argc; i++)
378     if (argv[i])
379       {
380         argv[e++] = argv[i];
381         if (i >= e)
382           argv[i] = NULL;
383       }
384   *argc_p = e;
385 }
386
387 /**
388  * g_test_init:
389  * @argc: Address of the @argc parameter of the main() function.
390  *        Changed if any arguments were handled.
391  * @argv: Address of the @argv parameter of main().
392  *        Any parameters understood by g_test_init() stripped before return.
393  * @Varargs: Reserved for future extension. Currently, you must pass %NULL.
394  *
395  * Initialize the GLib testing framework, e.g. by seeding the
396  * test random number generator, the name for g_get_prgname()
397  * and parsing test related command line args.
398  * So far, the following arguments are understood:
399  * <variablelist>
400  *   <varlistentry>
401  *     <term><option>-l</option></term>
402  *     <listitem><para>
403  *       list test cases available in a test executable.
404  *     </para></listitem>
405  *   </varlistentry>
406  *   <varlistentry>
407  *     <term><option>--seed=<replaceable>RANDOMSEED</replaceable></option></term>
408  *     <listitem><para>
409  *       provide a random seed to reproduce test runs using random numbers.
410  *     </para></listitem>
411  *     </varlistentry>
412  *     <varlistentry>
413  *       <term><option>--verbose</option></term>
414  *       <listitem><para>run tests verbosely.</para></listitem>
415  *     </varlistentry>
416  *     <varlistentry>
417  *       <term><option>-q</option>, <option>--quiet</option></term>
418  *       <listitem><para>run tests quietly.</para></listitem>
419  *     </varlistentry>
420  *     <varlistentry>
421  *       <term><option>-p <replaceable>TESTPATH</replaceable></option></term>
422  *       <listitem><para>
423  *         execute all tests matching <replaceable>TESTPATH</replaceable>.
424  *       </para></listitem>
425  *     </varlistentry>
426  *     <varlistentry>
427  *       <term><option>-m {perf|slow|thorough|quick}</option></term>
428  *       <listitem><para>
429  *         execute tests according to these test modes:
430  *         <variablelist>
431  *           <varlistentry>
432  *             <term>perf</term>
433  *             <listitem><para>
434  *               performance tests, may take long and report results.
435  *             </para></listitem>
436  *           </varlistentry>
437  *           <varlistentry>
438  *             <term>slow, thorough</term>
439  *             <listitem><para>
440  *               slow and thorough tests, may take quite long and 
441  *               maximize coverage.
442  *             </para></listitem>
443  *           </varlistentry>
444  *           <varlistentry>
445  *             <term>quick</term>
446  *             <listitem><para>
447  *               quick tests, should run really quickly and give good coverage.
448  *             </para></listitem>
449  *           </varlistentry>
450  *         </variablelist>
451  *       </para></listitem>
452  *     </varlistentry>
453  *     <varlistentry>
454  *       <term><option>--debug-log</option></term>
455  *       <listitem><para>debug test logging output.</para></listitem>
456  *     </varlistentry>
457  *     <varlistentry>
458  *       <term><option>-k</option>, <option>--keep-going</option></term>
459  *       <listitem><para>gtester-specific argument.</para></listitem>
460  *     </varlistentry>
461  *     <varlistentry>
462  *       <term><option>--GTestLogFD <replaceable>N</replaceable></option></term>
463  *       <listitem><para>gtester-specific argument.</para></listitem>
464  *     </varlistentry>
465  *     <varlistentry>
466  *       <term><option>--GTestSkipCount <replaceable>N</replaceable></option></term>
467  *       <listitem><para>gtester-specific argument.</para></listitem>
468  *     </varlistentry>
469  *  </variablelist>
470  *
471  * Since: 2.16
472  */
473 void
474 g_test_init (int    *argc,
475              char ***argv,
476              ...)
477 {
478   static char seedstr[4 + 4 * 8 + 1];
479   va_list args;
480   gpointer vararg1;
481   /* make warnings and criticals fatal for all test programs */
482   GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
483   fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
484   g_log_set_always_fatal (fatal_mask);
485   /* check caller args */
486   g_return_if_fail (argc != NULL);
487   g_return_if_fail (argv != NULL);
488   g_return_if_fail (g_test_config_vars->test_initialized == FALSE);
489   mutable_test_config_vars.test_initialized = TRUE;
490
491   va_start (args, argv);
492   vararg1 = va_arg (args, gpointer); /* reserved for future extensions */
493   va_end (args);
494   g_return_if_fail (vararg1 == NULL);
495
496   /* setup random seed string */
497   g_snprintf (seedstr, sizeof (seedstr), "R02S%08x%08x%08x%08x", g_random_int(), g_random_int(), g_random_int(), g_random_int());
498   test_run_seedstr = seedstr;
499
500   /* parse args, sets up mode, changes seed, etc. */
501   parse_args (argc, argv);
502   if (!g_get_prgname())
503     g_set_prgname ((*argv)[0]);
504
505   /* verify GRand reliability, needed for reliable seeds */
506   if (1)
507     {
508       GRand *rg = g_rand_new_with_seed (0xc8c49fb6);
509       guint32 t1 = g_rand_int (rg), t2 = g_rand_int (rg), t3 = g_rand_int (rg), t4 = g_rand_int (rg);
510       /* g_print ("GRand-current: 0x%x 0x%x 0x%x 0x%x\n", t1, t2, t3, t4); */
511       if (t1 != 0xfab39f9b || t2 != 0xb948fb0e || t3 != 0x3d31be26 || t4 != 0x43a19d66)
512         g_warning ("random numbers are not GRand-2.2 compatible, seeds may be broken (check $G_RANDOM_VERSION)");
513       g_rand_free (rg);
514     }
515
516   /* check rand seed */
517   test_run_seed (test_run_seedstr);
518
519   /* report program start */
520   g_log_set_default_handler (gtest_default_log_handler, NULL);
521   g_test_log (G_TEST_LOG_START_BINARY, g_get_prgname(), test_run_seedstr, 0, NULL);
522 }
523
524 static void
525 test_run_seed (const gchar *rseed)
526 {
527   guint seed_failed = 0;
528   if (test_run_rand)
529     g_rand_free (test_run_rand);
530   test_run_rand = NULL;
531   while (strchr (" \t\v\r\n\f", *rseed))
532     rseed++;
533   if (strncmp (rseed, "R02S", 4) == 0)  /* seed for random generator 02 (GRand-2.2) */
534     {
535       const char *s = rseed + 4;
536       if (strlen (s) >= 32)             /* require 4 * 8 chars */
537         {
538           guint32 seedarray[4];
539           gchar *p, hexbuf[9] = { 0, };
540           memcpy (hexbuf, s + 0, 8);
541           seedarray[0] = g_ascii_strtoull (hexbuf, &p, 16);
542           seed_failed += p != NULL && *p != 0;
543           memcpy (hexbuf, s + 8, 8);
544           seedarray[1] = g_ascii_strtoull (hexbuf, &p, 16);
545           seed_failed += p != NULL && *p != 0;
546           memcpy (hexbuf, s + 16, 8);
547           seedarray[2] = g_ascii_strtoull (hexbuf, &p, 16);
548           seed_failed += p != NULL && *p != 0;
549           memcpy (hexbuf, s + 24, 8);
550           seedarray[3] = g_ascii_strtoull (hexbuf, &p, 16);
551           seed_failed += p != NULL && *p != 0;
552           if (!seed_failed)
553             {
554               test_run_rand = g_rand_new_with_seed_array (seedarray, 4);
555               return;
556             }
557         }
558     }
559   g_error ("Unknown or invalid random seed: %s", rseed);
560 }
561
562 /**
563  * g_test_rand_int:
564  *
565  * Get a reproducible random integer number.
566  *
567  * The random numbers generated by the g_test_rand_*() family of functions
568  * change with every new test program start, unless the --seed option is
569  * given when starting test programs.
570  *
571  * For individual test cases however, the random number generator is
572  * reseeded, to avoid dependencies between tests and to make --seed
573  * effective for all test cases.
574  *
575  * Returns: a random number from the seeded random number generator.
576  *
577  * Since: 2.16
578  */
579 gint32
580 g_test_rand_int (void)
581 {
582   return g_rand_int (test_run_rand);
583 }
584
585 /**
586  * g_test_rand_int_range:
587  * @begin: the minimum value returned by this function
588  * @end:   the smallest value not to be returned by this function
589  *
590  * Get a reproducible random integer number out of a specified range,
591  * see g_test_rand_int() for details on test case random numbers.
592  *
593  * Returns: a number with @begin <= number < @end.
594  * 
595  * Since: 2.16
596  */
597 gint32
598 g_test_rand_int_range (gint32          begin,
599                        gint32          end)
600 {
601   return g_rand_int_range (test_run_rand, begin, end);
602 }
603
604 /**
605  * g_test_rand_double:
606  *
607  * Get a reproducible random floating point number,
608  * see g_test_rand_int() for details on test case random numbers.
609  *
610  * Returns: a random number from the seeded random number generator.
611  *
612  * Since: 2.16
613  */
614 double
615 g_test_rand_double (void)
616 {
617   return g_rand_double (test_run_rand);
618 }
619
620 /**
621  * g_test_rand_double_range:
622  * @range_start: the minimum value returned by this function
623  * @range_end: the minimum value not returned by this function
624  *
625  * Get a reproducible random floating pointer number out of a specified range,
626  * see g_test_rand_int() for details on test case random numbers.
627  *
628  * Returns: a number with @range_start <= number < @range_end.
629  *
630  * Since: 2.16
631  */
632 double
633 g_test_rand_double_range (double          range_start,
634                           double          range_end)
635 {
636   return g_rand_double_range (test_run_rand, range_start, range_end);
637 }
638
639 /**
640  * g_test_timer_start:
641  *
642  * Start a timing test. Call g_test_timer_elapsed() when the task is supposed
643  * to be done. Call this function again to restart the timer.
644  *
645  * Since: 2.16
646  */
647 void
648 g_test_timer_start (void)
649 {
650   if (!test_user_timer)
651     test_user_timer = g_timer_new();
652   test_user_stamp = 0;
653   g_timer_start (test_user_timer);
654 }
655
656 /**
657  * g_test_timer_elapsed:
658  *
659  * Get the time since the last start of the timer with g_test_timer_start().
660  *
661  * Returns: the time since the last start of the timer, as a double
662  *
663  * Since: 2.16
664  */
665 double
666 g_test_timer_elapsed (void)
667 {
668   test_user_stamp = test_user_timer ? g_timer_elapsed (test_user_timer, NULL) : 0;
669   return test_user_stamp;
670 }
671
672 /**
673  * g_test_timer_last:
674  *
675  * Report the last result of g_test_timer_elapsed().
676  *
677  * Returns: the last result of g_test_timer_elapsed(), as a double
678  *
679  * Since: 2.16
680  */
681 double
682 g_test_timer_last (void)
683 {
684   return test_user_stamp;
685 }
686
687 /**
688  * g_test_minimized_result:
689  * @minimized_quantity: the reported value
690  * @format: the format string of the report message
691  * @Varargs: arguments to pass to the printf() function
692  *
693  * Report the result of a performance or measurement test.
694  * The test should generally strive to minimize the reported
695  * quantities (smaller values are better than larger ones),
696  * this and @minimized_quantity can determine sorting
697  * order for test result reports.
698  *
699  * Since: 2.16
700  */
701 void
702 g_test_minimized_result (double          minimized_quantity,
703                          const char     *format,
704                          ...)
705 {
706   long double largs = minimized_quantity;
707   gchar *buffer;
708   va_list args;
709   va_start (args, format);
710   buffer = g_strdup_vprintf (format, args);
711   va_end (args);
712   g_test_log (G_TEST_LOG_MIN_RESULT, buffer, NULL, 1, &largs);
713   g_free (buffer);
714 }
715
716 /**
717  * g_test_maximized_result:
718  * @maximized_quantity: the reported value
719  * @format: the format string of the report message
720  * @Varargs: arguments to pass to the printf() function
721  *
722  * Report the result of a performance or measurement test.
723  * The test should generally strive to maximize the reported
724  * quantities (larger values are better than smaller ones),
725  * this and @maximized_quantity can determine sorting
726  * order for test result reports.
727  *
728  * Since: 2.16
729  */
730 void
731 g_test_maximized_result (double          maximized_quantity,
732                          const char     *format,
733                          ...)
734 {
735   long double largs = maximized_quantity;
736   gchar *buffer;
737   va_list args;
738   va_start (args, format);
739   buffer = g_strdup_vprintf (format, args);
740   va_end (args);
741   g_test_log (G_TEST_LOG_MAX_RESULT, buffer, NULL, 1, &largs);
742   g_free (buffer);
743 }
744
745 /**
746  * g_test_message:
747  * @format: the format string
748  * @...:    printf-like arguments to @format
749  *
750  * Add a message to the test report.
751  *
752  * Since: 2.16
753  */
754 void
755 g_test_message (const char *format,
756                 ...)
757 {
758   gchar *buffer;
759   va_list args;
760   va_start (args, format);
761   buffer = g_strdup_vprintf (format, args);
762   va_end (args);
763   g_test_log (G_TEST_LOG_MESSAGE, buffer, NULL, 0, NULL);
764   g_free (buffer);
765 }
766
767 /**
768  * g_test_bug_base:
769  * @uri_pattern: the base pattern for bug URIs
770  *
771  * Specify the base URI for bug reports.
772  *
773  * The base URI is used to construct bug report messages for
774  * g_test_message() when g_test_bug() is called.
775  * Calling this function outside of a test case sets the
776  * default base URI for all test cases. Calling it from within
777  * a test case changes the base URI for the scope of the test
778  * case only.
779  * Bug URIs are constructed by appending a bug specific URI
780  * portion to @uri_pattern, or by replacing the special string
781  * '%s' within @uri_pattern if that is present.
782  *
783  * Since: 2.16
784  */
785 void
786 g_test_bug_base (const char *uri_pattern)
787 {
788   g_free (test_uri_base);
789   test_uri_base = g_strdup (uri_pattern);
790 }
791
792 /**
793  * g_test_bug:
794  * @bug_uri_snippet: Bug specific bug tracker URI portion.
795  *
796  * This function adds a message to test reports that
797  * associates a bug URI with a test case.
798  * Bug URIs are constructed from a base URI set with g_test_bug_base()
799  * and @bug_uri_snippet.
800  *
801  * Since: 2.16
802  */
803 void
804 g_test_bug (const char *bug_uri_snippet)
805 {
806   char *c;
807   g_return_if_fail (test_uri_base != NULL);
808   g_return_if_fail (bug_uri_snippet != NULL);
809   c = strstr (test_uri_base, "%s");
810   if (c)
811     {
812       char *b = g_strndup (test_uri_base, c - test_uri_base);
813       char *s = g_strconcat (b, bug_uri_snippet, c + 2, NULL);
814       g_free (b);
815       g_test_message ("Bug Reference: %s", s);
816       g_free (s);
817     }
818   else
819     g_test_message ("Bug Reference: %s%s", test_uri_base, bug_uri_snippet);
820 }
821
822 /**
823  * g_test_get_root:
824  *
825  * Get the toplevel test suite for the test path API.
826  *
827  * Returns: the toplevel #GTestSuite
828  *
829  * Since: 2.16
830  */
831 GTestSuite*
832 g_test_get_root (void)
833 {
834   if (!test_suite_root)
835     {
836       test_suite_root = g_test_create_suite ("root");
837       g_free (test_suite_root->name);
838       test_suite_root->name = g_strdup ("");
839     }
840   return test_suite_root;
841 }
842
843 /**
844  * g_test_run:
845  *
846  * Runs all tests under the toplevel suite which can be retrieved
847  * with g_test_get_root(). Similar to g_test_run_suite(), the test
848  * cases to be run are filtered according to
849  * test path arguments (-p <replaceable>testpath</replaceable>) as 
850  * parsed by g_test_init().
851  * g_test_run_suite() or g_test_run() may only be called once
852  * in a program.
853  *
854  * Returns: 0 on success
855  *
856  * Since: 2.16
857  */
858 int
859 g_test_run (void)
860 {
861   return g_test_run_suite (g_test_get_root());
862 }
863
864 /**
865  * g_test_create_case:
866  * @test_name:     the name for the test case
867  * @data_size:     the size of the fixture data structure
868  * @test_data:     test data argument for the test functions
869  * @data_setup:    the function to set up the fixture data
870  * @data_test:     the actual test function
871  * @data_teardown: the function to teardown the fixture data
872  *
873  * Create a new #GTestCase, named @test_name, this API is fairly
874  * low level, calling g_test_add() or g_test_add_func() is preferable.
875  * When this test is executed, a fixture structure of size @data_size
876  * will be allocated and filled with 0s. Then data_setup() is called
877  * to initialize the fixture. After fixture setup, the actual test
878  * function data_test() is called. Once the test run completed, the
879  * fixture structure is torn down  by calling data_teardown() and
880  * after that the memory is released.
881  *
882  * Splitting up a test run into fixture setup, test function and
883  * fixture teardown is most usful if the same fixture is used for
884  * multiple tests. In this cases, g_test_create_case() will be
885  * called with the same fixture, but varying @test_name and
886  * @data_test arguments.
887  *
888  * Returns: a newly allocated #GTestCase.
889  *
890  * Since: 2.16
891  */
892 GTestCase*
893 g_test_create_case (const char     *test_name,
894                     gsize           data_size,
895                     gconstpointer   test_data,
896                     void          (*data_setup) (void),
897                     void          (*data_test) (void),
898                     void          (*data_teardown) (void))
899 {
900   GTestCase *tc;
901   g_return_val_if_fail (test_name != NULL, NULL);
902   g_return_val_if_fail (strchr (test_name, '/') == NULL, NULL);
903   g_return_val_if_fail (test_name[0] != 0, NULL);
904   g_return_val_if_fail (data_test != NULL, NULL);
905   tc = g_slice_new0 (GTestCase);
906   tc->name = g_strdup (test_name);
907   tc->test_data = (gpointer) test_data;
908   tc->fixture_size = data_size;
909   tc->fixture_setup = (void*) data_setup;
910   tc->fixture_test = (void*) data_test;
911   tc->fixture_teardown = (void*) data_teardown;
912   return tc;
913 }
914
915 void
916 g_test_add_vtable (const char     *testpath,
917                    gsize           data_size,
918                    gconstpointer   test_data,
919                    void          (*data_setup)    (void),
920                    void          (*fixture_test_func) (void),
921                    void          (*data_teardown) (void))
922 {
923   gchar **segments;
924   guint ui;
925   GTestSuite *suite;
926
927   g_return_if_fail (testpath != NULL);
928   g_return_if_fail (testpath[0] == '/');
929   g_return_if_fail (fixture_test_func != NULL);
930
931   suite = g_test_get_root();
932   segments = g_strsplit (testpath, "/", -1);
933   for (ui = 0; segments[ui] != NULL; ui++)
934     {
935       const char *seg = segments[ui];
936       gboolean islast = segments[ui + 1] == NULL;
937       if (islast && !seg[0])
938         g_error ("invalid test case path: %s", testpath);
939       else if (!seg[0])
940         continue;       /* initial or duplicate slash */
941       else if (!islast)
942         {
943           GTestSuite *csuite = g_test_create_suite (seg);
944           g_test_suite_add_suite (suite, csuite);
945           suite = csuite;
946         }
947       else /* islast */
948         {
949           GTestCase *tc = g_test_create_case (seg, data_size, test_data, data_setup, fixture_test_func, data_teardown);
950           g_test_suite_add (suite, tc);
951         }
952     }
953   g_strfreev (segments);
954 }
955
956 /**
957  * g_test_add_func:
958  * @testpath:   Slash-separated test case path name for the test.
959  * @test_func:  The test function to invoke for this test.
960  *
961  * Create a new test case, similar to g_test_create_case(). However
962  * the test is assumed to use no fixture, and test suites are automatically
963  * created on the fly and added to the root fixture, based on the
964  * slash-separated portions of @testpath.
965  *
966  * Since: 2.16
967  */
968 void
969 g_test_add_func (const char     *testpath,
970                  void          (*test_func) (void))
971 {
972   g_return_if_fail (testpath != NULL);
973   g_return_if_fail (testpath[0] == '/');
974   g_return_if_fail (test_func != NULL);
975   g_test_add_vtable (testpath, 0, NULL, NULL, test_func, NULL);
976 }
977
978 /**
979  * g_test_add_data_func:
980  * @testpath:   Slash-separated test case path name for the test.
981  * @test_data:  Test data argument for the test function.
982  * @test_func:  The test function to invoke for this test.
983  *
984  * Create a new test case, similar to g_test_create_case(). However
985  * the test is assumed to use no fixture, and test suites are automatically
986  * created on the fly and added to the root fixture, based on the
987  * slash-separated portions of @testpath. The @test_data argument
988  * will be passed as first argument to @test_func.
989  *
990  * Since: 2.16
991  */
992 void
993 g_test_add_data_func (const char     *testpath,
994                       gconstpointer   test_data,
995                       void          (*test_func) (gconstpointer))
996 {
997   g_return_if_fail (testpath != NULL);
998   g_return_if_fail (testpath[0] == '/');
999   g_return_if_fail (test_func != NULL);
1000   g_test_add_vtable (testpath, 0, test_data, NULL, (void(*)(void)) test_func, NULL);
1001 }
1002
1003 /**
1004  * g_test_create_suite:
1005  * @suite_name: a name for the suite
1006  *
1007  * Create a new test suite with the name @suite_name.
1008  *
1009  * Returns: A newly allocated #GTestSuite instance.
1010  *
1011  * Since: 2.16
1012  */
1013 GTestSuite*
1014 g_test_create_suite (const char *suite_name)
1015 {
1016   GTestSuite *ts;
1017   g_return_val_if_fail (suite_name != NULL, NULL);
1018   g_return_val_if_fail (strchr (suite_name, '/') == NULL, NULL);
1019   g_return_val_if_fail (suite_name[0] != 0, NULL);
1020   ts = g_slice_new0 (GTestSuite);
1021   ts->name = g_strdup (suite_name);
1022   return ts;
1023 }
1024
1025 /**
1026  * g_test_suite_add:
1027  * @suite: a #GTestSuite
1028  * @test_case: a #GTestCase
1029  *
1030  * Adds @test_case to @suite.
1031  *
1032  * Since: 2.16
1033  */
1034 void
1035 g_test_suite_add (GTestSuite     *suite,
1036                   GTestCase      *test_case)
1037 {
1038   g_return_if_fail (suite != NULL);
1039   g_return_if_fail (test_case != NULL);
1040   suite->cases = g_slist_prepend (suite->cases, test_case);
1041 }
1042
1043 /**
1044  * g_test_suite_add_suite:
1045  * @suite:       a #GTestSuite
1046  * @nestedsuite: another #GTestSuite
1047  *
1048  * Adds @nestedsuite to @suite.
1049  *
1050  * Since: 2.16
1051  */
1052 void
1053 g_test_suite_add_suite (GTestSuite     *suite,
1054                         GTestSuite     *nestedsuite)
1055 {
1056   g_return_if_fail (suite != NULL);
1057   g_return_if_fail (nestedsuite != NULL);
1058   suite->suites = g_slist_prepend (suite->suites, nestedsuite);
1059 }
1060
1061 /**
1062  * g_test_queue_free:
1063  * @gfree_pointer: the pointer to be stored.
1064  *
1065  * Enqueue a pointer to be released with g_free() during the next
1066  * teardown phase. This is equivalent to calling g_test_queue_destroy()
1067  * with a destroy callback of g_free().
1068  *
1069  * Since: 2.16
1070  */
1071 void
1072 g_test_queue_free (gpointer gfree_pointer)
1073 {
1074   if (gfree_pointer)
1075     g_test_queue_destroy (g_free, gfree_pointer);
1076 }
1077
1078 /**
1079  * g_test_queue_destroy:
1080  * @destroy_func:       Destroy callback for teardown phase.
1081  * @destroy_data:       Destroy callback data.
1082  *
1083  * This function enqueus a callback @destroy_func() to be executed
1084  * during the next test case teardown phase. This is most useful
1085  * to auto destruct allocted test resources at the end of a test run.
1086  * Resources are released in reverse queue order, that means enqueueing
1087  * callback A before callback B will cause B() to be called before
1088  * A() during teardown.
1089  *
1090  * Since: 2.16
1091  */
1092 void
1093 g_test_queue_destroy (GDestroyNotify destroy_func,
1094                       gpointer       destroy_data)
1095 {
1096   DestroyEntry *dentry;
1097   g_return_if_fail (destroy_func != NULL);
1098   dentry = g_slice_new0 (DestroyEntry);
1099   dentry->destroy_func = destroy_func;
1100   dentry->destroy_data = destroy_data;
1101   dentry->next = test_destroy_queue;
1102   test_destroy_queue = dentry;
1103 }
1104
1105 static int
1106 test_case_run (GTestCase *tc)
1107 {
1108   gchar *old_name = test_run_name, *old_base = g_strdup (test_uri_base);
1109   test_run_name = g_strconcat (old_name, "/", tc->name, NULL);
1110   if (++test_run_count <= test_skip_count)
1111     g_test_log (G_TEST_LOG_SKIP_CASE, test_run_name, NULL, 0, NULL);
1112   else if (test_run_list)
1113     {
1114       g_print ("%s\n", test_run_name);
1115       g_test_log (G_TEST_LOG_LIST_CASE, test_run_name, NULL, 0, NULL);
1116     }
1117   else
1118     {
1119       GTimer *test_run_timer = g_timer_new();
1120       long double largs[3];
1121       void *fixture;
1122       g_test_log (G_TEST_LOG_START_CASE, test_run_name, NULL, 0, NULL);
1123       test_run_forks = 0;
1124       g_timer_start (test_run_timer);
1125       fixture = tc->fixture_size ? g_malloc0 (tc->fixture_size) : tc->test_data;
1126       test_run_seed (test_run_seedstr);
1127       if (tc->fixture_setup)
1128         tc->fixture_setup (fixture, tc->test_data);
1129       tc->fixture_test (fixture, tc->test_data);
1130       test_trap_clear();
1131       while (test_destroy_queue)
1132         {
1133           DestroyEntry *dentry = test_destroy_queue;
1134           test_destroy_queue = dentry->next;
1135           dentry->destroy_func (dentry->destroy_data);
1136           g_slice_free (DestroyEntry, dentry);
1137         }
1138       if (tc->fixture_teardown)
1139         tc->fixture_teardown (fixture, tc->test_data);
1140       if (tc->fixture_size)
1141         g_free (fixture);
1142       g_timer_stop (test_run_timer);
1143       largs[0] = 0; /* OK */
1144       largs[1] = test_run_forks;
1145       largs[2] = g_timer_elapsed (test_run_timer, NULL);
1146       g_test_log (G_TEST_LOG_STOP_CASE, NULL, NULL, G_N_ELEMENTS (largs), largs);
1147       g_timer_destroy (test_run_timer);
1148     }
1149   g_free (test_run_name);
1150   test_run_name = old_name;
1151   g_free (test_uri_base);
1152   test_uri_base = old_base;
1153   return 0;
1154 }
1155
1156 static int
1157 g_test_run_suite_internal (GTestSuite *suite,
1158                            const char *path)
1159 {
1160   guint n_bad = 0, n_good = 0, bad_suite = 0, l;
1161   gchar *rest, *old_name = test_run_name;
1162   GSList *slist, *reversed;
1163   g_return_val_if_fail (suite != NULL, -1);
1164   while (path[0] == '/')
1165     path++;
1166   l = strlen (path);
1167   rest = strchr (path, '/');
1168   l = rest ? MIN (l, rest - path) : l;
1169   test_run_name = suite->name[0] == 0 ? g_strdup (test_run_name) : g_strconcat (old_name, "/", suite->name, NULL);
1170   reversed = g_slist_reverse (g_slist_copy (suite->cases));
1171   for (slist = reversed; slist; slist = slist->next)
1172     {
1173       GTestCase *tc = slist->data;
1174       guint n = l ? strlen (tc->name) : 0;
1175       if (l == n && strncmp (path, tc->name, n) == 0)
1176         {
1177           n_good++;
1178           n_bad += test_case_run (tc) != 0;
1179         }
1180     }
1181   g_slist_free (reversed);
1182   reversed = g_slist_reverse (g_slist_copy (suite->suites));
1183   for (slist = reversed; slist; slist = slist->next)
1184     {
1185       GTestSuite *ts = slist->data;
1186       guint n = l ? strlen (ts->name) : 0;
1187       if (l == n && strncmp (path, ts->name, n) == 0)
1188         bad_suite += g_test_run_suite_internal (ts, rest ? rest : "") != 0;
1189     }
1190   g_slist_free (reversed);
1191   g_free (test_run_name);
1192   test_run_name = old_name;
1193   return n_bad || bad_suite;
1194 }
1195
1196 /**
1197  * g_test_run_suite:
1198  * @suite: a #GTestSuite
1199  *
1200  * Execute the tests within @suite and all nested #GTestSuites.
1201  * The test suites to be executed are filtered according to
1202  * test path arguments (-p <replaceable>testpath</replaceable>) 
1203  * as parsed by g_test_init().
1204  * g_test_run_suite() or g_test_run() may only be called once
1205  * in a program.
1206  *
1207  * Returns: 0 on success
1208  *
1209  * Since: 2.16
1210  */
1211 int
1212 g_test_run_suite (GTestSuite *suite)
1213 {
1214   guint n_bad = 0;
1215   g_return_val_if_fail (g_test_config_vars->test_initialized, -1);
1216   g_return_val_if_fail (g_test_run_once == TRUE, -1);
1217   g_test_run_once = FALSE;
1218   if (!test_paths)
1219     test_paths = g_slist_prepend (test_paths, "");
1220   while (test_paths)
1221     {
1222       const char *rest, *path = test_paths->data;
1223       guint l, n = strlen (suite->name);
1224       test_paths = g_slist_delete_link (test_paths, test_paths);
1225       while (path[0] == '/')
1226         path++;
1227       if (!n) /* root suite, run unconditionally */
1228         {
1229           n_bad += 0 != g_test_run_suite_internal (suite, path);
1230           continue;
1231         }
1232       /* regular suite, match path */
1233       rest = strchr (path, '/');
1234       l = strlen (path);
1235       l = rest ? MIN (l, rest - path) : l;
1236       if ((!l || l == n) && strncmp (path, suite->name, n) == 0)
1237         n_bad += 0 != g_test_run_suite_internal (suite, rest ? rest : "");
1238     }
1239   return n_bad;
1240 }
1241
1242 static void
1243 gtest_default_log_handler (const gchar    *log_domain,
1244                            GLogLevelFlags  log_level,
1245                            const gchar    *message,
1246                            gpointer        unused_data)
1247 {
1248   const gchar *strv[16];
1249   gchar *msg;
1250   guint i = 0;
1251   if (log_domain)
1252     {
1253       strv[i++] = log_domain;
1254       strv[i++] = "-";
1255     }
1256   if (log_level & G_LOG_FLAG_FATAL)
1257     strv[i++] = "FATAL-";
1258   if (log_level & G_LOG_FLAG_RECURSION)
1259     strv[i++] = "RECURSIVE-";
1260   if (log_level & G_LOG_LEVEL_ERROR)
1261     strv[i++] = "ERROR";
1262   if (log_level & G_LOG_LEVEL_CRITICAL)
1263     strv[i++] = "CRITICAL";
1264   if (log_level & G_LOG_LEVEL_WARNING)
1265     strv[i++] = "WARNING";
1266   if (log_level & G_LOG_LEVEL_MESSAGE)
1267     strv[i++] = "MESSAGE";
1268   if (log_level & G_LOG_LEVEL_INFO)
1269     strv[i++] = "INFO";
1270   if (log_level & G_LOG_LEVEL_DEBUG)
1271     strv[i++] = "DEBUG";
1272   strv[i++] = ": ";
1273   strv[i++] = message;
1274   strv[i++] = NULL;
1275   msg = g_strjoinv ("", (gchar**) strv);
1276   g_test_log (G_TEST_LOG_ERROR, msg, NULL, 0, NULL);
1277   g_log_default_handler (log_domain, log_level, message, unused_data);
1278   g_free (msg);
1279 }
1280
1281 void
1282 g_assertion_message (const char     *domain,
1283                      const char     *file,
1284                      int             line,
1285                      const char     *func,
1286                      const char     *message)
1287 {
1288   char lstr[32];
1289   char *s;
1290   if (!message)
1291     message = "code should not be reached";
1292   g_snprintf (lstr, 32, "%d", line);
1293   s = g_strconcat (domain ? domain : "", domain && domain[0] ? ":" : "",
1294                    "ERROR:", file, ":", lstr, ":",
1295                    func, func[0] ? ":" : "",
1296                    " ", message, NULL);
1297   g_printerr ("**\n%s\n", s);
1298   g_test_log (G_TEST_LOG_ERROR, s, NULL, 0, NULL);
1299   g_free (s);
1300   abort();
1301 }
1302
1303 void
1304 g_assertion_message_expr (const char     *domain,
1305                           const char     *file,
1306                           int             line,
1307                           const char     *func,
1308                           const char     *expr)
1309 {
1310   char *s = g_strconcat ("assertion failed: (", expr, ")", NULL);
1311   g_assertion_message (domain, file, line, func, s);
1312   g_free (s);
1313 }
1314
1315 void
1316 g_assertion_message_cmpnum (const char     *domain,
1317                             const char     *file,
1318                             int             line,
1319                             const char     *func,
1320                             const char     *expr,
1321                             long double     arg1,
1322                             const char     *cmp,
1323                             long double     arg2,
1324                             char            numtype)
1325 {
1326   char *s = NULL;
1327   switch (numtype)
1328     {
1329     case 'i':   s = g_strdup_printf ("assertion failed (%s): (%.0Lf %s %.0Lf)", expr, arg1, cmp, arg2); break;
1330     case 'x':   s = g_strdup_printf ("assertion failed (%s): (0x%08" G_GINT64_MODIFIER "x %s 0x%08" G_GINT64_MODIFIER "x)", expr, (guint64) arg1, cmp, (guint64) arg2); break;
1331     case 'f':   s = g_strdup_printf ("assertion failed (%s): (%.9Lg %s %.9Lg)", expr, arg1, cmp, arg2); break;
1332       /* ideally use: floats=%.7g double=%.17g */
1333     }
1334   g_assertion_message (domain, file, line, func, s);
1335   g_free (s);
1336 }
1337
1338 void
1339 g_assertion_message_cmpstr (const char     *domain,
1340                             const char     *file,
1341                             int             line,
1342                             const char     *func,
1343                             const char     *expr,
1344                             const char     *arg1,
1345                             const char     *cmp,
1346                             const char     *arg2)
1347 {
1348   char *a1, *a2, *s, *t1 = NULL, *t2 = NULL;
1349   a1 = arg1 ? g_strconcat ("\"", t1 = g_strescape (arg1, NULL), "\"", NULL) : g_strdup ("NULL");
1350   a2 = arg2 ? g_strconcat ("\"", t2 = g_strescape (arg2, NULL), "\"", NULL) : g_strdup ("NULL");
1351   g_free (t1);
1352   g_free (t2);
1353   s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2);
1354   g_free (a1);
1355   g_free (a2);
1356   g_assertion_message (domain, file, line, func, s);
1357   g_free (s);
1358 }
1359
1360 /**
1361  * g_strcmp0:
1362  * @str1: a C string or %NULL
1363  * @str2: another C string or %NULL
1364  *
1365  * Compares @str1 and @str2 like strcmp(). Handles %NULL strings gracefully.
1366  *
1367  * Returns: -1, 0 or 1, if @str1 is <, == or > than @str2.
1368  *
1369  * Since: 2.16
1370  */
1371 int
1372 g_strcmp0 (const char     *str1,
1373            const char     *str2)
1374 {
1375   if (!str1)
1376     return -(str1 != str2);
1377   if (!str2)
1378     return str1 != str2;
1379   return strcmp (str1, str2);
1380 }
1381
1382 #ifdef G_OS_UNIX
1383 static int /* 0 on success */
1384 kill_child (int  pid,
1385             int *status,
1386             int  patience)
1387 {
1388   int wr;
1389   if (patience >= 3)    /* try graceful reap */
1390     {
1391       if (waitpid (pid, status, WNOHANG) > 0)
1392         return 0;
1393     }
1394   if (patience >= 2)    /* try SIGHUP */
1395     {
1396       kill (pid, SIGHUP);
1397       if (waitpid (pid, status, WNOHANG) > 0)
1398         return 0;
1399       g_usleep (20 * 1000); /* give it some scheduling/shutdown time */
1400       if (waitpid (pid, status, WNOHANG) > 0)
1401         return 0;
1402       g_usleep (50 * 1000); /* give it some scheduling/shutdown time */
1403       if (waitpid (pid, status, WNOHANG) > 0)
1404         return 0;
1405       g_usleep (100 * 1000); /* give it some scheduling/shutdown time */
1406       if (waitpid (pid, status, WNOHANG) > 0)
1407         return 0;
1408     }
1409   if (patience >= 1)    /* try SIGTERM */
1410     {
1411       kill (pid, SIGTERM);
1412       if (waitpid (pid, status, WNOHANG) > 0)
1413         return 0;
1414       g_usleep (200 * 1000); /* give it some scheduling/shutdown time */
1415       if (waitpid (pid, status, WNOHANG) > 0)
1416         return 0;
1417       g_usleep (400 * 1000); /* give it some scheduling/shutdown time */
1418       if (waitpid (pid, status, WNOHANG) > 0)
1419         return 0;
1420     }
1421   /* finish it off */
1422   kill (pid, SIGKILL);
1423   do
1424     wr = waitpid (pid, status, 0);
1425   while (wr < 0 && errno == EINTR);
1426   return wr;
1427 }
1428 #endif
1429
1430 static inline int
1431 g_string_must_read (GString *gstring,
1432                     int      fd)
1433 {
1434 #define STRING_BUFFER_SIZE     4096
1435   char buf[STRING_BUFFER_SIZE];
1436   gssize bytes;
1437  again:
1438   bytes = read (fd, buf, sizeof (buf));
1439   if (bytes == 0)
1440     return 0; /* EOF, calling this function assumes data is available */
1441   else if (bytes > 0)
1442     {
1443       g_string_append_len (gstring, buf, bytes);
1444       return 1;
1445     }
1446   else if (bytes < 0 && errno == EINTR)
1447     goto again;
1448   else /* bytes < 0 */
1449     {
1450       g_warning ("failed to read() from child process (%d): %s", test_trap_last_pid, g_strerror (errno));
1451       return 1; /* ignore error after warning */
1452     }
1453 }
1454
1455 static inline void
1456 g_string_write_out (GString *gstring,
1457                     int      outfd,
1458                     int     *stringpos)
1459 {
1460   if (*stringpos < gstring->len)
1461     {
1462       int r;
1463       do
1464         r = write (outfd, gstring->str + *stringpos, gstring->len - *stringpos);
1465       while (r < 0 && errno == EINTR);
1466       *stringpos += MAX (r, 0);
1467     }
1468 }
1469
1470 static void
1471 test_trap_clear (void)
1472 {
1473   test_trap_last_status = 0;
1474   test_trap_last_pid = 0;
1475   g_free (test_trap_last_stdout);
1476   test_trap_last_stdout = NULL;
1477   g_free (test_trap_last_stderr);
1478   test_trap_last_stderr = NULL;
1479 }
1480
1481 #ifdef G_OS_UNIX
1482
1483 static int
1484 sane_dup2 (int fd1,
1485            int fd2)
1486 {
1487   int ret;
1488   do
1489     ret = dup2 (fd1, fd2);
1490   while (ret < 0 && errno == EINTR);
1491   return ret;
1492 }
1493
1494 static guint64
1495 test_time_stamp (void)
1496 {
1497   GTimeVal tv;
1498   guint64 stamp;
1499   g_get_current_time (&tv);
1500   stamp = tv.tv_sec;
1501   stamp = stamp * 1000000 + tv.tv_usec;
1502   return stamp;
1503 }
1504
1505 #endif
1506
1507 /**
1508  * g_test_trap_fork:
1509  * @usec_timeout:    Timeout for the forked test in micro seconds.
1510  * @test_trap_flags: Flags to modify forking behaviour.
1511  *
1512  * Fork the current test program to execute a test case that might
1513  * not return or that might abort. The forked test case is aborted
1514  * and considered failing if its run time exceeds @usec_timeout.
1515  *
1516  * The forking behavior can be configured with the #GTestTrapFlags flags.
1517  *
1518  * In the following example, the test code forks, the forked child
1519  * process produces some sample output and exits successfully.
1520  * The forking parent process then asserts successful child program
1521  * termination and validates child program outputs.
1522  *
1523  * |[
1524  *   static void
1525  *   test_fork_patterns (void)
1526  *   {
1527  *     if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
1528  *       {
1529  *         g_print ("some stdout text: somagic17\n");
1530  *         g_printerr ("some stderr text: semagic43\n");
1531  *         exit (0); /&ast; successful test run &ast;/
1532  *       }
1533  *     g_test_trap_assert_passed();
1534  *     g_test_trap_assert_stdout ("*somagic17*");
1535  *     g_test_trap_assert_stderr ("*semagic43*");
1536  *   }
1537  * ]|
1538  *
1539  * This function is implemented only on Unix platforms.
1540  *
1541  * Returns: %TRUE for the forked child and %FALSE for the executing parent process.
1542  *
1543  * Since: 2.16
1544  */
1545 gboolean
1546 g_test_trap_fork (guint64        usec_timeout,
1547                   GTestTrapFlags test_trap_flags)
1548 {
1549 #ifdef G_OS_UNIX
1550   gboolean pass_on_forked_log = FALSE;
1551   int stdout_pipe[2] = { -1, -1 };
1552   int stderr_pipe[2] = { -1, -1 };
1553   int stdtst_pipe[2] = { -1, -1 };
1554   test_trap_clear();
1555   if (pipe (stdout_pipe) < 0 || pipe (stderr_pipe) < 0 || pipe (stdtst_pipe) < 0)
1556     g_error ("failed to create pipes to fork test program: %s", g_strerror (errno));
1557   signal (SIGCHLD, SIG_DFL);
1558   test_trap_last_pid = fork ();
1559   if (test_trap_last_pid < 0)
1560     g_error ("failed to fork test program: %s", g_strerror (errno));
1561   if (test_trap_last_pid == 0)  /* child */
1562     {
1563       int fd0 = -1;
1564       close (stdout_pipe[0]);
1565       close (stderr_pipe[0]);
1566       close (stdtst_pipe[0]);
1567       if (!(test_trap_flags & G_TEST_TRAP_INHERIT_STDIN))
1568         fd0 = open ("/dev/null", O_RDONLY);
1569       if (sane_dup2 (stdout_pipe[1], 1) < 0 || sane_dup2 (stderr_pipe[1], 2) < 0 || (fd0 >= 0 && sane_dup2 (fd0, 0) < 0))
1570         g_error ("failed to dup2() in forked test program: %s", g_strerror (errno));
1571       if (fd0 >= 3)
1572         close (fd0);
1573       if (stdout_pipe[1] >= 3)
1574         close (stdout_pipe[1]);
1575       if (stderr_pipe[1] >= 3)
1576         close (stderr_pipe[1]);
1577       test_log_fd = stdtst_pipe[1];
1578       return TRUE;
1579     }
1580   else                          /* parent */
1581     {
1582       GString *sout = g_string_new (NULL);
1583       GString *serr = g_string_new (NULL);
1584       guint64 sstamp;
1585       int soutpos = 0, serrpos = 0, wr, need_wait = TRUE;
1586       test_run_forks++;
1587       close (stdout_pipe[1]);
1588       close (stderr_pipe[1]);
1589       close (stdtst_pipe[1]);
1590       sstamp = test_time_stamp();
1591       /* read data until we get EOF on all pipes */
1592       while (stdout_pipe[0] >= 0 || stderr_pipe[0] >= 0 || stdtst_pipe[0] > 0)
1593         {
1594           fd_set fds;
1595           struct timeval tv;
1596           int ret;
1597           FD_ZERO (&fds);
1598           if (stdout_pipe[0] >= 0)
1599             FD_SET (stdout_pipe[0], &fds);
1600           if (stderr_pipe[0] >= 0)
1601             FD_SET (stderr_pipe[0], &fds);
1602           if (stdtst_pipe[0] >= 0)
1603             FD_SET (stdtst_pipe[0], &fds);
1604           tv.tv_sec = 0;
1605           tv.tv_usec = MIN (usec_timeout ? usec_timeout : 1000000, 100 * 1000); /* sleep at most 0.5 seconds to catch clock skews, etc. */
1606           ret = select (MAX (MAX (stdout_pipe[0], stderr_pipe[0]), stdtst_pipe[0]) + 1, &fds, NULL, NULL, &tv);
1607           if (ret < 0 && errno != EINTR)
1608             {
1609               g_warning ("Unexpected error in select() while reading from child process (%d): %s", test_trap_last_pid, g_strerror (errno));
1610               break;
1611             }
1612           if (stdout_pipe[0] >= 0 && FD_ISSET (stdout_pipe[0], &fds) &&
1613               g_string_must_read (sout, stdout_pipe[0]) == 0)
1614             {
1615               close (stdout_pipe[0]);
1616               stdout_pipe[0] = -1;
1617             }
1618           if (stderr_pipe[0] >= 0 && FD_ISSET (stderr_pipe[0], &fds) &&
1619               g_string_must_read (serr, stderr_pipe[0]) == 0)
1620             {
1621               close (stderr_pipe[0]);
1622               stderr_pipe[0] = -1;
1623             }
1624           if (stdtst_pipe[0] >= 0 && FD_ISSET (stdtst_pipe[0], &fds))
1625             {
1626               guint8 buffer[4096];
1627               gint l, r = read (stdtst_pipe[0], buffer, sizeof (buffer));
1628               if (r > 0 && test_log_fd > 0)
1629                 do
1630                   l = write (pass_on_forked_log ? test_log_fd : -1, buffer, r);
1631                 while (l < 0 && errno == EINTR);
1632               if (r == 0 || (r < 0 && errno != EINTR && errno != EAGAIN))
1633                 {
1634                   close (stdtst_pipe[0]);
1635                   stdtst_pipe[0] = -1;
1636                 }
1637             }
1638           if (!(test_trap_flags & G_TEST_TRAP_SILENCE_STDOUT))
1639             g_string_write_out (sout, 1, &soutpos);
1640           if (!(test_trap_flags & G_TEST_TRAP_SILENCE_STDERR))
1641             g_string_write_out (serr, 2, &serrpos);
1642           if (usec_timeout)
1643             {
1644               guint64 nstamp = test_time_stamp();
1645               int status = 0;
1646               sstamp = MIN (sstamp, nstamp); /* guard against backwards clock skews */
1647               if (usec_timeout < nstamp - sstamp)
1648                 {
1649                   /* timeout reached, need to abort the child now */
1650                   kill_child (test_trap_last_pid, &status, 3);
1651                   test_trap_last_status = 1024; /* timeout */
1652                   if (0 && WIFSIGNALED (status))
1653                     g_printerr ("%s: child timed out and received: %s\n", G_STRFUNC, g_strsignal (WTERMSIG (status)));
1654                   need_wait = FALSE;
1655                   break;
1656                 }
1657             }
1658         }
1659       close (stdout_pipe[0]);
1660       close (stderr_pipe[0]);
1661       close (stdtst_pipe[0]);
1662       if (need_wait)
1663         {
1664           int status = 0;
1665           do
1666             wr = waitpid (test_trap_last_pid, &status, 0);
1667           while (wr < 0 && errno == EINTR);
1668           if (WIFEXITED (status)) /* normal exit */
1669             test_trap_last_status = WEXITSTATUS (status); /* 0..255 */
1670           else if (WIFSIGNALED (status))
1671             test_trap_last_status = (WTERMSIG (status) << 12); /* signalled */
1672           else /* WCOREDUMP (status) */
1673             test_trap_last_status = 512; /* coredump */
1674         }
1675       test_trap_last_stdout = g_string_free (sout, FALSE);
1676       test_trap_last_stderr = g_string_free (serr, FALSE);
1677       return FALSE;
1678     }
1679 #else
1680   g_message ("Not implemented: g_test_trap_fork");
1681
1682   return FALSE;
1683 #endif
1684 }
1685
1686 /**
1687  * g_test_trap_has_passed:
1688  *
1689  * Check the result of the last g_test_trap_fork() call.
1690  *
1691  * Returns: %TRUE if the last forked child terminated successfully.
1692  *
1693  * Since: 2.16
1694  */
1695 gboolean
1696 g_test_trap_has_passed (void)
1697 {
1698   return test_trap_last_status == 0; /* exit_status == 0 && !signal && !coredump */
1699 }
1700
1701 /**
1702  * g_test_trap_reached_timeout:
1703  *
1704  * Check the result of the last g_test_trap_fork() call.
1705  *
1706  * Returns: %TRUE if the last forked child got killed due to a fork timeout.
1707  *
1708  * Since: 2.16
1709  */
1710 gboolean
1711 g_test_trap_reached_timeout (void)
1712 {
1713   return 0 != (test_trap_last_status & 1024); /* timeout flag */
1714 }
1715
1716 void
1717 g_test_trap_assertions (const char     *domain,
1718                         const char     *file,
1719                         int             line,
1720                         const char     *func,
1721                         guint64         assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
1722                         const char     *pattern)
1723 {
1724 #ifdef G_OS_UNIX
1725   gboolean must_pass = assertion_flags == 0;
1726   gboolean must_fail = assertion_flags == 1;
1727   gboolean match_result = 0 == (assertion_flags & 1);
1728   const char *stdout_pattern = (assertion_flags & 2) ? pattern : NULL;
1729   const char *stderr_pattern = (assertion_flags & 4) ? pattern : NULL;
1730   const char *match_error = match_result ? "failed to match" : "contains invalid match";
1731   if (test_trap_last_pid == 0)
1732     g_error ("child process failed to exit after g_test_trap_fork() and before g_test_trap_assert*()");
1733   if (must_pass && !g_test_trap_has_passed())
1734     {
1735       char *msg = g_strdup_printf ("child process (%d) of test trap failed unexpectedly", test_trap_last_pid);
1736       g_assertion_message (domain, file, line, func, msg);
1737       g_free (msg);
1738     }
1739   if (must_fail && g_test_trap_has_passed())
1740     {
1741       char *msg = g_strdup_printf ("child process (%d) did not fail as expected", test_trap_last_pid);
1742       g_assertion_message (domain, file, line, func, msg);
1743       g_free (msg);
1744     }
1745   if (stdout_pattern && match_result == !g_pattern_match_simple (stdout_pattern, test_trap_last_stdout))
1746     {
1747       char *msg = g_strdup_printf ("stdout of child process (%d) %s: %s", test_trap_last_pid, match_error, stdout_pattern);
1748       g_assertion_message (domain, file, line, func, msg);
1749       g_free (msg);
1750     }
1751   if (stderr_pattern && match_result == !g_pattern_match_simple (stderr_pattern, test_trap_last_stderr))
1752     {
1753       char *msg = g_strdup_printf ("stderr of child process (%d) %s: %s", test_trap_last_pid, match_error, stderr_pattern);
1754       g_assertion_message (domain, file, line, func, msg);
1755       g_free (msg);
1756     }
1757 #endif
1758 }
1759
1760 static void
1761 gstring_overwrite_int (GString *gstring,
1762                        guint    pos,
1763                        guint32  vuint)
1764 {
1765   vuint = g_htonl (vuint);
1766   g_string_overwrite_len (gstring, pos, (const gchar*) &vuint, 4);
1767 }
1768
1769 static void
1770 gstring_append_int (GString *gstring,
1771                     guint32  vuint)
1772 {
1773   vuint = g_htonl (vuint);
1774   g_string_append_len (gstring, (const gchar*) &vuint, 4);
1775 }
1776
1777 static void
1778 gstring_append_double (GString *gstring,
1779                        double   vdouble)
1780 {
1781   union { double vdouble; guint64 vuint64; } u;
1782   u.vdouble = vdouble;
1783   u.vuint64 = GUINT64_TO_BE (u.vuint64);
1784   g_string_append_len (gstring, (const gchar*) &u.vuint64, 8);
1785 }
1786
1787 static guint8*
1788 g_test_log_dump (GTestLogMsg *msg,
1789                  guint       *len)
1790 {
1791   GString *gstring = g_string_sized_new (1024);
1792   guint ui;
1793   gstring_append_int (gstring, 0);              /* message length */
1794   gstring_append_int (gstring, msg->log_type);
1795   gstring_append_int (gstring, msg->n_strings);
1796   gstring_append_int (gstring, msg->n_nums);
1797   gstring_append_int (gstring, 0);      /* reserved */
1798   for (ui = 0; ui < msg->n_strings; ui++)
1799     {
1800       guint l = strlen (msg->strings[ui]);
1801       gstring_append_int (gstring, l);
1802       g_string_append_len (gstring, msg->strings[ui], l);
1803     }
1804   for (ui = 0; ui < msg->n_nums; ui++)
1805     gstring_append_double (gstring, msg->nums[ui]);
1806   *len = gstring->len;
1807   gstring_overwrite_int (gstring, 0, *len);     /* message length */
1808   return (guint8*) g_string_free (gstring, FALSE);
1809 }
1810
1811 static inline long double
1812 net_double (const gchar **ipointer)
1813 {
1814   union { guint64 vuint64; double vdouble; } u;
1815   guint64 aligned_int64;
1816   memcpy (&aligned_int64, *ipointer, 8);
1817   *ipointer += 8;
1818   u.vuint64 = GUINT64_FROM_BE (aligned_int64);
1819   return u.vdouble;
1820 }
1821
1822 static inline guint32
1823 net_int (const gchar **ipointer)
1824 {
1825   guint32 aligned_int;
1826   memcpy (&aligned_int, *ipointer, 4);
1827   *ipointer += 4;
1828   return g_ntohl (aligned_int);
1829 }
1830
1831 static gboolean
1832 g_test_log_extract (GTestLogBuffer *tbuffer)
1833 {
1834   const gchar *p = tbuffer->data->str;
1835   GTestLogMsg msg;
1836   guint mlength;
1837   if (tbuffer->data->len < 4 * 5)
1838     return FALSE;
1839   mlength = net_int (&p);
1840   if (tbuffer->data->len < mlength)
1841     return FALSE;
1842   msg.log_type = net_int (&p);
1843   msg.n_strings = net_int (&p);
1844   msg.n_nums = net_int (&p);
1845   if (net_int (&p) == 0)
1846     {
1847       guint ui;
1848       msg.strings = g_new0 (gchar*, msg.n_strings + 1);
1849       msg.nums = g_new0 (long double, msg.n_nums);
1850       for (ui = 0; ui < msg.n_strings; ui++)
1851         {
1852           guint sl = net_int (&p);
1853           msg.strings[ui] = g_strndup (p, sl);
1854           p += sl;
1855         }
1856       for (ui = 0; ui < msg.n_nums; ui++)
1857         msg.nums[ui] = net_double (&p);
1858       if (p <= tbuffer->data->str + mlength)
1859         {
1860           g_string_erase (tbuffer->data, 0, mlength);
1861           tbuffer->msgs = g_slist_prepend (tbuffer->msgs, g_memdup (&msg, sizeof (msg)));
1862           return TRUE;
1863         }
1864     }
1865   g_free (msg.nums);
1866   g_strfreev (msg.strings);
1867   g_error ("corrupt log stream from test program");
1868   return FALSE;
1869 }
1870
1871 /**
1872  * g_test_log_buffer_new:
1873  *
1874  * Internal function for gtester to decode test log messages, no ABI guarantees provided.
1875  */
1876 GTestLogBuffer*
1877 g_test_log_buffer_new (void)
1878 {
1879   GTestLogBuffer *tb = g_new0 (GTestLogBuffer, 1);
1880   tb->data = g_string_sized_new (1024);
1881   return tb;
1882 }
1883
1884 /**
1885  * g_test_log_buffer_free
1886  *
1887  * Internal function for gtester to free test log messages, no ABI guarantees provided.
1888  */
1889 void
1890 g_test_log_buffer_free (GTestLogBuffer *tbuffer)
1891 {
1892   g_return_if_fail (tbuffer != NULL);
1893   while (tbuffer->msgs)
1894     g_test_log_msg_free (g_test_log_buffer_pop (tbuffer));
1895   g_string_free (tbuffer->data, TRUE);
1896   g_free (tbuffer);
1897 }
1898
1899 /**
1900  * g_test_log_buffer_push
1901  *
1902  * Internal function for gtester to decode test log messages, no ABI guarantees provided.
1903  */
1904 void
1905 g_test_log_buffer_push (GTestLogBuffer *tbuffer,
1906                         guint           n_bytes,
1907                         const guint8   *bytes)
1908 {
1909   g_return_if_fail (tbuffer != NULL);
1910   if (n_bytes)
1911     {
1912       gboolean more_messages;
1913       g_return_if_fail (bytes != NULL);
1914       g_string_append_len (tbuffer->data, (const gchar*) bytes, n_bytes);
1915       do
1916         more_messages = g_test_log_extract (tbuffer);
1917       while (more_messages);
1918     }
1919 }
1920
1921 /**
1922  * g_test_log_buffer_pop:
1923  *
1924  * Internal function for gtester to retrieve test log messages, no ABI guarantees provided.
1925  */
1926 GTestLogMsg*
1927 g_test_log_buffer_pop (GTestLogBuffer *tbuffer)
1928 {
1929   GTestLogMsg *msg = NULL;
1930   g_return_val_if_fail (tbuffer != NULL, NULL);
1931   if (tbuffer->msgs)
1932     {
1933       GSList *slist = g_slist_last (tbuffer->msgs);
1934       msg = slist->data;
1935       tbuffer->msgs = g_slist_delete_link (tbuffer->msgs, slist);
1936     }
1937   return msg;
1938 }
1939
1940 /**
1941  * g_test_log_msg_free:
1942  *
1943  * Internal function for gtester to free test log messages, no ABI guarantees provided.
1944  */
1945 void
1946 g_test_log_msg_free (GTestLogMsg *tmsg)
1947 {
1948   g_return_if_fail (tmsg != NULL);
1949   g_strfreev (tmsg->strings);
1950   g_free (tmsg->nums);
1951   g_free (tmsg);
1952 }
1953
1954 /* --- macros docs START --- */
1955 /**
1956  * g_test_add:
1957  * @testpath:  The test path for a new test case.
1958  * @Fixture:   The type of a fixture data structure.
1959  * @tdata:     Data argument for the test functions.
1960  * @fsetup:    The function to set up the fixture data.
1961  * @ftest:     The actual test function.
1962  * @fteardown: The function to tear down the fixture data.
1963  *
1964  * Hook up a new test case at @testpath, similar to g_test_add_func().
1965  * A fixture data structure with setup and teardown function may be provided
1966  * though, similar to g_test_create_case().
1967  * g_test_add() is implemented as a macro, so that the fsetup(), ftest() and
1968  * fteardown() callbacks can expect a @Fixture pointer as first argument in
1969  * a type safe manner.
1970  *
1971  * Since: 2.16
1972  **/
1973 /* --- macros docs END --- */
1974
1975 #define __G_TEST_UTILS_C__
1976 #include "galiasdef.c"