4 * Copyright (C) 2011 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * Author: Stef Walter <stefw@collabora.co.uk>
26 #include "egg-testing.h"
28 #include <glib-object.h>
30 #include <valgrind/valgrind.h>
33 #include <glib/gstdio.h>
39 static const char HEXC[] = "0123456789ABCDEF";
42 egg_test_escape_data (const guchar *data,
50 g_assert (data != NULL);
52 result = g_string_sized_new (n_data * 2 + 1);
53 for (i = 0; i < n_data; ++i) {
55 if (g_ascii_isprint (c) && !strchr ("\n\r\v", c)) {
56 g_string_append_c (result, c);
58 g_string_append (result, "\\x");
60 g_string_append_c (result, HEXC[j]);
62 g_string_append_c (result, HEXC[j]);
66 return g_string_free (result, FALSE);
70 egg_assertion_message_cmpmem (const char *domain,
82 a1 = arg1 ? egg_test_escape_data (arg1, n_arg1) : g_strdup ("NULL");
83 a2 = arg2 ? egg_test_escape_data (arg2, n_arg2) : g_strdup ("NULL");
84 s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2);
87 g_assertion_message (domain, file, line, func, s);
91 static void (*wait_stop_impl) (void);
92 static gboolean (*wait_until_impl) (int timeout);
95 egg_test_wait_stop (void)
97 g_assert (wait_stop_impl != NULL);
102 egg_test_wait_until (int timeout)
104 g_assert (wait_until_impl != NULL);
105 return (wait_until_impl) (timeout);
109 egg_test_wait_idle (void)
111 GMainContext *context;
113 g_assert (wait_until_impl != NULL);
115 context = g_main_context_get_thread_default ();
116 while (g_main_context_iteration (context, FALSE));
119 static GMainLoop *wait_loop = NULL;
122 loop_wait_stop (void)
124 g_assert (wait_loop != NULL);
125 g_main_loop_quit (wait_loop);
129 on_loop_wait_timeout (gpointer data)
131 gboolean *timed_out = data;
134 g_assert (wait_loop != NULL);
135 g_main_loop_quit (wait_loop);
137 return TRUE; /* we remove this source later */
141 loop_wait_until (int timeout)
143 gboolean timed_out = FALSE;
146 g_assert (wait_loop == NULL);
147 wait_loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
149 source = g_timeout_add (timeout, on_loop_wait_timeout, &timed_out);
151 g_main_loop_run (wait_loop);
153 g_source_remove (source);
154 g_main_loop_unref (wait_loop);
160 egg_tests_run_with_loop (void)
164 wait_stop_impl = loop_wait_stop;
165 wait_until_impl = loop_wait_until;
169 wait_stop_impl = NULL;
170 wait_until_impl = NULL;
172 while (g_main_context_iteration (NULL, FALSE));