From nobody Thu Sep 7 06:41:55 2023 X-Original-To: dev-commits-src-main@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 4Rh8mN1wXyz4slQv; Thu, 7 Sep 2023 06:41:56 +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 4Rh8mN1VVSz4M6x; Thu, 7 Sep 2023 06:41:56 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694068916; 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=JhadLKC7qqw0ZfttLVJKCzDm73JazZK7bfTQpHJdVi8=; b=QLjHdFRpYjERQRAed6Y5/xgEx2rkJ+/LbQXcGSIa3xTT/PaU2v+RwtqOE1HPDWeUReYPE8 O3/8O9SG6AMakvH4GHCM8kyKuBPc6sxBq3VMWDw+GlpLLfIk6Cr7m7c1jhAqw0prqAZJPW ZQh/3MJ3A8mdUle9MKeCo1PByGUpUPeE8gmQzO27otFa6BamjqnUOqA7MeQDCKlVcObTah xZAe/c8M+DnbFq9qFyFPRyFCknyoFBdmO9tMdksL7ZuZYngeesKy212/jmRfxxhowTtikQ TGK0BeDFUaj4Gv1EY6ZI1abVOwMnm7Jrxuu4kBUBYHKMjGXzXsXYC1Vw/JP8qw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1694068916; a=rsa-sha256; cv=none; b=UbTNoxkaJj4BKHSP3Z9g3T5SA39hxZ9gby2ka0g+sUafvn5sqtwY/kRosOrBDbXygWfGIM DmYR1v6/MhcL4EscCdqAzTVd+5/T/fAPGTSDErBJ61KvFMC2kl10YMys0ArfN7kQLuj6Le 4jKEiE5D/a1MRRNcktoK+HWlEXUZuCpW99f7tzUQ7i0jvvwFSIgtIiUlngGVwhIbGi/TS3 wYxTo0OsZxgxhhF1zXxSfP+XFW8D9iki5Fl6jBWVLXKaaaVTmoOopE6DZMXfFskhSKNG1e JVWYzRF+86RkUVcmwq6kOg9+6osloTkuAyAcHQ3A360Qau9ikoO0zM0JXaVnag== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1694068916; 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=JhadLKC7qqw0ZfttLVJKCzDm73JazZK7bfTQpHJdVi8=; b=sIykUn8/N4vD/FWaXFzsQFxOZK9Wi9XKZurnsd47rM5iuZtE19k7Z2La+aouYL6tO+PczJ n2BX+QNFubsgvZYCkY8TvDUWoh2FgoJ7EcB5wF9xgANeldiDUcM1MAvzm3lAnXwOkzCr3H lNHFKajJ4ycm3vNL8bc1XcR2AjWBbAsMOvt56+Me6PpDmr68Uz9S0JvD+MtIY1gZix3ej9 0jZ9tHRYxMJ5jYv4FVkIfIck6EvMSnMozGaO1DhCM2KJKyo5A78MATWJOaNy5rHENnZsUu h9lBYQZzKwGxS9FheZR2qJwG0028ipANq/kK4b30L0Q7hAI7N6VnWfyC3sKxzQ== 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 4Rh8mN0ZmCzpYQ; Thu, 7 Sep 2023 06:41:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3876ftH2002071; Thu, 7 Sep 2023 06:41:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3876ftRJ002068; Thu, 7 Sep 2023 06:41:55 GMT (envelope-from git) Date: Thu, 7 Sep 2023 06:41:55 GMT Message-Id: <202309070641.3876ftRJ002068@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 4ec9ee991276 - main - libc: Add a wide version of snprintf_test. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 4ec9ee9912765ac4ca57353999caa92a23283d8e Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=4ec9ee9912765ac4ca57353999caa92a23283d8e commit 4ec9ee9912765ac4ca57353999caa92a23283d8e Author: Dag-Erling Smørgrav AuthorDate: 2023-09-07 06:14:30 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2023-09-07 06:40:14 +0000 libc: Add a wide version of snprintf_test. Reviewed by: imp, emaste Differential Revision: https://reviews.freebsd.org/D41726 --- lib/libc/tests/stdio/Makefile | 1 + lib/libc/tests/stdio/snprintf_test.c | 21 +++--- lib/libc/tests/stdio/swprintf_test.c | 140 +++++++++++++++++++++++++++++++++++ 3 files changed, 152 insertions(+), 10 deletions(-) diff --git a/lib/libc/tests/stdio/Makefile b/lib/libc/tests/stdio/Makefile index f7383e5b3ef4..2a8a4e69a6c4 100644 --- a/lib/libc/tests/stdio/Makefile +++ b/lib/libc/tests/stdio/Makefile @@ -18,6 +18,7 @@ ATF_TESTS_C+= printfloat_test ATF_TESTS_C+= scanfloat_test ATF_TESTS_C+= snprintf_test ATF_TESTS_C+= sscanf_test +ATF_TESTS_C+= swprintf_test ATF_TESTS_C+= swscanf_test SRCS.fopen2_test= fopen_test.c diff --git a/lib/libc/tests/stdio/snprintf_test.c b/lib/libc/tests/stdio/snprintf_test.c index 0c335bc1a512..10d938435b96 100644 --- a/lib/libc/tests/stdio/snprintf_test.c +++ b/lib/libc/tests/stdio/snprintf_test.c @@ -12,18 +12,19 @@ #include -#define SNPRINTF_TEST(output, format, ...) \ +#ifndef nitems +#define nitems(a) (sizeof(a) / sizeof(a[0])) +#endif + +#define SNPRINTF_TEST(output, format, ...) \ do { \ char buf[256]; \ - assert(output == NULL || strlen(output) < sizeof(buf)); \ - int ret = snprintf(buf, sizeof(buf), format, __VA_ARGS__); \ - if (output == NULL) { \ - ATF_CHECK_EQ(-1, ret); \ - } else { \ - ATF_CHECK_EQ(strlen(output), ret); \ - if (ret > 0) { \ - ATF_CHECK_STREQ(output, buf); \ - } \ + assert(strlen(output) < nitems(buf)); \ + int ret = snprintf(buf, nitems(buf), format, \ + __VA_ARGS__); \ + ATF_CHECK_EQ(strlen(output), ret); \ + if (ret > 0) { \ + ATF_CHECK_EQ(0, strcmp(output, buf)); \ } \ } while (0) diff --git a/lib/libc/tests/stdio/swprintf_test.c b/lib/libc/tests/stdio/swprintf_test.c new file mode 100644 index 000000000000..23859b5cd2e1 --- /dev/null +++ b/lib/libc/tests/stdio/swprintf_test.c @@ -0,0 +1,140 @@ +/*- + * Copyright (c) 2023 Dag-Erling Smørgrav + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include +#include + +#include + +#ifndef nitems +#define nitems(a) (sizeof(a) / sizeof(a[0])) +#endif + +#define SWPRINTF_TEST(output, format, ...) \ + do { \ + wchar_t buf[256]; \ + assert(wcslen(L##output) < nitems(buf)); \ + int ret = swprintf(buf, nitems(buf), L##format, \ + __VA_ARGS__); \ + ATF_CHECK_EQ(wcslen(L##output), ret); \ + if (ret > 0) { \ + ATF_CHECK_EQ(0, wcscmp(L##output, buf)); \ + } \ + } while (0) + +ATF_TC_WITHOUT_HEAD(swprintf_b); +ATF_TC_BODY(swprintf_b, tc) +{ + SWPRINTF_TEST("0", "%b", 0); + SWPRINTF_TEST(" 0", "%12b", 0); + SWPRINTF_TEST("000000000000", "%012b", 0); + SWPRINTF_TEST("1", "%b", 1); + SWPRINTF_TEST(" 1", "%12b", 1); + SWPRINTF_TEST("000000000001", "%012b", 1); + SWPRINTF_TEST("1111111111111111111111111111111", "%b", INT_MAX); + SWPRINTF_TEST("0", "%#b", 0); + SWPRINTF_TEST(" 0", "%#12b", 0); + SWPRINTF_TEST("000000000000", "%#012b", 0); + SWPRINTF_TEST("0b1", "%#b", 1); + SWPRINTF_TEST(" 0b1", "%#12b", 1); + SWPRINTF_TEST("0b0000000001", "%#012b", 1); + SWPRINTF_TEST("0b1111111111111111111111111111111", "%#b", INT_MAX); +} + +ATF_TC_WITHOUT_HEAD(swprintf_B); +ATF_TC_BODY(swprintf_B, tc) +{ + SWPRINTF_TEST("0", "%B", 0); + SWPRINTF_TEST(" 0", "%12B", 0); + SWPRINTF_TEST("000000000000", "%012B", 0); + SWPRINTF_TEST("1", "%B", 1); + SWPRINTF_TEST(" 1", "%12B", 1); + SWPRINTF_TEST("000000000001", "%012B", 1); + SWPRINTF_TEST("1111111111111111111111111111111", "%B", INT_MAX); + SWPRINTF_TEST("0", "%#B", 0); + SWPRINTF_TEST(" 0", "%#12B", 0); + SWPRINTF_TEST("000000000000", "%#012B", 0); + SWPRINTF_TEST("0B1", "%#B", 1); + SWPRINTF_TEST(" 0B1", "%#12B", 1); + SWPRINTF_TEST("0B0000000001", "%#012B", 1); + SWPRINTF_TEST("0B1111111111111111111111111111111", "%#B", INT_MAX); +} + +ATF_TC_WITHOUT_HEAD(swprintf_d); +ATF_TC_BODY(swprintf_d, tc) +{ + SWPRINTF_TEST("0", "%d", 0); + SWPRINTF_TEST(" 0", "%12d", 0); + SWPRINTF_TEST("000000000000", "%012d", 0); + SWPRINTF_TEST("1", "%d", 1); + SWPRINTF_TEST(" 1", "%12d", 1); + SWPRINTF_TEST("000000000001", "%012d", 1); + SWPRINTF_TEST("2147483647", "%d", INT_MAX); + SWPRINTF_TEST(" 2147483647", "%12d", INT_MAX); + SWPRINTF_TEST("002147483647", "%012d", INT_MAX); + SWPRINTF_TEST("2,147,483,647", "%'d", INT_MAX); +} + +ATF_TC_WITHOUT_HEAD(swprintf_x); +ATF_TC_BODY(swprintf_x, tc) +{ + SWPRINTF_TEST("0", "%x", 0); + SWPRINTF_TEST(" 0", "%12x", 0); + SWPRINTF_TEST("000000000000", "%012x", 0); + SWPRINTF_TEST("1", "%x", 1); + SWPRINTF_TEST(" 1", "%12x", 1); + SWPRINTF_TEST("000000000001", "%012x", 1); + SWPRINTF_TEST("7fffffff", "%x", INT_MAX); + SWPRINTF_TEST(" 7fffffff", "%12x", INT_MAX); + SWPRINTF_TEST("00007fffffff", "%012x", INT_MAX); + SWPRINTF_TEST("0", "%#x", 0); + SWPRINTF_TEST(" 0", "%#12x", 0); + SWPRINTF_TEST("000000000000", "%#012x", 0); + SWPRINTF_TEST("0x1", "%#x", 1); + SWPRINTF_TEST(" 0x1", "%#12x", 1); + SWPRINTF_TEST("0x0000000001", "%#012x", 1); + SWPRINTF_TEST("0x7fffffff", "%#x", INT_MAX); + SWPRINTF_TEST(" 0x7fffffff", "%#12x", INT_MAX); + SWPRINTF_TEST("0x007fffffff", "%#012x", INT_MAX); +} + +ATF_TC_WITHOUT_HEAD(swprintf_X); +ATF_TC_BODY(swprintf_X, tc) +{ + SWPRINTF_TEST("0", "%X", 0); + SWPRINTF_TEST(" 0", "%12X", 0); + SWPRINTF_TEST("000000000000", "%012X", 0); + SWPRINTF_TEST("1", "%X", 1); + SWPRINTF_TEST(" 1", "%12X", 1); + SWPRINTF_TEST("000000000001", "%012X", 1); + SWPRINTF_TEST("7FFFFFFF", "%X", INT_MAX); + SWPRINTF_TEST(" 7FFFFFFF", "%12X", INT_MAX); + SWPRINTF_TEST("00007FFFFFFF", "%012X", INT_MAX); + SWPRINTF_TEST("0", "%#X", 0); + SWPRINTF_TEST(" 0", "%#12X", 0); + SWPRINTF_TEST("000000000000", "%#012X", 0); + SWPRINTF_TEST("0X1", "%#X", 1); + SWPRINTF_TEST(" 0X1", "%#12X", 1); + SWPRINTF_TEST("0X0000000001", "%#012X", 1); + SWPRINTF_TEST("0X7FFFFFFF", "%#X", INT_MAX); + SWPRINTF_TEST(" 0X7FFFFFFF", "%#12X", INT_MAX); + SWPRINTF_TEST("0X007FFFFFFF", "%#012X", INT_MAX); +} + +ATF_TP_ADD_TCS(tp) +{ + setlocale(LC_NUMERIC, "en_US.UTF-8"); + ATF_TP_ADD_TC(tp, swprintf_b); + ATF_TP_ADD_TC(tp, swprintf_B); + ATF_TP_ADD_TC(tp, swprintf_d); + ATF_TP_ADD_TC(tp, swprintf_x); + ATF_TP_ADD_TC(tp, swprintf_X); + return (atf_no_error()); +}