From owner-svn-src-head@FreeBSD.ORG Sat Feb 28 06:39:40 2009 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 5A9E61065672; Sat, 28 Feb 2009 06:39:40 +0000 (UTC) (envelope-from das@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 3F23F8FC0A; Sat, 28 Feb 2009 06:39:40 +0000 (UTC) (envelope-from das@FreeBSD.org) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id n1S6dedK098095; Sat, 28 Feb 2009 06:39:40 GMT (envelope-from das@svn.freebsd.org) Received: (from das@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n1S6deV1098093; Sat, 28 Feb 2009 06:39:40 GMT (envelope-from das@svn.freebsd.org) Message-Id: <200902280639.n1S6deV1098093@svn.freebsd.org> From: David Schultz Date: Sat, 28 Feb 2009 06:39:40 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r189142 - head/tools/regression/lib/libc/stdio X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 28 Feb 2009 06:39:40 -0000 Author: das Date: Sat Feb 28 06:39:39 2009 New Revision: 189142 URL: http://svn.freebsd.org/changeset/base/189142 Log: Tests for getdelim(). Added: head/tools/regression/lib/libc/stdio/test-getdelim.c (contents, props changed) Modified: head/tools/regression/lib/libc/stdio/Makefile Modified: head/tools/regression/lib/libc/stdio/Makefile ============================================================================== --- head/tools/regression/lib/libc/stdio/Makefile Sat Feb 28 06:37:10 2009 (r189141) +++ head/tools/regression/lib/libc/stdio/Makefile Sat Feb 28 06:39:39 2009 (r189142) @@ -1,6 +1,6 @@ # $FreeBSD$ -TESTS= test-perror test-print-positional test-printbasic test-printfloat test-scanfloat +TESTS= test-getdelim test-perror test-print-positional test-printbasic test-printfloat test-scanfloat CFLAGS+= -lm .PHONY: tests Added: head/tools/regression/lib/libc/stdio/test-getdelim.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/lib/libc/stdio/test-getdelim.c Sat Feb 28 06:39:39 2009 (r189142) @@ -0,0 +1,168 @@ +/*- + * Copyright (c) 2009 David Schultz + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#define _WITH_GETLINE +#include +#include +#include +#include +#include + +#define CHUNK_MAX 10 + +/* The assertions depend on this string. */ +char apothegm[] = "All work and no play\0 makes Jack a dull boy.\n"; + +/* + * This is a neurotic reader function designed to give getdelim() a + * hard time. It reads through the string `apothegm' and returns a + * random number of bytes up to the requested length. + */ +static int +_reader(void *cookie, char *buf, int len) +{ + size_t *offp = cookie; + size_t r; + + r = random() % CHUNK_MAX + 1; + if (len > r) + len = r; + if (len > sizeof(apothegm) - *offp) + len = sizeof(apothegm) - *offp; + memcpy(buf, apothegm + *offp, len); + *offp += len; + return (len); +} + +static FILE * +mkfilebuf(void) +{ + size_t *offp; + + offp = malloc(sizeof(*offp)); /* XXX leak */ + *offp = 0; + return (fropen(offp, _reader)); +} + +int +main(int argc, char *argv[]) +{ + FILE *fp; + char *line; + size_t linecap; + int i, n; + + srandom(0); + + printf("1..5\n"); + + /* + * Test multiple times with different buffer sizes + * and different _reader() return values. + */ + errno = 0; + for (i = 0; i < 8; i++) { + fp = mkfilebuf(); + linecap = i; + line = malloc(i); + /* First line: the full apothegm */ + assert(getline(&line, &linecap, fp) == sizeof(apothegm) - 1); + assert(memcmp(line, apothegm, sizeof(apothegm)) == 0); + assert(linecap >= sizeof(apothegm)); + /* Second line: the NUL terminator following the newline */ + assert(getline(&line, &linecap, fp) == 1); + assert(line[0] == '\0' && line[1] == '\0'); + /* Third line: EOF */ + line[0] = 'X'; + assert(getline(&line, &linecap, fp) == 0); + assert(line[0] == '\0'); + free(line); + assert(feof(fp)); + assert(!ferror(fp)); + fclose(fp); + } + assert(errno == 0); + printf("ok 1 - getline basic\n"); + + /* Make sure read errors are handled properly. */ + linecap = 0; + errno = 0; + assert(getline(&line, &linecap, stdout) == -1); + assert(errno == EBADF); + errno = 0; + assert(getdelim(&line, &linecap, 'X', stdout) == -1); + assert(errno == EBADF); + assert(ferror(stdout)); + printf("ok 2 - stream error\n"); + + /* Make sure NULL linep or linecapp pointers are handled. */ + fp = mkfilebuf(); + assert(getline(NULL, &linecap, fp) == -1); + assert(errno == EINVAL); + assert(getline(&line, NULL, fp) == -1); + assert(errno == EINVAL); + assert(ferror(fp)); + fclose(fp); + printf("ok 3 - invalid params\n"); + + /* Make sure getline() allocates memory as needed if fp is at EOF. */ + errno = 0; + fp = mkfilebuf(); + while (!feof(fp)) /* advance to EOF; can't fseek this stream */ + getc(fp); + free(line); + line = NULL; + linecap = 0; + assert(getline(&line, &linecap, fp) == 0); + assert(line[0] == '\0'); + assert(linecap > 0); + assert(errno == 0); + assert(feof(fp)); + assert(!ferror(fp)); + fclose(fp); + printf("ok 4 - eof\n"); + + /* Make sure a NUL delimiter works. */ + fp = mkfilebuf(); + n = strlen(apothegm); + assert(getdelim(&line, &linecap, '\0', fp) == n + 1); + assert(strcmp(line, apothegm) == 0); + assert(line[n + 1] == '\0'); + assert(linecap > n + 1); + n = strlen(apothegm + n + 1); + assert(getdelim(&line, &linecap, '\0', fp) == n + 1); + assert(line[n + 1] == '\0'); + assert(linecap > n + 1); + assert(errno == 0); + assert(!ferror(fp)); + fclose(fp); + printf("ok 5 - nul\n"); + + exit(0); +}