1*02698c5bSandvar /* $NetBSD: t_sprintf.c,v 1.8 2021/08/02 17:41:07 andvar Exp $ */
2ce11903fSperseant
3ce11903fSperseant /*-
4ce11903fSperseant * Copyright (c) 2017 The NetBSD Foundation, Inc.
5ce11903fSperseant * All rights reserved.
6ce11903fSperseant *
7ce11903fSperseant * This code is derived from software contributed to The NetBSD Foundation
88b7adb7bSperseant * by Konrad Schroder.
9ce11903fSperseant *
10ce11903fSperseant * Redistribution and use in source and binary forms, with or without
11ce11903fSperseant * modification, are permitted provided that the following conditions
12ce11903fSperseant * are met:
13ce11903fSperseant * 1. Redistributions of source code must retain the above copyright
14ce11903fSperseant * notice, this list of conditions and the following disclaimer.
15ce11903fSperseant * 2. Redistributions in binary form must reproduce the above copyright
16ce11903fSperseant * notice, this list of conditions and the following disclaimer in the
17ce11903fSperseant * documentation and/or other materials provided with the distribution.
18ce11903fSperseant *
19ce11903fSperseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20ce11903fSperseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21ce11903fSperseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22ce11903fSperseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23ce11903fSperseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24ce11903fSperseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25ce11903fSperseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26ce11903fSperseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27ce11903fSperseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ce11903fSperseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29ce11903fSperseant * POSSIBILITY OF SUCH DAMAGE.
30ce11903fSperseant */
31ce11903fSperseant
32ce11903fSperseant #include <sys/cdefs.h>
33ce11903fSperseant __COPYRIGHT("@(#) Copyright (c) 2017\
34ce11903fSperseant The NetBSD Foundation, inc. All rights reserved.");
35*02698c5bSandvar __RCSID("$NetBSD: t_sprintf.c,v 1.8 2021/08/02 17:41:07 andvar Exp $");
36ce11903fSperseant
37ce11903fSperseant #include <locale.h>
38fa3a535cSkre #include <math.h>
39ce11903fSperseant #include <stdio.h>
40ce11903fSperseant #include <stdlib.h>
41ce11903fSperseant #include <string.h>
42ce11903fSperseant #include <limits.h>
43ce11903fSperseant #include <ctype.h>
44ce11903fSperseant
45ce11903fSperseant #include <atf-c.h>
46ce11903fSperseant
47ce11903fSperseant static struct test {
48ce11903fSperseant const char *locale;
49ce11903fSperseant const int int_value;
50ce11903fSperseant const char *int_result;
51ce11903fSperseant const char *int_input;
52ce11903fSperseant const double double_value;
53ce11903fSperseant const char *double_result;
54ce11903fSperseant const char *double_input;
55ce11903fSperseant } tests[] = {
56ce11903fSperseant {
57ce11903fSperseant "en_US.UTF-8",
58ce11903fSperseant -12345,
59ce11903fSperseant "-12,345",
60ce11903fSperseant "-12345",
61ce11903fSperseant -12345.6789,
62ce11903fSperseant "-12,345.678900",
63ce11903fSperseant "-12345.678900",
64ce11903fSperseant }, {
65ce11903fSperseant "fr_FR.ISO8859-1",
66ce11903fSperseant -12345,
67ce11903fSperseant "-12\240345",
68ce11903fSperseant "-12345",
69ce11903fSperseant -12345.6789,
70ce11903fSperseant "-12\240345,678900",
71ce11903fSperseant "-12345,678900",
72ce11903fSperseant }, {
738b7adb7bSperseant "it_IT.ISO8859-1",
748b7adb7bSperseant -12345,
758b7adb7bSperseant "-12.345",
768b7adb7bSperseant "-12345",
778b7adb7bSperseant -12345.6789,
788b7adb7bSperseant "-12.345,678900",
798b7adb7bSperseant "-12345,678900",
808b7adb7bSperseant }, {
818b7adb7bSperseant "POSIX",
828b7adb7bSperseant /*
838b7adb7bSperseant * POSIX-1.2008 specifies that the C and POSIX
848b7adb7bSperseant * locales shall be identical (section 7.2) and
858b7adb7bSperseant * that the POSIX locale shall have an empty
868b7adb7bSperseant * thousands separator and "<period>" as its
878b7adb7bSperseant * decimal point (section 7.3.4). *printf
888b7adb7bSperseant * ought to honor these settings.
898b7adb7bSperseant */
908b7adb7bSperseant -12345,
918b7adb7bSperseant "-12345",
928b7adb7bSperseant "-12345",
938b7adb7bSperseant -12345.6789,
948b7adb7bSperseant "-12345.678900",
958b7adb7bSperseant "-12345.678900",
968b7adb7bSperseant }, {
97ce11903fSperseant NULL,
98ce11903fSperseant 0,
99ce11903fSperseant NULL,
100ce11903fSperseant NULL,
101ce11903fSperseant 0.0,
102ce11903fSperseant NULL,
103ce11903fSperseant NULL,
104ce11903fSperseant }
105ce11903fSperseant };
106ce11903fSperseant
107ce11903fSperseant static void
h_sprintf(const struct test * t)108ce11903fSperseant h_sprintf(const struct test *t)
109ce11903fSperseant {
110ce11903fSperseant char buf[1024];
111ce11903fSperseant
112ce11903fSperseant ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
113ce11903fSperseant printf("Trying locale %s...\n", t->locale);
114ce11903fSperseant ATF_REQUIRE(setlocale(LC_NUMERIC, t->locale) != NULL);
1158b7adb7bSperseant printf("Using locale: %s\n", setlocale(LC_ALL, NULL));
1168b7adb7bSperseant
117ce11903fSperseant sprintf(buf, "%'f", t->double_value);
118ce11903fSperseant ATF_REQUIRE_STREQ(buf, t->double_result);
119ce11903fSperseant
120ce11903fSperseant sprintf(buf, "%'d", t->int_value);
121ce11903fSperseant ATF_REQUIRE_STREQ(buf, t->int_result);
1228b7adb7bSperseant
1238b7adb7bSperseant atf_tc_expect_pass();
124ce11903fSperseant }
125ce11903fSperseant
126ce11903fSperseant static void
h_strto(const struct test * t)127ce11903fSperseant h_strto(const struct test *t)
128ce11903fSperseant {
129fa3a535cSkre double d, diff;
130b19b9696Skre
131ce11903fSperseant ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
132ce11903fSperseant printf("Trying locale %s...\n", t->locale);
133ce11903fSperseant ATF_REQUIRE(setlocale(LC_NUMERIC, t->locale) != NULL);
134ce11903fSperseant
135ce11903fSperseant ATF_REQUIRE_EQ((int)strtol(t->int_input, NULL, 10), t->int_value);
1363772e94eSkre
1373772e94eSkre /*
1383772e94eSkre * Note that the C standard permits function values to be
1393772e94eSkre * returned with more precision than is expected by (floating)
1403772e94eSkre * data types, and on i386 (and potentially other implementations)
1413772e94eSkre * that is exactly what happens, meaning that the result from
1423772e94eSkre * strtod() is not identical to the expected value - it turns out
1433772e94eSkre * that it is the same if the value is constrained to the number
1443772e94eSkre * of mantissa bits in a double (so the %a values printed below
1453772e94eSkre * show the exact same bit patterns) and on i386 -ffloat-store
1463772e94eSkre * will cause gcc to constrain the result that way, but nothing
1473772e94eSkre * demands that be true, so instead, we simply test that the
1483772e94eSkre * value returned is very very close to that expected.
1493772e94eSkre *
1503772e94eSkre * 1e-12 is chosen as the allowable delta, as we know (from
1513772e94eSkre * the data in the "struct test" earlier in this file) that
1523772e94eSkre * its magnitude is ~ 10^5, with values of that magnitude,
1533772e94eSkre * 10^-12 difference is a 10^-17 relative difference, and
1543772e94eSkre * with a 56 bit mantissa (standard IEEE "double") a difference
1553772e94eSkre * that small vanishes (requires at least 57 mantissa bits to
1563772e94eSkre * be representable). If the data values were to change, then
1573772e94eSkre * so might this delta (if they were not all the same, we would
1583772e94eSkre * move the delta into the struct rather than having it a constant
1593772e94eSkre * here.).
1603772e94eSkre *
1613772e94eSkre * Finally, note that our purpose here is not to test floating
1623772e94eSkre * point arithmetic, we're testing locale dependent string to
1633772e94eSkre * binary conversions.
1643772e94eSkre */
1653772e94eSkre
1663772e94eSkre d = (double)strtod(t->double_input, NULL);
16752a97812Skre diff = fabs(d - t->double_value);
1683772e94eSkre if (diff >= 1e-12)
16952a97812Skre ATF_REQUIRE_EQ_MSG(d, t->double_value, "In %s: "
17052a97812Skre "d=strtod(t->double_input[%s], NULL)[%.12g = %a] != "
1713772e94eSkre "t->double_value[%.12g = %a]: diff=%g", t->locale,
1723772e94eSkre t->double_input, d, d, t->double_value, t->double_value,
1733772e94eSkre diff);
174ce11903fSperseant }
175ce11903fSperseant
176ce11903fSperseant static void
h_sscanf(const struct test * t)177ce11903fSperseant h_sscanf(const struct test *t)
178ce11903fSperseant {
179ce11903fSperseant int int_reported;
180ce11903fSperseant double double_reported;
181ce11903fSperseant
182ce11903fSperseant ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
183ce11903fSperseant printf("Trying locale %s...\n", t->locale);
184ce11903fSperseant ATF_REQUIRE(setlocale(LC_NUMERIC, t->locale) != NULL);
185ce11903fSperseant
186ce11903fSperseant sscanf(t->int_input, "%d", &int_reported);
187ce11903fSperseant ATF_REQUIRE_EQ(int_reported, t->int_value);
188ce11903fSperseant sscanf(t->double_input, "%lf", &double_reported);
189ce11903fSperseant ATF_REQUIRE_EQ(double_reported, t->double_value);
190ce11903fSperseant }
191ce11903fSperseant
192ce11903fSperseant ATF_TC(sprintf);
ATF_TC_HEAD(sprintf,tc)193ce11903fSperseant ATF_TC_HEAD(sprintf, tc)
194ce11903fSperseant {
195ce11903fSperseant atf_tc_set_md_var(tc, "descr",
196*02698c5bSandvar "Checks sprintf %%'d and %%'f under different locales");
197ce11903fSperseant }
ATF_TC_BODY(sprintf,tc)198ce11903fSperseant ATF_TC_BODY(sprintf, tc)
199ce11903fSperseant {
200ce11903fSperseant struct test *t;
201ce11903fSperseant
202ce11903fSperseant for (t = &tests[0]; t->locale != NULL; ++t)
203ce11903fSperseant h_sprintf(t);
204ce11903fSperseant }
205ce11903fSperseant
206ce11903fSperseant ATF_TC(strto);
ATF_TC_HEAD(strto,tc)207ce11903fSperseant ATF_TC_HEAD(strto, tc)
208ce11903fSperseant {
209ce11903fSperseant atf_tc_set_md_var(tc, "descr",
210*02698c5bSandvar "Checks strtol and strtod under different locales");
211ce11903fSperseant }
ATF_TC_BODY(strto,tc)212ce11903fSperseant ATF_TC_BODY(strto, tc)
213ce11903fSperseant {
214ce11903fSperseant struct test *t;
215ce11903fSperseant
216ce11903fSperseant for (t = &tests[0]; t->locale != NULL; ++t)
217ce11903fSperseant h_strto(t);
218ce11903fSperseant }
219ce11903fSperseant
220ce11903fSperseant ATF_TC(sscanf);
ATF_TC_HEAD(sscanf,tc)221ce11903fSperseant ATF_TC_HEAD(sscanf, tc)
222ce11903fSperseant {
223ce11903fSperseant atf_tc_set_md_var(tc, "descr",
224*02698c5bSandvar "Checks sscanf under different locales");
225ce11903fSperseant }
ATF_TC_BODY(sscanf,tc)226ce11903fSperseant ATF_TC_BODY(sscanf, tc)
227ce11903fSperseant {
228ce11903fSperseant struct test *t;
229ce11903fSperseant
230ce11903fSperseant for (t = &tests[0]; t->locale != NULL; ++t)
231ce11903fSperseant h_sscanf(t);
232ce11903fSperseant }
233ce11903fSperseant
ATF_TP_ADD_TCS(tp)234ce11903fSperseant ATF_TP_ADD_TCS(tp)
235ce11903fSperseant {
236ce11903fSperseant
237ce11903fSperseant ATF_TP_ADD_TC(tp, sprintf);
238ce11903fSperseant ATF_TP_ADD_TC(tp, sscanf);
239ce11903fSperseant ATF_TP_ADD_TC(tp, strto);
240ce11903fSperseant
241ce11903fSperseant return atf_no_error();
242ce11903fSperseant }
243