printbasic_test.c revision 189141
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 <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/tools/regression/lib/libc/stdio/test-printbasic.c 189141 2009-02-28 06:37:10Z das $");
33
34#include <assert.h>
35#include <err.h>
36#include <limits.h>
37#include <locale.h>
38#include <stdio.h>
39#include <stdarg.h>
40#include <stddef.h>
41#include <stdint.h>
42#include <stdlib.h>
43#include <string.h>
44#include <wchar.h>
45
46#define	testfmt(result, fmt, ...)	\
47	_testfmt((result), __LINE__, #__VA_ARGS__, fmt, __VA_ARGS__)
48void _testfmt(const char *, int, const char *, const char *, ...);
49void smash_stack(void);
50
51#define	S_UINT64MAX	"18446744073709551615"
52#define	S_UINT32MAX	"4294967295"
53#define	S_INT64MIN	"-9223372036854775808"
54#define	S_INT32MIN	"-2147483648"
55
56#define	S_SIZEMAX	(SIZE_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)
57#define	S_ULONGMAX	(ULONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)
58#define	S_ULLONGMAX	(ULLONG_MAX == UINT64_MAX ? S_UINT64MAX : S_UINT32MAX)
59
60int
61main(int argc, char *argv[])
62{
63
64	printf("1..2\n");
65	assert(setlocale(LC_NUMERIC, "C"));
66
67	/* The test requires these to be true. */
68	assert(UINTMAX_MAX == UINT64_MAX);
69	assert(UINT_MAX == UINT32_MAX);
70	assert(USHRT_MAX == 0xffff);
71	assert(UCHAR_MAX == 0xff);
72
73	/*
74	 * Make sure we handle signed vs. unsigned args correctly.
75	 */
76	testfmt("-1", "%jd", (intmax_t)-1);
77	testfmt(S_UINT64MAX, "%ju", UINT64_MAX);
78
79	testfmt("-1", "%td", (ptrdiff_t)-1);
80	testfmt(S_SIZEMAX, "%tu", (size_t)-1);
81
82	testfmt("-1", "%zd", (ssize_t)-1);
83	testfmt(S_SIZEMAX, "%zu", (ssize_t)-1);
84
85	testfmt("-1", "%ld", (long)-1);
86	testfmt(S_ULONGMAX, "%lu", ULONG_MAX);
87
88	testfmt("-1", "%lld", (long long)-1);
89	testfmt(S_ULONGMAX, "%lu", ULLONG_MAX);
90
91	testfmt("-1", "%d", -1);
92	testfmt(S_UINT32MAX, "%lu", UINT32_MAX);
93
94	testfmt("-1", "%hd", -1);
95	testfmt("65535", "%hu", USHRT_MAX);
96
97	testfmt("-1", "%hhd", -1);
98	testfmt("255", "%hhu", UCHAR_MAX);
99
100	printf("ok 1 - printbasic signed/unsigned\n");
101
102	/*
103	 * Check that printing the largest negative number does not cause
104	 * overflow when it is negated.
105	 */
106	testfmt(S_INT32MIN, "%d", INT_MIN);
107	testfmt(S_INT64MIN, "%jd", INTMAX_MIN);
108
109	printf("ok 2 - printbasic INT_MIN\n");
110
111
112	return (0);
113}
114
115void
116smash_stack(void)
117{
118	static uint32_t junk = 0xdeadbeef;
119	uint32_t buf[512];
120	int i;
121
122	for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++)
123		buf[i] = junk;
124}
125
126void
127_testfmt(const char *result, int line, const char *argstr, const char *fmt,...)
128{
129#define	BUF	100
130	wchar_t ws[BUF], wfmt[BUF], wresult[BUF];
131	char s[BUF];
132	va_list ap, ap2;
133
134	va_start(ap, fmt);
135	va_copy(ap2, ap);
136	smash_stack();
137	vsnprintf(s, sizeof(s), fmt, ap);
138	if (strcmp(result, s) != 0) {
139		fprintf(stderr,
140		    "%d: printf(\"%s\", %s) ==> [%s], expected [%s]\n",
141		    line, fmt, argstr, s, result);
142		abort();
143	}
144
145	smash_stack();
146	mbstowcs(ws, s, BUF - 1);
147	mbstowcs(wfmt, fmt, BUF - 1);
148	mbstowcs(wresult, result, BUF - 1);
149	vswprintf(ws, sizeof(ws) / sizeof(ws[0]), wfmt, ap2);
150	if (wcscmp(wresult, ws) != 0) {
151		fprintf(stderr,
152		    "%d: wprintf(\"%ls\", %s) ==> [%ls], expected [%ls]\n",
153		    line, wfmt, argstr, ws, wresult);
154		abort();
155	}
156}
157