157718be8SEnji Cooper /* $NetBSD: t_mbtowc.c,v 1.1 2011/04/09 17:45:25 pgoyette Exp $ */
257718be8SEnji Cooper 
357718be8SEnji Cooper /*-
457718be8SEnji Cooper  * Copyright (c) 2011 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
857718be8SEnji Cooper  * modification, are permitted provided that the following conditions
957718be8SEnji Cooper  * are met:
1057718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1157718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1257718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1357718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1457718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1557718be8SEnji Cooper  *
1657718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1757718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1857718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1957718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2057718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2157718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2257718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2357718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2457718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2557718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2657718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
2757718be8SEnji Cooper  */
2857718be8SEnji Cooper 
2957718be8SEnji Cooper /*-
3057718be8SEnji Cooper  * Copyright (c)2007 Citrus Project,
3157718be8SEnji Cooper  * All rights reserved.
3257718be8SEnji Cooper  *
3357718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
3457718be8SEnji Cooper  * modification, are permitted provided that the following conditions
3557718be8SEnji Cooper  * are met:
3657718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
3757718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
3857718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
3957718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
4057718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
4157718be8SEnji Cooper  *
4257718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4357718be8SEnji Cooper  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4457718be8SEnji Cooper  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4557718be8SEnji Cooper  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4657718be8SEnji Cooper  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4757718be8SEnji Cooper  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4857718be8SEnji Cooper  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4957718be8SEnji Cooper  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5057718be8SEnji Cooper  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5157718be8SEnji Cooper  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5257718be8SEnji Cooper  * SUCH DAMAGE.
5357718be8SEnji Cooper  *
5457718be8SEnji Cooper  */
5557718be8SEnji Cooper 
5657718be8SEnji Cooper #include <sys/cdefs.h>
5757718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2011\
5857718be8SEnji Cooper  The NetBSD Foundation, inc. All rights reserved.");
5957718be8SEnji Cooper __RCSID("$NetBSD: t_mbtowc.c,v 1.1 2011/04/09 17:45:25 pgoyette Exp $");
6057718be8SEnji Cooper 
6157718be8SEnji Cooper #include <errno.h>
6257718be8SEnji Cooper #include <locale.h>
6357718be8SEnji Cooper #include <stdio.h>
6457718be8SEnji Cooper #include <stdlib.h>
6557718be8SEnji Cooper #include <string.h>
6657718be8SEnji Cooper #include <vis.h>
6757718be8SEnji Cooper #include <wchar.h>
6857718be8SEnji Cooper 
6957718be8SEnji Cooper #include <atf-c.h>
7057718be8SEnji Cooper 
7157718be8SEnji Cooper static void
h_mbtowc(const char * locale,const char * illegal,const char * legal)7257718be8SEnji Cooper h_mbtowc(const char *locale, const char *illegal, const char *legal)
7357718be8SEnji Cooper {
7457718be8SEnji Cooper 	char buf[64];
7557718be8SEnji Cooper 	size_t stateful, ret;
7657718be8SEnji Cooper 	char *str;
7757718be8SEnji Cooper 
7857718be8SEnji Cooper 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
79ff0ba872SEnji Cooper #ifdef __NetBSD__
8057718be8SEnji Cooper 	ATF_REQUIRE(setlocale(LC_CTYPE, locale) != NULL);
81effc3698SEnji Cooper #else
82effc3698SEnji Cooper 	if (setlocale(LC_CTYPE, locale) == NULL) {
83effc3698SEnji Cooper 		fprintf(stderr, "Locale %s not found.\n", locale);
84effc3698SEnji Cooper 		return;
85effc3698SEnji Cooper 	}
86effc3698SEnji Cooper #endif
8757718be8SEnji Cooper 
8857718be8SEnji Cooper 	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
8957718be8SEnji Cooper 	(void)printf("Using locale: %s\n", str);
9057718be8SEnji Cooper 
9157718be8SEnji Cooper 	stateful = wctomb(NULL, L'\0');
9257718be8SEnji Cooper 	(void)printf("Locale is state-%sdependent\n",
9357718be8SEnji Cooper 		stateful ? "in" : "");
9457718be8SEnji Cooper 
9557718be8SEnji Cooper 	/* initialize internal state */
9657718be8SEnji Cooper 	ret = mbtowc(NULL, NULL, 0);
9757718be8SEnji Cooper 	ATF_REQUIRE(stateful ? ret : !ret);
9857718be8SEnji Cooper 
9957718be8SEnji Cooper 	(void)strvis(buf, illegal, VIS_WHITE | VIS_OCTAL);
10057718be8SEnji Cooper 	(void)printf("Checking illegal sequence: \"%s\"\n", buf);
10157718be8SEnji Cooper 
10257718be8SEnji Cooper 	ret = mbtowc(NULL, illegal, strlen(illegal));
10357718be8SEnji Cooper 	(void)printf("mbtowc() returned: %zd\n", ret);
10457718be8SEnji Cooper 	ATF_REQUIRE_EQ(ret, (size_t)-1);
10557718be8SEnji Cooper 	(void)printf("errno: %s\n", strerror(errno));
10657718be8SEnji Cooper 	ATF_REQUIRE_EQ(errno, EILSEQ);
10757718be8SEnji Cooper 
10857718be8SEnji Cooper 	/* if this is stateless encoding, this re-initialization is not required. */
10957718be8SEnji Cooper 	if (stateful) {
11057718be8SEnji Cooper 		/* re-initialize internal state */
11157718be8SEnji Cooper 		ret = mbtowc(NULL, NULL, 0);
11257718be8SEnji Cooper 		ATF_REQUIRE(stateful ? ret : !ret);
11357718be8SEnji Cooper 	}
11457718be8SEnji Cooper 
11557718be8SEnji Cooper 	/* valid multibyte sequence case */
11657718be8SEnji Cooper 	(void)strvis(buf, legal, VIS_WHITE | VIS_OCTAL);
11757718be8SEnji Cooper 	(void)printf("Checking legal sequence: \"%s\"\n", buf);
11857718be8SEnji Cooper 
11957718be8SEnji Cooper 	errno = 0;
12057718be8SEnji Cooper 	ret = mbtowc(NULL, legal, strlen(legal));
12157718be8SEnji Cooper 	(void)printf("mbtowc() returned: %zd\n", ret);
12257718be8SEnji Cooper 	ATF_REQUIRE(ret != (size_t)-1);
12357718be8SEnji Cooper 	(void)printf("errno: %s\n", strerror(errno));
12457718be8SEnji Cooper 	ATF_REQUIRE_EQ(errno, 0);
12557718be8SEnji Cooper 
12657718be8SEnji Cooper 	(void)printf("Ok.\n");
12757718be8SEnji Cooper }
12857718be8SEnji Cooper 
12957718be8SEnji Cooper ATF_TC(mbtowc);
ATF_TC_HEAD(mbtowc,tc)13057718be8SEnji Cooper ATF_TC_HEAD(mbtowc, tc)
13157718be8SEnji Cooper {
13257718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Checks mbtowc(3)");
13357718be8SEnji Cooper }
ATF_TC_BODY(mbtowc,tc)13457718be8SEnji Cooper ATF_TC_BODY(mbtowc, tc)
13557718be8SEnji Cooper {
13657718be8SEnji Cooper 	h_mbtowc("en_US.UTF-8", "\240", "\302\240");
13757718be8SEnji Cooper 	h_mbtowc("ja_JP.ISO2022-JP", "\033$B", "\033$B$\"\033(B");
13857718be8SEnji Cooper 	h_mbtowc("ja_JP.SJIS", "\202", "\202\240");
13957718be8SEnji Cooper 	h_mbtowc("ja_JP.eucJP", "\244", "\244\242");
14057718be8SEnji Cooper 	h_mbtowc("zh_CN.GB18030", "\241", "\241\241");
14157718be8SEnji Cooper 	h_mbtowc("zh_TW.Big5", "\241", "\241@");
14257718be8SEnji Cooper 	h_mbtowc("zh_TW.eucTW", "\241", "\241\241");
14357718be8SEnji Cooper }
14457718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)14557718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
14657718be8SEnji Cooper {
14757718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mbtowc);
14857718be8SEnji Cooper 
14957718be8SEnji Cooper 	return atf_no_error();
15057718be8SEnji Cooper }
151