Mercurial > hg > th-libs
changeset 254:3d1e2af4e4e6
Start of a very simplistic unit test suite. Initially handling just printf()
style functions.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 16 Feb 2016 21:28:00 +0200 |
parents | d1b4f4ea4715 |
children | 7549e279fe18 |
files | Makefile Makefile.gen tests.c |
diffstat | 3 files changed, 215 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Tue Feb 16 21:28:00 2016 +0200 @@ -0,0 +1,37 @@ +# +# Configuration settings for Linux and generic UNIX +# See other Makefile.* files for more options. +# + +# C-compiler, flags and linker flags +CC ?= gcc +AR ?= ar +RANLIB ?= ranlib +INSTALL ?= install + +CFLAGS += -DHAVE_STRING_H -DHAVE_STDINT_H -DHAVE_CONFIG_H=1 -I. -DTH_USE_INTERNAL_SPRINTF=1 +LDFLAGS += + +#CFLAGS += -DHAVE_STDINT_H +#CFLAGS += -DHAVE_SYS_TYPES_H + +# Miscellaneous +BINPATH=./ +OBJPATH=obj/unix/ +EXEEXT= + + +### +### Stuff +### +ENDIANCHK_BIN=$(BINPATH)endianchk$(EXEEXT) +NOINST_TARGETS += $(ENDIANCHK_BIN) config.h + +include Makefile.gen + + +$(ENDIANCHK_BIN): endianchk.c + $(CC) $(CFLAGS) -o $@ $+ + +config.h: $(ENDIANCHK_BIN) + $(ENDIANCHK_BIN) > $@
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile.gen Tue Feb 16 21:28:00 2016 +0200 @@ -0,0 +1,60 @@ +CFLAGS += -g -W -Wall -Wextra +CFLAGS += -O2 +CFLAGS += -std=c11 -pedantic -D_XOPEN_SOURCE=500 +THLIBS=./ + +MKDIR ?= mkdir +MKDIR_P ?= $(MKDIR) -p + +# +# Objects +# +THLIBS_A=$(OBJPATH)thlibs.a +THLIBS_OBJ=th_util.o th_config.o th_string.o th_ioctx.o th_args.o th_crypto.o + + +TESTS_OBJ=tests.o +TESTS_BIN=$(BINPATH)tests$(EXEEXT) + +TARGETS += $(TESTS_BIN) +NOBUILD_TARGETS += $(OBJPATH) $(BINPATH) +NOINST_TARGETS += $(THLIBS_A) + + +# +# Target rules +# +all: $(NOBUILD_TARGETS) $(NOINST_TARGETS) $(TARGETS) + +$(OBJPATH): + $(MKDIR_P) $@ + +$(BINPATH): + $(MKDIR_P) $@ + +$(OBJPATH)%.o: $(THLIBS)%.c $(THLIBS)%.h + $(CC) $(CFLAGS) -c -o $@ $< + +$(OBJPATH)%.o: %.c %.h + $(CC) $(CFLAGS) -c -o $@ $< -I$(THLIBS) + +$(OBJPATH)%.o: %.c + $(CC) $(CFLAGS) -c -o $@ $< -I$(THLIBS) + + +$(THLIBS_A): $(addprefix $(OBJPATH),$(THLIBS_OBJ)) + $(AR) cru $@ $(addprefix $(OBJPATH),$(THLIBS_OBJ)) + $(RANLIB) $@ + +$(TESTS_BIN): $(addprefix $(OBJPATH),$(TESTS_OBJ)) $(THLIBS_A) $(EXTRAOBJS) + $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) + +# +# Special targets +# +clean: + $(RM) $(TARGETS) $(NOINST_TARGETS) $(OBJPATH)*.o + +srcclean: clean + $(RM) *~ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests.c Tue Feb 16 21:28:00 2016 +0200 @@ -0,0 +1,118 @@ +#include "th_types.h" +#include "th_util.h" +#include "th_string.h" +#include "th_crypto.h" + +#define SET_BUF_SIZE 128 + +int failed, passed; +char buf1[SET_BUF_SIZE+2], buf2[SET_BUF_SIZE+2]; + + +int test_snprintf_do2(size_t len, const char *fmt, va_list ap) +{ + int ret1, ret2, ret = 0; + va_list tmp; + + printf("th_vsnprintf(%" TH_PRIu_SIZE_T ", '%s'): ", len, fmt); + + va_copy(tmp, ap); + + ret1 = th_vsnprintf(buf1, len, fmt, ap); + ret2 = vsnprintf(buf2, len, fmt, tmp); + + if (ret1 != ret2) + { + printf("ret mismatch (%d != %d)\n", ret1, ret2); + ret = -1; + } + + if (strcmp(buf1, buf2)) + { + printf("result mismatch:\n'%s' VS\n'%s'\n", buf1, buf2); + ret = -2; + } + + if (ret == 0) + printf("OK\n"); + return ret; +} + + +void test_snprintf_do(size_t len, const char *fmt, va_list ap) +{ + int ret; + + memset(buf1, 0xe5, SET_BUF_SIZE+2); buf1[SET_BUF_SIZE+1] = 0; + memset(buf2, 0xe5, SET_BUF_SIZE+2); buf2[SET_BUF_SIZE+1] = 0; + + ret = test_snprintf_do2(len, fmt, ap); + + if ((unsigned char) buf1[len] != 0xe5) + { + printf(" buffer #1 overflow, sentinel 0x%02x\n", buf1[len]); + ret = -2; + } + + if ((unsigned char) buf2[len] != 0xe5) + { + printf(" buffer #2 overflow, sentinel 0x%02x\n", buf2[len]); + ret = -2; + } + + if (ret == 0) + passed++; + else + failed++; +} + + +void test_snprintf(const char *fmt, ...) +{ + va_list ap, tmp; + printf("----------------------------------------------\n"); + va_start(ap, fmt); + va_copy(tmp, ap); test_snprintf_do(0, fmt, tmp); + va_copy(tmp, ap); test_snprintf_do(1, fmt, tmp); + va_copy(tmp, ap); test_snprintf_do(2, fmt, tmp); + va_copy(tmp, ap); test_snprintf_do(16, fmt, tmp); + va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, fmt, tmp); + va_end(ap); + printf("----------------------------------------------\n"); +} + + +int main(int argc, char *argv[]) +{ + (void) argc; + (void) argv; + + th_init("th-test", "th-libs unit tests", "0.0.1", NULL, NULL); + th_verbosityLevel = 1; + + if (sizeof(char) != sizeof(unsigned char)) + { + THERR("sizeof(char) != sizeof(unsigned char)???\n"); + return -1; + } + + failed = passed = 0; + + int i_vals[] = { 2, 612342, -2, -612342, 0x1fff, 0x8000000, }; + char *i_fmts[] = { "%d", "%x", "%05d", "%5d", "%-5d", "%05x", "%5x", }; + int i1, i2; + + for (i1 = 0; i1 < sizeof(i_vals) / sizeof(i_vals[0]); i1++) + for (i2 = 0; i2 < sizeof(i_fmts) / sizeof(i_fmts[0]); i2++) + test_snprintf(i_fmts[i2], i_vals); + + char *s_vals[] = { "", "asdf", "xxx yyy zzz ppp fff", NULL, }; + char *s_fmts[] = { "%s", "%2s", "%-2s", "%5s", "%-5s", "%16s", "%-16s", }; + + for (i1 = 0; i1 < sizeof(s_vals) / sizeof(s_vals[0]); i1++) + for (i2 = 0; i2 < sizeof(s_fmts) / sizeof(s_fmts[0]); i2++) + test_snprintf(s_fmts[i2], s_vals); + + printf("%d tests failed, %d passed.\n", failed, passed); + return 0; +}