xref: /freebsd/contrib/netbsd-tests/lib/libc/locale/t_mbtowc.c (revision 38d120bc13ac1de5b739b67b87016b9122149374)
1 /* $NetBSD: t_mbtowc.c,v 1.1 2011/04/09 17:45:25 pgoyette 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)2007 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 
56 #include <sys/cdefs.h>
57 __COPYRIGHT("@(#) Copyright (c) 2011\
58  The NetBSD Foundation, inc. All rights reserved.");
59 __RCSID("$NetBSD: t_mbtowc.c,v 1.1 2011/04/09 17:45:25 pgoyette Exp $");
60 
61 #include <errno.h>
62 #include <locale.h>
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66 #include <vis.h>
67 #include <wchar.h>
68 
69 #include <atf-c.h>
70 
71 static void
72 h_mbtowc(const char *locale, const char *illegal, const char *legal)
73 {
74 	char buf[64];
75 	size_t stateful, ret;
76 	char *str;
77 
78 	ATF_REQUIRE_STREQ(setlocale(LC_ALL, "C"), "C");
79 #ifdef __NetBSD__
80 	ATF_REQUIRE(setlocale(LC_CTYPE, locale) != NULL);
81 #else
82 	if (setlocale(LC_CTYPE, locale) == NULL) {
83 		fprintf(stderr, "Locale %s not found.\n", locale);
84 		return;
85 	}
86 #endif
87 
88 	ATF_REQUIRE((str = setlocale(LC_ALL, NULL)) != NULL);
89 	(void)printf("Using locale: %s\n", str);
90 
91 	stateful = wctomb(NULL, L'\0');
92 	(void)printf("Locale is state-%sdependent\n",
93 		stateful ? "in" : "");
94 
95 	/* initialize internal state */
96 	ret = mbtowc(NULL, NULL, 0);
97 	ATF_REQUIRE(stateful ? ret : !ret);
98 
99 	(void)strvis(buf, illegal, VIS_WHITE | VIS_OCTAL);
100 	(void)printf("Checking illegal sequence: \"%s\"\n", buf);
101 
102 	ret = mbtowc(NULL, illegal, strlen(illegal));
103 	(void)printf("mbtowc() returned: %zd\n", ret);
104 	ATF_REQUIRE_EQ(ret, (size_t)-1);
105 	(void)printf("errno: %s\n", strerror(errno));
106 	ATF_REQUIRE_EQ(errno, EILSEQ);
107 
108 	/* if this is stateless encoding, this re-initialization is not required. */
109 	if (stateful) {
110 		/* re-initialize internal state */
111 		ret = mbtowc(NULL, NULL, 0);
112 		ATF_REQUIRE(stateful ? ret : !ret);
113 	}
114 
115 	/* valid multibyte sequence case */
116 	(void)strvis(buf, legal, VIS_WHITE | VIS_OCTAL);
117 	(void)printf("Checking legal sequence: \"%s\"\n", buf);
118 
119 	errno = 0;
120 	ret = mbtowc(NULL, legal, strlen(legal));
121 	(void)printf("mbtowc() returned: %zd\n", ret);
122 	ATF_REQUIRE(ret != (size_t)-1);
123 	(void)printf("errno: %s\n", strerror(errno));
124 	ATF_REQUIRE_EQ(errno, 0);
125 
126 	(void)printf("Ok.\n");
127 }
128 
129 ATF_TC(mbtowc);
130 ATF_TC_HEAD(mbtowc, tc)
131 {
132 	atf_tc_set_md_var(tc, "descr", "Checks mbtowc(3)");
133 }
134 ATF_TC_BODY(mbtowc, tc)
135 {
136 	h_mbtowc("en_US.UTF-8", "\240", "\302\240");
137 	h_mbtowc("ja_JP.ISO2022-JP", "\033$B", "\033$B$\"\033(B");
138 	h_mbtowc("ja_JP.SJIS", "\202", "\202\240");
139 	h_mbtowc("ja_JP.eucJP", "\244", "\244\242");
140 #ifndef __FreeBSD__
141 	/* Moved last as it fails */
142 	h_mbtowc("zh_CN.GB18030", "\241", "\241\241");
143 #endif
144 	h_mbtowc("zh_TW.Big5", "\241", "\241@");
145 	h_mbtowc("zh_TW.eucTW", "\241", "\241\241");
146 #ifdef __FreeBSD__
147 	atf_tc_expect_fail("zh_CN.GB18030");
148 	h_mbtowc("zh_CN.GB18030", "\241", "\241\241");
149 #endif
150 }
151 
152 ATF_TP_ADD_TCS(tp)
153 {
154 	ATF_TP_ADD_TC(tp, mbtowc);
155 
156 	return atf_no_error();
157 }
158