Path: blob/main/lib/libc/tests/stdlib/strfmon_test.c
103392 views
/*1* Copyright (c) 2018 Conrad Meyer <[email protected]>2* All rights reserved.3* Copyright (c) 2022-2025 Jose Luis Duran <[email protected]>4*5* SPDX-License-Identifier: BSD-2-Clause6*/78#include <sys/param.h>910#include <errno.h>11#include <locale.h>12#include <monetary.h>13#include <stdio.h>1415#include <atf-c.h>1617ATF_TC_WITHOUT_HEAD(strfmon_locale_thousands);18ATF_TC_BODY(strfmon_locale_thousands, tc)19{20char actual[40], expected[40];21struct lconv *lc;22const char *ts;23double n;2425setlocale(LC_MONETARY, "sv_SE.UTF-8");2627lc = localeconv();2829ts = lc->mon_thousands_sep;30if (strlen(ts) == 0)31ts = lc->thousands_sep;3233if (strlen(ts) < 2)34atf_tc_skip("multi-byte thousands-separator not found");3536n = 1234.56;37strfmon(actual, sizeof(actual) - 1, "%i", n);3839strcpy(expected, "1");40strlcat(expected, ts, sizeof(expected));41strlcat(expected, "234", sizeof(expected));4243/* We're just testing the thousands separator, not all of strfmon. */44actual[strlen(expected)] = '\0';45ATF_CHECK_STREQ(expected, actual);46}4748ATF_TC_WITHOUT_HEAD(strfmon_examples);49ATF_TC_BODY(strfmon_examples, tc)50{51const struct {52const char *format;53const char *expected;54} tests[] = {55{ "%n", "[$123.45] [-$123.45] [$3,456.78]" },56{ "%11n", "[ $123.45] [ -$123.45] [ $3,456.78]" },57{ "%#5n", "[ $ 123.45] [-$ 123.45] [ $ 3,456.78]" },58{ "%=*#5n", "[ $***123.45] [-$***123.45] [ $*3,456.78]" },59{ "%=0#5n", "[ $000123.45] [-$000123.45] [ $03,456.78]" },60{ "%^#5n", "[ $ 123.45] [-$ 123.45] [ $ 3456.78]" },61{ "%^#5.0n", "[ $ 123] [-$ 123] [ $ 3457]" },62{ "%^#5.4n", "[ $ 123.4500] [-$ 123.4500] [ $ 3456.7810]" },63{ "%(#5n", "[ $ 123.45 ] [($ 123.45)] [ $ 3,456.78 ]" },64{ "%!(#5n", "[ 123.45 ] [( 123.45)] [ 3,456.78 ]" },65{ "%-14#5.4n", "[ $ 123.4500 ] [-$ 123.4500 ] [ $ 3,456.7810 ]" },66{ "%14#5.4n", "[ $ 123.4500] [ -$ 123.4500] [ $ 3,456.7810]" },67};68size_t i;69char actual[100], format[50];7071if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL)72atf_tc_skip("unable to setlocale()");7374for (i = 0; i < nitems(tests); ++i) {75snprintf(format, sizeof(format), "[%s] [%s] [%s]",76tests[i].format, tests[i].format, tests[i].format);77strfmon(actual, sizeof(actual) - 1, format,78123.45, -123.45, 3456.781);79ATF_CHECK_STREQ_MSG(tests[i].expected, actual,80"[%s]", tests[i].format);81}82}8384ATF_TC(strfmon_cs_precedes_0);85ATF_TC_HEAD(strfmon_cs_precedes_0, tc)86{87atf_tc_set_md_var(tc, "descr",88"sep_by_space x sign_posn when cs_precedes = 0");89}90ATF_TC_BODY(strfmon_cs_precedes_0, tc)91{92const struct {93const char *expected;94} tests[] = {95/* sep_by_space x sign_posn */96{ "[(123.00$)] [-123.00$] [123.00$-] [123.00-$] [123.00$-]" },97{ "[(123.00 $)] [-123.00 $] [123.00 $-] [123.00 -$] [123.00 $-]" },98{ "[(123.00$)] [- 123.00$] [123.00$ -] [123.00- $] [123.00$ -]" },99};100size_t i, j;101struct lconv *lc;102char actual[100], buf[100];103104if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL)105atf_tc_skip("unable to setlocale()");106107lc = localeconv();108lc->n_cs_precedes = 0;109110for (i = 0; i < nitems(tests); ++i) {111actual[0] = '\0';112lc->n_sep_by_space = i;113114for (j = 0; j < 5; ++j) {115lc->n_sign_posn = j;116117strfmon(buf, sizeof(buf) - 1, "[%n] ", -123.0);118strlcat(actual, buf, sizeof(actual));119}120121actual[strlen(actual) - 1] = '\0';122ATF_CHECK_STREQ_MSG(tests[i].expected, actual,123"sep_by_space = %zu", i);124}125}126127ATF_TC(strfmon_cs_precedes_1);128ATF_TC_HEAD(strfmon_cs_precedes_1, tc)129{130atf_tc_set_md_var(tc, "descr",131"sep_by_space x sign_posn when cs_precedes = 1");132}133ATF_TC_BODY(strfmon_cs_precedes_1, tc)134{135const struct {136const char *expected;137} tests[] = {138/* sep_by_space x sign_posn */139{ "[($123.00)] [-$123.00] [$123.00-] [-$123.00] [$-123.00]" },140{ "[($ 123.00)] [-$ 123.00] [$ 123.00-] [-$ 123.00] [$- 123.00]" },141{ "[($123.00)] [- $123.00] [$123.00 -] [- $123.00] [$ -123.00]" },142};143size_t i, j;144struct lconv *lc;145char actual[100], buf[100];146147if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL)148atf_tc_skip("unable to setlocale()");149150lc = localeconv();151lc->n_cs_precedes = 1;152153for (i = 0; i < nitems(tests); ++i) {154actual[0] = '\0';155lc->n_sep_by_space = i;156157for (j = 0; j < 5; ++j) {158lc->n_sign_posn = j;159160strfmon(buf, sizeof(buf) - 1, "[%n] ", -123.0);161strlcat(actual, buf, sizeof(actual));162}163164actual[strlen(actual) - 1] = '\0';165ATF_CHECK_STREQ_MSG(tests[i].expected, actual,166"sep_by_space = %zu", i);167}168}169170ATF_TC_WITHOUT_HEAD(strfmon_international_currency_code);171ATF_TC_BODY(strfmon_international_currency_code, tc)172{173const struct {174const char *locale;175const char *expected;176} tests[] = {177{ "en_US.UTF-8", "[USD123.45]" },178{ "de_DE.UTF-8", "[123,45 EUR]" },179{ "C", "[123.45]" },180};181size_t i;182char actual[100];183184for (i = 0; i < nitems(tests); ++i) {185if (setlocale(LC_MONETARY, tests[i].locale) == NULL)186atf_tc_skip("unable to setlocale()");187188strfmon(actual, sizeof(actual) - 1, "[%i]", 123.45);189ATF_CHECK_STREQ(tests[i].expected, actual);190}191}192193ATF_TC_WITHOUT_HEAD(strfmon_plus_or_parenthesis);194ATF_TC_BODY(strfmon_plus_or_parenthesis, tc)195{196const struct {197const char *format;198const char *locale;199const char *expected;200} tests[] = {201{ "%+n", "en_US.UTF-8", "[$123.45] [-$123.45]" },202{ "%+i", "en_US.UTF-8", "[USD123.45] [-USD123.45]" },203{ "%(n", "C", "[123.45] [(123.45)]" },204{ "%(i", "C", "[123.45] [(123.45)]" },205{ "%(n", "en_US.UTF-8", "[$123.45] [($123.45)]" },206{ "%(i", "en_US.UTF-8", "[USD123.45] [(USD123.45)]" },207{ "%n", "C", "[123.45] [-123.45]" },208{ "%i", "C", "[123.45] [-123.45]" },209{ "%n", "en_US.UTF-8", "[$123.45] [-$123.45]" },210{ "%i", "en_US.UTF-8", "[USD123.45] [-USD123.45]" },211};212size_t i;213char actual[100], format[50];214215for (i = 0; i < nitems(tests); ++i) {216if (setlocale(LC_MONETARY, tests[i].locale) == NULL)217atf_tc_skip("unable to setlocale(): %s",218tests[i].locale);219220snprintf(format, sizeof(format), "[%s] [%s]",221tests[i].format, tests[i].format);222strfmon(actual, sizeof(actual) - 1, format,223123.45, -123.45);224ATF_CHECK_STREQ_MSG(tests[i].expected, actual,225"[%s] %s", tests[i].format, tests[i].locale);226}227228/*229* The '+' flag was included in a conversion specification and230* the locale's positive_sign and negative_sign values would231* both be returned by localeconv() as empty strings.232*/233if (setlocale(LC_MONETARY, "C") == NULL)234atf_tc_skip("unable to setlocale(): %s", tests[i].locale);235236ATF_CHECK_ERRNO(EINVAL, strfmon(actual, sizeof(actual) - 1,237"[%+n] [%+n]", 123.45, -123.45));238239ATF_CHECK_ERRNO(EINVAL, strfmon(actual, sizeof(actual) - 1,240"[%+i] [%+i]", 123.45, -123.45));241}242243ATF_TC(strfmon_l);244ATF_TC_HEAD(strfmon_l, tc)245{246atf_tc_set_md_var(tc, "descr",247"checks strfmon_l under different locales");248}249ATF_TC_BODY(strfmon_l, tc)250{251const struct {252const char *locale;253const char *expected;254} tests[] = {255{ "C", "[ **1234.57] [ **1234.57]" },256{ "de_DE.UTF-8", "[ **1234,57 €] [ **1.234,57 EUR]" },257{ "en_GB.UTF-8", "[ £**1234.57] [ GBP**1,234.57]" },258};259locale_t loc;260size_t i;261char buf[100];262263for (i = 0; i < nitems(tests); ++i) {264loc = newlocale(LC_MONETARY_MASK, tests[i].locale, NULL);265ATF_REQUIRE(loc != NULL);266267strfmon_l(buf, sizeof(buf) - 1, loc, "[%^=*#6n] [%=*#6i]",2681234.567, 1234.567);269ATF_REQUIRE_STREQ(tests[i].expected, buf);270271freelocale(loc);272}273}274275ATF_TP_ADD_TCS(tp)276{277ATF_TP_ADD_TC(tp, strfmon_locale_thousands);278ATF_TP_ADD_TC(tp, strfmon_examples);279ATF_TP_ADD_TC(tp, strfmon_cs_precedes_0);280ATF_TP_ADD_TC(tp, strfmon_cs_precedes_1);281ATF_TP_ADD_TC(tp, strfmon_international_currency_code);282ATF_TP_ADD_TC(tp, strfmon_plus_or_parenthesis);283ATF_TP_ADD_TC(tp, strfmon_l);284return (atf_no_error());285}286287288