xref: /freebsd/lib/libc/tests/stdlib/strfmon_test.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 /*-
2  * Copyright (C) 2018 Conrad Meyer <cem@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 
32 #include <locale.h>
33 #include <monetary.h>
34 #include <stdio.h>
35 
36 #include <atf-c.h>
37 
38 ATF_TC_WITHOUT_HEAD(strfmon_locale_thousands);
39 ATF_TC_BODY(strfmon_locale_thousands, tc)
40 {
41 	char actual[40], expected[40];
42 	struct lconv *lc;
43 	const char *ts;
44 	double n;
45 
46 	setlocale(LC_MONETARY, "sv_SE.UTF-8");
47 
48 	lc = localeconv();
49 
50 	ts = lc->mon_thousands_sep;
51 	if (strlen(ts) == 0)
52 		ts = lc->thousands_sep;
53 
54 	if (strlen(ts) < 2)
55 		atf_tc_skip("multi-byte thousands-separator not found");
56 
57 	n = 1234.56;
58 	strfmon(actual, sizeof(actual) - 1, "%i", n);
59 
60 	strcpy(expected, "1");
61 	strlcat(expected, ts, sizeof(expected));
62 	strlcat(expected, "234", sizeof(expected));
63 
64 	/* We're just testing the thousands separator, not all of strfmon. */
65 	actual[strlen(expected)] = '\0';
66 	ATF_CHECK_STREQ(expected, actual);
67 }
68 
69 ATF_TC_WITHOUT_HEAD(strfmon_examples);
70 ATF_TC_BODY(strfmon_examples, tc)
71 {
72 	const struct {
73 		const char *format;
74 		const char *expected;
75 	} tests[] = {
76 	    { "%n", "[$123.45] [-$123.45] [$3,456.78]" },
77 	    { "%11n", "[    $123.45] [   -$123.45] [  $3,456.78]" },
78 	    { "%#5n", "[ $   123.45] [-$   123.45] [ $ 3,456.78]" },
79 	    { "%=*#5n", "[ $***123.45] [-$***123.45] [ $*3,456.78]" },
80 	    { "%=0#5n", "[ $000123.45] [-$000123.45] [ $03,456.78]" },
81 	    { "%^#5n", "[ $  123.45] [-$  123.45] [ $ 3456.78]" },
82 	    { "%^#5.0n", "[ $  123] [-$  123] [ $ 3457]" },
83 	    { "%^#5.4n", "[ $  123.4500] [-$  123.4500] [ $ 3456.7810]" },
84 	    { "%(#5n", "[ $   123.45 ] [($   123.45)] [ $ 3,456.78 ]" },
85 	    { "%!(#5n", "[    123.45 ] [(   123.45)] [  3,456.78 ]" },
86 	    { "%-14#5.4n", "[ $   123.4500 ] [-$   123.4500 ] [ $ 3,456.7810 ]" },
87 	    { "%14#5.4n", "[  $   123.4500] [ -$   123.4500] [  $ 3,456.7810]" },
88 	};
89 	size_t i;
90 	char actual[100], format[50];
91 
92 	if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL)
93 		atf_tc_skip("unable to setlocale()");
94 
95 	for (i = 0; i < nitems(tests); ++i) {
96 		snprintf(format, sizeof(format), "[%s] [%s] [%s]",
97 		    tests[i].format, tests[i].format, tests[i].format);
98 		strfmon(actual, sizeof(actual) - 1, format,
99 		    123.45, -123.45, 3456.781);
100 		ATF_CHECK_STREQ_MSG(tests[i].expected, actual,
101 		    "[%s]", tests[i].format);
102 	}
103 }
104 
105 ATF_TC(strfmon_cs_precedes_0);
106 ATF_TC_HEAD(strfmon_cs_precedes_0, tc)
107 {
108 	atf_tc_set_md_var(tc, "descr",
109 	    "sep_by_space x sign_posn when cs_precedes = 0");
110 }
111 ATF_TC_BODY(strfmon_cs_precedes_0, tc)
112 {
113 	const struct {
114 		const char *expected;
115 	} tests[] = {
116 	    /* sep_by_space x sign_posn */
117 	    { "[(123.00$)] [-123.00$] [123.00$-] [123.00-$] [123.00$-]" },
118 	    { "[(123.00 $)] [-123.00 $] [123.00 $-] [123.00 -$] [123.00 $-]" },
119 	    { "[(123.00$)] [- 123.00$] [123.00$ -] [123.00- $] [123.00$ -]" },
120 	};
121 	size_t i, j;
122 	struct lconv *lc;
123 	char actual[100], buf[100];
124 
125 	if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL)
126 		atf_tc_skip("unable to setlocale()");
127 
128 	lc = localeconv();
129 	lc->n_cs_precedes = 0;
130 
131 	for (i = 0; i < nitems(tests); ++i) {
132 		actual[0] = '\0';
133 		lc->n_sep_by_space = i;
134 
135 		for (j = 0; j < 5; ++j) {
136 			lc->n_sign_posn = j;
137 
138 			strfmon(buf, sizeof(buf) - 1, "[%n] ", -123.0);
139 			strlcat(actual, buf, sizeof(actual));
140 		}
141 
142 		actual[strlen(actual) - 1] = '\0';
143 		ATF_CHECK_STREQ_MSG(tests[i].expected, actual,
144 		    "sep_by_space = %zu", i);
145 	}
146 }
147 
148 ATF_TC(strfmon_cs_precedes_1);
149 ATF_TC_HEAD(strfmon_cs_precedes_1, tc)
150 {
151 	atf_tc_set_md_var(tc, "descr",
152 	    "sep_by_space x sign_posn when cs_precedes = 1");
153 }
154 ATF_TC_BODY(strfmon_cs_precedes_1, tc)
155 {
156 	const struct {
157 		const char *expected;
158 	} tests[] = {
159 	    /* sep_by_space x sign_posn */
160 	    { "[($123.00)] [-$123.00] [$123.00-] [-$123.00] [$-123.00]" },
161 	    { "[($ 123.00)] [-$ 123.00] [$ 123.00-] [-$ 123.00] [$- 123.00]" },
162 	    { "[($123.00)] [- $123.00] [$123.00 -] [- $123.00] [$ -123.00]" },
163 	};
164 	size_t i, j;
165 	struct lconv *lc;
166 	char actual[100], buf[100];
167 
168 	if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL)
169 		atf_tc_skip("unable to setlocale()");
170 
171 	lc = localeconv();
172 	lc->n_cs_precedes = 1;
173 
174 	for (i = 0; i < nitems(tests); ++i) {
175 		actual[0] = '\0';
176 		lc->n_sep_by_space = i;
177 
178 		for (j = 0; j < 5; ++j) {
179 			lc->n_sign_posn = j;
180 
181 			strfmon(buf, sizeof(buf) - 1, "[%n] ", -123.0);
182 			strlcat(actual, buf, sizeof(actual));
183 		}
184 
185 		actual[strlen(actual) - 1] = '\0';
186 		ATF_CHECK_STREQ_MSG(tests[i].expected, actual,
187 		    "sep_by_space = %zu", i);
188 	}
189 }
190 
191 ATF_TC_WITHOUT_HEAD(strfmon_international_currency_code);
192 ATF_TC_BODY(strfmon_international_currency_code, tc)
193 {
194 	const struct {
195 		const char *locale;
196 		const char *expected;
197 	} tests[] = {
198 	    { "en_US.UTF-8", "[USD123.45]" },
199 	    { "de_DE.UTF-8", "[123,45 EUR]" },
200 	    { "C", "[123.45]" },
201 	};
202 	size_t i;
203 	char actual[100];
204 
205 	for (i = 0; i < nitems(tests); ++i) {
206 		if (setlocale(LC_MONETARY, tests[i].locale) == NULL)
207 			atf_tc_skip("unable to setlocale()");
208 
209 		strfmon(actual, sizeof(actual) - 1, "[%i]", 123.45);
210 		ATF_CHECK_STREQ(tests[i].expected, actual);
211 	}
212 }
213 
214 ATF_TC(strfmon_l);
215 ATF_TC_HEAD(strfmon_l, tc)
216 {
217 	atf_tc_set_md_var(tc, "descr",
218 	    "checks strfmon_l under different locales");
219 }
220 ATF_TC_BODY(strfmon_l, tc)
221 {
222 	const struct {
223 		const char *locale;
224 		const char *expected;
225 	} tests[] = {
226 	    { "C", "[ **1234.57 ] [ **1234.57 ]" },
227 	    { "de_DE.UTF-8", "[ **1234,57 €] [ **1.234,57 EUR]" },
228 	    { "en_GB.UTF-8", "[ £**1234.57] [ GBP**1,234.57]" },
229 	};
230 	locale_t loc;
231 	size_t i;
232 	char buf[100];
233 
234 	for (i = 0; i < nitems(tests); ++i) {
235 		loc = newlocale(LC_MONETARY_MASK, tests[i].locale, NULL);
236 		ATF_REQUIRE(loc != NULL);
237 
238 		strfmon_l(buf, sizeof(buf) - 1, loc, "[%^=*#6n] [%=*#6i]",
239 		    1234.567, 1234.567);
240 		ATF_REQUIRE_STREQ(tests[i].expected, buf);
241 
242 		freelocale(loc);
243 	}
244 }
245 
246 ATF_TP_ADD_TCS(tp)
247 {
248 	ATF_TP_ADD_TC(tp, strfmon_locale_thousands);
249 	ATF_TP_ADD_TC(tp, strfmon_examples);
250 	ATF_TP_ADD_TC(tp, strfmon_cs_precedes_0);
251 	ATF_TP_ADD_TC(tp, strfmon_cs_precedes_1);
252 	ATF_TP_ADD_TC(tp, strfmon_international_currency_code);
253 	ATF_TP_ADD_TC(tp, strfmon_l);
254 	return (atf_no_error());
255 }
256