tests: Convert g_assert() to g_assert_*() in glib/tests/mainloop.c

g_assert_*() give more informative failure messages, and aren’t compiled
out when building with G_DISABLE_ASSERT.

Signed-off-by: Philip Withnall <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2019-03-05 10:57:57 +00:00
parent 3a96e7d9cb
commit a1aebf7437

View File

@ -60,35 +60,35 @@ test_maincontext_basic (void)
ctx = g_main_context_new (); ctx = g_main_context_new ();
g_assert (!g_main_context_pending (ctx)); g_assert_false (g_main_context_pending (ctx));
g_assert (!g_main_context_iteration (ctx, FALSE)); g_assert_false (g_main_context_iteration (ctx, FALSE));
source = g_source_new (&funcs, sizeof (GSource)); source = g_source_new (&funcs, sizeof (GSource));
g_assert_cmpint (g_source_get_priority (source), ==, G_PRIORITY_DEFAULT); g_assert_cmpint (g_source_get_priority (source), ==, G_PRIORITY_DEFAULT);
g_assert (!g_source_is_destroyed (source)); g_assert_false (g_source_is_destroyed (source));
g_assert (!g_source_get_can_recurse (source)); g_assert_false (g_source_get_can_recurse (source));
g_assert (g_source_get_name (source) == NULL); g_assert_null (g_source_get_name (source));
g_source_set_can_recurse (source, TRUE); g_source_set_can_recurse (source, TRUE);
g_source_set_name (source, "d"); g_source_set_name (source, "d");
g_assert (g_source_get_can_recurse (source)); g_assert_true (g_source_get_can_recurse (source));
g_assert_cmpstr (g_source_get_name (source), ==, "d"); g_assert_cmpstr (g_source_get_name (source), ==, "d");
g_assert (g_main_context_find_source_by_user_data (ctx, NULL) == NULL); g_assert_null (g_main_context_find_source_by_user_data (ctx, NULL));
g_assert (g_main_context_find_source_by_funcs_user_data (ctx, &funcs, NULL) == NULL); g_assert_null (g_main_context_find_source_by_funcs_user_data (ctx, &funcs, NULL));
id = g_source_attach (source, ctx); id = g_source_attach (source, ctx);
g_assert_cmpint (g_source_get_id (source), ==, id); g_assert_cmpint (g_source_get_id (source), ==, id);
g_assert (g_main_context_find_source_by_id (ctx, id) == source); g_assert_true (g_main_context_find_source_by_id (ctx, id) == source);
g_source_set_priority (source, G_PRIORITY_HIGH); g_source_set_priority (source, G_PRIORITY_HIGH);
g_assert_cmpint (g_source_get_priority (source), ==, G_PRIORITY_HIGH); g_assert_cmpint (g_source_get_priority (source), ==, G_PRIORITY_HIGH);
g_source_destroy (source); g_source_destroy (source);
g_assert (g_source_get_context (source) == ctx); g_assert_true (g_source_get_context (source) == ctx);
g_assert (g_main_context_find_source_by_id (ctx, id) == NULL); g_assert_null (g_main_context_find_source_by_id (ctx, id));
g_main_context_unref (ctx); g_main_context_unref (ctx);
@ -96,7 +96,7 @@ test_maincontext_basic (void)
{ {
g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"*assertion*source->context != NULL*failed*"); "*assertion*source->context != NULL*failed*");
g_assert (g_source_get_context (source) == NULL); g_assert_null (g_source_get_context (source));
g_test_assert_expected_messages (); g_test_assert_expected_messages ();
} }
@ -110,19 +110,19 @@ test_maincontext_basic (void)
g_source_unref (source); g_source_unref (source);
g_source_set_name_by_id (id, "e"); g_source_set_name_by_id (id, "e");
g_assert_cmpstr (g_source_get_name (source), ==, "e"); g_assert_cmpstr (g_source_get_name (source), ==, "e");
g_assert (g_source_get_context (source) == ctx); g_assert_true (g_source_get_context (source) == ctx);
g_assert (g_source_remove_by_funcs_user_data (&funcs, data)); g_assert_true (g_source_remove_by_funcs_user_data (&funcs, data));
source = g_source_new (&funcs, sizeof (GSource)); source = g_source_new (&funcs, sizeof (GSource));
g_source_set_funcs (source, &funcs); g_source_set_funcs (source, &funcs);
g_source_set_callback (source, cb, data, NULL); g_source_set_callback (source, cb, data, NULL);
id = g_source_attach (source, ctx); id = g_source_attach (source, ctx);
g_source_unref (source); g_source_unref (source);
g_assert (g_source_remove_by_user_data (data)); g_assert_true (g_source_remove_by_user_data (data));
g_assert (!g_source_remove_by_user_data ((gpointer)0x1234)); g_assert_false (g_source_remove_by_user_data ((gpointer)0x1234));
g_idle_add (cb, data); g_idle_add (cb, data);
g_assert (g_idle_remove_by_data (data)); g_assert_true (g_idle_remove_by_data (data));
} }
static void static void
@ -133,12 +133,12 @@ test_mainloop_basic (void)
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
g_assert (!g_main_loop_is_running (loop)); g_assert_false (g_main_loop_is_running (loop));
g_main_loop_ref (loop); g_main_loop_ref (loop);
ctx = g_main_loop_get_context (loop); ctx = g_main_loop_get_context (loop);
g_assert (ctx == g_main_context_default ()); g_assert_true (ctx == g_main_context_default ());
g_main_loop_unref (loop); g_main_loop_unref (loop);
@ -233,24 +233,24 @@ test_priorities (void)
g_source_attach (sourceb, ctx); g_source_attach (sourceb, ctx);
g_source_unref (sourceb); g_source_unref (sourceb);
g_assert (g_main_context_pending (ctx)); g_assert_true (g_main_context_pending (ctx));
g_assert (g_main_context_iteration (ctx, FALSE)); g_assert_true (g_main_context_iteration (ctx, FALSE));
g_assert_cmpint (a, ==, 0); g_assert_cmpint (a, ==, 0);
g_assert_cmpint (b, ==, 1); g_assert_cmpint (b, ==, 1);
g_assert (g_main_context_iteration (ctx, FALSE)); g_assert_true (g_main_context_iteration (ctx, FALSE));
g_assert_cmpint (a, ==, 0); g_assert_cmpint (a, ==, 0);
g_assert_cmpint (b, ==, 2); g_assert_cmpint (b, ==, 2);
g_source_destroy (sourceb); g_source_destroy (sourceb);
g_assert (g_main_context_iteration (ctx, FALSE)); g_assert_true (g_main_context_iteration (ctx, FALSE));
g_assert_cmpint (a, ==, 1); g_assert_cmpint (a, ==, 1);
g_assert_cmpint (b, ==, 2); g_assert_cmpint (b, ==, 2);
g_assert (g_main_context_pending (ctx)); g_assert_true (g_main_context_pending (ctx));
g_source_destroy (sourcea); g_source_destroy (sourcea);
g_assert (!g_main_context_pending (ctx)); g_assert_false (g_main_context_pending (ctx));
g_main_context_unref (ctx); g_main_context_unref (ctx);
} }
@ -271,7 +271,7 @@ static gboolean
func (gpointer data) func (gpointer data)
{ {
if (data != NULL) if (data != NULL)
g_assert (data == g_thread_self ()); g_assert_true (data == g_thread_self ());
count++; count++;
@ -706,12 +706,12 @@ timeout1_callback (gpointer user_data)
gint64 mtime1, mtime2, time2; gint64 mtime1, mtime2, time2;
source = g_main_current_source (); source = g_main_current_source ();
g_assert (source == data->timeout1); g_assert_true (source == data->timeout1);
if (data->time1 == -1) if (data->time1 == -1)
{ {
/* First iteration */ /* First iteration */
g_assert (!g_source_is_destroyed (data->timeout2)); g_assert_false (g_source_is_destroyed (data->timeout2));
mtime1 = g_get_monotonic_time (); mtime1 = g_get_monotonic_time ();
data->time1 = g_source_get_time (source); data->time1 = g_source_get_time (source);
@ -733,7 +733,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
GTimeVal tv; GTimeVal tv;
/* Second iteration */ /* Second iteration */
g_assert (g_source_is_destroyed (data->timeout2)); g_assert_true (g_source_is_destroyed (data->timeout2));
/* g_source_get_time() MAY change between iterations; in this /* g_source_get_time() MAY change between iterations; in this
* case we know for sure that it did because of the g_usleep() * case we know for sure that it did because of the g_usleep()
@ -746,7 +746,7 @@ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
g_source_get_current_time (source, &tv); g_source_get_current_time (source, &tv);
G_GNUC_END_IGNORE_DEPRECATIONS G_GNUC_END_IGNORE_DEPRECATIONS
g_assert (tv.tv_sec > data->tv.tv_sec || g_assert_true (tv.tv_sec > data->tv.tv_sec ||
(tv.tv_sec == data->tv.tv_sec && (tv.tv_sec == data->tv.tv_sec &&
tv.tv_usec > data->tv.tv_usec)); tv.tv_usec > data->tv.tv_usec));
@ -764,9 +764,9 @@ timeout2_callback (gpointer user_data)
gint64 time2, time3; gint64 time2, time3;
source = g_main_current_source (); source = g_main_current_source ();
g_assert (source == data->timeout2); g_assert_true (source == data->timeout2);
g_assert (!g_source_is_destroyed (data->timeout1)); g_assert_false (g_source_is_destroyed (data->timeout1));
/* g_source_get_time() does not change between different sources in /* g_source_get_time() does not change between different sources in
* a single iteration of the mainloop. * a single iteration of the mainloop.
@ -804,8 +804,8 @@ test_source_time (void)
g_main_loop_run (data.loop); g_main_loop_run (data.loop);
g_assert (!g_source_is_destroyed (data.timeout1)); g_assert_false (g_source_is_destroyed (data.timeout1));
g_assert (g_source_is_destroyed (data.timeout2)); g_assert_true (g_source_is_destroyed (data.timeout2));
g_source_destroy (data.timeout1); g_source_destroy (data.timeout1);
g_source_unref (data.timeout1); g_source_unref (data.timeout1);
@ -833,9 +833,9 @@ on_source_fired_cb (gpointer user_data)
current_source = g_main_current_source (); current_source = g_main_current_source ();
current_context = g_source_get_context (current_source); current_context = g_source_get_context (current_source);
source_id = g_source_get_id (current_source); source_id = g_source_get_id (current_source);
g_assert (g_main_context_find_source_by_id (current_context, source_id) != NULL); g_assert_nonnull (g_main_context_find_source_by_id (current_context, source_id));
g_source_destroy (current_source); g_source_destroy (current_source);
g_assert (g_main_context_find_source_by_id (current_context, source_id) == NULL); g_assert_null (g_main_context_find_source_by_id (current_context, source_id));
if (data->outstanding_ops == 0) if (data->outstanding_ops == 0)
g_main_loop_quit (data->loop); g_main_loop_quit (data->loop);
@ -945,7 +945,7 @@ test_ready_time (void)
/* A source with no ready time set should not fire */ /* A source with no ready time set should not fire */
g_assert_cmpint (g_source_get_ready_time (source), ==, -1); g_assert_cmpint (g_source_get_ready_time (source), ==, -1);
while (g_main_context_iteration (NULL, FALSE)); while (g_main_context_iteration (NULL, FALSE));
g_assert (!ready_time_dispatched); g_assert_false (ready_time_dispatched);
/* The ready time should not have been changed */ /* The ready time should not have been changed */
g_assert_cmpint (g_source_get_ready_time (source), ==, -1); g_assert_cmpint (g_source_get_ready_time (source), ==, -1);
@ -959,21 +959,21 @@ test_ready_time (void)
*/ */
g_source_set_ready_time (source, g_get_monotonic_time () + G_TIME_SPAN_DAY); g_source_set_ready_time (source, g_get_monotonic_time () + G_TIME_SPAN_DAY);
while (g_main_context_iteration (NULL, FALSE)); while (g_main_context_iteration (NULL, FALSE));
g_assert (!ready_time_dispatched); g_assert_false (ready_time_dispatched);
/* Make sure it didn't get reset */ /* Make sure it didn't get reset */
g_assert_cmpint (g_source_get_ready_time (source), !=, -1); g_assert_cmpint (g_source_get_ready_time (source), !=, -1);
/* Ready time of -1 -> don't fire */ /* Ready time of -1 -> don't fire */
g_source_set_ready_time (source, -1); g_source_set_ready_time (source, -1);
while (g_main_context_iteration (NULL, FALSE)); while (g_main_context_iteration (NULL, FALSE));
g_assert (!ready_time_dispatched); g_assert_false (ready_time_dispatched);
/* Not reset, but should still be -1 from above */ /* Not reset, but should still be -1 from above */
g_assert_cmpint (g_source_get_ready_time (source), ==, -1); g_assert_cmpint (g_source_get_ready_time (source), ==, -1);
/* A ready time of the current time should fire immediately */ /* A ready time of the current time should fire immediately */
g_source_set_ready_time (source, g_get_monotonic_time ()); g_source_set_ready_time (source, g_get_monotonic_time ());
while (g_main_context_iteration (NULL, FALSE)); while (g_main_context_iteration (NULL, FALSE));
g_assert (ready_time_dispatched); g_assert_true (ready_time_dispatched);
ready_time_dispatched = FALSE; ready_time_dispatched = FALSE;
/* Should have gotten reset by the handler function */ /* Should have gotten reset by the handler function */
g_assert_cmpint (g_source_get_ready_time (source), ==, -1); g_assert_cmpint (g_source_get_ready_time (source), ==, -1);
@ -981,14 +981,14 @@ test_ready_time (void)
/* As well as one in the recent past... */ /* As well as one in the recent past... */
g_source_set_ready_time (source, g_get_monotonic_time () - G_TIME_SPAN_SECOND); g_source_set_ready_time (source, g_get_monotonic_time () - G_TIME_SPAN_SECOND);
while (g_main_context_iteration (NULL, FALSE)); while (g_main_context_iteration (NULL, FALSE));
g_assert (ready_time_dispatched); g_assert_true (ready_time_dispatched);
ready_time_dispatched = FALSE; ready_time_dispatched = FALSE;
g_assert_cmpint (g_source_get_ready_time (source), ==, -1); g_assert_cmpint (g_source_get_ready_time (source), ==, -1);
/* Zero is the 'official' way to get a source to fire immediately */ /* Zero is the 'official' way to get a source to fire immediately */
g_source_set_ready_time (source, 0); g_source_set_ready_time (source, 0);
while (g_main_context_iteration (NULL, FALSE)); while (g_main_context_iteration (NULL, FALSE));
g_assert (ready_time_dispatched); g_assert_true (ready_time_dispatched);
ready_time_dispatched = FALSE; ready_time_dispatched = FALSE;
g_assert_cmpint (g_source_get_ready_time (source), ==, -1); g_assert_cmpint (g_source_get_ready_time (source), ==, -1);
@ -1126,7 +1126,7 @@ write_bytes (gint fd,
return FALSE; return FALSE;
/* Detect if we run before we should */ /* Detect if we run before we should */
g_assert (*to_write >= 0); g_assert_cmpint (*to_write, >=, 0);
limit = MIN (*to_write, sizeof zeros); limit = MIN (*to_write, sizeof zeros);
*to_write -= write (fd, zeros, limit); *to_write -= write (fd, zeros, limit);
@ -1163,7 +1163,7 @@ test_unix_fd (void)
GSource *source_b; GSource *source_b;
s = pipe (fds); s = pipe (fds);
g_assert (s == 0); g_assert_cmpint (s, ==, 0);
to_read = fill_a_pipe (fds[1]); to_read = fill_a_pipe (fds[1]);
/* write at higher priority to keep the pipe full... */ /* write at higher priority to keep the pipe full... */
@ -1192,17 +1192,17 @@ test_unix_fd (void)
/* Since the sources are at different priority, only one of them /* Since the sources are at different priority, only one of them
* should possibly have run. * should possibly have run.
*/ */
g_assert (to_write == to_write_was || to_read == to_read_was); g_assert_true (to_write == to_write_was || to_read == to_read_was);
} }
g_assert (to_write == 0); g_assert_cmpint (to_write, ==, 0);
g_assert (to_read == 0); g_assert_cmpint (to_read, ==, 0);
/* 'a' is already removed by itself */ /* 'a' is already removed by itself */
g_assert (g_source_is_destroyed (source_a)); g_assert_true (g_source_is_destroyed (source_a));
g_source_unref (source_a); g_source_unref (source_a);
g_source_remove (b); g_source_remove (b);
g_assert (g_source_is_destroyed (source_b)); g_assert_true (g_source_is_destroyed (source_b));
g_source_unref (source_b); g_source_unref (source_b);
close (fds[1]); close (fds[1]);
close (fds[0]); close (fds[0]);
@ -1227,10 +1227,10 @@ assert_main_context_state (gint n_to_poll,
context = g_main_context_default (); context = g_main_context_default ();
acquired = g_main_context_acquire (context); acquired = g_main_context_acquire (context);
g_assert (acquired); g_assert_true (acquired);
immediate = g_main_context_prepare (context, &max_priority); immediate = g_main_context_prepare (context, &max_priority);
g_assert (!immediate); g_assert_false (immediate);
n = g_main_context_query (context, max_priority, &timeout, poll_fds, 10); n = g_main_context_query (context, max_priority, &timeout, poll_fds, 10);
g_assert_cmpint (n, ==, n_to_poll + 1); /* one will be the gwakeup */ g_assert_cmpint (n, ==, n_to_poll + 1); /* one will be the gwakeup */
@ -1290,7 +1290,7 @@ test_unix_fd_source (void)
assert_main_context_state (0); assert_main_context_state (0);
s = pipe (fds); s = pipe (fds);
g_assert (s == 0); g_assert_cmpint (s, ==, 0);
source = g_unix_fd_source_new (fds[1], G_IO_OUT); source = g_unix_fd_source_new (fds[1], G_IO_OUT);
g_source_attach (source, NULL); g_source_attach (source, NULL);
@ -1301,7 +1301,7 @@ test_unix_fd_source (void)
g_test_expect_message ("GLib", G_LOG_LEVEL_WARNING, "*GUnixFDSource dispatched without callback*"); g_test_expect_message ("GLib", G_LOG_LEVEL_WARNING, "*GUnixFDSource dispatched without callback*");
while (g_main_context_iteration (NULL, FALSE)); while (g_main_context_iteration (NULL, FALSE));
g_test_assert_expected_messages (); g_test_assert_expected_messages ();
g_assert (g_source_is_destroyed (source)); g_assert_true (g_source_is_destroyed (source));
g_source_unref (source); g_source_unref (source);
out = in = FALSE; out = in = FALSE;
@ -1324,7 +1324,7 @@ test_unix_fd_source (void)
g_source_attach (out_source, NULL); g_source_attach (out_source, NULL);
assert_main_context_state (1, assert_main_context_state (1,
fds[1], G_IO_OUT, 0); fds[1], G_IO_OUT, 0);
g_assert (!in && !out); g_assert_true (!in && !out);
in_source = g_unix_fd_source_new (fds[0], G_IO_IN); in_source = g_unix_fd_source_new (fds[0], G_IO_IN);
g_source_set_callback (in_source, (GSourceFunc) flag_bool, &in, NULL); g_source_set_callback (in_source, (GSourceFunc) flag_bool, &in, NULL);
@ -1334,7 +1334,7 @@ test_unix_fd_source (void)
fds[0], G_IO_IN, G_IO_IN, fds[0], G_IO_IN, G_IO_IN,
fds[1], G_IO_OUT, G_IO_OUT); fds[1], G_IO_OUT, G_IO_OUT);
/* out is higher priority so only it should fire */ /* out is higher priority so only it should fire */
g_assert (!in && out); g_assert_true (!in && out);
/* raise the priority of the in source to higher than out*/ /* raise the priority of the in source to higher than out*/
in = out = FALSE; in = out = FALSE;
@ -1342,7 +1342,7 @@ test_unix_fd_source (void)
assert_main_context_state (2, assert_main_context_state (2,
fds[0], G_IO_IN, G_IO_IN, fds[0], G_IO_IN, G_IO_IN,
fds[1], G_IO_OUT, G_IO_OUT); fds[1], G_IO_OUT, G_IO_OUT);
g_assert (in && !out); g_assert_true (in && !out);
/* now, let them be equal */ /* now, let them be equal */
in = out = FALSE; in = out = FALSE;
@ -1350,7 +1350,7 @@ test_unix_fd_source (void)
assert_main_context_state (2, assert_main_context_state (2,
fds[0], G_IO_IN, G_IO_IN, fds[0], G_IO_IN, G_IO_IN,
fds[1], G_IO_OUT, G_IO_OUT); fds[1], G_IO_OUT, G_IO_OUT);
g_assert (in && out); g_assert_true (in && out);
g_source_destroy (out_source); g_source_destroy (out_source);
g_source_unref (out_source); g_source_unref (out_source);
@ -1376,8 +1376,8 @@ return_true (GSource *source, GSourceFunc callback, gpointer user_data)
return TRUE; return TRUE;
} }
#define assert_flagged(s) g_assert (((FlagSource *) (s))->flagged); #define assert_flagged(s) g_assert_true (((FlagSource *) (s))->flagged);
#define assert_not_flagged(s) g_assert (!((FlagSource *) (s))->flagged); #define assert_not_flagged(s) g_assert_true (!((FlagSource *) (s))->flagged);
#define clear_flag(s) ((FlagSource *) (s))->flagged = 0 #define clear_flag(s) ((FlagSource *) (s))->flagged = 0
static void static void
@ -1505,7 +1505,7 @@ test_unix_file_poll (void)
GMainLoop *loop; GMainLoop *loop;
fd = open ("/dev/null", O_RDONLY); fd = open ("/dev/null", O_RDONLY);
g_assert (fd >= 0); g_assert_cmpint (fd, >=, 0);
loop = g_main_loop_new (NULL, FALSE); loop = g_main_loop_new (NULL, FALSE);
@ -1537,8 +1537,8 @@ timeout_cb (gpointer data)
GMainContext *context; GMainContext *context;
context = g_main_loop_get_context (loop); context = g_main_loop_get_context (loop);
g_assert (g_main_loop_is_running (loop)); g_assert_true (g_main_loop_is_running (loop));
g_assert (g_main_context_is_owner (context)); g_assert_true (g_main_context_is_owner (context));
g_main_loop_quit (loop); g_main_loop_quit (loop);
@ -1723,9 +1723,9 @@ test_nfds (void)
*/ */
g_main_context_iteration (ctx, FALSE); g_main_context_iteration (ctx, FALSE);
g_assert (source1_ran); g_assert_true (source1_ran);
#ifndef G_OS_WIN32 #ifndef G_OS_WIN32
g_assert (source3_ran); g_assert_true (source3_ran);
#endif #endif
g_source_destroy (source1); g_source_destroy (source1);