• 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 testing::Test {
27 public:
SetUpTestCase()28     static void SetUpTestCase()
29     {
30         GTEST_LOG_(INFO) << "SetUpTestCase";
31     }
32 
TearDownTestCase()33     static void TearDownTestCase()
34     {
35         GTEST_LOG_(INFO) << "TearDownCase";
36     }
37 
SetUp()38     void SetUp() override
39     {
40         JSRuntimeOptions options;
41 #if PANDA_TARGET_LINUX
42         // for consistency requirement, use ohos_icu4j/data as icu-data-path
43         options.SetIcuDataPath(ICU_PATH);
44 #endif
45         options.SetEnableForceGC(true);
46         instance = JSNApi::CreateEcmaVM(options);
47         instance->SetEnableForceGC(true);
48         ASSERT_TRUE(instance != nullptr) << "Cannot create EcmaVM";
49         thread = instance->GetJSThread();
50         scope = new EcmaHandleScope(thread);
51     }
52 
TearDown()53     void TearDown() override
54     {
55         TestHelper::DestroyEcmaVMWithScope(instance, scope);
56     }
57 
58     EcmaVM *instance {nullptr};
59     EcmaHandleScope *scope {nullptr};
60     JSThread *thread {nullptr};
61 };
62 
HWTEST_F_L0(StringHelperTest,RepalceAll)63 HWTEST_F_L0(StringHelperTest, RepalceAll)
64 {
65     CString sourceStr = "H\\e\\l\\l\\o\\W\\o\\r\\l\\d!\0";
66     const CString oldValue1 = "\\";
67     const CString oldValue2 = "World";
68     CString result = StringHelper::RepalceAll(sourceStr, oldValue1, "");
69     result = StringHelper::RepalceAll(result, oldValue2, " OpenHarmony");
70     EXPECT_STREQ(result.c_str(), "Hello OpenHarmony!");
71 }
72 
HWTEST_F_L0(StringHelperTest,Utf16ToU16String_Utf8ToU16String)73 HWTEST_F_L0(StringHelperTest, Utf16ToU16String_Utf8ToU16String)
74 {
75     const uint32_t utf16DataLen1 = 11;
76     const uint16_t utf16Data1[utf16DataLen1] = { // "OpenHarmony"
77         0x4f, 0x70, 0x65, 0x6e,
78         0x48, 0x61, 0x72, 0x6d,
79         0x6f, 0x6e, 0x79
80     };
81     std::u16string u16Str1 = StringHelper::Utf16ToU16String(utf16Data1, utf16DataLen1);
82     std::string u16Value1 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16Str1);
83     EXPECT_STREQ(u16Value1.c_str(), "OpenHarmony");
84 
85     const uint32_t utf16DataLen2 = 2;
86     const uint16_t utf16Data2[utf16DataLen2] = { 0x9e3f, 0x8499 }; // "鸿蒙"
87     std::u16string u16Str2 = StringHelper::Utf16ToU16String(utf16Data2, utf16DataLen2);
88     std::string u16Value2 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u16Str2);
89     EXPECT_STREQ(u16Value2.c_str(), "鸿蒙");
90 
91     const uint32_t utf8DataLen1 = 11;
92     const uint8_t utf8Data1[utf8DataLen1] = { // "OpenHarmony"
93         0x4f, 0x70, 0x65, 0x6e,
94         0x48, 0x61, 0x72, 0x6d,
95         0x6f, 0x6e, 0x79
96     };
97     std::u16string u8Str1 = StringHelper::Utf8ToU16String(utf8Data1, utf8DataLen1);
98     std::string u8Value1 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u8Str1);
99     EXPECT_STREQ(u8Value1.c_str(), "OpenHarmony");
100 
101     const uint32_t utf8DataLen2 = 6;
102     const uint8_t utf8Data2[utf8DataLen2] = { 0xe9, 0xb8, 0xbf, 0xe8, 0x92, 0x99 }; // "鸿蒙"
103     std::u16string u8Str2 = StringHelper::Utf8ToU16String(utf8Data2, utf8DataLen2);
104     std::string u8Value2 = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(u8Str2);
105     EXPECT_STREQ(u8Value2.c_str(), "鸿蒙");
106 }
107 
HWTEST_F_L0(StringHelperTest,Find_RFind)108 HWTEST_F_L0(StringHelperTest, Find_RFind)
109 {
110     const std::string valueStr = "Hello worldworld";
111     const std::string searchStr1 = "world";
112     const std::string searchStr2 = "undefined";
113     const std::u16string u16ValueStr = StringHelper::StringToU16string(valueStr);
114     const std::u16string u16SearchStr1 = StringHelper::StringToU16string(searchStr1);
115     const std::u16string u16SearchStr2 = StringHelper::StringToU16string(searchStr2);
116 
117     size_t pos1 = StringHelper::Find(valueStr, searchStr1, 0);
118     size_t pos2 = StringHelper::Find(valueStr, searchStr2, 0);
119     size_t pos3 = StringHelper::Find(u16ValueStr, u16SearchStr1, 0);
120     size_t pos4 = StringHelper::Find(u16ValueStr, u16SearchStr2, 0);
121     size_t pos5 = StringHelper::RFind(u16ValueStr, u16SearchStr1, 17); // 17 : Search to the end
122     size_t pos6 = StringHelper::RFind(u16ValueStr, u16SearchStr2, 17); // 17 : Search to the end
123     EXPECT_EQ(pos1, 6U);
124     EXPECT_EQ(pos2, std::string::npos);
125     EXPECT_EQ(pos3, 6U);
126     EXPECT_EQ(pos4, std::string::npos);
127     EXPECT_EQ(pos5, 11U);
128     EXPECT_EQ(pos6, std::string::npos);
129 }
130 
HWTEST_F_L0(StringHelperTest,ToUpper_ToLower_ToLocaleUpper_ToLocaleLower)131 HWTEST_F_L0(StringHelperTest, ToUpper_ToLower_ToLocaleUpper_ToLocaleLower)
132 {
133     const std::u16string u16SourceStr1 = StringHelper::StringToU16string("AbCdEfGhIjKlMnOpQrStUvWxYz");
134     std::string upperStr = StringHelper::ToUpper(u16SourceStr1);
135     std::string lowerStr = StringHelper::ToLower(u16SourceStr1);
136     EXPECT_STREQ(upperStr.c_str(), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
137     EXPECT_STREQ(lowerStr.c_str(), "abcdefghijklmnopqrstuvwxyz");
138 
139     icu::Locale locale("el", "Latn", "GR");
140     const std::u16string u16SourceStr2 = StringHelper::StringToU16string("Greek : Αυτό είναι ένα δοκιμαστικό κείμενο.");
141     std::string localeUpperStr = StringHelper::ToLocaleUpper(u16SourceStr2, locale);
142     EXPECT_STREQ(localeUpperStr.c_str(), "GREEK : ΑΥΤΟ ΕΙΝΑΙ ΕΝΑ ΔΟΚΙΜΑΣΤΙΚΟ ΚΕΙΜΕΝΟ.");
143 
144     const std::u16string u16SourceStr3 = StringHelper::StringToU16string("GREEK : ΑΥΤΌ ΕΊΝΑΙ ΈΝΑ ΔΟΚΙΜΑΣΤΙΚΌ ΚΕΊΜΕΝΟ.");
145     std::string localeLowerStr = StringHelper::ToLocaleLower(u16SourceStr3, locale);
146     EXPECT_STREQ(localeLowerStr.c_str(), "greek : αυτό είναι ένα δοκιμαστικό κείμενο.");
147 }
148 
HWTEST_F_L0(StringHelperTest,FindFromU16ToUpper)149 HWTEST_F_L0(StringHelperTest, FindFromU16ToUpper)
150 {
151     const std::u16string u16SourceStr = StringHelper::StringToU16string("HELLO WORLD!");
152     const uint32_t utf16DataLen = 5;
153     const uint32_t utf16DataLenUpper = 9;
154     uint16_t utf16Data[utf16DataLen] = { // "world"
155         0x77, 0x6f, 0x72, 0x6c, 0x64
156     };
157     uint16_t utf16DataUpper[utf16DataLenUpper] = { // "WORLD"
158         0x57, 0x4f, 0x52, 0x4c, 0x44
159     };
160     size_t pos1 = StringHelper::FindFromU16ToUpper(u16SourceStr, utf16Data);
161     size_t pos2 = StringHelper::FindFromU16ToUpper(u16SourceStr, utf16DataUpper);
162     EXPECT_EQ(pos1, 6U);
163     EXPECT_EQ(pos2, 6U);
164 }
165 
HWTEST_F_L0(StringHelperTest,UnicodeFromUtf8)166 HWTEST_F_L0(StringHelperTest, UnicodeFromUtf8)
167 {
168     int maxLen = 1;
169     const uint8_t *p = new uint8_t[6] { 0x00 };
170     const uint8_t **pp = &p;
171     const uint8_t oneByteVaild1[1] = { 0x00 };
172     const uint8_t oneByteVaild2[1] = { BIT_MASK_1 - 0x01 };
173     const uint8_t oneByteUnvaild[1] = { BIT_MASK_1 };
174     EXPECT_EQ(StringHelper::UnicodeFromUtf8(oneByteVaild1, maxLen, pp), 0);
175     EXPECT_EQ(StringHelper::UnicodeFromUtf8(oneByteVaild2, maxLen, pp), 127);
176     EXPECT_EQ(StringHelper::UnicodeFromUtf8(oneByteUnvaild, maxLen, pp), -1);
177 
178     maxLen = 2;
179     const uint8_t twoByteVaild[2] = { BIT_MASK_3 - 0x01, BIT_MASK_2 - 0x01 };
180     const uint8_t twoByteUnvaild1[2] = { BIT_MASK_2, BIT_MASK_2 };
181     const uint8_t twoByteUnvaild2[2] = { BIT_MASK_3, BIT_MASK_1 };
182     EXPECT_EQ(StringHelper::UnicodeFromUtf8(twoByteVaild, maxLen, pp), 2047); // 2047 : utf8 [0xDF, 0xBF]
183     EXPECT_EQ(StringHelper::UnicodeFromUtf8(twoByteUnvaild1, maxLen, pp), -1);
184     EXPECT_EQ(StringHelper::UnicodeFromUtf8(twoByteUnvaild2, maxLen, pp), -1);
185 
186     maxLen = 3;
187     const uint8_t threeByteVaild[3] = { BIT_MASK_4 - 0x01, BIT_MASK_2 - 0x01, BIT_MASK_2 - 0x01 };
188     const uint8_t threeByteUnvaild1[3] = { BIT_MASK_3, BIT_MASK_1, BIT_MASK_2 };
189     const uint8_t threeByteUnvaild2[3] = { BIT_MASK_3, BIT_MASK_2, BIT_MASK_1 };
190     const uint8_t threeByteUnvaild3[3] = { BIT_MASK_4, BIT_MASK_1, BIT_MASK_1 };
191     const uint8_t threeByteUnvaild4[3] = { BIT_MASK_4, BIT_MASK_2, BIT_MASK_2 };
192     EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteVaild, maxLen, pp), 65535); // 65535 : utf8 [0xEF, 0xBF, 0xBF]
193     EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteUnvaild1, maxLen, pp), -1);
194     EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteUnvaild2, maxLen, pp), -1);
195     EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteUnvaild3, maxLen, pp), -1);
196     EXPECT_EQ(StringHelper::UnicodeFromUtf8(threeByteUnvaild4, maxLen, pp), -1);
197 
198     maxLen = 4;
199     const uint8_t fourByteVaild[4] = {
200         BIT_MASK_5 - 0x01, BIT_MASK_2 - 0x01, BIT_MASK_2 - 0x01, BIT_MASK_2 - 0x01
201     };
202     const uint8_t fourByteUnvaild1[4] = { BIT_MASK_4, BIT_MASK_1, BIT_MASK_1, BIT_MASK_2 };
203     const uint8_t fourByteUnvaild2[4] = { BIT_MASK_4, BIT_MASK_1, BIT_MASK_2, BIT_MASK_1 };
204     const uint8_t fourByteUnvaild3[4] = { BIT_MASK_4, BIT_MASK_2, BIT_MASK_1, BIT_MASK_1 };
205     const uint8_t fourByteUnvaild4[4] = { BIT_MASK_5, BIT_MASK_1, BIT_MASK_1, BIT_MASK_1 };
206     const uint8_t fourByteUnvaild5[4] = { BIT_MASK_5, BIT_MASK_2, BIT_MASK_2, BIT_MASK_2 };
207     EXPECT_EQ(StringHelper::UnicodeFromUtf8(
208         fourByteVaild, maxLen, pp), 2097151); // 2097151 : utf [0xF7, 0xBF, 0xBF, 0xBF]
209     EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild1, maxLen, pp), -1);
210     EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild2, maxLen, pp), -1);
211     EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild3, maxLen, pp), -1);
212     EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild4, maxLen, pp), -1);
213     EXPECT_EQ(StringHelper::UnicodeFromUtf8(fourByteUnvaild5, maxLen, pp), -1);
214 }
215 
HWTEST_F_L0(StringHelperTest,Append_SplitString)216 HWTEST_F_L0(StringHelperTest, Append_SplitString)
217 {
218     const std::u16string str1 = StringHelper::StringToU16string("Hello ");
219     const std::u16string str2 = StringHelper::StringToU16string("world!");
220     std::u16string u16Result = StringHelper::Append(str1, str2);
221     std::string result = StringHelper::U16stringToString(u16Result);
222     EXPECT_STREQ(result.c_str(), "Hello world!");
223 
224     const std::string delimiter = " ";
225     std::vector<std::string> resultList = StringHelper::SplitString(result, delimiter);
226     EXPECT_STREQ(resultList[0].c_str(), "Hello");
227     EXPECT_STREQ(resultList[1].c_str(), "world!");
228 }
229 
HWTEST_F_L0(StringHelperTest,GetSpecifiedLine)230 HWTEST_F_L0(StringHelperTest, GetSpecifiedLine)
231 {
232     const std::string srcStr = "Hello\nworld\n!";
233     std::string resLine1 = StringHelper::GetSpecifiedLine(srcStr, 1);
234     std::string resLine2 = StringHelper::GetSpecifiedLine(srcStr, 2);
235     std::string resLine3 = StringHelper::GetSpecifiedLine(srcStr, 3);
236     EXPECT_STREQ(resLine1.c_str(), "Hello");
237     EXPECT_STREQ(resLine2.c_str(), "world");
238     EXPECT_STREQ(resLine3.c_str(), "!");
239 }
240 }  // namespace panda::test
241