From ac914490435f1e070c609c2a2a219a4322e5515e Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 5 Mar 2011 14:54:06 +0000 Subject: Add stuff. --- tests/Makefile | 469 +++++++++++++++++++ tests/Makefile.am | 4 + tests/Makefile.in | 469 +++++++++++++++++++ tests/unittests.c | 1287 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 2229 insertions(+) create mode 100644 tests/Makefile create mode 100644 tests/Makefile.am create mode 100644 tests/Makefile.in create mode 100644 tests/unittests.c (limited to 'tests') diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..a70c1a2 --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,469 @@ +# Makefile.in generated by automake 1.11.1 from Makefile.am. +# tests/Makefile. Generated from Makefile.in by configure. + +# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, +# 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, +# Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + + + +pkgdatadir = $(datadir)/dynstr +pkgincludedir = $(includedir)/dynstr +pkglibdir = $(libdir)/dynstr +pkglibexecdir = $(libexecdir)/dynstr +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = x86_64-unknown-linux-gnu +host_triplet = x86_64-unknown-linux-gnu +check_PROGRAMS = unittests$(EXEEXT) +subdir = tests +DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/libtool.m4 \ + $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \ + $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \ + $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +am_unittests_OBJECTS = unittests.$(OBJEXT) +unittests_OBJECTS = $(am_unittests_OBJECTS) +unittests_DEPENDENCIES = ../lib/libdynstr.la +DEFAULT_INCLUDES = -I. -I$(top_builddir) +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__depfiles_maybe = depfiles +am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ + --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +CCLD = $(CC) +LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ + --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \ + $(LDFLAGS) -o $@ +SOURCES = $(unittests_SOURCES) +DIST_SOURCES = $(unittests_SOURCES) +ETAGS = etags +CTAGS = ctags +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = ${SHELL} /home/liw/dynstr/dynstr/missing --run aclocal-1.11 +AMTAR = ${SHELL} /home/liw/dynstr/dynstr/missing --run tar +AR = ar +AUTOCONF = ${SHELL} /home/liw/dynstr/dynstr/missing --run autoconf +AUTOHEADER = ${SHELL} /home/liw/dynstr/dynstr/missing --run autoheader +AUTOMAKE = ${SHELL} /home/liw/dynstr/dynstr/missing --run automake-1.11 +AWK = gawk +CC = gcc -std=gnu99 +CCDEPMODE = depmode=gcc3 +CFLAGS = -g -O2 +CPP = gcc -E +CPPFLAGS = +CYGPATH_W = echo +DEFS = -DHAVE_CONFIG_H +DEPDIR = .deps +DSYMUTIL = +DUMPBIN = +ECHO_C = +ECHO_N = -n +ECHO_T = +EGREP = /bin/grep -E +EXEEXT = +FGREP = /bin/grep -F +GREP = /bin/grep +INSTALL = /usr/bin/install -c +INSTALL_DATA = ${INSTALL} -m 644 +INSTALL_PROGRAM = ${INSTALL} +INSTALL_SCRIPT = ${INSTALL} +INSTALL_STRIP_PROGRAM = $(install_sh) -c -s +LD = /usr/bin/ld -m elf_x86_64 +LDFLAGS = +LIBOBJS = +LIBS = +LIBTOOL = $(SHELL) $(top_builddir)/libtool +LIPO = +LN_S = ln -s +LTLIBOBJS = +MAKEINFO = ${SHELL} /home/liw/dynstr/dynstr/missing --run makeinfo +MKDIR_P = /bin/mkdir -p +NM = /usr/bin/nm -B +NMEDIT = +OBJDUMP = objdump +OBJEXT = o +OTOOL = +OTOOL64 = +PACKAGE = dynstr +PACKAGE_BUGREPORT = liw@liw.fi +PACKAGE_NAME = dynstr +PACKAGE_STRING = dynstr 0.0 +PACKAGE_TARNAME = dynstr +PACKAGE_URL = +PACKAGE_VERSION = 0.0 +PATH_SEPARATOR = : +RANLIB = ranlib +SED = /bin/sed +SET_MAKE = +SHELL = /bin/bash +STRIP = strip +VERSION = 0.0 +abs_builddir = /home/liw/dynstr/dynstr/tests +abs_srcdir = /home/liw/dynstr/dynstr/tests +abs_top_builddir = /home/liw/dynstr/dynstr +abs_top_srcdir = /home/liw/dynstr/dynstr +ac_ct_CC = gcc +ac_ct_DUMPBIN = +am__include = include +am__leading_dot = . +am__quote = +am__tar = ${AMTAR} chof - "$$tardir" +am__untar = ${AMTAR} xf - +bindir = ${exec_prefix}/bin +build = x86_64-unknown-linux-gnu +build_alias = +build_cpu = x86_64 +build_os = linux-gnu +build_vendor = unknown +builddir = . +datadir = ${datarootdir} +datarootdir = ${prefix}/share +docdir = ${datarootdir}/doc/${PACKAGE_TARNAME} +dvidir = ${docdir} +exec_prefix = ${prefix} +host = x86_64-unknown-linux-gnu +host_alias = +host_cpu = x86_64 +host_os = linux-gnu +host_vendor = unknown +htmldir = ${docdir} +includedir = ${prefix}/include +infodir = ${datarootdir}/info +install_sh = ${SHELL} /home/liw/dynstr/dynstr/install-sh +libdir = ${exec_prefix}/lib +libexecdir = ${exec_prefix}/libexec +localedir = ${datarootdir}/locale +localstatedir = ${prefix}/var +lt_ECHO = echo +mandir = ${datarootdir}/man +mkdir_p = /bin/mkdir -p +oldincludedir = /usr/include +pdfdir = ${docdir} +prefix = /home/liw/test +program_transform_name = s,x,x, +psdir = ${docdir} +sbindir = ${exec_prefix}/sbin +sharedstatedir = ${prefix}/com +srcdir = . +sysconfdir = ${prefix}/etc +target_alias = +top_build_prefix = ../ +top_builddir = .. +top_srcdir = .. +AM_CPPFLAGS = -I $(srcdir)/../lib +unittests_SOURCES = unittests.c +unittests_LDADD = ../lib/libdynstr.la +all: all-am + +.SUFFIXES: +.SUFFIXES: .c .lo .o .obj +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign tests/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign tests/Makefile +.PRECIOUS: Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +clean-checkPROGRAMS: + @list='$(check_PROGRAMS)'; test -n "$$list" || exit 0; \ + echo " rm -f" $$list; \ + rm -f $$list || exit $$?; \ + test -n "$(EXEEXT)" || exit 0; \ + list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \ + echo " rm -f" $$list; \ + rm -f $$list +unittests$(EXEEXT): $(unittests_OBJECTS) $(unittests_DEPENDENCIES) + @rm -f unittests$(EXEEXT) + $(LINK) $(unittests_OBJECTS) $(unittests_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +include ./$(DEPDIR)/unittests.Po + +.c.o: + $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< + $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +# source='$<' object='$@' libtool=no \ +# DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) \ +# $(COMPILE) -c $< + +.c.obj: + $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` + $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +# source='$<' object='$@' libtool=no \ +# DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) \ +# $(COMPILE) -c `$(CYGPATH_W) '$<'` + +.c.lo: + $(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< + $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo +# source='$<' object='$@' libtool=yes \ +# DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) \ +# $(LTCOMPILE) -c -o $@ $< + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + mkid -fID $$unique +tags: TAGS + +TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + set x; \ + here=`pwd`; \ + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: CTAGS +CTAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am + $(MAKE) $(AM_MAKEFLAGS) $(check_PROGRAMS) +check: check-am +all-am: Makefile +installdirs: +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-checkPROGRAMS clean-generic clean-libtool \ + mostlyclean-am + +distclean: distclean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: + +.MAKE: check-am install-am install-strip + +.PHONY: CTAGS GTAGS all all-am check check-am clean \ + clean-checkPROGRAMS clean-generic clean-libtool ctags \ + distclean distclean-compile distclean-generic \ + distclean-libtool distclean-tags distdir dvi dvi-am html \ + html-am info info-am install install-am install-data \ + install-data-am install-dvi install-dvi-am install-exec \ + install-exec-am install-html install-html-am install-info \ + install-info-am install-man install-pdf install-pdf-am \ + install-ps install-ps-am install-strip installcheck \ + installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ + tags uninstall uninstall-am + + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/tests/Makefile.am b/tests/Makefile.am new file mode 100644 index 0000000..f7f4cd2 --- /dev/null +++ b/tests/Makefile.am @@ -0,0 +1,4 @@ +AM_CPPFLAGS = -I $(srcdir)/../lib +check_PROGRAMS = unittests +unittests_SOURCES = unittests.c +unittests_LDADD = ../lib/libdynstr.la diff --git a/tests/Makefile.in b/tests/Makefile.in new file mode 100644 index 0000000..850e5b9 --- /dev/null +++ b/tests/Makefile.in @@ -0,0 +1,469 @@ +# Makefile.in generated by automake 1.11.1 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, +# 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, +# Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ +VPATH = @srcdir@ +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +check_PROGRAMS = unittests$(EXEEXT) +subdir = tests +DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/libtool.m4 \ + $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \ + $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \ + $(top_srcdir)/configure.ac +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +am_unittests_OBJECTS = unittests.$(OBJEXT) +unittests_OBJECTS = $(am_unittests_OBJECTS) +unittests_DEPENDENCIES = ../lib/libdynstr.la +DEFAULT_INCLUDES = -I.@am__isrc@ -I$(top_builddir) +depcomp = $(SHELL) $(top_srcdir)/depcomp +am__depfiles_maybe = depfiles +am__mv = mv -f +COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ + $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +LTCOMPILE = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ + --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ + $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) +CCLD = $(CC) +LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ + --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) \ + $(LDFLAGS) -o $@ +SOURCES = $(unittests_SOURCES) +DIST_SOURCES = $(unittests_SOURCES) +ETAGS = etags +CTAGS = ctags +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +AMTAR = @AMTAR@ +AR = @AR@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +GREP = @GREP@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +LD = @LD@ +LDFLAGS = @LDFLAGS@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LIBTOOL = @LIBTOOL@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LTLIBOBJS = @LTLIBOBJS@ +MAKEINFO = @MAKEINFO@ +MKDIR_P = @MKDIR_P@ +NM = @NM@ +NMEDIT = @NMEDIT@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +OTOOL = @OTOOL@ +OTOOL64 = @OTOOL64@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_URL = @PACKAGE_URL@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +RANLIB = @RANLIB@ +SED = @SED@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +STRIP = @STRIP@ +VERSION = @VERSION@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_CC = @ac_ct_CC@ +ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build = @build@ +build_alias = @build_alias@ +build_cpu = @build_cpu@ +build_os = @build_os@ +build_vendor = @build_vendor@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host = @host@ +host_alias = @host_alias@ +host_cpu = @host_cpu@ +host_os = @host_os@ +host_vendor = @host_vendor@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +localedir = @localedir@ +localstatedir = @localstatedir@ +lt_ECHO = @lt_ECHO@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +sysconfdir = @sysconfdir@ +target_alias = @target_alias@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +AM_CPPFLAGS = -I $(srcdir)/../lib +unittests_SOURCES = unittests.c +unittests_LDADD = ../lib/libdynstr.la +all: all-am + +.SUFFIXES: +.SUFFIXES: .c .lo .o .obj +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign tests/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --foreign tests/Makefile +.PRECIOUS: Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +clean-checkPROGRAMS: + @list='$(check_PROGRAMS)'; test -n "$$list" || exit 0; \ + echo " rm -f" $$list; \ + rm -f $$list || exit $$?; \ + test -n "$(EXEEXT)" || exit 0; \ + list=`for p in $$list; do echo "$$p"; done | sed 's/$(EXEEXT)$$//'`; \ + echo " rm -f" $$list; \ + rm -f $$list +unittests$(EXEEXT): $(unittests_OBJECTS) $(unittests_DEPENDENCIES) + @rm -f unittests$(EXEEXT) + $(LINK) $(unittests_OBJECTS) $(unittests_LDADD) $(LIBS) + +mostlyclean-compile: + -rm -f *.$(OBJEXT) + +distclean-compile: + -rm -f *.tab.c + +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/unittests.Po@am__quote@ + +.c.o: +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(COMPILE) -c $< + +.c.obj: +@am__fastdepCC_TRUE@ $(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'` +@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(COMPILE) -c `$(CYGPATH_W) '$<'` + +.c.lo: +@am__fastdepCC_TRUE@ $(LTCOMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $< +@am__fastdepCC_TRUE@ $(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Plo +@AMDEP_TRUE@@am__fastdepCC_FALSE@ source='$<' object='$@' libtool=yes @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(LTCOMPILE) -c -o $@ $< + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + mkid -fID $$unique +tags: TAGS + +TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + set x; \ + here=`pwd`; \ + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: CTAGS +CTAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am + $(MAKE) $(AM_MAKEFLAGS) $(check_PROGRAMS) +check: check-am +all-am: Makefile +installdirs: +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-checkPROGRAMS clean-generic clean-libtool \ + mostlyclean-am + +distclean: distclean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +distclean-am: clean-am distclean-compile distclean-generic \ + distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -rf ./$(DEPDIR) + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-compile mostlyclean-generic \ + mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: + +.MAKE: check-am install-am install-strip + +.PHONY: CTAGS GTAGS all all-am check check-am clean \ + clean-checkPROGRAMS clean-generic clean-libtool ctags \ + distclean distclean-compile distclean-generic \ + distclean-libtool distclean-tags distdir dvi dvi-am html \ + html-am info info-am install install-am install-data \ + install-data-am install-dvi install-dvi-am install-exec \ + install-exec-am install-html install-html-am install-info \ + install-info-am install-man install-pdf install-pdf-am \ + install-ps install-ps-am install-strip installcheck \ + installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-compile \ + mostlyclean-generic mostlyclean-libtool pdf pdf-am ps ps-am \ + tags uninstall uninstall-am + + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/tests/unittests.c b/tests/unittests.c new file mode 100644 index 0000000..0ba5b33 --- /dev/null +++ b/tests/unittests.c @@ -0,0 +1,1287 @@ +/* + * unittests.c - unit tests for dynamic byte strings + * Copyright 2010 Lars Wirzenius + * + * 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 3 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, see . + */ + + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dynstr.h" + + +#define FAIL(msg) \ + do { \ + printf("FAIL: %s\n", msg); \ + return false; \ + } while (0) + + +#define FAIL_UNLESS(cond) \ + do { \ + if (!(cond)) { \ + printf("FAIL: %s:%d: %s\n", \ + __FUNCTION__, __LINE__, #cond); \ + return false;\ + } \ + } while (0) + + +#define FAIL_UNLESS_EQUAL(a,b) \ + do { \ + if ((a) != (b)) { \ + printf("FAIL: %s:%d: not equal: '%s' and '%s'\n", \ + __FUNCTION__, __LINE__, #a, #b); \ + return false;\ + } \ + } while (0) + + +#define MAX_STRINGS 128 +static Dynstr *strings[MAX_STRINGS]; +static int num_strings; + +static void add(Dynstr *dynstr) +{ + if (num_strings < MAX_STRINGS) { + strings[num_strings] = dynstr; + num_strings++; + } +} + +static Dynstr *new_from_cstring(const char *cstring) +{ + Dynstr *dynstr; + + dynstr = dynstr_new_from_cstring(cstring); + add(dynstr); + return dynstr; +} + +static Dynstr *substr(Dynstr *dynstr, size_t offset, size_t size) +{ + Dynstr *sub; + + sub = dynstr_substr(dynstr, offset, size); + add(sub); + return sub; +} + +static void free_all_strings(void) +{ + while (num_strings > 0) { + num_strings--; + dynstr_free(strings[num_strings]); + } +} + + +static int test_default_error_handler_is_indicate(void) +{ + FAIL_UNLESS_EQUAL(dynstr_get_malloc_error_handler(), + dynstr_malloc_error_indicate); + return true; +} + + +static int test_sets_error_handler(void) +{ + dynstr_set_malloc_error_handler(dynstr_malloc_error_abort); + FAIL_UNLESS_EQUAL(dynstr_get_malloc_error_handler(), + dynstr_malloc_error_abort); + return true; +} + + +static int test_init_resets_error_handler(void) +{ + dynstr_set_malloc_error_handler(dynstr_malloc_error_abort); + dynstr_init(); + FAIL_UNLESS_EQUAL(dynstr_get_malloc_error_handler(), + dynstr_malloc_error_indicate); + return true; +} + + +static int test_indicate_handler_returns(void) +{ + dynstr_malloc_error_indicate(0, 0, NULL); + return true; +} + + +static jmp_buf env; +static void abort_handler(int signo) +{ + longjmp(env, 1); +} + + +static int test_abort_handler_calls_abort(void) +{ + struct sigaction act; + struct sigaction oldact; + + if (setjmp(env) == 0) { + act.sa_handler = abort_handler; + sigemptyset(&act.sa_mask); + act.sa_flags = 0; + sigaction(SIGABRT, &act, &oldact); + dynstr_set_malloc_error_handler(dynstr_malloc_error_abort); + dynstr_malloc_error_abort(0, 0, 0); + FAIL("dynstr_malloc_error_abort returned"); + } else { + sigaction(SIGABRT, &oldact, NULL); + } + return true; +} + + +static bool error_handler_called; + +static void error_handler(int error, size_t size, void *oldptr) +{ + error_handler_called = true; +} + +static int fail_malloc_after; + +static void *fail_malloc(size_t size) +{ + if (fail_malloc_after <= 0) + return NULL; + --fail_malloc_after; + return malloc(size); +} + +static int test_alloc_calls_error_handler(void) +{ + Dynstr *dynstr; + + error_handler_called = false; + dynstr_set_malloc_error_handler(error_handler); + dynstr_set_malloc(fail_malloc); + dynstr = dynstr_new_empty(); + FAIL_UNLESS_EQUAL(dynstr, NULL); + FAIL_UNLESS_EQUAL(error_handler_called, true); + return true; +} + +static int test_alloc_does_not_call_error_handler(void) +{ + Dynstr *dynstr; + + error_handler_called = false; + dynstr_set_malloc_error_handler(error_handler); + dynstr_set_malloc(malloc); + dynstr = dynstr_new_empty(); + FAIL_UNLESS_EQUAL(error_handler_called, false); + dynstr_free(dynstr); + return true; +} + + +static int test_uses_malloc_by_default(void) +{ + dynstr_init(); + FAIL_UNLESS_EQUAL(dynstr_get_malloc(), malloc); + return true; +} + + +static int test_sets_malloc(void) +{ + dynstr_set_malloc(fail_malloc); + FAIL_UNLESS_EQUAL(dynstr_get_malloc(), fail_malloc); + return true; +} + + +static int test_init_resets_malloc(void) +{ + dynstr_set_malloc(fail_malloc); + dynstr_init(); + FAIL_UNLESS_EQUAL(dynstr_get_malloc(), malloc); + return true; +} + + +static int test_empty_string_is_empty(void) +{ + Dynstr *dynstr; + + dynstr = dynstr_new_empty(); + FAIL_UNLESS_EQUAL(dynstr_len(dynstr), 0); + FAIL_UNLESS_EQUAL(dynstr_is_empty(dynstr), true); + dynstr_free(dynstr); + return true; +} + + +static int test_new_returns_NULL_upon_first_allocation_failure(void) +{ + Dynstr *dynstr; + + dynstr_init(); + dynstr_set_malloc(fail_malloc); + dynstr = dynstr_new_empty(); + FAIL_UNLESS_EQUAL(dynstr, NULL); + return true; +} + + +static int test_creates_from_cstring(void) +{ + const char bytes[] = "asdfasdfafdasdfasdfqw4tb"; + char newbytes[sizeof(bytes)]; + Dynstr *dynstr; + size_t size; + + dynstr = new_from_cstring(bytes); + FAIL_UNLESS_EQUAL(dynstr_len(dynstr), strlen(bytes)); + + size = dynstr_memcpy(newbytes, dynstr, 0, strlen(bytes)); + FAIL_UNLESS_EQUAL(size, strlen(bytes)); + FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, strlen(bytes)), 0); + + return true; +} + + +static int test_creates_from_memory(void) +{ + const char bytes[] = "asdfasdfafdasdfasdfqw4tb"; + char newbytes[sizeof(bytes)]; + Dynstr *dynstr; + size_t size; + + dynstr = dynstr_new_from_memory(bytes, sizeof(bytes)); + FAIL_UNLESS_EQUAL(dynstr_len(dynstr), sizeof(bytes)); + + size = dynstr_memcpy(newbytes, dynstr, 0, sizeof(bytes)); + FAIL_UNLESS_EQUAL(size, sizeof(bytes)); + FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, sizeof(bytes)), 0); + + dynstr_free(dynstr); + return true; +} + + +static int test_creates_from_constant_cstring(void) +{ + char bytes[] = "asdfasdfafdasdfasdfqw4tb"; + char newbytes[sizeof(bytes)]; + Dynstr *dynstr; + size_t size; + + dynstr = dynstr_new_from_constant_cstring(bytes); + FAIL_UNLESS_EQUAL(dynstr_len(dynstr), strlen(bytes)); + + size = dynstr_memcpy(newbytes, dynstr, 0, strlen(bytes)); + FAIL_UNLESS_EQUAL(size, strlen(bytes)); + FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, strlen(bytes)), 0); + + bytes[0] = 'x'; + dynstr_memcpy(newbytes, dynstr, 0, strlen(bytes)); + FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, strlen(bytes)), 0); + + dynstr_free(dynstr); + return true; +} + + +static int test_creates_from_constant_memory(void) +{ + char bytes[] = "asdfasdfafdasdfasdfqw4tb"; + char newbytes[sizeof(bytes)]; + Dynstr *dynstr; + size_t size; + + dynstr = dynstr_new_from_constant_memory(bytes, sizeof(bytes)); + FAIL_UNLESS_EQUAL(dynstr_len(dynstr), sizeof(bytes)); + + size = dynstr_memcpy(newbytes, dynstr, 0, sizeof(bytes)); + FAIL_UNLESS_EQUAL(size, sizeof(bytes)); + FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, sizeof(bytes)), 0); + + bytes[0] = 'x'; + dynstr_memcpy(newbytes, dynstr, 0, sizeof(bytes)); + FAIL_UNLESS_EQUAL(memcmp(bytes, newbytes, sizeof(bytes)), 0); + + dynstr_free(dynstr); + return true; +} + + +static int test_memcpy_returns_zero_if_offset_is_too_large(void) +{ + Dynstr *dynstr; + char mem[1024]; + + dynstr = new_from_cstring("hello"); + FAIL_UNLESS_EQUAL(dynstr_memcpy(mem, dynstr, dynstr_len(dynstr), 1), 0); + FAIL_UNLESS_EQUAL(dynstr_memcpy(mem, dynstr, dynstr_len(dynstr) + 1, 1), 0); + return true; +} + + +static int test_memcpy_truncates_if_copying_too_much(void) +{ + Dynstr *dynstr; + char mem[1024]; + size_t len; + + dynstr = new_from_cstring("hello"); + len = dynstr_len(dynstr); + FAIL_UNLESS_EQUAL(dynstr_memcpy(mem, dynstr, 0, len), len); + FAIL_UNLESS_EQUAL(dynstr_memcpy(mem, dynstr, 0, len + 1), len); + return true; +} + + +static int test_memcpy_copies_whole_string_ok(void) +{ + Dynstr *dynstr; + const char data[] = "hello"; + char mem[1024]; + size_t len; + + dynstr = new_from_cstring(data); + len = dynstr_len(dynstr); + dynstr_memcpy(mem, dynstr, 0, len); + FAIL_UNLESS_EQUAL(memcmp(data, mem, len), 0); + return true; +} + + +static int test_memcpy_copies_substring_ok(void) +{ + Dynstr *dynstr; + const char data[] = "hello"; + char mem[1024]; + size_t len; + + dynstr = new_from_cstring(data); + len = dynstr_len(dynstr); + dynstr_memcpy(mem, dynstr, 1, len-2); + FAIL_UNLESS_EQUAL(memcmp(data + 1, mem, len-2), 0); + return true; +} + + +static int test_copies_empty_substring_ok(void) +{ + Dynstr *dynstr; + Dynstr *sub; + size_t size; + char buf[1024]; + + dynstr = new_from_cstring("abcdef"); + sub = substr(dynstr, 1, 0); + size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); + FAIL_UNLESS_EQUAL(size, 0); + return true; +} + + +static int test_copies_single_byte_substring_ok(void) +{ + Dynstr *dynstr; + Dynstr *sub; + size_t size; + char buf[1024]; + + dynstr = new_from_cstring("abcdef"); + sub = substr(dynstr, 1, 1); + size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); + FAIL_UNLESS_EQUAL(size, 1); + FAIL_UNLESS_EQUAL(memcmp(buf, "b", 1), 0); + return true; +} + + +static int test_copies_middle_substring_ok(void) +{ + Dynstr *dynstr; + Dynstr *sub; + size_t size; + char buf[1024]; + + dynstr = new_from_cstring("abcdef"); + sub = substr(dynstr, 1, 4); + size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); + FAIL_UNLESS_EQUAL(size, 4); + FAIL_UNLESS_EQUAL(memcmp(buf, "bcde", 4), 0); + return true; +} + + +static int test_copies_initial_substring_ok(void) +{ + Dynstr *dynstr; + Dynstr *sub; + size_t size; + char buf[1024]; + + dynstr = new_from_cstring("abcdef"); + sub = substr(dynstr, 0, 5); + size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); + FAIL_UNLESS_EQUAL(size, 5); + FAIL_UNLESS_EQUAL(memcmp(buf, "abcde", 5), 0); + return true; +} + + +static int test_copies_final_substring_ok(void) +{ + Dynstr *dynstr; + Dynstr *sub; + size_t size; + char buf[1024]; + + dynstr = new_from_cstring("abcdef"); + sub = substr(dynstr, 1, 5); + size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); + FAIL_UNLESS_EQUAL(size, 5); + FAIL_UNLESS_EQUAL(memcmp(buf, "bcdef", 5), 0); + return true; +} + + +static int test_copies_empty_substring_when_offset_too_big(void) +{ + Dynstr *dynstr; + Dynstr *sub; + size_t size; + char buf[1024]; + + dynstr = new_from_cstring("abcdef"); + sub = substr(dynstr, 100, 1); + size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); + FAIL_UNLESS_EQUAL(size, 0); + return true; +} + + +static int test_copies_partial_substring_when_length_too_big(void) +{ + Dynstr *dynstr; + Dynstr *sub; + size_t size; + char buf[1024]; + + dynstr = new_from_cstring("abcdef"); + sub = substr(dynstr, 1, 6); + size = dynstr_memcpy(buf, sub, 0, dynstr_len(sub)); + FAIL_UNLESS_EQUAL(size, 5); + FAIL_UNLESS_EQUAL(memcmp(buf, "bcdef", 5), 0); + return true; +} + + +static int test_strdup_duplicates_string(void) +{ + Dynstr *dynstr; + char *cstring; + char data[] = "hello, world"; + + dynstr = dynstr_new_from_constant_cstring(data); + cstring = dynstr_strdup(dynstr); + FAIL_UNLESS_EQUAL(memcmp(data, cstring, sizeof(data)), 0); + free(cstring); + dynstr_free(dynstr); + return true; +} + + +static int test_cats_ok(void) +{ + Dynstr *a; + Dynstr *b; + Dynstr *c; + char buf[1024]; + size_t size; + + a = new_from_cstring("abc"); + b = new_from_cstring("def"); + c = dynstr_cat(a, b); + FAIL_UNLESS_EQUAL(dynstr_len(c), 6); + size = dynstr_memcpy(buf, c, 0, dynstr_len(c)); + FAIL_UNLESS_EQUAL(size, 6); + FAIL_UNLESS_EQUAL(memcmp(buf, "abcdef", 6), 0); + dynstr_free(c); + return true; +} + + +static int test_cat_returns_NULL_for_first_malloc_failure(void) +{ + Dynstr *a; + Dynstr *b; + Dynstr *c; + + a = new_from_cstring("abc"); + b = new_from_cstring("def"); + dynstr_set_malloc(fail_malloc); + c = dynstr_cat(a, b); + FAIL_UNLESS_EQUAL(c, NULL); + return true; +} + + +static int test_byteat_reports_correct_character(void) +{ + Dynstr *dynstr; + + dynstr = new_from_cstring("abc"); + FAIL_UNLESS_EQUAL(dynstr_byte_at(dynstr, 1), 'b'); + return true; +} + + +static int test_byteat_reports_error_for_too_large_offset(void) +{ + Dynstr *dynstr; + + dynstr = new_from_cstring("abc"); + FAIL_UNLESS_EQUAL(dynstr_byte_at(dynstr, 1024), -1); + return true; +} + + +static int test_byteat_reports_0xff_correctly(void) +{ + Dynstr *dynstr; + + dynstr = new_from_cstring("\xff"); + FAIL_UNLESS_EQUAL(dynstr_byte_at(dynstr, 0), 255); + return true; +} + + +static int test_first_byte_finds_byte(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = new_from_cstring("123456"); + offset = dynstr_first_byte(dynstr, 0, '3'); + FAIL_UNLESS_EQUAL(offset, 2); + return true; +} + + +static int test_first_byte_only_finds_first_byte(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = new_from_cstring("123123"); + offset = dynstr_first_byte(dynstr, 0, '3'); + FAIL_UNLESS_EQUAL(offset, 2); + return true; +} + + +static int test_first_byte_does_not_find_nonexistent_byte(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = new_from_cstring("123456"); + offset = dynstr_first_byte(dynstr, 0, 'x'); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + return true; +} + + +static int test_first_byte_does_not_find_byte_outside_range(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = new_from_cstring("123456"); + offset = dynstr_first_byte(dynstr, 4, '3'); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + return true; +} + + +static int test_first_byte_only_finds_byte_inside_range(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = new_from_cstring("123123"); + offset = dynstr_first_byte(dynstr, 3, '3'); + FAIL_UNLESS_EQUAL(offset, 5); + return true; +} + + +static int test_first_byte_does_not_find_in_range_outside_string(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = new_from_cstring("123123"); + offset = dynstr_first_byte(dynstr, 128, '3'); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + return true; +} + + +static int test_last_byte_finds_byte(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = new_from_cstring("123456"); + offset = dynstr_last_byte(dynstr, 0, '3'); + FAIL_UNLESS_EQUAL(offset, 2); + return true; +} + + +static int test_last_byte_only_finds_last_byte(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = new_from_cstring("123123"); + offset = dynstr_last_byte(dynstr, 0, '3'); + FAIL_UNLESS_EQUAL(offset, 5); + return true; +} + + +static int test_last_byte_does_not_find_nonexistent_byte(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = new_from_cstring("123456"); + offset = dynstr_last_byte(dynstr, 0, 'x'); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + return true; +} + + +static int test_last_byte_does_not_find_byte_outside_range(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = new_from_cstring("123456"); + offset = dynstr_last_byte(dynstr, 4, '3'); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + return true; +} + + +static int test_last_byte_only_finds_byte_inside_range(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = new_from_cstring("123123"); + offset = dynstr_last_byte(dynstr, 3, '3'); + FAIL_UNLESS_EQUAL(offset, 5); + return true; +} + + +static int test_last_byte_does_not_find_in_range_outside_string(void) +{ + Dynstr *dynstr; + size_t offset; + + dynstr = new_from_cstring("123123"); + offset = dynstr_last_byte(dynstr, 128, '3'); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + return true; +} + + +static int test_first_string_finds_pattern(void) +{ + Dynstr *dynstr; + Dynstr *pattern; + size_t offset; + + dynstr = new_from_cstring("123456"); + pattern = new_from_cstring("345"); + offset = dynstr_first_string(dynstr, 0, pattern); + FAIL_UNLESS_EQUAL(offset, 2); + return true; +} + + +static int test_first_string_only_finds_first_pattern(void) +{ + Dynstr *dynstr; + Dynstr *pattern; + size_t offset; + + dynstr = new_from_cstring("123123"); + pattern = new_from_cstring("123"); + offset = dynstr_first_string(dynstr, 0, pattern); + FAIL_UNLESS_EQUAL(offset, 0); + return true; +} + + +static int test_first_string_does_not_find_nonexistent_pattern(void) +{ + Dynstr *dynstr; + Dynstr *pattern; + size_t offset; + + dynstr = new_from_cstring("123456"); + pattern = new_from_cstring("x"); + offset = dynstr_first_string(dynstr, 0, pattern); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + return true; +} + + +static int test_first_string_does_not_find_pattern_outside_range(void) +{ + Dynstr *dynstr; + Dynstr *pattern; + size_t offset; + + dynstr = new_from_cstring("123456"); + pattern = new_from_cstring("123"); + offset = dynstr_first_string(dynstr, 3, pattern); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + return true; +} + + +static int test_first_string_only_finds_pattern_inside_range(void) +{ + Dynstr *dynstr; + Dynstr *pattern; + size_t offset; + + dynstr = new_from_cstring("123123"); + pattern = new_from_cstring("3"); + offset = dynstr_first_string(dynstr, 3, pattern); + FAIL_UNLESS_EQUAL(offset, 5); + return true; +} + + +static int test_first_string_does_not_find_empty_pattern(void) +{ + Dynstr *dynstr; + Dynstr *pattern; + size_t offset; + + dynstr = new_from_cstring("123123"); + pattern = new_from_cstring(""); + offset = dynstr_first_string(dynstr, 0, pattern); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + return true; +} + + +static int test_last_string_finds_pattern(void) +{ + Dynstr *dynstr; + Dynstr *pattern; + size_t offset; + + dynstr = new_from_cstring("123456"); + pattern = new_from_cstring("345"); + offset = dynstr_last_string(dynstr, 0, pattern); + FAIL_UNLESS_EQUAL(offset, 2); + return true; +} + + +static int test_last_string_only_finds_last_pattern(void) +{ + Dynstr *dynstr; + Dynstr *pattern; + size_t offset; + + dynstr = new_from_cstring("123123"); + pattern = new_from_cstring("123"); + offset = dynstr_last_string(dynstr, 0, pattern); + FAIL_UNLESS_EQUAL(offset, 3); + return true; +} + + +static int test_last_string_does_not_find_nonexistent_pattern(void) +{ + Dynstr *dynstr; + Dynstr *pattern; + size_t offset; + + dynstr = new_from_cstring("123456"); + pattern = new_from_cstring("x"); + offset = dynstr_last_string(dynstr, 0, pattern); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + return true; +} + + +static int test_last_string_does_not_find_pattern_outside_range(void) +{ + Dynstr *dynstr; + Dynstr *pattern; + size_t offset; + + dynstr = new_from_cstring("123456"); + pattern = new_from_cstring("123"); + offset = dynstr_last_string(dynstr, 3, pattern); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + return true; +} + + +static int test_last_string_only_finds_pattern_inside_range(void) +{ + Dynstr *dynstr; + Dynstr *pattern; + size_t offset; + + dynstr = new_from_cstring("123123"); + pattern = new_from_cstring("3"); + offset = dynstr_last_string(dynstr, 3, pattern); + FAIL_UNLESS_EQUAL(offset, 5); + return true; +} + + +static int test_last_string_does_not_find_empty_pattern(void) +{ + Dynstr *dynstr; + Dynstr *pattern; + size_t offset; + + dynstr = new_from_cstring("123123"); + pattern = new_from_cstring(""); + offset = dynstr_last_string(dynstr, 0, pattern); + FAIL_UNLESS_EQUAL(offset, DYNSTR_NOT_FOUND); + return true; +} + +static int test_cmp_returns_0_for_equal_strings(void) +{ + Dynstr *a; + Dynstr *b; + + a = new_from_cstring("foo"); + b = new_from_cstring("foo"); + FAIL_UNLESS_EQUAL(dynstr_cmp(a, b), 0); + return true; +} + + +static int test_cmp_returns_0_for_empty_strings(void) +{ + Dynstr *a; + Dynstr *b; + + a = dynstr_new_empty(); + b = dynstr_new_empty(); + FAIL_UNLESS_EQUAL(dynstr_cmp(a, b), 0); + dynstr_free(a); + dynstr_free(b); + return true; +} + + +static int test_cmp_returns_negative_if_only_first_string_is_empty(void) +{ + Dynstr *a; + Dynstr *b; + + a = new_from_cstring(""); + b = new_from_cstring("foo"); + FAIL_UNLESS(dynstr_cmp(a, b) < 0); + return true; +} + + +static int test_cmp_returns_positive_if_only_second_string_is_empty(void) +{ + Dynstr *a; + Dynstr *b; + + a = new_from_cstring("foo"); + b = new_from_cstring(""); + FAIL_UNLESS(dynstr_cmp(a, b) > 0); + return true; +} + + +static int test_cmp_returns_negative_if_first_string_comes_first(void) +{ + Dynstr *a; + Dynstr *b; + + a = new_from_cstring("x"); + b = new_from_cstring("\xff"); /* Check for unsigned comparision, too. */ + FAIL_UNLESS(dynstr_cmp(a, b) < 0); + return true; +} + + +static int test_cmp_returns_positive_if_second_string_comes_first(void) +{ + Dynstr *a; + Dynstr *b; + + a = new_from_cstring("\xff"); + b = new_from_cstring("x"); + FAIL_UNLESS(dynstr_cmp(a, b) > 0); + return true; +} + + +static int write_test(long (*callback)(char *filename, Dynstr *dynstr)) +{ + char tempname[] = "unittest.tmp.XXXXXX"; + int fd; + size_t num_bytes; + Dynstr *dynstr; + Dynstr *dynstr2; + char buf[1024]; + int read_bytes; + + dynstr = new_from_cstring("life is too short for str* and mem* in apps"); + + fd = mkstemp(tempname); + if (fd == -1) + abort(); + + num_bytes = callback(tempname, dynstr); + + FAIL_UNLESS_EQUAL(num_bytes, dynstr_len(dynstr)); + + read_bytes = read(fd, buf, sizeof(buf)); + if (read_bytes == -1) + abort(); + close(fd); + remove(tempname); + + dynstr2 = dynstr_new_from_memory(buf, read_bytes); + FAIL_UNLESS_EQUAL(read_bytes, dynstr_len(dynstr)); + FAIL_UNLESS_EQUAL(dynstr_cmp(dynstr, dynstr2), 0); + dynstr_free(dynstr2); + + return true; +} + + +static long fwrite_callback(char *filename, Dynstr *dynstr) +{ + FILE *f; + long num_bytes; + + f = fopen(filename, "w"); + num_bytes = dynstr_fwrite(f, dynstr); + fclose(f); + return num_bytes; +} + + +static int test_fwrite_writes_string(void) +{ + return write_test(fwrite_callback); +} + + +static long write_callback(char *filename, Dynstr *dynstr) +{ + int fd; + long num_bytes; + + fd = open(filename, O_WRONLY, 0); + num_bytes = dynstr_write(fd, dynstr); + close(fd); + return num_bytes; +} + + +static int test_write_writes_string(void) +{ + return write_test(write_callback); +} + + +static int read_test(Dynstr *(*callback)(char *filename, size_t size)) +{ + char tempname[] = "unittest.tmp.XXXXXX"; + int fd; + Dynstr *dynstr; + Dynstr *dynstr2; + const char canonical[] = "life is too short for str* and mem* in apps"; + + fd = mkstemp(tempname); + if (fd == -1) + abort(); + if (write(fd, canonical, sizeof(canonical)) == -1) + abort(); + close(fd); + + dynstr = callback(tempname, sizeof(canonical) * 2); + remove(tempname); + + FAIL_UNLESS(dynstr != NULL); + FAIL_UNLESS_EQUAL(dynstr_len(dynstr), sizeof(canonical)); + + dynstr2 = dynstr_new_from_memory(canonical, sizeof(canonical)); + FAIL_UNLESS_EQUAL(dynstr_cmp(dynstr, dynstr2), 0); + + dynstr_free(dynstr); + dynstr_free(dynstr2); + + return true; +} + + +static Dynstr *fread_callback(char *filename, size_t size) +{ + FILE *f; + Dynstr *dynstr; + + f = fopen(filename, "r"); + dynstr = dynstr_fread(f, size); + fclose(f); + return dynstr; +} + + +static int test_fread_reads_string(void) +{ + return read_test(fread_callback); +} + + +static Dynstr *read_callback(char *filename, size_t size) +{ + int fd; + Dynstr *dynstr; + + fd = open(filename, O_RDONLY, 0); + dynstr = dynstr_read(fd, size); + close(fd); + return dynstr; +} + + +static int test_read_reads_string(void) +{ + return read_test(read_callback); +} + + +static int readline_test(Dynstr *(*callback)(char *filename)) +{ + char tempname[] = "unittest.tmp.XXXXXX"; + int fd; + const char cline1[] = "life is too short for str* and mem* in apps\n"; + const char cline2[] = "use dynstr instead!\n"; + Dynstr *lines; + Dynstr *line1; + Dynstr *line2; + Dynstr *result; + + fd = mkstemp(tempname); + if (fd == -1) + abort(); + + line1 = dynstr_new_from_constant_cstring(cline1); + line2 = dynstr_new_from_constant_cstring(cline2); + lines = dynstr_cat(line1, line2); + if (dynstr_write(fd, lines) != dynstr_len(lines)) + abort(); + close(fd); + + result = callback(tempname); + remove(tempname); + + FAIL_UNLESS_EQUAL(dynstr_cmp(line1, result), 0); + + dynstr_free(line1); + dynstr_free(line2); + dynstr_free(lines); + dynstr_free(result); + + return true; +} + + +static Dynstr *freadline_callback(char *filename) +{ + FILE *f; + Dynstr *line1; + + f = fopen(filename, "r"); + line1 = dynstr_freadline(f); + fclose(f); + return line1; +} + + +static int test_freadline_reads_first_line(void) +{ + return readline_test(freadline_callback); +} + + +static Dynstr *readline_callback(char *filename) +{ + int fd; + Dynstr *line1; + + fd = open(filename, O_RDONLY, 0); + line1 = dynstr_readline(fd); + close(fd); + return line1; +} + + +static int test_readline_reads_first_line(void) +{ + return readline_test(readline_callback); +} + + +static void setup(void) +{ + dynstr_init(); + fail_malloc_after = 0; +} + + +static void teardown(void) +{ + free_all_strings(); +} + + +struct test { + const char *name; + int (*test)(void); +}; + + +#define TEST(func) { #func, func } + +static const struct test tests[] = { + TEST(test_default_error_handler_is_indicate), + TEST(test_sets_error_handler), + TEST(test_init_resets_error_handler), + TEST(test_uses_malloc_by_default), + TEST(test_sets_malloc), + TEST(test_init_resets_malloc), + TEST(test_alloc_calls_error_handler), + TEST(test_alloc_does_not_call_error_handler), + TEST(test_indicate_handler_returns), + TEST(test_abort_handler_calls_abort), + TEST(test_empty_string_is_empty), + TEST(test_new_returns_NULL_upon_first_allocation_failure), + TEST(test_creates_from_cstring), + TEST(test_creates_from_memory), + TEST(test_creates_from_constant_cstring), + TEST(test_creates_from_constant_memory), + TEST(test_memcpy_returns_zero_if_offset_is_too_large), + TEST(test_memcpy_truncates_if_copying_too_much), + TEST(test_memcpy_copies_whole_string_ok), + TEST(test_memcpy_copies_substring_ok), + TEST(test_strdup_duplicates_string), + TEST(test_copies_empty_substring_ok), + TEST(test_copies_single_byte_substring_ok), + TEST(test_copies_middle_substring_ok), + TEST(test_copies_initial_substring_ok), + TEST(test_copies_final_substring_ok), + TEST(test_copies_empty_substring_when_offset_too_big), + TEST(test_copies_partial_substring_when_length_too_big), + TEST(test_cats_ok), + TEST(test_cat_returns_NULL_for_first_malloc_failure), + TEST(test_byteat_reports_correct_character), + TEST(test_byteat_reports_error_for_too_large_offset), + TEST(test_byteat_reports_0xff_correctly), + TEST(test_first_byte_finds_byte), + TEST(test_first_byte_only_finds_first_byte), + TEST(test_first_byte_does_not_find_nonexistent_byte), + TEST(test_first_byte_does_not_find_byte_outside_range), + TEST(test_first_byte_only_finds_byte_inside_range), + TEST(test_first_byte_does_not_find_in_range_outside_string), + TEST(test_last_byte_finds_byte), + TEST(test_last_byte_only_finds_last_byte), + TEST(test_last_byte_does_not_find_nonexistent_byte), + TEST(test_last_byte_does_not_find_byte_outside_range), + TEST(test_last_byte_only_finds_byte_inside_range), + TEST(test_last_byte_does_not_find_in_range_outside_string), + TEST(test_first_string_finds_pattern), + TEST(test_first_string_only_finds_first_pattern), + TEST(test_first_string_does_not_find_nonexistent_pattern), + TEST(test_first_string_does_not_find_pattern_outside_range), + TEST(test_first_string_only_finds_pattern_inside_range), + TEST(test_first_string_does_not_find_empty_pattern), + TEST(test_last_string_finds_pattern), + TEST(test_last_string_only_finds_last_pattern), + TEST(test_last_string_does_not_find_nonexistent_pattern), + TEST(test_last_string_does_not_find_pattern_outside_range), + TEST(test_last_string_only_finds_pattern_inside_range), + TEST(test_last_string_does_not_find_empty_pattern), + TEST(test_cmp_returns_0_for_equal_strings), + TEST(test_cmp_returns_0_for_empty_strings), + TEST(test_cmp_returns_negative_if_only_first_string_is_empty), + TEST(test_cmp_returns_positive_if_only_second_string_is_empty), + TEST(test_cmp_returns_negative_if_first_string_comes_first), + TEST(test_cmp_returns_positive_if_second_string_comes_first), + TEST(test_fwrite_writes_string), + TEST(test_write_writes_string), + TEST(test_fread_reads_string), + TEST(test_read_reads_string), + TEST(test_freadline_reads_first_line), + TEST(test_readline_reads_first_line), +}; +static const int num_tests = sizeof(tests) / sizeof(tests[0]); + +int main(void) +{ + int exit; + + exit = EXIT_SUCCESS; + for (int i = 0; i < num_tests; ++i) { + setup(); + if (!tests[i].test()) { + exit = EXIT_FAILURE; + } + teardown(); + } + return exit; +} -- cgit v1.2.1