1189141Sdas/*- 2189141Sdas * Copyright (c) 2009 David Schultz <das@FreeBSD.org> 3189141Sdas * All rights reserved. 4189141Sdas * 5189141Sdas * Redistribution and use in source and binary forms, with or without 6189141Sdas * modification, are permitted provided that the following conditions 7189141Sdas * are met: 8189141Sdas * 1. Redistributions of source code must retain the above copyright 9189141Sdas * notice, this list of conditions and the following disclaimer. 10189141Sdas * 2. Redistributions in binary form must reproduce the above copyright 11189141Sdas * notice, this list of conditions and the following disclaimer in the 12189141Sdas * documentation and/or other materials provided with the distribution. 13189141Sdas * 14189141Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15189141Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16189141Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17189141Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18189141Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19189141Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20189141Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21189141Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22189141Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23189141Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24189141Sdas * SUCH DAMAGE. 25189141Sdas */ 26189141Sdas 27189141Sdas/* 28189141Sdas * Tests for basic and miscellaneous printf() formats. 29189141Sdas */ 30189141Sdas 31189141Sdas#include <sys/cdefs.h> 32189141Sdas__FBSDID("$FreeBSD: releng/10.3/lib/libc/tests/stdio/printbasic_test.c 292152 2015-12-13 04:29:09Z ngie $"); 33189141Sdas 34189141Sdas#include <err.h> 35189141Sdas#include <limits.h> 36189141Sdas#include <locale.h> 37189141Sdas#include <stdio.h> 38189141Sdas#include <stdarg.h> 39189141Sdas#include <stddef.h> 40189141Sdas#include <stdint.h> 41189141Sdas#include <stdlib.h> 42189141Sdas#include <string.h> 43189141Sdas#include <wchar.h> 44189141Sdas 45290537Sngie#include <atf-c.h> 46189141Sdas 47189141Sdas#define S_UINT64MAX "18446744073709551615" 48189141Sdas#define S_UINT32MAX "4294967295" 49189141Sdas#define S_INT64MIN "-9223372036854775808" 50189141Sdas#define S_INT32MIN "-2147483648" 51189141Sdas 52189141Sdas#define S_SIZEMAX (SIZE_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX) 53189141Sdas#define S_ULONGMAX (ULONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX) 54189141Sdas#define S_ULLONGMAX (ULLONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX) 55189141Sdas 56290537Sngiestatic void 57290537Sngiesmash_stack(void) 58189141Sdas{ 59290537Sngie static uint32_t junk = 0xdeadbeef; 60290537Sngie uint32_t buf[512]; 61290537Sngie int i; 62189141Sdas 63290537Sngie for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++) 64290537Sngie buf[i] = junk; 65290537Sngie} 66189141Sdas 67290537Sngie#define testfmt(result, fmt, ...) \ 68290537Sngie _testfmt((result), #__VA_ARGS__, fmt, __VA_ARGS__) 69290537Sngiestatic void 70290537Sngie_testfmt(const char *result, const char *argstr, const char *fmt,...) 71290537Sngie{ 72290537Sngie#define BUF 100 73290537Sngie wchar_t ws[BUF], wfmt[BUF], wresult[BUF]; 74290537Sngie char s[BUF]; 75290537Sngie va_list ap, ap2; 76290537Sngie 77290537Sngie va_start(ap, fmt); 78290537Sngie va_copy(ap2, ap); 79290537Sngie smash_stack(); 80290537Sngie vsnprintf(s, sizeof(s), fmt, ap); 81290537Sngie if (strcmp(result, s) != 0) { 82290537Sngie atf_tc_fail( 83290537Sngie "printf(\"%s\", %s) ==> [%s], expected [%s]\n", 84290537Sngie fmt, argstr, s, result); 85290537Sngie } 86290537Sngie 87290537Sngie smash_stack(); 88290537Sngie mbstowcs(ws, s, BUF - 1); 89290537Sngie mbstowcs(wfmt, fmt, BUF - 1); 90290537Sngie mbstowcs(wresult, result, BUF - 1); 91290537Sngie vswprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, ap2); 92290537Sngie if (wcscmp(wresult, ws) != 0) { 93290537Sngie atf_tc_fail( 94290537Sngie "wprintf(\"%ls\", %s) ==> [%ls], expected [%ls]\n", 95290537Sngie wfmt, argstr, ws, wresult); 96290537Sngie } 97292152Sngie va_end(ap); 98292152Sngie va_end(ap2); 99290537Sngie} 100290537Sngie 101290537SngieATF_TC_WITHOUT_HEAD(int_within_limits); 102290537SngieATF_TC_BODY(int_within_limits, tc) 103290537Sngie{ 104290537Sngie 105290537Sngie ATF_REQUIRE(setlocale(LC_NUMERIC, "C")); 106290537Sngie 107189141Sdas /* The test requires these to be true. */ 108290537Sngie ATF_REQUIRE(UINTMAX_MAX == UINT64_MAX); 109290537Sngie ATF_REQUIRE(UINT_MAX == UINT32_MAX); 110290537Sngie ATF_REQUIRE(USHRT_MAX == 0xffff); 111290537Sngie ATF_REQUIRE(UCHAR_MAX == 0xff); 112189141Sdas 113290537Sngie /* Make sure we handle signed vs. unsigned args correctly. */ 114189141Sdas testfmt("-1", "%jd", (intmax_t)-1); 115189141Sdas testfmt(S_UINT64MAX, "%ju", UINT64_MAX); 116189141Sdas 117189141Sdas testfmt("-1", "%td", (ptrdiff_t)-1); 118189141Sdas testfmt(S_SIZEMAX, "%tu", (size_t)-1); 119189141Sdas 120189141Sdas testfmt("-1", "%zd", (ssize_t)-1); 121189141Sdas testfmt(S_SIZEMAX, "%zu", (ssize_t)-1); 122189141Sdas 123189141Sdas testfmt("-1", "%ld", (long)-1); 124189141Sdas testfmt(S_ULONGMAX, "%lu", ULONG_MAX); 125189141Sdas 126189141Sdas testfmt("-1", "%lld", (long long)-1); 127189141Sdas testfmt(S_ULONGMAX, "%lu", ULLONG_MAX); 128189141Sdas 129189141Sdas testfmt("-1", "%d", -1); 130189141Sdas testfmt(S_UINT32MAX, "%lu", UINT32_MAX); 131189141Sdas 132189141Sdas testfmt("-1", "%hd", -1); 133189141Sdas testfmt("65535", "%hu", USHRT_MAX); 134189141Sdas 135189141Sdas testfmt("-1", "%hhd", -1); 136189141Sdas testfmt("255", "%hhu", UCHAR_MAX); 137290537Sngie} 138189141Sdas 139290537SngieATF_TC_WITHOUT_HEAD(int_limits); 140290537SngieATF_TC_BODY(int_limits, tc) 141290537Sngie{ 142189141Sdas 143290537Sngie ATF_REQUIRE(setlocale(LC_NUMERIC, "C")); 144290537Sngie 145189141Sdas /* 146189141Sdas * Check that printing the largest negative number does not cause 147189141Sdas * overflow when it is negated. 148189141Sdas */ 149189141Sdas testfmt(S_INT32MIN, "%d", INT_MIN); 150189141Sdas testfmt(S_INT64MIN, "%jd", INTMAX_MIN); 151189141Sdas} 152189141Sdas 153290537SngieATF_TP_ADD_TCS(tp) 154189141Sdas{ 155189141Sdas 156290537Sngie ATF_TP_ADD_TC(tp, int_within_limits); 157290537Sngie ATF_TP_ADD_TC(tp, int_limits); 158189141Sdas 159290537Sngie return (atf_no_error()); 160189141Sdas} 161