]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - lib/libc/tests/stdio/print_positional_test.c
MFC r321333:
[FreeBSD/stable/10.git] / lib / libc / tests / stdio / print_positional_test.c
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 <sys/param.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <wchar.h>
40
41 #include <atf-c.h>
42
43 const char correct[] =
44         "|xx 01 02 03 04\n"
45         "|xx 05 06 07 08\n"
46         "|xx 09 10 11 12\n"
47         "|xx 13 14 15 16\n"
48         "|xx 17 18 19 20\n"
49         "|xx 21 22 23 24\n"
50         "|xx 25 26 27 28\n"
51         "|xx 29 30 31 32\n"
52         "|xx 33 34 35 36\n"
53         "|xx 37 38 39 40\n"
54         "|xx 41 42 43 44\n"
55         "|xx 45 -1 1 -1 1\n";
56
57 const char correct2[] =
58         "b bs BSD";
59 static char buf[1024];
60 static wchar_t wbuf1[1024], wbuf2[1024];
61 static const char *temp;
62
63 ATF_TC_WITHOUT_HEAD(positional_normal);
64 ATF_TC_BODY(positional_normal, tc)
65 {
66
67         /* Test positional arguments */
68         snprintf(buf, sizeof buf,
69             "|xx %1$s %2$s %3$s %4$s\n"
70             "|xx %5$s %6$s %7$s %8$s\n"
71             "|xx %9$s %10$s %11$s %12$s\n"
72             "|xx %13$s %14$s %15$s %16$s\n"
73             "|xx %17$s %18$s %19$s %20$s\n"
74             "|xx %21$s %22$s %23$s %24$s\n"
75             "|xx %25$s %26$s %27$s %28$s\n"
76             "|xx %29$s %30$s %31$s %32$s\n"
77             "|xx %33$s %34$s %35$s %36$s\n"
78             "|xx %37$s %38$s %39$s %40$s\n"
79             "|xx %41$s %42$s %43$s %44$s\n"
80             "|xx %45$d %46$ld %47$lld %48$d %49$lld\n",
81             "01", "02", "03", "04", "05", "06",
82             "07", "08", "09", "10", "11", "12",
83             "13", "14", "15", "16", "17", "18",
84             "19", "20", "21", "22", "23", "24",
85             "25", "26", "27", "28", "29", "30",
86             "31", "32", "33", "34", "35", "36",
87             "37", "38", "39", "40", "41", "42",
88             "43", "44", 45, -1L, 1LL, -1, 1LL
89             );
90         ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,
91             "buffers didn't match");
92 }
93
94 ATF_TC_WITHOUT_HEAD(positional_wide);
95 ATF_TC_BODY(positional_wide, tc)
96 {
97
98         swprintf(wbuf1, nitems(wbuf1),
99             L"|xx %1$s %2$s %3$s %4$s\n"
100             "|xx %5$s %6$s %7$s %8$s\n"
101             "|xx %9$s %10$s %11$s %12$s\n"
102             "|xx %13$s %14$s %15$s %16$s\n"
103             "|xx %17$s %18$s %19$s %20$s\n"
104             "|xx %21$s %22$s %23$s %24$s\n"
105             "|xx %25$s %26$s %27$s %28$s\n"
106             "|xx %29$s %30$s %31$s %32$s\n"
107             "|xx %33$s %34$s %35$s %36$s\n"
108             "|xx %37$s %38$s %39$s %40$s\n"
109             "|xx %41$s %42$s %43$s %44$s\n"
110             "|xx %45$d %46$ld %47$lld %48$d %49$lld\n",
111             "01", "02", "03", "04", "05", "06",
112             "07", "08", "09", "10", "11", "12",
113             "13", "14", "15", "16", "17", "18",
114             "19", "20", "21", "22", "23", "24",
115             "25", "26", "27", "28", "29", "30",
116             "31", "32", "33", "34", "35", "36",
117             "37", "38", "39", "40", "41", "42",
118             "43", "44", 45, -1L, 1LL, -1, 1LL
119             );
120         temp = correct;
121         mbsrtowcs(wbuf2, &temp, nitems(wbuf2), NULL);
122         ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,
123             "buffers didn't match");
124 }
125
126 ATF_TC_WITHOUT_HEAD(positional_precision);
127 ATF_TC_BODY(positional_precision, tc)
128 {
129
130         snprintf(buf, sizeof buf, "%2$.*4$s %2$.*3$s %1$s",
131                  "BSD", "bsd", 2, 1);
132         ATF_REQUIRE_MSG(strcmp(buf, correct2) == 0,
133             "buffers didn't match");
134 }
135
136 ATF_TC_WITHOUT_HEAD(positional_precision_wide);
137 ATF_TC_BODY(positional_precision_wide, tc)
138 {
139
140         swprintf(wbuf1, sizeof buf, L"%2$.*4$s %2$.*3$s %1$s",
141                  "BSD", "bsd", 2, 1);
142         temp = correct2;
143         mbsrtowcs(wbuf2, &temp, nitems(wbuf2), NULL);
144         ATF_REQUIRE_MSG(wcscmp(wbuf1, wbuf2) == 0,
145             "buffers didn't match");
146 }
147
148 ATF_TP_ADD_TCS(tp)
149 {
150
151         ATF_TP_ADD_TC(tp, positional_normal);
152         ATF_TP_ADD_TC(tp, positional_wide);
153         ATF_TP_ADD_TC(tp, positional_precision);
154         ATF_TP_ADD_TC(tp, positional_precision_wide);
155
156         return (atf_no_error());
157 }