git: 4ec9ee991276 - main - libc: Add a wide version of snprintf_test.
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 07 Sep 2023 06:41:55 UTC
The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=4ec9ee9912765ac4ca57353999caa92a23283d8e commit 4ec9ee9912765ac4ca57353999caa92a23283d8e Author: Dag-Erling Smørgrav <des@FreeBSD.org> AuthorDate: 2023-09-07 06:14:30 +0000 Commit: Dag-Erling Smørgrav <des@FreeBSD.org> 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 <atf-c.h> -#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 <assert.h> +#include <limits.h> +#include <locale.h> +#include <stdint.h> +#include <stdio.h> +#include <wchar.h> + +#include <atf-c.h> + +#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()); +}