Move tests/thread-test.c to glib/tests/thread-deprecated.c

Helps issue #1434
This commit is contained in:
Emmanuel Fleury 2022-05-05 18:41:54 +02:00
parent 6399ff04c3
commit 647506966d
3 changed files with 91 additions and 81 deletions

View File

@ -115,6 +115,7 @@ glib_tests = {
'testing' : {}, 'testing' : {},
'test-printf' : {}, 'test-printf' : {},
'thread' : {}, 'thread' : {},
'thread-deprecated' : {},
'thread-pool' : {}, 'thread-pool' : {},
'thread-pool-slow' : {'suite' : ['slow']}, 'thread-pool-slow' : {'suite' : ['slow']},
'timeout' : {}, 'timeout' : {},

View File

@ -1,5 +1,6 @@
#undef G_DISABLE_ASSERT #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
#undef G_LOG_DOMAIN #define GLIB_DISABLE_DEPRECATION_WARNINGS
#endif
#include <glib.h> #include <glib.h>
@ -13,12 +14,12 @@ G_LOCK_DEFINE_STATIC (test_g_mutex);
static gpointer static gpointer
test_g_mutex_thread (gpointer data) test_g_mutex_thread (gpointer data)
{ {
g_assert (GPOINTER_TO_INT (data) == 42); g_assert_cmpint (GPOINTER_TO_INT (data), ==, 42);
g_assert (g_mutex_trylock (&test_g_mutex_mutex) == FALSE); g_assert_false (g_mutex_trylock (&test_g_mutex_mutex));
g_assert (G_TRYLOCK (test_g_mutex) == FALSE); g_assert_false (G_TRYLOCK (test_g_mutex));
test_g_mutex_thread_ready = TRUE; test_g_mutex_thread_ready = TRUE;
g_mutex_lock (&test_g_mutex_mutex); g_mutex_lock (&test_g_mutex_mutex);
g_assert (test_g_mutex_int == 42); g_assert_cmpint (test_g_mutex_int, ==, 42);
g_mutex_unlock (&test_g_mutex_mutex); g_mutex_unlock (&test_g_mutex_mutex);
return GINT_TO_POINTER (41); return GINT_TO_POINTER (41);
@ -29,8 +30,8 @@ test_g_mutex (void)
{ {
GThread *thread; GThread *thread;
g_assert (g_mutex_trylock (&test_g_mutex_mutex)); g_assert_true (g_mutex_trylock (&test_g_mutex_mutex));
g_assert (G_TRYLOCK (test_g_mutex)); g_assert_true (G_TRYLOCK (test_g_mutex));
test_g_mutex_thread_ready = FALSE; test_g_mutex_thread_ready = FALSE;
thread = g_thread_create (test_g_mutex_thread, GINT_TO_POINTER (42), thread = g_thread_create (test_g_mutex_thread, GINT_TO_POINTER (42),
TRUE, NULL); TRUE, NULL);
@ -41,7 +42,7 @@ test_g_mutex (void)
test_g_mutex_int = 42; test_g_mutex_int = 42;
G_UNLOCK (test_g_mutex); G_UNLOCK (test_g_mutex);
g_mutex_unlock (&test_g_mutex_mutex); g_mutex_unlock (&test_g_mutex_mutex);
g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 41); g_assert_cmpint (GPOINTER_TO_INT (g_thread_join (thread)), ==, 41);
} }
/* GStaticRecMutex */ /* GStaticRecMutex */
@ -53,13 +54,14 @@ static gboolean test_g_static_rec_mutex_thread_ready;
static gpointer static gpointer
test_g_static_rec_mutex_thread (gpointer data) test_g_static_rec_mutex_thread (gpointer data)
{ {
g_assert (GPOINTER_TO_INT (data) == 42); g_assert_cmpint (GPOINTER_TO_INT (data), ==, 42);
g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex) g_assert_false (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
== FALSE);
test_g_static_rec_mutex_thread_ready = TRUE; test_g_static_rec_mutex_thread_ready = TRUE;
g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex); g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex); g_static_rec_mutex_lock (&test_g_static_rec_mutex_mutex);
g_assert (test_g_static_rec_mutex_int == 42); g_assert_cmpint (test_g_static_rec_mutex_int, ==, 42);
test_g_static_rec_mutex_thread_ready = FALSE; test_g_static_rec_mutex_thread_ready = FALSE;
g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex); g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex); g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
@ -75,7 +77,7 @@ test_g_static_rec_mutex (void)
{ {
GThread *thread; GThread *thread;
g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex)); g_assert_true (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
test_g_static_rec_mutex_thread_ready = FALSE; test_g_static_rec_mutex_thread_ready = FALSE;
thread = g_thread_create (test_g_static_rec_mutex_thread, thread = g_thread_create (test_g_static_rec_mutex_thread,
GINT_TO_POINTER (42), TRUE, NULL); GINT_TO_POINTER (42), TRUE, NULL);
@ -84,7 +86,7 @@ test_g_static_rec_mutex (void)
while (!test_g_static_rec_mutex_thread_ready) while (!test_g_static_rec_mutex_thread_ready)
g_usleep (G_USEC_PER_SEC / 5); g_usleep (G_USEC_PER_SEC / 5);
g_assert (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex)); g_assert_true (g_static_rec_mutex_trylock (&test_g_static_rec_mutex_mutex));
test_g_static_rec_mutex_int = 41; test_g_static_rec_mutex_int = 41;
g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex); g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
test_g_static_rec_mutex_int = 42; test_g_static_rec_mutex_int = 42;
@ -99,7 +101,7 @@ test_g_static_rec_mutex (void)
test_g_static_rec_mutex_int = 0; test_g_static_rec_mutex_int = 0;
g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex); g_static_rec_mutex_unlock (&test_g_static_rec_mutex_mutex);
g_assert (GPOINTER_TO_INT (g_thread_join (thread)) == 43); g_assert_cmpint (GPOINTER_TO_INT (g_thread_join (thread)), ==, 43);
} }
/* GStaticPrivate */ /* GStaticPrivate */
@ -157,8 +159,8 @@ test_g_static_private_thread (gpointer data)
} }
*private2 = number * 2; *private2 = number * 2;
g_usleep (G_USEC_PER_SEC / 5); g_usleep (G_USEC_PER_SEC / 5);
g_assert (number == *private1); g_assert_cmpint (number, ==, *private1);
g_assert (number * 2 == *private2); g_assert_cmpint (number * 2, ==, *private2);
} }
g_mutex_lock (&test_g_static_private_mutex); g_mutex_lock (&test_g_static_private_mutex);
test_g_static_private_ready++; test_g_static_private_ready++;
@ -180,7 +182,7 @@ test_g_static_private_thread (gpointer data)
} }
*private2 = number * 2; *private2 = number * 2;
g_usleep (G_USEC_PER_SEC / 5); g_usleep (G_USEC_PER_SEC / 5);
g_assert (number * 2 == *private2); g_assert_cmpint (number * 2, ==, *private2);
} }
return GINT_TO_POINTER (GPOINTER_TO_INT (data) * 3); return GINT_TO_POINTER (GPOINTER_TO_INT (data) * 3);
@ -211,9 +213,9 @@ test_g_static_private (void)
test_g_static_private_ready = 0; test_g_static_private_ready = 0;
for (i = 0; i < THREADS; i++) for (i = 0; i < THREADS; i++)
g_assert (GPOINTER_TO_UINT (g_thread_join (threads[i])) == i * 3); g_assert_cmpint (GPOINTER_TO_UINT (g_thread_join (threads[i])), ==, i * 3);
g_assert (test_g_static_private_counter == 0); g_assert_cmpint (test_g_static_private_counter, ==, 0);
} }
/* GStaticRWLock */ /* GStaticRWLock */
@ -239,7 +241,7 @@ test_g_static_rw_lock_thread (gpointer data)
if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock)) if (!g_static_rw_lock_reader_trylock (&test_g_static_rw_lock_lock))
continue; continue;
G_LOCK (test_g_static_rw_lock_state); G_LOCK (test_g_static_rw_lock_state);
g_assert (test_g_static_rw_lock_state >= 0); g_assert_cmpint (test_g_static_rw_lock_state, >=, 0);
test_g_static_rw_lock_state++; test_g_static_rw_lock_state++;
G_UNLOCK (test_g_static_rw_lock_state); G_UNLOCK (test_g_static_rw_lock_state);
@ -253,14 +255,13 @@ test_g_static_rw_lock_thread (gpointer data)
} }
else /* I'm a writer */ else /* I'm a writer */
{ {
if (g_random_double () > .2) /* I'll block */
if (g_random_double() > .2) /* I'll block */
g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock); g_static_rw_lock_writer_lock (&test_g_static_rw_lock_lock);
else /* I'll only try */ else /* I'll only try */
if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock)) if (!g_static_rw_lock_writer_trylock (&test_g_static_rw_lock_lock))
continue; continue;
G_LOCK (test_g_static_rw_lock_state); G_LOCK (test_g_static_rw_lock_state);
g_assert (test_g_static_rw_lock_state == 0); g_assert_cmpint (test_g_static_rw_lock_state, ==, 0);
test_g_static_rw_lock_state = -1; test_g_static_rw_lock_state = -1;
G_UNLOCK (test_g_static_rw_lock_state); G_UNLOCK (test_g_static_rw_lock_state);
@ -292,7 +293,7 @@ test_g_static_rw_lock (void)
{ {
g_thread_join (threads[i]); g_thread_join (threads[i]);
} }
g_assert (test_g_static_rw_lock_state == 0); g_assert_cmpint (test_g_static_rw_lock_state, ==, 0);
} }
#define G_ONCE_SIZE 100 #define G_ONCE_SIZE 100
@ -322,17 +323,21 @@ test_g_once_thread (gpointer ignore)
for (i = 0; i < 1000; i++) for (i = 0; i < 1000; i++)
{ {
guint pos = g_random_int_range (0, G_ONCE_SIZE); guint pos = g_random_int_range (0, G_ONCE_SIZE);
gpointer ret = g_once (test_g_once_array + pos, test_g_once_init_func, gpointer ret = g_once (test_g_once_array + pos,
test_g_once_init_func,
test_g_once_guint_array + pos); test_g_once_guint_array + pos);
g_assert (ret == test_g_once_guint_array + pos); g_assert_cmpmem (ret, sizeof (int),
test_g_once_guint_array + pos, sizeof (int));
} }
/* Make sure, that all counters are touched at least once */ /* Make sure, that all counters are touched at least once */
for (i = 0; i < G_ONCE_SIZE; i++) for (i = 0; i < G_ONCE_SIZE; i++)
{ {
gpointer ret = g_once (test_g_once_array + i, test_g_once_init_func, gpointer ret = g_once (test_g_once_array + i,
test_g_once_init_func,
test_g_once_guint_array + i); test_g_once_guint_array + i);
g_assert (ret == test_g_once_guint_array + i); g_assert_cmpmem (ret, sizeof (int),
test_g_once_guint_array + i, sizeof (int));
} }
return NULL; return NULL;
@ -352,7 +357,7 @@ test_g_thread_once (void)
G_LOCK (test_g_once); G_LOCK (test_g_once);
for (i = 0; i < G_ONCE_THREADS; i++) for (i = 0; i < G_ONCE_THREADS; i++)
{ {
threads[i] = g_thread_create (test_g_once_thread, GUINT_TO_POINTER(i%2), threads[i] = g_thread_create (test_g_once_thread, GUINT_TO_POINTER (i % 2),
TRUE, NULL); TRUE, NULL);
} }
G_UNLOCK (test_g_once); G_UNLOCK (test_g_once);
@ -363,38 +368,43 @@ test_g_thread_once (void)
for (i = 0; i < G_ONCE_SIZE; i++) for (i = 0; i < G_ONCE_SIZE; i++)
{ {
g_assert (test_g_once_guint_array[i] == i + 1); g_assert_cmpint (test_g_once_guint_array[i], ==, i + 1);
} }
} }
/* run all the tests */ /* rerun all the tests */
static void static void
run_all_tests (void) test_rerun_all (void)
{ {
/* Now we rerun all tests, but this time we fool the system into
* thinking, that the available thread system is not native, but
* userprovided. */
g_thread_use_default_impl = FALSE;
test_g_mutex (); test_g_mutex ();
test_g_static_rec_mutex (); test_g_static_rec_mutex ();
test_g_static_private (); test_g_static_private ();
test_g_static_rw_lock (); test_g_static_rw_lock ();
test_g_thread_once (); test_g_thread_once ();
/* XXX: And this shows how silly the above non-native tests are */
g_static_rw_lock_free (&test_g_static_rw_lock_lock);
g_static_rec_mutex_free (&test_g_static_rec_mutex_mutex);
g_static_private_free (&test_g_static_private_private2);
} }
int int
main (int argc, main (int argc,
char *argv[]) char *argv[])
{ {
run_all_tests (); g_test_init (&argc, &argv, NULL);
/* Now we rerun all tests, but this time we fool the system into g_test_add_func ("/thread/mutex", test_g_mutex);
* thinking, that the available thread system is not native, but g_test_add_func ("/thread/static-rec-mutex", test_g_static_rec_mutex);
* userprovided. */ g_test_add_func ("/thread/static-private", test_g_static_private);
g_test_add_func ("/thread/static-rw-lock", test_g_static_rw_lock);
g_test_add_func ("/thread/once", test_g_thread_once);
g_test_add_func ("/thread/rerun-all", test_rerun_all);
g_thread_use_default_impl = FALSE; return g_test_run ();
run_all_tests ();
/* XXX: And this shows how silly the above non-native tests are */
g_static_rw_lock_free (&test_g_static_rw_lock_lock);
g_static_rec_mutex_free (&test_g_static_rec_mutex_mutex);
g_static_private_free (&test_g_static_private_private2);
return 0;
} }

View File

@ -22,7 +22,6 @@ tests = {
'slice-threadinit' : { 'slice-threadinit' : {
'dependencies' : [libgthread_dep], 'dependencies' : [libgthread_dep],
}, },
'thread-test' : {},
'module-test-library' : { 'module-test-library' : {
'dependencies' : [libgmodule_dep], 'dependencies' : [libgmodule_dep],
'export_dynamic' : true, 'export_dynamic' : true,