xref: /netbsd-src/tests/lib/libc/locale/t_sprintf.c (revision b19b969600c0af1bbdd2bae13f7ed4d88a2eccb4)
1 /* $NetBSD: t_sprintf.c,v 1.4 2017/11/23 23:47:09 kre Exp $ */
2 
3 /*-
4  * Copyright (c) 2017 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Konrad Schroder.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN 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 __COPYRIGHT("@(#) Copyright (c) 2017\
34  The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_sprintf.c,v 1.4 2017/11/23 23:47:09 kre Exp $");
36 
37 #include <locale.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <limits.h>
42 #include <ctype.h>
43 
44 #include <atf-c.h>
45 
46 static struct test {
47 	const char *locale;
48 	const int int_value;
49 	const char *int_result;
50 	const char *int_input;
51 	const double double_value;
52 	const char *double_result;
53 	const char *double_input;
54 } tests[] = {
55 	{
56 		"en_US.UTF-8",
57 		-12345,
58 		"-12,345",
59 		"-12345",
60 		-12345.6789,
61 		"-12,345.678900",
62 		"-12345.678900",
63 	}, {
64 		"fr_FR.ISO8859-1",
65 		-12345,
66 		"-12\240345",
67 		"-12345",
68 		-12345.6789,
69 		"-12\240345,678900",
70 		"-12345,678900",
71 	}, {
72 		"it_IT.ISO8859-1",
73 		-12345,
74 		"-12.345",
75 		"-12345",
76 		-12345.6789,
77 		"-12.345,678900",
78 		"-12345,678900",
79 	}, {
80 		"POSIX",
81 		/*
82 		 * POSIX-1.2008 specifies that the C and POSIX
83 		 * locales shall be identical (section 7.2) and
84 		 * that the POSIX locale shall have an empty
85 		 * thousands separator and "<period>" as its
86 		 * decimal point (section 7.3.4).  *printf
87 		 * ought to honor these settings.
88 		 */
89 		-12345,
90 		"-12345",
91 		"-12345",
92 		-12345.6789,
93 		"-12345.678900",
94 		"-12345.678900",
95 	}, {
96 		NULL,
97 		0,
98 		NULL,
99 		NULL,
100 		0.0,
101 		NULL,
102 		NULL,
103 	}
104 };
105 
106 static void
107 h_sprintf(const struct test *t)
108 {
109 	char buf[1024];
110 
111 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
112 	printf("Trying locale %s...\n", t->locale);
113 	ATF_REQUIRE(setlocale(LC_NUMERIC, t->locale) != NULL);
114 	printf("Using locale: %s\n", setlocale(LC_ALL, NULL));
115 
116 	sprintf(buf, "%'f", t->double_value);
117 	ATF_REQUIRE_STREQ(buf, t->double_result);
118 
119 	sprintf(buf, "%'d", t->int_value);
120 	ATF_REQUIRE_STREQ(buf, t->int_result);
121 
122         atf_tc_expect_pass();
123 }
124 
125 static void
126 h_strto(const struct test *t)
127 {
128 	double d;
129 
130 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
131 	printf("Trying locale %s...\n", t->locale);
132 	ATF_REQUIRE(setlocale(LC_NUMERIC, t->locale) != NULL);
133 
134 	ATF_REQUIRE_EQ((int)strtol(t->int_input, NULL, 10), t->int_value);
135 	d = strtod(t->double_input, NULL);
136 	ATF_REQUIRE_EQ_MSG(d, t->double_value, "In %s: "
137 	    "strtod(t->double_input[%s], NULL)[%g] != t->double_value[%g]",
138 	    t->locale, t->double_input, d, t->double_value);
139 }
140 
141 static void
142 h_sscanf(const struct test *t)
143 {
144 	int int_reported;
145 	double double_reported;
146 
147 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
148 	printf("Trying locale %s...\n", t->locale);
149 	ATF_REQUIRE(setlocale(LC_NUMERIC, t->locale) != NULL);
150 
151 	sscanf(t->int_input, "%d", &int_reported);
152 	ATF_REQUIRE_EQ(int_reported, t->int_value);
153 	sscanf(t->double_input, "%lf", &double_reported);
154 	ATF_REQUIRE_EQ(double_reported, t->double_value);
155 }
156 
157 ATF_TC(sprintf);
158 ATF_TC_HEAD(sprintf, tc)
159 {
160 	atf_tc_set_md_var(tc, "descr",
161 		"Checks sprintf %%'d and %%'f under diferent locales");
162 }
163 ATF_TC_BODY(sprintf, tc)
164 {
165 	struct test *t;
166 
167 	for (t = &tests[0]; t->locale != NULL; ++t)
168 		h_sprintf(t);
169 }
170 
171 ATF_TC(strto);
172 ATF_TC_HEAD(strto, tc)
173 {
174 	atf_tc_set_md_var(tc, "descr",
175 		"Checks strtol and strtod under diferent locales");
176 }
177 ATF_TC_BODY(strto, tc)
178 {
179 	struct test *t;
180 
181 	for (t = &tests[0]; t->locale != NULL; ++t)
182 		h_strto(t);
183 }
184 
185 ATF_TC(sscanf);
186 ATF_TC_HEAD(sscanf, tc)
187 {
188 	atf_tc_set_md_var(tc, "descr",
189 		"Checks sscanf under diferent locales");
190 }
191 ATF_TC_BODY(sscanf, tc)
192 {
193 	struct test *t;
194 
195 	for (t = &tests[0]; t->locale != NULL; ++t)
196 		h_sscanf(t);
197 }
198 
199 ATF_TP_ADD_TCS(tp)
200 {
201 
202 	ATF_TP_ADD_TC(tp, sprintf);
203 	ATF_TP_ADD_TC(tp, sscanf);
204 	ATF_TP_ADD_TC(tp, strto);
205 
206 	return atf_no_error();
207 }
208