xref: /netbsd-src/tests/lib/libc/locale/t_sprintf.c (revision adbde1f90007dcbbba222bd12b3ad39e535dc73d)
1 /* $NetBSD: t_sprintf.c,v 1.3 2017/07/12 17:32:51 perseant 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.3 2017/07/12 17:32:51 perseant 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 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
129 	printf("Trying locale %s...\n", t->locale);
130 	ATF_REQUIRE(setlocale(LC_NUMERIC, t->locale) != NULL);
131 
132 	ATF_REQUIRE_EQ((int)strtol(t->int_input, NULL, 10), t->int_value);
133 	ATF_REQUIRE_EQ(strtod(t->double_input, NULL), t->double_value);
134 }
135 
136 static void
137 h_sscanf(const struct test *t)
138 {
139 	int int_reported;
140 	double double_reported;
141 
142 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
143 	printf("Trying locale %s...\n", t->locale);
144 	ATF_REQUIRE(setlocale(LC_NUMERIC, t->locale) != NULL);
145 
146 	sscanf(t->int_input, "%d", &int_reported);
147 	ATF_REQUIRE_EQ(int_reported, t->int_value);
148 	sscanf(t->double_input, "%lf", &double_reported);
149 	ATF_REQUIRE_EQ(double_reported, t->double_value);
150 }
151 
152 ATF_TC(sprintf);
153 ATF_TC_HEAD(sprintf, tc)
154 {
155 	atf_tc_set_md_var(tc, "descr",
156 		"Checks sprintf %%'d and %%'f under diferent locales");
157 }
158 ATF_TC_BODY(sprintf, tc)
159 {
160 	struct test *t;
161 
162 	for (t = &tests[0]; t->locale != NULL; ++t)
163 		h_sprintf(t);
164 }
165 
166 ATF_TC(strto);
167 ATF_TC_HEAD(strto, tc)
168 {
169 	atf_tc_set_md_var(tc, "descr",
170 		"Checks strtol and strtod under diferent locales");
171 }
172 ATF_TC_BODY(strto, tc)
173 {
174 	struct test *t;
175 
176 	for (t = &tests[0]; t->locale != NULL; ++t)
177 		h_strto(t);
178 }
179 
180 ATF_TC(sscanf);
181 ATF_TC_HEAD(sscanf, tc)
182 {
183 	atf_tc_set_md_var(tc, "descr",
184 		"Checks sscanf under diferent locales");
185 }
186 ATF_TC_BODY(sscanf, tc)
187 {
188 	struct test *t;
189 
190 	for (t = &tests[0]; t->locale != NULL; ++t)
191 		h_sscanf(t);
192 }
193 
194 ATF_TP_ADD_TCS(tp)
195 {
196 
197 	ATF_TP_ADD_TC(tp, sprintf);
198 	ATF_TP_ADD_TC(tp, sscanf);
199 	ATF_TP_ADD_TC(tp, strto);
200 
201 	return atf_no_error();
202 }
203