[Spice-devel] [PATCH spice-gtk 3/4] tests: add some coroutine tests

Marc-André Lureau marcandre.lureau at gmail.com
Tue Nov 19 07:14:24 PST 2013


---
 tests/Makefile.am |  18 +++++---
 tests/coroutine.c | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 147 insertions(+), 7 deletions(-)
 create mode 100644 tests/coroutine.c

diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9510e2c..6f80d93 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,18 +1,22 @@
 NULL =
 
-noinst_PROGRAMS = util
+noinst_PROGRAMS =				\
+	coroutine				\
+	util					\
+	$(NULL)
+
 TESTS = $(noinst_PROGRAMS)
 
 AM_CPPFLAGS =					\
 	$(GIO_CFLAGS) -I$(top_srcdir)/gtk	\
 	-DG_LOG_DOMAIN=\"GSpice\"		\
 	$(NULL)
-AM_LDFLAGS = $(GIO_LIBS)
 
-util_SOURCES =					\
-	$(top_srcdir)/gtk/spice-util-priv.h	\
-	$(top_srcdir)/gtk/spice-util.c		\
-	$(top_srcdir)/gtk/spice-util.h  	\
-	util.c					\
+AM_LDFLAGS = $(GIO_LIBS) -static
+
+LDADD =							\
+	$(top_builddir)/gtk/libspice-client-glib-2.0.la	\
 	$(NULL)
 
+util_SOURCES = util.c
+coroutine_SOURCES = coroutine.c
diff --git a/tests/coroutine.c b/tests/coroutine.c
new file mode 100644
index 0000000..09bb74a
--- /dev/null
+++ b/tests/coroutine.c
@@ -0,0 +1,136 @@
+#include <glib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "coroutine.h"
+
+static gpointer co_entry_check_self(gpointer data)
+{
+    g_assert(data == coroutine_self());
+    g_assert(!coroutine_self_is_main());
+
+    return NULL;
+}
+
+static gpointer co_entry_42(gpointer data)
+{
+    g_assert(GPOINTER_TO_INT(data) == 42);
+    g_assert(!coroutine_self_is_main());
+
+    return GINT_TO_POINTER(0x42);
+}
+
+static void test_coroutine_simple(void)
+{
+    struct coroutine *self = coroutine_self();
+    struct coroutine co = {
+        .stack_size = 16 << 20,
+        .entry = co_entry_42,
+    };
+    gpointer result;
+
+    g_assert(coroutine_self_is_main());
+
+    g_assert(coroutine_init(&co) == 0);
+    result = coroutine_yieldto(&co, GINT_TO_POINTER(42));
+    g_assert_cmpint(GPOINTER_TO_INT(result), ==, 0x42);
+
+#if GLIB_CHECK_VERSION(2,34,0)
+    g_test_expect_message(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*!to->exited*");
+    coroutine_yieldto(&co, GINT_TO_POINTER(42));
+    g_test_assert_expected_messages();
+#endif
+
+    g_assert(self == coroutine_self());
+    g_assert(coroutine_self_is_main());
+}
+
+static gpointer co_entry_two(gpointer data)
+{
+    struct coroutine *self = coroutine_self();
+    struct coroutine co = {
+        .stack_size = 16 << 20,
+        .entry = co_entry_check_self,
+    };
+
+    g_assert(!coroutine_self_is_main());
+    g_assert(coroutine_init(&co) == 0);
+    coroutine_yieldto(&co, &co);
+
+    g_assert(self == coroutine_self());
+    return NULL;
+}
+
+static void test_coroutine_two(void)
+{
+    struct coroutine *self = coroutine_self();
+    struct coroutine co = {
+        .stack_size = 16 << 20,
+        .entry = co_entry_two,
+    };
+
+    g_assert(coroutine_init(&co) == 0);
+    coroutine_yieldto(&co, NULL);
+
+    g_assert(self == coroutine_self());
+}
+
+static gpointer co_entry_yield(gpointer data)
+{
+    gpointer val;
+
+    g_assert(data == NULL);
+    val = coroutine_yield(GINT_TO_POINTER(1));
+    g_assert_cmpint(GPOINTER_TO_INT(val), ==, 2);
+
+    g_assert(!coroutine_self_is_main());
+
+    val = coroutine_yield(GINT_TO_POINTER(3));
+    g_assert_cmpint(GPOINTER_TO_INT(val), ==, 4);
+
+    return NULL;
+}
+
+static void test_coroutine_yield(void)
+{
+    struct coroutine *self = coroutine_self();
+    struct coroutine co = {
+        .stack_size = 16 << 20,
+        .entry = co_entry_yield,
+    };
+    gpointer val;
+
+    g_assert (coroutine_init(&co) == 0);
+    val = coroutine_yieldto(&co, NULL);
+
+    g_assert(self == coroutine_self());
+    g_assert_cmpint(GPOINTER_TO_INT(val), ==, 1);
+
+    val = coroutine_yieldto(&co, GINT_TO_POINTER(2));
+
+    g_assert(self == coroutine_self());
+    g_assert_cmpint(GPOINTER_TO_INT(val), ==, 3);
+
+    val = coroutine_yieldto(&co, GINT_TO_POINTER(4));
+
+    g_assert(self == coroutine_self());
+    g_assert(val == NULL);
+
+#if GLIB_CHECK_VERSION(2,34,0)
+    g_test_expect_message(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "*!to->exited*");
+    coroutine_yieldto(&co, GINT_TO_POINTER(42));
+    g_test_assert_expected_messages();
+#endif
+}
+
+int main(int argc, char* argv[])
+{
+    g_test_init(&argc, &argv, NULL);
+
+    g_test_add_func("/coroutine/simple", test_coroutine_simple);
+    g_test_add_func("/coroutine/two", test_coroutine_two);
+    g_test_add_func("/coroutine/yield", test_coroutine_yield);
+
+    return g_test_run ();
+}
-- 
1.8.3.1



More information about the Spice-devel mailing list