xref: /trueos/tools/regression/lib/libc/stdio/test-print-positional.c (revision d36ce0f109efab721a127c6aa11db35ca9074797)
1 /*      $OpenBSD: sprintf_test.c,v 1.3 2004/09/16 20:22:26 otto Exp $ */
2 
3 /*
4  * Copyright (c) 2003 Theo de Raadt
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  *    - Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *    - Redistributions in binary form must reproduce the above
14  *      copyright notice, this list of conditions and the following
15  *      disclaimer in the documentation and/or other materials provided
16  *      with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <wchar.h>
39 
40 const char correct[] =
41 	"|xx 01 02 03 04\n"
42 	"|xx 05 06 07 08\n"
43 	"|xx 09 10 11 12\n"
44 	"|xx 13 14 15 16\n"
45 	"|xx 17 18 19 20\n"
46 	"|xx 21 22 23 24\n"
47 	"|xx 25 26 27 28\n"
48 	"|xx 29 30 31 32\n"
49 	"|xx 33 34 35 36\n"
50 	"|xx 37 38 39 40\n"
51 	"|xx 41 42 43 44\n"
52 	"|xx 45 -1 1 -1 1\n";
53 
54 const char correct2[] =
55 	"b bs BSD";
56 
57 int
main(int argc,char * argv[])58 main(int argc, char *argv[])
59 {
60 	char buf[1024];
61 	wchar_t wbuf1[1024], wbuf2[1024];
62 	const char *temp;
63 
64 	printf("1..4\n");
65 
66 	/* Test positional arguments */
67 	snprintf(buf, sizeof buf,
68 	    "|xx %1$s %2$s %3$s %4$s\n"
69 	    "|xx %5$s %6$s %7$s %8$s\n"
70 	    "|xx %9$s %10$s %11$s %12$s\n"
71 	    "|xx %13$s %14$s %15$s %16$s\n"
72 	    "|xx %17$s %18$s %19$s %20$s\n"
73 	    "|xx %21$s %22$s %23$s %24$s\n"
74 	    "|xx %25$s %26$s %27$s %28$s\n"
75 	    "|xx %29$s %30$s %31$s %32$s\n"
76 	    "|xx %33$s %34$s %35$s %36$s\n"
77 	    "|xx %37$s %38$s %39$s %40$s\n"
78 	    "|xx %41$s %42$s %43$s %44$s\n"
79 	    "|xx %45$d %46$ld %47$lld %48$d %49$lld\n",
80 	    "01", "02", "03", "04", "05", "06",
81 	    "07", "08", "09", "10", "11", "12",
82 	    "13", "14", "15", "16", "17", "18",
83 	    "19", "20", "21", "22", "23", "24",
84 	    "25", "26", "27", "28", "29", "30",
85 	    "31", "32", "33", "34", "35", "36",
86 	    "37", "38", "39", "40", "41", "42",
87 	    "43", "44", 45, -1L, 1LL, -1, 1LL
88 	    );
89 	printf("%sok 1 - print-positional normal\n",
90 	       strcmp(buf, correct) == 0 ? "" : "not ");
91 
92 	swprintf(wbuf1, sizeof wbuf1,
93 	    L"|xx %1$s %2$s %3$s %4$s\n"
94 	    "|xx %5$s %6$s %7$s %8$s\n"
95 	    "|xx %9$s %10$s %11$s %12$s\n"
96 	    "|xx %13$s %14$s %15$s %16$s\n"
97 	    "|xx %17$s %18$s %19$s %20$s\n"
98 	    "|xx %21$s %22$s %23$s %24$s\n"
99 	    "|xx %25$s %26$s %27$s %28$s\n"
100 	    "|xx %29$s %30$s %31$s %32$s\n"
101 	    "|xx %33$s %34$s %35$s %36$s\n"
102 	    "|xx %37$s %38$s %39$s %40$s\n"
103 	    "|xx %41$s %42$s %43$s %44$s\n"
104 	    "|xx %45$d %46$ld %47$lld %48$d %49$lld\n",
105 	    "01", "02", "03", "04", "05", "06",
106 	    "07", "08", "09", "10", "11", "12",
107 	    "13", "14", "15", "16", "17", "18",
108 	    "19", "20", "21", "22", "23", "24",
109 	    "25", "26", "27", "28", "29", "30",
110 	    "31", "32", "33", "34", "35", "36",
111 	    "37", "38", "39", "40", "41", "42",
112 	    "43", "44", 45, -1L, 1LL, -1, 1LL
113 	    );
114 	temp = correct;
115 	mbsrtowcs(wbuf2, &temp, sizeof wbuf2, NULL);
116 	printf("%sok 2 - print-positional wide\n",
117 	       wcscmp(wbuf1, wbuf2) == 0 ? "" : "not ");
118 
119 	snprintf(buf, sizeof buf, "%2$.*4$s %2$.*3$s %1$s",
120 		 "BSD", "bsd", 2, 1);
121 	printf("%sok 3 - print-positional precision\n",
122 	       strcmp(buf, correct2) == 0 ? "" : "not ");
123 
124 	swprintf(wbuf1, sizeof buf, L"%2$.*4$s %2$.*3$s %1$s",
125 		 "BSD", "bsd", 2, 1);
126 	temp = correct2;
127 	mbsrtowcs(wbuf2, &temp, sizeof wbuf2, NULL);
128 	printf("%sok 4 - print-positional precision wide\n",
129 	       wcscmp(wbuf1, wbuf2) == 0 ? "" : "not ");
130 
131 	exit(0);
132 }
133