1/* $NetBSD: t_wctype.c,v 1.3 2022/05/24 20:50:20 andvar Exp $ */ 2 3/*- 4 * Copyright (c) 2017 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Konrad Schroder 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33__COPYRIGHT("@(#) Copyright (c) 2017\ 34 The NetBSD Foundation, inc. All rights reserved."); 35__RCSID("$NetBSD: t_wctype.c,v 1.3 2022/05/24 20:50:20 andvar Exp $"); 36 37#include <locale.h> 38#include <stdio.h> 39#include <string.h> 40#include <vis.h> 41#include <ctype.h> 42#include <wctype.h> 43#include <wchar.h> 44 45#include <atf-c.h> 46 47static const char *typenames[] = { 48 "alnum", 49 "alpha", 50 "blank", 51 "cntrl", 52 "digit", 53 "graph", 54 "ideogram", 55 "lower", 56 "phonogram", 57 "print", 58 "punct", 59 "rune", 60 "space", 61 "special", 62 "upper", 63 "xdigit", 64}; 65 66static struct test { 67 const char *locale; 68 const struct { 69 const char *in; 70 const char *out; 71 } classes[16]; 72} tests[] = { 73 { 74 "C", 75 { 76 { /* alnum */ "abc123", "&^%$" }, 77 { /* alpha */ "ABCabc", "123*&^" }, 78 { /* blank */ " \t", "abc123%$^&\r\n" }, 79 { /* cntrl */ "\a", "abc123^&*" }, 80 { /* digit */ "123", "abc*()" }, 81 { /* graph */ "abc123ABC[];?~", " " }, 82 { /* ideogram */ "", "" }, 83 { /* lower */ "abc", "ABC123&*(" }, 84 { /* phonogram */ "", "" }, 85 { /* print */ "abcABC123%^&* ", "\r\n\t" }, 86 { /* punct */ ".,;:!?", "abc123 \n" }, 87 { /* rune */ "", "" }, 88 { /* space */ " \t\r\n", "abc123ABC&*(" }, 89 { /* special */ "", "" }, 90 { /* upper */ "ABC", "abc123&*(" }, 91 { /* xdigit */ "123abcABC", "xyz" }, 92 }, 93 }, { 94 "en_US.UTF-8", 95 { 96 { /* alnum */ "abc123", "&^%$" }, 97 { /* alpha */ "ABCabc", "123*&^" }, 98 { /* blank */ " \t", "abc123%$^&\r\n" }, 99 { /* cntrl */ "\a", "abc123^&*" }, 100 { /* digit */ "123", "abc*()" }, 101 { /* graph */ "abc123ABC[];?~", " " }, 102 { /* ideogram */ "", "" }, 103 { /* lower */ "abc", "ABC123&*(" }, 104 { /* phonogram */ "", "" }, 105 { /* print */ "abcABC123%^&* ", "\r\n\t" }, 106 { /* punct */ ".,;:!?", "abc123 \n" }, 107 { /* rune */ "", "" }, 108 { /* space */ " \t\r\n", "abc123ABC&*(" }, 109 { /* special */ "", "" }, 110 { /* upper */ "ABC", "abc123&*(" }, 111 { /* xdigit */ "123abcABC", "xyz" }, 112 }, 113 }, { 114 "ru_RU.KOI8-R", 115 { 116 { /* alnum */ "abc123i\xa3\xb3\xd6", "&^%$" }, 117 { /* alpha */ "ABCabci\xa3\xb3\xd6", "123*&^" }, 118 { /* blank */ " \t", "abc123%$^&\r\n" }, 119 { /* cntrl */ "\a", "abc123^&*" }, 120 { /* digit */ "123", "abc*()" }, 121 { /* graph */ "abc123ABC[];?~i\xa3\xb3\xd6", " " }, 122 { /* ideogram */ "", "" }, 123 { /* lower */ "abci\xa3\xd6", "ABC123&*(\xb3\xf6" }, 124 { /* phonogram */ "", "" }, 125 { /* print */ "abcABC123%^&* \xa3\xb3\xd6\xbf", "\r\n\t" }, 126 { /* punct */ ".,;:!?", "abc123 \n" }, 127 { /* rune */ "", "" }, 128 { /* space */ " \t\r\n", "abc123ABC&*(\xa3\xb3\xd6" }, 129 { /* special */ "", "" }, 130 { /* upper */ "ABC\xb3\xe0\xff", "abc123&*(\xa3\xc0\xdf" }, 131 { /* xdigit */ "123abcABC", "xyz\xc1\xc5" }, 132 } 133 }, { 134 NULL, 135 { {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, {NULL, NULL}, } 136 } 137}; 138 139static void testall(int, int, wchar_t, wctype_t, int); 140 141static void 142h_ctype(const struct test *t) 143{ 144 wctype_t wct; 145 wchar_t wc; 146 int i; 147 const char *cp; 148 149 ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C"); 150 printf("Trying locale %s...\n", t->locale); 151 ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL); 152 printf("Using locale %s\n", setlocale(LC_CTYPE, NULL)); 153 154 for (i = 0; i < 16; i++) { 155 /* Skip any type that has no chars */ 156 if (t->classes[i].in[0] == '\0' && 157 t->classes[i].out[0] == '\0') 158 continue; 159 160 /* First examine wctype */ 161 wct = wctype(typenames[i]); 162 ATF_REQUIRE(wct != 0); 163 164 /* Convert in to wide and check that they match */ 165 for (cp = (const char *)(t->classes[i].in); *cp; ++cp) { 166 wc = btowc((unsigned char)*cp); 167 testall(i, (unsigned char)*cp, wc, wct, 1); 168 } 169 /* Convert out to wide and check that they do not match */ 170 for (cp = (const char *)(t->classes[i].out); *cp; ++cp) { 171 wc = btowc((unsigned char)*cp); 172 testall(i, (unsigned char)*cp, wc, wct, 0); 173 } 174 } 175} 176 177void testall(int idx, int c, wchar_t wc, wctype_t wct, int inout) 178{ 179 printf("Testing class %d (%s), char %c (0x%2.2x), wc %x, wct %ld, expect %d\n", 180 idx, typenames[idx], c, c, wc, (long int)wct, inout); 181 ATF_REQUIRE(!!iswctype(wc, wct) == inout); 182 183 /* Switch based on wct and call is{,w}* to check */ 184 switch (idx) { 185 case 0: /* alnum */ 186 ATF_REQUIRE_EQ(!!isalnum(c), inout); 187 ATF_REQUIRE_EQ(!!iswalnum(wc), inout); 188 break; 189 case 1: /* alpha */ 190 ATF_REQUIRE_EQ(!!isalpha(c), inout); 191 ATF_REQUIRE_EQ(!!iswalpha(wc), inout); 192 break; 193 case 2: /* blank */ 194 ATF_REQUIRE_EQ(!!isblank(c), inout); 195 ATF_REQUIRE_EQ(!!iswblank(wc), inout); 196 break; 197 case 3: /* cntrl */ 198 ATF_REQUIRE_EQ(!!iscntrl(c), inout); 199 ATF_REQUIRE_EQ(!!iswcntrl(wc), inout); 200 break; 201 case 4: /* digit */ 202 ATF_REQUIRE_EQ(!!isdigit(c), inout); 203 ATF_REQUIRE_EQ(!!iswdigit(wc), inout); 204 break; 205 case 5: /* graph */ 206 ATF_REQUIRE_EQ(!!isgraph(c), inout); 207 ATF_REQUIRE_EQ(!!iswgraph(wc), inout); 208 break; 209 case 6: /* ideogram */ 210#if 0 211 ATF_REQUIRE_EQ(!!isideogram(c), inout); 212 ATF_REQUIRE_EQ(!!iswideogram(wc), inout); 213#endif 214 break; 215 case 7: /* lower */ 216 ATF_REQUIRE_EQ(!!islower(c), inout); 217 ATF_REQUIRE_EQ(!!iswlower(wc), inout); 218 break; 219 case 8: /* phonogram */ 220#if 0 221 ATF_REQUIRE_EQ(!!isphonogram(c), inout); 222 ATF_REQUIRE_EQ(!!iswphonogram(wc), inout); 223#endif 224 break; 225 case 9: /* print */ 226 ATF_REQUIRE_EQ(!!isprint(c), inout); 227 ATF_REQUIRE_EQ(!!iswprint(wc), inout); 228 break; 229 case 10: /* punct */ 230 ATF_REQUIRE_EQ(!!ispunct(c), inout); 231 ATF_REQUIRE_EQ(!!iswpunct(wc), inout); 232 break; 233 case 11: /* rune */ 234#if 0 235 ATF_REQUIRE_EQ(!!isrune(c), inout); 236 ATF_REQUIRE_EQ(!!iswrune(wc), inout); 237#endif 238 break; 239 case 12: /* space */ 240 ATF_REQUIRE_EQ(!!isspace(c), inout); 241 ATF_REQUIRE_EQ(!!iswspace(wc), inout); 242 break; 243 case 13: /* special */ 244#if 0 245 ATF_REQUIRE_EQ(!!isspecial(c), inout); 246 ATF_REQUIRE_EQ(!!iswspecial(wc), inout); 247#endif 248 break; 249 case 14: /* upper */ 250 ATF_REQUIRE_EQ(!!isupper(c), inout); 251 ATF_REQUIRE_EQ(!!iswupper(wc), inout); 252 break; 253 case 15: /* xdigit */ 254 ATF_REQUIRE_EQ(!!isxdigit(c), inout); 255 ATF_REQUIRE_EQ(!!iswxdigit(wc), inout); 256 break; 257 } 258} 259 260ATF_TC(ctype); 261 262ATF_TC_HEAD(ctype, tc) 263{ 264 atf_tc_set_md_var(tc, "descr", 265 "Checks is* and isw* under different locales"); 266} 267 268ATF_TC_BODY(ctype, tc) 269{ 270 struct test *t; 271 272 for (t = &tests[0]; t->locale != NULL; ++t) 273 h_ctype(t); 274} 275 276ATF_TP_ADD_TCS(tp) 277{ 278 279 ATF_TP_ADD_TC(tp, ctype); 280 281 return atf_no_error(); 282} 283