26
26
#include "egg-testing.h"
28
#include <glib-object.h>
30
#include <valgrind/valgrind.h>
33
#include <glib/gstdio.h>
28
39
static const char HEXC[] = "0123456789ABCDEF";
31
hex_dump (const guchar *data, gsize n_data)
42
egg_test_escape_data (const guchar *data,
50
g_assert (data != NULL);
39
52
result = g_string_sized_new (n_data * 2 + 1);
40
53
for (i = 0; i < n_data; ++i) {
41
g_string_append (result, "\\x");
43
j = data[i] >> 4 & 0xf;
44
g_string_append_c (result, HEXC[j]);
46
g_string_append_c (result, HEXC[j]);
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]);
49
66
return g_string_free (result, FALSE);
70
is_readable_ptr (gpointer was_object)
72
static gint test_memory_fd = -1;
74
/* First make sure this memory is still accessible */
75
if (test_memory_fd < 0)
76
test_memory_fd = g_open ("/dev/null", O_WRONLY, 0);
77
if (write (test_memory_fd, was_object, 1) > 0)
79
return (errno != EFAULT);
83
egg_assertion_not_object (const char *domain,
92
if (RUNNING_ON_VALGRIND)
95
if (!is_readable_ptr (was_object))
98
if (G_IS_OBJECT (was_object)) {
99
s = g_strdup_printf ("assertion failed: %s is still referenced", expr);
100
g_assertion_message (domain, file, line, func, s);
53
106
egg_assertion_message_cmpmem (const char *domain,
64
117
char *a1, *a2, *s;
65
a1 = arg1 ? hex_dump (arg1, n_arg1) : g_strdup ("NULL");
66
a2 = arg2 ? hex_dump (arg2, n_arg2) : g_strdup ("NULL");
118
a1 = arg1 ? egg_test_escape_data (arg1, n_arg1) : g_strdup ("NULL");
119
a2 = arg2 ? egg_test_escape_data (arg2, n_arg2) : g_strdup ("NULL");
67
120
s = g_strdup_printf ("assertion failed (%s): (%s %s %s)", expr, a1, cmp, a2);
70
123
g_assertion_message (domain, file, line, func, s);
127
static void (*wait_stop_impl) (void);
128
static gboolean (*wait_until_impl) (int timeout);
131
egg_test_wait_stop (void)
133
g_assert (wait_stop_impl != NULL);
138
egg_test_wait_until (int timeout)
140
g_assert (wait_until_impl != NULL);
141
return (wait_until_impl) (timeout);
145
egg_test_wait_idle (void)
147
GMainContext *context;
149
g_assert (wait_until_impl != NULL);
151
context = g_main_context_get_thread_default ();
152
while (g_main_context_iteration (context, FALSE));
155
static GMainLoop *wait_loop = NULL;
158
loop_wait_stop (void)
160
g_assert (wait_loop != NULL);
161
g_main_loop_quit (wait_loop);
165
on_loop_wait_timeout (gpointer data)
167
gboolean *timed_out = data;
170
g_assert (wait_loop != NULL);
171
g_main_loop_quit (wait_loop);
173
return TRUE; /* we remove this source later */
177
loop_wait_until (int timeout)
179
gboolean timed_out = FALSE;
182
g_assert (wait_loop == NULL);
183
wait_loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
185
source = g_timeout_add (timeout, on_loop_wait_timeout, &timed_out);
187
g_main_loop_run (wait_loop);
189
g_source_remove (source);
190
g_main_loop_unref (wait_loop);
196
egg_tests_run_with_loop (void)
200
wait_stop_impl = loop_wait_stop;
201
wait_until_impl = loop_wait_until;
205
wait_stop_impl = NULL;
206
wait_until_impl = NULL;
208
while (g_main_context_iteration (NULL, FALSE));