From b74f46db6b585abe7fb665651e51888aea88b356 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 3 Oct 2011 22:22:55 -0400 Subject: [PATCH] Add some more thread tests --- glib/tests/Makefile.am | 3 ++ glib/tests/thread.c | 119 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 glib/tests/thread.c diff --git a/glib/tests/Makefile.am b/glib/tests/Makefile.am index aa5e554af..80dd95ba7 100644 --- a/glib/tests/Makefile.am +++ b/glib/tests/Makefile.am @@ -209,6 +209,9 @@ cond_LDADD = $(progs_ldadd) TEST_PROGS += private private_LDADD = $(progs_ldadd) +TEST_PROGS += thread +thread_LDADD = $(progs_ldadd) + if OS_UNIX TEST_PROGS += unix diff --git a/glib/tests/thread.c b/glib/tests/thread.c new file mode 100644 index 000000000..18aaa49eb --- /dev/null +++ b/glib/tests/thread.c @@ -0,0 +1,119 @@ +/* Unit tests for GThread + * Copyright (C) 2011 Red Hat, Inc + * Author: Matthias Clasen + * + * This work is provided "as is"; redistribution and modification + * in whole or in part, in any medium, physical or electronic is + * permitted without restriction. + * + * This work is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * In no event shall the authors or contributors be liable for any + * direct, indirect, incidental, special, exemplary, or consequential + * damages (including, but not limited to, procurement of substitute + * goods or services; loss of use, data, or profits; or business + * interruption) however caused and on any theory of liability, whether + * in contract, strict liability, or tort (including negligence or + * otherwise) arising in any way out of the use of this software, even + * if advised of the possibility of such damage. + */ + +#include + +static gpointer +thread1_func (gpointer data) +{ + g_thread_exit (GINT_TO_POINTER (1)); + + g_assert_not_reached (); + + return NULL; +} + +/* test that g_thread_exit() works */ +static void +test_thread1 (void) +{ + gpointer result; + GThread *thread; + + thread = g_thread_new ("test", thread1_func, NULL, TRUE, NULL); + + result = g_thread_join (thread); + + g_assert_cmpint (GPOINTER_TO_INT (result), ==, 1); +} + +static gpointer +thread2_func (gpointer data) +{ + return g_thread_self (); +} + +/* test that g_thread_self() works */ +static void +test_thread2 (void) +{ + gpointer result; + GThread *thread; + + thread = g_thread_new ("test", thread2_func, NULL, TRUE, NULL); + + g_assert (g_thread_self () != thread); + + result = g_thread_join (thread); + + g_assert (result == thread); +} + +static gpointer +thread3_func (gpointer data) +{ + GThread *peer = data; + gint retval; + + retval = 3; + + if (peer) + { + gpointer result; + + result = g_thread_join (peer); + + retval += GPOINTER_TO_INT (result); + } + + return GINT_TO_POINTER (retval); +} + +/* test that g_thread_join() works across peers */ +static void +test_thread3 (void) +{ + gpointer result; + GThread *thread1, *thread2, *thread3; + + thread1 = g_thread_new ("a", thread3_func, NULL, TRUE, NULL); + thread2 = g_thread_new ("b", thread3_func, thread1, TRUE, NULL); + thread3 = g_thread_new ("c", thread3_func, thread2, TRUE, NULL); + + result = g_thread_join (thread3); + + g_assert_cmpint (GPOINTER_TO_INT(result), ==, 9); +} + +int +main (int argc, char *argv[]) +{ + g_test_init (&argc, &argv, NULL); + + g_assert (g_thread_get_initialized ()); + + g_test_add_func ("/thread/thread1", test_thread1); + g_test_add_func ("/thread/thread2", test_thread2); + g_test_add_func ("/thread/thread3", test_thread3); + + return g_test_run (); +}