110710Sddmitriev/* $NetBSD: t_mbtowc.c,v 1.1 2011/04/09 17:45:25 pgoyette Exp $ */ 210710Sddmitriev 310710Sddmitriev/*- 410710Sddmitriev * Copyright (c) 2011 The NetBSD Foundation, Inc. 510710Sddmitriev * All rights reserved. 610710Sddmitriev * 710710Sddmitriev * Redistribution and use in source and binary forms, with or without 810710Sddmitriev * modification, are permitted provided that the following conditions 910710Sddmitriev * are met: 1010710Sddmitriev * 1. Redistributions of source code must retain the above copyright 1110710Sddmitriev * notice, this list of conditions and the following disclaimer. 1210710Sddmitriev * 2. Redistributions in binary form must reproduce the above copyright 1310710Sddmitriev * notice, this list of conditions and the following disclaimer in the 1410710Sddmitriev * documentation and/or other materials provided with the distribution. 1510710Sddmitriev * 1610710Sddmitriev * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 1710710Sddmitriev * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1810710Sddmitriev * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1910710Sddmitriev * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2010710Sddmitriev * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2110710Sddmitriev * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2210710Sddmitriev * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2310710Sddmitriev * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2410710Sddmitriev * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2510710Sddmitriev * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2610710Sddmitriev * POSSIBILITY OF SUCH DAMAGE. 2710710Sddmitriev */ 2810710Sddmitriev 2910710Sddmitriev/*- 3010710Sddmitriev * Copyright (c)2007 Citrus Project, 3110710Sddmitriev * All rights reserved. 3210710Sddmitriev * 3310710Sddmitriev * Redistribution and use in source and binary forms, with or without 3410710Sddmitriev * modification, are permitted provided that the following conditions 3510710Sddmitriev * are met: 3610710Sddmitriev * 1. Redistributions of source code must retain the above copyright 3710710Sddmitriev * notice, this list of conditions and the following disclaimer. 3810710Sddmitriev * 2. Redistributions in binary form must reproduce the above copyright 3910710Sddmitriev * notice, this list of conditions and the following disclaimer in the 4010710Sddmitriev * documentation and/or other materials provided with the distribution. 4110710Sddmitriev * 4210710Sddmitriev * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 4310710Sddmitriev * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4410710Sddmitriev * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4510710Sddmitriev * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4610710Sddmitriev * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4710710Sddmitriev * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4810710Sddmitriev * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4910710Sddmitriev * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5010710Sddmitriev * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5110710Sddmitriev * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5210710Sddmitriev * SUCH DAMAGE. 5310710Sddmitriev * 5410710Sddmitriev */ 5510710Sddmitriev 5610710Sddmitriev#include <sys/cdefs.h> 5710710Sddmitriev__COPYRIGHT("@(#) Copyright (c) 2011\ 5810710Sddmitriev The NetBSD Foundation, inc. All rights reserved."); 5910710Sddmitriev__RCSID("$NetBSD: t_mbtowc.c,v 1.1 2011/04/09 17:45:25 pgoyette Exp $"); 6010710Sddmitriev 6110710Sddmitriev#include <errno.h> 6210710Sddmitriev#include <locale.h> 6310710Sddmitriev#include <stdio.h> 6410710Sddmitriev#include <stdlib.h> 6510710Sddmitriev#include <string.h> 6610710Sddmitriev#include <vis.h> 6710710Sddmitriev#include <wchar.h> 6810710Sddmitriev 6910710Sddmitriev#include <atf-c.h> 7010710Sddmitriev 7110710Sddmitrievstatic void 7210710Sddmitrievh_mbtowc(const char *locale, const char *illegal, const char *legal) 7310710Sddmitriev{ 7410710Sddmitriev char buf[64]; 7510710Sddmitriev size_t stateful, ret; 7610710Sddmitriev char *str; 7710710Sddmitriev 7810710Sddmitriev ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C"); 7910710Sddmitriev#ifdef __NetBSD__ 8010710Sddmitriev ATF_REQUIRE(setlocale(LC_CTYPE, locale) != NULL); 8110710Sddmitriev#else 8210710Sddmitriev if (setlocale(LC_CTYPE, locale) == NULL) { 8310710Sddmitriev fprintf(stderr, "Locale %s not found.\n", locale); 8410710Sddmitriev return; 8510710Sddmitriev } 8610710Sddmitriev#endif 8710710Sddmitriev 8810710Sddmitriev ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL); 8910710Sddmitriev (void)printf("Using locale: %s\n", str); 9010710Sddmitriev 9110710Sddmitriev stateful = wctomb(NULL, L'\0'); 9210710Sddmitriev (void)printf("Locale is state-%sdependent\n", 9310710Sddmitriev stateful ? "in" : ""); 9410710Sddmitriev 9510710Sddmitriev /* initialize internal state */ 9610710Sddmitriev ret = mbtowc(NULL, NULL, 0); 9710710Sddmitriev ATF_REQUIRE(stateful ? ret : !ret); 9810710Sddmitriev 9910710Sddmitriev (void)strvis(buf, illegal, VIS_WHITE | VIS_OCTAL); 10010710Sddmitriev (void)printf("Checking illegal sequence: \"%s\"\n", buf); 10110710Sddmitriev 10210710Sddmitriev ret = mbtowc(NULL, illegal, strlen(illegal)); 10310710Sddmitriev (void)printf("mbtowc() returned: %zd\n", ret); 10410710Sddmitriev ATF_REQUIRE_EQ(ret, (size_t)-1); 10510710Sddmitriev (void)printf("errno: %s\n", strerror(errno)); 10610710Sddmitriev ATF_REQUIRE_EQ(errno, EILSEQ); 10710710Sddmitriev 10810710Sddmitriev /* if this is stateless encoding, this re-initialization is not required. */ 10910710Sddmitriev if (stateful) { 11010710Sddmitriev /* re-initialize internal state */ 11110710Sddmitriev ret = mbtowc(NULL, NULL, 0); 11210710Sddmitriev ATF_REQUIRE(stateful ? ret : !ret); 11310710Sddmitriev } 11410710Sddmitriev 11510710Sddmitriev /* valid multibyte sequence case */ 11610710Sddmitriev (void)strvis(buf, legal, VIS_WHITE | VIS_OCTAL); 11710710Sddmitriev (void)printf("Checking legal sequence: \"%s\"\n", buf); 11810710Sddmitriev 11910710Sddmitriev errno = 0; 12010710Sddmitriev ret = mbtowc(NULL, legal, strlen(legal)); 12110710Sddmitriev (void)printf("mbtowc() returned: %zd\n", ret); 12210710Sddmitriev ATF_REQUIRE(ret != (size_t)-1); 12310710Sddmitriev (void)printf("errno: %s\n", strerror(errno)); 12410710Sddmitriev ATF_REQUIRE_EQ(errno, 0); 12510710Sddmitriev 12610710Sddmitriev (void)printf("Ok.\n"); 12710710Sddmitriev} 12810710Sddmitriev 12910710SddmitrievATF_TC(mbtowc); 13010710SddmitrievATF_TC_HEAD(mbtowc, tc) 13110710Sddmitriev{ 13210710Sddmitriev atf_tc_set_md_var(tc, "descr", "Checks mbtowc(3)"); 13310710Sddmitriev} 13410710SddmitrievATF_TC_BODY(mbtowc, tc) 13510710Sddmitriev{ 13610710Sddmitriev h_mbtowc("en_US.UTF-8", "\240", "\302\240"); 13710710Sddmitriev h_mbtowc("ja_JP.ISO2022-JP", "\033$B", "\033$B$\"\033(B"); 13810710Sddmitriev h_mbtowc("ja_JP.SJIS", "\202", "\202\240"); 13910710Sddmitriev h_mbtowc("ja_JP.eucJP", "\244", "\244\242"); 14010710Sddmitriev h_mbtowc("zh_CN.GB18030", "\241", "\241\241"); 14110710Sddmitriev h_mbtowc("zh_TW.Big5", "\241", "\241@"); 14210710Sddmitriev h_mbtowc("zh_TW.eucTW", "\241", "\241\241"); 14310710Sddmitriev} 14410710Sddmitriev 14510710SddmitrievATF_TP_ADD_TCS(tp) 14610710Sddmitriev{ 14710710Sddmitriev ATF_TP_ADD_TC(tp, mbtowc); 14810710Sddmitriev 14910710Sddmitriev return atf_no_error(); 15010710Sddmitriev} 15110710Sddmitriev