diff options
author | Richard Hughes <richard@hughsie.com> | 2007-08-31 00:20:13 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2007-08-31 00:20:13 +0100 |
commit | f2e239b7f7ee856baf236861b10596ee3482458a (patch) | |
tree | 1443ece77aef4b594b0666c502b4171bf7a41962 /libselftest | |
parent | 9db7bdd032ce7b5fc79282eda2d81f87f187297f (diff) |
use a selftest library
Diffstat (limited to 'libselftest')
-rw-r--r-- | libselftest/.gitignore | 8 | ||||
-rw-r--r-- | libselftest/Makefile.am | 20 | ||||
-rw-r--r-- | libselftest/libselftest.c | 142 | ||||
-rw-r--r-- | libselftest/libselftest.h | 62 |
4 files changed, 232 insertions, 0 deletions
diff --git a/libselftest/.gitignore b/libselftest/.gitignore new file mode 100644 index 00000000..c25dd54a --- /dev/null +++ b/libselftest/.gitignore @@ -0,0 +1,8 @@ +.deps +.libs +*.o +*.la +*.lo +*-marshal.c +*-marshal.h + diff --git a/libselftest/Makefile.am b/libselftest/Makefile.am new file mode 100644 index 00000000..3f119a56 --- /dev/null +++ b/libselftest/Makefile.am @@ -0,0 +1,20 @@ +INCLUDES = \ + $(GLIB_CFLAGS) \ + $(DBUS_CFLAGS) \ + -I$(top_srcdir)/libselftest-glib \ + -DPACKAGE_DATA_DIR=\""$(datadir)"\" \ + -DPACKAGE_LOCALE_DIR=\""$(prefix)/$(DATADIRNAME)/locale"\" + +noinst_LTLIBRARIES = \ + libselftest.la + +libselftest_la_SOURCES = \ + libselftest.c \ + libselftest.h +libselftest_la_LIBADD = @DBUS_LIBS@ $(INTLLIBS) $(GLIB_LIBS) + +clean-local: + rm -f *~ + +CLEANFILES = $(BUILT_SOURCES) + diff --git a/libselftest/libselftest.c b/libselftest/libselftest.c new file mode 100644 index 00000000..362ccb0b --- /dev/null +++ b/libselftest/libselftest.c @@ -0,0 +1,142 @@ +/* -*- 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 <stdlib.h> +#include <glib.h> +#include <string.h> +#include <glib/gi18n.h> +#include <glib-object.h> + +#include "libselftest.h" + +void +libselftest_init (LibSelfTest *test) +{ + test->total = 0; + test->succeeded = 0; + test->type = NULL; + test->started = FALSE; + test->class = CLASS_AUTO; + test->level = LEVEL_ALL; +} + +gint +libselftest_finish (LibSelfTest *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; + } + return retval; +} + +gboolean +libselftest_start (LibSelfTest *test, const gchar *name, LibSelfTestClass class) +{ + if (class == CLASS_AUTO && test->class == CLASS_MANUAL) { + return FALSE; + } + if (class == CLASS_MANUAL && test->class == CLASS_AUTO) { + return FALSE; + } + if (test->started == TRUE) { + g_print ("Not ended test! Cannot start!\n"); + exit (1); + } + test->type = g_strdup (name); + test->started = TRUE; + if (test->level == LEVEL_NORMAL) { + g_print ("%s...", test->type); + } + return TRUE; +} + +void +libselftest_end (LibSelfTest *test) +{ + if (test->started == FALSE) { + g_print ("Not started test! Cannot finish!\n"); + exit (1); + } + if (test->level == LEVEL_NORMAL) { + g_print ("OK\n"); + } + test->started = FALSE; + g_free (test->type); +} + +void +libselftest_title (LibSelfTest *test, const gchar *format, ...) +{ + va_list args; + gchar va_args_buffer [1025]; + if (test->level == LEVEL_ALL) { + va_start (args, format); + g_vsnprintf (va_args_buffer, 1024, format, args); + va_end (args); + g_print ("> check #%u\t%s: \t%s...", test->total+1, test->type, va_args_buffer); + } + test->total++; +} + +void +libselftest_success (LibSelfTest *test, const gchar *format, ...) +{ + va_list args; + gchar va_args_buffer [1025]; + if (test->level == LEVEL_ALL) { + if (format == NULL) { + g_print ("...OK\n"); + goto finish; + } + va_start (args, format); + g_vsnprintf (va_args_buffer, 1024, format, args); + va_end (args); + g_print ("...OK [%s]\n", va_args_buffer); + } +finish: + test->succeeded++; +} + +void +libselftest_failed (LibSelfTest *test, const gchar *format, ...) +{ + va_list args; + gchar va_args_buffer [1025]; + if (test->level == LEVEL_ALL || test->level == LEVEL_NORMAL) { + if (format == NULL) { + g_print ("FAILED\n"); + goto failed; + } + va_start (args, format); + g_vsnprintf (va_args_buffer, 1024, format, args); + va_end (args); + g_print ("FAILED [%s]\n", va_args_buffer); + } +failed: + exit (1); +} + diff --git a/libselftest/libselftest.h b/libselftest/libselftest.h new file mode 100644 index 00000000..ea6d9287 --- /dev/null +++ b/libselftest/libselftest.h @@ -0,0 +1,62 @@ +/* -*- 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. + */ + +#ifndef __libselftest_H +#define __libselftest_H + +#include <glib.h> + +typedef enum +{ + CLASS_ALL, + CLASS_AUTO, + CLASS_MANUAL, + CLASS_LAST +} LibSelfTestClass; + +typedef enum +{ + LEVEL_QUIET, + LEVEL_NORMAL, + LEVEL_ALL, + LEVEL_LAST +} LibSelfTestLevel; + +typedef struct +{ + guint total; + guint succeeded; + gboolean started; + LibSelfTestClass class; + LibSelfTestLevel level; + gchar *type; +} LibSelfTest; + +gboolean libselftest_start (LibSelfTest *test, const gchar *name, LibSelfTestClass class); +void libselftest_end (LibSelfTest *test); +void libselftest_title (LibSelfTest *test, const gchar *format, ...); +void libselftest_success (LibSelfTest *test, const gchar *format, ...); +void libselftest_failed (LibSelfTest *test, const gchar *format, ...); +void libselftest_init (LibSelfTest *test); +gint libselftest_finish (LibSelfTest *test); + +#endif /* __libselftest_H */ + |