1180103Sdas/*      $OpenBSD: sprintf_test.c,v 1.3 2004/09/16 20:22:26 otto Exp $ */
2180103Sdas
3180103Sdas/*
4180103Sdas * Copyright (c) 2003 Theo de Raadt
5180103Sdas * All rights reserved.
6180103Sdas *
7180103Sdas * Redistribution and use in source and binary forms, with or without
8180103Sdas * modification, are permitted provided that the following conditions
9180103Sdas * are met:
10180103Sdas *
11180103Sdas *    - Redistributions of source code must retain the above copyright
12180103Sdas *      notice, this list of conditions and the following disclaimer.
13180103Sdas *    - Redistributions in binary form must reproduce the above
14180103Sdas *      copyright notice, this list of conditions and the following
15180103Sdas *      disclaimer in the documentation and/or other materials provided
16180103Sdas *      with the distribution.
17180103Sdas *
18180103Sdas * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19180103Sdas * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20180103Sdas * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21180103Sdas * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22180103Sdas * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23180103Sdas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24180103Sdas * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25180103Sdas * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26180103Sdas * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27180103Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28180103Sdas * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29180103Sdas * POSSIBILITY OF SUCH DAMAGE.
30180103Sdas */
31180103Sdas
32180103Sdas#include <sys/cdefs.h>
33180103Sdas__FBSDID("$FreeBSD$");
34180103Sdas
35301808Sngie#include <sys/param.h>
36180103Sdas#include <stdio.h>
37180103Sdas#include <stdlib.h>
38180103Sdas#include <string.h>
39180103Sdas#include <wchar.h>
40180103Sdas
41291840Sngie#include <atf-c.h>
42291840Sngie
43180103Sdasconst char correct[] =
44180103Sdas	"|xx 01 02 03 04\n"
45180103Sdas	"|xx 05 06 07 08\n"
46180103Sdas	"|xx 09 10 11 12\n"
47180103Sdas	"|xx 13 14 15 16\n"
48180103Sdas	"|xx 17 18 19 20\n"
49180103Sdas	"|xx 21 22 23 24\n"
50180103Sdas	"|xx 25 26 27 28\n"
51180103Sdas	"|xx 29 30 31 32\n"
52180103Sdas	"|xx 33 34 35 36\n"
53180103Sdas	"|xx 37 38 39 40\n"
54180103Sdas	"|xx 41 42 43 44\n"
55180103Sdas	"|xx 45 -1 1 -1 1\n";
56180103Sdas
57180103Sdasconst char correct2[] =
58180103Sdas	"b bs BSD";
59291840Sngiestatic char buf[1024];
60291840Sngiestatic wchar_t wbuf1[1024], wbuf2[1024];
61291840Sngiestatic const char *temp;
62180103Sdas
63291840SngieATF_TC_WITHOUT_HEAD(positional_normal);
64291840SngieATF_TC_BODY(positional_normal, tc)
65180103Sdas{
66180103Sdas
67180103Sdas	/* Test positional arguments */
68180103Sdas	snprintf(buf, sizeof buf,
69180103Sdas	    "|xx %1$s %2$s %3$s %4$s\n"
70180103Sdas	    "|xx %5$s %6$s %7$s %8$s\n"
71180103Sdas	    "|xx %9$s %10$s %11$s %12$s\n"
72180103Sdas	    "|xx %13$s %14$s %15$s %16$s\n"
73180103Sdas	    "|xx %17$s %18$s %19$s %20$s\n"
74180103Sdas	    "|xx %21$s %22$s %23$s %24$s\n"
75180103Sdas	    "|xx %25$s %26$s %27$s %28$s\n"
76180103Sdas	    "|xx %29$s %30$s %31$s %32$s\n"
77180103Sdas	    "|xx %33$s %34$s %35$s %36$s\n"
78180103Sdas	    "|xx %37$s %38$s %39$s %40$s\n"
79180103Sdas	    "|xx %41$s %42$s %43$s %44$s\n"
80180103Sdas	    "|xx %45$d %46$ld %47$lld %48$d %49$lld\n",
81180103Sdas	    "01", "02", "03", "04", "05", "06",
82180103Sdas	    "07", "08", "09", "10", "11", "12",
83180103Sdas	    "13", "14", "15", "16", "17", "18",
84180103Sdas	    "19", "20", "21", "22", "23", "24",
85180103Sdas	    "25", "26", "27", "28", "29", "30",
86180103Sdas	    "31", "32", "33", "34", "35", "36",
87180103Sdas	    "37", "38", "39", "40", "41", "42",
88180103Sdas	    "43", "44", 45, -1L, 1LL, -1, 1LL
89180103Sdas	    );
90291840Sngie	ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,
91291840Sngie	    "buffers didn't match");
92291840Sngie}
93180103Sdas
94291840SngieATF_TC_WITHOUT_HEAD(positional_wide);
95291840SngieATF_TC_BODY(positional_wide, tc)
96291840Sngie{
97291840Sngie
98301808Sngie	swprintf(wbuf1, nitems(wbuf1),
99180103Sdas	    L"|xx %1$s %2$s %3$s %4$s\n"
100180103Sdas	    "|xx %5$s %6$s %7$s %8$s\n"
101180103Sdas	    "|xx %9$s %10$s %11$s %12$s\n"
102180103Sdas	    "|xx %13$s %14$s %15$s %16$s\n"
103180103Sdas	    "|xx %17$s %18$s %19$s %20$s\n"
104180103Sdas	    "|xx %21$s %22$s %23$s %24$s\n"
105180103Sdas	    "|xx %25$s %26$s %27$s %28$s\n"
106180103Sdas	    "|xx %29$s %30$s %31$s %32$s\n"
107180103Sdas	    "|xx %33$s %34$s %35$s %36$s\n"
108180103Sdas	    "|xx %37$s %38$s %39$s %40$s\n"
109180103Sdas	    "|xx %41$s %42$s %43$s %44$s\n"
110180103Sdas	    "|xx %45$d %46$ld %47$lld %48$d %49$lld\n",
111180103Sdas	    "01", "02", "03", "04", "05", "06",
112180103Sdas	    "07", "08", "09", "10", "11", "12",
113180103Sdas	    "13", "14", "15", "16", "17", "18",
114180103Sdas	    "19", "20", "21", "22", "23", "24",
115180103Sdas	    "25", "26", "27", "28", "29", "30",
116180103Sdas	    "31", "32", "33", "34", "35", "36",
117180103Sdas	    "37", "38", "39", "40", "41", "42",
118180103Sdas	    "43", "44", 45, -1L, 1LL, -1, 1LL
119180103Sdas	    );
120180103Sdas	temp = correct;
121301808Sngie	mbsrtowcs(wbuf2, &temp, nitems(wbuf2), NULL);
122291840Sngie	ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,
123291840Sngie	    "buffers didn't match");
124291840Sngie}
125180103Sdas
126291840SngieATF_TC_WITHOUT_HEAD(positional_precision);
127291840SngieATF_TC_BODY(positional_precision, tc)
128291840Sngie{
129291840Sngie
130180103Sdas	snprintf(buf, sizeof buf, "%2$.*4$s %2$.*3$s %1$s",
131180103Sdas		 "BSD", "bsd", 2, 1);
132291840Sngie	ATF_REQUIRE_MSG(strcmp(buf, correct2) == 0,
133291840Sngie	    "buffers didn't match");
134291840Sngie}
135180103Sdas
136291840SngieATF_TC_WITHOUT_HEAD(positional_precision_wide);
137291840SngieATF_TC_BODY(positional_precision_wide, tc)
138291840Sngie{
139291840Sngie
140180103Sdas	swprintf(wbuf1, sizeof buf, L"%2$.*4$s %2$.*3$s %1$s",
141180103Sdas		 "BSD", "bsd", 2, 1);
142180103Sdas	temp = correct2;
143301808Sngie	mbsrtowcs(wbuf2, &temp, nitems(wbuf2), NULL);
144291840Sngie	ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,
145291840Sngie	    "buffers didn't match");
146291840Sngie}
147180103Sdas
148291840SngieATF_TP_ADD_TCS(tp)
149291840Sngie{
150291840Sngie
151291840Sngie	ATF_TP_ADD_TC(tp, positional_normal);
152291840Sngie	ATF_TP_ADD_TC(tp, positional_wide);
153291840Sngie	ATF_TP_ADD_TC(tp, positional_precision);
154291840Sngie	ATF_TP_ADD_TC(tp, positional_precision_wide);
155291840Sngie
156291840Sngie	return (atf_no_error());
157180103Sdas}
158