From a244e42201b55b2094e6128777a4af44c90450df Mon Sep 17 00:00:00 2001 From: Enji Cooper Date: Sun, 15 Nov 2015 04:50:08 +0000 Subject: [PATCH] Polish up iswctype_test - Split up the testcases into C locale and ja_JP.eucJP testcases. - Avoid a segfault in the event that setlocale fails, similar to r290843 - Replace `sizeof(x) / sizeof(*x)` pattern with `nitems(x)` MFC after: 1 week X-MFC with: r290532 Sponsored by: EMC / Isilon Storage Division --- lib/libc/tests/locale/iswctype_test.c | 76 +++++++++++++++++---------- 1 file changed, 49 insertions(+), 27 deletions(-) diff --git a/lib/libc/tests/locale/iswctype_test.c b/lib/libc/tests/locale/iswctype_test.c index 061f7b1c8bdb..14e57b4e29a3 100644 --- a/lib/libc/tests/locale/iswctype_test.c +++ b/lib/libc/tests/locale/iswctype_test.c @@ -34,6 +34,8 @@ #include __FBSDID("$FreeBSD$"); +#include +#include #include #include #include @@ -42,31 +44,45 @@ __FBSDID("$FreeBSD$"); #include -ATF_TC_WITHOUT_HEAD(iswctype_test); -ATF_TC_BODY(iswctype_test, tc) +static void +require_lc_ctype(const char *locale_name) { - wctype_t t; - int i, j; - struct { - const char *name; - int (*func)(wint_t); - } cls[] = { - { "alnum", iswalnum }, - { "alpha", iswalpha }, - { "blank", iswblank }, - { "cntrl", iswcntrl }, - { "digit", iswdigit }, - { "graph", iswgraph }, - { "lower", iswlower }, - { "print", iswprint }, - { "punct", iswpunct }, - { "space", iswspace }, - { "upper", iswupper }, - { "xdigit", iswxdigit } - }; + char *lc_ctype_set; - /* C/POSIX locale. */ - for (i = 0; i < sizeof(cls) / sizeof(*cls); i++) { + lc_ctype_set = setlocale(LC_CTYPE, locale_name); + if (lc_ctype_set == NULL) + atf_tc_fail("setlocale(LC_CTYPE, \"%s\") failed; errno=%d", + locale_name, errno); + + ATF_REQUIRE(strcmp(lc_ctype_set, locale_name) == 0); +} + +static wctype_t t; +static int i, j; +static struct { + const char *name; + int (*func)(wint_t); +} cls[] = { + { "alnum", iswalnum }, + { "alpha", iswalpha }, + { "blank", iswblank }, + { "cntrl", iswcntrl }, + { "digit", iswdigit }, + { "graph", iswgraph }, + { "lower", iswlower }, + { "print", iswprint }, + { "punct", iswpunct }, + { "space", iswspace }, + { "upper", iswupper }, + { "xdigit", iswxdigit } +}; + +ATF_TC_WITHOUT_HEAD(iswctype_c_locale_test); +ATF_TC_BODY(iswctype_c_locale_test, tc) +{ + + require_lc_ctype("C"); + for (i = 0; i < nitems(cls); i++) { t = wctype(cls[i].name); ATF_REQUIRE(t != 0); for (j = 0; j < 256; j++) @@ -76,10 +92,15 @@ ATF_TC_BODY(iswctype_test, tc) ATF_REQUIRE(t == 0); for (i = 0; i < 256; i++) ATF_REQUIRE(iswctype(i, t) == 0); +} - /* Japanese (EUC) locale. */ - ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0); - for (i = 0; i < sizeof(cls) / sizeof(*cls); i++) { +ATF_TC_WITHOUT_HEAD(iswctype_euc_jp_test); +ATF_TC_BODY(iswctype_euc_jp_test, tc) +{ + + require_lc_ctype("ja_JP.eucJP"); + + for (i = 0; i < nitems(cls); i++) { t = wctype(cls[i].name); ATF_REQUIRE(t != 0); for (j = 0; j < 65536; j++) @@ -94,7 +115,8 @@ ATF_TC_BODY(iswctype_test, tc) ATF_TP_ADD_TCS(tp) { - ATF_TP_ADD_TC(tp, iswctype_test); + ATF_TP_ADD_TC(tp, iswctype_c_locale_test); + ATF_TP_ADD_TC(tp, iswctype_euc_jp_test); return (atf_no_error()); }