xref: /freebsd/lib/libc/tests/stdlib/strfmon_test.c (revision f91301cc792c27c5e3e179b606dd8271a624744b)
18c1c50ffSConrad Meyer /*-
28c1c50ffSConrad Meyer  * Copyright (C) 2018 Conrad Meyer <cem@FreeBSD.org>
38c1c50ffSConrad Meyer  * All rights reserved.
48c1c50ffSConrad Meyer  *
58c1c50ffSConrad Meyer  * Redistribution and use in source and binary forms, with or without
68c1c50ffSConrad Meyer  * modification, are permitted provided that the following conditions
78c1c50ffSConrad Meyer  * are met:
88c1c50ffSConrad Meyer  * 1. Redistributions of source code must retain the above copyright
98c1c50ffSConrad Meyer  *    notice, this list of conditions and the following disclaimer.
108c1c50ffSConrad Meyer  * 2. Redistributions in binary form must reproduce the above copyright
118c1c50ffSConrad Meyer  *    notice, this list of conditions and the following disclaimer in the
128c1c50ffSConrad Meyer  *    documentation and/or other materials provided with the distribution.
138c1c50ffSConrad Meyer  *
148c1c50ffSConrad Meyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
158c1c50ffSConrad Meyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168c1c50ffSConrad Meyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178c1c50ffSConrad Meyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
188c1c50ffSConrad Meyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198c1c50ffSConrad Meyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
208c1c50ffSConrad Meyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
218c1c50ffSConrad Meyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228c1c50ffSConrad Meyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
238c1c50ffSConrad Meyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
248c1c50ffSConrad Meyer  * SUCH DAMAGE.
258c1c50ffSConrad Meyer  */
268c1c50ffSConrad Meyer 
278c1c50ffSConrad Meyer #include <sys/cdefs.h>
288c1c50ffSConrad Meyer __FBSDID("$FreeBSD$");
298c1c50ffSConrad Meyer 
308c1c50ffSConrad Meyer #include <locale.h>
318c1c50ffSConrad Meyer #include <monetary.h>
328c1c50ffSConrad Meyer #include <stdio.h>
338c1c50ffSConrad Meyer 
348c1c50ffSConrad Meyer #include <atf-c.h>
358c1c50ffSConrad Meyer 
368c1c50ffSConrad Meyer ATF_TC_WITHOUT_HEAD(strfmon_locale_thousands);
378c1c50ffSConrad Meyer ATF_TC_BODY(strfmon_locale_thousands, tc)
388c1c50ffSConrad Meyer {
398c1c50ffSConrad Meyer 	char actual[40], expected[40];
408c1c50ffSConrad Meyer 	struct lconv *lc;
418c1c50ffSConrad Meyer 	const char *ts;
428c1c50ffSConrad Meyer 	double n;
438c1c50ffSConrad Meyer 
448c1c50ffSConrad Meyer 	setlocale(LC_MONETARY, "sv_SE.UTF-8");
458c1c50ffSConrad Meyer 
468c1c50ffSConrad Meyer 	lc = localeconv();
478c1c50ffSConrad Meyer 
488c1c50ffSConrad Meyer 	ts = lc->mon_thousands_sep;
498c1c50ffSConrad Meyer 	if (strlen(ts) == 0)
508c1c50ffSConrad Meyer 		ts = lc->thousands_sep;
518c1c50ffSConrad Meyer 
528c1c50ffSConrad Meyer 	if (strlen(ts) < 2)
538c1c50ffSConrad Meyer 		atf_tc_skip("multi-byte thousands-separator not found");
548c1c50ffSConrad Meyer 
558c1c50ffSConrad Meyer 	n = 1234.56;
568c1c50ffSConrad Meyer 	strfmon(actual, sizeof(actual), "%i", n);
578c1c50ffSConrad Meyer 
588c1c50ffSConrad Meyer 	strcpy(expected, "1");
598c1c50ffSConrad Meyer 	strlcat(expected, ts, sizeof(expected));
608c1c50ffSConrad Meyer 	strlcat(expected, "234", sizeof(expected));
618c1c50ffSConrad Meyer 
62*f91301ccSJose Luis Duran 	/* We're just testing the thousands separator, not all of strfmon. */
638c1c50ffSConrad Meyer 	actual[strlen(expected)] = '\0';
648c1c50ffSConrad Meyer 	ATF_CHECK_STREQ(expected, actual);
658c1c50ffSConrad Meyer }
668c1c50ffSConrad Meyer 
678c1c50ffSConrad Meyer ATF_TP_ADD_TCS(tp)
688c1c50ffSConrad Meyer {
698c1c50ffSConrad Meyer 	ATF_TP_ADD_TC(tp, strfmon_locale_thousands);
708c1c50ffSConrad Meyer 	return (atf_no_error());
718c1c50ffSConrad Meyer }
72