1 /*
2 * Copyright (C) 2023 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 <wchar.h>
21
22 #include "berberis/ndk_program_tests/file.h" // NOLINT
23
24 // Generally in Bionic there is no support for wide-chars. Majority of functions just convert
25 // between char and wchar_t. But some operations call to BSD-internals that process wide-chars
26 // correctly.
27
28 // File input-output tests
29
30 // wint_t doesn't coincide with the type of L'A'
wint(char symbol)31 static wint_t wint(char symbol) {
32 return static_cast<wint_t>(symbol);
33 }
34
35 // TODO(b/190469865): Fix and enable!
TEST(WChar,DISABLED_FgetwcFputwc)36 TEST(WChar, DISABLED_FgetwcFputwc) {
37 TempFile f;
38 ASSERT_EQ(fputwc(L'A', f.get()), wint(L'A'));
39 ASSERT_EQ(fputwc(L'B', f.get()), wint(L'B'));
40 ASSERT_EQ(fseek(f.get(), 0, SEEK_SET), 0);
41 EXPECT_EQ(fgetwc(f.get()), wint(L'A'));
42 EXPECT_EQ(fgetwc(f.get()), wint(L'B'));
43 }
44
45 // TODO(b/190469865): Fix and enable!
TEST(WChar,DISABLED_Ungetwc)46 TEST(WChar, DISABLED_Ungetwc) {
47 TempFile f;
48 ASSERT_EQ(ungetwc(wint(L'A'), f.get()), wint(L'A'));
49 EXPECT_EQ(fgetwc(f.get()), wint(L'A'));
50 ASSERT_EQ(ungetwc(wint(L'B'), f.get()), wint(L'B'));
51 EXPECT_EQ(fgetwc(f.get()), wint(L'B'));
52 EXPECT_EQ(fgetwc(f.get()), WEOF);
53 }
54
55 // Wchar-type tests
56
TEST(WChar,Iswctype)57 TEST(WChar, Iswctype) {
58 EXPECT_TRUE(iswctype(wint(L'A'), wctype("alpha")));
59 EXPECT_TRUE(iswctype(wint(L' '), wctype("blank")));
60 EXPECT_TRUE(iswctype(wint(L'\n'), wctype("cntrl")));
61 EXPECT_TRUE(iswctype(wint(L'0'), wctype("digit")));
62 EXPECT_TRUE(iswctype(wint(L'A'), wctype("graph")));
63 EXPECT_TRUE(iswctype(wint(L'a'), wctype("lower")));
64 EXPECT_TRUE(iswctype(wint(L'A'), wctype("print")));
65 EXPECT_TRUE(iswctype(wint(L'!'), wctype("punct")));
66 EXPECT_TRUE(iswctype(wint(L' '), wctype("space")));
67 EXPECT_TRUE(iswctype(wint(L'A'), wctype("upper")));
68 EXPECT_TRUE(iswctype(wint(L'F'), wctype("xdigit")));
69
70 EXPECT_FALSE(iswctype(wint(L'Z'), wctype("xdigit")));
71 }
72
TEST(WChar,Towupper)73 TEST(WChar, Towupper) {
74 EXPECT_EQ(towupper(wint(L'a')), wint(L'A'));
75 }
76
TEST(WChar,MbrtowcWcrtomb)77 TEST(WChar, MbrtowcWcrtomb) {
78 wchar_t wc = L'A';
79 wchar_t ref_wc = L'B';
80 EXPECT_TRUE(mbrtowc(&wc, reinterpret_cast<char*>(&ref_wc), sizeof(wchar_t), nullptr));
81 EXPECT_EQ(wc, ref_wc);
82
83 char c = 'C';
84 EXPECT_EQ(wcrtomb(&c, L'D', nullptr), 1U);
85 EXPECT_EQ(c, 'D');
86 }
87
TEST(WChar,Wcscoll)88 TEST(WChar, Wcscoll) {
89 // Bionic's wcscoll doesn't use locale but correctly processes wide-strings (calling wcscmp)
90 EXPECT_EQ(wcscoll(L"ABC", L"ABC"), 0);
91 EXPECT_LT(wcscoll(L"ABC", L"a"), 0);
92 }
93
TEST(WChar,Wcsftime)94 TEST(WChar, Wcsftime) {
95 wchar_t buffer[100];
96 struct tm time_data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 0, "GMT"};
97 EXPECT_EQ(wcsftime(buffer, 100, L"%c", &time_data), 24U);
98 EXPECT_EQ(wcscmp(L"Sat May 3 02:01:00 1905", buffer), 0);
99 }
100
TEST(WChar,Wcsxfrm)101 TEST(WChar, Wcsxfrm) {
102 // Bionic's wcsxfrm doesn't use locale but correctly processes wide-strings (calling wcslen)
103 wchar_t dest[2];
104 EXPECT_EQ(wcsxfrm(nullptr, L"ABC", 0), 3U);
105 EXPECT_EQ(wcsxfrm(dest, L"ABC", 2), 3U);
106 EXPECT_EQ(dest[0], L'A');
107 EXPECT_EQ(dest[1], L'\0');
108 }
109