[gnome-color-manager] Add the start of a self test framework to be run on make check



commit 3403cd3491f942fa8720ad9ef50b8d93375c4a50
Author: Richard Hughes <richard hughsie com>
Date:   Fri Nov 27 10:05:09 2009 +0000

    Add the start of a self test framework to be run on make check

 configure.ac        |   11 ++
 src/.gitignore      |    1 +
 src/Makefile.am     |   29 ++++
 src/egg-test.c      |  357 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/egg-test.h      |   48 +++++++
 src/gcm-edid.c      |   34 +++++
 src/gcm-self-test.c |   45 +++++++
 7 files changed, 525 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 2b0aee3..ae30327 100644
--- a/configure.ac
+++ b/configure.ac
@@ -184,6 +184,16 @@ if test x$enable_hardware_detection = xyes; then
 fi
 
 dnl ---------------------------------------------------------------------------
+dnl - Build self tests
+dnl ---------------------------------------------------------------------------
+AC_ARG_ENABLE(tests, AS_HELP_STRING([--enable-tests],[enable unit test code]),
+	      enable_tests=$enableval,enable_tests=yes)
+AM_CONDITIONAL(EGG_BUILD_TESTS, test x$enable_tests = xyes)
+if test x$enable_tests = xyes; then
+	AC_DEFINE(EGG_BUILD_TESTS,1,[Build test code])
+fi
+
+dnl ---------------------------------------------------------------------------
 dnl - Makefiles, etc.
 dnl ---------------------------------------------------------------------------
 AC_OUTPUT([
@@ -214,6 +224,7 @@ echo "
         cflags:                    ${CFLAGS}
         cppflags:                  ${CPPFLAGS}
         hardware auto-detection:   ${enable_hardware_detection}
+        building unit tests:       ${enable_tests}
         gconf-schema dir:          $GCONF_SCHEMA_FILE_DIR
 "
 
diff --git a/src/.gitignore b/src/.gitignore
index d8edad6..6d6bbc7 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -8,5 +8,6 @@ gcm-prefs
 gcm-inspect
 gcm-session
 gcm-dump-edid
+gcm-self-test
 org.gnome.ColorManager.h
 
diff --git a/src/Makefile.am b/src/Makefile.am
index a704ae5..8a8d8f7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -154,6 +154,35 @@ gcm_session_LDADD =					\
 gcm_session_CFLAGS =					\
 	$(WARNINGFLAGS_C)
 
+if EGG_BUILD_TESTS
+
+check_PROGRAMS =					\
+	gcm-self-test
+
+gcm_self_test_SOURCES =					\
+	gcm-self-test.c					\
+	gcm-edid.c					\
+	egg-test.h					\
+	egg-test.c					\
+	$(NULL)
+
+gcm_self_test_LDADD =					\
+	libgcmshared.a					\
+	$(GLIB_LIBS)					\
+	$(GNOMEDESKTOP_LIBS)				\
+	$(UNIQUE_LIBS)					\
+	$(GCONF_LIBS)					\
+	$(GUDEV_LIBS)					\
+	$(DBUS_GLIB_LIBS)				\
+	$(XORG_LIBS)					\
+	$(GTK_LIBS)
+
+gcm_self_test_CFLAGS = -DEGG_TEST $(AM_CFLAGS)
+
+TESTS = gcm-self-test
+
+endif
+
 org.gnome.ColorManager.h: org.gnome.ColorManager.xml
 	$(LIBTOOL) --mode=execute dbus-binding-tool	\
 		--prefix=gcm_dbus			\
diff --git a/src/egg-test.c b/src/egg-test.c
new file mode 100644
index 0000000..ef4eef3
--- /dev/null
+++ b/src/egg-test.c
@@ -0,0 +1,357 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007-2008 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <stdlib.h>
+#include <glib.h>
+#include <string.h>
+#include <glib/gprintf.h>
+
+#include "egg-test.h"
+
+struct EggTest {
+	guint		 total;
+	guint		 succeeded;
+	gboolean	 started;
+	gboolean	 titled;
+	gchar		*type;
+	GTimer		*timer;
+	GMainLoop	*loop;
+	guint		 hang_loop_id;
+	gpointer	 user_data;
+};
+
+/**
+ * egg_test_init:
+ **/
+EggTest *
+egg_test_init ()
+{
+	EggTest *test;
+	test = g_new (EggTest, 1);
+	test->total = 0;
+	test->succeeded = 0;
+	test->type = NULL;
+	test->started = FALSE;
+	test->titled = FALSE;
+	test->timer = g_timer_new ();
+	test->loop = g_main_loop_new (NULL, FALSE);
+	test->hang_loop_id = 0;
+	return test;
+}
+
+/**
+ * egg_test_loop_quit:
+ **/
+void
+egg_test_loop_quit (EggTest *test)
+{
+	/* disable the loop watch */
+	if (test->hang_loop_id != 0) {
+		g_source_remove (test->hang_loop_id);
+		test->hang_loop_id = 0;
+	}
+	g_main_loop_quit (test->loop);
+}
+
+/**
+ * egg_test_hang_check:
+ **/
+static gboolean
+egg_test_hang_check (gpointer data)
+{
+	EggTest *test = (EggTest *) data;
+	g_main_loop_quit (test->loop);
+	return FALSE;
+}
+
+/**
+ * egg_test_loop_wait:
+ **/
+void
+egg_test_loop_wait (EggTest *test, guint timeout)
+{
+	test->hang_loop_id = g_timeout_add (timeout, egg_test_hang_check, test);
+	g_main_loop_run (test->loop);
+}
+
+/**
+ * egg_test_loop_check:
+ **/
+void
+egg_test_loop_check (EggTest *test)
+{
+	guint elapsed = egg_test_elapsed (test);
+	egg_test_title (test, "did we timeout out of the loop");
+	if (test->hang_loop_id == 0) {
+		egg_test_success (test, "loop blocked for %ims", elapsed);
+	} else {
+		egg_test_failed (test, "hangcheck saved us after %ims", elapsed);
+	}
+}
+
+/**
+ * egg_test_set_user_data:
+ **/
+void
+egg_test_set_user_data (EggTest *test, gpointer user_data)
+{
+	test->user_data = user_data;
+}
+
+/**
+ * egg_test_get_user_data:
+ **/
+gpointer
+egg_test_get_user_data (EggTest *test)
+{
+	return test->user_data;
+}
+
+/**
+ * egg_test_finish:
+ **/
+gint
+egg_test_finish (EggTest *test)
+{
+	gint retval;
+	g_print ("test passes (%u/%u) : ", test->succeeded, test->total);
+	if (test->succeeded == test->total) {
+		g_print ("ALL OKAY\n");
+		retval = 0;
+	} else {
+		g_print ("%u FAILURE(S)\n", test->total - test->succeeded);
+		retval = 1;
+	}
+
+	g_timer_destroy (test->timer);
+	g_main_loop_unref (test->loop);
+	g_free (test);
+
+	return retval;
+}
+
+/**
+ * egg_test_elapsed:
+ *
+ * Returns: time in ms
+ **/
+guint
+egg_test_elapsed (EggTest *test)
+{
+	gdouble time_s;
+	time_s = g_timer_elapsed (test->timer, NULL);
+	return (guint) (time_s * 1000.0f);
+}
+
+/**
+ * egg_test_start:
+ **/
+gboolean
+egg_test_start (EggTest *test, const gchar *name)
+{
+	if (test->started) {
+		g_print ("Not ended test! Cannot start!\n");
+		exit (1);
+	}
+	test->type = g_strdup (name);
+	test->started = TRUE;
+	return TRUE;
+}
+
+/**
+ * egg_test_end:
+ **/
+void
+egg_test_end (EggTest *test)
+{
+	if (test->started == FALSE) {
+		g_print ("Not started test! Cannot finish!\n");
+		exit (1);
+	}
+
+	/* disable hang check */
+	if (test->hang_loop_id != 0) {
+		g_source_remove (test->hang_loop_id);
+		test->hang_loop_id = 0;
+	}
+
+	/* remove all the test callbacks */
+	while (g_source_remove_by_user_data (test))
+		g_print ("WARNING: removed callback for test module");
+
+	/* check we don't have any pending iterations */
+	if (g_main_context_pending (NULL)) {
+		g_print ("WARNING: Pending event in context! Running to completion... ");
+		while (g_main_context_pending (NULL))
+			g_main_context_iteration (NULL, TRUE);
+		g_print ("Done!\n");
+	}
+
+	test->started = FALSE;
+	g_free (test->type);
+}
+
+/**
+ * egg_test_title:
+ **/
+void
+egg_test_title (EggTest *test, const gchar *format, ...)
+{
+	va_list args;
+	gchar *va_args_buffer = NULL;
+
+	/* already titled? */
+	if (test->titled) {
+		g_print ("Already titled!\n");
+		exit (1);
+	}
+
+	/* reset the value egg_test_elapsed replies with */
+	g_timer_reset (test->timer);
+
+	va_start (args, format);
+	g_vasprintf (&va_args_buffer, format, args);
+	va_end (args);
+	g_print ("> check #%u\t%s: \t%s...", test->total+1, test->type, va_args_buffer);
+	g_free (va_args_buffer);
+
+	test->titled = TRUE;
+	test->total++;
+}
+
+/**
+ * egg_test_success:
+ **/
+void
+egg_test_success (EggTest *test, const gchar *format, ...)
+{
+	va_list args;
+	gchar *va_args_buffer = NULL;
+
+	/* not titled? */
+	if (!test->titled) {
+		g_print ("Not titled!\n");
+		exit (1);
+	}
+	if (format == NULL) {
+		g_print ("...OK\n");
+		goto finish;
+	}
+	va_start (args, format);
+	g_vasprintf (&va_args_buffer, format, args);
+	va_end (args);
+	g_print ("...OK [%s]\n", va_args_buffer);
+	g_free (va_args_buffer);
+finish:
+	test->titled = FALSE;
+	test->succeeded++;
+}
+
+/**
+ * egg_test_failed:
+ **/
+void
+egg_test_failed (EggTest *test, const gchar *format, ...)
+{
+	va_list args;
+	gchar *va_args_buffer = NULL;
+
+	/* not titled? */
+	if (!test->titled) {
+		g_print ("Not titled!\n");
+		exit (1);
+	}
+	if (format == NULL) {
+		g_print ("FAILED\n");
+		goto failed;
+	}
+	va_start (args, format);
+	g_vasprintf (&va_args_buffer, format, args);
+	va_end (args);
+	g_print ("FAILED [%s]\n", va_args_buffer);
+	g_free (va_args_buffer);
+failed:
+	exit (1);
+}
+
+/**
+ * egg_test_assert:
+ **/
+void
+egg_test_assert (EggTest *test, gboolean value)
+{
+	if (value)
+		egg_test_success (test, NULL);
+	else
+		egg_test_failed (test, NULL);
+}
+
+/**
+ * egg_test_title_assert:
+ **/
+void
+egg_test_title_assert (EggTest *test, const gchar *text, gboolean value)
+{
+	egg_test_title (test, "%s", text);
+	if (value)
+		egg_test_success (test, NULL);
+	else
+		egg_test_failed (test, NULL);
+}
+
+/**
+ * egg_test_get_data_file:
+ **/
+gchar *
+egg_test_get_data_file (const gchar *filename)
+{
+	gboolean ret;
+	gchar *full;
+
+	/* check to see if we are being run in the build root */
+	full = g_build_filename ("..", "data", "tests", filename, NULL);
+	ret = g_file_test (full, G_FILE_TEST_EXISTS);
+	if (ret)
+		return full;
+	g_free (full);
+
+	/* check to see if we are being run in the build root */
+	full = g_build_filename ("..", "..", "data", "tests", filename, NULL);
+	ret = g_file_test (full, G_FILE_TEST_EXISTS);
+	if (ret)
+		return full;
+	g_free (full);
+
+	/* check to see if we are being run in make check */
+	full = g_build_filename ("..", "..", "data", "tests", filename, NULL);
+	ret = g_file_test (full, G_FILE_TEST_EXISTS);
+	if (ret)
+		return full;
+	g_free (full);
+	full = g_build_filename ("..", "..", "..", "data", "tests", filename, NULL);
+	ret = g_file_test (full, G_FILE_TEST_EXISTS);
+	if (ret)
+		return full;
+	g_print ("[WARN] failed to find '%s'\n", full);
+	g_free (full);
+	return NULL;
+}
+
diff --git a/src/egg-test.h b/src/egg-test.h
new file mode 100644
index 0000000..ea4b94b
--- /dev/null
+++ b/src/egg-test.h
@@ -0,0 +1,48 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007-2008 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __EGG_TEST_H
+#define __EGG_TEST_H
+
+#include <glib.h>
+#include <string.h>
+
+typedef struct EggTest EggTest;
+
+gboolean	 egg_test_start			(EggTest *test, const gchar *name);
+void		 egg_test_end			(EggTest *test);
+void		 egg_test_title			(EggTest *test, const gchar *format, ...);
+void		 egg_test_title_assert		(EggTest *test, const gchar *text, gboolean value);
+void		 egg_test_assert		(EggTest *test, gboolean value);
+void		 egg_test_success		(EggTest *test, const gchar *format, ...);
+void		 egg_test_failed		(EggTest *test, const gchar *format, ...) G_GNUC_NORETURN;
+EggTest		*egg_test_init			(void);
+gint		 egg_test_finish		(EggTest *test);
+guint		 egg_test_elapsed		(EggTest *test);
+void		 egg_test_loop_quit		(EggTest *test);
+void		 egg_test_loop_wait		(EggTest *test, guint timeout);
+void		 egg_test_loop_check		(EggTest *test);
+void		 egg_test_set_user_data		(EggTest *test, gpointer user_data);
+gpointer	 egg_test_get_user_data		(EggTest *test);
+gchar		*egg_test_get_data_file		(const gchar *filename);
+
+#endif	/* __EGG_TEST_H */
+
diff --git a/src/gcm-edid.c b/src/gcm-edid.c
index 47d1215..50696a0 100644
--- a/src/gcm-edid.c
+++ b/src/gcm-edid.c
@@ -372,3 +372,37 @@ gcm_edid_new (void)
 	return GCM_EDID (edid);
 }
 
+/***************************************************************************
+ ***                          MAKE CHECK TESTS                           ***
+ ***************************************************************************/
+#ifdef EGG_TEST
+#include "egg-test.h"
+
+void
+gcm_edid_test (EggTest *test)
+{
+	GcmEdid *edid;
+
+	if (!egg_test_start (test, "GcmEdid"))
+		return;
+
+	/************************************************************/
+	egg_test_title (test, "get a edid object");
+	edid = gcm_edid_new ();
+	egg_test_assert (test, edid != NULL);
+
+#if 0
+	/************************************************************/
+	egg_test_title (test, "parse an example edid");
+	ret = gcm_edid_parse (edid, &error);
+	if (!ret)
+		egg_test_success (test, NULL);
+	else
+		egg_test_failed (test, "failed to parse: %s", error->message);
+#endif
+	g_object_unref (edid);
+
+	egg_test_end (test);
+}
+#endif
+
diff --git a/src/gcm-self-test.c b/src/gcm-self-test.c
new file mode 100644
index 0000000..8f82300
--- /dev/null
+++ b/src/gcm-self-test.c
@@ -0,0 +1,45 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007 Richard Hughes <richard hughsie com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <glib-object.h>
+#include "egg-test.h"
+#include <egg-debug.h>
+
+/* prototypes */
+void gcm_edid_test (EggTest *test);
+
+int
+main (int argc, char **argv)
+{
+	EggTest *test;
+
+	if (! g_thread_supported ())
+		g_thread_init (NULL);
+	g_type_init ();
+	test = egg_test_init ();
+	egg_debug_init (&argc, &argv);
+
+	/* components */
+	gcm_edid_test (test);
+
+	return (egg_test_finish (test));
+}
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]