From a8501fab9f7df0f1ec2ff80189f68b55c4810a0f Mon Sep 17 00:00:00 2001 From: ak1 Date: Thu, 24 Feb 2005 23:46:11 +0000 Subject: [PATCH] Andreas Krennmair: added simple test cases git-svn-id: svn://svn.berlios.de/mutt-ng/trunk@90 e385b8ad-14ed-0310-8656-cc95a2468c6d --- test/Makefile.am | 4 +- test/Makefile.in | 12 ++++-- test/test_muttng.c | 104 +++++++++++++++++++++++++++++++++++++++++++-- test/test_muttng.h | 3 +- 4 files changed, 113 insertions(+), 10 deletions(-) diff --git a/test/Makefile.am b/test/Makefile.am index 422dda9..16e9979 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,6 +1,6 @@ # mutt-ng test suite noinst_PROGRAMS=test_muttng -test_muttng_SOURCES= test_muttng.c -test_muttng_INCLUDES= @CFLAGS@ test_muttng.h +test_muttng_SOURCES= test_muttng.c ../lib.c +test_muttng_INCLUDES= @CFLAGS@ test_muttng.h -I../ test_muttng_LIBS= @LIBS@ diff --git a/test/Makefile.in b/test/Makefile.in index 750e70b..147eace 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -177,8 +177,8 @@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ noinst_PROGRAMS = test_muttng -test_muttng_SOURCES = test_muttng.c -test_muttng_INCLUDES = @CFLAGS@ test_muttng.h +test_muttng_SOURCES = test_muttng.c ../lib.c +test_muttng_INCLUDES = @CFLAGS@ test_muttng.h -I../ test_muttng_LIBS = @LIBS@ subdir = test ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 @@ -188,7 +188,7 @@ CONFIG_CLEAN_FILES = noinst_PROGRAMS = test_muttng$(EXEEXT) PROGRAMS = $(noinst_PROGRAMS) -am_test_muttng_OBJECTS = test_muttng.$(OBJEXT) +am_test_muttng_OBJECTS = test_muttng.$(OBJEXT) lib.$(OBJEXT) test_muttng_OBJECTS = $(am_test_muttng_OBJECTS) test_muttng_LDADD = $(LDADD) test_muttng_DEPENDENCIES = @@ -232,6 +232,12 @@ distclean-compile: .c.obj: $(COMPILE) -c `if test -f '$<'; then $(CYGPATH_W) '$<'; else $(CYGPATH_W) '$(srcdir)/$<'; fi` + +lib.o: ../lib.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o lib.o `test -f '../lib.c' || echo '$(srcdir)/'`../lib.c + +lib.obj: ../lib.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o lib.obj `if test -f '../lib.c'; then $(CYGPATH_W) '../lib.c'; else $(CYGPATH_W) '$(srcdir)/../lib.c'; fi` uninstall-info-am: ETAGS = etags diff --git a/test/test_muttng.c b/test/test_muttng.c index 43b2946..4f1f95a 100644 --- a/test/test_muttng.c +++ b/test/test_muttng.c @@ -1,16 +1,112 @@ /* based on MinUnit */ #include +#include #include "test_muttng.h" +#include + int tests_run = 0; +int asserts_run = 0; + +/* XXXXXXXXXXXXXXXXXXXXX */ +/* the following things are only here to satisfy the linker */ +int Umask; +void (*mutt_error) (const char *, ...); +const char * gettext(const char * a) { return a; } +void mutt_exit(int i) { exit(i); } +/* XXXXXXXXXXXXXXXXXXXX */ + + +/* tests the basic functions in lib.c */ +static char * test_lib(void) { + /* first check whether allocating 0 bytes behaves correctly */ + mu_assert("safe_malloc(0)!=NULL",safe_malloc(0)==NULL); + mu_assert("safe_calloc(0,0)!=NULL",safe_calloc(0,0)==NULL); + mu_assert("safe_calloc(1,0)!=NULL",safe_calloc(1,0)==NULL); + mu_assert("safe_calloc(0,1)!=NULL",safe_calloc(0,1)==NULL); + + /* check whether safe_malloc()/safe_free work correctly */ + { + char * ptr; + ptr = safe_malloc(1); + mu_assert("safe_malloc(1)==NULL",ptr!=NULL); + safe_free(&ptr); + mu_assert("ptr!=NULL",ptr==NULL); + } + + /* check whether safe_strdup works correctly */ + { + char * ptr; + mu_assert("safe_strdup(NULL)!=NULL",safe_strdup(NULL)==NULL); + mu_assert("safe_strdup("")!=NULL",safe_strdup("")==NULL); + ptr = safe_strdup("teststring"); + mu_assert("safe_strdup(\"teststring\")!=\"teststring\"",strcmp(ptr,"teststring")==0); + safe_free(&ptr); + + } + + /* check whether safe_strcat works correctly */ + { + char buf[16] = { 0 }; + safe_strcat(buf,sizeof(buf),"asdf"); + mu_assert("buf != \"asdf\"",strcmp(buf,"asdf")==0); + safe_strcat(buf,sizeof(buf),"qwer"); + mu_assert("buf != \"asdfqwer\"",strcmp(buf,"asdfqwer")==0); + safe_strcat(buf,sizeof(buf),"0123456789"); + mu_assert("buf != \"asdfqwer0123456\"",strcmp(buf,"asdfqwer0123456")==0); + safe_strcat(buf,sizeof(buf),"trash"); + mu_assert("buf != \"asdfqwer0123456\" (2)",strcmp(buf,"asdfqwer0123456")==0); + } + + /* check whether safe_strncat works correctly */ + { + char buf[16] = { 0 }; + safe_strncat(buf,sizeof(buf),"asdf",3); + mu_assert("buf != \"asd\"",strcmp(buf,"asd")==0); + safe_strncat(buf,sizeof(buf),"fghj",3); + mu_assert("buf != \"asdfgh\"",strcmp(buf,"asdfgh")==0); + safe_strncat(buf,sizeof(buf),"",10); + mu_assert("buf != \"asdfgh\" (2)",strcmp(buf,"asdfgh")==0); + safe_strncat(buf,sizeof(buf),"qwertzuiopyxcvvbnm",255); + mu_assert("buf != \"asdfghqwertzuio\"",strcmp(buf,"asdfghqwertzuio")==0); + } + + /* check whether mutt_str_replace works correctly */ + { + char * ptr = NULL; + mutt_str_replace(&ptr,"foobar"); + mu_assert("ptr != \"foobar\"",strcmp(ptr,"foobar")==0); + mutt_str_replace(&ptr,"quux"); + mu_assert("ptr != \"quux\"",strcmp(ptr,"quux")==0); + mutt_str_replace(&ptr,NULL); + mu_assert("ptr != NULL",ptr==NULL); + } + + /* check whether mutt_str_adjust works correctly */ + { + char * ptr = safe_strdup("some teststring"); + mutt_str_adjust(&ptr); + mu_assert("ptr != \"some teststring\"",strcmp(ptr,"some teststring")==0); + safe_free(&ptr); + } + + /* check whether mutt_strlower works correctly */ + { + char * supposed_result = "all you need is love"; + char * ptr = safe_strdup("ALL YOU NEED IS LOVE"); + mu_assert("ptr != \"all you need is love\"",strcmp(mutt_strlower(ptr),supposed_result)==0); + mutt_str_replace(&ptr,"All You Need Is LovE"); + mu_assert("ptr != \"all you need is love\" (2)",strcmp(mutt_strlower(ptr),supposed_result)==0); + mutt_str_replace(&ptr,"all you need is love"); + mu_assert("ptr != \"all you need is love\" (3)",strcmp(mutt_strlower(ptr),supposed_result)==0); + safe_free(&ptr); + } -static char * test_case(void) { - mu_assert("NULL != 0",NULL == 0); return 0; } static char * all_tests(void) { - mu_run_test(test_case); + mu_run_test(test_lib); return 0; } @@ -21,6 +117,6 @@ int main(int argc, char **argv) { } else { printf("ALL TESTS PASSED\n"); } - printf("Tests run: %d\n", tests_run); + printf("Tests run: %d\nAssertions checked: %d\n", tests_run,asserts_run); return result != 0; } diff --git a/test/test_muttng.h b/test/test_muttng.h index aad4ccc..5d70316 100644 --- a/test/test_muttng.h +++ b/test/test_muttng.h @@ -1,6 +1,6 @@ #ifndef TEST_MUTTNG__H -#define mu_assert(message, test) do { if (!(test)) return message; } while (0) +#define mu_assert(message, test) do { asserts_run++; if (!(test)) return message; } while (0) #define mu_run_test(test) do { char *message = test(); \ tests_run++; \ if (message) { \ @@ -8,5 +8,6 @@ } \ } while (0) extern int tests_run; +extern int asserts_run; #endif -- 2.20.1