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 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 License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 * Stef Walter <stefw@collabora.co.uk>
26 #include "egg-testing.h"
28 #include <glib-object.h>
30 #include <valgrind/valgrind.h>
35 static const char HEXC[] = "0123456789ABCDEF";
38 hex_dump (const guchar *data, gsize n_data)
46 result = g_string_sized_new (n_data * 2 + 1);
47 for (i = 0; i < n_data; ++i) {
48 g_string_append (result, "\\x");
50 j = data[i] >> 4 & 0xf;
51 g_string_append_c (result, HEXC[j]);
53 g_string_append_c (result, HEXC[j]);
56 return g_string_free (result, FALSE);
60 egg_assertion_not_object (const char *domain,
69 if (RUNNING_ON_VALGRIND)
71 if (G_IS_OBJECT (was_object)) {
72 s = g_strdup_printf ("assertion failed: %s is still referenced", expr);
73 g_assertion_message (domain, file, line, func, s);
79 egg_assertion_message_cmpmem (const char *domain,
91 a1 = arg1 ? hex_dump (arg1, n_arg1) : g_strdup ("NULL");
92 a2 = arg2 ? hex_dump (arg2, n_arg2) : g_strdup ("NULL");
93 s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2);
96 g_assertion_message (domain, file, line, func, s);
100 static void (*wait_stop_impl) (void);
101 static gboolean (*wait_until_impl) (int timeout);
104 egg_test_wait_stop (void)
106 g_assert (wait_stop_impl != NULL);
111 egg_test_wait_until (int timeout)
113 g_assert (wait_until_impl != NULL);
114 return (wait_until_impl) (timeout);
117 static GMainLoop *wait_loop = NULL;
120 loop_wait_stop (void)
122 g_assert (wait_loop != NULL);
123 g_main_loop_quit (wait_loop);
127 on_loop_wait_timeout (gpointer data)
129 gboolean *timed_out = data;
132 g_assert (wait_loop != NULL);
133 g_main_loop_quit (wait_loop);
135 return TRUE; /* we remove this source later */
139 loop_wait_until (int timeout)
141 gboolean ret = FALSE;
142 gboolean timed_out = FALSE;
145 g_assert (wait_loop == NULL);
146 wait_loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
148 source = g_timeout_add (timeout, on_loop_wait_timeout, &timed_out);
150 g_main_loop_run (wait_loop);
153 g_source_remove (source);
159 g_main_loop_unref (wait_loop);
165 egg_tests_run_with_loop (void)
169 wait_stop_impl = loop_wait_stop;
170 wait_until_impl = loop_wait_until;
174 wait_stop_impl = NULL;
175 wait_until_impl = NULL;
177 while (g_main_context_iteration (NULL, FALSE));