xref: /freebsd/lib/libc/tests/stdio/printbasic_test.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*-
2  * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Tests for basic and miscellaneous printf() formats.
29  */
30 
31 #include <err.h>
32 #include <limits.h>
33 #include <locale.h>
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <stddef.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <wchar.h>
41 
42 #include <atf-c.h>
43 
44 #define	S_UINT64MAX	"18446744073709551615"
45 #define	S_UINT32MAX	"4294967295"
46 #define	S_INT64MIN	"-9223372036854775808"
47 #define	S_INT32MIN	"-2147483648"
48 
49 #define	S_SIZEMAX	(SIZE_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)
50 #define	S_ULONGMAX	(ULONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)
51 #define	S_ULLONGMAX	(ULLONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)
52 
53 static void
54 smash_stack(void)
55 {
56 	static uint32_t junk = 0xdeadbeef;
57 	uint32_t buf[512];
58 	int i;
59 
60 	for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++)
61 		buf[i] = junk;
62 }
63 
64 #define	testfmt(result, fmt, ...)       \
65 	_testfmt((result), #__VA_ARGS__, fmt, __VA_ARGS__)
66 static void
67 _testfmt(const char *result, const char *argstr, const char *fmt,...)
68 {
69 #define	BUF	100
70 	wchar_t ws[BUF], wfmt[BUF], wresult[BUF];
71 	char s[BUF];
72 	va_list ap, ap2;
73 
74 	va_start(ap, fmt);
75 	va_copy(ap2, ap);
76 	smash_stack();
77 	vsnprintf(s, sizeof(s), fmt, ap);
78 	ATF_CHECK_MSG(strcmp(result, s) == 0,
79 	    "printf(\"%s\", %s) ==> [%s], expected [%s]",
80 	    fmt, argstr, s, result);
81 
82 	smash_stack();
83 	mbstowcs(ws, s, BUF - 1);
84 	mbstowcs(wfmt, fmt, BUF - 1);
85 	mbstowcs(wresult, result, BUF - 1);
86 	vswprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, ap2);
87 	ATF_CHECK_MSG(wcscmp(wresult, ws) == 0,
88 	    "wprintf(\"%ls\", %s) ==> [%ls], expected [%ls]",
89 	    wfmt, argstr, ws, wresult);
90 
91 	va_end(ap);
92 	va_end(ap2);
93 }
94 
95 ATF_TC_WITHOUT_HEAD(int_within_limits);
96 ATF_TC_BODY(int_within_limits, tc)
97 {
98 
99 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
100 
101 	/* The test requires these to be true. */
102 	ATF_REQUIRE(UINTMAX_MAX == UINT64_MAX);
103 	ATF_REQUIRE(UINT_MAX == UINT32_MAX);
104 	ATF_REQUIRE(USHRT_MAX == 0xffff);
105 	ATF_REQUIRE(UCHAR_MAX == 0xff);
106 
107 	/* Make sure we handle signed vs. unsigned args correctly. */
108 	testfmt("-1", "%jd", (intmax_t)-1);
109 	testfmt(S_UINT64MAX, "%ju", UINT64_MAX);
110 
111 	if (sizeof(ptrdiff_t) != sizeof(uintmax_t))
112 		atf_tc_expect_fail("the %%t qualifier is broken on 32-bit "
113 		    "platforms where there's a mismatch between ptrdiff_t and "
114 		    "uintmax_t's type width; bug # 191674");
115 
116 	testfmt("-1", "%td", (ptrdiff_t)-1);
117 	testfmt(S_SIZEMAX, "%tu", (size_t)-1);
118 
119 	testfmt("-1", "%zd", (ssize_t)-1);
120 	testfmt(S_SIZEMAX, "%zu", (ssize_t)-1);
121 
122 	testfmt("-1", "%ld", (long)-1);
123 	testfmt(S_ULONGMAX, "%lu", ULONG_MAX);
124 
125 	testfmt("-1", "%lld", (long long)-1);
126 	testfmt(S_ULLONGMAX, "%llu", ULLONG_MAX);
127 
128 	testfmt("-1", "%d", -1);
129 	testfmt(S_UINT32MAX, "%u", UINT32_MAX);
130 
131 	testfmt("-1", "%hd", -1);
132 	testfmt("65535", "%hu", USHRT_MAX);
133 
134 	testfmt("-1", "%hhd", -1);
135 	testfmt("255", "%hhu", UCHAR_MAX);
136 }
137 
138 ATF_TC_WITHOUT_HEAD(int_limits);
139 ATF_TC_BODY(int_limits, tc)
140 {
141 
142 	ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
143 
144 	/*
145 	 * Check that printing the largest negative number does not cause
146 	 * overflow when it is negated.
147 	 */
148 	testfmt(S_INT32MIN, "%d", INT_MIN);
149 	testfmt(S_INT64MIN, "%jd", INTMAX_MIN);
150 }
151 
152 ATF_TP_ADD_TCS(tp)
153 {
154 
155 	ATF_TP_ADD_TC(tp, int_within_limits);
156 	ATF_TP_ADD_TC(tp, int_limits);
157 
158 	return (atf_no_error());
159 }
160