2 * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
28 * Tests for basic and miscellaneous printf() formats.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
47 #define S_UINT64MAX "18446744073709551615"
48 #define S_UINT32MAX "4294967295"
49 #define S_INT64MIN "-9223372036854775808"
50 #define S_INT32MIN "-2147483648"
52 #define S_SIZEMAX (SIZE_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)
53 #define S_ULONGMAX (ULONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)
54 #define S_ULLONGMAX (ULLONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)
59 static uint32_t junk = 0xdeadbeef;
63 for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++)
67 #define testfmt(result, fmt, ...) \
68 _testfmt((result), #__VA_ARGS__, fmt, __VA_ARGS__)
70 _testfmt(const char *result, const char *argstr, const char *fmt,...)
73 wchar_t ws[BUF], wfmt[BUF], wresult[BUF];
80 vsnprintf(s, sizeof(s), fmt, ap);
81 if (strcmp(result, s) != 0) {
83 "printf(\"%s\", %s) ==> [%s], expected [%s]\n",
84 fmt, argstr, s, result);
88 mbstowcs(ws, s, BUF - 1);
89 mbstowcs(wfmt, fmt, BUF - 1);
90 mbstowcs(wresult, result, BUF - 1);
91 vswprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, ap2);
92 if (wcscmp(wresult, ws) != 0) {
94 "wprintf(\"%ls\", %s) ==> [%ls], expected [%ls]\n",
95 wfmt, argstr, ws, wresult);
101 ATF_TC_WITHOUT_HEAD(int_within_limits);
102 ATF_TC_BODY(int_within_limits, tc)
105 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
107 /* The test requires these to be true. */
108 ATF_REQUIRE(UINTMAX_MAX == UINT64_MAX);
109 ATF_REQUIRE(UINT_MAX == UINT32_MAX);
110 ATF_REQUIRE(USHRT_MAX == 0xffff);
111 ATF_REQUIRE(UCHAR_MAX == 0xff);
113 /* Make sure we handle signed vs. unsigned args correctly. */
114 testfmt("-1", "%jd", (intmax_t)-1);
115 testfmt(S_UINT64MAX, "%ju", UINT64_MAX);
117 testfmt("-1", "%td", (ptrdiff_t)-1);
118 testfmt(S_SIZEMAX, "%tu", (size_t)-1);
120 testfmt("-1", "%zd", (ssize_t)-1);
121 testfmt(S_SIZEMAX, "%zu", (ssize_t)-1);
123 testfmt("-1", "%ld", (long)-1);
124 testfmt(S_ULONGMAX, "%lu", ULONG_MAX);
126 testfmt("-1", "%lld", (long long)-1);
127 testfmt(S_ULONGMAX, "%lu", ULLONG_MAX);
129 testfmt("-1", "%d", -1);
130 testfmt(S_UINT32MAX, "%lu", UINT32_MAX);
132 testfmt("-1", "%hd", -1);
133 testfmt("65535", "%hu", USHRT_MAX);
135 testfmt("-1", "%hhd", -1);
136 testfmt("255", "%hhu", UCHAR_MAX);
139 ATF_TC_WITHOUT_HEAD(int_limits);
140 ATF_TC_BODY(int_limits, tc)
143 ATF_REQUIRE(setlocale(LC_NUMERIC, "C"));
146 * Check that printing the largest negative number does not cause
147 * overflow when it is negated.
149 testfmt(S_INT32MIN, "%d", INT_MIN);
150 testfmt(S_INT64MIN, "%jd", INTMAX_MIN);
156 ATF_TP_ADD_TC(tp, int_within_limits);
157 ATF_TP_ADD_TC(tp, int_limits);
159 return (atf_no_error());