[Cvs-nserver-commits] CVS: cvs-nserver/lib stringbuf.c,NONE,1.1.2.1 stringbuf.h,NONE,1.1.2.1 check_s
Brought to you by:
tyranny
From: Alexey M. <ty...@us...> - 2002-06-23 12:55:56
|
Update of /cvsroot/cvs-nserver/cvs-nserver/lib In directory usw-pr-cvs1:/tmp/cvs-serv26878 Modified Files: Tag: NCLI-1-11-1 Makefile.am Makefile.in Added Files: Tag: NCLI-1-11-1 stringbuf.c stringbuf.h check_stringbuf.c Log Message: Moved stringbuf here --- NEW FILE: stringbuf.c --- #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "stringbuf.h" #include <assert.h> #ifdef HAVE_STDLIB_H #include <stdlib.h> #endif #ifdef HAVE_STRING_H #include <string.h> #endif struct stringbuf * new_stringbuf (const char *str) { struct stringbuf *sb; size_t allocated; if (str == NULL) return NULL; sb = (struct stringbuf *) malloc(sizeof(struct stringbuf)); if (sb == NULL) return NULL; sb->len = strlen(str); if (sb->len == 0) allocated = 8; else allocated = sb->len * 2; sb->buf = (char *) malloc(allocated); if (sb->buf == NULL) { free(sb); return NULL; } strcpy(sb->buf, str); sb->allocated = allocated; return sb; } void free_stringbuf (struct stringbuf *buf) { if (buf == NULL) return; free(buf->buf); free(buf); } struct stringbuf * dup_stringbuf (struct stringbuf *buf) { struct stringbuf *newbuf; if (buf == NULL) return NULL; newbuf = (struct stringbuf *) malloc(sizeof(struct stringbuf)); if (newbuf == NULL) return NULL; newbuf->len = buf->len; newbuf->allocated = buf->allocated; newbuf->buf = (char *) malloc(buf->allocated); if (newbuf->buf == NULL) { free(newbuf); return NULL; } strcpy(newbuf->buf, buf->buf); return newbuf; } struct stringbuf * catc_stringbuf (struct stringbuf *buf, char c) { if (buf == NULL) return NULL; if (buf->len + 1 >= buf->allocated) { size_t needs_to_allocate = buf->allocated * 2; char *tmp = (char *) realloc(buf->buf, needs_to_allocate); if (tmp == NULL) return NULL; buf->buf = tmp; buf->allocated = needs_to_allocate; } buf->buf[buf->len++] = c; buf->buf[buf->len] = '\0'; return buf; } struct stringbuf * cat_stringbuf (struct stringbuf *buf, const char *str) { size_t len; if (buf == NULL) return NULL; if (str == NULL) return buf; len = strlen(str); if (len == 0) return buf; if (buf->len + len > buf->allocated) { size_t needs_to_allocate = buf->allocated + len * 2 + 1; char *tmp = (char *) realloc(buf->buf, needs_to_allocate); if (tmp == NULL) return NULL; buf->buf = tmp; buf->allocated = needs_to_allocate; } strcpy(buf->buf + buf->len, str); buf->len += len; return buf; } struct stringbuf * shrink_stringbuf (struct stringbuf *buf, size_t newlen) { if (newlen > buf->len) return NULL; buf->len = newlen; buf->buf[buf->len] = '\0'; return buf; } --- NEW FILE: stringbuf.h --- #ifndef STRINGBUF_H #define STRINGBUF_H 1 #ifdef HAVE_CONFIG_H #include <config.h> #endif /* stdlib.h includes stddef.h which in turn defines size_t */ #ifdef HAVE_STDLIB_H #include <stdlib.h> #endif struct stringbuf { char *buf; size_t len; size_t allocated; }; typedef struct stringbuf stringbuf; /*@null@*/ struct stringbuf *new_stringbuf (const char *str); void free_stringbuf (struct stringbuf *buf); /*@null@*/struct stringbuf *dup_stringbuf (struct stringbuf *buf); /*@null@*/struct stringbuf *catc_stringbuf (struct stringbuf *buf, char c); struct stringbuf *cat_stringbuf (struct stringbuf *buf, const char *str); struct stringbuf *shrink_stringbuf (struct stringbuf *buf, size_t newlen); #endif /* STRINGBUF_H */ --- NEW FILE: check_stringbuf.c --- /* 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, 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. Copyright (c) Alexey Mahotkin <al...@hs...> 2001 Test-suite for struct stringbuf */ #include "config.h" #include "stringbuf.h" #include <assert.h> #include <stdlib.h> #ifdef HAVE_STRING_H #include <string.h> #endif static void test_NULL_stringbuf (void) { /* cannot create stringbuf from NULL string */ struct stringbuf *buf; struct stringbuf *buf2; buf = new_stringbuf(NULL); assert(buf == NULL); /* check that duplicating NULL stringbuf works */ buf2 = dup_stringbuf(NULL); assert(buf2 == NULL); /* check that freeing NULL stringbuf works */ free_stringbuf (buf); } static void test_empty_stringbuf (void) { /* check creating new stringbuf from empty string */ struct stringbuf *buf; struct stringbuf *buf2; buf = new_stringbuf(""); assert(buf->buf != NULL); assert(buf->len == 0); assert(buf->allocated > 0); assert(strcmp(buf->buf, "") == 0); /* check duplicating empty stringbuf */ buf2 = dup_stringbuf(buf); assert(buf2->buf != NULL); assert(buf2->len == 0); assert(buf2->allocated > 0); assert(strcmp(buf2->buf, "") == 0); free_stringbuf(buf); /* free the duplicated stringbuf */ free_stringbuf(buf2); } static void test_stringbuf (void) { struct stringbuf *buf; struct stringbuf *buf2; /* check creating new stringbuf */ buf = new_stringbuf("foo"); assert(buf->buf != NULL); assert(strlen(buf->buf) == buf->len); assert(buf->allocated > buf->len); assert(strcmp(buf->buf, "foo") == 0); /* check duplicating stringbuf */ buf2 = dup_stringbuf(buf); assert(buf2 != NULL); assert(buf->len == buf2->len); assert(buf->allocated == buf2->allocated); assert(strcmp(buf->buf, buf2->buf) == 0); assert(strcmp(buf->buf, "foo") == 0); /* check freeing non-empty stringbuf */ free_stringbuf(buf); free_stringbuf(buf2); } static void test_catc_stringbuf (void) { struct stringbuf *buf; struct stringbuf *tmp; unsigned int old_allocated; unsigned int old_len; /* handles catc to NULL stringbuf */ assert(catc_stringbuf(NULL, 'c') == NULL); buf = new_stringbuf(""); /* check adding a single character to empty stringbuf */ tmp = catc_stringbuf(buf, 'c'); assert(tmp == buf); assert(buf->len == 1); assert(strcmp(buf->buf, "c") == 0); assert(buf->allocated > buf->len); /* check adding a single character to non-empty stringbuf */ tmp = catc_stringbuf(buf, 'c'); assert(tmp == buf); assert(buf->len == 2); assert(strcmp(buf->buf, "cc") == 0); assert(buf->allocated > buf->len); /* add characters until there is a need to reallocate */ while (buf->allocated > buf->len + 1) { struct stringbuf *tmp; tmp = catc_stringbuf(buf, 'c'); } /* the reallocation must now happen */ old_allocated = buf->allocated; old_len = buf->len; tmp = catc_stringbuf(buf, 'c'); /* check that reallocation happened */ assert(tmp == buf); assert(buf->allocated > old_allocated); assert(buf->len = old_len + 1); free_stringbuf(buf); } static void test_cat_stringbuf (void) { struct stringbuf *buf; struct stringbuf *tmp; unsigned int old_allocated; unsigned int old_len; /* handles cat to NULL stringbuf */ tmp = cat_stringbuf(NULL, "foo"); assert(tmp == NULL); buf = new_stringbuf(""); /* check catting of NULL string to stringbuf */ tmp = cat_stringbuf(buf, NULL); assert(tmp == buf); assert(buf->len == 0); assert(strcmp(buf->buf, "") == 0); /* check catting of empty string to stringbuf */ tmp = cat_stringbuf(buf, ""); assert(tmp == buf); assert(buf->len == 0); assert(strcmp(buf->buf, "") == 0); /* check catting the string to stringbuf */ tmp = cat_stringbuf(buf, "foo"); assert(tmp == buf); assert(buf->len == 3); assert(buf->allocated > buf->len); assert(strcmp(buf->buf, "foo") == 0); /* add characters until there is a need to reallocate */ while (buf->allocated > buf->len + 1) { struct stringbuf *tmp; tmp = catc_stringbuf(buf, 'c'); } /* check catting the string to stringbuf with reallocation */ old_len = buf->len; old_allocated = buf->allocated; tmp = cat_stringbuf(buf, "bar"); assert(tmp == buf); assert(buf->allocated > old_allocated); assert(buf->len = old_len + 3); free_stringbuf(buf); } static void test_shrink_stringbuf (void) { struct stringbuf *buf; struct stringbuf *tmp; buf = new_stringbuf("foobar"); assert(buf != NULL); /* cannot shrink to larger size */ tmp = shrink_stringbuf(buf, 100); assert(tmp == NULL); tmp = shrink_stringbuf(buf, 3); assert(tmp == buf); assert(buf->len == 3); assert(strcmp(buf->buf, "foo") == 0); } int main(void) { test_NULL_stringbuf(); test_empty_stringbuf(); test_stringbuf(); test_catc_stringbuf(); test_cat_stringbuf(); test_shrink_stringbuf(); exit(0); } Index: Makefile.am =================================================================== RCS file: /cvsroot/cvs-nserver/cvs-nserver/lib/Makefile.am,v retrieving revision 1.1.1.1 retrieving revision 1.1.1.1.4.1 diff -u -d -r1.1.1.1 -r1.1.1.1.4.1 --- Makefile.am 19 May 2001 12:05:14 -0000 1.1.1.1 +++ Makefile.am 23 Jun 2002 12:55:53 -0000 1.1.1.1.4.1 @@ -58,23 +58,11 @@ md5.h \ regex.h \ savecwd.h \ + stringbuf.h stringbuf.c \ system.h \ wait.h \ xselect.h \ xtime.h -## because @LIBOBJS@ is included below, automake automatically knows about -## dup2.c -## fncase.c -## fnmatch.c -## hostname.c -## memmove.c -## mkdir.c -## rename.c -## strstr.c -## strerror.c -## strtoul.c -## valloc.c -## waitpid.c libcvs_a_LIBADD = @LIBOBJS@ EXTRA_DIST = \ @@ -82,6 +70,12 @@ ChangeLog.fsf \ build_lib.com \ xgssapi.h + +TESTS = check_stringbuf + +noinst_PROGRAMS = check_stringbuf + +check_stringbuf_SOURCES = check_stringbuf.c stringbuf.h stringbuf.c # for backwards compatibility with the old makefiles realclean: maintainer-clean Index: Makefile.in =================================================================== RCS file: /cvsroot/cvs-nserver/cvs-nserver/lib/Makefile.in,v retrieving revision 1.1.1.4.2.3.2.5 retrieving revision 1.1.1.4.2.3.2.6 diff -u -d -r1.1.1.4.2.3.2.5 -r1.1.1.4.2.3.2.6 --- Makefile.in 25 Apr 2002 21:41:29 -0000 1.1.1.4.2.3.2.5 +++ Makefile.in 23 Jun 2002 12:55:53 -0000 1.1.1.4.2.3.2.6 @@ -148,6 +148,7 @@ md5.h \ regex.h \ savecwd.h \ + stringbuf.h stringbuf.c \ system.h \ wait.h \ xselect.h \ @@ -161,6 +162,12 @@ build_lib.com \ xgssapi.h + +TESTS = check_stringbuf + +noinst_PROGRAMS = check_stringbuf + +check_stringbuf_SOURCES = check_stringbuf.c stringbuf.h stringbuf.c subdir = lib mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs CONFIG_HEADER = $(top_builddir)/config.h $(top_builddir)/src/options.h @@ -173,8 +180,17 @@ getdate.$(OBJEXT) getline.$(OBJEXT) getopt.$(OBJEXT) \ getopt1.$(OBJEXT) md5.$(OBJEXT) regex.$(OBJEXT) \ savecwd.$(OBJEXT) sighandle.$(OBJEXT) stripslash.$(OBJEXT) \ - xgetwd.$(OBJEXT) yesno.$(OBJEXT) + xgetwd.$(OBJEXT) yesno.$(OBJEXT) stringbuf.$(OBJEXT) libcvs_a_OBJECTS = $(am_libcvs_a_OBJECTS) +noinst_PROGRAMS = check_stringbuf$(EXEEXT) +PROGRAMS = $(noinst_PROGRAMS) + +am_check_stringbuf_OBJECTS = check_stringbuf.$(OBJEXT) \ + stringbuf.$(OBJEXT) +check_stringbuf_OBJECTS = $(am_check_stringbuf_OBJECTS) +check_stringbuf_LDADD = $(LDADD) +check_stringbuf_DEPENDENCIES = +check_stringbuf_LDFLAGS = DEFS = @DEFS@ DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir) -I$(top_builddir)/src @@ -182,7 +198,8 @@ LDFLAGS = @LDFLAGS@ LIBS = @LIBS@ depcomp = $(SHELL) $(top_srcdir)/depcomp -@AMDEP_TRUE@DEP_FILES = $(DEPDIR)/argmatch.Po $(DEPDIR)/dup2.Po \ +@AMDEP_TRUE@DEP_FILES = $(DEPDIR)/argmatch.Po \ +@AMDEP_TRUE@ $(DEPDIR)/check_stringbuf.Po $(DEPDIR)/dup2.Po \ @AMDEP_TRUE@ $(DEPDIR)/fncase.Po $(DEPDIR)/fnmatch.Po \ @AMDEP_TRUE@ $(DEPDIR)/ftruncate.Po $(DEPDIR)/getdate.Po \ @AMDEP_TRUE@ $(DEPDIR)/getline.Po $(DEPDIR)/getopt.Po \ @@ -191,22 +208,23 @@ @AMDEP_TRUE@ $(DEPDIR)/mkdir.Po $(DEPDIR)/regex.Po \ @AMDEP_TRUE@ $(DEPDIR)/rename.Po $(DEPDIR)/savecwd.Po \ @AMDEP_TRUE@ $(DEPDIR)/sighandle.Po $(DEPDIR)/snprintf.Po \ -@AMDEP_TRUE@ $(DEPDIR)/strerror.Po $(DEPDIR)/stripslash.Po \ -@AMDEP_TRUE@ $(DEPDIR)/strstr.Po $(DEPDIR)/strtoul.Po \ -@AMDEP_TRUE@ $(DEPDIR)/valloc.Po $(DEPDIR)/waitpid.Po \ -@AMDEP_TRUE@ $(DEPDIR)/xgetwd.Po $(DEPDIR)/yesno.Po +@AMDEP_TRUE@ $(DEPDIR)/strerror.Po $(DEPDIR)/stringbuf.Po \ +@AMDEP_TRUE@ $(DEPDIR)/stripslash.Po $(DEPDIR)/strstr.Po \ +@AMDEP_TRUE@ $(DEPDIR)/strtoul.Po $(DEPDIR)/valloc.Po \ +@AMDEP_TRUE@ $(DEPDIR)/waitpid.Po $(DEPDIR)/xgetwd.Po \ +@AMDEP_TRUE@ $(DEPDIR)/yesno.Po COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) CCLD = $(CC) LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ CFLAGS = @CFLAGS@ YACCCOMPILE = $(YACC) $(YFLAGS) $(AM_YFLAGS) -DIST_SOURCES = $(libcvs_a_SOURCES) +DIST_SOURCES = $(libcvs_a_SOURCES) $(check_stringbuf_SOURCES) DIST_COMMON = ChangeLog Makefile.am Makefile.in ansi2knr.1 ansi2knr.c \ dup2.c fncase.c fnmatch.c getdate.c hostname.c memmove.c \ mkdir.c rename.c snprintf.c strerror.c strstr.c strtoul.c \ valloc.c waitpid.c -SOURCES = $(libcvs_a_SOURCES) +SOURCES = $(libcvs_a_SOURCES) $(check_stringbuf_SOURCES) all: all-am @@ -229,6 +247,12 @@ $(libcvs_a_AR) libcvs.a $(libcvs_a_OBJECTS) $(libcvs_a_LIBADD) $(RANLIB) libcvs.a +clean-noinstPROGRAMS: + -test -z "$(noinst_PROGRAMS)" || rm -f $(noinst_PROGRAMS) +check_stringbuf$(EXEEXT): $(check_stringbuf_OBJECTS) $(check_stringbuf_DEPENDENCIES) + @rm -f check_stringbuf$(EXEEXT) + $(LINK) $(check_stringbuf_LDFLAGS) $(check_stringbuf_OBJECTS) $(check_stringbuf_LDADD) $(LIBS) + mostlyclean-compile: -rm -f *.$(OBJEXT) core *.core @@ -236,6 +260,7 @@ -rm -f *.tab.c @AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/argmatch.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/check_stringbuf.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/dup2.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/fncase.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/fnmatch.Po@am__quote@ @@ -254,6 +279,7 @@ @AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/sighandle.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/snprintf.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/strerror.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/stringbuf.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/stripslash.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/strstr.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@$(DEPDIR)/strtoul.Po@am__quote@ @@ -321,6 +347,61 @@ distclean-tags: -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH +check-TESTS: $(TESTS) + @failed=0; all=0; xfail=0; xpass=0; \ + srcdir=$(srcdir); export srcdir; \ + list='$(TESTS)'; \ + if test -n "$$list"; then \ + for tst in $$list; do \ + if test -f ./$$tst; then dir=./; \ + elif test -f $$tst; then dir=; \ + else dir="$(srcdir)/"; fi; \ + if $(TESTS_ENVIRONMENT) $${dir}$$tst; then \ + all=`expr $$all + 1`; \ + case " $(XFAIL_TESTS) " in \ + *" $$tst "*) \ + xpass=`expr $$xpass + 1`; \ + failed=`expr $$failed + 1`; \ + echo "XPASS: $$tst"; \ + ;; \ + *) \ + echo "PASS: $$tst"; \ + ;; \ + esac; \ + elif test $$? -ne 77; then \ + all=`expr $$all + 1`; \ + case " $(XFAIL_TESTS) " in \ + *" $$tst "*) \ + xfail=`expr $$xfail + 1`; \ + echo "XFAIL: $$tst"; \ + ;; \ + *) \ + failed=`expr $$failed + 1`; \ + echo "FAIL: $$tst"; \ + ;; \ + esac; \ + fi; \ + done; \ + if test "$$failed" -eq 0; then \ + if test "$$xfail" -eq 0; then \ + banner="All $$all tests passed"; \ + else \ + banner="All $$all tests behaved as expected ($$xfail expected failures)"; \ + fi; \ + else \ + if test "$$xpass" -eq 0; then \ + banner="$$failed of $$all tests failed"; \ + else \ + banner="$$failed of $$all tests did not behave as expected ($$xpass unexpected passes)"; \ + fi; \ + fi; \ + dashes=`echo "$$banner" | sed s/./=/g`; \ + echo "$$dashes"; \ + echo "$$banner"; \ + echo "$$dashes"; \ + test "$$failed" -eq 0; \ + fi + DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) top_distdir = .. @@ -343,8 +424,9 @@ fi; \ done check-am: all-am + $(MAKE) $(AM_MAKEFLAGS) check-TESTS check: check-am -all-am: Makefile $(LIBRARIES) +all-am: Makefile $(LIBRARIES) $(PROGRAMS) installdirs: @@ -373,7 +455,8 @@ @echo "it deletes files that may require special tools to rebuild." clean: clean-am -clean-am: clean-generic clean-noinstLIBRARIES mostlyclean-am +clean-am: clean-generic clean-noinstLIBRARIES clean-noinstPROGRAMS \ + mostlyclean-am distclean: distclean-am @@ -408,16 +491,16 @@ uninstall-am: uninstall-info-am -.PHONY: GTAGS all all-am check check-am clean clean-generic \ - clean-noinstLIBRARIES distclean distclean-compile \ - distclean-depend distclean-generic distclean-tags distdir dvi \ - dvi-am info info-am install install-am install-data \ - install-data-am install-exec install-exec-am install-info \ - install-info-am install-man install-strip installcheck \ - installcheck-am installdirs maintainer-clean \ - maintainer-clean-generic mostlyclean mostlyclean-compile \ - mostlyclean-generic tags uninstall uninstall-am \ - uninstall-info-am +.PHONY: GTAGS all all-am check check-TESTS check-am clean clean-generic \ + clean-noinstLIBRARIES clean-noinstPROGRAMS distclean \ + distclean-compile distclean-depend distclean-generic \ + distclean-tags distdir dvi dvi-am info info-am install \ + install-am install-data install-data-am install-exec \ + install-exec-am install-info install-info-am install-man \ + install-strip installcheck installcheck-am installdirs \ + maintainer-clean maintainer-clean-generic mostlyclean \ + mostlyclean-compile mostlyclean-generic tags uninstall \ + uninstall-am uninstall-info-am # for backwards compatibility with the old makefiles |