1 /* $NetBSD: t_mbstowcs.c,v 1.2 2017/07/12 17:32:51 perseant Exp $ */ 2 3 /*- 4 * Copyright (c) 2011 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /*- 30 * Copyright (c)2003 Citrus Project, 31 * All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 45 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 */ 54 55 #include <sys/cdefs.h> 56 __COPYRIGHT("@(#) Copyright (c) 2011\ 57 The NetBSD Foundation, inc. All rights reserved."); 58 __RCSID("$NetBSD: t_mbstowcs.c,v 1.2 2017/07/12 17:32:51 perseant Exp $"); 59 60 #include <errno.h> 61 #include <locale.h> 62 #include <stdio.h> 63 #include <stdlib.h> 64 #include <string.h> 65 #include <vis.h> 66 #include <wchar.h> 67 68 #include <atf-c.h> 69 70 #define REQUIRE_ERRNO(x, v) \ 71 ATF_REQUIRE_MSG((x) != (v), "%s: %s", #x, strerror(errno)) 72 73 #define SIZE 256 74 75 static struct test { 76 const char *locale; 77 const char *data; 78 wchar_t wchars[64]; 79 int widths[64]; 80 int width; 81 } tests[] = { 82 { 83 "en_US.UTF-8", 84 "[\001\177][\302\200\337\277][\340\240\200\357\277\277][\360\220\200" 85 "\200\367\277\277\277][\370\210\200\200\200\373\277\277\277\277][\374" 86 "\204\200\200\200\200\375\277\277\277\277\277]", 87 { 88 0x5B, 0x01, 0x7F, 0x5D, 0x5B, 0x80, 0x07FF, 0x5D, 0x5B, 0x0800, 89 0xFFFF, 0x5D, 0x5B, 0x10000, 0x1FFFFF, 0x5D, 0x5B, 0x200000, 90 0x3FFFFFF, 0x5D, 0x5B, 0x4000000, 0x7FFFFFFF, 0x5D, 0x0A 91 }, 92 { 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 1, 1, -1, -1, 93 1, 1, -1, -1, 1, 1, -1, -1, 1, -1 94 }, 95 -1 96 }, { 97 "ja_JP.ISO2022-JP", 98 "\033$B#J#I#S$G$9!#\033(Baaaa\033$B$\"$$$&$($*\033(B", 99 { 100 #ifdef __STDC_ISO_10646__ 101 0xFF2A, 0xFF29, 0xFF33, 0x3067, 0x3059, 102 0x3002, 0x61, 0x61, 0x61, 0x61, 0x3042, 0x3044, 103 0x3046, 0x3048, 0x304A, 0x0A 104 #else 105 0x4200234A, 0x42002349, 0x42002353, 0x42002447, 0x42002439, 106 0x42002123, 0x61, 0x61, 0x61, 0x61, 0x42002422, 0x42002424, 107 0x42002426, 0x42002428, 0x4200242A, 0x0A 108 #endif 109 }, 110 { 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 }, 111 26 112 }, { 113 "ja_JP.SJIS", 114 "\202r\202i\202h\202r\202\305\202\267\201Baaaa\202\240\202\242" 115 "\202\244\202\246\202\250", 116 { 117 #ifdef __STDC_ISO_10646__ 118 0xFF33, 0xFF2A, 0xFF29, 0xFF33, 0x3067, 0x3059, 0x3002, 0x61, 119 0x61, 0x61, 0x61, 0x3042, 0x3044, 0x3046, 0x3048, 0x304A, 0x0A 120 #else 121 0x8272, 0x8269, 0x8268, 0x8272, 0x82C5, 0x82B7, 0x8142, 0x61, 122 0x61, 0x61, 0x61, 0x82A0, 0x82A2, 0x82A4, 0x82A6, 0x82A8, 0x0A 123 #endif 124 }, 125 { 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 }, 126 28 127 }, { 128 "ja_JP.eucJP", 129 "\243\305\243\325\243\303\244\307\244\271\241\243aaaa\244\242\244" 130 "\244\244\246\244\250\244\252", 131 { 132 #ifdef __STDC_ISO_10646__ 133 0xFF25, 0xFF35, 0xFF23, 0x3067, 0x3059, 0x3002, 0x61, 0x61, 0x61, 134 0x61, 0x3042, 0x3044, 0x3046, 0x3048, 0x304A, 0x0A 135 #else 136 0xA3C5, 0xA3D5, 0xA3C3, 0xA4C7, 0xA4B9, 0xA1A3, 0x61, 0x61, 0x61, 137 0x61, 0xA4A2, 0xA4A4, 0xA4A6, 0xA4A8, 0xA4AA, 0x0A 138 #endif 139 }, 140 { 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 2, 2, -1 }, 141 26 142 }, { 143 NULL, 144 NULL, 145 {}, 146 {}, 147 0 148 } 149 }; 150 151 ATF_TC(mbstowcs_basic); 152 ATF_TC_HEAD(mbstowcs_basic, tc) 153 { 154 atf_tc_set_md_var(tc, "descr", 155 "Checks wide character functions with different locales"); 156 } 157 ATF_TC_BODY(mbstowcs_basic, tc) 158 { 159 struct test *t; 160 161 for (t = &tests[0]; t->data != NULL; ++t) { 162 wchar_t wbuf[SIZE]; 163 char buf[SIZE]; 164 char visbuf[SIZE]; 165 char *str; 166 int i; 167 168 ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C"); 169 ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL); 170 171 (void)strvis(visbuf, t->data, VIS_WHITE | VIS_OCTAL); 172 (void)printf("Checking string: \"%s\"\n", visbuf); 173 174 ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL); 175 (void)printf("Using locale: %s\n", str); 176 177 REQUIRE_ERRNO((ssize_t)mbstowcs(wbuf, t->data, SIZE-1), -1); 178 REQUIRE_ERRNO((ssize_t)wcstombs(buf, wbuf, SIZE-1), -1); 179 180 if (strcmp(buf, t->data) != 0) { 181 (void)strvis(visbuf, buf, VIS_WHITE | VIS_OCTAL); 182 (void)printf("Conversion to wcs and back failed: " 183 "\"%s\"\n", visbuf); 184 atf_tc_fail("Test failed"); 185 } 186 187 /* The output here is implementation-dependent. */ 188 189 for (i = 0; wbuf[i] != 0; ++i) { 190 if (wbuf[i] == t->wchars[i] && 191 wcwidth(wbuf[i]) == t->widths[i]) 192 continue; 193 194 (void)printf("At position %d:\n", i); 195 (void)printf(" expected: 0x%04X (%d)\n", 196 t->wchars[i], t->widths[i]); 197 (void)printf(" got : 0x%04X (%d)\n", wbuf[i], 198 wcwidth(wbuf[i])); 199 atf_tc_fail("Test failed"); 200 } 201 202 if (wcswidth(wbuf, SIZE-1) != t->width) { 203 (void)printf("Incorrect wcswidth:\n"); 204 (void)printf(" expected: %d\n", t->width); 205 (void)printf(" got : %d\n", wcswidth(wbuf, SIZE-1)); 206 atf_tc_fail("Test failed"); 207 } 208 209 (void)printf("Ok.\n"); 210 } 211 } 212 213 ATF_TP_ADD_TCS(tp) 214 { 215 ATF_TP_ADD_TC(tp, mbstowcs_basic); 216 217 return atf_no_error(); 218 } 219