1*ecbffb52Schristos /* $NetBSD: t_strfmon.c,v 1.6 2023/11/27 19:45:36 christos Exp $ */
229f5b623Sjoerg
329f5b623Sjoerg /*-
429f5b623Sjoerg * Copyright (c) 2017 The NetBSD Foundation, Inc.
59a3e6516Schristos * Copyright (C) 2018 Conrad Meyer <cem@FreeBSD.org>
629f5b623Sjoerg * All rights reserved.
729f5b623Sjoerg *
829f5b623Sjoerg * This code is derived from software contributed to The NetBSD Foundation
929f5b623Sjoerg * by Joerg Sonnenberger.
1029f5b623Sjoerg *
1129f5b623Sjoerg * Redistribution and use in source and binary forms, with or without
1229f5b623Sjoerg * modification, are permitted provided that the following conditions
1329f5b623Sjoerg * are met:
1429f5b623Sjoerg * 1. Redistributions of source code must retain the above copyright
1529f5b623Sjoerg * notice, this list of conditions and the following disclaimer.
1629f5b623Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1729f5b623Sjoerg * notice, this list of conditions and the following disclaimer in the
1829f5b623Sjoerg * documentation and/or other materials provided with the distribution.
1929f5b623Sjoerg *
2029f5b623Sjoerg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2129f5b623Sjoerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2229f5b623Sjoerg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2329f5b623Sjoerg * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2429f5b623Sjoerg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2529f5b623Sjoerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2629f5b623Sjoerg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2729f5b623Sjoerg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2829f5b623Sjoerg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2929f5b623Sjoerg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3029f5b623Sjoerg * POSSIBILITY OF SUCH DAMAGE.
3129f5b623Sjoerg */
3229f5b623Sjoerg
3329f5b623Sjoerg #include <sys/cdefs.h>
34*ecbffb52Schristos __RCSID("$NetBSD: t_strfmon.c,v 1.6 2023/11/27 19:45:36 christos Exp $");
3529f5b623Sjoerg
3629f5b623Sjoerg #include <atf-c.h>
379a3e6516Schristos #include <stdio.h>
3829f5b623Sjoerg #include <locale.h>
3929f5b623Sjoerg #include <monetary.h>
4029f5b623Sjoerg
410630eb1fSchristos ATF_TC(strfmon_locale);
4229f5b623Sjoerg
ATF_TC_HEAD(strfmon_locale,tc)430630eb1fSchristos ATF_TC_HEAD(strfmon_locale, tc)
4429f5b623Sjoerg {
4529f5b623Sjoerg atf_tc_set_md_var(tc, "descr",
4602698c5bSandvar "Checks strfmon_l under different locales");
4729f5b623Sjoerg }
4829f5b623Sjoerg
ATF_TC_BODY(strfmon_locale,tc)490630eb1fSchristos ATF_TC_BODY(strfmon_locale, tc)
5029f5b623Sjoerg {
5129f5b623Sjoerg const struct {
5229f5b623Sjoerg const char *locale;
5329f5b623Sjoerg const char *expected;
5429f5b623Sjoerg } tests[] = {
5529f5b623Sjoerg { "C", "[ **1234.57 ] [ **1234.57 ]" },
5629f5b623Sjoerg { "de_DE.UTF-8", "[ **1234,57 €] [ **1.234,57 EUR]" },
5729f5b623Sjoerg { "en_GB.UTF-8", "[ £**1234.57] [ GBP**1,234.57]" },
5829f5b623Sjoerg };
5929f5b623Sjoerg locale_t loc;
6029f5b623Sjoerg size_t i;
6129f5b623Sjoerg char buf[80];
6229f5b623Sjoerg for (i = 0; i < __arraycount(tests); ++i) {
6329f5b623Sjoerg loc = newlocale(LC_MONETARY_MASK, tests[i].locale, 0);
6429f5b623Sjoerg ATF_REQUIRE(loc != 0);
6529f5b623Sjoerg strfmon_l(buf, sizeof(buf), loc, "[%^=*#6n] [%=*#6i]",
6629f5b623Sjoerg 1234.567, 1234.567);
6729f5b623Sjoerg ATF_REQUIRE_STREQ(tests[i].expected, buf);
6829f5b623Sjoerg freelocale(loc);
6929f5b623Sjoerg }
7029f5b623Sjoerg }
7129f5b623Sjoerg
720630eb1fSchristos ATF_TC(strfmon_pad);
730630eb1fSchristos
ATF_TC_HEAD(strfmon_pad,tc)740630eb1fSchristos ATF_TC_HEAD(strfmon_pad, tc)
750630eb1fSchristos {
760630eb1fSchristos atf_tc_set_md_var(tc, "descr", "Checks strfmon padding");
770630eb1fSchristos }
780630eb1fSchristos
ATF_TC_BODY(strfmon_pad,tc)790630eb1fSchristos ATF_TC_BODY(strfmon_pad, tc)
800630eb1fSchristos {
810630eb1fSchristos char string[1024];
820630eb1fSchristos
830630eb1fSchristos ATF_REQUIRE(setlocale(LC_MONETARY, "en_US.UTF-8") != NULL);
840630eb1fSchristos strfmon(string, sizeof(string), "[%8n] [%8n]", 123.45, 123.45);
850630eb1fSchristos ATF_REQUIRE_STREQ(string, "[ $123.45] [ $123.45]");
860630eb1fSchristos }
870630eb1fSchristos
889a3e6516Schristos ATF_TC(strfmon_locale_thousands);
899a3e6516Schristos
ATF_TC_HEAD(strfmon_locale_thousands,tc)909a3e6516Schristos ATF_TC_HEAD(strfmon_locale_thousands, tc)
919a3e6516Schristos {
929a3e6516Schristos atf_tc_set_md_var(tc, "descr",
939a3e6516Schristos "Checks strfmon locale thousands separator");
949a3e6516Schristos }
959a3e6516Schristos
ATF_TC_BODY(strfmon_locale_thousands,tc)969a3e6516Schristos ATF_TC_BODY(strfmon_locale_thousands, tc)
979a3e6516Schristos {
989a3e6516Schristos char actual[40], expected[40];
999a3e6516Schristos struct lconv *lc;
1009a3e6516Schristos const char *ts;
1019a3e6516Schristos double n;
1029a3e6516Schristos
1039a3e6516Schristos setlocale(LC_MONETARY, "sv_SE.UTF-8");
1049a3e6516Schristos
1059a3e6516Schristos lc = localeconv();
1069a3e6516Schristos
1079a3e6516Schristos ts = lc->mon_thousands_sep;
1089a3e6516Schristos if (strlen(ts) == 0)
1099a3e6516Schristos ts = lc->thousands_sep;
1109a3e6516Schristos
1119a3e6516Schristos if (strlen(ts) < 2)
1129a3e6516Schristos atf_tc_skip("multi-byte thousands-separator not found");
1139a3e6516Schristos
1149a3e6516Schristos n = 1234.56;
1159a3e6516Schristos strfmon(actual, sizeof(actual) - 1, "%i", n);
1169a3e6516Schristos
1179a3e6516Schristos strcpy(expected, "1");
1189a3e6516Schristos strlcat(expected, ts, sizeof(expected));
1199a3e6516Schristos strlcat(expected, "234", sizeof(expected));
1209a3e6516Schristos
1219a3e6516Schristos /* We're just testing the thousands separator, not all of strfmon. */
1229a3e6516Schristos actual[strlen(expected)] = '\0';
1239a3e6516Schristos ATF_CHECK_STREQ(expected, actual);
1249a3e6516Schristos }
1259a3e6516Schristos
1269a3e6516Schristos ATF_TC(strfmon_examples);
ATF_TC_HEAD(strfmon_examples,tc)1279a3e6516Schristos ATF_TC_HEAD(strfmon_examples, tc) {
1289a3e6516Schristos atf_tc_set_md_var(tc, "descr",
1299a3e6516Schristos "Checks strfmon field formats");
1309a3e6516Schristos }
1319a3e6516Schristos
ATF_TC_BODY(strfmon_examples,tc)1329a3e6516Schristos ATF_TC_BODY(strfmon_examples, tc)
1339a3e6516Schristos {
1349a3e6516Schristos const struct {
1359a3e6516Schristos const char *format;
1369a3e6516Schristos const char *expected;
1379a3e6516Schristos } tests[] = {
1389a3e6516Schristos { "%n", "[$123.45] [-$123.45] [$3,456.78]" },
1399a3e6516Schristos { "%11n", "[ $123.45] [ -$123.45] [ $3,456.78]" },
1409a3e6516Schristos { "%#5n", "[ $ 123.45] [-$ 123.45] [ $ 3,456.78]" },
1419a3e6516Schristos { "%=*#5n", "[ $***123.45] [-$***123.45] [ $*3,456.78]" },
1429a3e6516Schristos { "%=0#5n", "[ $000123.45] [-$000123.45] [ $03,456.78]" },
1439a3e6516Schristos { "%^#5n", "[ $ 123.45] [-$ 123.45] [ $ 3456.78]" },
1449a3e6516Schristos { "%^#5.0n", "[ $ 123] [-$ 123] [ $ 3457]" },
1459a3e6516Schristos { "%^#5.4n", "[ $ 123.4500] [-$ 123.4500] [ $ 3456.7810]" },
1469a3e6516Schristos { "%(#5n", "[ $ 123.45 ] [($ 123.45)] [ $ 3,456.78 ]" },
1479a3e6516Schristos { "%!(#5n", "[ 123.45 ] [( 123.45)] [ 3,456.78 ]" },
1489a3e6516Schristos { "%-14#5.4n", "[ $ 123.4500 ] [-$ 123.4500 ] [ $ 3,456.7810 ]" },
1499a3e6516Schristos { "%14#5.4n", "[ $ 123.4500] [ -$ 123.4500] [ $ 3,456.7810]" },
1509a3e6516Schristos };
1519a3e6516Schristos size_t i;
1529a3e6516Schristos char actual[100], format[50];
1539a3e6516Schristos
1549a3e6516Schristos if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL)
1559a3e6516Schristos atf_tc_skip("unable to setlocale()");
1569a3e6516Schristos
1579a3e6516Schristos for (i = 0; i < __arraycount(tests); ++i) {
1589a3e6516Schristos snprintf(format, sizeof(format), "[%s] [%s] [%s]",
1599a3e6516Schristos tests[i].format, tests[i].format, tests[i].format);
160*ecbffb52Schristos strfmon(actual, sizeof(actual) - 1, format,
1619a3e6516Schristos 123.45, -123.45, 3456.781);
1629a3e6516Schristos ATF_CHECK_STREQ_MSG(tests[i].expected, actual,
1639a3e6516Schristos "[%s]", tests[i].format);
1649a3e6516Schristos }
1659a3e6516Schristos }
1669a3e6516Schristos
1679a3e6516Schristos ATF_TC(strfmon_cs_precedes_0);
1689a3e6516Schristos
ATF_TC_HEAD(strfmon_cs_precedes_0,tc)1699a3e6516Schristos ATF_TC_HEAD(strfmon_cs_precedes_0, tc)
1709a3e6516Schristos {
1719a3e6516Schristos atf_tc_set_md_var(tc, "descr",
1729a3e6516Schristos "sep_by_space x sign_posn when cs_precedes = 0");
1739a3e6516Schristos }
1749a3e6516Schristos
ATF_TC_BODY(strfmon_cs_precedes_0,tc)1759a3e6516Schristos ATF_TC_BODY(strfmon_cs_precedes_0, tc)
1769a3e6516Schristos {
1779a3e6516Schristos const struct {
1789a3e6516Schristos const char *expected;
1799a3e6516Schristos } tests[] = {
1809a3e6516Schristos /* sep_by_space x sign_posn */
1819a3e6516Schristos { "[(123.00$)] [-123.00$] [123.00$-] [123.00-$] [123.00$-]" },
1829a3e6516Schristos { "[(123.00 $)] [-123.00 $] [123.00 $-] [123.00 -$] [123.00 $-]" },
1839a3e6516Schristos { "[(123.00$)] [- 123.00$] [123.00$ -] [123.00- $] [123.00$ -]" },
1849a3e6516Schristos };
1859a3e6516Schristos size_t i, j;
1869a3e6516Schristos struct lconv *lc;
1879a3e6516Schristos char actual[100], buf[100];
1889a3e6516Schristos
1899a3e6516Schristos if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL)
1909a3e6516Schristos atf_tc_skip("unable to setlocale()");
1919a3e6516Schristos
1929a3e6516Schristos lc = localeconv();
1939a3e6516Schristos lc->n_cs_precedes = 0;
1949a3e6516Schristos
1959a3e6516Schristos for (i = 0; i < __arraycount(tests); ++i) {
1969a3e6516Schristos actual[0] = '\0';
1979a3e6516Schristos lc->n_sep_by_space = i;
1989a3e6516Schristos
1999a3e6516Schristos for (j = 0; j < 5; ++j) {
2009a3e6516Schristos lc->n_sign_posn = j;
2019a3e6516Schristos
2029a3e6516Schristos strfmon(buf, sizeof(buf) - 1, "[%n] ", -123.0);
2039a3e6516Schristos strlcat(actual, buf, sizeof(actual));
2049a3e6516Schristos }
2059a3e6516Schristos
2069a3e6516Schristos actual[strlen(actual) - 1] = '\0';
2079a3e6516Schristos ATF_CHECK_STREQ_MSG(tests[i].expected, actual,
2089a3e6516Schristos "sep_by_space = %zu", i);
2099a3e6516Schristos }
2109a3e6516Schristos }
2119a3e6516Schristos
2129a3e6516Schristos ATF_TC(strfmon_cs_precedes_1);
2139a3e6516Schristos
ATF_TC_HEAD(strfmon_cs_precedes_1,tc)2149a3e6516Schristos ATF_TC_HEAD(strfmon_cs_precedes_1, tc)
2159a3e6516Schristos {
2169a3e6516Schristos atf_tc_set_md_var(tc, "descr",
2179a3e6516Schristos "sep_by_space x sign_posn when cs_precedes = 1");
2189a3e6516Schristos }
2199a3e6516Schristos
ATF_TC_BODY(strfmon_cs_precedes_1,tc)2209a3e6516Schristos ATF_TC_BODY(strfmon_cs_precedes_1, tc)
2219a3e6516Schristos {
2229a3e6516Schristos const struct {
2239a3e6516Schristos const char *expected;
2249a3e6516Schristos } tests[] = {
2259a3e6516Schristos /* sep_by_space x sign_posn */
2269a3e6516Schristos { "[($123.00)] [-$123.00] [$123.00-] [-$123.00] [$-123.00]" },
2279a3e6516Schristos { "[($ 123.00)] [-$ 123.00] [$ 123.00-] [-$ 123.00] [$- 123.00]" },
2289a3e6516Schristos { "[($123.00)] [- $123.00] [$123.00 -] [- $123.00] [$ -123.00]" },
2299a3e6516Schristos };
2309a3e6516Schristos size_t i, j;
2319a3e6516Schristos struct lconv *lc;
2329a3e6516Schristos char actual[100], buf[100];
2339a3e6516Schristos
2349a3e6516Schristos if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL)
2359a3e6516Schristos atf_tc_skip("unable to setlocale()");
2369a3e6516Schristos
2379a3e6516Schristos lc = localeconv();
2389a3e6516Schristos lc->n_cs_precedes = 1;
2399a3e6516Schristos
2409a3e6516Schristos for (i = 0; i < __arraycount(tests); ++i) {
2419a3e6516Schristos actual[0] = '\0';
2429a3e6516Schristos lc->n_sep_by_space = i;
2439a3e6516Schristos
2449a3e6516Schristos for (j = 0; j < 5; ++j) {
2459a3e6516Schristos lc->n_sign_posn = j;
2469a3e6516Schristos
2479a3e6516Schristos strfmon(buf, sizeof(buf) - 1, "[%n] ", -123.0);
2489a3e6516Schristos strlcat(actual, buf, sizeof(actual));
2499a3e6516Schristos }
2509a3e6516Schristos
2519a3e6516Schristos actual[strlen(actual) - 1] = '\0';
2529a3e6516Schristos ATF_CHECK_STREQ_MSG(tests[i].expected, actual,
2539a3e6516Schristos "sep_by_space = %zu", i);
2549a3e6516Schristos }
2559a3e6516Schristos }
2569a3e6516Schristos
2579a3e6516Schristos ATF_TC(strfmon_international_currency_code);
ATF_TC_HEAD(strfmon_international_currency_code,tc)2589a3e6516Schristos ATF_TC_HEAD(strfmon_international_currency_code, tc)
2599a3e6516Schristos {
2609a3e6516Schristos atf_tc_set_md_var(tc, "descr",
2619a3e6516Schristos "checks strfmon international currency code");
2629a3e6516Schristos }
2639a3e6516Schristos
ATF_TC_BODY(strfmon_international_currency_code,tc)2649a3e6516Schristos ATF_TC_BODY(strfmon_international_currency_code, tc)
2659a3e6516Schristos {
2669a3e6516Schristos const struct {
2679a3e6516Schristos const char *locale;
2689a3e6516Schristos const char *expected;
2699a3e6516Schristos } tests[] = {
2709a3e6516Schristos { "en_US.UTF-8", "[USD123.45]" },
2719a3e6516Schristos { "de_DE.UTF-8", "[123,45 EUR]" },
2729a3e6516Schristos { "C", "[123.45]" },
2739a3e6516Schristos };
2749a3e6516Schristos size_t i;
2759a3e6516Schristos char actual[100];
2769a3e6516Schristos
2779a3e6516Schristos for (i = 0; i < __arraycount(tests); ++i) {
2789a3e6516Schristos if (setlocale(LC_MONETARY, tests[i].locale) == NULL)
2799a3e6516Schristos atf_tc_skip("unable to setlocale()");
2809a3e6516Schristos
2819a3e6516Schristos strfmon(actual, sizeof(actual) - 1, "[%i]", 123.45);
2829a3e6516Schristos ATF_CHECK_STREQ(tests[i].expected, actual);
2839a3e6516Schristos }
2849a3e6516Schristos }
2859a3e6516Schristos
ATF_TP_ADD_TCS(tp)28629f5b623Sjoerg ATF_TP_ADD_TCS(tp)
28729f5b623Sjoerg {
28829f5b623Sjoerg
2890630eb1fSchristos ATF_TP_ADD_TC(tp, strfmon_locale);
2900630eb1fSchristos ATF_TP_ADD_TC(tp, strfmon_pad);
2919a3e6516Schristos ATF_TP_ADD_TC(tp, strfmon_locale_thousands);
2929a3e6516Schristos ATF_TP_ADD_TC(tp, strfmon_examples);
2939a3e6516Schristos ATF_TP_ADD_TC(tp, strfmon_cs_precedes_0);
2949a3e6516Schristos ATF_TP_ADD_TC(tp, strfmon_cs_precedes_1);
2959a3e6516Schristos ATF_TP_ADD_TC(tp, strfmon_international_currency_code);
29629f5b623Sjoerg
29729f5b623Sjoerg return atf_no_error();
29829f5b623Sjoerg }
299