From owner-svn-src-all@FreeBSD.ORG Sat Feb 28 06:37:10 2009 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id ABA5B106564A; Sat, 28 Feb 2009 06:37:10 +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 98B908FC13; Sat, 28 Feb 2009 06:37:10 +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 n1S6bAtY097998; Sat, 28 Feb 2009 06:37:10 GMT (envelope-from das@svn.freebsd.org) Received: (from das@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id n1S6bAkD097996; Sat, 28 Feb 2009 06:37:10 GMT (envelope-from das@svn.freebsd.org) Message-Id: <200902280637.n1S6bAkD097996@svn.freebsd.org> From: David Schultz Date: Sat, 28 Feb 2009 06:37:10 +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: r189141 - head/tools/regression/lib/libc/stdio X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 28 Feb 2009 06:37:11 -0000 Author: das Date: Sat Feb 28 06:37:10 2009 New Revision: 189141 URL: http://svn.freebsd.org/changeset/base/189141 Log: Add a file containing tests for simple format specifiers. Currently it only has tests for a few sign issues with integer formats, including PR 131880. Added: head/tools/regression/lib/libc/stdio/test-printbasic.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:34:04 2009 (r189140) +++ head/tools/regression/lib/libc/stdio/Makefile Sat Feb 28 06:37:10 2009 (r189141) @@ -1,6 +1,6 @@ # $FreeBSD$ -TESTS= test-perror test-print-positional test-printfloat test-scanfloat +TESTS= test-perror test-print-positional test-printbasic test-printfloat test-scanfloat CFLAGS+= -lm .PHONY: tests Added: head/tools/regression/lib/libc/stdio/test-printbasic.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/tools/regression/lib/libc/stdio/test-printbasic.c Sat Feb 28 06:37:10 2009 (r189141) @@ -0,0 +1,156 @@ +/*- + * 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. + */ + +/* + * Tests for basic and miscellaneous printf() formats. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define testfmt(result, fmt, ...) \ + _testfmt((result), __LINE__, #__VA_ARGS__, fmt, __VA_ARGS__) +void _testfmt(const char *, int, const char *, const char *, ...); +void smash_stack(void); + +#define S_UINT64MAX "18446744073709551615" +#define S_UINT32MAX "4294967295" +#define S_INT64MIN "-9223372036854775808" +#define S_INT32MIN "-2147483648" + +#define S_SIZEMAX (SIZE_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX) +#define S_ULONGMAX (ULONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX) +#define S_ULLONGMAX (ULLONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX) + +int +main(int argc, char *argv[]) +{ + + printf("1..2\n"); + assert(setlocale(LC_NUMERIC, "C")); + + /* The test requires these to be true. */ + assert(UINTMAX_MAX == UINT64_MAX); + assert(UINT_MAX == UINT32_MAX); + assert(USHRT_MAX == 0xffff); + assert(UCHAR_MAX == 0xff); + + /* + * Make sure we handle signed vs. unsigned args correctly. + */ + testfmt("-1", "%jd", (intmax_t)-1); + testfmt(S_UINT64MAX, "%ju", UINT64_MAX); + + testfmt("-1", "%td", (ptrdiff_t)-1); + testfmt(S_SIZEMAX, "%tu", (size_t)-1); + + testfmt("-1", "%zd", (ssize_t)-1); + testfmt(S_SIZEMAX, "%zu", (ssize_t)-1); + + testfmt("-1", "%ld", (long)-1); + testfmt(S_ULONGMAX, "%lu", ULONG_MAX); + + testfmt("-1", "%lld", (long long)-1); + testfmt(S_ULONGMAX, "%lu", ULLONG_MAX); + + testfmt("-1", "%d", -1); + testfmt(S_UINT32MAX, "%lu", UINT32_MAX); + + testfmt("-1", "%hd", -1); + testfmt("65535", "%hu", USHRT_MAX); + + testfmt("-1", "%hhd", -1); + testfmt("255", "%hhu", UCHAR_MAX); + + printf("ok 1 - printbasic signed/unsigned\n"); + + /* + * Check that printing the largest negative number does not cause + * overflow when it is negated. + */ + testfmt(S_INT32MIN, "%d", INT_MIN); + testfmt(S_INT64MIN, "%jd", INTMAX_MIN); + + printf("ok 2 - printbasic INT_MIN\n"); + + + return (0); +} + +void +smash_stack(void) +{ + static uint32_t junk = 0xdeadbeef; + uint32_t buf[512]; + int i; + + for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++) + buf[i] = junk; +} + +void +_testfmt(const char *result, int line, const char *argstr, const char *fmt,...) +{ +#define BUF 100 + wchar_t ws[BUF], wfmt[BUF], wresult[BUF]; + char s[BUF]; + va_list ap, ap2; + + va_start(ap, fmt); + va_copy(ap2, ap); + smash_stack(); + vsnprintf(s, sizeof(s), fmt, ap); + if (strcmp(result, s) != 0) { + fprintf(stderr, + "%d: printf(\"%s\", %s) ==> [%s], expected [%s]\n", + line, fmt, argstr, s, result); + abort(); + } + + smash_stack(); + mbstowcs(ws, s, BUF - 1); + mbstowcs(wfmt, fmt, BUF - 1); + mbstowcs(wresult, result, BUF - 1); + vswprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, ap2); + if (wcscmp(wresult, ws) != 0) { + fprintf(stderr, + "%d: wprintf(\"%ls\", %s) ==> [%ls], expected [%ls]\n", + line, wfmt, argstr, ws, wresult); + abort(); + } +}