Home | History | Annotate | Line # | Download | only in locale
      1 /* $NetBSD: t_mbrtowc.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  * This code is derived from software contributed to The NetBSD Foundation
      8  * by YAMAMOTO Takashi
      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 /*-
     33  * Copyright (c)2003 Citrus Project,
     34  * All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  *
     45  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     46  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     47  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     48  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     49  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     50  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     51  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     53  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     54  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     55  * SUCH DAMAGE.
     56  */
     57 
     58 #include <sys/cdefs.h>
     59 __COPYRIGHT("@(#) Copyright (c) 2011\
     60  The NetBSD Foundation, inc. All rights reserved.");
     61 __RCSID("$NetBSD: t_mbrtowc.c,v 1.2 2017/07/12 17:32:51 perseant Exp $");
     62 
     63 #include <errno.h>
     64 #include <locale.h>
     65 #include <stdio.h>
     66 #include <stdlib.h>
     67 #include <string.h>
     68 #include <vis.h>
     69 #include <wchar.h>
     70 
     71 #include <atf-c.h>
     72 
     73 #define SIZE 256
     74 
     75 static struct test {
     76 	const char *locale;
     77 	const char *data;
     78 	const wchar_t wchars[64];
     79 	const wchar_t widths[64];
     80 	size_t length;
     81 } tests[] = {
     82 {
     83 	"C",
     84 	"ABCD01234_\\",
     85 	{ 0x41, 0x42, 0x43, 0x44, 0x30, 0x31, 0x32, 0x33, 0x34, 0x5F, 0x5C },
     86 	{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
     87 	11
     88 }, {
     89 	"en_US.UTF-8",
     90 	"[\001\177][\302\200\337\277][\340\240\200\357\277\277][\360\220\200"
     91 	"\200\367\277\277\277][\370\210\200\200\200\373\277\277\277\277][\374"
     92 	"\204\200\200\200\200\375\277\277\277\277\277]",
     93 	{ 0x5b, 0x01, 0x7f, 0x5d, 0x5b, 0x80, 0x7ff, 0x5d, 0x5b, 0x800, 0xffff,
     94 	  0x5d, 0x5b, 0x10000, 0x1fffff, 0x5d, 0x5b, 0x200000, 0x3ffffff, 0x5d,
     95 	  0x5b, 0x4000000, 0x7fffffff, 0x5d },
     96 	{ 1, 1, 1, 1, 1, 2, 2, 1, 1, 3, 3, 1, 1, 4, 4, 1, 1, 5, 5, 1, 1, 6, 6, 1 },
     97 	24
     98 }, {
     99 	"ja_JP.ISO2022-JP2",
    100 	"\033$BF|K\1348l\033(BA\033$B$\"\033(BB\033$B$$\033(B",
    101 #ifdef __STDC_ISO_10646__
    102 	{ 0x65E5, 0x672C, 0x8A9E, 0x41, 0x3042, 0x42, 0x3044 },
    103 #else
    104 	{ 0x4200467c, 0x42004b5c, 0x4200386c, 0x41, 0x42002422, 0x42, 0x42002424 },
    105 #endif
    106 	{ 5, 2, 2, 4, 5, 4, 5 },
    107 	7
    108 }, {
    109 	"ja_JP.SJIS",
    110 	"\223\372\226{\214\352A\202\240B\202\242",
    111 #ifdef __STDC_ISO_10646__
    112 	{ 0x65E5, 0x672C, 0x8A9E, 0x41, 0x3042, 0x42, 0x3044 },
    113 #else
    114 	{ 0x93FA, 0x967B, 0x8CEA, 0x41, 0x82A0, 0x42, 0x82A2 },
    115 #endif
    116 	{ 2, 2, 2, 1, 2, 1, 2 },
    117 	7
    118 }, {
    119 	"ja_JP.eucJP",
    120 	"\306\374\313\334\270\354A\244\242B\244\244",
    121 #ifdef __STDC_ISO_10646__
    122 	{ 0x65E5, 0x672C, 0x8A9E, 0x41, 0x3042, 0x42, 0x3044 },
    123 #else
    124 	{ 0xC6FC, 0xCBDC, 0xB8EC, 0x41, 0xA4A2, 0x42, 0xA4A4 },
    125 #endif
    126 	{ 2, 2, 2, 1, 2, 1, 2 },
    127 	7
    128 }, {
    129 	NULL,
    130 	NULL,
    131 	{ },
    132 	{ },
    133 	0
    134 }
    135 };
    136 
    137 static void
    138 h_ctype2(const struct test *t, bool use_mbstate)
    139 {
    140 	mbstate_t *stp;
    141 	mbstate_t st;
    142 	char buf[SIZE];
    143 	char *str;
    144 	size_t n;
    145 
    146 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
    147 	ATF_REQUIRE(setlocale(LC_CTYPE, t->locale) != NULL);
    148 
    149 	(void)strvis(buf, t->data, VIS_WHITE | VIS_OCTAL);
    150 	(void)printf("Checking string: \"%s\"\n", buf);
    151 
    152 	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
    153 	(void)printf("Using locale: %s\n", str);
    154 
    155 	(void)printf("Using mbstate: %s\n", use_mbstate ? "yes" : "no");
    156 
    157 	(void)memset(&st, 0, sizeof(st));
    158 //	mbrtowc(0, 0, 0, &st); /* XXX for ISO2022-JP */
    159 	stp = use_mbstate ? &st : 0;
    160 
    161 	printf("First using repeated mbrtowc\n");
    162 
    163 	for (n = 9; n > 0; n--) {
    164 		const char *src = t->data;
    165 		wchar_t dst;
    166 		size_t nchar = 0;
    167 		int width = 0;
    168 
    169 		ATF_REQUIRE(mbsinit(stp) != 0);
    170 
    171 		for (;;) {
    172 			size_t rv = mbrtowc(&dst, src, n, stp);
    173 
    174 			if (rv == 0)
    175 				break;
    176 
    177 			if (rv == (size_t)-2) {
    178 				src += n;
    179 				width += n;
    180 
    181 				continue;
    182 			}
    183 			if (rv == (size_t)-1) {
    184 				ATF_REQUIRE_EQ(errno, EILSEQ);
    185 				atf_tc_fail("Invalid sequence");
    186 				/* NOTREACHED */
    187 			}
    188 
    189 			width += rv;
    190 			src += rv;
    191 
    192 			if (dst != t->wchars[nchar] ||
    193 			    width != t->widths[nchar]) {
    194 				(void)printf("At position %zd:\n", nchar);
    195 				(void)printf("  expected: 0x%04X (%u)\n",
    196 					t->wchars[nchar], t->widths[nchar]);
    197 				(void)printf("  got     : 0x%04X (%u)\n",
    198 					dst, width);
    199 				atf_tc_fail("Test failed");
    200 			}
    201 
    202 			nchar++;
    203 			width = 0;
    204 		}
    205 
    206 		ATF_REQUIRE_EQ_MSG(dst, 0, "Incorrect terminating character: "
    207 			"0x%04X (expected: 0x00)", dst);
    208 
    209 		ATF_REQUIRE_EQ_MSG(nchar, t->length, "Incorrect length: "
    210 			"%zd (expected: %zd)", nchar, t->length);
    211 	}
    212 
    213 	printf("Now using mbsrtowcs\n");
    214 
    215 	{
    216 		wchar_t wbuf[SIZE];
    217 		size_t rv;
    218 		char const *src = t->data;
    219 		int i;
    220 
    221 		(void)memset(wbuf, 0xFF, sizeof(wbuf));
    222 
    223 		rv = mbsrtowcs(wbuf, &src, SIZE, stp);
    224 
    225 		ATF_REQUIRE_EQ_MSG(rv, t->length, "Incorrect length: %zd "
    226 			"(expected: %zd)", rv, t->length);
    227 		ATF_REQUIRE_EQ(src, NULL);
    228 
    229 		for (i = 0; wbuf[i] != 0; ++i) {
    230 			if (wbuf[i] == t->wchars[i])
    231 				continue;
    232 
    233 			(void)printf("At position %d:\n", i);
    234 			(void)printf("  expected: 0x%04X\n", t->wchars[i]);
    235 			(void)printf("  got     : 0x%04X\n", wbuf[i]);
    236 			atf_tc_fail("Test failed");
    237 		}
    238 
    239 		ATF_REQUIRE_EQ_MSG((size_t)i, t->length, "Incorrect length: "
    240 			"%d (expected: %zd)", i, t->length);
    241 	}
    242 
    243 	(void)printf("Ok.\n");
    244 }
    245 
    246 ATF_TC(mbrtowc_internal);
    247 ATF_TC_HEAD(mbrtowc_internal, tc)
    248 {
    249 	atf_tc_set_md_var(tc, "descr",
    250 		"Checks mbrtowc(3) and mbsrtowcs(3) (using internal "
    251 		"state) with different locales");
    252 }
    253 ATF_TC_BODY(mbrtowc_internal, tc)
    254 {
    255 	struct test *t;
    256 
    257 	for (t = &tests[0]; t->data != NULL; ++t)
    258 		h_ctype2(t, false);
    259 }
    260 
    261 ATF_TC(mbrtowc_object);
    262 ATF_TC_HEAD(mbrtowc_object, tc)
    263 {
    264 	atf_tc_set_md_var(tc, "descr",
    265 		"Checks mbrtowc(3) and mbsrtowcs(3) (using state "
    266 		"object) with different locales");
    267 }
    268 ATF_TC_BODY(mbrtowc_object, tc)
    269 {
    270 	struct test *t;
    271 
    272 	for (t = &tests[0]; t->data != NULL; ++t)
    273 		h_ctype2(t, true);
    274 }
    275 
    276 ATF_TP_ADD_TCS(tp)
    277 {
    278 
    279 	ATF_TP_ADD_TC(tp, mbrtowc_internal);
    280 	ATF_TP_ADD_TC(tp, mbrtowc_object);
    281 
    282 	return atf_no_error();
    283 }
    284