aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJose Luis Duran <jlduran@FreeBSD.org>2025-11-26 20:34:55 +0000
committerJose Luis Duran <jlduran@FreeBSD.org>2025-11-26 20:34:55 +0000
commit19e153004fb63c32eba0ef40249f5ede61a93170 (patch)
tree3a43b1181d0c7774500b612c06e68335ae62a444
parent73dd00f2fd7de2a5d8dda8fa25ebcd7c8964ff52 (diff)
strfmon: Add tests for Austin Group Defect 1199
Add tests for The Open Group Base Specifications Issue 8[1], Austin Group Defect 1199[2]. Items marked with XXX represent an invalid output. These items will be fixed in subsequent commits. Notice that an existing test is now considered invalid. Our locale definitions do not include int_p_sep_by_space nor int_n_sep_by_space[3]. Those will be addressed in a subsequent commit. However, the CLDR project defines them as "0", which causes the output to appear as "USD123.45". If our locale definitions were to set the international {n,p}_sep_by_space to "1", the output would display as the expected "USD 123.45". While here, use the SPDX license identifier and add my name to the file. [1]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/strfmon.html [2]: https://www.austingroupbugs.net/view.php?id=1199 [3]: https://unicode-org.atlassian.net/browse/CLDR-237 Reviewed by: kib MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D53911
-rw-r--r--lib/libc/tests/stdlib/strfmon_test.c80
1 files changed, 57 insertions, 23 deletions
diff --git a/lib/libc/tests/stdlib/strfmon_test.c b/lib/libc/tests/stdlib/strfmon_test.c
index 86f6256dba0b..3c97d61ba82c 100644
--- a/lib/libc/tests/stdlib/strfmon_test.c
+++ b/lib/libc/tests/stdlib/strfmon_test.c
@@ -1,31 +1,14 @@
-/*-
- * Copyright (C) 2018 Conrad Meyer <cem@FreeBSD.org>
+/*
+ * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org>
* All rights reserved.
+ * Copyright (c) 2022-2025 Jose Luis Duran <jlduran@FreeBSD.org>
*
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
+ * SPDX-License-Identifier: BSD-2-Clause
*/
#include <sys/param.h>
+#include <errno.h>
#include <locale.h>
#include <monetary.h>
#include <stdio.h>
@@ -208,6 +191,56 @@ ATF_TC_BODY(strfmon_international_currency_code, tc)
}
}
+ATF_TC_WITHOUT_HEAD(strfmon_plus_or_parenthesis);
+ATF_TC_BODY(strfmon_plus_or_parenthesis, tc)
+{
+ const struct {
+ const char *format;
+ const char *locale;
+ const char *expected;
+ } tests[] = {
+ { "%+n", "en_US.UTF-8", "[$123.45] [-$123.45]" },
+ { "%+i", "en_US.UTF-8", "[USD123.45] [-USD123.45]" },
+ { "%(n", "C", "[123.45] [(123.45)]" },
+ { "%(i", "C", "[123.45] [(123.45)]" },
+ { "%(n", "en_US.UTF-8", "[$123.45] [($123.45)]" },
+ { "%(i", "en_US.UTF-8", "[USD123.45] [(USD123.45)]" },
+ { "%n", "C", "[123.45] [(123.45)]" }, /* XXX */
+ { "%i", "C", "[123.45] [(123.45)]" }, /* XXX */
+ { "%n", "en_US.UTF-8", "[$123.45] [-$123.45]" },
+ { "%i", "en_US.UTF-8", "[USD123.45] [-USD123.45]" },
+ };
+ size_t i;
+ char actual[100], format[50];
+
+ for (i = 0; i < nitems(tests); ++i) {
+ if (setlocale(LC_MONETARY, tests[i].locale) == NULL)
+ atf_tc_skip("unable to setlocale(): %s",
+ tests[i].locale);
+
+ snprintf(format, sizeof(format), "[%s] [%s]",
+ tests[i].format, tests[i].format);
+ strfmon(actual, sizeof(actual) - 1, format,
+ 123.45, -123.45);
+ ATF_CHECK_STREQ_MSG(tests[i].expected, actual,
+ "[%s] %s", tests[i].format, tests[i].locale);
+ }
+
+ /*
+ * The '+' flag was included in a conversion specification and
+ * the locale's positive_sign and negative_sign values would
+ * both be returned by localeconv() as empty strings.
+ */
+ if (setlocale(LC_MONETARY, "C") == NULL)
+ atf_tc_skip("unable to setlocale(): %s", tests[i].locale);
+
+ /* ATF_CHECK_ERRNO(EINVAL, strfmon(actual, sizeof(actual) - 1,
+ "[%+n] [%+n]", 123.45, -123.45)); XXX */
+
+ /* ATF_CHECK_ERRNO(EINVAL, strfmon(actual, sizeof(actual) - 1,
+ "[%+i] [%+i]", 123.45, -123.45)); XXX */
+}
+
ATF_TC(strfmon_l);
ATF_TC_HEAD(strfmon_l, tc)
{
@@ -220,7 +253,7 @@ ATF_TC_BODY(strfmon_l, tc)
const char *locale;
const char *expected;
} tests[] = {
- { "C", "[ **1234.57 ] [ **1234.57 ]" },
+ { "C", "[ **1234.57 ] [ **1234.57 ]" }, /* XXX */
{ "de_DE.UTF-8", "[ **1234,57 €] [ **1.234,57 EUR]" },
{ "en_GB.UTF-8", "[ £**1234.57] [ GBP**1,234.57]" },
};
@@ -247,6 +280,7 @@ ATF_TP_ADD_TCS(tp)
ATF_TP_ADD_TC(tp, strfmon_cs_precedes_0);
ATF_TP_ADD_TC(tp, strfmon_cs_precedes_1);
ATF_TP_ADD_TC(tp, strfmon_international_currency_code);
+ ATF_TP_ADD_TC(tp, strfmon_plus_or_parenthesis);
ATF_TP_ADD_TC(tp, strfmon_l);
return (atf_no_error());
}