Path: blob/main/lib/libc/tests/stdio/printbasic_test.c
39530 views
/*-1* Copyright (c) 2009 David Schultz <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Tests for basic and miscellaneous printf() formats.28*/2930#include <err.h>31#include <limits.h>32#include <locale.h>33#include <stdio.h>34#include <stdarg.h>35#include <stddef.h>36#include <stdint.h>37#include <stdlib.h>38#include <string.h>39#include <wchar.h>4041#include <atf-c.h>4243#define S_UINT64MAX "18446744073709551615"44#define S_UINT32MAX "4294967295"45#define S_INT64MIN "-9223372036854775808"46#define S_INT32MIN "-2147483648"4748#define S_SIZEMAX (SIZE_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)49#define S_ULONGMAX (ULONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)50#define S_ULLONGMAX (ULLONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)5152static void53smash_stack(void)54{55static uint32_t junk = 0xdeadbeef;56uint32_t buf[512];57int i;5859for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++)60buf[i] = junk;61}6263#define testfmt(result, fmt, ...) \64_testfmt((result), #__VA_ARGS__, fmt, __VA_ARGS__)65static void66_testfmt(const char *result, const char *argstr, const char *fmt,...)67{68#define BUF 10069wchar_t ws[BUF], wfmt[BUF], wresult[BUF];70char s[BUF];71va_list ap, ap2;7273va_start(ap, fmt);74va_copy(ap2, ap);75smash_stack();76vsnprintf(s, sizeof(s), fmt, ap);77ATF_CHECK_MSG(strcmp(result, s) == 0,78"printf(\"%s\", %s) ==> [%s], expected [%s]",79fmt, argstr, s, result);8081smash_stack();82mbstowcs(ws, s, BUF - 1);83mbstowcs(wfmt, fmt, BUF - 1);84mbstowcs(wresult, result, BUF - 1);85vswprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, ap2);86ATF_CHECK_MSG(wcscmp(wresult, ws) == 0,87"wprintf(\"%ls\", %s) ==> [%ls], expected [%ls]",88wfmt, argstr, ws, wresult);8990va_end(ap);91va_end(ap2);92}9394ATF_TC_WITHOUT_HEAD(int_within_limits);95ATF_TC_BODY(int_within_limits, tc)96{9798ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));99100/* The test requires these to be true. */101ATF_REQUIRE(UINTMAX_MAX == UINT64_MAX);102ATF_REQUIRE(UINT_MAX == UINT32_MAX);103ATF_REQUIRE(USHRT_MAX == 0xffff);104ATF_REQUIRE(UCHAR_MAX == 0xff);105106/* Make sure we handle signed vs. unsigned args correctly. */107testfmt("-1", "%jd", (intmax_t)-1);108testfmt(S_UINT64MAX, "%ju", UINT64_MAX);109110if (sizeof(ptrdiff_t) != sizeof(uintmax_t))111atf_tc_expect_fail("the %%t qualifier is broken on 32-bit "112"platforms where there's a mismatch between ptrdiff_t and "113"uintmax_t's type width; bug # 191674");114115testfmt("-1", "%td", (ptrdiff_t)-1);116testfmt(S_SIZEMAX, "%tu", (size_t)-1);117118testfmt("-1", "%zd", (ssize_t)-1);119testfmt(S_SIZEMAX, "%zu", (ssize_t)-1);120121testfmt("-1", "%ld", (long)-1);122testfmt(S_ULONGMAX, "%lu", ULONG_MAX);123124testfmt("-1", "%lld", (long long)-1);125testfmt(S_ULLONGMAX, "%llu", ULLONG_MAX);126127testfmt("-1", "%d", -1);128testfmt(S_UINT32MAX, "%u", UINT32_MAX);129130testfmt("-1", "%hd", -1);131testfmt("65535", "%hu", USHRT_MAX);132133testfmt("-1", "%hhd", -1);134testfmt("255", "%hhu", UCHAR_MAX);135}136137ATF_TC_WITHOUT_HEAD(int_limits);138ATF_TC_BODY(int_limits, tc)139{140141ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));142143/*144* Check that printing the largest negative number does not cause145* overflow when it is negated.146*/147testfmt(S_INT32MIN, "%d", INT_MIN);148testfmt(S_INT64MIN, "%jd", INTMAX_MIN);149}150151ATF_TP_ADD_TCS(tp)152{153154ATF_TP_ADD_TC(tp, int_within_limits);155ATF_TP_ADD_TC(tp, int_limits);156157return (atf_no_error());158}159160161