From nobody Tue Nov 01 00:46:25 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4N1WYG3stbz4gxty; Tue, 1 Nov 2022 00:46:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4N1WYG1LvZz3Qb0; Tue, 1 Nov 2022 00:46:26 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1667263586; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=HeJfK75Jda+KaAWV0w5MiL3dLfSYaPAkGbCWR9rWLvM=; b=wSl/3BPyVoPSMtLMSwXtnpYQBQRFL0D2aj5oJb/KwFNFAf/xT1cky9qenArMsq38PcermZ zRXpjZcvcb9AnmdQl0rhmO2AzsDwiVZ0w3BX87MmG2fZfiAbrVGHq1OUJgmE3dFrXHEMqe PfLsgXaB3rBOTSUgP9ZS3NVbEqHh4t3wYnhtllyVBkQlq9DbQKSSiA5famhENHDwrBI7RF GJKQbBquOiYKKqHWEMGRZV5axlh44JxdQyY89/P5ydMH9cF7qe71QqXcy4dSaARx3CXgTe /gsaYiKgOBLENJLL4in2r5VSUhOucVnS8wqd66G+KUd9aP+VQ3O0Oxy7qSkJ4Q== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4N1WYG090Tzwwx; Tue, 1 Nov 2022 00:46:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2A10kPx7041898; Tue, 1 Nov 2022 00:46:25 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2A10kPB4041897; Tue, 1 Nov 2022 00:46:25 GMT (envelope-from git) Date: Tue, 1 Nov 2022 00:46:25 GMT Message-Id: <202211010046.2A10kPB4041897@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 712101eb2bca - stable/13 - strfmon_test: Add some tests List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 712101eb2bcafc118cbfa088877f87de8a801c0f Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1667263586; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=HeJfK75Jda+KaAWV0w5MiL3dLfSYaPAkGbCWR9rWLvM=; b=m4A+geYQ4bDr9G2zhXsbw0KuJ+h97PBsXnwq6YX+F1YEoVaIpfFsq1PWOrMd0K/ss2+8ET nCO+P1SaTqJstHYsUJnEkzKXWU++XW7WEOaeDYWakzbiLmhaE8o8tQxxNZanBfuQ29XzYE S5Kt8kg+bOa9GUbir0u0WyQeUADG/gFQ2Roc7UQM7qD/Aeh+RSoPFAkds6mzjExN5a6D7c 8AmjTv+B6d5cMOBzCLgmmSEIzs+9WeukORKfSRozUBw+XYMzMSTA3YrtRzbo3+OIUM8dxM GbbYmmIfyh16sf/3BqRRFqRJyI6GKXBDTXk6HfELrsZ1G5yV0Z4eMHqTFvGG7w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1667263586; a=rsa-sha256; cv=none; b=JNnt11HWdhRNbznIMjQiJ+iFnsThK0w15IwdxiFA1o2izdLI5mYC1PdTYcsFSBZnmCKlkr E8JeCkJoUIyU4O9h+urlF2mb+LdZFhFJg/oG9dzbh1YH2+w6kY81oT2FVqNY4jtq7OZkw9 ify8ETJUQz70Jf47++PmV05xOHE63CP/Dh7/+mNx32Q386VQVy5jiwF33QuGAF2aDleBJQ hblZVNNU/hHiYLaj46mqisKKt6OO++cwFateb1ruW91zhqTgAgLNYIIw87V0KwnTo1p7hk Gk9nnOMYZdQr7mJYbtfai5/PRyuh5I+TSnYeBOjL80Gx23sP9P5W73Rdo94eLQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=712101eb2bcafc118cbfa088877f87de8a801c0f commit 712101eb2bcafc118cbfa088877f87de8a801c0f Author: Jose Luis Duran AuthorDate: 2022-10-16 04:01:17 +0000 Commit: Konstantin Belousov CommitDate: 2022-11-01 00:44:41 +0000 strfmon_test: Add some tests (cherry picked from commit 3f97d37ac586f43022fa54fdde881bf30550e67d) --- lib/libc/tests/stdlib/strfmon_test.c | 151 +++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) diff --git a/lib/libc/tests/stdlib/strfmon_test.c b/lib/libc/tests/stdlib/strfmon_test.c index 69398091baef..3e77a4f5290f 100644 --- a/lib/libc/tests/stdlib/strfmon_test.c +++ b/lib/libc/tests/stdlib/strfmon_test.c @@ -27,6 +27,8 @@ #include __FBSDID("$FreeBSD$"); +#include + #include #include #include @@ -64,8 +66,157 @@ ATF_TC_BODY(strfmon_locale_thousands, tc) ATF_CHECK_STREQ(expected, actual); } +ATF_TC_WITHOUT_HEAD(strfmon_examples); +ATF_TC_BODY(strfmon_examples, tc) +{ + const struct { + const char *format; + const char *expected; + } tests[] = { + { "%n", "[$123.45] [-$123.45] [$3,456.78]" }, + { "%11n", "[ $123.45] [ -$123.45] [ $3,456.78]" }, /* XXX */ + { "%#5n", "[ $ 123.45] [-$ 123.45] [ $ 3,456.78]" }, + { "%=*#5n", "[ $***123.45] [-$***123.45] [ $*3,456.78]" }, + { "%=0#5n", "[ $000123.45] [-$000123.45] [ $03,456.78]" }, + { "%^#5n", "[ $ 123.45] [-$ 123.45] [ $ 3456.78]" }, + { "%^#5.0n", "[ $ 123] [-$ 123] [ $ 3457]" }, + { "%^#5.4n", "[ $ 123.4500] [-$ 123.4500] [ $ 3456.7810]" }, + { "%(#5n", "[$ 123.45] [($ 123.45)] [$ 3,456.78]" }, /* XXX */ + { "%!(#5n", "[ 123.45] [( 123.45)] [ 3,456.78]" }, /* XXX */ + { "%-14#5.4n", "[ $ 123.4500 ] [-$ 123.4500 ] [ $ 3,456.7810 ]" }, + { "%14#5.4n", "[ $ 123.4500] [ -$ 123.4500] [ $ 3,456.7810]" }, + }; + size_t i; + char actual[100], format[50]; + + if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL) + atf_tc_skip("unable to setlocale()"); + + for (i = 0; i < nitems(tests); ++i) { + snprintf(format, sizeof(format), "[%s] [%s] [%s]", + tests[i].format, tests[i].format, tests[i].format); + strfmon(actual, sizeof(actual), format, + 123.45, -123.45, 3456.781); + ATF_CHECK_STREQ_MSG(tests[i].expected, actual, + "[%s]", tests[i].format); + } +} + +ATF_TC(strfmon_cs_precedes_0); +ATF_TC_HEAD(strfmon_cs_precedes_0, tc) +{ + atf_tc_set_md_var(tc, "descr", + "sep_by_space x sign_posn when cs_precedes = 0"); +} +ATF_TC_BODY(strfmon_cs_precedes_0, tc) +{ + const struct { + const char *expected; + } tests[] = { + /* sep_by_space x sign_posn */ + { "[(123.00$)] [-123.00$] [123.00$-] [123.00-$] [123.00$-]" }, + { "[(123.00 $)] [-123.00 $] [123.00 $-] [123.00 -$] [123.00 $-]" }, + { "[(123.00$)] [-123.00$] [123.00$ -] [123.00- $] [123.00$ -]" }, /* XXX */ + }; + size_t i, j; + struct lconv *lc; + char actual[100], buf[100]; + + if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL) + atf_tc_skip("unable to setlocale()"); + + lc = localeconv(); + lc->n_cs_precedes = 0; + + for (i = 0; i < nitems(tests); ++i) { + actual[0] = '\0'; + lc->n_sep_by_space = i; + + for (j = 0; j < 5; ++j) { + lc->n_sign_posn = j; + + strfmon(buf, sizeof(buf), "[%n] ", -123.0); + strlcat(actual, buf, sizeof(actual)); + } + + actual[strlen(actual) - 1] = '\0'; + ATF_CHECK_STREQ_MSG(tests[i].expected, actual, + "sep_by_space = %zu", i); + } +} + +ATF_TC(strfmon_cs_precedes_1); +ATF_TC_HEAD(strfmon_cs_precedes_1, tc) +{ + atf_tc_set_md_var(tc, "descr", + "sep_by_space x sign_posn when cs_precedes = 1"); +} +ATF_TC_BODY(strfmon_cs_precedes_1, tc) +{ + const struct { + const char *expected; + } tests[] = { + /* sep_by_space x sign_posn */ + { "[($123.00)] [-$123.00] [$123.00-] [-$123.00] [$-123.00]" }, + { "[($ 123.00)] [-$ 123.00] [$ 123.00-] [-$ 123.00] [$- 123.00]" }, + { "[($123.00)] [- $123.00] [$123.00 -] [- $123.00] [$ -123.00]" }, + }; + size_t i, j; + struct lconv *lc; + char actual[100], buf[100]; + + if (setlocale(LC_MONETARY, "en_US.UTF-8") == NULL) + atf_tc_skip("unable to setlocale()"); + + lc = localeconv(); + lc->n_cs_precedes = 1; + + for (i = 0; i < nitems(tests); ++i) { + actual[0] = '\0'; + lc->n_sep_by_space = i; + + for (j = 0; j < 5; ++j) { + lc->n_sign_posn = j; + + strfmon(buf, sizeof(buf), "[%n] ", -123.0); + strlcat(actual, buf, sizeof(actual)); + } + + actual[strlen(actual) - 1] = '\0'; + ATF_CHECK_STREQ_MSG(tests[i].expected, actual, + "sep_by_space = %zu", i); + } +} + +ATF_TC_WITHOUT_HEAD(strfmon_international_currency_code); +ATF_TC_BODY(strfmon_international_currency_code, tc) +{ + const struct { + const char *locale; + const char *expected; + } tests[] = { + { "en_US.UTF-8", "[USD 123.45]" }, /* XXX */ + { "de_DE.UTF-8", "[123,45 EUR ]" }, /* XXX */ + { "C", "[123.45]" }, /* XXX OOB access */ + }; + size_t i; + char actual[100]; + + for (i = 0; i < nitems(tests); ++i) { + if (setlocale(LC_MONETARY, tests[i].locale) == NULL) + atf_tc_skip("unable to setlocale()"); + + strfmon(actual, sizeof(actual), "[%i]", 123.45); + ATF_CHECK_STREQ(tests[i].expected, actual); + } +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, strfmon_locale_thousands); + ATF_TP_ADD_TC(tp, strfmon_examples); + 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); return (atf_no_error()); }