blob: 20566f26ac1e31b544ec4a22c61e6734da84b8c6 [file] [log] [blame]
Elliott Hughes77e944f2014-04-04 17:34:51 -07001/*
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 <limits.h>
20#include <wchar.h>
21
22TEST(wchar, sizeof_wchar_t) {
23 EXPECT_EQ(4U, sizeof(wchar_t));
24 EXPECT_EQ(4U, sizeof(wint_t));
25}
26
27TEST(wchar, mbrlen) {
28 char bytes[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
29 EXPECT_EQ(0U, mbrlen(&bytes[0], 0, NULL));
30 EXPECT_EQ(1U, mbrlen(&bytes[0], 1, NULL));
31
32 EXPECT_EQ(1U, mbrlen(&bytes[4], 1, NULL));
33 EXPECT_EQ(0U, mbrlen(&bytes[5], 1, NULL));
34}
35
36TEST(wchar, wctomb_wcrtomb) {
37 // wctomb and wcrtomb behave differently when s == NULL.
38 EXPECT_EQ(0, wctomb(NULL, L'h'));
39 EXPECT_EQ(0, wctomb(NULL, L'\0'));
40 EXPECT_EQ(1U, wcrtomb(NULL, L'\0', NULL));
41 EXPECT_EQ(1U, wcrtomb(NULL, L'h', NULL));
42
43 char bytes[MB_LEN_MAX];
44
45 // wctomb and wcrtomb behave similarly for the null wide character.
46 EXPECT_EQ(1, wctomb(bytes, L'\0'));
47 EXPECT_EQ(1U, wcrtomb(bytes, L'\0', NULL));
48
49 // ...and for regular characters.
50 bytes[0] = 'x';
51 EXPECT_EQ(1, wctomb(bytes, L'h'));
52 EXPECT_EQ('h', bytes[0]);
53
54 bytes[0] = 'x';
55 EXPECT_EQ(1U, wcrtomb(bytes, L'h', NULL));
56 EXPECT_EQ('h', bytes[0]);
57}