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