wchar_test.cpp revision fd0ce866ce46ed62aa17dc2959a085cb3bf5755d
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
20#include <limits.h>
21#include <locale.h>
22#include <stdint.h>
23#include <wchar.h>
24
25TEST(wchar, sizeof_wchar_t) {
26  EXPECT_EQ(4U, sizeof(wchar_t));
27  EXPECT_EQ(4U, sizeof(wint_t));
28}
29
30TEST(wchar, mbrlen) {
31  char bytes[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
32  EXPECT_EQ(0U, mbrlen(&bytes[0], 0, NULL));
33  EXPECT_EQ(1U, mbrlen(&bytes[0], 1, NULL));
34
35  EXPECT_EQ(1U, mbrlen(&bytes[4], 1, NULL));
36  EXPECT_EQ(0U, mbrlen(&bytes[5], 1, NULL));
37}
38
39TEST(wchar, wctomb_wcrtomb) {
40  // wctomb and wcrtomb behave differently when s == NULL.
41  EXPECT_EQ(0, wctomb(NULL, L'h'));
42  EXPECT_EQ(0, wctomb(NULL, L'\0'));
43  EXPECT_EQ(1U, wcrtomb(NULL, L'\0', NULL));
44  EXPECT_EQ(1U, wcrtomb(NULL, L'h', NULL));
45
46  char bytes[MB_LEN_MAX];
47
48  // wctomb and wcrtomb behave similarly for the null wide character.
49  EXPECT_EQ(1, wctomb(bytes, L'\0'));
50  EXPECT_EQ(1U, wcrtomb(bytes, L'\0', NULL));
51
52  // ...and for regular characters.
53  memset(bytes, 0, sizeof(bytes));
54  EXPECT_EQ(1, wctomb(bytes, L'h'));
55  EXPECT_EQ('h', bytes[0]);
56  memset(bytes, 0, sizeof(bytes));
57  EXPECT_EQ(1U, wcrtomb(bytes, L'h', NULL));
58  EXPECT_EQ('h', bytes[0]);
59
60  ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
61  uselocale(LC_GLOBAL_LOCALE);
62
63  // 1-byte UTF-8.
64  memset(bytes, 0, sizeof(bytes));
65  EXPECT_EQ(1U, wcrtomb(bytes, L'h', NULL));
66  EXPECT_EQ('h', bytes[0]);
67  // 2-byte UTF-8.
68  memset(bytes, 0, sizeof(bytes));
69  EXPECT_EQ(2U, wcrtomb(bytes, 0x00a2, NULL));
70  EXPECT_EQ('\xc2', bytes[0]);
71  EXPECT_EQ('\xa2', bytes[1]);
72  // 3-byte UTF-8.
73  memset(bytes, 0, sizeof(bytes));
74  EXPECT_EQ(3U, wcrtomb(bytes, 0x20ac, NULL));
75  EXPECT_EQ('\xe2', bytes[0]);
76  EXPECT_EQ('\x82', bytes[1]);
77  EXPECT_EQ('\xac', bytes[2]);
78  // 4-byte UTF-8.
79  memset(bytes, 0, sizeof(bytes));
80  EXPECT_EQ(4U, wcrtomb(bytes, 0x24b62, NULL));
81  EXPECT_EQ('\xf0', bytes[0]);
82  EXPECT_EQ('\xa4', bytes[1]);
83  EXPECT_EQ('\xad', bytes[2]);
84  EXPECT_EQ('\xa2', bytes[3]);
85  // Invalid code point.
86  EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(bytes, 0xffffffff, NULL));
87  EXPECT_EQ(EILSEQ, errno);
88}
89
90TEST(wchar, wcrtomb_start_state) {
91  char out[MB_LEN_MAX];
92  mbstate_t ps;
93
94  // Any non-initial state is invalid when calling wcrtomb.
95  memset(&ps, 0, sizeof(ps));
96  EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(NULL, "\xc2", 1, &ps));
97  EXPECT_EQ(static_cast<size_t>(-1), wcrtomb(out, 0x00a2, &ps));
98  EXPECT_EQ(EILSEQ, errno);
99
100  // If the first argument to wcrtomb is NULL or the second is L'\0' the shift
101  // state should be reset.
102  memset(&ps, 0, sizeof(ps));
103  EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(NULL, "\xc2", 1, &ps));
104  EXPECT_EQ(1U, wcrtomb(NULL, 0x00a2, &ps));
105  EXPECT_TRUE(mbsinit(&ps));
106
107  memset(&ps, 0, sizeof(ps));
108  EXPECT_EQ(static_cast<size_t>(-2), mbrtowc(NULL, "\xf0\xa4", 1, &ps));
109  EXPECT_EQ(1U, wcrtomb(out, L'\0', &ps));
110  EXPECT_TRUE(mbsinit(&ps));
111}
112
113TEST(wchar, wcstombs_wcrtombs) {
114  const wchar_t chars[] = { L'h', L'e', L'l', L'l', L'o', 0 };
115  const wchar_t bad_chars[] = { L'h', L'i', static_cast<wchar_t>(0xffffffff), 0 };
116  const wchar_t* src;
117  char bytes[BUFSIZ];
118
119  // Given a NULL destination, these functions count valid characters.
120  EXPECT_EQ(5U, wcstombs(NULL, chars, 0));
121  EXPECT_EQ(5U, wcstombs(NULL, chars, 4));
122  EXPECT_EQ(5U, wcstombs(NULL, chars, 256));
123  src = chars;
124  EXPECT_EQ(5U, wcsrtombs(NULL, &src, 0, NULL));
125  EXPECT_EQ(&chars[0], src);
126  src = chars;
127  EXPECT_EQ(5U, wcsrtombs(NULL, &src, 4, NULL));
128  EXPECT_EQ(&chars[0], src);
129  src = chars;
130  EXPECT_EQ(5U, wcsrtombs(NULL, &src, 256, NULL));
131  EXPECT_EQ(&chars[0], src);
132
133  // An unrepresentable char just returns an error from wcstombs...
134  errno = 0;
135  EXPECT_EQ(static_cast<size_t>(-1), wcstombs(NULL, bad_chars, 0));
136  EXPECT_EQ(EILSEQ, errno);
137  errno = 0;
138  EXPECT_EQ(static_cast<size_t>(-1), wcstombs(NULL, bad_chars, 256));
139  EXPECT_EQ(EILSEQ, errno);
140
141  // And wcsrtombs doesn't tell us where it got stuck because we didn't ask it
142  // to actually convert anything...
143  errno = 0;
144  src = bad_chars;
145  EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(NULL, &src, 0, NULL));
146  EXPECT_EQ(&bad_chars[0], src);
147  EXPECT_EQ(EILSEQ, errno);
148  errno = 0;
149  src = bad_chars;
150  EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(NULL, &src, 256, NULL));
151  EXPECT_EQ(&bad_chars[0], src);
152  EXPECT_EQ(EILSEQ, errno);
153
154  // Okay, now let's test actually converting something...
155  memset(bytes, 'x', sizeof(bytes));
156  EXPECT_EQ(0U, wcstombs(bytes, chars, 0));
157  memset(bytes, 'x', sizeof(bytes));
158  EXPECT_EQ(4U, wcstombs(bytes, chars, 4));
159  bytes[5] = 0;
160  EXPECT_STREQ("hellx", bytes);
161  memset(bytes, 'x', sizeof(bytes));
162  EXPECT_EQ(5U, wcstombs(bytes, chars, 256));
163  EXPECT_STREQ("hello", bytes);
164  memset(bytes, 'x', sizeof(bytes));
165  EXPECT_EQ(5U, wcstombs(bytes, chars, 6));
166  EXPECT_STREQ("hello", bytes);
167  errno = 0;
168  memset(bytes, 'x', sizeof(bytes));
169  EXPECT_EQ(static_cast<size_t>(-1), wcstombs(bytes, bad_chars, 256));
170  EXPECT_EQ(EILSEQ, errno);
171  bytes[3] = 0;
172  EXPECT_STREQ("hix", bytes);
173
174  // wcsrtombs is a bit more informative...
175  memset(bytes, 'x', sizeof(bytes));
176  src = chars;
177  EXPECT_EQ(0U, wcsrtombs(bytes, &src, 0, NULL));
178  EXPECT_EQ(&chars[0], src); // No input consumed.
179  EXPECT_EQ(EILSEQ, errno);
180
181  memset(bytes, 'x', sizeof(bytes));
182  src = chars;
183  EXPECT_EQ(4U, wcsrtombs(bytes, &src, 4, NULL));
184  EXPECT_EQ(&chars[4], src); // Some input consumed.
185  EXPECT_EQ(EILSEQ, errno);
186  bytes[5] = 0;
187  EXPECT_STREQ("hellx", bytes);
188
189  memset(bytes, 'x', sizeof(bytes));
190  src = chars;
191  EXPECT_EQ(5U, wcsrtombs(bytes, &src, 256, NULL));
192  EXPECT_EQ(NULL, src); // All input consumed!
193  EXPECT_EQ(EILSEQ, errno);
194  EXPECT_STREQ("hello", bytes);
195
196  memset(bytes, 'x', sizeof(bytes));
197  src = chars;
198  EXPECT_EQ(5U, wcsrtombs(bytes, &src, 6, NULL));
199  EXPECT_EQ(NULL, src); // All input consumed.
200  EXPECT_EQ(EILSEQ, errno);
201  EXPECT_STREQ("hello", bytes);
202
203  memset(bytes, 'x', sizeof(bytes));
204  src = bad_chars;
205  EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(bytes, &src, 256, NULL));
206  EXPECT_EQ(&bad_chars[2], src);
207  EXPECT_EQ(EILSEQ, errno);
208  bytes[3] = 0;
209  EXPECT_STREQ("hix", bytes);
210
211  // Any non-initial state is invalid when calling wcsrtombs.
212  mbstate_t ps;
213  src = chars;
214  memset(&ps, 0, sizeof(ps));
215  ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(NULL, "\xc2", 1, &ps));
216  EXPECT_EQ(static_cast<size_t>(-1), wcsrtombs(NULL, &src, 0, &ps));
217  EXPECT_EQ(EILSEQ, errno);
218}
219
220TEST(wchar, limits) {
221  ASSERT_LT(WCHAR_MIN, WCHAR_MAX);
222}
223
224TEST(wchar, wcsstr_wcswcs) {
225  const wchar_t* haystack = L"matches hello world, not the second hello world";
226  const wchar_t* empty_needle = L"";
227  const wchar_t* good_needle = L"ll";
228  const wchar_t* bad_needle = L"wort";
229
230  ASSERT_EQ(haystack, wcsstr(haystack, empty_needle));
231  ASSERT_EQ(&haystack[10], wcsstr(haystack, good_needle));
232  ASSERT_EQ(NULL, wcsstr(haystack, bad_needle));
233
234  ASSERT_EQ(haystack, wcswcs(haystack, empty_needle));
235  ASSERT_EQ(&haystack[10], wcswcs(haystack, good_needle));
236  ASSERT_EQ(NULL, wcswcs(haystack, bad_needle));
237}
238
239TEST(wchar, mbtowc) {
240  wchar_t out[8];
241
242  out[0] = 'x';
243  ASSERT_EQ(0, mbtowc(out, "hello", 0));
244  ASSERT_EQ('x', out[0]);
245
246  ASSERT_EQ(0, mbtowc(out, "hello", 0));
247  ASSERT_EQ(0, mbtowc(out, "", 0));
248  ASSERT_EQ(1, mbtowc(out, "hello", 1));
249  ASSERT_EQ(L'h', out[0]);
250
251  ASSERT_EQ(0, mbtowc(NULL, "hello", 0));
252  ASSERT_EQ(0, mbtowc(NULL, "", 0));
253  ASSERT_EQ(1, mbtowc(NULL, "hello", 1));
254
255  ASSERT_EQ(0, mbtowc(NULL, NULL, 0));
256}
257
258TEST(wchar, mbrtowc) {
259  wchar_t out[8];
260
261  out[0] = 'x';
262  ASSERT_EQ(0U, mbrtowc(out, "hello", 0, NULL));
263  ASSERT_EQ('x', out[0]);
264
265  ASSERT_EQ(0U, mbrtowc(out, "hello", 0, NULL));
266  ASSERT_EQ(0U, mbrtowc(out, "", 0, NULL));
267  ASSERT_EQ(1U, mbrtowc(out, "hello", 1, NULL));
268  ASSERT_EQ(L'h', out[0]);
269
270  ASSERT_EQ(0U, mbrtowc(NULL, "hello", 0, NULL));
271  ASSERT_EQ(0U, mbrtowc(NULL, "", 0, NULL));
272  ASSERT_EQ(1U, mbrtowc(NULL, "hello", 1, NULL));
273
274  ASSERT_EQ(0U, mbrtowc(NULL, NULL, 0, NULL));
275
276  ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
277  uselocale(LC_GLOBAL_LOCALE);
278
279  // 1-byte UTF-8.
280  ASSERT_EQ(1U, mbrtowc(out, "abcdef", 6, NULL));
281  ASSERT_EQ(L'a', out[0]);
282  // 2-byte UTF-8.
283  ASSERT_EQ(2U, mbrtowc(out, "\xc2\xa2" "cdef", 6, NULL));
284  ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[0]);
285  // 3-byte UTF-8.
286  ASSERT_EQ(3U, mbrtowc(out, "\xe2\x82\xac" "def", 6, NULL));
287  ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[0]);
288  // 4-byte UTF-8.
289  ASSERT_EQ(4U, mbrtowc(out, "\xf0\xa4\xad\xa2" "ef", 6, NULL));
290  ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[0]);
291#if defined(__BIONIC__) // glibc allows this.
292  // Illegal 5-byte UTF-8.
293  ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(out, "\xf8\xa1\xa2\xa3\xa4" "f", 6, NULL));
294  ASSERT_EQ(EILSEQ, errno);
295#endif
296  // Illegal over-long sequence.
297  ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(out, "\xf0\x82\x82\xac" "ef", 6, NULL));
298  ASSERT_EQ(EILSEQ, errno);
299}
300
301void test_mbrtowc_incomplete(mbstate_t* ps) {
302  ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
303  uselocale(LC_GLOBAL_LOCALE);
304
305  wchar_t out;
306  // 2-byte UTF-8.
307  ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
308  ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "cdef", 5, ps));
309  ASSERT_EQ(static_cast<wchar_t>(0x00a2), out);
310  ASSERT_TRUE(mbsinit(ps));
311  // 3-byte UTF-8.
312  ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xe2", 1, ps));
313  ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\x82", 1, ps));
314  ASSERT_EQ(1U, mbrtowc(&out, "\xac" "def", 4, ps));
315  ASSERT_EQ(static_cast<wchar_t>(0x20ac), out);
316  ASSERT_TRUE(mbsinit(ps));
317  // 4-byte UTF-8.
318  ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xf0", 1, ps));
319  ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xa4\xad", 2, ps));
320  ASSERT_EQ(1U, mbrtowc(&out, "\xa2" "ef", 3, ps));
321  ASSERT_EQ(static_cast<wchar_t>(0x24b62), out);
322  ASSERT_TRUE(mbsinit(ps));
323
324  // Invalid 2-byte
325  ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, ps));
326  ASSERT_EQ(static_cast<size_t>(-1), mbrtowc(&out, "\x20" "cdef", 5, ps));
327  ASSERT_EQ(EILSEQ, errno);
328}
329
330TEST(wchar, mbrtowc_incomplete) {
331  mbstate_t ps;
332  memset(&ps, 0, sizeof(ps));
333
334  test_mbrtowc_incomplete(&ps);
335  test_mbrtowc_incomplete(NULL);
336}
337
338void test_mbsrtowcs(mbstate_t* ps) {
339  wchar_t out[4];
340
341  const char* valid = "A" "\xc2\xa2" "\xe2\x82\xac" "\xf0\xa4\xad\xa2" "ef";
342  ASSERT_EQ(4U, mbsrtowcs(out, &valid, 4, ps));
343  ASSERT_EQ(L'A', out[0]);
344  ASSERT_EQ(static_cast<wchar_t>(0x00a2), out[1]);
345  ASSERT_EQ(static_cast<wchar_t>(0x20ac), out[2]);
346  ASSERT_EQ(static_cast<wchar_t>(0x24b62), out[3]);
347  ASSERT_EQ('e', *valid);
348
349  const char* invalid = "A" "\xc2\x20" "ef";
350  ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &invalid, 4, ps));
351  EXPECT_EQ(EILSEQ, errno);
352  ASSERT_EQ('\xc2', *invalid);
353
354  const char* incomplete = "A" "\xc2";
355  ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(out, &incomplete, 2, ps));
356  EXPECT_EQ(EILSEQ, errno);
357  ASSERT_EQ('\xc2', *incomplete);
358}
359
360TEST(wchar, mbsrtowcs) {
361  ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
362  uselocale(LC_GLOBAL_LOCALE);
363
364  mbstate_t ps;
365  memset(&ps, 0, sizeof(ps));
366  test_mbsrtowcs(&ps);
367  test_mbsrtowcs(NULL);
368
369  // Invalid multi byte continuation.
370  const char* invalid = "\x20";
371  wchar_t out;
372  ASSERT_EQ(static_cast<size_t>(-2), mbrtowc(&out, "\xc2", 1, &ps));
373  ASSERT_EQ(static_cast<size_t>(-1), mbsrtowcs(&out, &invalid, 1, &ps));
374  EXPECT_EQ(EILSEQ, errno);
375  ASSERT_EQ('\x20', *invalid);
376}
377
378TEST(wchar, wcstod) {
379  ASSERT_DOUBLE_EQ(1.23, wcstod(L"1.23", NULL));
380}
381
382TEST(wchar, wcstof) {
383  ASSERT_FLOAT_EQ(1.23f, wcstof(L"1.23", NULL));
384}
385
386TEST(wchar, wcstol) {
387  ASSERT_EQ(123L, wcstol(L"123", NULL, 0));
388}
389
390TEST(wchar, wcstoll) {
391  ASSERT_EQ(123LL, wcstol(L"123", NULL, 0));
392}
393
394TEST(wchar, wcstold) {
395  ASSERT_DOUBLE_EQ(1.23L, wcstold(L"1.23", NULL));
396}
397
398TEST(wchar, wcstoul) {
399  ASSERT_EQ(123UL, wcstoul(L"123", NULL, 0));
400}
401
402TEST(wchar, wcstoull) {
403  ASSERT_EQ(123ULL, wcstoul(L"123", NULL, 0));
404}
405
406TEST(wchar, mbsnrtowcs) {
407  wchar_t dst[128];
408  const char* s = "hello, world!";
409  const char* src;
410
411  memset(dst, 0, sizeof(dst));
412  src = s;
413  ASSERT_EQ(0U, mbsnrtowcs(dst, &src, 0, 0, NULL));
414
415  memset(dst, 0, sizeof(dst));
416  src = s;
417  ASSERT_EQ(2U, mbsnrtowcs(dst, &src, 2, 123, NULL)); // glibc chokes on SIZE_MAX here.
418  ASSERT_EQ(L'h', dst[0]);
419  ASSERT_EQ(L'e', dst[1]);
420  ASSERT_EQ(&s[2], src);
421
422  memset(dst, 0, sizeof(dst));
423  src = s;
424  ASSERT_EQ(3U, mbsnrtowcs(dst, &src, SIZE_MAX, 3, NULL));
425  ASSERT_EQ(L'h', dst[0]);
426  ASSERT_EQ(L'e', dst[1]);
427  ASSERT_EQ(L'l', dst[2]);
428  ASSERT_EQ(&s[3], src);
429}
430
431TEST(wchar, wcsftime) {
432  setenv("TZ", "UTC", 1);
433
434  struct tm t;
435  memset(&t, 0, sizeof(tm));
436  t.tm_year = 200;
437  t.tm_mon = 2;
438  t.tm_mday = 10;
439
440  wchar_t buf[64];
441
442  EXPECT_EQ(24U, wcsftime(buf, sizeof(buf), L"%c", &t));
443  EXPECT_STREQ(L"Sun Mar 10 00:00:00 2100", buf);
444}
445