Home | History | Annotate | Line # | Download | only in locale
t_mbtowc.c revision 1.3
      1  1.3    jruoho /* $NetBSD: t_mbtowc.c,v 1.3 2020/06/30 16:09:40 jruoho Exp $ */
      2  1.1  pgoyette 
      3  1.1  pgoyette /*-
      4  1.1  pgoyette  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  1.1  pgoyette  * All rights reserved.
      6  1.1  pgoyette  *
      7  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
      8  1.1  pgoyette  * modification, are permitted provided that the following conditions
      9  1.1  pgoyette  * are met:
     10  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     11  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     12  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     15  1.1  pgoyette  *
     16  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  pgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  pgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  pgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  pgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  pgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  pgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  pgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  pgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  pgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  pgoyette  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  pgoyette  */
     28  1.1  pgoyette 
     29  1.1  pgoyette /*-
     30  1.1  pgoyette  * Copyright (c)2007 Citrus Project,
     31  1.1  pgoyette  * All rights reserved.
     32  1.1  pgoyette  *
     33  1.1  pgoyette  * Redistribution and use in source and binary forms, with or without
     34  1.1  pgoyette  * modification, are permitted provided that the following conditions
     35  1.1  pgoyette  * are met:
     36  1.1  pgoyette  * 1. Redistributions of source code must retain the above copyright
     37  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer.
     38  1.1  pgoyette  * 2. Redistributions in binary form must reproduce the above copyright
     39  1.1  pgoyette  *    notice, this list of conditions and the following disclaimer in the
     40  1.1  pgoyette  *    documentation and/or other materials provided with the distribution.
     41  1.1  pgoyette  *
     42  1.1  pgoyette  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     43  1.1  pgoyette  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     44  1.1  pgoyette  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     45  1.1  pgoyette  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     46  1.1  pgoyette  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     47  1.1  pgoyette  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     48  1.1  pgoyette  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     49  1.1  pgoyette  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     50  1.1  pgoyette  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     51  1.1  pgoyette  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     52  1.1  pgoyette  * SUCH DAMAGE.
     53  1.1  pgoyette  *
     54  1.1  pgoyette  */
     55  1.1  pgoyette 
     56  1.3    jruoho /*-
     57  1.3    jruoho  * Copyright (c) 2005 Miloslav Trmac
     58  1.3    jruoho  * All rights reserved.
     59  1.3    jruoho  *
     60  1.3    jruoho  * Redistribution and use in source and binary forms, with or without
     61  1.3    jruoho  * modification, are permitted provided that the following conditions
     62  1.3    jruoho  * are met:
     63  1.3    jruoho 
     64  1.3    jruoho  * 1. Redistributions of source code must retain the above copyright
     65  1.3    jruoho  *    notice, this list of conditions and the following disclaimer.
     66  1.3    jruoho  * 2. Redistributions in binary form must reproduce the above copyright
     67  1.3    jruoho  *    notice, this list of conditions and the following disclaimer in the
     68  1.3    jruoho  *    documentation and/or other materials provided with the distribution.
     69  1.3    jruoho  *
     70  1.3    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     71  1.3    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     72  1.3    jruoho  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     73  1.3    jruoho  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
     74  1.3    jruoho  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     75  1.3    jruoho  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     76  1.3    jruoho  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     77  1.3    jruoho  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     78  1.3    jruoho  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     79  1.3    jruoho  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     80  1.3    jruoho  * POSSIBILITY OF SUCH DAMAGE.
     81  1.3    jruoho  */
     82  1.3    jruoho 
     83  1.1  pgoyette #include <sys/cdefs.h>
     84  1.1  pgoyette __COPYRIGHT("@(#) Copyright (c) 2011\
     85  1.1  pgoyette  The NetBSD Foundation, inc. All rights reserved.");
     86  1.3    jruoho __RCSID("$NetBSD: t_mbtowc.c,v 1.3 2020/06/30 16:09:40 jruoho Exp $");
     87  1.1  pgoyette 
     88  1.1  pgoyette #include <errno.h>
     89  1.3    jruoho #include <langinfo.h>
     90  1.1  pgoyette #include <locale.h>
     91  1.3    jruoho #include <limits.h>
     92  1.1  pgoyette #include <stdio.h>
     93  1.1  pgoyette #include <stdlib.h>
     94  1.1  pgoyette #include <string.h>
     95  1.1  pgoyette #include <vis.h>
     96  1.1  pgoyette #include <wchar.h>
     97  1.1  pgoyette 
     98  1.1  pgoyette #include <atf-c.h>
     99  1.1  pgoyette 
    100  1.1  pgoyette static void
    101  1.3    jruoho h_mbtowc(const char *locale, const char *illegal,
    102  1.3    jruoho     const char *legal, size_t stateful)
    103  1.1  pgoyette {
    104  1.1  pgoyette 	char buf[64];
    105  1.2  perseant 	size_t ret;
    106  1.1  pgoyette 	char *str;
    107  1.1  pgoyette 
    108  1.1  pgoyette 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
    109  1.2  perseant 	(void)printf("Trying locale: %s\n", locale);
    110  1.1  pgoyette 	ATF_REQUIRE(setlocale(LC_CTYPE, locale) != NULL);
    111  1.1  pgoyette 
    112  1.1  pgoyette 	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
    113  1.1  pgoyette 	(void)printf("Using locale: %s\n", str);
    114  1.1  pgoyette 	(void)printf("Locale is state-%sdependent\n",
    115  1.2  perseant 		!stateful ? "in" : "");
    116  1.1  pgoyette 
    117  1.1  pgoyette 	/* initialize internal state */
    118  1.1  pgoyette 	ret = mbtowc(NULL, NULL, 0);
    119  1.1  pgoyette 	ATF_REQUIRE(stateful ? ret : !ret);
    120  1.1  pgoyette 
    121  1.1  pgoyette 	(void)strvis(buf, illegal, VIS_WHITE | VIS_OCTAL);
    122  1.1  pgoyette 	(void)printf("Checking illegal sequence: \"%s\"\n", buf);
    123  1.1  pgoyette 
    124  1.1  pgoyette 	ret = mbtowc(NULL, illegal, strlen(illegal));
    125  1.1  pgoyette 	(void)printf("mbtowc() returned: %zd\n", ret);
    126  1.1  pgoyette 	ATF_REQUIRE_EQ(ret, (size_t)-1);
    127  1.1  pgoyette 	(void)printf("errno: %s\n", strerror(errno));
    128  1.1  pgoyette 	ATF_REQUIRE_EQ(errno, EILSEQ);
    129  1.1  pgoyette 
    130  1.3    jruoho 	/*
    131  1.3    jruoho 	 * If this is stateless encoding, this
    132  1.3    jruoho 	 * re-initialization is not required.
    133  1.3    jruoho 	 */
    134  1.1  pgoyette 	if (stateful) {
    135  1.1  pgoyette 		/* re-initialize internal state */
    136  1.2  perseant 		mbtowc(NULL, NULL, 0);
    137  1.1  pgoyette 	}
    138  1.1  pgoyette 
    139  1.1  pgoyette 	/* valid multibyte sequence case */
    140  1.1  pgoyette 	(void)strvis(buf, legal, VIS_WHITE | VIS_OCTAL);
    141  1.1  pgoyette 	(void)printf("Checking legal sequence: \"%s\"\n", buf);
    142  1.1  pgoyette 
    143  1.1  pgoyette 	errno = 0;
    144  1.1  pgoyette 	ret = mbtowc(NULL, legal, strlen(legal));
    145  1.1  pgoyette 	(void)printf("mbtowc() returned: %zd\n", ret);
    146  1.1  pgoyette 	ATF_REQUIRE(ret != (size_t)-1);
    147  1.1  pgoyette 	(void)printf("errno: %s\n", strerror(errno));
    148  1.1  pgoyette 	ATF_REQUIRE_EQ(errno, 0);
    149  1.1  pgoyette 
    150  1.1  pgoyette 	(void)printf("Ok.\n");
    151  1.1  pgoyette }
    152  1.1  pgoyette 
    153  1.3    jruoho ATF_TC(mbtowc_basic);
    154  1.3    jruoho ATF_TC_HEAD(mbtowc_basic, tc)
    155  1.1  pgoyette {
    156  1.3    jruoho 	atf_tc_set_md_var(tc, "descr", "A basic test of mbtowc(3)");
    157  1.1  pgoyette }
    158  1.3    jruoho 
    159  1.3    jruoho ATF_TC_BODY(mbtowc_basic, tc)
    160  1.1  pgoyette {
    161  1.2  perseant 	h_mbtowc("en_US.UTF-8", "\240", "\302\240", 0);
    162  1.2  perseant 	h_mbtowc("ja_JP.ISO2022-JP", "\033$B", "\033$B$\"\033(B", 1);
    163  1.2  perseant 	h_mbtowc("ja_JP.SJIS", "\202", "\202\240", 0);
    164  1.2  perseant 	h_mbtowc("ja_JP.eucJP", "\244", "\244\242", 0);
    165  1.2  perseant 	h_mbtowc("zh_CN.GB18030", "\241", "\241\241", 0);
    166  1.2  perseant 	h_mbtowc("zh_TW.Big5", "\241", "\241@", 0);
    167  1.2  perseant 	h_mbtowc("zh_TW.eucTW", "\241", "\241\241", 0);
    168  1.1  pgoyette }
    169  1.1  pgoyette 
    170  1.3    jruoho ATF_TC(mbtowc_sign);
    171  1.3    jruoho ATF_TC_HEAD(mbtowc_sign, tc)
    172  1.3    jruoho {
    173  1.3    jruoho 	atf_tc_set_md_var(tc, "descr", "Test mbtowc(3) sign conversion");
    174  1.3    jruoho }
    175  1.3    jruoho 
    176  1.3    jruoho ATF_TC_BODY(mbtowc_sign, tc)
    177  1.3    jruoho {
    178  1.3    jruoho 	char back[MB_LEN_MAX];
    179  1.3    jruoho 	wchar_t wc;
    180  1.3    jruoho 	size_t i;
    181  1.3    jruoho 	int ret;
    182  1.3    jruoho 
    183  1.3    jruoho 	(void)setlocale(LC_ALL, "");
    184  1.3    jruoho 	(void)printf("Charset: %s\n", nl_langinfo(CODESET));
    185  1.3    jruoho 	ret = mbtowc(&wc, "\xe4", 1);
    186  1.3    jruoho 	(void)printf("mbtowc(): %d\n", ret);
    187  1.3    jruoho 
    188  1.3    jruoho 	if (ret > 0) {
    189  1.3    jruoho 		(void)printf("Result: 0x%08lX\n",(unsigned long)wc);
    190  1.3    jruoho 		ret = wctomb(back, wc);
    191  1.3    jruoho 		(void)printf("wctomb(): %d\n", ret);
    192  1.3    jruoho 		for(i = 0; ret > 0 && i < (size_t)ret; i++)
    193  1.3    jruoho 			printf("%02X ",(unsigned char)back[i]);
    194  1.3    jruoho 		putchar('\n');
    195  1.3    jruoho 	}
    196  1.3    jruoho 
    197  1.3    jruoho 	ATF_REQUIRE(ret > 0);
    198  1.3    jruoho }
    199  1.3    jruoho 
    200  1.1  pgoyette ATF_TP_ADD_TCS(tp)
    201  1.1  pgoyette {
    202  1.3    jruoho 
    203  1.3    jruoho 	ATF_TP_ADD_TC(tp, mbtowc_basic);
    204  1.3    jruoho 	ATF_TP_ADD_TC(tp, mbtowc_sign);
    205  1.1  pgoyette 
    206  1.1  pgoyette 	return atf_no_error();
    207  1.1  pgoyette }
    208