Blame glib/gtestutils.c

Packit ae235b
/* GLib testing utilities
Packit ae235b
 * Copyright (C) 2007 Imendio AB
Packit ae235b
 * Authors: Tim Janik, Sven Herzberg
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "gfileutils.h"
Packit ae235b
Packit ae235b
#include <sys/types.h>
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <sys/wait.h>
Packit ae235b
#include <sys/time.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <unistd.h>
Packit ae235b
#include <glib/gstdio.h>
Packit ae235b
#endif
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdlib.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#ifdef HAVE_SYS_RESOURCE_H
Packit ae235b
#include <sys/resource.h>
Packit ae235b
#endif
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <io.h>
Packit ae235b
#include <windows.h>
Packit ae235b
#endif
Packit ae235b
#include <errno.h>
Packit ae235b
#include <signal.h>
Packit ae235b
#ifdef HAVE_SYS_SELECT_H
Packit ae235b
#include <sys/select.h>
Packit ae235b
#endif /* HAVE_SYS_SELECT_H */
Packit ae235b
Packit ae235b
#include "gmain.h"
Packit ae235b
#include "gpattern.h"
Packit ae235b
#include "grand.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gtimer.h"
Packit ae235b
#include "gslice.h"
Packit ae235b
#include "gspawn.h"
Packit ae235b
#include "glib-private.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:testing
Packit ae235b
 * @title: Testing
Packit ae235b
 * @short_description: a test framework
Packit ae235b
 * @see_also: [gtester][gtester], [gtester-report][gtester-report]
Packit ae235b
 *
Packit ae235b
 * GLib provides a framework for writing and maintaining unit tests
Packit ae235b
 * in parallel to the code they are testing. The API is designed according
Packit ae235b
 * to established concepts found in the other test frameworks (JUnit, NUnit,
Packit ae235b
 * RUnit), which in turn is based on smalltalk unit testing concepts.
Packit ae235b
 *
Packit ae235b
 * - Test case: Tests (test methods) are grouped together with their
Packit ae235b
 *   fixture into test cases.
Packit ae235b
 *
Packit ae235b
 * - Fixture: A test fixture consists of fixture data and setup and
Packit ae235b
 *   teardown methods to establish the environment for the test
Packit ae235b
 *   functions. We use fresh fixtures, i.e. fixtures are newly set
Packit ae235b
 *   up and torn down around each test invocation to avoid dependencies
Packit ae235b
 *   between tests.
Packit ae235b
 *
Packit ae235b
 * - Test suite: Test cases can be grouped into test suites, to allow
Packit ae235b
 *   subsets of the available tests to be run. Test suites can be
Packit ae235b
 *   grouped into other test suites as well.
Packit ae235b
 *
Packit ae235b
 * The API is designed to handle creation and registration of test suites
Packit ae235b
 * and test cases implicitly. A simple call like
Packit ae235b
 * |[ 
Packit ae235b
 *   g_test_add_func ("/misc/assertions", test_assertions);
Packit ae235b
 * ]|
Packit ae235b
 * creates a test suite called "misc" with a single test case named
Packit ae235b
 * "assertions", which consists of running the test_assertions function.
Packit ae235b
 *
Packit ae235b
 * In addition to the traditional g_assert(), the test framework provides
Packit ae235b
 * an extended set of assertions for comparisons: g_assert_cmpfloat(),
Packit ae235b
 * g_assert_cmpint(), g_assert_cmpuint(), g_assert_cmphex(),
Packit ae235b
 * g_assert_cmpstr(), and g_assert_cmpmem(). The advantage of these
Packit ae235b
 * variants over plain g_assert() is that the assertion messages can be
Packit ae235b
 * more elaborate, and include the values of the compared entities.
Packit ae235b
 *
Packit ae235b
 * A full example of creating a test suite with two tests using fixtures:
