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: releng/10.2/tools/regression/lib/libc/stdio/test-print-positional.c 180103 2008-06-29 21:03:14Z das $");
34180103Sdas
35180103Sdas#include <stdio.h>
36180103Sdas#include <stdlib.h>
37180103Sdas#include <string.h>
38180103Sdas#include <wchar.h>
39180103Sdas
40180103Sdasconst char correct[] =
41180103Sdas	"|xx 01 02 03 04\n"
42180103Sdas	"|xx 05 06 07 08\n"
43180103Sdas	"|xx 09 10 11 12\n"
44180103Sdas	"|xx 13 14 15 16\n"
45180103Sdas	"|xx 17 18 19 20\n"
46180103Sdas	"|xx 21 22 23 24\n"
47180103Sdas	"|xx 25 26 27 28\n"
48180103Sdas	"|xx 29 30 31 32\n"
49180103Sdas	"|xx 33 34 35 36\n"
50180103Sdas	"|xx 37 38 39 40\n"
51180103Sdas	"|xx 41 42 43 44\n"
52180103Sdas	"|xx 45 -1 1 -1 1\n";
53180103Sdas
54180103Sdasconst char correct2[] =
55180103Sdas	"b bs BSD";
56180103Sdas
57180103Sdasint
58180103Sdasmain(int argc, char *argv[])
59180103Sdas{
60180103Sdas	char buf[1024];
61180103Sdas	wchar_t wbuf1[1024], wbuf2[1024];
62180103Sdas	const char *temp;
63180103Sdas
64180103Sdas	printf("1..4\n");
65180103Sdas
66180103Sdas	/* Test positional arguments */
67180103Sdas	snprintf(buf, sizeof buf,
68180103Sdas	    "|xx %1$s %2$s %3$s %4$s\n"
69180103Sdas	    "|xx %5$s %6$s %7$s %8$s\n"
70180103Sdas	    "|xx %9$s %10$s %11$s %12$s\n"
71180103Sdas	    "|xx %13$s %14$s %15$s %16$s\n"
72180103Sdas	    "|xx %17$s %18$s %19$s %20$s\n"
73180103Sdas	    "|xx %21$s %22$s %23$s %24$s\n"
74180103Sdas	    "|xx %25$s %26$s %27$s %28$s\n"
75180103Sdas	    "|xx %29$s %30$s %31$s %32$s\n"
76180103Sdas	    "|xx %33$s %34$s %35$s %36$s\n"
77180103Sdas	    "|xx %37$s %38$s %39$s %40$s\n"
78180103Sdas	    "|xx %41$s %42$s %43$s %44$s\n"
79180103Sdas	    "|xx %45$d %46$ld %47$lld %48$d %49$lld\n",
80180103Sdas	    "01", "02", "03", "04", "05", "06",
81180103Sdas	    "07", "08", "09", "10", "11", "12",
82180103Sdas	    "13", "14", "15", "16", "17", "18",
83180103Sdas	    "19", "20", "21", "22", "23", "24",
84180103Sdas	    "25", "26", "27", "28", "29", "30",
85180103Sdas	    "31", "32", "33", "34", "35", "36",
86180103Sdas	    "37", "38", "39", "40", "41", "42",
87180103Sdas	    "43", "44", 45, -1L, 1LL, -1, 1LL
88180103Sdas	    );
89180103Sdas	printf("%sok 1 - print-positional normal\n",
90180103Sdas	       strcmp(buf, correct) == 0 ? "" : "not ");
91180103Sdas
92180103Sdas	swprintf(wbuf1, sizeof wbuf1,
93180103Sdas	    L"|xx %1$s %2$s %3$s %4$s\n"
94180103Sdas	    "|xx %5$s %6$s %7$s %8$s\n"
95180103Sdas	    "|xx %9$s %10$s %11$s %12$s\n"
96180103Sdas	    "|xx %13$s %14$s %15$s %16$s\n"
97180103Sdas	    "|xx %17$s %18$s %19$s %20$s\n"
98180103Sdas	    "|xx %21$s %22$s %23$s %24$s\n"
99180103Sdas	    "|xx %25$s %26$s %27$s %28$s\n"
100180103Sdas	    "|xx %29$s %30$s %31$s %32$s\n"
101180103Sdas	    "|xx %33$s %34$s %35$s %36$s\n"
102180103Sdas	    "|xx %37$s %38$s %39$s %40$s\n"
103180103Sdas	    "|xx %41$s %42$s %43$s %44$s\n"
104180103Sdas	    "|xx %45$d %46$ld %47$lld %48$d %49$lld\n",
105180103Sdas	    "01", "02", "03", "04", "05", "06",
106180103Sdas	    "07", "08", "09", "10", "11", "12",
107180103Sdas	    "13", "14", "15", "16", "17", "18",
108180103Sdas	    "19", "20", "21", "22", "23", "24",
109180103Sdas	    "25", "26", "27", "28", "29", "30",
110180103Sdas	    "31", "32", "33", "34", "35", "36",
111180103Sdas	    "37", "38", "39", "40", "41", "42",
112180103Sdas	    "43", "44", 45, -1L, 1LL, -1, 1LL
113180103Sdas	    );
114180103Sdas	temp = correct;
115180103Sdas	mbsrtowcs(wbuf2, &temp, sizeof wbuf2, NULL);
116180103Sdas	printf("%sok 2 - print-positional wide\n",
117180103Sdas	       wcscmp(wbuf1, wbuf2) == 0 ? "" : "not ");
118180103Sdas
119180103Sdas	snprintf(buf, sizeof buf, "%2$.*4$s %2$.*3$s %1$s",
120180103Sdas		 "BSD", "bsd", 2, 1);
121180103Sdas	printf("%sok 3 - print-positional precision\n",
122180103Sdas	       strcmp(buf, correct2) == 0 ? "" : "not ");
123180103Sdas
124180103Sdas	swprintf(wbuf1, sizeof buf, L"%2$.*4$s %2$.*3$s %1$s",
125180103Sdas		 "BSD", "bsd", 2, 1);
126180103Sdas	temp = correct2;
127180103Sdas	mbsrtowcs(wbuf2, &temp, sizeof wbuf2, NULL);
128180103Sdas	printf("%sok 4 - print-positional precision wide\n",
129180103Sdas	       wcscmp(wbuf1, wbuf2) == 0 ? "" : "not ");
130180103Sdas
131180103Sdas	exit(0);
132180103Sdas}
133