xref: /freebsd/lib/libc/tests/locale/mbsnrtowcs_test.c (revision 1db64f89363c97858961c4df0b7d02f3223723cf)
1 /*-
2  * Copyright (c) 2002-2004 Tim J. Robbins
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * Test program for mbsnrtowcs().
29  *
30  * The function is tested with both the "C" ("POSIX") LC_CTYPE setting and
31  * "ja_JP.eucJP". Other encodings are not tested.
32  */
33 
34 #include <errno.h>
35 #include <limits.h>
36 #include <locale.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <wchar.h>
41 
42 #include <atf-c.h>
43 
44 ATF_TC_WITHOUT_HEAD(mbsnrtowcs_test);
45 ATF_TC_BODY(mbsnrtowcs_test, tc)
46 {
47 	char srcbuf[128];
48 	wchar_t dstbuf[128];
49 	char *src;
50 	mbstate_t s;
51 
52 	/* C/POSIX locale. */
53 
54 	/* Simple null terminated string. */
55 	memset(srcbuf, 0xcc, sizeof(srcbuf));
56 	strcpy(srcbuf, "hello");
57 	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
58 	src = srcbuf;
59 	memset(&s, 0, sizeof(s));
60 	ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 6, sizeof(dstbuf) /
61 	    sizeof(*dstbuf), &s) == 5);
62 	ATF_REQUIRE(wcscmp(dstbuf, L"hello") == 0);
63 	ATF_REQUIRE(dstbuf[6] == 0xcccc);
64 	ATF_REQUIRE(src == NULL);
65 
66 	/* Simple null terminated string, stopping early. */
67 	memset(srcbuf, 0xcc, sizeof(srcbuf));
68 	strcpy(srcbuf, "hello");
69 	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
70 	src = srcbuf;
71 	memset(&s, 0, sizeof(s));
72 	ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 4, sizeof(dstbuf) /
73 	    sizeof(*dstbuf), &s) == 4);
74 	ATF_REQUIRE(wmemcmp(dstbuf, L"hell", 4) == 0);
75 	ATF_REQUIRE(dstbuf[5] == 0xcccc);
76 	ATF_REQUIRE(src == srcbuf + 4);
77 
78 	/* Not enough space in destination buffer. */
79 	memset(srcbuf, 0xcc, sizeof(srcbuf));
80 	strcpy(srcbuf, "hello");
81 	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
82 	src = srcbuf;
83 	memset(&s, 0, sizeof(s));
84 	ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 6, 4, &s) == 4);
85 	ATF_REQUIRE(wmemcmp(dstbuf, L"hell", 4) == 0);
86 	ATF_REQUIRE(dstbuf[5] == 0xcccc);
87 	ATF_REQUIRE(src == srcbuf + 4);
88 
89 	/* Null terminated string, internal dest. buffer */
90 	memset(srcbuf, 0xcc, sizeof(srcbuf));
91 	strcpy(srcbuf, "hello");
92 	src = srcbuf;
93 	memset(&s, 0, sizeof(s));
94 	ATF_REQUIRE(mbsnrtowcs(NULL, (const char **)&src, 6, 0, &s) == 5);
95 
96 	/* Null terminated string, internal dest. buffer, stopping early */
97 	memset(srcbuf, 0xcc, sizeof(srcbuf));
98 	strcpy(srcbuf, "hello");
99 	src = srcbuf;
100 	memset(&s, 0, sizeof(s));
101 	ATF_REQUIRE(mbsnrtowcs(NULL, (const char **)&src, 4, 0, &s) == 4);
102 
103 	/* Null terminated string, internal state. */
104 	memset(srcbuf, 0xcc, sizeof(srcbuf));
105 	strcpy(srcbuf, "hello");
106 	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
107 	src = srcbuf;
108 	ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 6, sizeof(dstbuf) /
109 	    sizeof(*dstbuf), NULL) == 5);
110 	ATF_REQUIRE(wcscmp(dstbuf, L"hello") == 0);
111 	ATF_REQUIRE(dstbuf[6] == 0xcccc);
112 	ATF_REQUIRE(src == NULL);
113 
114 	/* Null terminated string, internal state, internal dest. buffer. */
115 	memset(srcbuf, 0xcc, sizeof(srcbuf));
116 	strcpy(srcbuf, "hello");
117 	src = srcbuf;
118 	ATF_REQUIRE(mbsnrtowcs(NULL, (const char **)&src, 6, 0, NULL) == 5);
119 
120 	/* Empty source buffer. */
121 	memset(srcbuf, 0xcc, sizeof(srcbuf));
122 	srcbuf[0] = '\0';
123 	src = srcbuf;
124 	memset(&s, 0, sizeof(s));
125 	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
126 	ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 1, 1, &s) == 0);
127 	ATF_REQUIRE(dstbuf[0] == 0);
128 	ATF_REQUIRE(dstbuf[1] == 0xcccc);
129 	ATF_REQUIRE(src == NULL);
130 
131 	/* Zero length destination buffer. */
132 	memset(srcbuf, 0xcc, sizeof(srcbuf));
133 	strcpy(srcbuf, "hello");
134 	src = srcbuf;
135 	memset(&s, 0, sizeof(s));
136 	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
137 	ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 1, 0, &s) == 0);
138 	ATF_REQUIRE(dstbuf[0] == 0xcccc);
139 	ATF_REQUIRE(src == srcbuf);
140 
141 	/* Zero length source buffer. */
142 	memset(srcbuf, 0xcc, sizeof(srcbuf));
143 	src = srcbuf;
144 	memset(&s, 0, sizeof(s));
145 	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
146 	ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 0, 1, &s) == 0);
147 	ATF_REQUIRE(dstbuf[0] == 0xcccc);
148 	ATF_REQUIRE(src == srcbuf);
149 
150 	/*
151 	 * Japanese (EUC) locale.
152 	 */
153 
154 	ATF_REQUIRE(strcmp(setlocale(LC_CTYPE, "ja_JP.eucJP"), "ja_JP.eucJP") == 0);
155 	ATF_REQUIRE(MB_CUR_MAX > 1);
156 
157 	memset(srcbuf, 0xcc, sizeof(srcbuf));
158 	strcpy(srcbuf, "\xA3\xC1 B \xA3\xC3");
159 	src = srcbuf;
160 	memset(&s, 0, sizeof(s));
161 	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
162 	ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 8, sizeof(dstbuf) /
163 	    sizeof(*dstbuf), &s) == 5);
164 	ATF_REQUIRE(dstbuf[0] == 0xA3C1 && dstbuf[1] == 0x20 && dstbuf[2] == 0x42 &&
165 	    dstbuf[3] == 0x20 && dstbuf[4] == 0xA3C3 && dstbuf[5] == 0);
166 	ATF_REQUIRE(src == NULL);
167 
168 	/* Partial character. */
169 	memset(srcbuf, 0xcc, sizeof(srcbuf));
170 	strcpy(srcbuf, "\xA3\xC1 B \xA3\xC3");
171 	src = srcbuf;
172 	memset(&s, 0, sizeof(s));
173 	wmemset(dstbuf, 0xcccc, sizeof(dstbuf) / sizeof(*dstbuf));
174 	ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 6, sizeof(dstbuf) /
175 	    sizeof(*dstbuf), &s) == 4);
176 	ATF_REQUIRE(src == srcbuf + 6);
177 	ATF_REQUIRE(!mbsinit(&s));
178 	ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 1, sizeof(dstbuf) /
179 	    sizeof(*dstbuf), &s) == 1);
180 	ATF_REQUIRE(src == srcbuf + 7);
181 	ATF_REQUIRE(mbsnrtowcs(dstbuf, (const char **)&src, 1, sizeof(dstbuf) /
182 	    sizeof(*dstbuf), &s) == 0);
183 	ATF_REQUIRE(src == NULL);
184 }
185 
186 ATF_TP_ADD_TCS(tp)
187 {
188 
189 	ATF_TP_ADD_TC(tp, mbsnrtowcs_test);
190 
191 	return (atf_no_error());
192 }
193