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