xref: /freebsd/lib/libc/tests/stdio/printbasic_test.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
11ee02192SEnji Cooper /*-
21ee02192SEnji Cooper  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
31ee02192SEnji Cooper  * All rights reserved.
41ee02192SEnji Cooper  *
51ee02192SEnji Cooper  * Redistribution and use in source and binary forms, with or without
61ee02192SEnji Cooper  * modification, are permitted provided that the following conditions
71ee02192SEnji Cooper  * are met:
81ee02192SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
91ee02192SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
101ee02192SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
111ee02192SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
121ee02192SEnji Cooper  *    documentation and/or other materials provided with the distribution.
131ee02192SEnji Cooper  *
141ee02192SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151ee02192SEnji Cooper  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161ee02192SEnji Cooper  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171ee02192SEnji Cooper  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181ee02192SEnji Cooper  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191ee02192SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201ee02192SEnji Cooper  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211ee02192SEnji Cooper  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221ee02192SEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231ee02192SEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241ee02192SEnji Cooper  * SUCH DAMAGE.
251ee02192SEnji Cooper  */
261ee02192SEnji Cooper 
271ee02192SEnji Cooper /*
281ee02192SEnji Cooper  * Tests for basic and miscellaneous printf() formats.
291ee02192SEnji Cooper  */
301ee02192SEnji Cooper 
311ee02192SEnji Cooper #include <err.h>
321ee02192SEnji Cooper #include <limits.h>
331ee02192SEnji Cooper #include <locale.h>
341ee02192SEnji Cooper #include <stdio.h>
351ee02192SEnji Cooper #include <stdarg.h>
361ee02192SEnji Cooper #include <stddef.h>
371ee02192SEnji Cooper #include <stdint.h>
381ee02192SEnji Cooper #include <stdlib.h>
391ee02192SEnji Cooper #include <string.h>
401ee02192SEnji Cooper #include <wchar.h>
411ee02192SEnji Cooper 
421ee02192SEnji Cooper #include <atf-c.h>
431ee02192SEnji Cooper 
441ee02192SEnji Cooper #define	S_UINT64MAX	"18446744073709551615"
451ee02192SEnji Cooper #define	S_UINT32MAX	"4294967295"
461ee02192SEnji Cooper #define	S_INT64MIN	"-9223372036854775808"
471ee02192SEnji Cooper #define	S_INT32MIN	"-2147483648"
481ee02192SEnji Cooper 
491ee02192SEnji Cooper #define	S_SIZEMAX	(SIZE_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)
501ee02192SEnji Cooper #define	S_ULONGMAX	(ULONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)
511ee02192SEnji Cooper #define	S_ULLONGMAX	(ULLONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)
521ee02192SEnji Cooper 
531ee02192SEnji Cooper static void
smash_stack(void)541ee02192SEnji Cooper smash_stack(void)
551ee02192SEnji Cooper {
561ee02192SEnji Cooper 	static uint32_t junk = 0xdeadbeef;
571ee02192SEnji Cooper 	uint32_t buf[512];
581ee02192SEnji Cooper 	int i;
591ee02192SEnji Cooper 
601ee02192SEnji Cooper 	for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++)
611ee02192SEnji Cooper 		buf[i] = junk;
621ee02192SEnji Cooper }
631ee02192SEnji Cooper 
641ee02192SEnji Cooper #define	testfmt(result, fmt, ...)       \
651ee02192SEnji Cooper 	_testfmt((result), #__VA_ARGS__, fmt, __VA_ARGS__)
661ee02192SEnji Cooper static void
_testfmt(const char * result,const char * argstr,const char * fmt,...)671ee02192SEnji Cooper _testfmt(const char *result, const char *argstr, const char *fmt,...)
681ee02192SEnji Cooper {
691ee02192SEnji Cooper #define	BUF	100
701ee02192SEnji Cooper 	wchar_t ws[BUF], wfmt[BUF], wresult[BUF];
711ee02192SEnji Cooper 	char s[BUF];
721ee02192SEnji Cooper 	va_list ap, ap2;
731ee02192SEnji Cooper 
741ee02192SEnji Cooper 	va_start(ap, fmt);
751ee02192SEnji Cooper 	va_copy(ap2, ap);
761ee02192SEnji Cooper 	smash_stack();
771ee02192SEnji Cooper 	vsnprintf(s, sizeof(s), fmt, ap);
781eca9a9aSEnji Cooper 	ATF_CHECK_MSG(strcmp(result, s) == 0,
79e57c1140SEnji Cooper 	    "printf(\"%s\", %s) ==> [%s], expected [%s]",
801ee02192SEnji Cooper 	    fmt, argstr, s, result);
811ee02192SEnji Cooper 
821ee02192SEnji Cooper 	smash_stack();
831ee02192SEnji Cooper 	mbstowcs(ws, s, BUF - 1);
841ee02192SEnji Cooper 	mbstowcs(wfmt, fmt, BUF - 1);
851ee02192SEnji Cooper 	mbstowcs(wresult, result, BUF - 1);
861ee02192SEnji Cooper 	vswprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, ap2);
871eca9a9aSEnji Cooper 	ATF_CHECK_MSG(wcscmp(wresult, ws) == 0,
88e57c1140SEnji Cooper 	    "wprintf(\"%ls\", %s) ==> [%ls], expected [%ls]",
891ee02192SEnji Cooper 	    wfmt, argstr, ws, wresult);
901eca9a9aSEnji Cooper 
913e227991SEnji Cooper 	va_end(ap);
923e227991SEnji Cooper 	va_end(ap2);
931ee02192SEnji Cooper }
941ee02192SEnji Cooper 
951ee02192SEnji Cooper ATF_TC_WITHOUT_HEAD(int_within_limits);
ATF_TC_BODY(int_within_limits,tc)961ee02192SEnji Cooper ATF_TC_BODY(int_within_limits, tc)
971ee02192SEnji Cooper {
981ee02192SEnji Cooper 
991ee02192SEnji Cooper 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
1001ee02192SEnji Cooper 
1011ee02192SEnji Cooper 	/* The test requires these to be true. */
1021ee02192SEnji Cooper 	ATF_REQUIRE(UINTMAX_MAX == UINT64_MAX);
1031ee02192SEnji Cooper 	ATF_REQUIRE(UINT_MAX == UINT32_MAX);
1041ee02192SEnji Cooper 	ATF_REQUIRE(USHRT_MAX == 0xffff);
1051ee02192SEnji Cooper 	ATF_REQUIRE(UCHAR_MAX == 0xff);
1061ee02192SEnji Cooper 
1071ee02192SEnji Cooper 	/* Make sure we handle signed vs. unsigned args correctly. */
1081ee02192SEnji Cooper 	testfmt("-1", "%jd", (intmax_t)-1);
1091ee02192SEnji Cooper 	testfmt(S_UINT64MAX, "%ju", UINT64_MAX);
1101ee02192SEnji Cooper 
111*9a41ce4aSEnji Cooper 	if (sizeof(ptrdiff_t) != sizeof(uintmax_t))
112*9a41ce4aSEnji Cooper 		atf_tc_expect_fail("the %%t qualifier is broken on 32-bit "
113*9a41ce4aSEnji Cooper 		    "platforms where there's a mismatch between ptrdiff_t and "
114*9a41ce4aSEnji Cooper 		    "uintmax_t's type width; bug # 191674");
115*9a41ce4aSEnji Cooper 
1161ee02192SEnji Cooper 	testfmt("-1", "%td", (ptrdiff_t)-1);
1171ee02192SEnji Cooper 	testfmt(S_SIZEMAX, "%tu", (size_t)-1);
1181ee02192SEnji Cooper 
1191ee02192SEnji Cooper 	testfmt("-1", "%zd", (ssize_t)-1);
1201ee02192SEnji Cooper 	testfmt(S_SIZEMAX, "%zu", (ssize_t)-1);
1211ee02192SEnji Cooper 
1221ee02192SEnji Cooper 	testfmt("-1", "%ld", (long)-1);
1231ee02192SEnji Cooper 	testfmt(S_ULONGMAX, "%lu", ULONG_MAX);
1241ee02192SEnji Cooper 
1251ee02192SEnji Cooper 	testfmt("-1", "%lld", (long long)-1);
126c3fa65b1SRuslan Bukin 	testfmt(S_ULLONGMAX, "%llu", ULLONG_MAX);
1271ee02192SEnji Cooper 
1281ee02192SEnji Cooper 	testfmt("-1", "%d", -1);
129c3fa65b1SRuslan Bukin 	testfmt(S_UINT32MAX, "%u", UINT32_MAX);
1301ee02192SEnji Cooper 
1311ee02192SEnji Cooper 	testfmt("-1", "%hd", -1);
1321ee02192SEnji Cooper 	testfmt("65535", "%hu", USHRT_MAX);
1331ee02192SEnji Cooper 
1341ee02192SEnji Cooper 	testfmt("-1", "%hhd", -1);
1351ee02192SEnji Cooper 	testfmt("255", "%hhu", UCHAR_MAX);
1361ee02192SEnji Cooper }
1371ee02192SEnji Cooper 
1381ee02192SEnji Cooper ATF_TC_WITHOUT_HEAD(int_limits);
ATF_TC_BODY(int_limits,tc)1391ee02192SEnji Cooper ATF_TC_BODY(int_limits, tc)
1401ee02192SEnji Cooper {
1411ee02192SEnji Cooper 
1421ee02192SEnji Cooper 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
1431ee02192SEnji Cooper 
1441ee02192SEnji Cooper 	/*
1451ee02192SEnji Cooper 	 * Check that printing the largest negative number does not cause
1461ee02192SEnji Cooper 	 * overflow when it is negated.
1471ee02192SEnji Cooper 	 */
1481ee02192SEnji Cooper 	testfmt(S_INT32MIN, "%d", INT_MIN);
1491ee02192SEnji Cooper 	testfmt(S_INT64MIN, "%jd", INTMAX_MIN);
1501ee02192SEnji Cooper }
1511ee02192SEnji Cooper 
ATF_TP_ADD_TCS(tp)1521ee02192SEnji Cooper ATF_TP_ADD_TCS(tp)
1531ee02192SEnji Cooper {
1541ee02192SEnji Cooper 
1551ee02192SEnji Cooper 	ATF_TP_ADD_TC(tp, int_within_limits);
1561ee02192SEnji Cooper 	ATF_TP_ADD_TC(tp, int_limits);
1571ee02192SEnji Cooper 
1581ee02192SEnji Cooper 	return (atf_no_error());
1591ee02192SEnji Cooper }
160