• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/base/string_helper.h"
17 #include "ecmascript/base/utf_helper.h"
18 #include "ecmascript/global_env.h"
19 #include "ecmascript/tests/test_helper.h"
20 
21 using namespace panda::ecmascript;
22 using namespace panda::ecmascript::base;
23 using namespace panda::ecmascript::base::utf_helper;
24 
25 namespace panda::test {
26 class StringHelperTest : public BaseTestWithScope<true> {
27 };
28 
HWTEST_F_L0(StringHelperTest,ConcatToCString)29 HWTEST_F_L0(StringHelperTest, ConcatToCString)
30 {
31     CString a = "Hello";
32     std::string b = "World";
33     CString result1 = ConcatToCString(a, ", ", b, '!');
34     EXPECT_STREQ(result1.c_str(), "Hello, World!");
35     CString result2 = ConcatToCString(b, ", ", a, '!');
36     EXPECT_STREQ(result2.c_str(), "World, Hello!");
37     CString result4 = ConcatToCString("x", b, ", ", a, '!');
38     EXPECT_STREQ(result4.c_str(), "xWorld, Hello!");
39 }
40 
HWTEST_F_L0(StringHelperTest,ConcatToStdString)41 HWTEST_F_L0(StringHelperTest, ConcatToStdString)
42 {
43     CString a = "Hello";
44     std::string b = "World";
45     std::string result1 = ConcatToStdString(a, ", ", b, '!');
46     EXPECT_STREQ(result1.c_str(), "Hello, World!");
47     std::string result2 = ConcatToStdString(b, ", ", a, '!');
48     EXPECT_STREQ(result2.c_str(), "World, Hello!");
49     std::string result4 = ConcatToStdString("x", b, ", ", a, '!');
50     EXPECT_STREQ(result4.c_str(), "xWorld, Hello!");
51 }
52 
HWTEST_F_L0(StringHelperTest,AppendToBaseCString)53 HWTEST_F_L0(StringHelperTest, AppendToBaseCString)
54 {
55     CString a = "Hello";
56     std::string b = "World";
57     AppendToBaseString(a, ", ", b, '!');
58     EXPECT_STREQ(a.c_str(), "Hello, World!");
59 }
60 
HWTEST_F_L0(StringHelperTest,AppendToBaseStdString)61 HWTEST_F_L0(StringHelperTest, AppendToBaseStdString)
62 {
63     std::string a = "Hello";
64     CString b = "World";
65     AppendToBaseString(a, ", ", b, '!');
66     EXPECT_STREQ(a.c_str(), "Hello, World!");
67 }
68 
HWTEST_F_L0(StringHelperTest,ReplaceAll)69 HWTEST_F_L0(StringHelperTest, ReplaceAll)
70 {
71     CString sourceStr = "H\\e\\l\\l\\o\\W\\o\\r\\l\\d!\0";
72     const CString oldValue1 = "\\";
73     const CString oldValue2 = "World";
74     CString result = StringHelper::ReplaceAll(sourceStr, oldValue1, "");
75     result = StringHelper::ReplaceAll(result, oldValue2, " OpenHarmony");
76     EXPECT_STREQ(result.c_str(), "Hello OpenHarmony!");
77 }
78 
HWTEST_F_L0(StringHelperTest,Utf16ToU16String_Utf8ToU16String)79 HWTEST_F_L0(StringHelperTest, Utf16ToU16String_Utf8ToU16String)
80 {
81     const uint32_t utf16DataLen1 = 11;
82     const uint16_t utf16Data1[utf16DataLen1] = { // "OpenHarmony"
83         0x4f, 0x70, 0x65, 0x6e,
84         0x48, 0x61, 0x72, 0x6d,
85         0x6f, 0x6e, 0x79
86     };
87     std::u16string u16Str1 = StringHelper::Utf16ToU16String(utf16Data1, utf16DataLen1);
88     std::string u16Value1 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16Str1);
89     EXPECT_STREQ(u16Value1.c_str(), "OpenHarmony");
90 
91     const uint32_t utf16DataLen2 = 2;
92     const uint16_t utf16Data2[utf16DataLen2] = { 0x9e3f, 0x8499 }; // "鸿蒙"
93     std::u16string u16Str2 = StringHelper::Utf16ToU16String(utf16Data2, utf16DataLen2);
94     std::string u16Value2 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16Str2);
95     EXPECT_STREQ(u16Value2.c_str(), "鸿蒙");
96 
97     const uint32_t utf8DataLen1 = 11;
98     const uint8_t utf8Data1[utf8DataLen1] = { // "OpenHarmony"
99         0x4f, 0x70, 0x65, 0x6e,
100         0x48, 0x61, 0x72, 0x6d,
101         0x6f, 0x6e, 0x79
102     };
103     std::u16string u8Str1 = StringHelper::Utf8ToU16String(utf8Data1, utf8DataLen1);
104     std::string u8Value1 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u8Str1);
105     EXPECT_STREQ(u8Value1.c_str(), "OpenHarmony");
106 
107     const uint32_t utf8DataLen2 = 6;
108     const uint8_t utf8Data2[utf8DataLen2] = { 0xe9, 0xb8, 0xbf, 0xe8, 0x92, 0x99 }; // "鸿蒙"
109     std::u16string u8Str2 = StringHelper::Utf8ToU16String(utf8Data2, utf8DataLen2);
110     std::string u8Value2 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u8Str2);
111     EXPECT_STREQ(u8Value2.c_str(), "鸿蒙");
112 }
113 
HWTEST_F_L0(StringHelperTest,Find_RFind)114 HWTEST_F_L0(StringHelperTest, Find_RFind)
115 {
116     const std::string valueStr = "Hello worldworld";
117     const std::string searchStr1 = "world";
118     const std::string searchStr2 = "undefined";
119     const std::u16string u16ValueStr = StringHelper::StringToU16string(valueStr);
120     const std::u16string u16SearchStr1 = StringHelper::StringToU16string(searchStr1);
121     const std::u16string u16SearchStr2 = StringHelper::StringToU16string(searchStr2);
122 
123     size_t pos1 = StringHelper::Find(valueStr, searchStr1, 0);
124     size_t pos2 = StringHelper::Find(valueStr, searchStr2, 0);
125     size_t pos3 = StringHelper::Find(u16ValueStr, u16SearchStr1, 0);
126     size_t pos4 = StringHelper::Find(u16ValueStr, u16SearchStr2, 0);
127     size_t pos5 = StringHelper::RFind(u16ValueStr, u16SearchStr1, 17); // 17 : Search to the end
128     size_t pos6 = StringHelper::RFind(u16ValueStr, u16SearchStr2, 17); // 17 : Search to the end
129     EXPECT_EQ(pos1, 6U);
130     EXPECT_EQ(pos2, std::string::npos);
131     EXPECT_EQ(pos3, 6U);
132     EXPECT_EQ(pos4, std::string::npos);
133     EXPECT_EQ(pos5, 11U);
134     EXPECT_EQ(pos6, std::string::npos);
135 }
136 
HWTEST_F_L0(StringHelperTest,ToUpper_ToLower_ToLocaleUpper_ToLocaleLower)137 HWTEST_F_L0(StringHelperTest, ToUpper_ToLower_ToLocaleUpper_ToLocaleLower)
138 {
139     const std::u16string u16SourceStr1 = StringHelper::StringToU16string("AbCdEfGhIjKlMnOpQrStUvWxYz");
140     std::string upperStr = StringHelper::ToUpper(u16SourceStr1);
141     std::string lowerStr = StringHelper::ToLower(u16SourceStr1);
142     EXPECT_STREQ(upperStr.c_str(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
143     EXPECT_STREQ(lowerStr.c_str(), "abcdefghijklmnopqrstuvwxyz");
144 
145     icu::Locale locale("el", "Latn", "GR");
146     const std::u16string u16SourceStr2 = StringHelper::StringToU16string("Greek : Αυτό είναι ένα δοκιμαστικό κείμενο.");
147     std::string localeUpperStr = StringHelper::ToLocaleUpper(u16SourceStr2, locale);
148     EXPECT_STREQ(localeUpperStr.c_str(), "GREEK : ΑΥΤΟ ΕΙΝΑΙ ΕΝΑ ΔΟΚΙΜΑΣΤΙΚΟ ΚΕΙΜΕΝΟ.");
149 
150     const std::u16string u16SourceStr3 = StringHelper::StringToU16string("GREEK : ΑΥΤΌ ΕΊΝΑΙ ΈΝΑ ΔΟΚΙΜΑΣΤΙΚΌ ΚΕΊΜΕΝΟ.");
151     std::string localeLowerStr = StringHelper::ToLocaleLower(u16SourceStr3, locale);
152     EXPECT_STREQ(localeLowerStr.c_str(), "greek : αυτό είναι ένα δοκιμαστικό κείμενο.");
153 }
154 
HWTEST_F_L0(StringHelperTest,FindFromU16ToUpper)155 HWTEST_F_L0(StringHelperTest, FindFromU16ToUpper)
156 {
157     const std::u16string u16SourceStr = StringHelper::StringToU16string("HELLO WORLD!");
158     const uint32_t utf16DataLen = 5;
159     const uint32_t utf16DataLenUpper = 9;
160     uint16_t utf16Data[utf16DataLen] = { // "world"
161         0x77, 0x6f, 0x72, 0x6c, 0x64
162     };
163     uint16_t utf16DataUpper[utf16DataLenUpper] = { // "WORLD"
164         0x57, 0x4f, 0x52, 0x4c, 0x44
165     };
166     size_t pos1 = StringHelper::FindFromU16ToUpper(u16SourceStr, utf16Data);
167     size_t pos2 = StringHelper::FindFromU16ToUpper(u16SourceStr, utf16DataUpper);
168     EXPECT_EQ(pos1, 6U);
169     EXPECT_EQ(pos2, 6U);
170 }
171 
HWTEST_F_L0(StringHelperTest,UnicodeFromUtf8)172 HWTEST_F_L0(StringHelperTest, UnicodeFromUtf8)
173 {
174     int maxLen = 1;
175     const uint8_t *p = new uint8_t[6] { 0x00 };
176     const uint8_t **pp = &p;
177     const uint8_t oneByteVaild1[1] = { 0x00 };
178     const uint8_t oneByteVaild2[1] = { BIT_MASK_1 - 0x01 };
179     const uint8_t oneByteUnvaild[1] = { BIT_MASK_1 };
180     EXPECT_EQ(StringHelper::UnicodeFromUtf8(oneByteVaild1, maxLen, pp), 0);
181     EXPECT_EQ(StringHelper::UnicodeFromUtf8(oneByteVaild2, maxLen, pp), 127);
182     EXPECT_EQ(StringHelper::UnicodeFromUtf8(oneByteUnvaild, maxLen, pp), -1);
183 
184     maxLen = 2;
185     const uint8_t twoByteVaild[2] = { BIT_MASK_3 - 0x01, BIT_MASK_2 - 0x01 };
186     const uint8_t twoByteUnvaild1[2] = { BIT_MASK_2, BIT_MASK_2 };
187     const uint8_t twoByteUnvaild2[2] = { BIT_MASK_3, BIT_MASK_1 };
188     EXPECT_EQ(StringHelper::UnicodeFromUtf8(twoByteVaild, maxLen, pp), 2047); // 2047 : utf8 [0xDF, 0xBF]
189     EXPECT_EQ(StringHelper::UnicodeFromUtf8(twoByteUnvaild1, maxLen, pp), -1);
190     EXPECT_EQ(StringHelper::UnicodeFromUtf8(twoByteUnvaild2, maxLen, pp), -1);
191 
192     maxLen = 3;
193     const uint8_t threeByteVaild[3] = { BIT_MASK_4 - 0x01, BIT_MASK_2 - 0x01, BIT_MASK_2 - 0x01 };
194     const uint8_t threeByteUnvaild1[3] = { BIT_MASK_3, BIT_MASK_1, BIT_MASK_2 };
195     const uint8_t threeByteUnvaild2[3] = { BIT_MASK_3, BIT_MASK_2, BIT_MASK_1 };
196     const uint8_t threeByteUnvaild3[3] = { BIT_MASK_4, BIT_MASK_1, BIT_MASK_1 };
197     const uint8_t threeByteUnvaild4[3] = { BIT_MASK_4, BIT_MASK_2, BIT_MASK_2 };
198     EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteVaild, maxLen, pp), 65535); // 65535 : utf8 [0xEF, 0xBF, 0xBF]
199     EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteUnvaild1, maxLen, pp), -1);
200     EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteUnvaild2, maxLen, pp), -1);
201     EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteUnvaild3, maxLen, pp), -1);
202     EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteUnvaild4, maxLen, pp), -1);
203 
204     maxLen = 4;
205     const uint8_t fourByteVaild[4] = {
206         BIT_MASK_5 - 0x01, BIT_MASK_2 - 0x01, BIT_MASK_2 - 0x01, BIT_MASK_2 - 0x01
207     };
208     const uint8_t fourByteUnvaild1[4] = { BIT_MASK_4, BIT_MASK_1, BIT_MASK_1, BIT_MASK_2 };
209     const uint8_t fourByteUnvaild2[4] = { BIT_MASK_4, BIT_MASK_1, BIT_MASK_2, BIT_MASK_1 };
210     const uint8_t fourByteUnvaild3[4] = { BIT_MASK_4, BIT_MASK_2, BIT_MASK_1, BIT_MASK_1 };
211     const uint8_t fourByteUnvaild4[4] = { BIT_MASK_5, BIT_MASK_1, BIT_MASK_1, BIT_MASK_1 };
212     const uint8_t fourByteUnvaild5[4] = { BIT_MASK_5, BIT_MASK_2, BIT_MASK_2, BIT_MASK_2 };
213     EXPECT_EQ(StringHelper::UnicodeFromUtf8(
214         fourByteVaild, maxLen, pp), 2097151); // 2097151 : utf [0xF7, 0xBF, 0xBF, 0xBF]
215     EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild1, maxLen, pp), -1);
216     EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild2, maxLen, pp), -1);
217     EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild3, maxLen, pp), -1);
218     EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild4, maxLen, pp), -1);
219     EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild5, maxLen, pp), -1);
220 }
221 
HWTEST_F_L0(StringHelperTest,Append_SplitString)222 HWTEST_F_L0(StringHelperTest, Append_SplitString)
223 {
224     const std::u16string str1 = StringHelper::StringToU16string("Hello ");
225     const std::u16string str2 = StringHelper::StringToU16string("world!");
226     std::u16string u16Result = StringHelper::Append(str1, str2);
227     std::string result = StringHelper::U16stringToString(u16Result);
228     EXPECT_STREQ(result.c_str(), "Hello world!");
229 
230     const std::string delimiter = " ";
231     std::vector<std::string> resultList = StringHelper::SplitString(result, delimiter);
232     EXPECT_STREQ(resultList[0].c_str(), "Hello");
233     EXPECT_STREQ(resultList[1].c_str(), "world!");
234 }
235 
HWTEST_F_L0(StringHelperTest,GetSpecifiedLine)236 HWTEST_F_L0(StringHelperTest, GetSpecifiedLine)
237 {
238     const std::string srcStr = "Hello\nworld\n!";
239     std::string resLine1 = StringHelper::GetSpecifiedLine(srcStr, 1);
240     std::string resLine2 = StringHelper::GetSpecifiedLine(srcStr, 2);
241     std::string resLine3 = StringHelper::GetSpecifiedLine(srcStr, 3);
242     EXPECT_STREQ(resLine1.c_str(), "Hello");
243     EXPECT_STREQ(resLine2.c_str(), "world");
244     EXPECT_STREQ(resLine3.c_str(), "!");
245     const std::string srcStr1 = "Hello\\nworld\\n!";
246     std::string resLine11 = StringHelper::GetSpecifiedLine(srcStr1, 1);
247     std::string resLine22 = StringHelper::GetSpecifiedLine(srcStr1, 2);
248     std::string resLine33 = StringHelper::GetSpecifiedLine(srcStr1, 3);
249     EXPECT_STREQ(resLine11.c_str(), "Hello");
250     EXPECT_STREQ(resLine22.c_str(), "world");
251     EXPECT_STREQ(resLine33.c_str(), "!");
252 }
253 
HWTEST_F_L0(StringHelperTest,Replace)254 HWTEST_F_L0(StringHelperTest, Replace)
255 {
256     CString sourceStr = "@arkui-x.test.path";
257     CString result = StringHelper::Replace<CString>(sourceStr, "@arkui-x.", "@ohos:");
258     EXPECT_STREQ(result.c_str(), "@ohos:test.path");
259 }
260 }  // namespace panda::test
261