Packit ae235b
 * |[
Packit ae235b
 * #include <glib.h>
Packit ae235b
 * #include <locale.h>
Packit ae235b
 *
Packit ae235b
 * typedef struct {
Packit ae235b
 *   MyObject *obj;
Packit ae235b
 *   OtherObject *helper;
Packit ae235b
 * } MyObjectFixture;
Packit ae235b
 *
Packit ae235b
 * static void
Packit ae235b
 * my_object_fixture_set_up (MyObjectFixture *fixture,
Packit ae235b
 *                           gconstpointer user_data)
Packit ae235b
 * {
Packit ae235b
 *   fixture->obj = my_object_new ();
Packit ae235b
 *   my_object_set_prop1 (fixture->obj, "some-value");
Packit ae235b
 *   my_object_do_some_complex_setup (fixture->obj, user_data);
Packit ae235b
 *
Packit ae235b
 *   fixture->helper = other_object_new ();
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * static void
Packit ae235b
 * my_object_fixture_tear_down (MyObjectFixture *fixture,
Packit ae235b
 *                              gconstpointer user_data)
Packit ae235b
 * {
Packit ae235b
 *   g_clear_object (&fixture->helper);
Packit ae235b
 *   g_clear_object (&fixture->obj);
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * static void
Packit ae235b
 * test_my_object_test1 (MyObjectFixture *fixture,
Packit ae235b
 *                       gconstpointer user_data)
Packit ae235b
 * {
Packit ae235b
 *   g_assert_cmpstr (my_object_get_property (fixture->obj), ==, "initial-value");
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * static void
Packit ae235b
 * test_my_object_test2 (MyObjectFixture *fixture,
Packit ae235b
 *                       gconstpointer user_data)
Packit ae235b
 * {
Packit ae235b
 *   my_object_do_some_work_using_helper (fixture->obj, fixture->helper);
Packit ae235b
 *   g_assert_cmpstr (my_object_get_property (fixture->obj), ==, "updated-value");
Packit ae235b
 * }
Packit ae235b
 *
Packit ae235b
 * int
Packit ae235b
 * main (int argc, char *argv[])
Packit ae235b
 * {
Packit ae235b
 *   setlocale (LC_ALL, "");
Packit ae235b
 *
Packit ae235b
 *   g_test_init (&argc, &argv, NULL);
Packit ae235b
 *   g_test_bug_base ("http://bugzilla.gnome.org/show_bug.cgi?id=");
Packit ae235b
 *
Packit ae235b
 *   // Define the tests.
Packit ae235b
 *   g_test_add ("/my-object/test1", MyObjectFixture, "some-user-data",
Packit ae235b
 *               my_object_fixture_set_up, test_my_object_test1,
Packit ae235b
 *               my_object_fixture_tear_down);
Packit ae235b
 *   g_test_add ("/my-object/test2", MyObjectFixture, "some-user-data",
Packit ae235b
 *               my_object_fixture_set_up, test_my_object_test2,
Packit ae235b
 *               my_object_fixture_tear_down);
Packit ae235b
 *
Packit ae235b
 *   return g_test_run ();
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * ### Integrating GTest in your project
Packit ae235b
 *
Packit ae235b
 * If you are using the [Meson](http://mesonbuild.com) build system, you will
Packit ae235b
 * typically use the provided `test()` primitive to call the test binaries,
Packit ae235b
 * e.g.:
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 *   test(
Packit ae235b
 *     'foo',
Packit ae235b
 *     executable('foo', 'foo.c', dependencies: deps),
Packit ae235b
 *     env: [
Packit ae235b
 *       'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()),
Packit ae235b
 *       'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()),
Packit ae235b
 *     ],
Packit ae235b
 *   )
Packit ae235b
 *
Packit ae235b
 *   test(
Packit ae235b
 *     'bar',
Packit ae235b
 *     executable('bar', 'bar.c', dependencies: deps),
Packit ae235b
 *     env: [
Packit ae235b
 *       'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()),
Packit ae235b
 *       'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()),
Packit ae235b
 *     ],
Packit ae235b
 *   )
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * If you are using Autotools, you're strongly encouraged to use the Automake
Packit ae235b
 * [TAP](https://testanything.org/) harness; GLib provides template files for
Packit ae235b
 * easily integrating with it:
Packit ae235b
 *
Packit ae235b
 *   - [glib-tap.mk](https://git.gnome.org/browse/glib/tree/glib-tap.mk)
Packit ae235b
 *   - [tap-test](https://git.gnome.org/browse/glib/tree/tap-test)
Packit ae235b
 *   - [tap-driver.sh](https://git.gnome.org/browse/glib/tree/tap-driver.sh)
Packit ae235b
 *
Packit ae235b
 * You can copy these files in your own project's root directory, and then
Packit ae235b
 * set up your `Makefile.am` file to reference them, for instance:
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 * include $(top_srcdir)/glib-tap.mk
Packit ae235b
 *
Packit ae235b
 * # test binaries
Packit ae235b
 * test_programs = \
Packit ae235b
 *   foo \
Packit ae235b
 *   bar
Packit ae235b
 *
Packit ae235b
 * # data distributed in the tarball
Packit ae235b
 * dist_test_data = \
Packit ae235b
 *   foo.data.txt \
Packit ae235b
 *   bar.data.txt
Packit ae235b
 *
Packit ae235b
 * # data not distributed in the tarball
Packit ae235b
 * test_data = \
Packit ae235b
 *   blah.data.txt
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Make sure to distribute the TAP files, using something like the following
Packit ae235b
 * in your top-level `Makefile.am`:
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 * EXTRA_DIST += \
Packit ae235b
 *   tap-driver.sh \
Packit ae235b
 *   tap-test
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * `glib-tap.mk` will be distributed implicitly due to being included in a
Packit ae235b
 * `Makefile.am`. All three files should be added to version control.
Packit ae235b
 *
Packit ae235b
 * If you don't have access to the Autotools TAP harness, you can use the
Packit ae235b
 * [gtester][gtester] and [gtester-report][gtester-report] tools, and use
Packit ae235b
 * the [glib.mk](https://git.gnome.org/browse/glib/tree/glib.mk) Automake
Packit ae235b
 * template provided by GLib.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_initialized:
Packit ae235b
 *
Packit ae235b
 * Returns %TRUE if g_test_init() has been called.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if g_test_init() has been called.
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_quick:
Packit ae235b
 *
Packit ae235b
 * Returns %TRUE if tests are run in quick mode.
Packit ae235b
 * Exactly one of g_test_quick() and g_test_slow() is active in any run;
Packit ae235b
 * there is no "medium speed".
Packit ae235b
 *
Packit ae235b
 * By default, tests are run in quick mode. In tests that use
Packit ae235b
 * g_test_init(), the options `-m quick`, `-m slow` and `-m thorough`
Packit ae235b
 * can be used to change this.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if in quick mode
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_slow:
Packit ae235b
 *
Packit ae235b
 * Returns %TRUE if tests are run in slow mode.
Packit ae235b
 * Exactly one of g_test_quick() and g_test_slow() is active in any run;
Packit ae235b
 * there is no "medium speed".
Packit ae235b
 *
Packit ae235b
 * By default, tests are run in quick mode. In tests that use
Packit ae235b
 * g_test_init(), the options `-m quick`, `-m slow` and `-m thorough`
Packit ae235b
 * can be used to change this.
Packit ae235b
 *
Packit ae235b
 * Returns: the opposite of g_test_quick()
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_thorough:
Packit ae235b
 *
Packit ae235b
 * Returns %TRUE if tests are run in thorough mode, equivalent to
Packit ae235b
 * g_test_slow().
Packit ae235b
 *
Packit ae235b
 * By default, tests are run in quick mode. In tests that use
Packit ae235b
 * g_test_init(), the options `-m quick`, `-m slow` and `-m thorough`
Packit ae235b
 * can be used to change this.
Packit ae235b
 *
Packit ae235b
 * Returns: the same thing as g_test_slow()
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_perf:
Packit ae235b
 *
Packit ae235b
 * Returns %TRUE if tests are run in performance mode.
Packit ae235b
 *
Packit ae235b
 * By default, tests are run in quick mode. In tests that use
Packit ae235b
 * g_test_init(), the option `-m perf` enables performance tests, while
Packit ae235b
 * `-m quick` disables them.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if in performance mode
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_undefined:
Packit ae235b
 *
Packit ae235b
 * Returns %TRUE if tests may provoke assertions and other formally-undefined
Packit ae235b
 * behaviour, to verify that appropriate warnings are given. It might, in some
Packit ae235b
 * cases, be useful to turn this off with if running tests under valgrind;
Packit ae235b
 * in tests that use g_test_init(), the option `-m no-undefined` disables
Packit ae235b
 * those tests, while `-m undefined` explicitly enables them (the default
Packit ae235b
 * behaviour).
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if tests may provoke programming errors
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_verbose:
Packit ae235b
 *
Packit ae235b
 * Returns %TRUE if tests are run in verbose mode.
Packit ae235b
 * In tests that use g_test_init(), the option `--verbose` enables this,
Packit ae235b
 * while `-q` or `--quiet` disables it.
Packit ae235b
 * The default is neither g_test_verbose() nor g_test_quiet().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if in verbose mode
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_quiet:
Packit ae235b
 *
Packit ae235b
 * Returns %TRUE if tests are run in quiet mode.
Packit ae235b
 * In tests that use g_test_init(), the option `-q` or `--quiet` enables
Packit ae235b
 * this, while `--verbose` disables it.
Packit ae235b
 * The default is neither g_test_verbose() nor g_test_quiet().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if in quiet mode
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_queue_unref:
Packit ae235b
 * @gobject: the object to unref
Packit ae235b
 *
Packit ae235b
 * Enqueue an object to be released with g_object_unref() during
Packit ae235b
 * the next teardown phase. This is equivalent to calling
Packit ae235b
 * g_test_queue_destroy() with a destroy callback of g_object_unref().
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTestTrapFlags:
Packit ae235b
 * @G_TEST_TRAP_SILENCE_STDOUT: Redirect stdout of the test child to
Packit ae235b
 *     `/dev/null` so it cannot be observed on the console during test
Packit ae235b
 *     runs. The actual output is still captured though to allow later
Packit ae235b
 *     tests with g_test_trap_assert_stdout().
Packit ae235b
 * @G_TEST_TRAP_SILENCE_STDERR: Redirect stderr of the test child to
Packit ae235b
 *     `/dev/null` so it cannot be observed on the console during test
Packit ae235b
 *     runs. The actual output is still captured though to allow later
Packit ae235b
 *     tests with g_test_trap_assert_stderr().
Packit ae235b
 * @G_TEST_TRAP_INHERIT_STDIN: If this flag is given, stdin of the
Packit ae235b
 *     child process is shared with stdin of its parent process.
Packit ae235b
 *     It is redirected to `/dev/null` otherwise.
Packit ae235b
 *
Packit ae235b
 * Test traps are guards around forked tests.
Packit ae235b
 * These flags determine what traps to set.
Packit ae235b
 *
Packit ae235b
 * Deprecated: #GTestTrapFlags is used only with g_test_trap_fork(),
Packit ae235b
 * which is deprecated. g_test_trap_subprocess() uses
Packit ae235b
 * #GTestSubprocessFlags.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTestSubprocessFlags:
Packit ae235b
 * @G_TEST_SUBPROCESS_INHERIT_STDIN: If this flag is given, the child
Packit ae235b
 *     process will inherit the parent's stdin. Otherwise, the child's
Packit ae235b
 *     stdin is redirected to `/dev/null`.
Packit ae235b
 * @G_TEST_SUBPROCESS_INHERIT_STDOUT: If this flag is given, the child
Packit ae235b
 *     process will inherit the parent's stdout. Otherwise, the child's
Packit ae235b
 *     stdout will not be visible, but it will be captured to allow
Packit ae235b
 *     later tests with g_test_trap_assert_stdout().
Packit ae235b
 * @G_TEST_SUBPROCESS_INHERIT_STDERR: If this flag is given, the child
Packit ae235b
 *     process will inherit the parent's stderr. Otherwise, the child's
Packit ae235b
 *     stderr will not be visible, but it will be captured to allow
Packit ae235b
 *     later tests with g_test_trap_assert_stderr().
Packit ae235b
 *
Packit ae235b
 * Flags to pass to g_test_trap_subprocess() to control input and output.
Packit ae235b
 *
Packit ae235b
 * Note that in contrast with g_test_trap_fork(), the default is to
Packit ae235b
 * not show stdout and stderr.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_trap_assert_passed:
Packit ae235b
 *
Packit ae235b
 * Assert that the last test subprocess passed.
Packit ae235b
 * See g_test_trap_subprocess().
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_trap_assert_failed:
Packit ae235b
 *
Packit ae235b
 * Assert that the last test subprocess failed.
Packit ae235b
 * See g_test_trap_subprocess().
Packit ae235b
 *
Packit ae235b
 * This is sometimes used to test situations that are formally considered to
Packit ae235b
 * be undefined behaviour, like inputs that fail a g_return_if_fail()
Packit ae235b
 * check. In these situations you should skip the entire test, including the
Packit ae235b
 * call to g_test_trap_subprocess(), unless g_test_undefined() returns %TRUE
Packit ae235b
 * to indicate that undefined behaviour may be tested.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_trap_assert_stdout:
Packit ae235b
 * @soutpattern: a glob-style [pattern][glib-Glob-style-pattern-matching]
Packit ae235b
 *
Packit ae235b
 * Assert that the stdout output of the last test subprocess matches
Packit ae235b
 * @soutpattern. See g_test_trap_subprocess().
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_trap_assert_stdout_unmatched:
Packit ae235b
 * @soutpattern: a glob-style [pattern][glib-Glob-style-pattern-matching]
Packit ae235b
 *
Packit ae235b
 * Assert that the stdout output of the last test subprocess
Packit ae235b
 * does not match @soutpattern. See g_test_trap_subprocess().
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_trap_assert_stderr:
Packit ae235b
 * @serrpattern: a glob-style [pattern][glib-Glob-style-pattern-matching]
Packit ae235b
 *
Packit ae235b
 * Assert that the stderr output of the last test subprocess
Packit ae235b
 * matches @serrpattern. See  g_test_trap_subprocess().
Packit ae235b
 *
Packit ae235b
 * This is sometimes used to test situations that are formally
Packit ae235b
 * considered to be undefined behaviour, like code that hits a
Packit ae235b
 * g_assert() or g_error(). In these situations you should skip the
Packit ae235b
 * entire test, including the call to g_test_trap_subprocess(), unless
Packit ae235b
 * g_test_undefined() returns %TRUE to indicate that undefined
Packit ae235b
 * behaviour may be tested.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_trap_assert_stderr_unmatched:
Packit ae235b
 * @serrpattern: a glob-style [pattern][glib-Glob-style-pattern-matching]
Packit ae235b
 *
Packit ae235b
 * Assert that the stderr output of the last test subprocess
Packit ae235b
 * does not match @serrpattern. See g_test_trap_subprocess().
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_rand_bit:
Packit ae235b
 *
Packit ae235b
 * Get a reproducible random bit (0 or 1), see g_test_rand_int()
Packit ae235b
 * for details on test case random numbers.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert:
Packit ae235b
 * @expr: the expression to check
Packit ae235b
 *
Packit ae235b
 * Debugging macro to terminate the application if the assertion
Packit ae235b
 * fails. If the assertion fails (i.e. the expression is not true),
Packit ae235b
 * an error message is logged and the application is terminated.
Packit ae235b
 *
Packit ae235b
 * The macro can be turned off in final releases of code by defining
Packit ae235b
 * `G_DISABLE_ASSERT` when compiling the application, so code must
Packit ae235b
 * not depend on any side effects from @expr.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_not_reached:
Packit ae235b
 *
Packit ae235b
 * Debugging macro to terminate the application if it is ever
Packit ae235b
 * reached. If it is reached, an error message is logged and the
Packit ae235b
 * application is terminated.
Packit ae235b
 *
Packit ae235b
 * The macro can be turned off in final releases of code by defining
Packit ae235b
 * `G_DISABLE_ASSERT` when compiling the application.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_true:
Packit ae235b
 * @expr: the expression to check
Packit ae235b
 *
Packit ae235b
 * Debugging macro to check that an expression is true.
Packit ae235b
 *
Packit ae235b
 * If the assertion fails (i.e. the expression is not true),
Packit ae235b
 * an error message is logged and the application is either
Packit ae235b
 * terminated or the testcase marked as failed.
Packit ae235b
 *
Packit ae235b
 * See g_test_set_nonfatal_assertions().
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_false:
Packit ae235b
 * @expr: the expression to check
Packit ae235b
 *
Packit ae235b
 * Debugging macro to check an expression is false.
Packit ae235b
 *
Packit ae235b
 * If the assertion fails (i.e. the expression is not false),
Packit ae235b
 * an error message is logged and the application is either
Packit ae235b
 * terminated or the testcase marked as failed.
Packit ae235b
 *
Packit ae235b
 * See g_test_set_nonfatal_assertions().
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_null:
Packit ae235b
 * @expr: the expression to check
Packit ae235b
 *
Packit ae235b
 * Debugging macro to check an expression is %NULL.
Packit ae235b
 *
Packit ae235b
 * If the assertion fails (i.e. the expression is not %NULL),
Packit ae235b
 * an error message is logged and the application is either
Packit ae235b
 * terminated or the testcase marked as failed.
Packit ae235b
 *
Packit ae235b
 * See g_test_set_nonfatal_assertions().
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_nonnull:
Packit ae235b
 * @expr: the expression to check
Packit ae235b
 *
Packit ae235b
 * Debugging macro to check an expression is not %NULL.
Packit ae235b
 *
Packit ae235b
 * If the assertion fails (i.e. the expression is %NULL),
Packit ae235b
 * an error message is logged and the application is either
Packit ae235b
 * terminated or the testcase marked as failed.
Packit ae235b
 *
Packit ae235b
 * See g_test_set_nonfatal_assertions().
Packit ae235b
 *
Packit ae235b
 * Since: 2.40
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_cmpstr:
Packit ae235b
 * @s1: a string (may be %NULL)
Packit ae235b
 * @cmp: The comparison operator to use.
Packit ae235b
 *     One of ==, !=, <, >, <=, >=.
Packit ae235b
 * @s2: another string (may be %NULL)
Packit ae235b
 *
Packit ae235b
 * Debugging macro to compare two strings. If the comparison fails,
Packit ae235b
 * an error message is logged and the application is either terminated
Packit ae235b
 * or the testcase marked as failed.
Packit ae235b
 * The strings are compared using g_strcmp0().
Packit ae235b
 *
Packit ae235b
 * The effect of `g_assert_cmpstr (s1, op, s2)` is
Packit ae235b
 * the same as `g_assert_true (g_strcmp0 (s1, s2) op 0)`.
Packit ae235b
 * The advantage of this macro is that it can produce a message that
Packit ae235b
 * includes the actual values of @s1 and @s2.
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   g_assert_cmpstr (mystring, ==, "fubar");
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_cmpint:
Packit ae235b
 * @n1: an integer
Packit ae235b
 * @cmp: The comparison operator to use.
Packit ae235b
 *     One of ==, !=, <, >, <=, >=.
Packit ae235b
 * @n2: another integer
Packit ae235b
 *
Packit ae235b
 * Debugging macro to compare two integers.
Packit ae235b
 *
Packit ae235b
 * The effect of `g_assert_cmpint (n1, op, n2)` is
Packit ae235b
 * the same as `g_assert_true (n1 op n2)`. The advantage
Packit ae235b
 * of this macro is that it can produce a message that includes the
Packit ae235b
 * actual values of @n1 and @n2.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_cmpuint:
Packit ae235b
 * @n1: an unsigned integer
Packit ae235b
 * @cmp: The comparison operator to use.
Packit ae235b
 *     One of ==, !=, <, >, <=, >=.
Packit ae235b
 * @n2: another unsigned integer
Packit ae235b
 *
Packit ae235b
 * Debugging macro to compare two unsigned integers.
Packit ae235b
 *
Packit ae235b
 * The effect of `g_assert_cmpuint (n1, op, n2)` is
Packit ae235b
 * the same as `g_assert_true (n1 op n2)`. The advantage
Packit ae235b
 * of this macro is that it can produce a message that includes the
Packit ae235b
 * actual values of @n1 and @n2.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_cmphex:
Packit ae235b
 * @n1: an unsigned integer
Packit ae235b
 * @cmp: The comparison operator to use.
Packit ae235b
 *     One of ==, !=, <, >, <=, >=.
Packit ae235b
 * @n2: another unsigned integer
Packit ae235b
 *
Packit ae235b
 * Debugging macro to compare to unsigned integers.
Packit ae235b
 *
Packit ae235b
 * This is a variant of g_assert_cmpuint() that displays the numbers
Packit ae235b
 * in hexadecimal notation in the message.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_cmpfloat:
Packit ae235b
 * @n1: an floating point number
Packit ae235b
 * @cmp: The comparison operator to use.
Packit ae235b
 *     One of ==, !=, <, >, <=, >=.
Packit ae235b
 * @n2: another floating point number
Packit ae235b
 *
Packit ae235b
 * Debugging macro to compare two floating point numbers.
Packit ae235b
 *
Packit ae235b
 * The effect of `g_assert_cmpfloat (n1, op, n2)` is
Packit ae235b
 * the same as `g_assert_true (n1 op n2)`. The advantage
Packit ae235b
 * of this macro is that it can produce a message that includes the
Packit ae235b
 * actual values of @n1 and @n2.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_cmpmem:
Packit ae235b
 * @m1: pointer to a buffer
Packit ae235b
 * @l1: length of @m1
Packit ae235b
 * @m2: pointer to another buffer
Packit ae235b
 * @l2: length of @m2
Packit ae235b
 *
Packit ae235b
 * Debugging macro to compare memory regions. If the comparison fails,
Packit ae235b
 * an error message is logged and the application is either terminated
Packit ae235b
 * or the testcase marked as failed.
Packit ae235b
 *
Packit ae235b
 * The effect of `g_assert_cmpmem (m1, l1, m2, l2)` is
Packit ae235b
 * the same as `g_assert_true (l1 == l2 && memcmp (m1, m2, l1) == 0)`.
Packit ae235b
 * The advantage of this macro is that it can produce a message that
Packit ae235b
 * includes the actual values of @l1 and @l2.
Packit ae235b
 *
Packit ae235b
 * |[
Packit ae235b
 *   g_assert_cmpmem (buf->data, buf->len, expected, sizeof (expected));
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Since: 2.46
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_no_error:
Packit ae235b
 * @err: a #GError, possibly %NULL
Packit ae235b
 *
Packit ae235b
 * Debugging macro to check that a #GError is not set.
Packit ae235b
 *
Packit ae235b
 * The effect of `g_assert_no_error (err)` is
Packit ae235b
 * the same as `g_assert_true (err == NULL)`. The advantage
Packit ae235b
 * of this macro is that it can produce a message that includes
Packit ae235b
 * the error message and code.
Packit ae235b
 *
Packit ae235b
 * Since: 2.20
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assert_error:
Packit ae235b
 * @err: a #GError, possibly %NULL
Packit ae235b
 * @dom: the expected error domain (a #GQuark)
Packit ae235b
 * @c: the expected error code
Packit ae235b
 *
Packit ae235b
 * Debugging macro to check that a method has returned
Packit ae235b
 * the correct #GError.
Packit ae235b
 *
Packit ae235b
 * The effect of `g_assert_error (err, dom, c)` is
Packit ae235b
 * the same as `g_assert_true (err != NULL && err->domain
Packit ae235b
 * == dom && err->code == c)`. The advantage of this
Packit ae235b
 * macro is that it can produce a message that includes the incorrect
Packit ae235b
 * error message and code.
Packit ae235b
 *
Packit ae235b
 * This can only be used to test for a specific error. If you want to
Packit ae235b
 * test that @err is set, but don't care what it's set to, just use
Packit ae235b
 * `g_assert (err != NULL)`
Packit ae235b
 *
Packit ae235b
 * Since: 2.20
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTestCase:
Packit ae235b
 *
Packit ae235b
 * An opaque structure representing a test case.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTestSuite:
Packit ae235b
 *
Packit ae235b
 * An opaque structure representing a test suite.
Packit ae235b
 */
Packit ae235b
Packit ae235b
Packit ae235b
/* Global variable for storing assertion messages; this is the counterpart to
Packit ae235b
 * glibc's (private) __abort_msg variable, and allows developers and crash
Packit ae235b
 * analysis systems like Apport and ABRT to fish out assertion messages from
Packit ae235b
 * core dumps, instead of having to catch them on screen output.
Packit ae235b
 */
Packit ae235b
GLIB_VAR char *__glib_assert_msg;
Packit ae235b
char *__glib_assert_msg = NULL;
Packit ae235b
Packit ae235b
/* --- constants --- */
Packit ae235b
#define G_TEST_STATUS_TIMED_OUT 1024
Packit ae235b
Packit ae235b
/* --- structures --- */
Packit ae235b
struct GTestCase
Packit ae235b
{
Packit ae235b
  gchar  *name;
Packit ae235b
  guint   fixture_size;
Packit ae235b
  void   (*fixture_setup)    (void*, gconstpointer);
Packit ae235b
  void   (*fixture_test)     (void*, gconstpointer);
Packit ae235b
  void   (*fixture_teardown) (void*, gconstpointer);
Packit ae235b
  gpointer test_data;
Packit ae235b
};
Packit ae235b
struct GTestSuite
Packit ae235b
{
Packit ae235b
  gchar  *name;
Packit ae235b
  GSList *suites;
Packit ae235b
  GSList *cases;
Packit ae235b
};
Packit ae235b
typedef struct DestroyEntry DestroyEntry;
Packit ae235b
struct DestroyEntry
Packit ae235b
{
Packit ae235b
  DestroyEntry *next;
Packit ae235b
  GDestroyNotify destroy_func;
Packit ae235b
  gpointer       destroy_data;
Packit ae235b
};
Packit ae235b
Packit ae235b
/* --- prototypes --- */
Packit ae235b
static void     test_run_seed                   (const gchar *rseed);
Packit ae235b
static void     test_trap_clear                 (void);
Packit ae235b
static guint8*  g_test_log_dump                 (GTestLogMsg *msg,
Packit ae235b
                                                 guint       *len);
Packit ae235b
static void     gtest_default_log_handler       (const gchar    *log_domain,
Packit ae235b
                                                 GLogLevelFlags  log_level,
Packit ae235b
                                                 const gchar    *message,
Packit ae235b
                                                 gpointer        unused_data);
Packit ae235b
Packit ae235b
Packit ae235b
static const char * const g_test_result_names[] = {
Packit ae235b
  "OK",
Packit ae235b
  "SKIP",
Packit ae235b
  "FAIL",
Packit ae235b
  "TODO"
Packit ae235b
};
Packit ae235b
Packit ae235b
/* --- variables --- */
Packit ae235b
static int         test_log_fd = -1;
Packit ae235b
static gboolean    test_mode_fatal = TRUE;
Packit ae235b
static gboolean    g_test_run_once = TRUE;
Packit ae235b
static gboolean    test_run_list = FALSE;
Packit ae235b
static gchar      *test_run_seedstr = NULL;
Packit ae235b
static GRand      *test_run_rand = NULL;
Packit ae235b
static gchar      *test_run_name = "";
Packit ae235b
static GSList    **test_filename_free_list;
Packit ae235b
static guint       test_run_forks = 0;
Packit ae235b
static guint       test_run_count = 0;
Packit ae235b
static guint       test_count = 0;
Packit ae235b
static guint       test_skipped_count = 0;
Packit ae235b
static GTestResult test_run_success = G_TEST_RUN_FAILURE;
Packit ae235b
static gchar      *test_run_msg = NULL;
Packit ae235b
static guint       test_startup_skip_count = 0;
Packit ae235b
static GTimer     *test_user_timer = NULL;
Packit ae235b
static double      test_user_stamp = 0;
Packit ae235b
static GSList     *test_paths = NULL;
Packit ae235b
static GSList     *test_paths_skipped = NULL;
Packit ae235b
static GTestSuite *test_suite_root = NULL;
Packit ae235b
static int         test_trap_last_status = 0;  /* unmodified platform-specific status */
Packit ae235b
static GPid        test_trap_last_pid = 0;
Packit ae235b
static char       *test_trap_last_subprocess = NULL;
Packit ae235b
static char       *test_trap_last_stdout = NULL;
Packit ae235b
static char       *test_trap_last_stderr = NULL;
Packit ae235b
static char       *test_uri_base = NULL;
Packit ae235b
static gboolean    test_debug_log = FALSE;
Packit ae235b
static gboolean    test_tap_log = FALSE;
Packit ae235b
static gboolean    test_nonfatal_assertions = FALSE;
Packit ae235b
static DestroyEntry *test_destroy_queue = NULL;
Packit ae235b
static char       *test_argv0 = NULL;
Packit ae235b
static char       *test_argv0_dirname;
Packit ae235b
static const char *test_disted_files_dir;
Packit ae235b
static const char *test_built_files_dir;
Packit ae235b
static char       *test_initial_cwd = NULL;
Packit ae235b
static gboolean    test_in_forked_child = FALSE;
Packit ae235b
static gboolean    test_in_subprocess = FALSE;
Packit ae235b
static GTestConfig mutable_test_config_vars = {
Packit ae235b
  FALSE,        /* test_initialized */
Packit ae235b
  TRUE,         /* test_quick */
Packit ae235b
  FALSE,        /* test_perf */
Packit ae235b
  FALSE,        /* test_verbose */
Packit ae235b
  FALSE,        /* test_quiet */
Packit ae235b
  TRUE,         /* test_undefined */
Packit ae235b
};
Packit ae235b
const GTestConfig * const g_test_config_vars = &mutable_test_config_vars;
Packit ae235b
static gboolean  no_g_set_prgname = FALSE;
Packit ae235b
Packit ae235b
/* --- functions --- */
Packit ae235b
const char*
Packit ae235b
g_test_log_type_name (GTestLogType log_type)
Packit ae235b
{
Packit ae235b
  switch (log_type)
Packit ae235b
    {
Packit ae235b
    case G_TEST_LOG_NONE:               return "none";
Packit ae235b
    case G_TEST_LOG_ERROR:              return "error";
Packit ae235b
    case G_TEST_LOG_START_BINARY:       return "binary";
Packit ae235b
    case G_TEST_LOG_LIST_CASE:          return "list";
Packit ae235b
    case G_TEST_LOG_SKIP_CASE:          return "skip";
Packit ae235b
    case G_TEST_LOG_START_CASE:         return "start";
Packit ae235b
    case G_TEST_LOG_STOP_CASE:          return "stop";
Packit ae235b
    case G_TEST_LOG_MIN_RESULT:         return "minperf";
Packit ae235b
    case G_TEST_LOG_MAX_RESULT:         return "maxperf";
Packit ae235b
    case G_TEST_LOG_MESSAGE:            return "message";
Packit ae235b
    case G_TEST_LOG_START_SUITE:        return "start suite";
Packit ae235b
    case G_TEST_LOG_STOP_SUITE:         return "stop suite";
Packit ae235b
    }
Packit ae235b
  return "???";
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_test_log_send (guint         n_bytes,
Packit ae235b
                 const guint8 *buffer)
Packit ae235b
{
Packit ae235b
  if (test_log_fd >= 0)
Packit ae235b
    {
Packit ae235b
      int r;
Packit ae235b
      do
Packit ae235b
        r = write (test_log_fd, buffer, n_bytes);
Packit ae235b
      while (r < 0 && errno == EINTR);
Packit ae235b
    }
Packit ae235b
  if (test_debug_log)
Packit ae235b
    {
Packit ae235b
      GTestLogBuffer *lbuffer = g_test_log_buffer_new ();
Packit ae235b
      GTestLogMsg *msg;
Packit ae235b
      guint ui;
Packit ae235b
      g_test_log_buffer_push (lbuffer, n_bytes, buffer);
Packit ae235b
      msg = g_test_log_buffer_pop (lbuffer);
Packit ae235b
      g_warn_if_fail (msg != NULL);
Packit ae235b
      g_warn_if_fail (lbuffer->data->len == 0);
Packit ae235b
      g_test_log_buffer_free (lbuffer);
Packit ae235b
      /* print message */
Packit ae235b
      g_printerr ("{*LOG(%s)", g_test_log_type_name (msg->log_type));
Packit ae235b
      for (ui = 0; ui < msg->n_strings; ui++)
Packit ae235b
        g_printerr (":{%s}", msg->strings[ui]);
Packit ae235b
      if (msg->n_nums)
Packit ae235b
        {
Packit ae235b
          g_printerr (":(");
Packit ae235b
          for (ui = 0; ui < msg->n_nums; ui++)
Packit ae235b
            {
Packit ae235b
              if ((long double) (long) msg->nums[ui] == msg->nums[ui])
Packit ae235b
                g_printerr ("%s%ld", ui ? ";" : "", (long) msg->nums[ui]);
Packit ae235b
              else
Packit ae235b
                g_printerr ("%s%.16g", ui ? ";" : "", (double) msg->nums[ui]);
Packit ae235b
            }
Packit ae235b
          g_printerr (")");
Packit ae235b
        }
Packit ae235b
      g_printerr (":LOG*}\n");
Packit ae235b
      g_test_log_msg_free (msg);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_test_log (GTestLogType lbit,
Packit ae235b
            const gchar *string1,
Packit ae235b
            const gchar *string2,
Packit ae235b
            guint        n_args,
Packit ae235b
            long double *largs)
Packit ae235b
{
Packit ae235b
  GTestResult result;
Packit ae235b
  gboolean fail;
Packit ae235b
  GTestLogMsg msg;
Packit ae235b
  gchar *astrings[3] = { NULL, NULL, NULL };
Packit ae235b
  guint8 *dbuffer;
Packit ae235b
  guint32 dbufferlen;
Packit ae235b
Packit ae235b
  switch (lbit)
Packit ae235b
    {
Packit ae235b
    case G_TEST_LOG_START_BINARY:
Packit ae235b
      if (test_tap_log)
Packit ae235b
        g_print ("# random seed: %s\n", string2);
Packit ae235b
      else if (g_test_verbose ())
Packit ae235b
        g_print ("GTest: random seed: %s\n", string2);
Packit ae235b
      break;
Packit ae235b
    case G_TEST_LOG_START_SUITE:
Packit ae235b
      if (test_tap_log)
Packit ae235b
        {
Packit ae235b
          if (string1[0] != 0)
Packit ae235b
            g_print ("# Start of %s tests\n", string1);
Packit ae235b
          else
Packit ae235b
            g_print ("1..%d\n", test_count);
Packit ae235b
        }
Packit ae235b
      break;
Packit ae235b
    case G_TEST_LOG_STOP_SUITE:
Packit ae235b
      if (test_tap_log)
Packit ae235b
        {
Packit ae235b
          if (string1[0] != 0)
Packit ae235b
            g_print ("# End of %s tests\n", string1);
Packit ae235b
        }
Packit ae235b
      break;
Packit ae235b
    case G_TEST_LOG_STOP_CASE:
Packit ae235b
      result = largs[0];
Packit ae235b
      fail = result == G_TEST_RUN_FAILURE;
Packit ae235b
      if (test_tap_log)
Packit ae235b
        {
Packit ae235b
          g_print ("%s %d %s", fail ? "not ok" : "ok", test_run_count, string1);
Packit ae235b
          if (result == G_TEST_RUN_INCOMPLETE)
Packit ae235b
            g_print (" # TODO %s\n", string2 ? string2 : "");
Packit ae235b
          else if (result == G_TEST_RUN_SKIPPED)
Packit ae235b
            g_print (" # SKIP %s\n", string2 ? string2 : "");
Packit ae235b
          else
Packit ae235b
            g_print ("\n");
Packit ae235b
        }
Packit ae235b
      else if (g_test_verbose ())
Packit ae235b
        g_print ("GTest: result: %s\n", g_test_result_names[result]);
Packit ae235b
      else if (!g_test_quiet ())
Packit ae235b
        g_print ("%s\n", g_test_result_names[result]);
Packit ae235b
      if (fail && test_mode_fatal)
Packit ae235b
        {
Packit ae235b
          if (test_tap_log)
Packit ae235b
            g_print ("Bail out!\n");
Packit ae235b
          g_abort ();
Packit ae235b
        }
Packit ae235b
      if (result == G_TEST_RUN_SKIPPED)
Packit ae235b
        test_skipped_count++;
Packit ae235b
      break;
Packit ae235b
    case G_TEST_LOG_MIN_RESULT:
Packit ae235b
      if (test_tap_log)
Packit ae235b
        g_print ("# min perf: %s\n", string1);
Packit ae235b
      else if (g_test_verbose ())
Packit ae235b
        g_print ("(MINPERF:%s)\n", string1);
Packit ae235b
      break;
Packit ae235b
    case G_TEST_LOG_MAX_RESULT:
Packit ae235b
      if (test_tap_log)
Packit ae235b
        g_print ("# max perf: %s\n", string1);
Packit ae235b
      else if (g_test_verbose ())
Packit ae235b
        g_print ("(MAXPERF:%s)\n", string1);
Packit ae235b
      break;
Packit ae235b
    case G_TEST_LOG_MESSAGE:
Packit ae235b
      if (test_tap_log)
Packit ae235b
        g_print ("# %s\n", string1);
Packit ae235b
      else if (g_test_verbose ())
Packit ae235b
        g_print ("(MSG: %s)\n", string1);
Packit ae235b
      break;
Packit ae235b
    case G_TEST_LOG_ERROR:
Packit ae235b
      if (test_tap_log)
Packit ae235b
        g_print ("Bail out! %s\n", string1);
Packit ae235b
      else if (g_test_verbose ())
Packit ae235b
        g_print ("(ERROR: %s)\n", string1);
Packit ae235b
      break;
Packit ae235b
    default: ;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  msg.log_type = lbit;
Packit ae235b
  msg.n_strings = (string1 != NULL) + (string1 && string2);
Packit ae235b
  msg.strings = astrings;
Packit ae235b
  astrings[0] = (gchar*) string1;
Packit ae235b
  astrings[1] = astrings[0] ? (gchar*) string2 : NULL;
Packit ae235b
  msg.n_nums = n_args;
Packit ae235b
  msg.nums = largs;
Packit ae235b
  dbuffer = g_test_log_dump (&msg, &dbufferlen);
Packit ae235b
  g_test_log_send (dbufferlen, dbuffer);
Packit ae235b
  g_free (dbuffer);
Packit ae235b
Packit ae235b
  switch (lbit)
Packit ae235b
    {
Packit ae235b
    case G_TEST_LOG_START_CASE:
Packit ae235b
      if (test_tap_log)
Packit ae235b
        ;
Packit ae235b
      else if (g_test_verbose ())
Packit ae235b
        g_print ("GTest: run: %s\n", string1);
Packit ae235b
      else if (!g_test_quiet ())
Packit ae235b
        g_print ("%s: ", string1);
Packit ae235b
      break;
Packit ae235b
    default: ;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* We intentionally parse the command line without GOptionContext
Packit ae235b
 * because otherwise you would never be able to test it.
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
parse_args (gint    *argc_p,
Packit ae235b
            gchar ***argv_p)
Packit ae235b
{
Packit ae235b
  guint argc = *argc_p;
Packit ae235b
  gchar **argv = *argv_p;
Packit ae235b
  guint i, e;
Packit ae235b
Packit ae235b
  test_argv0 = argv[0];
Packit ae235b
  test_initial_cwd = g_get_current_dir ();
Packit ae235b
Packit ae235b
  /* parse known args */
Packit ae235b
  for (i = 1; i < argc; i++)
Packit ae235b
    {
Packit ae235b
      if (strcmp (argv[i], "--g-fatal-warnings") == 0)
Packit ae235b
        {
Packit ae235b
          GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
Packit ae235b
          fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
Packit ae235b
          g_log_set_always_fatal (fatal_mask);
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp (argv[i], "--keep-going") == 0 ||
Packit ae235b
               strcmp (argv[i], "-k") == 0)
Packit ae235b
        {
Packit ae235b
          test_mode_fatal = FALSE;
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp (argv[i], "--debug-log") == 0)
Packit ae235b
        {
Packit ae235b
          test_debug_log = TRUE;
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp (argv[i], "--tap") == 0)
Packit ae235b
        {
Packit ae235b
          test_tap_log = TRUE;
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp ("--GTestLogFD", argv[i]) == 0 || strncmp ("--GTestLogFD=", argv[i], 13) == 0)
Packit ae235b
        {
Packit ae235b
          gchar *equal = argv[i] + 12;
Packit ae235b
          if (*equal == '=')
Packit ae235b
            test_log_fd = g_ascii_strtoull (equal + 1, NULL, 0);
Packit ae235b
          else if (i + 1 < argc)
Packit ae235b
            {
Packit ae235b
              argv[i++] = NULL;
Packit ae235b
              test_log_fd = g_ascii_strtoull (argv[i], NULL, 0);
Packit ae235b
            }
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp ("--GTestSkipCount", argv[i]) == 0 || strncmp ("--GTestSkipCount=", argv[i], 17) == 0)
Packit ae235b
        {
Packit ae235b
          gchar *equal = argv[i] + 16;
Packit ae235b
          if (*equal == '=')
Packit ae235b
            test_startup_skip_count = g_ascii_strtoull (equal + 1, NULL, 0);
Packit ae235b
          else if (i + 1 < argc)
Packit ae235b
            {
Packit ae235b
              argv[i++] = NULL;
Packit ae235b
              test_startup_skip_count = g_ascii_strtoull (argv[i], NULL, 0);
Packit ae235b
            }
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp ("--GTestSubprocess", argv[i]) == 0)
Packit ae235b
        {
Packit ae235b
          test_in_subprocess = TRUE;
Packit ae235b
          /* We typically expect these child processes to crash, and some
Packit ae235b
           * tests spawn a *lot* of them.  Avoid spamming system crash
Packit ae235b
           * collection programs such as systemd-coredump and abrt.
Packit ae235b
           */
Packit ae235b
#ifdef HAVE_SYS_RESOURCE_H
Packit ae235b
          {
Packit ae235b
            struct rlimit limit = { 0, 0 };
Packit ae235b
            (void) setrlimit (RLIMIT_CORE, &limit);
Packit ae235b
          }
Packit ae235b
#endif
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp ("-p", argv[i]) == 0 || strncmp ("-p=", argv[i], 3) == 0)
Packit ae235b
        {
Packit ae235b
          gchar *equal = argv[i] + 2;
Packit ae235b
          if (*equal == '=')
Packit ae235b
            test_paths = g_slist_prepend (test_paths, equal + 1);
Packit ae235b
          else if (i + 1 < argc)
Packit ae235b
            {
Packit ae235b
              argv[i++] = NULL;
Packit ae235b
              test_paths = g_slist_prepend (test_paths, argv[i]);
Packit ae235b
            }
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp ("-s", argv[i]) == 0 || strncmp ("-s=", argv[i], 3) == 0)
Packit ae235b
        {
Packit ae235b
          gchar *equal = argv[i] + 2;
Packit ae235b
          if (*equal == '=')
Packit ae235b
            test_paths_skipped = g_slist_prepend (test_paths_skipped, equal + 1);
Packit ae235b
          else if (i + 1 < argc)
Packit ae235b
            {
Packit ae235b
              argv[i++] = NULL;
Packit ae235b
              test_paths_skipped = g_slist_prepend (test_paths_skipped, argv[i]);
Packit ae235b
            }
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp ("-m", argv[i]) == 0 || strncmp ("-m=", argv[i], 3) == 0)
Packit ae235b
        {
Packit ae235b
          gchar *equal = argv[i] + 2;
Packit ae235b
          const gchar *mode = "";
Packit ae235b
          if (*equal == '=')
Packit ae235b
            mode = equal + 1;
Packit ae235b
          else if (i + 1 < argc)
Packit ae235b
            {
Packit ae235b
              argv[i++] = NULL;
Packit ae235b
              mode = argv[i];
Packit ae235b
            }
Packit ae235b
          if (strcmp (mode, "perf") == 0)
Packit ae235b
            mutable_test_config_vars.test_perf = TRUE;
Packit ae235b
          else if (strcmp (mode, "slow") == 0)
Packit ae235b
            mutable_test_config_vars.test_quick = FALSE;
Packit ae235b
          else if (strcmp (mode, "thorough") == 0)
Packit ae235b
            mutable_test_config_vars.test_quick = FALSE;
Packit ae235b
          else if (strcmp (mode, "quick") == 0)
Packit ae235b
            {
Packit ae235b
              mutable_test_config_vars.test_quick = TRUE;
Packit ae235b
              mutable_test_config_vars.test_perf = FALSE;
Packit ae235b
            }
Packit ae235b
          else if (strcmp (mode, "undefined") == 0)
Packit ae235b
            mutable_test_config_vars.test_undefined = TRUE;
Packit ae235b
          else if (strcmp (mode, "no-undefined") == 0)
Packit ae235b
            mutable_test_config_vars.test_undefined = FALSE;
Packit ae235b
          else
Packit ae235b
            g_error ("unknown test mode: -m %s", mode);
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp ("-q", argv[i]) == 0 || strcmp ("--quiet", argv[i]) == 0)
Packit ae235b
        {
Packit ae235b
          mutable_test_config_vars.test_quiet = TRUE;
Packit ae235b
          mutable_test_config_vars.test_verbose = FALSE;
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp ("--verbose", argv[i]) == 0)
Packit ae235b
        {
Packit ae235b
          mutable_test_config_vars.test_quiet = FALSE;
Packit ae235b
          mutable_test_config_vars.test_verbose = TRUE;
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp ("-l", argv[i]) == 0)
Packit ae235b
        {
Packit ae235b
          test_run_list = TRUE;
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp ("--seed", argv[i]) == 0 || strncmp ("--seed=", argv[i], 7) == 0)
Packit ae235b
        {
Packit ae235b
          gchar *equal = argv[i] + 6;
Packit ae235b
          if (*equal == '=')
Packit ae235b
            test_run_seedstr = equal + 1;
Packit ae235b
          else if (i + 1 < argc)
Packit ae235b
            {
Packit ae235b
              argv[i++] = NULL;
Packit ae235b
              test_run_seedstr = argv[i];
Packit ae235b
            }
Packit ae235b
          argv[i] = NULL;
Packit ae235b
        }
Packit ae235b
      else if (strcmp ("-?", argv[i]) == 0 ||
Packit ae235b
               strcmp ("-h", argv[i]) == 0 ||
Packit ae235b
               strcmp ("--help", argv[i]) == 0)
Packit ae235b
        {
Packit ae235b
          printf ("Usage:\n"
Packit ae235b
                  "  %s [OPTION...]\n\n"
Packit ae235b
                  "Help Options:\n"
Packit ae235b
                  "  -h, --help                     Show help options\n\n"
Packit ae235b
                  "Test Options:\n"
Packit ae235b
                  "  --g-fatal-warnings             Make all warnings fatal\n"
Packit ae235b
                  "  -l                             List test cases available in a test executable\n"
Packit ae235b
                  "  -m {perf|slow|thorough|quick}  Execute tests according to mode\n"
Packit ae235b
                  "  -m {undefined|no-undefined}    Execute tests according to mode\n"
Packit ae235b
                  "  -p TESTPATH                    Only start test cases matching TESTPATH\n"
Packit ae235b
                  "  -s TESTPATH                    Skip all tests matching TESTPATH\n"
Packit ae235b
                  "  --seed=SEEDSTRING              Start tests with random seed SEEDSTRING\n"
Packit ae235b
                  "  --debug-log                    debug test logging output\n"
Packit ae235b
                  "  -q, --quiet                    Run tests quietly\n"
Packit ae235b
                  "  --verbose                      Run tests verbosely\n",
Packit ae235b
                  argv[0]);
Packit ae235b
          exit (0);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  /* collapse argv */
Packit ae235b
  e = 1;
Packit ae235b
  for (i = 1; i < argc; i++)
Packit ae235b
    if (argv[i])
Packit ae235b
      {
Packit ae235b
        argv[e++] = argv[i];
Packit ae235b
        if (i >= e)
Packit ae235b
          argv[i] = NULL;
Packit ae235b
      }
Packit ae235b
  *argc_p = e;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_init:
Packit ae235b
 * @argc: Address of the @argc parameter of the main() function.
Packit ae235b
 *        Changed if any arguments were handled.
Packit ae235b
 * @argv: Address of the @argv parameter of main().
Packit ae235b
 *        Any parameters understood by g_test_init() stripped before return.
Packit ae235b
 * @...: %NULL-terminated list of special options. Currently the only
Packit ae235b
 *       defined option is `"no_g_set_prgname"`, which
Packit ae235b
 *       will cause g_test_init() to not call g_set_prgname().
Packit ae235b
 *
Packit ae235b
 * Initialize the GLib testing framework, e.g. by seeding the
Packit ae235b
 * test random number generator, the name for g_get_prgname()
Packit ae235b
 * and parsing test related command line args.
Packit ae235b
 *
Packit ae235b
 * So far, the following arguments are understood:
Packit ae235b
 *
Packit ae235b
 * - `-l`: List test cases available in a test executable.
Packit ae235b
 * - `--seed=SEED`: Provide a random seed to reproduce test
Packit ae235b
 *   runs using random numbers.
Packit ae235b
 * - `--verbose`: Run tests verbosely.
Packit ae235b
 * - `-q`, `--quiet`: Run tests quietly.
Packit ae235b
 * - `-p PATH`: Execute all tests matching the given path.
Packit ae235b
 * - `-s PATH`: Skip all tests matching the given path.
Packit ae235b
 *   This can also be used to force a test to run that would otherwise
Packit ae235b
 *   be skipped (ie, a test whose name contains "/subprocess").
Packit ae235b
 * - `-m {perf|slow|thorough|quick|undefined|no-undefined}`: Execute tests according to these test modes:
Packit ae235b
 *
Packit ae235b
 *   `perf`: Performance tests, may take long and report results (off by default).
Packit ae235b
 *
Packit ae235b
 *   `slow`, `thorough`: Slow and thorough tests, may take quite long and maximize coverage
Packit ae235b
 *   (off by default).
Packit ae235b
 *
Packit ae235b
 *   `quick`: Quick tests, should run really quickly and give good coverage (the default).
Packit ae235b
 *
Packit ae235b
 *   `undefined`: Tests for undefined behaviour, may provoke programming errors
Packit ae235b
 *   under g_test_trap_subprocess() or g_test_expect_message() to check
Packit ae235b
 *   that appropriate assertions or warnings are given (the default).
Packit ae235b
 *
Packit ae235b
 *   `no-undefined`: Avoid tests for undefined behaviour
Packit ae235b
 *
Packit ae235b
 * - `--debug-log`: Debug test logging output.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_init (int    *argc,
Packit ae235b
             char ***argv,
Packit ae235b
             ...)
Packit ae235b
{
Packit ae235b
  static char seedstr[4 + 4 * 8 + 1];
Packit ae235b
  va_list args;
Packit ae235b
  gpointer option;
Packit ae235b
  /* make warnings and criticals fatal for all test programs */
Packit ae235b
  GLogLevelFlags fatal_mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
Packit ae235b
Packit ae235b
  fatal_mask = (GLogLevelFlags) (fatal_mask | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL);
Packit ae235b
  g_log_set_always_fatal (fatal_mask);
Packit ae235b
  /* check caller args */
Packit ae235b
  g_return_if_fail (argc != NULL);
Packit ae235b
  g_return_if_fail (argv != NULL);
Packit ae235b
  g_return_if_fail (g_test_config_vars->test_initialized == FALSE);
Packit ae235b
  mutable_test_config_vars.test_initialized = TRUE;
Packit ae235b
Packit ae235b
  va_start (args, argv);
Packit ae235b
  while ((option = va_arg (args, char *)))
Packit ae235b
    {
Packit ae235b
      if (g_strcmp0 (option, "no_g_set_prgname") == 0)
Packit ae235b
        no_g_set_prgname = TRUE;
Packit ae235b
    }
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  /* setup random seed string */
Packit ae235b
  g_snprintf (seedstr, sizeof (seedstr), "R02S%08x%08x%08x%08x", g_random_int(), g_random_int(), g_random_int(), g_random_int());
Packit ae235b
  test_run_seedstr = seedstr;
Packit ae235b
Packit ae235b
  /* parse args, sets up mode, changes seed, etc. */
Packit ae235b
  parse_args (argc, argv);
Packit ae235b
Packit ae235b
  if (!g_get_prgname() && !no_g_set_prgname)
Packit ae235b
    g_set_prgname ((*argv)[0]);
Packit ae235b
Packit ae235b
  /* sanity check */
Packit ae235b
  if (test_tap_log)
Packit ae235b
    {
Packit ae235b
      if (test_paths || test_startup_skip_count)
Packit ae235b
        {
Packit ae235b
          /* Not invoking every test (even if SKIPped) breaks the "1..XX" plan */
Packit ae235b
          g_printerr ("%s: -p and --GTestSkipCount options are incompatible with --tap\n",
Packit ae235b
                      (*argv)[0]);
Packit ae235b
          exit (1);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* verify GRand reliability, needed for reliable seeds */
Packit ae235b
  if (1)
Packit ae235b
    {
Packit ae235b
      GRand *rg = g_rand_new_with_seed (0xc8c49fb6);
Packit ae235b
      guint32 t1 = g_rand_int (rg), t2 = g_rand_int (rg), t3 = g_rand_int (rg), t4 = g_rand_int (rg);
Packit ae235b
      /* g_print ("GRand-current: 0x%x 0x%x 0x%x 0x%x\n", t1, t2, t3, t4); */
Packit ae235b
      if (t1 != 0xfab39f9b || t2 != 0xb948fb0e || t3 != 0x3d31be26 || t4 != 0x43a19d66)
Packit ae235b
        g_warning ("random numbers are not GRand-2.2 compatible, seeds may be broken (check $G_RANDOM_VERSION)");
Packit ae235b
      g_rand_free (rg);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* check rand seed */
Packit ae235b
  test_run_seed (test_run_seedstr);
Packit ae235b
Packit ae235b
  /* report program start */
Packit ae235b
  g_log_set_default_handler (gtest_default_log_handler, NULL);
Packit ae235b
  g_test_log (G_TEST_LOG_START_BINARY, g_get_prgname(), test_run_seedstr, 0, NULL);
Packit ae235b
Packit ae235b
  test_argv0_dirname = g_path_get_dirname (test_argv0);
Packit ae235b
Packit ae235b
  /* Make sure we get the real dirname that the test was run from */
Packit ae235b
  if (g_str_has_suffix (test_argv0_dirname, "/.libs"))
Packit ae235b
    {
Packit ae235b
      gchar *tmp;
Packit ae235b
      tmp = g_path_get_dirname (test_argv0_dirname);
Packit ae235b
      g_free (test_argv0_dirname);
Packit ae235b
      test_argv0_dirname = tmp;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  test_disted_files_dir = g_getenv ("G_TEST_SRCDIR");
Packit ae235b
  if (!test_disted_files_dir)
Packit ae235b
    test_disted_files_dir = test_argv0_dirname;
Packit ae235b
Packit ae235b
  test_built_files_dir = g_getenv ("G_TEST_BUILDDIR");
Packit ae235b
  if (!test_built_files_dir)
Packit ae235b
    test_built_files_dir = test_argv0_dirname;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_run_seed (const gchar *rseed)
Packit ae235b
{
Packit ae235b
  guint seed_failed = 0;
Packit ae235b
  if (test_run_rand)
Packit ae235b
    g_rand_free (test_run_rand);
Packit ae235b
  test_run_rand = NULL;
Packit ae235b
  while (strchr (" \t\v\r\n\f", *rseed))
Packit ae235b
    rseed++;
Packit ae235b
  if (strncmp (rseed, "R02S", 4) == 0)  /* seed for random generator 02 (GRand-2.2) */
Packit ae235b
    {
Packit ae235b
      const char *s = rseed + 4;
Packit ae235b
      if (strlen (s) >= 32)             /* require 4 * 8 chars */
Packit ae235b
        {
Packit ae235b
          guint32 seedarray[4];
Packit ae235b
          gchar *p, hexbuf[9] = { 0, };
Packit ae235b
          memcpy (hexbuf, s + 0, 8);
Packit ae235b
          seedarray[0] = g_ascii_strtoull (hexbuf, &p, 16);
Packit ae235b
          seed_failed += p != NULL && *p != 0;
Packit ae235b
          memcpy (hexbuf, s + 8, 8);
Packit ae235b
          seedarray[1] = g_ascii_strtoull (hexbuf, &p, 16);
Packit ae235b
          seed_failed += p != NULL && *p != 0;
Packit ae235b
          memcpy (hexbuf, s + 16, 8);
Packit ae235b
          seedarray[2] = g_ascii_strtoull (hexbuf, &p, 16);
Packit ae235b
          seed_failed += p != NULL && *p != 0;
Packit ae235b
          memcpy (hexbuf, s + 24, 8);
Packit ae235b
          seedarray[3] = g_ascii_strtoull (hexbuf, &p, 16);
Packit ae235b
          seed_failed += p != NULL && *p != 0;
Packit ae235b
          if (!seed_failed)
Packit ae235b
            {
Packit ae235b
              test_run_rand = g_rand_new_with_seed_array (seedarray, 4);
Packit ae235b
              return;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  g_error ("Unknown or invalid random seed: %s", rseed);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_rand_int:
Packit ae235b
 *
Packit ae235b
 * Get a reproducible random integer number.
Packit ae235b
 *
Packit ae235b
 * The random numbers generated by the g_test_rand_*() family of functions
Packit ae235b
 * change with every new test program start, unless the --seed option is
Packit ae235b
 * given when starting test programs.
Packit ae235b
 *
Packit ae235b
 * For individual test cases however, the random number generator is
Packit ae235b
 * reseeded, to avoid dependencies between tests and to make --seed
Packit ae235b
 * effective for all test cases.
Packit ae235b
 *
Packit ae235b
 * Returns: a random number from the seeded random number generator.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
gint32
Packit ae235b
g_test_rand_int (void)
Packit ae235b
{
Packit ae235b
  return g_rand_int (test_run_rand);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_rand_int_range:
Packit ae235b
 * @begin: the minimum value returned by this function
Packit ae235b
 * @end:   the smallest value not to be returned by this function
Packit ae235b
 *
Packit ae235b
 * Get a reproducible random integer number out of a specified range,
Packit ae235b
 * see g_test_rand_int() for details on test case random numbers.
Packit ae235b
 *
Packit ae235b
 * Returns: a number with @begin <= number < @end.
Packit ae235b
 * 
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
gint32
Packit ae235b
g_test_rand_int_range (gint32          begin,
Packit ae235b
                       gint32          end)
Packit ae235b
{
Packit ae235b
  return g_rand_int_range (test_run_rand, begin, end);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_rand_double:
Packit ae235b
 *
Packit ae235b
 * Get a reproducible random floating point number,
Packit ae235b
 * see g_test_rand_int() for details on test case random numbers.
Packit ae235b
 *
Packit ae235b
 * Returns: a random number from the seeded random number generator.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
double
Packit ae235b
g_test_rand_double (void)
Packit ae235b
{
Packit ae235b
  return g_rand_double (test_run_rand);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_rand_double_range:
Packit ae235b
 * @range_start: the minimum value returned by this function
Packit ae235b
 * @range_end: the minimum value not returned by this function
Packit ae235b
 *
Packit ae235b
 * Get a reproducible random floating pointer number out of a specified range,
Packit ae235b
 * see g_test_rand_int() for details on test case random numbers.
Packit ae235b
 *
Packit ae235b
 * Returns: a number with @range_start <= number < @range_end.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
double
Packit ae235b
g_test_rand_double_range (double          range_start,
Packit ae235b
                          double          range_end)
Packit ae235b
{
Packit ae235b
  return g_rand_double_range (test_run_rand, range_start, range_end);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_timer_start:
Packit ae235b
 *
Packit ae235b
 * Start a timing test. Call g_test_timer_elapsed() when the task is supposed
Packit ae235b
 * to be done. Call this function again to restart the timer.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_timer_start (void)
Packit ae235b
{
Packit ae235b
  if (!test_user_timer)
Packit ae235b
    test_user_timer = g_timer_new();
Packit ae235b
  test_user_stamp = 0;
Packit ae235b
  g_timer_start (test_user_timer);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_timer_elapsed:
Packit ae235b
 *
Packit ae235b
 * Get the time since the last start of the timer with g_test_timer_start().
Packit ae235b
 *
Packit ae235b
 * Returns: the time since the last start of the timer, as a double
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
double
Packit ae235b
g_test_timer_elapsed (void)
Packit ae235b
{
Packit ae235b
  test_user_stamp = test_user_timer ? g_timer_elapsed (test_user_timer, NULL) : 0;
Packit ae235b
  return test_user_stamp;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_timer_last:
Packit ae235b
 *
Packit ae235b
 * Report the last result of g_test_timer_elapsed().
Packit ae235b
 *
Packit ae235b
 * Returns: the last result of g_test_timer_elapsed(), as a double
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
double
Packit ae235b
g_test_timer_last (void)
Packit ae235b
{
Packit ae235b
  return test_user_stamp;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_minimized_result:
Packit ae235b
 * @minimized_quantity: the reported value
Packit ae235b
 * @format: the format string of the report message
Packit ae235b
 * @...: arguments to pass to the printf() function
Packit ae235b
 *
Packit ae235b
 * Report the result of a performance or measurement test.
Packit ae235b
 * The test should generally strive to minimize the reported
Packit ae235b
 * quantities (smaller values are better than larger ones),
Packit ae235b
 * this and @minimized_quantity can determine sorting
Packit ae235b
 * order for test result reports.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_minimized_result (double          minimized_quantity,
Packit ae235b
                         const char     *format,
Packit ae235b
                         ...)
Packit ae235b
{
Packit ae235b
  long double largs = minimized_quantity;
Packit ae235b
  gchar *buffer;
Packit ae235b
  va_list args;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  buffer = g_strdup_vprintf (format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  g_test_log (G_TEST_LOG_MIN_RESULT, buffer, NULL, 1, &largs);
Packit ae235b
  g_free (buffer);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_maximized_result:
Packit ae235b
 * @maximized_quantity: the reported value
Packit ae235b
 * @format: the format string of the report message
Packit ae235b
 * @...: arguments to pass to the printf() function
Packit ae235b
 *
Packit ae235b
 * Report the result of a performance or measurement test.
Packit ae235b
 * The test should generally strive to maximize the reported
Packit ae235b
 * quantities (larger values are better than smaller ones),
Packit ae235b
 * this and @maximized_quantity can determine sorting
Packit ae235b
 * order for test result reports.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_maximized_result (double          maximized_quantity,
Packit ae235b
                         const char     *format,
Packit ae235b
                         ...)
Packit ae235b
{
Packit ae235b
  long double largs = maximized_quantity;
Packit ae235b
  gchar *buffer;
Packit ae235b
  va_list args;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  buffer = g_strdup_vprintf (format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  g_test_log (G_TEST_LOG_MAX_RESULT, buffer, NULL, 1, &largs);
Packit ae235b
  g_free (buffer);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_message:
Packit ae235b
 * @format: the format string
Packit ae235b
 * @...:    printf-like arguments to @format
Packit ae235b
 *
Packit ae235b
 * Add a message to the test report.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_message (const char *format,
Packit ae235b
                ...)
Packit ae235b
{
Packit ae235b
  gchar *buffer;
Packit ae235b
  va_list args;
Packit ae235b
Packit ae235b
  va_start (args, format);
Packit ae235b
  buffer = g_strdup_vprintf (format, args);
Packit ae235b
  va_end (args);
Packit ae235b
Packit ae235b
  g_test_log (G_TEST_LOG_MESSAGE, buffer, NULL, 0, NULL);
Packit ae235b
  g_free (buffer);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_bug_base:
Packit ae235b
 * @uri_pattern: the base pattern for bug URIs
Packit ae235b
 *
Packit ae235b
 * Specify the base URI for bug reports.
Packit ae235b
 *
Packit ae235b
 * The base URI is used to construct bug report messages for
Packit ae235b
 * g_test_message() when g_test_bug() is called.
Packit ae235b
 * Calling this function outside of a test case sets the
Packit ae235b
 * default base URI for all test cases. Calling it from within
Packit ae235b
 * a test case changes the base URI for the scope of the test
Packit ae235b
 * case only.
Packit ae235b
 * Bug URIs are constructed by appending a bug specific URI
Packit ae235b
 * portion to @uri_pattern, or by replacing the special string
Packit ae235b
 * '\%s' within @uri_pattern if that is present.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_bug_base (const char *uri_pattern)
Packit ae235b
{
Packit ae235b
  g_free (test_uri_base);
Packit ae235b
  test_uri_base = g_strdup (uri_pattern);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_bug:
Packit ae235b
 * @bug_uri_snippet: Bug specific bug tracker URI portion.
Packit ae235b
 *
Packit ae235b
 * This function adds a message to test reports that
Packit ae235b
 * associates a bug URI with a test case.
Packit ae235b
 * Bug URIs are constructed from a base URI set with g_test_bug_base()
Packit ae235b
 * and @bug_uri_snippet.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_bug (const char *bug_uri_snippet)
Packit ae235b
{
Packit ae235b
  char *c;
Packit ae235b
Packit ae235b
  g_return_if_fail (test_uri_base != NULL);
Packit ae235b
  g_return_if_fail (bug_uri_snippet != NULL);
Packit ae235b
Packit ae235b
  c = strstr (test_uri_base, "%s");
Packit ae235b
  if (c)
Packit ae235b
    {
Packit ae235b
      char *b = g_strndup (test_uri_base, c - test_uri_base);
Packit ae235b
      char *s = g_strconcat (b, bug_uri_snippet, c + 2, NULL);
Packit ae235b
      g_free (b);
Packit ae235b
      g_test_message ("Bug Reference: %s", s);
Packit ae235b
      g_free (s);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_test_message ("Bug Reference: %s%s", test_uri_base, bug_uri_snippet);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_get_root:
Packit ae235b
 *
Packit ae235b
 * Get the toplevel test suite for the test path API.
Packit ae235b
 *
Packit ae235b
 * Returns: the toplevel #GTestSuite
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
GTestSuite*
Packit ae235b
g_test_get_root (void)
Packit ae235b
{
Packit ae235b
  if (!test_suite_root)
Packit ae235b
    {
Packit ae235b
      test_suite_root = g_test_create_suite ("root");
Packit ae235b
      g_free (test_suite_root->name);
Packit ae235b
      test_suite_root->name = g_strdup ("");
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return test_suite_root;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_run:
Packit ae235b
 *
Packit ae235b
 * Runs all tests under the toplevel suite which can be retrieved
Packit ae235b
 * with g_test_get_root(). Similar to g_test_run_suite(), the test
Packit ae235b
 * cases to be run are filtered according to test path arguments
Packit ae235b
 * (`-p testpath` and `-s testpath`) as parsed by g_test_init().
Packit ae235b
 * g_test_run_suite() or g_test_run() may only be called once in a
Packit ae235b
 * program.
Packit ae235b
 *
Packit ae235b
 * In general, the tests and sub-suites within each suite are run in
Packit ae235b
 * the order in which they are defined. However, note that prior to
Packit ae235b
 * GLib 2.36, there was a bug in the `g_test_add_*`
Packit ae235b
 * functions which caused them to create multiple suites with the same
Packit ae235b
 * name, meaning that if you created tests "/foo/simple",
Packit ae235b
 * "/bar/simple", and "/foo/using-bar" in that order, they would get
Packit ae235b
 * run in that order (since g_test_run() would run the first "/foo"
Packit ae235b
 * suite, then the "/bar" suite, then the second "/foo" suite). As of
Packit ae235b
 * 2.36, this bug is fixed, and adding the tests in that order would
Packit ae235b
 * result in a running order of "/foo/simple", "/foo/using-bar",
Packit ae235b
 * "/bar/simple". If this new ordering is sub-optimal (because it puts
Packit ae235b
 * more-complicated tests before simpler ones, making it harder to
Packit ae235b
 * figure out exactly what has failed), you can fix it by changing the
Packit ae235b
 * test paths to group tests by suite in a way that will result in the
Packit ae235b
 * desired running order. Eg, "/simple/foo", "/simple/bar",
Packit ae235b
 * "/complex/foo-using-bar".
Packit ae235b
 *
Packit ae235b
 * However, you should never make the actual result of a test depend
Packit ae235b
 * on the order that tests are run in. If you need to ensure that some
Packit ae235b
 * particular code runs before or after a given test case, use
Packit ae235b
 * g_test_add(), which lets you specify setup and teardown functions.
Packit ae235b
 *
Packit ae235b
 * If all tests are skipped, this function will return 0 if
Packit ae235b
 * producing TAP output, or 77 (treated as "skip test" by Automake) otherwise.
Packit ae235b
 *
Packit ae235b
 * Returns: 0 on success, 1 on failure (assuming it returns at all),
Packit ae235b
 *   0 or 77 if all tests were skipped with g_test_skip()
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_test_run (void)
Packit ae235b
{
Packit ae235b
  if (g_test_run_suite (g_test_get_root()) != 0)
Packit ae235b
    return 1;
Packit ae235b
Packit ae235b
  /* 77 is special to Automake's default driver, but not Automake's TAP driver
Packit ae235b
   * or Perl's prove(1) TAP driver. */
Packit ae235b
  if (test_tap_log)
Packit ae235b
    return 0;
Packit ae235b
Packit ae235b
  if (test_run_count > 0 && test_run_count == test_skipped_count)
Packit ae235b
    return 77;
Packit ae235b
  else
Packit ae235b
    return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_create_case:
Packit ae235b
 * @test_name:     the name for the test case
Packit ae235b
 * @data_size:     the size of the fixture data structure
Packit ae235b
 * @test_data:     test data argument for the test functions
Packit ae235b
 * @data_setup:    (scope async): the function to set up the fixture data
Packit ae235b
 * @data_test:     (scope async): the actual test function
Packit ae235b
 * @data_teardown: (scope async): the function to teardown the fixture data
Packit ae235b
 *
Packit ae235b
 * Create a new #GTestCase, named @test_name, this API is fairly
Packit ae235b
 * low level, calling g_test_add() or g_test_add_func() is preferable.
Packit ae235b
 * When this test is executed, a fixture structure of size @data_size
Packit ae235b
 * will be automatically allocated and filled with zeros. Then @data_setup is
Packit ae235b
 * called to initialize the fixture. After fixture setup, the actual test
Packit ae235b
 * function @data_test is called. Once the test run completes, the
Packit ae235b
 * fixture structure is torn down by calling @data_teardown and
Packit ae235b
 * after that the memory is automatically released by the test framework.
Packit ae235b
 *
Packit ae235b
 * Splitting up a test run into fixture setup, test function and
Packit ae235b
 * fixture teardown is most useful if the same fixture is used for
Packit ae235b
 * multiple tests. In this cases, g_test_create_case() will be
Packit ae235b
 * called with the same fixture, but varying @test_name and
Packit ae235b
 * @data_test arguments.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated #GTestCase.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
GTestCase*
Packit ae235b
g_test_create_case (const char       *test_name,
Packit ae235b
                    gsize             data_size,
Packit ae235b
                    gconstpointer     test_data,
Packit ae235b
                    GTestFixtureFunc  data_setup,
Packit ae235b
                    GTestFixtureFunc  data_test,
Packit ae235b
                    GTestFixtureFunc  data_teardown)
Packit ae235b
{
Packit ae235b
  GTestCase *tc;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (test_name != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (strchr (test_name, '/') == NULL, NULL);
Packit ae235b
  g_return_val_if_fail (test_name[0] != 0, NULL);
Packit ae235b
  g_return_val_if_fail (data_test != NULL, NULL);
Packit ae235b
Packit ae235b
  tc = g_slice_new0 (GTestCase);
Packit ae235b
  tc->name = g_strdup (test_name);
Packit ae235b
  tc->test_data = (gpointer) test_data;
Packit ae235b
  tc->fixture_size = data_size;
Packit ae235b
  tc->fixture_setup = (void*) data_setup;
Packit ae235b
  tc->fixture_test = (void*) data_test;
Packit ae235b
  tc->fixture_teardown = (void*) data_teardown;
Packit ae235b
Packit ae235b
  return tc;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
find_suite (gconstpointer l, gconstpointer s)
Packit ae235b
{
Packit ae235b
  const GTestSuite *suite = l;
Packit ae235b
  const gchar *str = s;
Packit ae235b
Packit ae235b
  return strcmp (suite->name, str);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
find_case (gconstpointer l, gconstpointer s)
Packit ae235b
{
Packit ae235b
  const GTestCase *tc = l;
Packit ae235b
  const gchar *str = s;
Packit ae235b
Packit ae235b
  return strcmp (tc->name, str);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTestFixtureFunc:
Packit ae235b
 * @fixture: (not nullable): the test fixture
Packit ae235b
 * @user_data: the data provided when registering the test
Packit ae235b
 *
Packit ae235b
 * The type used for functions that operate on test fixtures.  This is
Packit ae235b
 * used for the fixture setup and teardown functions as well as for the
Packit ae235b
 * testcases themselves.
Packit ae235b
 *
Packit ae235b
 * @user_data is a pointer to the data that was given when registering
Packit ae235b
 * the test case.
Packit ae235b
 *
Packit ae235b
 * @fixture will be a pointer to the area of memory allocated by the
Packit ae235b
 * test framework, of the size requested.  If the requested size was
Packit ae235b
 * zero then @fixture will be equal to @user_data.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_add_vtable (const char       *testpath,
Packit ae235b
                   gsize             data_size,
Packit ae235b
                   gconstpointer     test_data,
Packit ae235b
                   GTestFixtureFunc  data_setup,
Packit ae235b
                   GTestFixtureFunc  fixture_test_func,
Packit ae235b
                   GTestFixtureFunc  data_teardown)
Packit ae235b
{
Packit ae235b
  gchar **segments;
Packit ae235b
  guint ui;
Packit ae235b
  GTestSuite *suite;
Packit ae235b
Packit ae235b
  g_return_if_fail (testpath != NULL);
Packit ae235b
  g_return_if_fail (g_path_is_absolute (testpath));
Packit ae235b
  g_return_if_fail (fixture_test_func != NULL);
Packit ae235b
Packit ae235b
  suite = g_test_get_root();
Packit ae235b
  segments = g_strsplit (testpath, "/", -1);
Packit ae235b
  for (ui = 0; segments[ui] != NULL; ui++)
Packit ae235b
    {
Packit ae235b
      const char *seg = segments[ui];
Packit ae235b
      gboolean islast = segments[ui + 1] == NULL;
Packit ae235b
      if (islast && !seg[0])
Packit ae235b
        g_error ("invalid test case path: %s", testpath);
Packit ae235b
      else if (!seg[0])
Packit ae235b
        continue;       /* initial or duplicate slash */
Packit ae235b
      else if (!islast)
Packit ae235b
        {
Packit ae235b
          GSList *l;
Packit ae235b
          GTestSuite *csuite;
Packit ae235b
          l = g_slist_find_custom (suite->suites, seg, find_suite);
Packit ae235b
          if (l)
Packit ae235b
            {
Packit ae235b
              csuite = l->data;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              csuite = g_test_create_suite (seg);
Packit ae235b
              g_test_suite_add_suite (suite, csuite);
Packit ae235b
            }
Packit ae235b
          suite = csuite;
Packit ae235b
        }
Packit ae235b
      else /* islast */
Packit ae235b
        {
Packit ae235b
          GTestCase *tc;
Packit ae235b
Packit ae235b
          if (g_slist_find_custom (suite->cases, seg, find_case))
Packit ae235b
            g_error ("duplicate test case path: %s", testpath);
Packit ae235b
Packit ae235b
          tc = g_test_create_case (seg, data_size, test_data, data_setup, fixture_test_func, data_teardown);
Packit ae235b
          g_test_suite_add (suite, tc);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  g_strfreev (segments);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_fail:
Packit ae235b
 *
Packit ae235b
 * Indicates that a test failed. This function can be called
Packit ae235b
 * multiple times from the same test. You can use this function
Packit ae235b
 * if your test failed in a recoverable way.
Packit ae235b
 * 
Packit ae235b
 * Do not use this function if the failure of a test could cause
Packit ae235b
 * other tests to malfunction.
Packit ae235b
 *
Packit ae235b
 * Calling this function will not stop the test from running, you
Packit ae235b
 * need to return from the test function yourself. So you can
Packit ae235b
 * produce additional diagnostic messages or even continue running
Packit ae235b
 * the test.
Packit ae235b
 *
Packit ae235b
 * If not called from inside a test, this function does nothing.
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_test_fail (void)
Packit ae235b
{
Packit ae235b
  test_run_success = G_TEST_RUN_FAILURE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_incomplete:
Packit ae235b
 * @msg: (nullable): explanation
Packit ae235b
 *
Packit ae235b
 * Indicates that a test failed because of some incomplete
Packit ae235b
 * functionality. This function can be called multiple times
Packit ae235b
 * from the same test.
Packit ae235b
 *
Packit ae235b
 * Calling this function will not stop the test from running, you
Packit ae235b
 * need to return from the test function yourself. So you can
Packit ae235b
 * produce additional diagnostic messages or even continue running
Packit ae235b
 * the test.
Packit ae235b
 *
Packit ae235b
 * If not called from inside a test, this function does nothing.
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_incomplete (const gchar *msg)
Packit ae235b
{
Packit ae235b
  test_run_success = G_TEST_RUN_INCOMPLETE;
Packit ae235b
  g_free (test_run_msg);
Packit ae235b
  test_run_msg = g_strdup (msg);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_skip:
Packit ae235b
 * @msg: (nullable): explanation
Packit ae235b
 *
Packit ae235b
 * Indicates that a test was skipped.
Packit ae235b
 *
Packit ae235b
 * Calling this function will not stop the test from running, you
Packit ae235b
 * need to return from the test function yourself. So you can
Packit ae235b
 * produce additional diagnostic messages or even continue running
Packit ae235b
 * the test.
Packit ae235b
 *
Packit ae235b
 * If not called from inside a test, this function does nothing.
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_skip (const gchar *msg)
Packit ae235b
{
Packit ae235b
  test_run_success = G_TEST_RUN_SKIPPED;
Packit ae235b
  g_free (test_run_msg);
Packit ae235b
  test_run_msg = g_strdup (msg);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_failed:
Packit ae235b
 *
Packit ae235b
 * Returns whether a test has already failed. This will
Packit ae235b
 * be the case when g_test_fail(), g_test_incomplete()
Packit ae235b
 * or g_test_skip() have been called, but also if an
Packit ae235b
 * assertion has failed.
Packit ae235b
 *
Packit ae235b
 * This can be useful to return early from a test if
Packit ae235b
 * continuing after a failed assertion might be harmful.
Packit ae235b
 *
Packit ae235b
 * The return value of this function is only meaningful
Packit ae235b
 * if it is called from inside a test function.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the test has failed
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_test_failed (void)
Packit ae235b
{
Packit ae235b
  return test_run_success != G_TEST_RUN_SUCCESS;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_set_nonfatal_assertions:
Packit ae235b
 *
Packit ae235b
 * Changes the behaviour of g_assert_cmpstr(), g_assert_cmpint(),
Packit ae235b
 * g_assert_cmpuint(), g_assert_cmphex(), g_assert_cmpfloat(),
Packit ae235b
 * g_assert_true(), g_assert_false(), g_assert_null(), g_assert_no_error(),
Packit ae235b
 * g_assert_error(), g_test_assert_expected_messages() and the various
Packit ae235b
 * g_test_trap_assert_*() macros to not abort to program, but instead
Packit ae235b
 * call g_test_fail() and continue. (This also changes the behavior of
Packit ae235b
 * g_test_fail() so that it will not cause the test program to abort
Packit ae235b
 * after completing the failed test.)
Packit ae235b
 *
Packit ae235b
 * Note that the g_assert_not_reached() and g_assert() are not
Packit ae235b
 * affected by this.
Packit ae235b
 *
Packit ae235b
 * This function can only be called after g_test_init().
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_set_nonfatal_assertions (void)
Packit ae235b
{
Packit ae235b
  if (!g_test_config_vars->test_initialized)
Packit ae235b
    g_error ("g_test_set_nonfatal_assertions called without g_test_init");
Packit ae235b
  test_nonfatal_assertions = TRUE;
Packit ae235b
  test_mode_fatal = FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTestFunc:
Packit ae235b
 *
Packit ae235b
 * The type used for test case functions.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_add_func:
Packit ae235b
 * @testpath:  /-separated test case path name for the test.
Packit ae235b
 * @test_func: (scope async):  The test function to invoke for this test.
Packit ae235b
 *
Packit ae235b
 * Create a new test case, similar to g_test_create_case(). However
Packit ae235b
 * the test is assumed to use no fixture, and test suites are automatically
Packit ae235b
 * created on the fly and added to the root fixture, based on the
Packit ae235b
 * slash-separated portions of @testpath.
Packit ae235b
 *
Packit ae235b
 * If @testpath includes the component "subprocess" anywhere in it,
Packit ae235b
 * the test will be skipped by default, and only run if explicitly
Packit ae235b
 * required via the `-p` command-line option or g_test_trap_subprocess().
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_add_func (const char *testpath,
Packit ae235b
                 GTestFunc   test_func)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (testpath != NULL);
Packit ae235b
  g_return_if_fail (testpath[0] == '/');
Packit ae235b
  g_return_if_fail (test_func != NULL);
Packit ae235b
  g_test_add_vtable (testpath, 0, NULL, NULL, (GTestFixtureFunc) test_func, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GTestDataFunc:
Packit ae235b
 * @user_data: the data provided when registering the test
Packit ae235b
 *
Packit ae235b
 * The type used for test case functions that take an extra pointer
Packit ae235b
 * argument.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_add_data_func:
Packit ae235b
 * @testpath:  /-separated test case path name for the test.
Packit ae235b
 * @test_data: Test data argument for the test function.
Packit ae235b
 * @test_func: (scope async): The test function to invoke for this test.
Packit ae235b
 *
Packit ae235b
 * Create a new test case, similar to g_test_create_case(). However
Packit ae235b
 * the test is assumed to use no fixture, and test suites are automatically
Packit ae235b
 * created on the fly and added to the root fixture, based on the
Packit ae235b
 * slash-separated portions of @testpath. The @test_data argument
Packit ae235b
 * will be passed as first argument to @test_func.
Packit ae235b
 *
Packit ae235b
 * If @testpath includes the component "subprocess" anywhere in it,
Packit ae235b
 * the test will be skipped by default, and only run if explicitly
Packit ae235b
 * required via the `-p` command-line option or g_test_trap_subprocess().
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_add_data_func (const char     *testpath,
Packit ae235b
                      gconstpointer   test_data,
Packit ae235b
                      GTestDataFunc   test_func)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (testpath != NULL);
Packit ae235b
  g_return_if_fail (testpath[0] == '/');
Packit ae235b
  g_return_if_fail (test_func != NULL);
Packit ae235b
Packit ae235b
  g_test_add_vtable (testpath, 0, test_data, NULL, (GTestFixtureFunc) test_func, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_add_data_func_full:
Packit ae235b
 * @testpath: /-separated test case path name for the test.
Packit ae235b
 * @test_data: Test data argument for the test function.
Packit ae235b
 * @test_func: The test function to invoke for this test.
Packit ae235b
 * @data_free_func: #GDestroyNotify for @test_data.
Packit ae235b
 *
Packit ae235b
 * Create a new test case, as with g_test_add_data_func(), but freeing
Packit ae235b
 * @test_data after the test run is complete.
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_add_data_func_full (const char     *testpath,
Packit ae235b
                           gpointer        test_data,
Packit ae235b
                           GTestDataFunc   test_func,
Packit ae235b
                           GDestroyNotify  data_free_func)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (testpath != NULL);
Packit ae235b
  g_return_if_fail (testpath[0] == '/');
Packit ae235b
  g_return_if_fail (test_func != NULL);
Packit ae235b
Packit ae235b
  g_test_add_vtable (testpath, 0, test_data, NULL,
Packit ae235b
                     (GTestFixtureFunc) test_func,
Packit ae235b
                     (GTestFixtureFunc) data_free_func);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_test_suite_case_exists (GTestSuite *suite,
Packit ae235b
                          const char *test_path)
Packit ae235b
{
Packit ae235b
  GSList *iter;
Packit ae235b
  char *slash;
Packit ae235b
  GTestCase *tc;
Packit ae235b
Packit ae235b
  test_path++;
Packit ae235b
  slash = strchr (test_path, '/');
Packit ae235b
Packit ae235b
  if (slash)
Packit ae235b
    {
Packit ae235b
      for (iter = suite->suites; iter; iter = iter->next)
Packit ae235b
        {
Packit ae235b
          GTestSuite *child_suite = iter->data;
Packit ae235b
Packit ae235b
          if (!strncmp (child_suite->name, test_path, slash - test_path))
Packit ae235b
            if (g_test_suite_case_exists (child_suite, slash))
Packit ae235b
              return TRUE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      for (iter = suite->cases; iter; iter = iter->next)
Packit ae235b
        {
Packit ae235b
          tc = iter->data;
Packit ae235b
          if (!strcmp (tc->name, test_path))
Packit ae235b
            return TRUE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_create_suite:
Packit ae235b
 * @suite_name: a name for the suite
Packit ae235b
 *
Packit ae235b
 * Create a new test suite with the name @suite_name.
Packit ae235b
 *
Packit ae235b
 * Returns: A newly allocated #GTestSuite instance.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
GTestSuite*
Packit ae235b
g_test_create_suite (const char *suite_name)
Packit ae235b
{
Packit ae235b
  GTestSuite *ts;
Packit ae235b
  g_return_val_if_fail (suite_name != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (strchr (suite_name, '/') == NULL, NULL);
Packit ae235b
  g_return_val_if_fail (suite_name[0] != 0, NULL);
Packit ae235b
  ts = g_slice_new0 (GTestSuite);
Packit ae235b
  ts->name = g_strdup (suite_name);
Packit ae235b
  return ts;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_suite_add:
Packit ae235b
 * @suite: a #GTestSuite
Packit ae235b
 * @test_case: a #GTestCase
Packit ae235b
 *
Packit ae235b
 * Adds @test_case to @suite.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_suite_add (GTestSuite     *suite,
Packit ae235b
                  GTestCase      *test_case)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (suite != NULL);
Packit ae235b
  g_return_if_fail (test_case != NULL);
Packit ae235b
Packit ae235b
  suite->cases = g_slist_append (suite->cases, test_case);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_suite_add_suite:
Packit ae235b
 * @suite:       a #GTestSuite
Packit ae235b
 * @nestedsuite: another #GTestSuite
Packit ae235b
 *
Packit ae235b
 * Adds @nestedsuite to @suite.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_suite_add_suite (GTestSuite     *suite,
Packit ae235b
                        GTestSuite     *nestedsuite)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (suite != NULL);
Packit ae235b
  g_return_if_fail (nestedsuite != NULL);
Packit ae235b
Packit ae235b
  suite->suites = g_slist_append (suite->suites, nestedsuite);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_queue_free:
Packit ae235b
 * @gfree_pointer: the pointer to be stored.
Packit ae235b
 *
Packit ae235b
 * Enqueue a pointer to be released with g_free() during the next
Packit ae235b
 * teardown phase. This is equivalent to calling g_test_queue_destroy()
Packit ae235b
 * with a destroy callback of g_free().
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_queue_free (gpointer gfree_pointer)
Packit ae235b
{
Packit ae235b
  if (gfree_pointer)
Packit ae235b
    g_test_queue_destroy (g_free, gfree_pointer);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_queue_destroy:
Packit ae235b
 * @destroy_func:       Destroy callback for teardown phase.
Packit ae235b
 * @destroy_data:       Destroy callback data.
Packit ae235b
 *
Packit ae235b
 * This function enqueus a callback @destroy_func to be executed
Packit ae235b
 * during the next test case teardown phase. This is most useful
Packit ae235b
 * to auto destruct allocated test resources at the end of a test run.
Packit ae235b
 * Resources are released in reverse queue order, that means enqueueing
Packit ae235b
 * callback A before callback B will cause B() to be called before
Packit ae235b
 * A() during teardown.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_queue_destroy (GDestroyNotify destroy_func,
Packit ae235b
                      gpointer       destroy_data)
Packit ae235b
{
Packit ae235b
  DestroyEntry *dentry;
Packit ae235b
Packit ae235b
  g_return_if_fail (destroy_func != NULL);
Packit ae235b
Packit ae235b
  dentry = g_slice_new0 (DestroyEntry);
Packit ae235b
  dentry->destroy_func = destroy_func;
Packit ae235b
  dentry->destroy_data = destroy_data;
Packit ae235b
  dentry->next = test_destroy_queue;
Packit ae235b
  test_destroy_queue = dentry;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
test_case_run (GTestCase *tc)
Packit ae235b
{
Packit ae235b
  gchar *old_base = g_strdup (test_uri_base);
Packit ae235b
  GSList **old_free_list, *filename_free_list = NULL;
Packit ae235b
  gboolean success = G_TEST_RUN_SUCCESS;
Packit ae235b
Packit ae235b
  old_free_list = test_filename_free_list;
Packit ae235b
  test_filename_free_list = &filename_free_list;
Packit ae235b
Packit ae235b
  if (++test_run_count <= test_startup_skip_count)
Packit ae235b
    g_test_log (G_TEST_LOG_SKIP_CASE, test_run_name, NULL, 0, NULL);
Packit ae235b
  else if (test_run_list)
Packit ae235b
    {
Packit ae235b
      g_print ("%s\n", test_run_name);
Packit ae235b
      g_test_log (G_TEST_LOG_LIST_CASE, test_run_name, NULL, 0, NULL);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      GTimer *test_run_timer = g_timer_new();
Packit ae235b
      long double largs[3];
Packit ae235b
      void *fixture;
Packit ae235b
      g_test_log (G_TEST_LOG_START_CASE, test_run_name, NULL, 0, NULL);
Packit ae235b
      test_run_forks = 0;
Packit ae235b
      test_run_success = G_TEST_RUN_SUCCESS;
Packit ae235b
      g_clear_pointer (&test_run_msg, g_free);
Packit ae235b
      g_test_log_set_fatal_handler (NULL, NULL);
Packit ae235b
      if (test_paths_skipped && g_slist_find_custom (test_paths_skipped, test_run_name, (GCompareFunc)g_strcmp0))
Packit ae235b
        g_test_skip ("by request (-s option)");
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          g_timer_start (test_run_timer);
Packit ae235b
          fixture = tc->fixture_size ? g_malloc0 (tc->fixture_size) : tc->test_data;
Packit ae235b
          test_run_seed (test_run_seedstr);
Packit ae235b
          if (tc->fixture_setup)
Packit ae235b
            tc->fixture_setup (fixture, tc->test_data);
Packit ae235b
          tc->fixture_test (fixture, tc->test_data);
Packit ae235b
          test_trap_clear();
Packit ae235b
          while (test_destroy_queue)
Packit ae235b
            {
Packit ae235b
              DestroyEntry *dentry = test_destroy_queue;
Packit ae235b
              test_destroy_queue = dentry->next;
Packit ae235b
              dentry->destroy_func (dentry->destroy_data);
Packit ae235b
              g_slice_free (DestroyEntry, dentry);
Packit ae235b
            }
Packit ae235b
          if (tc->fixture_teardown)
Packit ae235b
            tc->fixture_teardown (fixture, tc->test_data);
Packit ae235b
          if (tc->fixture_size)
Packit ae235b
            g_free (fixture);
Packit ae235b
          g_timer_stop (test_run_timer);
Packit ae235b
        }
Packit ae235b
      success = test_run_success;
Packit ae235b
      test_run_success = G_TEST_RUN_FAILURE;
Packit ae235b
      largs[0] = success; /* OK */
Packit ae235b
      largs[1] = test_run_forks;
Packit ae235b
      largs[2] = g_timer_elapsed (test_run_timer, NULL);
Packit ae235b
      g_test_log (G_TEST_LOG_STOP_CASE, test_run_name, test_run_msg, G_N_ELEMENTS (largs), largs);
Packit ae235b
      g_clear_pointer (&test_run_msg, g_free);
Packit ae235b
      g_timer_destroy (test_run_timer);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_slist_free_full (filename_free_list, g_free);
Packit ae235b
  test_filename_free_list = old_free_list;
Packit ae235b
  g_free (test_uri_base);
Packit ae235b
  test_uri_base = old_base;
Packit ae235b
Packit ae235b
  return (success == G_TEST_RUN_SUCCESS ||
Packit ae235b
          success == G_TEST_RUN_SKIPPED);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
path_has_prefix (const char *path,
Packit ae235b
                 const char *prefix)
Packit ae235b
{
Packit ae235b
  int prefix_len = strlen (prefix);
Packit ae235b
Packit ae235b
  return (strncmp (path, prefix, prefix_len) == 0 &&
Packit ae235b
          (path[prefix_len] == '\0' ||
Packit ae235b
           path[prefix_len] == '/'));
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
test_should_run (const char *test_path,
Packit ae235b
                 const char *cmp_path)
Packit ae235b
{
Packit ae235b
  if (strstr (test_run_name, "/subprocess"))
Packit ae235b
    {
Packit ae235b
      if (g_strcmp0 (test_path, cmp_path) == 0)
Packit ae235b
        return TRUE;
Packit ae235b
Packit ae235b
      if (g_test_verbose ())
Packit ae235b
        g_print ("GTest: skipping: %s\n", test_run_name);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return !cmp_path || path_has_prefix (test_path, cmp_path);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Recurse through @suite, running tests matching @path (or all tests
Packit ae235b
 * if @path is %NULL).
Packit ae235b
 */
Packit ae235b
static int
Packit ae235b
g_test_run_suite_internal (GTestSuite *suite,
Packit ae235b
                           const char *path)
Packit ae235b
{
Packit ae235b
  guint n_bad = 0;
Packit ae235b
  gchar *old_name = test_run_name;
Packit ae235b
  GSList *iter;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (suite != NULL, -1);
Packit ae235b
Packit ae235b
  g_test_log (G_TEST_LOG_START_SUITE, suite->name, NULL, 0, NULL);
Packit ae235b
Packit ae235b
  for (iter = suite->cases; iter; iter = iter->next)
Packit ae235b
    {
Packit ae235b
      GTestCase *tc = iter->data;
Packit ae235b
Packit ae235b
      test_run_name = g_build_path ("/", old_name, tc->name, NULL);
Packit ae235b
      if (test_should_run (test_run_name, path))
Packit ae235b
        {
Packit ae235b
          if (!test_case_run (tc))
Packit ae235b
            n_bad++;
Packit ae235b
        }
Packit ae235b
      g_free (test_run_name);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (iter = suite->suites; iter; iter = iter->next)
Packit ae235b
    {
Packit ae235b
      GTestSuite *ts = iter->data;
Packit ae235b
Packit ae235b
      test_run_name = g_build_path ("/", old_name, ts->name, NULL);
Packit ae235b
      if (!path || path_has_prefix (path, test_run_name))
Packit ae235b
        n_bad += g_test_run_suite_internal (ts, path);
Packit ae235b
      g_free (test_run_name);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  test_run_name = old_name;
Packit ae235b
Packit ae235b
  g_test_log (G_TEST_LOG_STOP_SUITE, suite->name, NULL, 0, NULL);
Packit ae235b
Packit ae235b
  return n_bad;
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
g_test_suite_count (GTestSuite *suite)
Packit ae235b
{
Packit ae235b
  int n = 0;
Packit ae235b
  GSList *iter;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (suite != NULL, -1);
Packit ae235b
Packit ae235b
  for (iter = suite->cases; iter; iter = iter->next)
Packit ae235b
    {
Packit ae235b
      GTestCase *tc = iter->data;
Packit ae235b
Packit ae235b
      if (strcmp (tc->name, "subprocess") != 0)
Packit ae235b
        n++;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  for (iter = suite->suites; iter; iter = iter->next)
Packit ae235b
    {
Packit ae235b
      GTestSuite *ts = iter->data;
Packit ae235b
Packit ae235b
      if (strcmp (ts->name, "subprocess") != 0)
Packit ae235b
        n += g_test_suite_count (ts);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return n;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_run_suite:
Packit ae235b
 * @suite: a #GTestSuite
Packit ae235b
 *
Packit ae235b
 * Execute the tests within @suite and all nested #GTestSuites.
Packit ae235b
 * The test suites to be executed are filtered according to
Packit ae235b
 * test path arguments (`-p testpath` and `-s testpath`) as parsed by
Packit ae235b
 * g_test_init(). See the g_test_run() documentation for more
Packit ae235b
 * information on the order that tests are run in.
Packit ae235b
 *
Packit ae235b
 * g_test_run_suite() or g_test_run() may only be called once
Packit ae235b
 * in a program.
Packit ae235b
 *
Packit ae235b
 * Returns: 0 on success
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_test_run_suite (GTestSuite *suite)
Packit ae235b
{
Packit ae235b
  int n_bad = 0;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (g_test_run_once == TRUE, -1);
Packit ae235b
Packit ae235b
  g_test_run_once = FALSE;
Packit ae235b
  test_count = g_test_suite_count (suite);
Packit ae235b
Packit ae235b
  test_run_name = g_strdup_printf ("/%s", suite->name);
Packit ae235b
Packit ae235b
  if (test_paths)
Packit ae235b
    {
Packit ae235b
      GSList *iter;
Packit ae235b
Packit ae235b
      for (iter = test_paths; iter; iter = iter->next)
Packit ae235b
        n_bad += g_test_run_suite_internal (suite, iter->data);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    n_bad = g_test_run_suite_internal (suite, NULL);
Packit ae235b
Packit ae235b
  g_free (test_run_name);
Packit ae235b
  test_run_name = NULL;
Packit ae235b
Packit ae235b
  return n_bad;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
gtest_default_log_handler (const gchar    *log_domain,
Packit ae235b
                           GLogLevelFlags  log_level,
Packit ae235b
                           const gchar    *message,
Packit ae235b
                           gpointer        unused_data)
Packit ae235b
{
Packit ae235b
  const gchar *strv[16];
Packit ae235b
  gboolean fatal = FALSE;
Packit ae235b
  gchar *msg;
Packit ae235b
  guint i = 0;
Packit ae235b
Packit ae235b
  if (log_domain)
Packit ae235b
    {
Packit ae235b
      strv[i++] = log_domain;
Packit ae235b
      strv[i++] = "-";
Packit ae235b
    }
Packit ae235b
  if (log_level & G_LOG_FLAG_FATAL)
Packit ae235b
    {
Packit ae235b
      strv[i++] = "FATAL-";
Packit ae235b
      fatal = TRUE;
Packit ae235b
    }
Packit ae235b
  if (log_level & G_LOG_FLAG_RECURSION)
Packit ae235b
    strv[i++] = "RECURSIVE-";
Packit ae235b
  if (log_level & G_LOG_LEVEL_ERROR)
Packit ae235b
    strv[i++] = "ERROR";
Packit ae235b
  if (log_level & G_LOG_LEVEL_CRITICAL)
Packit ae235b
    strv[i++] = "CRITICAL";
Packit ae235b
  if (log_level & G_LOG_LEVEL_WARNING)
Packit ae235b
    strv[i++] = "WARNING";
Packit ae235b
  if (log_level & G_LOG_LEVEL_MESSAGE)
Packit ae235b
    strv[i++] = "MESSAGE";
Packit ae235b
  if (log_level & G_LOG_LEVEL_INFO)
Packit ae235b
    strv[i++] = "INFO";
Packit ae235b
  if (log_level & G_LOG_LEVEL_DEBUG)
Packit ae235b
    strv[i++] = "DEBUG";
Packit ae235b
  strv[i++] = ": ";
Packit ae235b
  strv[i++] = message;
Packit ae235b
  strv[i++] = NULL;
Packit ae235b
Packit ae235b
  msg = g_strjoinv ("", (gchar**) strv);
Packit ae235b
  g_test_log (fatal ? G_TEST_LOG_ERROR : G_TEST_LOG_MESSAGE, msg, NULL, 0, NULL);
Packit ae235b
  g_log_default_handler (log_domain, log_level, message, unused_data);
Packit ae235b
Packit ae235b
  g_free (msg);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_assertion_message (const char     *domain,
Packit ae235b
                     const char     *file,
Packit ae235b
                     int             line,
Packit ae235b
                     const char     *func,
Packit ae235b
                     const char     *message)
Packit ae235b
{
Packit ae235b
  char lstr[32];
Packit ae235b
  char *s;
Packit ae235b
Packit ae235b
  if (!message)
Packit ae235b
    message = "code should not be reached";
Packit ae235b
  g_snprintf (lstr, 32, "%d", line);
Packit ae235b
  s = g_strconcat (domain ? domain : "", domain && domain[0] ? ":" : "",
Packit ae235b
                   "ERROR:", file, ":", lstr, ":",
Packit ae235b
                   func, func[0] ? ":" : "",
Packit ae235b
                   " ", message, NULL);
Packit ae235b
  g_printerr ("**\n%s\n", s);
Packit ae235b
Packit ae235b
  /* Don't print a fatal error indication if assertions are non-fatal, or
Packit ae235b
   * if we are a child process that might be sharing the parent's stdout. */
Packit ae235b
  if (test_nonfatal_assertions || test_in_subprocess || test_in_forked_child)
Packit ae235b
    g_test_log (G_TEST_LOG_MESSAGE, s, NULL, 0, NULL);
Packit ae235b
  else
Packit ae235b
    g_test_log (G_TEST_LOG_ERROR, s, NULL, 0, NULL);
Packit ae235b
Packit ae235b
  if (test_nonfatal_assertions)
Packit ae235b
    {
Packit ae235b
      g_free (s);
Packit ae235b
      g_test_fail ();
Packit ae235b
      return;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* store assertion message in global variable, so that it can be found in a
Packit ae235b
   * core dump */
Packit ae235b
  if (__glib_assert_msg != NULL)
Packit ae235b
    /* free the old one */
Packit ae235b
    free (__glib_assert_msg);
Packit ae235b
  __glib_assert_msg = (char*) malloc (strlen (s) + 1);
Packit ae235b
  strcpy (__glib_assert_msg, s);
Packit ae235b
Packit ae235b
  g_free (s);
Packit ae235b
Packit ae235b
  if (test_in_subprocess)
Packit ae235b
    {
Packit ae235b
      /* If this is a test case subprocess then it probably hit this
Packit ae235b
       * assertion on purpose, so just exit() rather than abort()ing,
Packit ae235b
       * to avoid triggering any system crash-reporting daemon.
Packit ae235b
       */
Packit ae235b
      _exit (1);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    g_abort ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_assertion_message_expr: (skip)
Packit ae235b
 * @domain: (nullable):
Packit ae235b
 * @file:
Packit ae235b
 * @line:
Packit ae235b
 * @func:
Packit ae235b
 * @expr: (nullable):
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_assertion_message_expr (const char     *domain,
Packit ae235b
                          const char     *file,
Packit ae235b
                          int             line,
Packit ae235b
                          const char     *func,
Packit ae235b
                          const char     *expr)
Packit ae235b
{
Packit ae235b
  char *s;
Packit ae235b
  if (!expr)
Packit ae235b
    s = g_strdup ("code should not be reached");
Packit ae235b
  else
Packit ae235b
    s = g_strconcat ("assertion failed: (", expr, ")", NULL);
Packit ae235b
  g_assertion_message (domain, file, line, func, s);
Packit ae235b
  g_free (s);
Packit ae235b
Packit ae235b
  /* Normally g_assertion_message() won't return, but we need this for
Packit ae235b
   * when test_nonfatal_assertions is set, since
Packit ae235b
   * g_assertion_message_expr() is used for always-fatal assertions.
Packit ae235b
   */
Packit ae235b
  if (test_in_subprocess)
Packit ae235b
    _exit (1);
Packit ae235b
  else
Packit ae235b
    g_abort ();
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_assertion_message_cmpnum (const char     *domain,
Packit ae235b
                            const char     *file,
Packit ae235b
                            int             line,
Packit ae235b
                            const char     *func,
Packit ae235b
                            const char     *expr,
Packit ae235b
                            long double     arg1,
Packit ae235b
                            const char     *cmp,
Packit ae235b
                            long double     arg2,
Packit ae235b
                            char            numtype)
Packit ae235b
{
Packit ae235b
  char *s = NULL;
Packit ae235b
Packit ae235b
  switch (numtype)
Packit ae235b
    {
Packit ae235b
    case 'i':   s = g_strdup_printf ("assertion failed (%s): (%" G_GINT64_MODIFIER "i %s %" G_GINT64_MODIFIER "i)", expr, (gint64) arg1, cmp, (gint64) arg2); break;
Packit ae235b
    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;
Packit ae235b
    case 'f':   s = g_strdup_printf ("assertion failed (%s): (%.9g %s %.9g)", expr, (double) arg1, cmp, (double) arg2); break;
Packit ae235b
      /* ideally use: floats=%.7g double=%.17g */
Packit ae235b
    }
Packit ae235b
  g_assertion_message (domain, file, line, func, s);
Packit ae235b
  g_free (s);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_assertion_message_cmpstr (const char     *domain,
Packit ae235b
                            const char     *file,
Packit ae235b
                            int             line,
Packit ae235b
                            const char     *func,
Packit ae235b
                            const char     *expr,
Packit ae235b
                            const char     *arg1,
Packit ae235b
                            const char     *cmp,
Packit ae235b
                            const char     *arg2)
Packit ae235b
{
Packit ae235b
  char *a1, *a2, *s, *t1 = NULL, *t2 = NULL;
Packit ae235b
  a1 = arg1 ? g_strconcat ("\"", t1 = g_strescape (arg1, NULL), "\"", NULL) : g_strdup ("NULL");
Packit ae235b
  a2 = arg2 ? g_strconcat ("\"", t2 = g_strescape (arg2, NULL), "\"", NULL) : g_strdup ("NULL");
Packit ae235b
  g_free (t1);
Packit ae235b
  g_free (t2);
Packit ae235b
  s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2);
Packit ae235b
  g_free (a1);
Packit ae235b
  g_free (a2);
Packit ae235b
  g_assertion_message (domain, file, line, func, s);
Packit ae235b
  g_free (s);
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_assertion_message_error (const char     *domain,
Packit ae235b
			   const char     *file,
Packit ae235b
			   int             line,
Packit ae235b
			   const char     *func,
Packit ae235b
			   const char     *expr,
Packit ae235b
			   const GError   *error,
Packit ae235b
			   GQuark          error_domain,
Packit ae235b
			   int             error_code)
Packit ae235b
{
Packit ae235b
  GString *gstring;
Packit ae235b
Packit ae235b
  /* This is used by both g_assert_error() and g_assert_no_error(), so there
Packit ae235b
   * are three cases: expected an error but got the wrong error, expected
Packit ae235b
   * an error but got no error, and expected no error but got an error.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  gstring = g_string_new ("assertion failed ");
Packit ae235b
  if (error_domain)
Packit ae235b
      g_string_append_printf (gstring, "(%s == (%s, %d)): ", expr,
Packit ae235b
			      g_quark_to_string (error_domain), error_code);
Packit ae235b
  else
Packit ae235b
    g_string_append_printf (gstring, "(%s == NULL): ", expr);
Packit ae235b
Packit ae235b
  if (error)
Packit ae235b
      g_string_append_printf (gstring, "%s (%s, %d)", error->message,
Packit ae235b
			      g_quark_to_string (error->domain), error->code);
Packit ae235b
  else
Packit ae235b
    g_string_append_printf (gstring, "%s is NULL", expr);
Packit ae235b
Packit ae235b
  g_assertion_message (domain, file, line, func, gstring->str);
Packit ae235b
  g_string_free (gstring, TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_strcmp0:
Packit ae235b
 * @str1: (nullable): a C string or %NULL
Packit ae235b
 * @str2: (nullable): another C string or %NULL
Packit ae235b
 *
Packit ae235b
 * Compares @str1 and @str2 like strcmp(). Handles %NULL
Packit ae235b
 * gracefully by sorting it before non-%NULL strings.
Packit ae235b
 * Comparing two %NULL pointers returns 0.
Packit ae235b
 *
Packit ae235b
 * Returns: an integer less than, equal to, or greater than zero, if @str1 is <, == or > than @str2.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
int
Packit ae235b
g_strcmp0 (const char     *str1,
Packit ae235b
           const char     *str2)
Packit ae235b
{
Packit ae235b
  if (!str1)
Packit ae235b
    return -(str1 != str2);
Packit ae235b
  if (!str2)
Packit ae235b
    return str1 != str2;
Packit ae235b
  return strcmp (str1, str2);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_trap_clear (void)
Packit ae235b
{
Packit ae235b
  test_trap_last_status = 0;
Packit ae235b
  test_trap_last_pid = 0;
Packit ae235b
  g_clear_pointer (&test_trap_last_subprocess, g_free);
Packit ae235b
  g_clear_pointer (&test_trap_last_stdout, g_free);
Packit ae235b
  g_clear_pointer (&test_trap_last_stderr, g_free);
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
Packit ae235b
static int
Packit ae235b
sane_dup2 (int fd1,
Packit ae235b
           int fd2)
Packit ae235b
{
Packit ae235b
  int ret;
Packit ae235b
  do
Packit ae235b
    ret = dup2 (fd1, fd2);
Packit ae235b
  while (ret < 0 && errno == EINTR);
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
typedef struct {
Packit ae235b
  GPid pid;
Packit ae235b
  GMainLoop *loop;
Packit ae235b
  int child_status;  /* unmodified platform-specific status */
Packit ae235b
Packit ae235b
  GIOChannel *stdout_io;
Packit ae235b
  gboolean echo_stdout;
Packit ae235b
  GString *stdout_str;
Packit ae235b
Packit ae235b
  GIOChannel *stderr_io;
Packit ae235b
  gboolean echo_stderr;
Packit ae235b
  GString *stderr_str;
Packit ae235b
} WaitForChildData;
Packit ae235b
Packit ae235b
static void
Packit ae235b
check_complete (WaitForChildData *data)
Packit ae235b
{
Packit ae235b
  if (data->child_status != -1 && data->stdout_io == NULL && data->stderr_io == NULL)
Packit ae235b
    g_main_loop_quit (data->loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
child_exited (GPid     pid,
Packit ae235b
              gint     status,
Packit ae235b
              gpointer user_data)
Packit ae235b
{
Packit ae235b
  WaitForChildData *data = user_data;
Packit ae235b
Packit ae235b
  g_assert (status != -1);
Packit ae235b
  data->child_status = status;
Packit ae235b
Packit ae235b
  check_complete (data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
child_timeout (gpointer user_data)
Packit ae235b
{
Packit ae235b
  WaitForChildData *data = user_data;
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  TerminateProcess (data->pid, G_TEST_STATUS_TIMED_OUT);
Packit ae235b
#else
Packit ae235b
  kill (data->pid, SIGALRM);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
child_read (GIOChannel *io, GIOCondition cond, gpointer user_data)
Packit ae235b
{
Packit ae235b
  WaitForChildData *data = user_data;
Packit ae235b
  GIOStatus status;
Packit ae235b
  gsize nread, nwrote, total;
Packit ae235b
  gchar buf[4096];
Packit ae235b
  FILE *echo_file = NULL;
Packit ae235b
Packit ae235b
  status = g_io_channel_read_chars (io, buf, sizeof (buf), &nread, NULL);
Packit ae235b
  if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF)
Packit ae235b
    {
Packit ae235b
      // FIXME data->error = (status == G_IO_STATUS_ERROR);
Packit ae235b
      if (io == data->stdout_io)
Packit ae235b
        g_clear_pointer (&data->stdout_io, g_io_channel_unref);
Packit ae235b
      else
Packit ae235b
        g_clear_pointer (&data->stderr_io, g_io_channel_unref);
Packit ae235b
Packit ae235b
      check_complete (data);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  else if (status == G_IO_STATUS_AGAIN)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  if (io == data->stdout_io)
Packit ae235b
    {
Packit ae235b
      g_string_append_len (data->stdout_str, buf, nread);
Packit ae235b
      if (data->echo_stdout)
Packit ae235b
        echo_file = stdout;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_string_append_len (data->stderr_str, buf, nread);
Packit ae235b
      if (data->echo_stderr)
Packit ae235b
        echo_file = stderr;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (echo_file)
Packit ae235b
    {
Packit ae235b
      for (total = 0; total < nread; total += nwrote)
Packit ae235b
        {
Packit ae235b
          int errsv;
Packit ae235b
Packit ae235b
          nwrote = fwrite (buf + total, 1, nread - total, echo_file);
Packit ae235b
          errsv = errno;
Packit ae235b
          if (nwrote == 0)
Packit ae235b
            g_error ("write failed: %s", g_strerror (errsv));
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
wait_for_child (GPid pid,
Packit ae235b
                int stdout_fd, gboolean echo_stdout,
Packit ae235b
                int stderr_fd, gboolean echo_stderr,
Packit ae235b
                guint64 timeout)
Packit ae235b
{
Packit ae235b
  WaitForChildData data;
Packit ae235b
  GMainContext *context;
Packit ae235b
  GSource *source;
Packit ae235b
Packit ae235b
  data.pid = pid;
Packit ae235b
  data.child_status = -1;
Packit ae235b
Packit ae235b
  context = g_main_context_new ();
Packit ae235b
  data.loop = g_main_loop_new (context, FALSE);
Packit ae235b
Packit ae235b
  source = g_child_watch_source_new (pid);
Packit ae235b
  g_source_set_callback (source, (GSourceFunc) child_exited, &data, NULL);
Packit ae235b
  g_source_attach (source, context);
Packit ae235b
  g_source_unref (source);
Packit ae235b
Packit ae235b
  data.echo_stdout = echo_stdout;
Packit ae235b
  data.stdout_str = g_string_new (NULL);
Packit ae235b
  data.stdout_io = g_io_channel_unix_new (stdout_fd);
Packit ae235b
  g_io_channel_set_close_on_unref (data.stdout_io, TRUE);
Packit ae235b
  g_io_channel_set_encoding (data.stdout_io, NULL, NULL);
Packit ae235b
  g_io_channel_set_buffered (data.stdout_io, FALSE);
Packit ae235b
  source = g_io_create_watch (data.stdout_io, G_IO_IN | G_IO_ERR | G_IO_HUP);
Packit ae235b
  g_source_set_callback (source, (GSourceFunc) child_read, &data, NULL);
Packit ae235b
  g_source_attach (source, context);
Packit ae235b
  g_source_unref (source);
Packit ae235b
Packit ae235b
  data.echo_stderr = echo_stderr;
Packit ae235b
  data.stderr_str = g_string_new (NULL);
Packit ae235b
  data.stderr_io = g_io_channel_unix_new (stderr_fd);
Packit ae235b
  g_io_channel_set_close_on_unref (data.stderr_io, TRUE);
Packit ae235b
  g_io_channel_set_encoding (data.stderr_io, NULL, NULL);
Packit ae235b
  g_io_channel_set_buffered (data.stderr_io, FALSE);
Packit ae235b
  source = g_io_create_watch (data.stderr_io, G_IO_IN | G_IO_ERR | G_IO_HUP);
Packit ae235b
  g_source_set_callback (source, (GSourceFunc) child_read, &data, NULL);
Packit ae235b
  g_source_attach (source, context);
Packit ae235b
  g_source_unref (source);
Packit ae235b
Packit ae235b
  if (timeout)
Packit ae235b
    {
Packit ae235b
      source = g_timeout_source_new (0);
Packit ae235b
      g_source_set_ready_time (source, g_get_monotonic_time () + timeout);
Packit ae235b
      g_source_set_callback (source, (GSourceFunc) child_timeout, &data, NULL);
Packit ae235b
      g_source_attach (source, context);
Packit ae235b
      g_source_unref (source);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_main_loop_run (data.loop);
Packit ae235b
  g_main_loop_unref (data.loop);
Packit ae235b
  g_main_context_unref (context);
Packit ae235b
Packit ae235b
  test_trap_last_pid = pid;
Packit ae235b
  test_trap_last_status = data.child_status;
Packit ae235b
  test_trap_last_stdout = g_string_free (data.stdout_str, FALSE);
Packit ae235b
  test_trap_last_stderr = g_string_free (data.stderr_str, FALSE);
Packit ae235b
Packit ae235b
  g_clear_pointer (&data.stdout_io, g_io_channel_unref);
Packit ae235b
  g_clear_pointer (&data.stderr_io, g_io_channel_unref);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_trap_fork:
Packit ae235b
 * @usec_timeout:    Timeout for the forked test in micro seconds.
Packit ae235b
 * @test_trap_flags: Flags to modify forking behaviour.
Packit ae235b
 *
Packit ae235b
 * Fork the current test program to execute a test case that might
Packit ae235b
 * not return or that might abort.
Packit ae235b
 *
Packit ae235b
 * If @usec_timeout is non-0, the forked test case is aborted and
Packit ae235b
 * considered failing if its run time exceeds it.
Packit ae235b
 *
Packit ae235b
 * The forking behavior can be configured with the #GTestTrapFlags flags.
Packit ae235b
 *
Packit ae235b
 * In the following example, the test code forks, the forked child
Packit ae235b
 * process produces some sample output and exits successfully.
Packit ae235b
 * The forking parent process then asserts successful child program
Packit ae235b
 * termination and validates child program outputs.
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   static void
Packit ae235b
 *   test_fork_patterns (void)
Packit ae235b
 *   {
Packit ae235b
 *     if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
Packit ae235b
 *       {
Packit ae235b
 *         g_print ("some stdout text: somagic17\n");
Packit ae235b
 *         g_printerr ("some stderr text: semagic43\n");
Packit ae235b
 *         exit (0); // successful test run
Packit ae235b
 *       }
Packit ae235b
 *     g_test_trap_assert_passed ();
Packit ae235b
 *     g_test_trap_assert_stdout ("*somagic17*");
Packit ae235b
 *     g_test_trap_assert_stderr ("*semagic43*");
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE for the forked child and %FALSE for the executing parent process.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 *
Packit ae235b
 * Deprecated: This function is implemented only on Unix platforms,
Packit ae235b
 * and is not always reliable due to problems inherent in
Packit ae235b
 * fork-without-exec. Use g_test_trap_subprocess() instead.
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_test_trap_fork (guint64        usec_timeout,
Packit ae235b
                  GTestTrapFlags test_trap_flags)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  int stdout_pipe[2] = { -1, -1 };
Packit ae235b
  int stderr_pipe[2] = { -1, -1 };
Packit ae235b
  int errsv;
Packit ae235b
Packit ae235b
  test_trap_clear();
Packit ae235b
  if (pipe (stdout_pipe) < 0 || pipe (stderr_pipe) < 0)
Packit ae235b
    {
Packit ae235b
      errsv = errno;
Packit ae235b
      g_error ("failed to create pipes to fork test program: %s", g_strerror (errsv));
Packit ae235b
    }
Packit ae235b
  test_trap_last_pid = fork ();
Packit ae235b
  errsv = errno;
Packit ae235b
  if (test_trap_last_pid < 0)
Packit ae235b
    g_error ("failed to fork test program: %s", g_strerror (errsv));
Packit ae235b
  if (test_trap_last_pid == 0)  /* child */
Packit ae235b
    {
Packit ae235b
      int fd0 = -1;
Packit ae235b
      test_in_forked_child = TRUE;
Packit ae235b
      close (stdout_pipe[0]);
Packit ae235b
      close (stderr_pipe[0]);
Packit ae235b
      if (!(test_trap_flags & G_TEST_TRAP_INHERIT_STDIN))
Packit ae235b
        {
Packit ae235b
          fd0 = g_open ("/dev/null", O_RDONLY, 0);
Packit ae235b
          if (fd0 < 0)
Packit ae235b
            g_error ("failed to open /dev/null for stdin redirection");
Packit ae235b
        }
Packit ae235b
      if (sane_dup2 (stdout_pipe[1], 1) < 0 || sane_dup2 (stderr_pipe[1], 2) < 0 || (fd0 >= 0 && sane_dup2 (fd0, 0) < 0))
Packit ae235b
        {
Packit ae235b
          errsv = errno;
Packit ae235b
          g_error ("failed to dup2() in forked test program: %s", g_strerror (errsv));
Packit ae235b
        }
Packit ae235b
      if (fd0 >= 3)
Packit ae235b
        close (fd0);
Packit ae235b
      if (stdout_pipe[1] >= 3)
Packit ae235b
        close (stdout_pipe[1]);
Packit ae235b
      if (stderr_pipe[1] >= 3)
Packit ae235b
        close (stderr_pipe[1]);
Packit ae235b
      return TRUE;
Packit ae235b
    }
Packit ae235b
  else                          /* parent */
Packit ae235b
    {
Packit ae235b
      test_run_forks++;
Packit ae235b
      close (stdout_pipe[1]);
Packit ae235b
      close (stderr_pipe[1]);
Packit ae235b
Packit ae235b
      wait_for_child (test_trap_last_pid,
Packit ae235b
                      stdout_pipe[0], !(test_trap_flags & G_TEST_TRAP_SILENCE_STDOUT),
Packit ae235b
                      stderr_pipe[0], !(test_trap_flags & G_TEST_TRAP_SILENCE_STDERR),
Packit ae235b
                      usec_timeout);
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
#else
Packit ae235b
  g_message ("Not implemented: g_test_trap_fork");
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_trap_subprocess:
Packit ae235b
 * @test_path: (nullable): Test to run in a subprocess
Packit ae235b
 * @usec_timeout: Timeout for the subprocess test in micro seconds.
Packit ae235b
 * @test_flags:   Flags to modify subprocess behaviour.
Packit ae235b
 *
Packit ae235b
 * Respawns the test program to run only @test_path in a subprocess.
Packit ae235b
 * This can be used for a test case that might not return, or that
Packit ae235b
 * might abort.
Packit ae235b
 *
Packit ae235b
 * If @test_path is %NULL then the same test is re-run in a subprocess.
Packit ae235b
 * You can use g_test_subprocess() to determine whether the test is in
Packit ae235b
 * a subprocess or not.
Packit ae235b
 *
Packit ae235b
 * @test_path can also be the name of the parent test, followed by
Packit ae235b
 * "`/subprocess/`" and then a name for the specific subtest (or just
Packit ae235b
 * ending with "`/subprocess`" if the test only has one child test);
Packit ae235b
 * tests with names of this form will automatically be skipped in the
Packit ae235b
 * parent process.
Packit ae235b
 *
Packit ae235b
 * If @usec_timeout is non-0, the test subprocess is aborted and
Packit ae235b
 * considered failing if its run time exceeds it.
Packit ae235b
 *
Packit ae235b
 * The subprocess behavior can be configured with the
Packit ae235b
 * #GTestSubprocessFlags flags.
Packit ae235b
 *
Packit ae235b
 * You can use methods such as g_test_trap_assert_passed(),
Packit ae235b
 * g_test_trap_assert_failed(), and g_test_trap_assert_stderr() to
Packit ae235b
 * check the results of the subprocess. (But note that
Packit ae235b
 * g_test_trap_assert_stdout() and g_test_trap_assert_stderr()
Packit ae235b
 * cannot be used if @test_flags specifies that the child should
Packit ae235b
 * inherit the parent stdout/stderr.) 
Packit ae235b
 *
Packit ae235b
 * If your `main ()` needs to behave differently in
Packit ae235b
 * the subprocess, you can call g_test_subprocess() (after calling
Packit ae235b
 * g_test_init()) to see whether you are in a subprocess.
Packit ae235b
 *
Packit ae235b
 * The following example tests that calling
Packit ae235b
 * `my_object_new(1000000)` will abort with an error
Packit ae235b
 * message.
Packit ae235b
 *
Packit ae235b
 * |[ 
Packit ae235b
 *   static void
Packit ae235b
 *   test_create_large_object (void)
Packit ae235b
 *   {
Packit ae235b
 *     if (g_test_subprocess ())
Packit ae235b
 *       {
Packit ae235b
 *         my_object_new (1000000);
Packit ae235b
 *         return;
Packit ae235b
 *       }
Packit ae235b
 *
Packit ae235b
 *     // Reruns this same test in a subprocess
Packit ae235b
 *     g_test_trap_subprocess (NULL, 0, 0);
Packit ae235b
 *     g_test_trap_assert_failed ();
Packit ae235b
 *     g_test_trap_assert_stderr ("*ERROR*too large*");
Packit ae235b
 *   }
Packit ae235b
 *
Packit ae235b
 *   int
Packit ae235b
 *   main (int argc, char **argv)
Packit ae235b
 *   {
Packit ae235b
 *     g_test_init (&argc, &argv, NULL);
Packit ae235b
 *
Packit ae235b
 *     g_test_add_func ("/myobject/create_large_object",
Packit ae235b
 *                      test_create_large_object);
Packit ae235b
 *     return g_test_run ();
Packit ae235b
 *   }
Packit ae235b
 * ]|
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_trap_subprocess (const char           *test_path,
Packit ae235b
                        guint64               usec_timeout,
Packit ae235b
                        GTestSubprocessFlags  test_flags)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GPtrArray *argv;
Packit ae235b
  GSpawnFlags flags;
Packit ae235b
  int stdout_fd, stderr_fd;
Packit ae235b
  GPid pid;
Packit ae235b
Packit ae235b
  /* Sanity check that they used GTestSubprocessFlags, not GTestTrapFlags */
Packit ae235b
  g_assert ((test_flags & (G_TEST_TRAP_INHERIT_STDIN | G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)) == 0);
Packit ae235b
Packit ae235b
  if (test_path)
Packit ae235b
    {
Packit ae235b
      if (!g_test_suite_case_exists (g_test_get_root (), test_path))
Packit ae235b
        g_error ("g_test_trap_subprocess: test does not exist: %s", test_path);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      test_path = test_run_name;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (g_test_verbose ())
Packit ae235b
    g_print ("GTest: subprocess: %s\n", test_path);
Packit ae235b
Packit ae235b
  test_trap_clear ();
Packit ae235b
  test_trap_last_subprocess = g_strdup (test_path);
Packit ae235b
Packit ae235b
  argv = g_ptr_array_new ();
Packit ae235b
  g_ptr_array_add (argv, test_argv0);
Packit ae235b
  g_ptr_array_add (argv, "-q");
Packit ae235b
  g_ptr_array_add (argv, "-p");
Packit ae235b
  g_ptr_array_add (argv, (char *)test_path);
Packit ae235b
  g_ptr_array_add (argv, "--GTestSubprocess");
Packit ae235b
  if (test_log_fd != -1)
Packit ae235b
    {
Packit ae235b
      char log_fd_buf[128];
Packit ae235b
Packit ae235b
      g_ptr_array_add (argv, "--GTestLogFD");
Packit ae235b
      g_snprintf (log_fd_buf, sizeof (log_fd_buf), "%d", test_log_fd);
Packit ae235b
      g_ptr_array_add (argv, log_fd_buf);
Packit ae235b
    }
Packit ae235b
  g_ptr_array_add (argv, NULL);
Packit ae235b
Packit ae235b
  flags = G_SPAWN_DO_NOT_REAP_CHILD;
Packit ae235b
  if (test_flags & G_TEST_TRAP_INHERIT_STDIN)
Packit ae235b
    flags |= G_SPAWN_CHILD_INHERITS_STDIN;
Packit ae235b
Packit ae235b
  if (!g_spawn_async_with_pipes (test_initial_cwd,
Packit ae235b
                                 (char **)argv->pdata,
Packit ae235b
                                 NULL, flags,
Packit ae235b
                                 NULL, NULL,
Packit ae235b
                                 &pid, NULL, &stdout_fd, &stderr_fd,
Packit ae235b
                                 &error))
Packit ae235b
    {
Packit ae235b
      g_error ("g_test_trap_subprocess() failed: %s\n",
Packit ae235b
               error->message);
Packit ae235b
    }
Packit ae235b
  g_ptr_array_free (argv, TRUE);
Packit ae235b
Packit ae235b
  wait_for_child (pid,
Packit ae235b
                  stdout_fd, !!(test_flags & G_TEST_SUBPROCESS_INHERIT_STDOUT),
Packit ae235b
                  stderr_fd, !!(test_flags & G_TEST_SUBPROCESS_INHERIT_STDERR),
Packit ae235b
                  usec_timeout);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_subprocess:
Packit ae235b
 *
Packit ae235b
 * Returns %TRUE (after g_test_init() has been called) if the test
Packit ae235b
 * program is running under g_test_trap_subprocess().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the test program is running under
Packit ae235b
 * g_test_trap_subprocess().
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_test_subprocess (void)
Packit ae235b
{
Packit ae235b
  return test_in_subprocess;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_trap_has_passed:
Packit ae235b
 *
Packit ae235b
 * Check the result of the last g_test_trap_subprocess() call.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the last test subprocess terminated successfully.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_test_trap_has_passed (void)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  return (WIFEXITED (test_trap_last_status) &&
Packit ae235b
      WEXITSTATUS (test_trap_last_status) == 0);
Packit ae235b
#else
Packit ae235b
  return test_trap_last_status == 0;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_trap_reached_timeout:
Packit ae235b
 *
Packit ae235b
 * Check the result of the last g_test_trap_subprocess() call.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the last test subprocess got killed due to a timeout.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_test_trap_reached_timeout (void)
Packit ae235b
{
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  return (WIFSIGNALED (test_trap_last_status) &&
Packit ae235b
      WTERMSIG (test_trap_last_status) == SIGALRM);
Packit ae235b
#else
Packit ae235b
  return test_trap_last_status == G_TEST_STATUS_TIMED_OUT;
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
log_child_output (const gchar *process_id)
Packit ae235b
{
Packit ae235b
  gchar *escaped;
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  if (WIFEXITED (test_trap_last_status)) /* normal exit */
Packit ae235b
    {
Packit ae235b
      if (WEXITSTATUS (test_trap_last_status) == 0)
Packit ae235b
        g_test_message ("child process (%s) exit status: 0 (success)",
Packit ae235b
            process_id);
Packit ae235b
      else
Packit ae235b
        g_test_message ("child process (%s) exit status: %d (error)",
Packit ae235b
            process_id, WEXITSTATUS (test_trap_last_status));
Packit ae235b
    }
Packit ae235b
  else if (WIFSIGNALED (test_trap_last_status) &&
Packit ae235b
      WTERMSIG (test_trap_last_status) == SIGALRM)
Packit ae235b
    {
Packit ae235b
      g_test_message ("child process (%s) timed out", process_id);
Packit ae235b
    }
Packit ae235b
  else if (WIFSIGNALED (test_trap_last_status))
Packit ae235b
    {
Packit ae235b
      const gchar *maybe_dumped_core = "";
Packit ae235b
Packit ae235b
#ifdef WCOREDUMP
Packit ae235b
      if (WCOREDUMP (test_trap_last_status))
Packit ae235b
        maybe_dumped_core = ", core dumped";
Packit ae235b
#endif
Packit ae235b
Packit ae235b
      g_test_message ("child process (%s) killed by signal %d (%s)%s",
Packit ae235b
          process_id, WTERMSIG (test_trap_last_status),
Packit ae235b
          g_strsignal (WTERMSIG (test_trap_last_status)),
Packit ae235b
          maybe_dumped_core);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_test_message ("child process (%s) unknown wait status %d",
Packit ae235b
          process_id, test_trap_last_status);
Packit ae235b
    }
Packit ae235b
#else
Packit ae235b
  if (test_trap_last_status == 0)
Packit ae235b
    g_test_message ("child process (%s) exit status: 0 (success)",
Packit ae235b
        process_id);
Packit ae235b
  else
Packit ae235b
    g_test_message ("child process (%s) exit status: %d (error)",
Packit ae235b
        process_id, test_trap_last_status);
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  escaped = g_strescape (test_trap_last_stdout, NULL);
Packit ae235b
  g_test_message ("child process (%s) stdout: \"%s\"", process_id, escaped);
Packit ae235b
  g_free (escaped);
Packit ae235b
Packit ae235b
  escaped = g_strescape (test_trap_last_stderr, NULL);
Packit ae235b
  g_test_message ("child process (%s) stderr: \"%s\"", process_id, escaped);
Packit ae235b
  g_free (escaped);
Packit ae235b
Packit ae235b
  /* so we can use short-circuiting:
Packit ae235b
   * logged_child_output = logged_child_output || log_child_output (...) */
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
void
Packit ae235b
g_test_trap_assertions (const char     *domain,
Packit ae235b
                        const char     *file,
Packit ae235b
                        int             line,
Packit ae235b
                        const char     *func,
Packit ae235b
                        guint64         assertion_flags, /* 0-pass, 1-fail, 2-outpattern, 4-errpattern */
Packit ae235b
                        const char     *pattern)
Packit ae235b
{
Packit ae235b
  gboolean must_pass = assertion_flags == 0;
Packit ae235b
  gboolean must_fail = assertion_flags == 1;
Packit ae235b
  gboolean match_result = 0 == (assertion_flags & 1);
Packit ae235b
  gboolean logged_child_output = FALSE;
Packit ae235b
  const char *stdout_pattern = (assertion_flags & 2) ? pattern : NULL;
Packit ae235b
  const char *stderr_pattern = (assertion_flags & 4) ? pattern : NULL;
Packit ae235b
  const char *match_error = match_result ? "failed to match" : "contains invalid match";
Packit ae235b
  char *process_id;
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  if (test_trap_last_subprocess != NULL)
Packit ae235b
    {
Packit ae235b
      process_id = g_strdup_printf ("%s [%d]", test_trap_last_subprocess,
Packit ae235b
                                    test_trap_last_pid);
Packit ae235b
    }
Packit ae235b
  else if (test_trap_last_pid != 0)
Packit ae235b
    process_id = g_strdup_printf ("%d", test_trap_last_pid);
Packit ae235b
#else
Packit ae235b
  if (test_trap_last_subprocess != NULL)
Packit ae235b
    process_id = g_strdup (test_trap_last_subprocess);
Packit ae235b
#endif
Packit ae235b
  else
Packit ae235b
    g_error ("g_test_trap_ assertion with no trapped test");
Packit ae235b
Packit ae235b
  if (must_pass && !g_test_trap_has_passed())
Packit ae235b
    {
Packit ae235b
      char *msg;
Packit ae235b
Packit ae235b
      logged_child_output = logged_child_output || log_child_output (process_id);
Packit ae235b
Packit ae235b
      msg = g_strdup_printf ("child process (%s) failed unexpectedly", process_id);
Packit ae235b
      g_assertion_message (domain, file, line, func, msg);
Packit ae235b
      g_free (msg);
Packit ae235b
    }
Packit ae235b
  if (must_fail && g_test_trap_has_passed())
Packit ae235b
    {
Packit ae235b
      char *msg;
Packit ae235b
Packit ae235b
      logged_child_output = logged_child_output || log_child_output (process_id);
Packit ae235b
Packit ae235b
      msg = g_strdup_printf ("child process (%s) did not fail as expected", process_id);
Packit ae235b
      g_assertion_message (domain, file, line, func, msg);
Packit ae235b
      g_free (msg);
Packit ae235b
    }
Packit ae235b
  if (stdout_pattern && match_result == !g_pattern_match_simple (stdout_pattern, test_trap_last_stdout))
Packit ae235b
    {
Packit ae235b
      char *msg;
Packit ae235b
Packit ae235b
      logged_child_output = logged_child_output || log_child_output (process_id);
Packit ae235b
Packit ae235b
      msg = g_strdup_printf ("stdout of child process (%s) %s: %s", process_id, match_error, stdout_pattern);
Packit ae235b
      g_assertion_message (domain, file, line, func, msg);
Packit ae235b
      g_free (msg);
Packit ae235b
    }
Packit ae235b
  if (stderr_pattern && match_result == !g_pattern_match_simple (stderr_pattern, test_trap_last_stderr))
Packit ae235b
    {
Packit ae235b
      char *msg;
Packit ae235b
Packit ae235b
      logged_child_output = logged_child_output || log_child_output (process_id);
Packit ae235b
Packit ae235b
      msg = g_strdup_printf ("stderr of child process (%s) %s: %s", process_id, match_error, stderr_pattern);
Packit ae235b
      g_assertion_message (domain, file, line, func, msg);
Packit ae235b
      g_free (msg);
Packit ae235b
    }
Packit ae235b
  g_free (process_id);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
gstring_overwrite_int (GString *gstring,
Packit ae235b
                       guint    pos,
Packit ae235b
                       guint32  vuint)
Packit ae235b
{
Packit ae235b
  vuint = g_htonl (vuint);
Packit ae235b
  g_string_overwrite_len (gstring, pos, (const gchar*) &vuint, 4);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
gstring_append_int (GString *gstring,
Packit ae235b
                    guint32  vuint)
Packit ae235b
{
Packit ae235b
  vuint = g_htonl (vuint);
Packit ae235b
  g_string_append_len (gstring, (const gchar*) &vuint, 4);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
gstring_append_double (GString *gstring,
Packit ae235b
                       double   vdouble)
Packit ae235b
{
Packit ae235b
  union { double vdouble; guint64 vuint64; } u;
Packit ae235b
  u.vdouble = vdouble;
Packit ae235b
  u.vuint64 = GUINT64_TO_BE (u.vuint64);
Packit ae235b
  g_string_append_len (gstring, (const gchar*) &u.vuint64, 8);
Packit ae235b
}
Packit ae235b
Packit ae235b
static guint8*
Packit ae235b
g_test_log_dump (GTestLogMsg *msg,
Packit ae235b
                 guint       *len)
Packit ae235b
{
Packit ae235b
  GString *gstring = g_string_sized_new (1024);
Packit ae235b
  guint ui;
Packit ae235b
  gstring_append_int (gstring, 0);              /* message length */
Packit ae235b
  gstring_append_int (gstring, msg->log_type);
Packit ae235b
  gstring_append_int (gstring, msg->n_strings);
Packit ae235b
  gstring_append_int (gstring, msg->n_nums);
Packit ae235b
  gstring_append_int (gstring, 0);      /* reserved */
Packit ae235b
  for (ui = 0; ui < msg->n_strings; ui++)
Packit ae235b
    {
Packit ae235b
      guint l = strlen (msg->strings[ui]);
Packit ae235b
      gstring_append_int (gstring, l);
Packit ae235b
      g_string_append_len (gstring, msg->strings[ui], l);
Packit ae235b
    }
Packit ae235b
  for (ui = 0; ui < msg->n_nums; ui++)
Packit ae235b
    gstring_append_double (gstring, msg->nums[ui]);
Packit ae235b
  *len = gstring->len;
Packit ae235b
  gstring_overwrite_int (gstring, 0, *len);     /* message length */
Packit ae235b
  return (guint8*) g_string_free (gstring, FALSE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline long double
Packit ae235b
net_double (const gchar **ipointer)
Packit ae235b
{
Packit ae235b
  union { guint64 vuint64; double vdouble; } u;
Packit ae235b
  guint64 aligned_int64;
Packit ae235b
  memcpy (&aligned_int64, *ipointer, 8);
Packit ae235b
  *ipointer += 8;
Packit ae235b
  u.vuint64 = GUINT64_FROM_BE (aligned_int64);
Packit ae235b
  return u.vdouble;
Packit ae235b
}
Packit ae235b
Packit ae235b
static inline guint32
Packit ae235b
net_int (const gchar **ipointer)
Packit ae235b
{
Packit ae235b
  guint32 aligned_int;
Packit ae235b
  memcpy (&aligned_int, *ipointer, 4);
Packit ae235b
  *ipointer += 4;
Packit ae235b
  return g_ntohl (aligned_int);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_test_log_extract (GTestLogBuffer *tbuffer)
Packit ae235b
{
Packit ae235b
  const gchar *p = tbuffer->data->str;
Packit ae235b
  GTestLogMsg msg;
Packit ae235b
  guint mlength;
Packit ae235b
  if (tbuffer->data->len < 4 * 5)
Packit ae235b
    return FALSE;
Packit ae235b
  mlength = net_int (&p);
Packit ae235b
  if (tbuffer->data->len < mlength)
Packit ae235b
    return FALSE;
Packit ae235b
  msg.log_type = net_int (&p);
Packit ae235b
  msg.n_strings = net_int (&p);
Packit ae235b
  msg.n_nums = net_int (&p);
Packit ae235b
  if (net_int (&p) == 0)
Packit ae235b
    {
Packit ae235b
      guint ui;
Packit ae235b
      msg.strings = g_new0 (gchar*, msg.n_strings + 1);
Packit ae235b
      msg.nums = g_new0 (long double, msg.n_nums);
Packit ae235b
      for (ui = 0; ui < msg.n_strings; ui++)
Packit ae235b
        {
Packit ae235b
          guint sl = net_int (&p);
Packit ae235b
          msg.strings[ui] = g_strndup (p, sl);
Packit ae235b
          p += sl;
Packit ae235b
        }
Packit ae235b
      for (ui = 0; ui < msg.n_nums; ui++)
Packit ae235b
        msg.nums[ui] = net_double (&p);
Packit ae235b
      if (p <= tbuffer->data->str + mlength)
Packit ae235b
        {
Packit ae235b
          g_string_erase (tbuffer->data, 0, mlength);
Packit ae235b
          tbuffer->msgs = g_slist_prepend (tbuffer->msgs, g_memdup (&msg, sizeof (msg)));
Packit ae235b
          return TRUE;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_free (msg.nums);
Packit ae235b
      g_strfreev (msg.strings);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_error ("corrupt log stream from test program");
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_log_buffer_new:
Packit ae235b
 *
Packit ae235b
 * Internal function for gtester to decode test log messages, no ABI guarantees provided.
Packit ae235b
 */
Packit ae235b
GTestLogBuffer*
Packit ae235b
g_test_log_buffer_new (void)
Packit ae235b
{
Packit ae235b
  GTestLogBuffer *tb = g_new0 (GTestLogBuffer, 1);
Packit ae235b
  tb->data = g_string_sized_new (1024);
Packit ae235b
  return tb;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_log_buffer_free:
Packit ae235b
 *
Packit ae235b
 * Internal function for gtester to free test log messages, no ABI guarantees provided.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_log_buffer_free (GTestLogBuffer *tbuffer)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (tbuffer != NULL);
Packit ae235b
  while (tbuffer->msgs)
Packit ae235b
    g_test_log_msg_free (g_test_log_buffer_pop (tbuffer));
Packit ae235b
  g_string_free (tbuffer->data, TRUE);
Packit ae235b
  g_free (tbuffer);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_log_buffer_push:
Packit ae235b
 *
Packit ae235b
 * Internal function for gtester to decode test log messages, no ABI guarantees provided.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_log_buffer_push (GTestLogBuffer *tbuffer,
Packit ae235b
                        guint           n_bytes,
Packit ae235b
                        const guint8   *bytes)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (tbuffer != NULL);
Packit ae235b
  if (n_bytes)
Packit ae235b
    {
Packit ae235b
      gboolean more_messages;
Packit ae235b
      g_return_if_fail (bytes != NULL);
Packit ae235b
      g_string_append_len (tbuffer->data, (const gchar*) bytes, n_bytes);
Packit ae235b
      do
Packit ae235b
        more_messages = g_test_log_extract (tbuffer);
Packit ae235b
      while (more_messages);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_log_buffer_pop:
Packit ae235b
 *
Packit ae235b
 * Internal function for gtester to retrieve test log messages, no ABI guarantees provided.
Packit ae235b
 */
Packit ae235b
GTestLogMsg*
Packit ae235b
g_test_log_buffer_pop (GTestLogBuffer *tbuffer)
Packit ae235b
{
Packit ae235b
  GTestLogMsg *msg = NULL;
Packit ae235b
  g_return_val_if_fail (tbuffer != NULL, NULL);
Packit ae235b
  if (tbuffer->msgs)
Packit ae235b
    {
Packit ae235b
      GSList *slist = g_slist_last (tbuffer->msgs);
Packit ae235b
      msg = slist->data;
Packit ae235b
      tbuffer->msgs = g_slist_delete_link (tbuffer->msgs, slist);
Packit ae235b
    }
Packit ae235b
  return msg;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_log_msg_free:
Packit ae235b
 *
Packit ae235b
 * Internal function for gtester to free test log messages, no ABI guarantees provided.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_test_log_msg_free (GTestLogMsg *tmsg)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (tmsg != NULL);
Packit ae235b
  g_strfreev (tmsg->strings);
Packit ae235b
  g_free (tmsg->nums);
Packit ae235b
  g_free (tmsg);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar *
Packit ae235b
g_test_build_filename_va (GTestFileType  file_type,
Packit ae235b
                          const gchar   *first_path,
Packit ae235b
                          va_list        ap)
Packit ae235b
{
Packit ae235b
  const gchar *pathv[16];
Packit ae235b
  gint num_path_segments;
Packit ae235b
Packit ae235b
  if (file_type == G_TEST_DIST)
Packit ae235b
    pathv[0] = test_disted_files_dir;
Packit ae235b
  else if (file_type == G_TEST_BUILT)
Packit ae235b
    pathv[0] = test_built_files_dir;
Packit ae235b
  else
Packit ae235b
    g_assert_not_reached ();
Packit ae235b
Packit ae235b
  pathv[1] = first_path;
Packit ae235b
Packit ae235b
  for (num_path_segments = 2; num_path_segments < G_N_ELEMENTS (pathv); num_path_segments++)
Packit ae235b
    {
Packit ae235b
      pathv[num_path_segments] = va_arg (ap, const char *);
Packit ae235b
      if (pathv[num_path_segments] == NULL)
Packit ae235b
        break;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_assert_cmpint (num_path_segments, <, G_N_ELEMENTS (pathv));
Packit ae235b
Packit ae235b
  return g_build_filenamev ((gchar **) pathv);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_build_filename:
Packit ae235b
 * @file_type: the type of file (built vs. distributed)
Packit ae235b
 * @first_path: the first segment of the pathname
Packit ae235b
 * @...: %NULL-terminated additional path segments
Packit ae235b
 *
Packit ae235b
 * Creates the pathname to a data file that is required for a test.
Packit ae235b
 *
Packit ae235b
 * This function is conceptually similar to g_build_filename() except
Packit ae235b
 * that the first argument has been replaced with a #GTestFileType
Packit ae235b
 * argument.
Packit ae235b
 *
Packit ae235b
 * The data file should either have been distributed with the module
Packit ae235b
 * containing the test (%G_TEST_DIST) or built as part of the build
Packit ae235b
 * system of that module (%G_TEST_BUILT).
Packit ae235b
 *
Packit ae235b
 * In order for this function to work in srcdir != builddir situations,
Packit ae235b
 * the G_TEST_SRCDIR and G_TEST_BUILDDIR environment variables need to
Packit ae235b
 * have been defined.  As of 2.38, this is done by the glib.mk
Packit ae235b
 * included in GLib.  Please ensure that your copy is up to date before
Packit ae235b
 * using this function.
Packit ae235b
 *
Packit ae235b
 * In case neither variable is set, this function will fall back to
Packit ae235b
 * using the dirname portion of argv[0], possibly removing ".libs".
Packit ae235b
 * This allows for casual running of tests directly from the commandline
Packit ae235b
 * in the srcdir == builddir case and should also support running of
Packit ae235b
 * installed tests, assuming the data files have been installed in the
Packit ae235b
 * same relative path as the test binary.
Packit ae235b
 *
Packit ae235b
 * Returns: the path of the file, to be freed using g_free()
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 **/
Packit ae235b
/**
Packit ae235b
 * GTestFileType:
Packit ae235b
 * @G_TEST_DIST: a file that was included in the distribution tarball
Packit ae235b
 * @G_TEST_BUILT: a file that was built on the compiling machine
Packit ae235b
 *
Packit ae235b
 * The type of file to return the filename for, when used with
Packit ae235b
 * g_test_build_filename().
Packit ae235b
 *
Packit ae235b
 * These two options correspond rather directly to the 'dist' and
Packit ae235b
 * 'built' terminology that automake uses and are explicitly used to
Packit ae235b
 * distinguish between the 'srcdir' and 'builddir' being separate.  All
Packit ae235b
 * files in your project should either be dist (in the
Packit ae235b
 * `EXTRA_DIST` or `dist_schema_DATA`
Packit ae235b
 * sense, in which case they will always be in the srcdir) or built (in
Packit ae235b
 * the `BUILT_SOURCES` sense, in which case they will
Packit ae235b
 * always be in the builddir).
Packit ae235b
 *
Packit ae235b
 * Note: as a general rule of automake, files that are generated only as
Packit ae235b
 * part of the build-from-git process (but then are distributed with the
Packit ae235b
 * tarball) always go in srcdir (even if doing a srcdir != builddir
Packit ae235b
 * build from git) and are considered as distributed files.
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 **/
Packit ae235b
gchar *
Packit ae235b
g_test_build_filename (GTestFileType  file_type,
Packit ae235b
                       const gchar   *first_path,
Packit ae235b
                       ...)
Packit ae235b
{
Packit ae235b
  gchar *result;
Packit ae235b
  va_list ap;
Packit ae235b
Packit ae235b
  g_assert (g_test_initialized ());
Packit ae235b
Packit ae235b
  va_start (ap, first_path);
Packit ae235b
  result = g_test_build_filename_va (file_type, first_path, ap);
Packit ae235b
  va_end (ap);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_get_dir:
Packit ae235b
 * @file_type: the type of file (built vs. distributed)
Packit ae235b
 *
Packit ae235b
 * Gets the pathname of the directory containing test files of the type
Packit ae235b
 * specified by @file_type.
Packit ae235b
 *
Packit ae235b
 * This is approximately the same as calling g_test_build_filename("."),
Packit ae235b
 * but you don't need to free the return value.
Packit ae235b
 *
Packit ae235b
 * Returns: (type filename): the path of the directory, owned by GLib
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 **/
Packit ae235b
const gchar *
Packit ae235b
g_test_get_dir (GTestFileType file_type)
Packit ae235b
{
Packit ae235b
  g_assert (g_test_initialized ());
Packit ae235b
Packit ae235b
  if (file_type == G_TEST_DIST)
Packit ae235b
    return test_disted_files_dir;
Packit ae235b
  else if (file_type == G_TEST_BUILT)
Packit ae235b
    return test_built_files_dir;
Packit ae235b
Packit ae235b
  g_assert_not_reached ();
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_test_get_filename:
Packit ae235b
 * @file_type: the type of file (built vs. distributed)
Packit ae235b
 * @first_path: the first segment of the pathname
Packit ae235b
 * @...: %NULL-terminated additional path segments
Packit ae235b
 *
Packit ae235b
 * Gets the pathname to a data file that is required for a test.
Packit ae235b
 *
Packit ae235b
 * This is the same as g_test_build_filename() with two differences.
Packit ae235b
 * The first difference is that must only use this function from within
Packit ae235b
 * a testcase function.  The second difference is that you need not free
Packit ae235b
 * the return value -- it will be automatically freed when the testcase
Packit ae235b
 * finishes running.
Packit ae235b
 *
Packit ae235b
 * It is safe to use this function from a thread inside of a testcase
Packit ae235b
 * but you must ensure that all such uses occur before the main testcase
Packit ae235b
 * function returns (ie: it is best to ensure that all threads have been
Packit ae235b
 * joined).
Packit ae235b
 *
Packit ae235b
 * Returns: the path, automatically freed at the end of the testcase
Packit ae235b
 *
Packit ae235b
 * Since: 2.38
Packit ae235b
 **/
Packit ae235b
const gchar *
Packit ae235b
g_test_get_filename (GTestFileType  file_type,
Packit ae235b
                     const gchar   *first_path,
Packit ae235b
                     ...)
Packit ae235b
{
Packit ae235b
  gchar *result;
Packit ae235b
  GSList *node;
Packit ae235b
  va_list ap;
Packit ae235b
Packit ae235b
  g_assert (g_test_initialized ());
Packit ae235b
  if (test_filename_free_list == NULL)
Packit ae235b
    g_error ("g_test_get_filename() can only be used within testcase functions");
Packit ae235b
Packit ae235b
  va_start (ap, first_path);
Packit ae235b
  result = g_test_build_filename_va (file_type, first_path, ap);
Packit ae235b
  va_end (ap);
Packit ae235b
Packit ae235b
  node = g_slist_prepend (NULL, result);
Packit ae235b
  do
Packit ae235b
    node->next = *test_filename_free_list;
Packit ae235b
  while (!g_atomic_pointer_compare_and_exchange (test_filename_free_list, node->next, node));
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* --- macros docs START --- */
Packit ae235b
/**
Packit ae235b
 * g_test_add:
Packit ae235b
 * @testpath:  The test path for a new test case.
Packit ae235b
 * @Fixture:   The type of a fixture data structure.
Packit ae235b
 * @tdata:     Data argument for the test functions.
Packit ae235b
 * @fsetup:    The function to set up the fixture data.
Packit ae235b
 * @ftest:     The actual test function.
Packit ae235b
 * @fteardown: The function to tear down the fixture data.
Packit ae235b
 *
Packit ae235b
 * Hook up a new test case at @testpath, similar to g_test_add_func().
Packit ae235b
 * A fixture data structure with setup and teardown functions may be provided,
Packit ae235b
 * similar to g_test_create_case().
Packit ae235b
 *
Packit ae235b
 * g_test_add() is implemented as a macro, so that the fsetup(), ftest() and
Packit ae235b
 * fteardown() callbacks can expect a @Fixture pointer as their first argument
Packit ae235b
 * in a type safe manner. They otherwise have type #GTestFixtureFunc.
Packit ae235b
 *
Packit ae235b
 * Since: 2.16
Packit ae235b
 **/
Packit ae235b
/* --- macros docs END --- */