1 /* 2 * Copyright (c) 2021-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 <atomic> 17 #include <chrono> 18 #include <ctime> 19 #include <memory> 20 #include <regex> 21 #include <string> 22 #include <sys/time.h> 23 24 #include "gtest/gtest.h" 25 26 #include "base/log/log.h" 27 #include "base/utils/base_id.h" 28 #include "base/utils/date_util.h" 29 #include "base/utils/resource_configuration.h" 30 #include "base/utils/string_expression.h" 31 #include "base/utils/string_utils.h" 32 #include "base/utils/time_util.h" 33 #include "base/utils/utf.h" 34 #include "base/utils/utf_helper.h" 35 #include "base/utils/utils.h" 36 37 #ifndef WINDOWS_PLATFORM 38 #include "securec.h" 39 #endif 40 41 using namespace testing; 42 using namespace testing::ext; 43 44 namespace OHOS::Ace { 45 namespace { 46 constexpr uint32_t DEFAULT_YEAR = 1900; 47 constexpr uint32_t DEFAULT_MONTH = 0; 48 constexpr uint32_t DEFAULT_DAY = 1; 49 constexpr uint32_t DEFAULT_WEEK = 0; 50 constexpr uint32_t DAY_OF_MONTH_TWENTY_NINE = 29; 51 constexpr uint32_t DAY_OF_MONTH_THIRTY = 30; 52 constexpr uint32_t DAY_OF_MONTH_THIRTY_ONE = 31; 53 constexpr uint32_t DAY_OF_MONTH_DEFAULT = 0; 54 constexpr uint32_t DAY_OF_WEEK = 4; 55 constexpr uint32_t ADD_ONE = 1; 56 constexpr uint32_t TEST_INPUT_UINT32 = 123456; 57 constexpr uint64_t NUM_OF_ID = 1; 58 constexpr int32_t TEST_INPUT_INT32 = 123456; 59 constexpr int32_t TWELVE_HOUR_BASE = 12; 60 constexpr int32_t TWENTY_FOUR_HOUR_BASE = 24; 61 constexpr int32_t DAY_TIME_LOWER_LIMIT = 6; 62 constexpr int32_t DAY_TIME_UPPER_LIMIT = 18; 63 constexpr int32_t SET_HOURS_FOR_THE_FIRST_TIME = -13; 64 constexpr int32_t SET_HOURS_FOR_THE_SECOND_TIME = 8; 65 constexpr int32_t SET_HOURS_FOR_THE_THIRD_TIME = 24; 66 constexpr int32_t GET_HOURSWEST_FOR_THE_FIRST_TIME = 11; 67 constexpr int32_t GET_HOURSWEST_FOR_THE_SECOND_TIME = 8; 68 constexpr int32_t TEST_INPUT_ARGS_TWO = 2022; 69 constexpr int64_t TEST_INPUT_INT64 = 123456; 70 constexpr int64_t MICROSEC_TO_MILLISEC = 1000; 71 constexpr int64_t NANOSEC_TO_MILLISEC = 1000000; 72 constexpr int64_t CONTENT_OF_RESOURCEHANDLERS = 255; 73 constexpr size_t MAX_STRING_SIZE = 256; 74 constexpr double NORMAL_CALC_RESULT = 0.0; 75 constexpr double ERROR_CALC_RESULT = 0.0; 76 constexpr double CONFIGURATION_OF_DENSITY = 1.0; 77 constexpr double CONFIGURATION_OF_FONT_RATIO = 1.0; 78 constexpr double TEST_INPUT_DOUBLE = 123456; 79 constexpr double STRING_TO_DIMENSION_RESULT = 100.0; 80 constexpr double STRING_TO_DEGREE_RESULT = 360.0; 81 constexpr float TEST_INPUT_FLOAT = 123456; 82 const std::string CONFIGURATION_PATH = "/data/app/el2/100/base"; 83 const std::string FORMULA_ONE = "2 * 3 - (2 + 3) / 5 + 6 / 2"; 84 const std::string FORMULA_TWO = "8 / 2 + 1 )"; 85 const std::string FORMULA_THREE = "931 - 3 * + 102 / +"; 86 const std::string TEST_INPUT_FMT_STR = "%s-%d"; 87 const std::string FMT_OUT_STR = "TODAY-2022"; 88 const std::string TEST_INPUT_U8_STRING = "THIS IS A STRING"; 89 const std::string DEFAULT_STRING = "error"; 90 const std::string TEST_INPUT_U8_STRING_NULL = ""; 91 const std::string TEST_INPUT_U8_STRING_NUMBER = "123456"; 92 const std::string STRING_TO_CALC_DIMENSION_RESULT = "100.0calc"; 93 const std::u16string TEST_INPUT_U16_STRING = u"THIS IS A STRING"; 94 const std::u16string TEST_INPUT_U16_EMOJI = u""; 95 const std::u16string DEFAULT_USTRING = u"error"; 96 const std::u32string TEST_INPUT_U32_STRING = U"THIS IS A STRING"; 97 const std::u32string TEST_INPUT_U32_EMOJI = U""; 98 const std::u32string DEFAULT_U32STRING = U"error"; 99 const std::wstring TEST_INPUT_W_STRING = L"THIS IS A STRING"; 100 const std::wstring DEFAULT_WSTRING = L"error"; 101 const char TEST_INPUT_ARGS_ONE[MAX_STRING_SIZE] = "TODAY"; 102 const std::vector<int64_t> RESOURCEHANDLERS = { 255 }; 103 } // namespace 104 105 class BaseUtilsTest : public testing::Test {}; 106 107 /** 108 * @tc.name: BaseUtilsTest001 109 * @tc.desc: Call BaseId() constructor twice 110 * @tc.type: FUNC 111 */ 112 HWTEST_F(BaseUtilsTest, BaseUtilsTest001, TestSize.Level1) 113 { 114 BaseId baseId_1; 115 ASSERT_TRUE(baseId_1.GetId() == NUM_OF_ID); 116 } 117 118 /** 119 * @tc.name: BaseUtilsTest002 120 * @tc.desc: Set year:2022,month:10,day:28 121 * @tc.type: FUNC 122 */ 123 HWTEST_F(BaseUtilsTest, BaseUtilsTest002, TestSize.Level1) 124 { 125 Date oneDay; 126 ASSERT_EQ(oneDay.year, DEFAULT_YEAR); 127 ASSERT_EQ(oneDay.month, DEFAULT_MONTH); 128 ASSERT_EQ(oneDay.day, DEFAULT_DAY); 129 ASSERT_EQ(oneDay.week, DEFAULT_WEEK); 130 auto locaDay = oneDay.Current(); 131 time_t nowTime; 132 struct tm* localTime; 133 time(&nowTime); 134 localTime = localtime(&nowTime); 135 ASSERT_EQ(locaDay.year, localTime->tm_year + DEFAULT_YEAR); 136 ASSERT_EQ(locaDay.month, localTime->tm_mon + ADD_ONE); 137 ASSERT_EQ(locaDay.day, localTime->tm_mday); 138 ASSERT_EQ(locaDay.week, localTime->tm_wday); 139 ASSERT_EQ(locaDay.DayOfMonth(2000, 1), DAY_OF_MONTH_THIRTY_ONE); 140 ASSERT_EQ(locaDay.DayOfMonth(2000, 2), DAY_OF_MONTH_TWENTY_NINE); 141 ASSERT_EQ(locaDay.DayOfMonth(2000, 3), DAY_OF_MONTH_THIRTY_ONE); 142 ASSERT_EQ(locaDay.DayOfMonth(2000, 4), DAY_OF_MONTH_THIRTY); 143 ASSERT_EQ(locaDay.DayOfMonth(2000, 5), DAY_OF_MONTH_THIRTY_ONE); 144 ASSERT_EQ(locaDay.DayOfMonth(2000, 6), DAY_OF_MONTH_THIRTY); 145 ASSERT_EQ(locaDay.DayOfMonth(2000, 7), DAY_OF_MONTH_THIRTY_ONE); 146 ASSERT_EQ(locaDay.DayOfMonth(2000, 8), DAY_OF_MONTH_THIRTY_ONE); 147 ASSERT_EQ(locaDay.DayOfMonth(2000, 9), DAY_OF_MONTH_THIRTY); 148 ASSERT_EQ(locaDay.DayOfMonth(2000, 10), DAY_OF_MONTH_THIRTY_ONE); 149 ASSERT_EQ(locaDay.DayOfMonth(2000, 11), DAY_OF_MONTH_THIRTY); 150 ASSERT_EQ(locaDay.DayOfMonth(2000, 12), DAY_OF_MONTH_THIRTY_ONE); 151 ASSERT_EQ(locaDay.DayOfMonth(2000, 13), DAY_OF_MONTH_DEFAULT); 152 ASSERT_EQ(locaDay.CalculateWeekDay(2000, 1, 28), DAY_OF_WEEK); 153 } 154 155 /** 156 * @tc.name: BaseUtilsTest003 157 * @tc.desc: Give a normal formula to ConvertDal2Rpn 158 * @tc.type: FUNC 159 */ 160 HWTEST_F(BaseUtilsTest, BaseUtilsTest003, TestSize.Level1) 161 { 162 double CalculateExp(const std::string& expression, const std::function<double(const Dimension&)>& calcFunc); 163 auto calcResult = __anonf6e886ea0202(const Dimension& dim) 164 StringExpression::CalculateExp(FORMULA_ONE, [](const Dimension& dim) -> double { return dim.Value(); }); 165 ASSERT_EQ(calcResult, NORMAL_CALC_RESULT); 166 } 167 168 /** 169 * @tc.name: BaseUtilsTest004 170 * @tc.desc: Give a error formula to ConvertDal2Rpn 171 * @tc.type: FUNC 172 */ 173 HWTEST_F(BaseUtilsTest, BaseUtilsTest004, TestSize.Level1) 174 { 175 double CalculateExp(const std::string& expression, const std::function<double(const Dimension&)>& calcFunc); 176 auto errResultTwo = __anonf6e886ea0302(const Dimension& dim) 177 StringExpression::CalculateExp(FORMULA_TWO, [](const Dimension& dim) -> double { return dim.Value(); }); 178 auto errResultThree = __anonf6e886ea0402(const Dimension& dim) 179 StringExpression::CalculateExp(FORMULA_THREE, [](const Dimension& dim) -> double { return dim.Value(); }); 180 ASSERT_EQ(errResultTwo, ERROR_CALC_RESULT); 181 ASSERT_EQ(errResultThree, ERROR_CALC_RESULT); 182 } 183 184 /** 185 * @tc.name: BaseUtilsTest005 186 * @tc.desc: Check FormatString 187 * @tc.type: FUNC 188 */ 189 HWTEST_F(BaseUtilsTest, BaseUtilsTest005, TestSize.Level1) 190 { 191 auto firstTestText = StringUtils::FormatString(""); 192 auto secondTestText = 193 StringUtils::FormatString(TEST_INPUT_FMT_STR.c_str(), TEST_INPUT_ARGS_ONE, TEST_INPUT_ARGS_TWO); 194 ASSERT_TRUE(firstTestText == ""); 195 ASSERT_EQ(secondTestText, FMT_OUT_STR); 196 } 197 198 /** 199 * @tc.name: BaseUtilsTest006 200 * @tc.desc: Set the time zone within [-14, -12] 201 * @tc.type: FUNC 202 */ 203 HWTEST_F(BaseUtilsTest, BaseUtilsTest006, TestSize.Level1) 204 { 205 time_t nowTime; 206 struct tm* localTime; 207 time(&nowTime); 208 localTime = gmtime(&nowTime); 209 int32_t localTimeHour12, localTimeHour24; 210 auto theTimeOfNow = GetTimeOfNow(SET_HOURS_FOR_THE_FIRST_TIME); 211 auto theTimeOfZone = GetTimeOfZone(SET_HOURS_FOR_THE_FIRST_TIME); 212 localTimeHour24 = localTime->tm_hour - SET_HOURS_FOR_THE_FIRST_TIME; 213 if (localTimeHour24 >= TWENTY_FOUR_HOUR_BASE) { 214 localTimeHour24 -= TWENTY_FOUR_HOUR_BASE; 215 } else if (localTimeHour24 < 0) { 216 localTimeHour24 += TWENTY_FOUR_HOUR_BASE; 217 } 218 if (localTimeHour24 >= TWELVE_HOUR_BASE) { 219 localTimeHour12 = localTimeHour24 - TWELVE_HOUR_BASE; 220 } else { 221 localTimeHour12 = localTimeHour24; 222 } 223 ASSERT_EQ(theTimeOfNow.hoursWest_, GET_HOURSWEST_FOR_THE_FIRST_TIME); 224 ASSERT_EQ(theTimeOfNow.second_, localTime->tm_sec); 225 ASSERT_EQ(theTimeOfNow.minute_, localTime->tm_min); 226 ASSERT_EQ(theTimeOfNow.hour12_, localTimeHour12); 227 ASSERT_EQ(theTimeOfNow.hour24_, localTimeHour24); 228 ASSERT_EQ(theTimeOfZone.hoursWest_, GET_HOURSWEST_FOR_THE_FIRST_TIME); 229 ASSERT_EQ(theTimeOfZone.second_, localTime->tm_sec); 230 ASSERT_EQ(theTimeOfZone.minute_, localTime->tm_min); 231 ASSERT_EQ(theTimeOfZone.hour12_, localTimeHour12); 232 ASSERT_EQ(theTimeOfZone.hour24_, localTimeHour24); 233 } 234 235 /** 236 * @tc.name: BaseUtilsTest007 237 * @tc.desc: Set the time zone within [-12, 12] 238 * @tc.type: FUNC 239 */ 240 HWTEST_F(BaseUtilsTest, BaseUtilsTest007, TestSize.Level1) 241 { 242 time_t nowTime; 243 struct tm* localTime; 244 time(&nowTime); 245 localTime = gmtime(&nowTime); 246 int32_t localTimeHour12, localTimeHour24; 247 auto theTimeOfNow = GetTimeOfNow(SET_HOURS_FOR_THE_SECOND_TIME); 248 auto theTimeOfZone = GetTimeOfZone(SET_HOURS_FOR_THE_SECOND_TIME); 249 localTimeHour24 = localTime->tm_hour - SET_HOURS_FOR_THE_SECOND_TIME; 250 if (localTimeHour24 >= TWENTY_FOUR_HOUR_BASE) { 251 localTimeHour24 -= TWENTY_FOUR_HOUR_BASE; 252 } else if (localTimeHour24 < 0) { 253 localTimeHour24 += TWENTY_FOUR_HOUR_BASE; 254 } 255 if (localTimeHour24 >= TWELVE_HOUR_BASE) { 256 localTimeHour12 = localTimeHour24 - TWELVE_HOUR_BASE; 257 } else { 258 localTimeHour12 = localTimeHour24; 259 } 260 bool isDayTimeOfNow = IsDayTime(theTimeOfNow); 261 bool isDayTimeOfZone = IsDayTime(theTimeOfZone); 262 ASSERT_EQ(theTimeOfNow.hoursWest_, GET_HOURSWEST_FOR_THE_SECOND_TIME); 263 ASSERT_EQ(theTimeOfNow.second_, localTime->tm_sec); 264 ASSERT_EQ(theTimeOfNow.minute_, localTime->tm_min); 265 ASSERT_EQ(theTimeOfNow.hour12_, localTimeHour12); 266 ASSERT_EQ(theTimeOfNow.hour24_, localTimeHour24); 267 ASSERT_EQ(theTimeOfZone.hoursWest_, GET_HOURSWEST_FOR_THE_SECOND_TIME); 268 ASSERT_EQ(theTimeOfZone.second_, localTime->tm_sec); 269 ASSERT_EQ(theTimeOfZone.minute_, localTime->tm_min); 270 ASSERT_EQ(theTimeOfZone.hour12_, localTimeHour12); 271 ASSERT_EQ(theTimeOfZone.hour24_, localTimeHour24); 272 if (localTimeHour24 >= DAY_TIME_LOWER_LIMIT && localTimeHour24 < DAY_TIME_UPPER_LIMIT) { 273 ASSERT_EQ(isDayTimeOfNow, true); 274 ASSERT_EQ(isDayTimeOfZone, true); 275 } else { 276 ASSERT_EQ(isDayTimeOfNow, false); 277 ASSERT_EQ(isDayTimeOfZone, false); 278 } 279 } 280 281 /** 282 * @tc.name: BaseUtilsTest008 283 * @tc.desc: Set the time zone beyond the range of [-14, 12] 284 * @tc.type: FUNC 285 */ 286 HWTEST_F(BaseUtilsTest, BaseUtilsTest008, TestSize.Level1) 287 { 288 time_t nowTime; 289 struct tm* localTime; 290 time(&nowTime); 291 localTime = gmtime(&nowTime); 292 auto theTimeOfNow = GetTimeOfNow(SET_HOURS_FOR_THE_THIRD_TIME); 293 auto theTimeOfZone = GetTimeOfZone(SET_HOURS_FOR_THE_THIRD_TIME); 294 struct timeval currentTime; 295 struct timezone timeZone; 296 gettimeofday(¤tTime, &timeZone); 297 auto getHoursWest = timeZone.tz_minuteswest / 60; 298 ASSERT_EQ(theTimeOfNow.hoursWest_, getHoursWest); 299 ASSERT_EQ(theTimeOfNow.second_, localTime->tm_sec); 300 ASSERT_EQ(theTimeOfNow.minute_, localTime->tm_min); 301 ASSERT_EQ(theTimeOfZone.hoursWest_, getHoursWest); 302 ASSERT_EQ(theTimeOfZone.second_, localTime->tm_sec); 303 ASSERT_EQ(theTimeOfZone.minute_, localTime->tm_min); 304 } 305 306 /** 307 * @tc.name: BaseUtilsTest009 308 * @tc.desc: Check the time since the device was started 309 * @tc.type: FUNC 310 */ 311 HWTEST_F(BaseUtilsTest, BaseUtilsTest009, TestSize.Level1) 312 { 313 auto microTickCount = GetMicroTickCount(); 314 auto sysTimestamp = GetSysTimestamp(); 315 int64_t microTickCountMsec, sysTimestampMsec; 316 microTickCountMsec = microTickCount / MICROSEC_TO_MILLISEC; 317 sysTimestampMsec = sysTimestamp / NANOSEC_TO_MILLISEC; 318 ASSERT_EQ(microTickCountMsec, sysTimestampMsec); 319 } 320 321 /** 322 * @tc.name: BaseUtilsTest010 323 * @tc.desc: Set ResourceConfiguration 324 * @tc.type: FUNC 325 */ 326 HWTEST_F(BaseUtilsTest, BaseUtilsTest010, TestSize.Level1) 327 { 328 ResourceConfiguration resConfiguration; 329 ASSERT_EQ(resConfiguration.TestFlag( 330 ResourceConfiguration::COLOR_MODE_UPDATED_FLAG, ResourceConfiguration::FONT_RATIO_UPDATED_FLAG), 331 false); 332 resConfiguration.SetDeviceType(DeviceType::UNKNOWN); 333 resConfiguration.SetOrientation(DeviceOrientation::ORIENTATION_UNDEFINED); 334 resConfiguration.SetDensity(CONFIGURATION_OF_DENSITY); 335 resConfiguration.SetFontRatio(CONFIGURATION_OF_FONT_RATIO); 336 resConfiguration.SetColorMode(ColorMode::COLOR_MODE_UNDEFINED); 337 resConfiguration.SetDeviceAccess(true); 338 ASSERT_EQ(resConfiguration.GetDeviceType(), DeviceType::UNKNOWN); 339 ASSERT_EQ(resConfiguration.GetOrientation(), DeviceOrientation::ORIENTATION_UNDEFINED); 340 ASSERT_EQ(resConfiguration.GetDensity(), CONFIGURATION_OF_DENSITY); 341 ASSERT_EQ(resConfiguration.GetFontRatio(), CONFIGURATION_OF_FONT_RATIO); 342 ASSERT_EQ(resConfiguration.GetColorMode(), ColorMode::COLOR_MODE_UNDEFINED); 343 ASSERT_EQ(resConfiguration.GetDeviceAccess(), true); 344 } 345 346 /** 347 * @tc.name: BaseUtilsTest011 348 * @tc.desc: Set ResourceInfo 349 * @tc.type: FUNC 350 */ 351 HWTEST_F(BaseUtilsTest, BaseUtilsTest011, TestSize.Level1) 352 { 353 ResourceInfo resourceInfo; 354 ResourceConfiguration resConfiguration; 355 resourceInfo.SetResourceConfiguration(resConfiguration); 356 resourceInfo.SetResourceHandlers(RESOURCEHANDLERS); 357 resourceInfo.SetHapPath(CONFIGURATION_PATH); 358 resourceInfo.SetPackagePath(CONFIGURATION_PATH); 359 resourceInfo.SetThemeId(NUM_OF_ID); 360 ASSERT_EQ(resourceInfo.GetResourceConfiguration().GetDeviceType(), DeviceType::PHONE); 361 ASSERT_EQ(resourceInfo.GetResourceHandlers()[0], CONTENT_OF_RESOURCEHANDLERS); 362 ASSERT_EQ(resourceInfo.GetHapPath(), CONFIGURATION_PATH); 363 ASSERT_EQ(resourceInfo.GetPackagePath(), CONFIGURATION_PATH); 364 ASSERT_EQ(resourceInfo.GetThemeId(), NUM_OF_ID); 365 } 366 367 /** 368 * @tc.name: BaseUtilsTest012 369 * @tc.desc: Json does not contain "colorMode" 370 * @tc.type: FUNC 371 */ 372 HWTEST_F(BaseUtilsTest, BaseUtilsTest012, TestSize.Level1) 373 { 374 const std::string inputTestJsonStr = "{" 375 " \"deviceType\": \"PHONE\", " 376 " \"fontScale\": 1.0 " 377 "}"; 378 ResourceConfiguration resConfiguration; 379 uint32_t updateFlags = ResourceConfiguration::COLOR_MODE_UPDATED_FLAG; 380 bool isUpdateFromJsonString = resConfiguration.UpdateFromJsonString(inputTestJsonStr, updateFlags); 381 ASSERT_EQ(isUpdateFromJsonString, true); 382 } 383 384 /** 385 * @tc.name: BaseUtilsTest013 386 * @tc.desc: Json does not contain "fontScale" 387 * @tc.type: FUNC 388 */ 389 HWTEST_F(BaseUtilsTest, BaseUtilsTest013, TestSize.Level1) 390 { 391 const std::string inputTestJsonStr = "{" 392 " \"deviceType\": \"PHONE\", " 393 " \"colorMode\": 1.0 " 394 "}"; 395 ResourceConfiguration resConfiguration; 396 uint32_t updateFlags = ResourceConfiguration::COLOR_MODE_UPDATED_FLAG; 397 bool isUpdateFromJsonString = resConfiguration.UpdateFromJsonString(inputTestJsonStr, updateFlags); 398 ASSERT_EQ(isUpdateFromJsonString, true); 399 } 400 401 /** 402 * @tc.name: BaseUtilsTest014 403 * @tc.desc: ColorMode is not "light" or "dark" 404 * @tc.type: FUNC 405 */ 406 HWTEST_F(BaseUtilsTest, BaseUtilsTest014, TestSize.Level1) 407 { 408 const std::string inputTestJsonStr = "{" 409 " \"deviceType\": \"PHONE\", " 410 " \"fontScale\": \"1.0\", " 411 " \"colorMode\": \"undefined\" " 412 "}"; 413 ResourceConfiguration resConfiguration; 414 uint32_t updateFlags = ResourceConfiguration::COLOR_MODE_UPDATED_FLAG; 415 bool isUpdateFromJsonString = resConfiguration.UpdateFromJsonString(inputTestJsonStr, updateFlags); 416 ASSERT_EQ(isUpdateFromJsonString, true); 417 } 418 419 /** 420 * @tc.name: BaseUtilsTest015 421 * @tc.desc: FontScale is not 1.0 422 * @tc.type: FUNC 423 */ 424 HWTEST_F(BaseUtilsTest, BaseUtilsTest015, TestSize.Level1) 425 { 426 const std::string inputTestJsonStr = "{" 427 " \"deviceType\": \"PHONE\", " 428 " \"fontScale\": 2.0, " 429 " \"colorMode\": \"light\" " 430 "}"; 431 ResourceConfiguration resConfiguration; 432 uint32_t updateFlags = ResourceConfiguration::COLOR_MODE_UPDATED_FLAG; 433 bool isUpdateFromJsonString = resConfiguration.UpdateFromJsonString(inputTestJsonStr, updateFlags); 434 ASSERT_EQ(isUpdateFromJsonString, true); 435 ASSERT_EQ(updateFlags, ResourceConfiguration::FONT_RATIO_UPDATED_FLAG); 436 } 437 438 /** 439 * @tc.name: BaseUtilsTest016 440 * @tc.desc: Set colorMode "dark" 441 * @tc.type: FUNC 442 */ 443 HWTEST_F(BaseUtilsTest, BaseUtilsTest016, TestSize.Level1) 444 { 445 const std::string inputTestJsonStr = "{" 446 " \"deviceType\": \"PHONE\", " 447 " \"fontScale\": 1.0, " 448 " \"colorMode\": \"dark\" " 449 "}"; 450 ResourceConfiguration resConfiguration; 451 uint32_t updateFlags = ResourceConfiguration::COLOR_MODE_UPDATED_FLAG; 452 bool isUpdateFromJsonString = resConfiguration.UpdateFromJsonString(inputTestJsonStr, updateFlags); 453 ASSERT_EQ(isUpdateFromJsonString, true); 454 ASSERT_EQ(updateFlags, ResourceConfiguration::COLOR_MODE_UPDATED_FLAG); 455 } 456 457 /** 458 * @tc.name: BaseUtilsTest017 459 * @tc.desc: Check char in bmp and whether the content of string is a number 460 * @tc.type: FUNC 461 */ 462 HWTEST_F(BaseUtilsTest, BaseUtilsTest017, TestSize.Level1) 463 { 464 const char16_t testChar16 = 0xD800; 465 const wchar_t testWchar = 0xD800; 466 ASSERT_EQ(StringUtils::NotInUtf16Bmp(testChar16), true); 467 ASSERT_EQ(StringUtils::NotInBmp(testWchar), true); 468 ASSERT_EQ(StringUtils::IsNumber(TEST_INPUT_U8_STRING_NULL), false); 469 ASSERT_EQ(StringUtils::IsNumber(TEST_INPUT_U8_STRING_NUMBER), true); 470 } 471 472 /** 473 * @tc.name: BaseUtilsTest018 474 * @tc.desc: U16string-string-wstring, string-uint32, string-int64 convert 475 * @tc.type: FUNC 476 */ 477 HWTEST_F(BaseUtilsTest, BaseUtilsTest018, TestSize.Level1) 478 { 479 ASSERT_EQ(StringUtils::Str8ToStr16(TEST_INPUT_U8_STRING), TEST_INPUT_U16_STRING); 480 ASSERT_EQ(StringUtils::Str16ToStr8(TEST_INPUT_U16_STRING), TEST_INPUT_U8_STRING); 481 ASSERT_EQ(StringUtils::ToWstring(TEST_INPUT_U8_STRING), TEST_INPUT_W_STRING); 482 ASSERT_EQ(StringUtils::ToString(TEST_INPUT_W_STRING), TEST_INPUT_U8_STRING); 483 ASSERT_EQ(StringUtils::Str8ToStr16(StringUtils::DEFAULT_STRING), StringUtils::DEFAULT_USTRING); 484 ASSERT_EQ(StringUtils::Str16ToStr8(StringUtils::DEFAULT_USTRING), StringUtils::DEFAULT_STRING); 485 ASSERT_EQ(StringUtils::ToWstring(StringUtils::DEFAULT_STRING), StringUtils::DEFAULT_WSTRING); 486 ASSERT_EQ(StringUtils::ToString(StringUtils::DEFAULT_WSTRING), StringUtils::DEFAULT_STRING); 487 ASSERT_EQ(StringUtils::DoubleToString(TEST_INPUT_DOUBLE, 0), TEST_INPUT_U8_STRING_NUMBER); 488 ASSERT_EQ(StringUtils::StringToLongInt(TEST_INPUT_U8_STRING_NUMBER), TEST_INPUT_INT64); 489 ASSERT_EQ(StringUtils::StringToUint(TEST_INPUT_U8_STRING_NUMBER), TEST_INPUT_UINT32); 490 } 491 492 /** 493 * @tc.name: BaseUtilsTest019 494 * @tc.desc: Delete all mark : "." 495 * @tc.type: FUNC 496 */ 497 HWTEST_F(BaseUtilsTest, BaseUtilsTest019, TestSize.Level1) 498 { 499 std::string markString = "THIS. IS. A. STRING"; 500 const char mark = '.'; 501 StringUtils::DeleteAllMark(markString, mark); 502 ASSERT_EQ(markString, TEST_INPUT_U8_STRING); 503 } 504 505 /** 506 * @tc.name: BaseUtilsTest020 507 * @tc.desc: Replace tab and newline with space 508 * @tc.type: FUNC 509 */ 510 HWTEST_F(BaseUtilsTest, BaseUtilsTest020, TestSize.Level1) 511 { 512 std::string inputTabAndNewLine = "THIS\rIS\nA\tSTRING"; 513 StringUtils::ReplaceTabAndNewLine(inputTabAndNewLine); 514 ASSERT_EQ(inputTabAndNewLine, TEST_INPUT_U8_STRING); 515 } 516 517 /** 518 * @tc.name: BaseUtilsTest021 519 * @tc.desc: Input a string contain "auto" 520 * @tc.type: FUNC 521 */ 522 HWTEST_F(BaseUtilsTest, BaseUtilsTest021, TestSize.Level1) 523 { 524 const std::string stringToDimensionValue = "auto"; 525 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true); 526 auto calcDim = StringUtils::StringToCalcDimension(stringToDimensionValue, true); 527 ASSERT_EQ(dim.Value(), 0); 528 ASSERT_EQ(dim.Unit(), DimensionUnit::AUTO); 529 ASSERT_EQ(calcDim.Value(), 0); 530 ASSERT_EQ(calcDim.Unit(), DimensionUnit::AUTO); 531 } 532 533 /** 534 * @tc.name: BaseUtilsTest022 535 * @tc.desc: Input a string contain "calc" 536 * @tc.type: FUNC 537 */ 538 HWTEST_F(BaseUtilsTest, BaseUtilsTest022, TestSize.Level1) 539 { 540 const std::string stringToDimensionValue = "100.0calc"; 541 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true); 542 auto calcDim = StringUtils::StringToCalcDimension(stringToDimensionValue, true); 543 ASSERT_EQ(dim.Unit(), DimensionUnit::VP); 544 ASSERT_EQ(calcDim.CalcValue(), STRING_TO_CALC_DIMENSION_RESULT); 545 ASSERT_EQ(calcDim.Unit(), DimensionUnit::CALC); 546 } 547 548 /** 549 * @tc.name: BaseUtilsTest023 550 * @tc.desc: Input a string contain "%" 551 * @tc.type: FUNC 552 */ 553 HWTEST_F(BaseUtilsTest, BaseUtilsTest023, TestSize.Level1) 554 { 555 const std::string stringToDimensionValue = "100.0%"; 556 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true); 557 ASSERT_EQ(dim.Value(), 1.0); 558 ASSERT_EQ(dim.Unit(), DimensionUnit::PERCENT); 559 } 560 561 /** 562 * @tc.name: BaseUtilsTest024 563 * @tc.desc: Input a string contain "px" 564 * @tc.type: FUNC 565 */ 566 HWTEST_F(BaseUtilsTest, BaseUtilsTest024, TestSize.Level1) 567 { 568 const std::string stringToDimensionValue = "100.0px"; 569 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true); 570 ASSERT_EQ(dim.Value(), STRING_TO_DIMENSION_RESULT); 571 ASSERT_EQ(dim.Unit(), DimensionUnit::PX); 572 } 573 574 /** 575 * @tc.name: BaseUtilsTest025 576 * @tc.desc: Input a string contain "vp" 577 * @tc.type: FUNC 578 */ 579 HWTEST_F(BaseUtilsTest, BaseUtilsTest025, TestSize.Level1) 580 { 581 const std::string stringToDimensionValue = "100.0vp"; 582 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true); 583 ASSERT_EQ(dim.Value(), STRING_TO_DIMENSION_RESULT); 584 ASSERT_EQ(dim.Unit(), DimensionUnit::VP); 585 } 586 587 /** 588 * @tc.name: BaseUtilsTest026 589 * @tc.desc: Input a string contain "fp" 590 * @tc.type: FUNC 591 */ 592 HWTEST_F(BaseUtilsTest, BaseUtilsTest026, TestSize.Level1) 593 { 594 const std::string stringToDimensionValue = "100.0fp"; 595 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true); 596 ASSERT_EQ(dim.Value(), STRING_TO_DIMENSION_RESULT); 597 ASSERT_EQ(dim.Unit(), DimensionUnit::FP); 598 } 599 600 /** 601 * @tc.name: BaseUtilsTest027 602 * @tc.desc: Input a string contain "lpx" 603 * @tc.type: FUNC 604 */ 605 HWTEST_F(BaseUtilsTest, BaseUtilsTest027, TestSize.Level1) 606 { 607 const std::string stringToDimensionValue = "100.0lpx"; 608 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true); 609 ASSERT_EQ(dim.Value(), STRING_TO_DIMENSION_RESULT); 610 ASSERT_EQ(dim.Unit(), DimensionUnit::LPX); 611 } 612 613 /** 614 * @tc.name: BaseUtilsTest028 615 * @tc.desc: Set an empty string 616 * @tc.type: FUNC 617 */ 618 HWTEST_F(BaseUtilsTest, BaseUtilsTest028, TestSize.Level1) 619 { 620 const std::string stringToDimensionValue = ""; 621 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true); 622 ASSERT_EQ(dim.Value(), 0.0); 623 ASSERT_EQ(dim.Unit(), DimensionUnit::VP); 624 } 625 626 /** 627 * @tc.name: BaseUtilsTest029 628 * @tc.desc: Set an empty string, check StringToDegree 629 * @tc.type: FUNC 630 */ 631 HWTEST_F(BaseUtilsTest, BaseUtilsTest029, TestSize.Level1) 632 { 633 const std::string stringToDegreeValue = ""; 634 auto degreeValue = StringUtils::StringToDegree(stringToDegreeValue); 635 ASSERT_EQ(degreeValue, 0.0); 636 } 637 638 /** 639 * @tc.name: BaseUtilsTest030 640 * @tc.desc: Input a string contain "deg" 641 * @tc.type: FUNC 642 */ 643 HWTEST_F(BaseUtilsTest, BaseUtilsTest030, TestSize.Level1) 644 { 645 const std::string stringToDegreeValue = "360.0deg"; 646 auto degreeValue = StringUtils::StringToDegree(stringToDegreeValue); 647 ASSERT_EQ(degreeValue, STRING_TO_DEGREE_RESULT); 648 } 649 650 /** 651 * @tc.name: BaseUtilsTest031 652 * @tc.desc: Input a string contain "gard" 653 * @tc.type: FUNC 654 */ 655 HWTEST_F(BaseUtilsTest, BaseUtilsTest031, TestSize.Level1) 656 { 657 const std::string stringToDegreeValue = "400.0grad"; 658 auto degreeValue = StringUtils::StringToDegree(stringToDegreeValue); 659 ASSERT_EQ(degreeValue, STRING_TO_DEGREE_RESULT); 660 } 661 662 /** 663 * @tc.name: BaseUtilsTest032 664 * @tc.desc: Input a string contain "rad" 665 * @tc.type: FUNC 666 */ 667 HWTEST_F(BaseUtilsTest, BaseUtilsTest032, TestSize.Level1) 668 { 669 const std::string stringToDegreeValue = "6.28318530717958647692rad"; 670 auto degreeValue = StringUtils::StringToDegree(stringToDegreeValue); 671 ASSERT_EQ(degreeValue, STRING_TO_DEGREE_RESULT); 672 } 673 674 /** 675 * @tc.name: BaseUtilsTest033 676 * @tc.desc: Input a string contain "turn" 677 * @tc.type: FUNC 678 */ 679 HWTEST_F(BaseUtilsTest, BaseUtilsTest033, TestSize.Level1) 680 { 681 const std::string stringToDegreeValue = "1turn"; 682 auto degreeValue = StringUtils::StringToDegree(stringToDegreeValue); 683 ASSERT_EQ(degreeValue, STRING_TO_DEGREE_RESULT); 684 } 685 686 /** 687 * @tc.name: BaseUtilsTest034 688 * @tc.desc: Input a string contain "-", split string to string 689 * @tc.type: FUNC 690 */ 691 HWTEST_F(BaseUtilsTest, BaseUtilsTest034, TestSize.Level1) 692 { 693 const std::string stringSplitterValue = "123456-123456"; 694 const char delimiter = '-'; 695 std::vector<std::string> out; 696 StringUtils::StringSplitter(stringSplitterValue, delimiter, out); 697 ASSERT_EQ(out[0], TEST_INPUT_U8_STRING_NUMBER); 698 } 699 700 /** 701 * @tc.name: BaseUtilsTest035 702 * @tc.desc: Input a string contain "-", split string to int32_t 703 * @tc.type: FUNC 704 */ 705 HWTEST_F(BaseUtilsTest, BaseUtilsTest035, TestSize.Level1) 706 { 707 const std::string stringSplitterValue = "123456-123456"; 708 const char delimiter = '-'; 709 std::vector<int32_t> out; 710 StringUtils::StringSplitter(stringSplitterValue, delimiter, out); 711 ASSERT_EQ(out[0], TEST_INPUT_INT32); 712 } 713 714 /** 715 * @tc.name: BaseUtilsTest036 716 * @tc.desc: Input a string contain "-", split string to double 717 * @tc.type: FUNC 718 */ 719 HWTEST_F(BaseUtilsTest, BaseUtilsTest036, TestSize.Level1) 720 { 721 const std::string stringSplitterValue = "123456-123456"; 722 const char delimiter = '-'; 723 std::vector<double> out; 724 StringUtils::StringSplitter(stringSplitterValue, delimiter, out); 725 ASSERT_EQ(out[0], TEST_INPUT_DOUBLE); 726 } 727 728 /** 729 * @tc.name: BaseUtilsTest037 730 * @tc.desc: Input a string contain "-", split string to float 731 * @tc.type: FUNC 732 */ 733 HWTEST_F(BaseUtilsTest, BaseUtilsTest037, TestSize.Level1) 734 { 735 const std::string stringSplitterValue = "123456-123456"; 736 const char delimiter = '-'; 737 std::vector<float> out; 738 StringUtils::StringSplitter(stringSplitterValue, delimiter, out); 739 ASSERT_EQ(out[0], TEST_INPUT_FLOAT); 740 } 741 742 /** 743 * @tc.name: BaseUtilsTest038 744 * @tc.desc: Input a string contain "-", split string to Dimension 745 * @tc.type: FUNC 746 */ 747 HWTEST_F(BaseUtilsTest, BaseUtilsTest038, TestSize.Level1) 748 { 749 const std::string stringSplitterValue = "123456-123456"; 750 const char delimiter = '-'; 751 std::vector<Dimension> out; 752 StringUtils::StringSplitter(stringSplitterValue, delimiter, out); 753 ASSERT_EQ(out[0].Value(), TEST_INPUT_DOUBLE); 754 ASSERT_EQ(out[0].Unit(), DimensionUnit::PX); 755 } 756 757 /** 758 * @tc.name: BaseUtilsTest039 759 * @tc.desc: No characters found for splitSepValue in splitStrValue 760 * @tc.type: FUNC 761 */ 762 HWTEST_F(BaseUtilsTest, BaseUtilsTest039, TestSize.Level1) 763 { 764 const std::string splitStrValue = "THIS IS A STRING"; 765 const std::string splitSepValue = "!?"; 766 std::vector<std::string> stringOut; 767 std::vector<Dimension> dimOut; 768 StringUtils::SplitStr(splitStrValue, splitSepValue, stringOut, true); 769 StringUtils::SplitStr(splitStrValue, splitSepValue, dimOut, true); 770 ASSERT_EQ(stringOut[0], TEST_INPUT_U8_STRING); 771 ASSERT_EQ(dimOut[0].Value(), 0); 772 ASSERT_EQ(dimOut[0].Unit(), DimensionUnit::PX); 773 } 774 775 /** 776 * @tc.name: BaseUtilsTest040 777 * @tc.desc: There is some characters found for splitSepValue in splitStrValue 778 * @tc.type: FUNC 779 */ 780 HWTEST_F(BaseUtilsTest, BaseUtilsTest040, TestSize.Level1) 781 { 782 const std::string splitStrValue = "##THIS IS A STRING***##"; 783 const std::string splitSepValue = "#*"; 784 std::vector<std::string> stringOut; 785 std::vector<Dimension> dimOut; 786 StringUtils::SplitStr(splitStrValue, splitSepValue, stringOut, true); 787 StringUtils::SplitStr(splitStrValue, splitSepValue, dimOut, true); 788 ASSERT_EQ(stringOut[0], TEST_INPUT_U8_STRING); 789 ASSERT_EQ(dimOut[0].Value(), 0); 790 ASSERT_EQ(dimOut[0].Unit(), DimensionUnit::PX); 791 } 792 793 /** 794 * @tc.name: BaseUtilsTest041 795 * @tc.desc: splitString is empty 796 * @tc.type: FUNC 797 */ 798 HWTEST_F(BaseUtilsTest, BaseUtilsTest041, TestSize.Level1) 799 { 800 const std::string splitStrValue = ""; 801 const std::string splitSepValue = ""; 802 std::vector<std::string> stringOut; 803 std::vector<Dimension> dimOut; 804 StringUtils::SplitStr(splitStrValue, splitSepValue, stringOut, true); 805 StringUtils::SplitStr(splitStrValue, splitSepValue, dimOut, true); 806 ASSERT_TRUE(stringOut.empty()); 807 ASSERT_TRUE(dimOut.empty()); 808 } 809 810 /** 811 * @tc.name: BaseUtilsTest042 812 * @tc.desc: StartWith, EndWith the same string 813 * @tc.type: FUNC 814 */ 815 HWTEST_F(BaseUtilsTest, BaseUtilsTest042, TestSize.Level1) 816 { 817 const std::string startWithValue = "THIS IS A STRING"; 818 const std::string prefixString = "THIS IS A STRING"; 819 const char* prefixChar = "THIS IS A STRING"; 820 size_t prefixLen = startWithValue.size(); 821 ASSERT_EQ(StringUtils::StartWith(startWithValue, prefixString), true); 822 ASSERT_EQ(StringUtils::StartWith(startWithValue, prefixChar, prefixLen), true); 823 ASSERT_EQ(StringUtils::EndWith(startWithValue, prefixString), true); 824 ASSERT_EQ(StringUtils::EndWith(startWithValue, prefixString), true); 825 } 826 827 /** 828 * @tc.name: BaseUtilsTest043 829 * @tc.desc: utf8 string 830 * @tc.type: FUNC 831 */ 832 HWTEST_F(BaseUtilsTest, BaseUtilsTest043, TestSize.Level1) 833 { 834 std::string utf8String = "THIS IS A UTF-8 STRING é (e-acute)."; 835 ASSERT_EQ(IsUTF8(utf8String), true); 836 } 837 838 /** 839 * @tc.name: BaseUtilsTest044 840 * @tc.desc: empty string 841 * @tc.type: FUNC 842 */ 843 HWTEST_F(BaseUtilsTest, BaseUtilsTest044, TestSize.Level1) 844 { 845 std::string emptyUtf8 = ""; 846 ASSERT_EQ(IsUTF8(emptyUtf8), false); 847 } 848 849 /** 850 * @tc.name: BaseUtilsTest045 851 * @tc.desc: singleByte string 852 * @tc.type: FUNC 853 */ 854 HWTEST_F(BaseUtilsTest, BaseUtilsTest045, TestSize.Level1) 855 { 856 std::string singleByte = "\x80"; 857 ASSERT_EQ(IsUTF8(singleByte), false); 858 } 859 860 /** 861 * @tc.name: BaseUtilsTest046 862 * @tc.desc: singleByte string 863 * @tc.type: FUNC 864 */ 865 HWTEST_F(BaseUtilsTest, BaseUtilsTest046, TestSize.Level1) 866 { 867 uint8_t data = 0x41; 868 auto result = ConvertMUtf8ToUtf16Pair(&data, 1); 869 EXPECT_EQ(result.first, 0x0041); 870 EXPECT_EQ(result.second, 1); 871 } 872 873 /** 874 * @tc.name: BaseUtilsTest047 875 * @tc.desc: twoByte string 876 * @tc.type: FUNC 877 */ 878 HWTEST_F(BaseUtilsTest, BaseUtilsTest047, TestSize.Level1) 879 { 880 uint8_t data[2] = { 0xC3, 0x81 }; 881 auto result = ConvertMUtf8ToUtf16Pair(data, 2); 882 EXPECT_EQ(result.first, 0xc1); 883 EXPECT_EQ(result.second, 2); 884 } 885 886 /** 887 * @tc.name: BaseUtilsTest048 888 * @tc.desc: threeByte string 889 * @tc.type: FUNC 890 */ 891 HWTEST_F(BaseUtilsTest, BaseUtilsTest048, TestSize.Level1) 892 { 893 uint8_t data[3] = { 0xE1, 0x82, 0x81 }; 894 auto result = ConvertMUtf8ToUtf16Pair(data, 3); 895 EXPECT_EQ(result.first, 0x1081); 896 EXPECT_EQ(result.second, 3); 897 } 898 899 /** 900 * @tc.name: BaseUtilsTest049 901 * @tc.desc:fourByte string 902 * @tc.type: FUNC 903 */ 904 HWTEST_F(BaseUtilsTest, BaseUtilsTest049, TestSize.Level1) 905 { 906 uint8_t data[4] = { 0xF1, 0x80, 0x80, 0x81 }; 907 auto result = ConvertMUtf8ToUtf16Pair(data, 4); 908 EXPECT_EQ(result.first, 0xD8C0DC01); 909 EXPECT_EQ(result.second, 4); 910 } 911 912 /** 913 * @tc.name: BaseUtilsTest050 914 * @tc.desc: empty string 915 * @tc.type: FUNC 916 */ 917 HWTEST_F(BaseUtilsTest, BaseUtilsTest050, TestSize.Level1) 918 { 919 const uint8_t* emptyStr = reinterpret_cast<const uint8_t*>(""); 920 size_t size = MUtf8ToUtf16Size(emptyStr, 0); 921 EXPECT_EQ(size, 0); 922 } 923 924 /** 925 * @tc.name: BaseUtilsTest051 926 * @tc.desc: singleByte string 927 * @tc.type: FUNC 928 */ 929 HWTEST_F(BaseUtilsTest, BaseUtilsTest051, TestSize.Level1) 930 { 931 const uint8_t* singleByteStr = reinterpret_cast<const uint8_t*>("a"); 932 size_t size = MUtf8ToUtf16Size(singleByteStr, 1); 933 EXPECT_EQ(size, 1); 934 } 935 936 /** 937 * @tc.name: BaseUtilsTest052 938 * @tc.desc: mixed string 939 * @tc.type: FUNC 940 */ 941 HWTEST_F(BaseUtilsTest, BaseUtilsTest052, TestSize.Level1) 942 { 943 const uint8_t* mixedStr = reinterpret_cast<const uint8_t*>(u8"Hello, 世界!"); 944 size_t size = MUtf8ToUtf16Size(mixedStr, strlen(reinterpret_cast<const char*>(mixedStr))); 945 EXPECT_EQ(size, 10); 946 } 947 948 /** 949 * @tc.name: BaseUtilsTest053 950 * @tc.desc: empty string 951 * @tc.type: FUNC 952 */ 953 HWTEST_F(BaseUtilsTest, BaseUtilsTest053, TestSize.Level1) 954 { 955 const uint8_t* emptyInput = nullptr; 956 uint16_t output[10] = { 0 }; 957 size_t result = ConvertRegionUtf8ToUtf16(emptyInput, output, 0, 10, 0); 958 EXPECT_EQ(result, 0); 959 } 960 961 /** 962 * @tc.name: BaseUtilsTest054 963 * @tc.desc: singleByte string 964 * @tc.type: FUNC 965 */ 966 HWTEST_F(BaseUtilsTest, BaseUtilsTest054, TestSize.Level1) 967 { 968 const uint8_t* input = reinterpret_cast<const uint8_t*>(u8"a"); 969 uint16_t output[10] = { 0 }; 970 size_t result = ConvertRegionUtf8ToUtf16(input, output, 1, 10, 0); 971 EXPECT_EQ(result, 1); 972 EXPECT_EQ(output[0], 'a'); 973 } 974 975 /** 976 * @tc.name: BaseUtilsTest055 977 * @tc.desc: mixed string 978 * @tc.type: FUNC 979 */ 980 HWTEST_F(BaseUtilsTest, BaseUtilsTest055, TestSize.Level1) 981 { 982 const uint8_t* input = reinterpret_cast<const uint8_t*>(u8"Hello, 世界!"); 983 uint16_t output[50] = { 0 }; 984 size_t result = ConvertRegionUtf8ToUtf16(input, output, strlen(reinterpret_cast<const char*>(input)), 50, 0); 985 EXPECT_EQ(result, 10); 986 } 987 988 /** 989 * @tc.name: BaseUtilsTest056 990 * @tc.desc: invalid string 991 * @tc.type: FUNC 992 */ 993 HWTEST_F(BaseUtilsTest, BaseUtilsTest056, TestSize.Level1) 994 { 995 const uint8_t* input = reinterpret_cast<const uint8_t*>(u8"XXXX"); 996 uint16_t output[10] = { 0 }; 997 size_t result = ConvertRegionUtf8ToUtf16(input, output, 4, 10, 0); 998 EXPECT_EQ(result, 4); 999 } 1000 1001 /** 1002 * @tc.name: BaseUtilsTest057 1003 * @tc.desc: null string 1004 * @tc.type: FUNC 1005 */ 1006 HWTEST_F(BaseUtilsTest, BaseUtilsTest057, TestSize.Level1) 1007 { 1008 const uint16_t* emptyInput = nullptr; 1009 uint8_t output[10] = { 0 }; 1010 size_t result = DebuggerConvertRegionUtf16ToUtf8(emptyInput, output, 0, 10, 0); 1011 EXPECT_EQ(result, 0); 1012 } 1013 1014 /** 1015 * @tc.name: BaseUtilsTest058 1016 * @tc.desc: singlebyte string 1017 * @tc.type: FUNC 1018 */ 1019 HWTEST_F(BaseUtilsTest, BaseUtilsTest058, TestSize.Level1) 1020 { 1021 const uint16_t input[] = { 0x0061 }; 1022 uint8_t output[10] = { 0 }; 1023 size_t result = DebuggerConvertRegionUtf16ToUtf8(input, output, 1, 10, 0); 1024 EXPECT_EQ(result, 1); 1025 EXPECT_EQ(output[0], 0x61); 1026 } 1027 1028 /** 1029 * @tc.name: BaseUtilsTest059 1030 * @tc.desc: twobyte string 1031 * @tc.type: FUNC 1032 */ 1033 HWTEST_F(BaseUtilsTest, BaseUtilsTest059, TestSize.Level1) 1034 { 1035 const uint16_t input[] = { 0xD800, 0xDC00 }; 1036 uint8_t output[10] = { 0 }; 1037 size_t result = DebuggerConvertRegionUtf16ToUtf8(input, output, 2, 10, 0); 1038 EXPECT_EQ(result, 4); 1039 } 1040 1041 /** 1042 * @tc.name: BaseUtilsTest060 1043 * @tc.desc: twobyte string 1044 * @tc.type: FUNC 1045 */ 1046 HWTEST_F(BaseUtilsTest, BaseUtilsTest060, TestSize.Level1) 1047 { 1048 const uint16_t input[] = { 0xD800, 0xD800 }; 1049 uint8_t output[10] = { 0 }; 1050 size_t result = DebuggerConvertRegionUtf16ToUtf8(input, output, 2, 10, 0); 1051 EXPECT_EQ(result, 6); 1052 } 1053 1054 /** 1055 * @tc.name: BaseUtilsTest061 1056 * @tc.desc: standard string 1057 * @tc.type: FUNC 1058 */ 1059 HWTEST_F(BaseUtilsTest, BaseUtilsTest061, TestSize.Level1) 1060 { 1061 std::string legalStr = "THIS IS A UTF-8 STRING é (e-acute)."; 1062 ConvertIllegalStr(legalStr); 1063 EXPECT_TRUE(IsUTF8(legalStr)); 1064 EXPECT_EQ(legalStr, "THIS IS A UTF-8 STRING é (e-acute)."); 1065 } 1066 1067 /** 1068 * @tc.name: BaseUtilsTest062 1069 * @tc.desc: valid string 1070 * @tc.type: FUNC 1071 */ 1072 HWTEST_F(BaseUtilsTest, BaseUtilsTest062, TestSize.Level1) 1073 { 1074 std::string illegalStr = "Hello, \xFF\xFE World!"; 1075 ConvertIllegalStr(illegalStr); 1076 EXPECT_TRUE(IsUTF8(illegalStr)); 1077 EXPECT_EQ(illegalStr, "Hello, World!"); 1078 } 1079 1080 /** 1081 * @tc.name: BaseUtilsTest063 1082 * @tc.desc: emptyStr string 1083 * @tc.type: FUNC 1084 */ 1085 HWTEST_F(BaseUtilsTest, BaseUtilsTest063, TestSize.Level1) 1086 { 1087 std::string emptyStr = ""; 1088 ConvertIllegalStr(emptyStr); 1089 EXPECT_FALSE(IsUTF8(emptyStr)); 1090 EXPECT_EQ(emptyStr, ""); 1091 } 1092 1093 /** 1094 * @tc.name: BaseUtilsTest064 1095 * @tc.desc: allByte string 1096 * @tc.type: FUNC 1097 */ 1098 HWTEST_F(BaseUtilsTest, BaseUtilsTest064, TestSize.Level1) 1099 { 1100 std::string allByteValues(256, 0); 1101 for (int i = 0; i < 256; ++i) { 1102 allByteValues[i] = static_cast<char>(i); 1103 } 1104 ConvertIllegalStr(allByteValues); 1105 EXPECT_TRUE(IsUTF8(allByteValues)); 1106 } 1107 1108 /** 1109 * @tc.name: BaseUtilsTest065 1110 * @tc.desc: test utf_helper.cpp: Convert u16string and string 1111 * @tc.type: FUNC 1112 */ 1113 HWTEST_F(BaseUtilsTest, BaseUtilsTest065, TestSize.Level1) 1114 { 1115 ASSERT_EQ(UtfUtils::Str16ToStr8(TEST_INPUT_U16_STRING), TEST_INPUT_U8_STRING); 1116 ASSERT_EQ(UtfUtils::Str8ToStr16(TEST_INPUT_U8_STRING), TEST_INPUT_U16_STRING); 1117 ASSERT_EQ(UtfUtils::Str16ToStr8(UtfUtils::DEFAULT_U16STR), UtfUtils::DEFAULT_STR); 1118 ASSERT_EQ(UtfUtils::Str8ToStr16(UtfUtils::DEFAULT_STR), UtfUtils::DEFAULT_U16STR); 1119 } 1120 1121 /** 1122 * @tc.name: BaseUtilsTest066 1123 * @tc.desc: test utf_helper.cpp: Convert u16string and string with debug for nornal string 1124 * @tc.type: FUNC 1125 */ 1126 HWTEST_F(BaseUtilsTest, BaseUtilsTest066, TestSize.Level1) 1127 { 1128 ASSERT_EQ(UtfUtils::Str16DebugToStr8(TEST_INPUT_U16_STRING), TEST_INPUT_U8_STRING); 1129 ASSERT_EQ(UtfUtils::Str8DebugToStr16(TEST_INPUT_U8_STRING), TEST_INPUT_U16_STRING); 1130 ASSERT_EQ(UtfUtils::Str16DebugToStr8(UtfUtils::DEFAULT_U16STR), UtfUtils::DEFAULT_STR); 1131 ASSERT_EQ(UtfUtils::Str8DebugToStr16(UtfUtils::DEFAULT_STR), UtfUtils::DEFAULT_U16STR); 1132 } 1133 1134 /** 1135 * @tc.name: BaseUtilsTest067 1136 * @tc.desc: test utf_helper.cpp: Convert u16string to string with debug for truncated emoji string 1137 * @tc.type: FUNC 1138 */ 1139 HWTEST_F(BaseUtilsTest, BaseUtilsTest067, TestSize.Level1) 1140 { 1141 std::u16string emojiStr = u"哈哈"; 1142 std::u16string subEmojiStr = emojiStr.substr(0, 3); 1143 std::string excpectSubEmojiStr = "哈哈\uFFFD"; /* \uFFFD is emoji ? */ 1144 ASSERT_EQ(UtfUtils::Str16DebugToStr8(subEmojiStr), excpectSubEmojiStr); 1145 } 1146 1147 /** 1148 * @tc.name: BaseUtilsTest068 1149 * @tc.desc: test utf_helper.cpp: Convert string to u6string with debug for truncated emoji string 1150 * @tc.type: FUNC 1151 */ 1152 HWTEST_F(BaseUtilsTest, BaseUtilsTest068, TestSize.Level1) 1153 { 1154 std::string emojiStr = "环境"; 1155 std::string subEmojiStr = emojiStr.substr(0, 9); 1156 std::u16string excpectSubEmojiStr = u"环境\uFFFD"; /* \uFFFD is emoji ? */ 1157 ASSERT_EQ(UtfUtils::Str8DebugToStr16(subEmojiStr), excpectSubEmojiStr); 1158 } 1159 1160 /** 1161 * @tc.name: BaseUtilsTest069 1162 * @tc.desc: test utf_helper.cpp: Unpaired surrogates are replace with U+FFFD 1163 * @tc.type: FUNC 1164 */ 1165 HWTEST_F(BaseUtilsTest, BaseUtilsTest069, TestSize.Level1) 1166 { 1167 std::u16string emojiStr = u"哈哈"; 1168 std::u16string subEmojiStr = emojiStr.substr(0, 3); 1169 std::u16string excpectSubEmojiStr = u"哈哈\uFFFD"; /* \uFFFD is emoji ? */ 1170 UtfUtils::HandleInvalidUTF16(reinterpret_cast<uint16_t*>(subEmojiStr.data()), subEmojiStr.length(), 0); 1171 ASSERT_EQ(subEmojiStr, excpectSubEmojiStr); 1172 } 1173 1174 /** 1175 * @tc.name: BaseUtilsTest070 1176 * @tc.desc: test utf_helper.cpp: Nothing will be changed for normal string without truncation 1177 * @tc.type: FUNC 1178 */ 1179 HWTEST_F(BaseUtilsTest, BaseUtilsTest070, TestSize.Level1) 1180 { 1181 std::u16string emojiStr = u"哈哈"; 1182 std::u16string excpectEmojiStr = emojiStr; 1183 UtfUtils::HandleInvalidUTF16(reinterpret_cast<uint16_t*>(emojiStr.data()), emojiStr.length(), 0); 1184 ASSERT_EQ(emojiStr, excpectEmojiStr); 1185 } 1186 1187 /** 1188 * @tc.name: BaseUtilsTest071 1189 * @tc.desc: invalid byte 1190 * @tc.type: FUNC 1191 */ 1192 HWTEST_F(BaseUtilsTest, BaseUtilsTest071, TestSize.Level1) 1193 { 1194 uint8_t invalidByte[1] = { 0x80 }; 1195 size_t size = MUtf8ToUtf16Size(invalidByte, 1); 1196 EXPECT_EQ(size, 1); 1197 } 1198 1199 /** 1200 * @tc.name: BaseUtilsTest072 1201 * @tc.desc: HandlesSupplementaryPairs 1202 * @tc.type: FUNC 1203 */ 1204 HWTEST_F(BaseUtilsTest, BaseUtilsTest072, TestSize.Level1) 1205 { 1206 uint8_t mutf8[] = { 0xED, 0xA0, 0xBC, 0xED, 0xB7, 0x84 }; 1207 size_t mutf8Len = sizeof(mutf8) / sizeof(mutf8[0]); 1208 size_t utf16Size = MUtf8ToUtf16Size(mutf8, mutf8Len); 1209 EXPECT_EQ(utf16Size, 2); 1210 } 1211 1212 /** 1213 * @tc.name: BaseUtilsTest073 1214 * @tc.desc: SingleByte String 1215 * @tc.type: FUNC 1216 */ 1217 HWTEST_F(BaseUtilsTest, BaseUtilsTest073, TestSize.Level1) 1218 { 1219 uint8_t data[1] = { 0x80 }; 1220 auto result = ConvertMUtf8ToUtf16Pair(data, 1); 1221 EXPECT_EQ(result.first, 0x80); 1222 EXPECT_EQ(result.second, 1); 1223 } 1224 1225 /** 1226 * @tc.name: BaseUtilsTest074 1227 * @tc.desc: 2Byte String 1228 * @tc.type: FUNC 1229 */ 1230 HWTEST_F(BaseUtilsTest, BaseUtilsTest074, TestSize.Level1) 1231 { 1232 uint8_t data[2] = { 0xE0, 0x80 }; 1233 auto result = ConvertMUtf8ToUtf16Pair(data, 2); 1234 EXPECT_EQ(result.first, 0xE0); 1235 EXPECT_EQ(result.second, 1); 1236 } 1237 1238 /** 1239 * @tc.name: BaseUtilsTest075 1240 * @tc.desc: 3Byte String 1241 * @tc.type: FUNC 1242 */ 1243 HWTEST_F(BaseUtilsTest, BaseUtilsTest075, TestSize.Level1) 1244 { 1245 uint8_t data[3] = { 0xF0, 0x80, 0x80 }; 1246 auto result = ConvertMUtf8ToUtf16Pair(data, 3); 1247 EXPECT_EQ(result.first, 0xF0); 1248 EXPECT_EQ(result.second, 1); 1249 } 1250 1251 /** 1252 * @tc.name: BaseUtilsTest076 1253 * @tc.desc: 4Byte String 1254 * @tc.type: FUNC 1255 */ 1256 HWTEST_F(BaseUtilsTest, BaseUtilsTest076, TestSize.Level1) 1257 { 1258 uint8_t mutf8[] = { 0xF0, 0x9D, 0x84, 0x9E }; 1259 auto [codePoint, nbytes] = ConvertMUtf8ToUtf16Pair(mutf8, 4); 1260 EXPECT_TRUE(codePoint > 0xFFFF); 1261 EXPECT_EQ(nbytes, 4); 1262 } 1263 1264 /** 1265 * @tc.name: StringExpressionTest001 1266 * @tc.desc: InitMapping() 1267 * @tc.type: FUNC 1268 */ 1269 HWTEST_F(BaseUtilsTest, StringExpressionTest001, TestSize.Level1) 1270 { 1271 std::map<std::string, int> mapping; 1272 StringExpression::InitMapping(mapping); 1273 1274 EXPECT_EQ(mapping.find("+")->second, 0); 1275 EXPECT_EQ(mapping.find("-")->second, 0); 1276 EXPECT_EQ(mapping.find("*")->second, 1); 1277 EXPECT_EQ(mapping.find("/")->second, 1); 1278 EXPECT_EQ(mapping.find("(")->second, 2); 1279 EXPECT_EQ(mapping.find(")")->second, 2); 1280 } 1281 1282 /** 1283 * @tc.name: StringExpressionTest002 1284 * @tc.desc: CheckCalcIsValid() 1285 * @tc.type: FUNC 1286 */ 1287 HWTEST_F(BaseUtilsTest, StringExpressionTest002, TestSize.Level1) 1288 { 1289 std::string formula; 1290 EXPECT_TRUE(StringExpression::CheckCalcIsValid(formula)); 1291 formula = "+calc(1,1)-calc(1,1)"; 1292 EXPECT_FALSE(StringExpression::CheckCalcIsValid(formula)); 1293 formula = "(calc{1,1}+calc{1,1}-calc{1,1})"; 1294 EXPECT_TRUE(StringExpression::CheckCalcIsValid(formula)); 1295 } 1296 1297 /** 1298 * @tc.name: StringExpressionTest003 1299 * @tc.desc: CheckCalcIsValid() 1300 * @tc.type: FUNC 1301 */ 1302 HWTEST_F(BaseUtilsTest, StringExpressionTest003, TestSize.Level1) 1303 { 1304 std::string formula; 1305 StringExpression::ReplaceSignNumber(formula); 1306 EXPECT_EQ(formula, ""); 1307 formula = "+10-10+1.0+2.0+5-1.0"; 1308 StringExpression::ReplaceSignNumber(formula); 1309 EXPECT_EQ(formula, " (0 + 10) (0 - 10) (0 + 1.0) (0 + 2.0) (0 + 5) (0 - 1.0)"); 1310 } 1311 1312 /** 1313 * @tc.name: StringExpressionTest004 1314 * @tc.desc: PushOpStack() 1315 * @tc.type: FUNC 1316 */ 1317 HWTEST_F(BaseUtilsTest, StringExpressionTest004, TestSize.Level1) 1318 { 1319 std::vector<std::string> result; 1320 std::vector<std::string> opStack; 1321 std::string curNum; 1322 std::string formula = "2 * 3 - (2 + 3) / 5 + 6 / 2"; 1323 EXPECT_TRUE(StringExpression::PushOpStack(formula, curNum, result, opStack)); 1324 EXPECT_TRUE(!result.empty()); 1325 EXPECT_TRUE(!opStack.empty()); 1326 EXPECT_TRUE(!curNum.empty()); 1327 } 1328 1329 /** 1330 * @tc.name: StringExpressionTest005 1331 * @tc.desc: CalculateFourOperationsExp() 1332 * @tc.type: FUNC 1333 */ 1334 HWTEST_F(BaseUtilsTest, StringExpressionTest005, TestSize.Level1) 1335 { 1336 double opRes = 0.0; 1337 Dimension num1 = 10.0_px; 1338 Dimension num2 = 5.0_px; 1339 std::string formula = "+"; 1340 EXPECT_TRUE(StringExpression::CalculateFourOperationsExp( __anonf6e886ea0502(const Dimension& dim) 1341 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1342 EXPECT_EQ(opRes, 15.0); 1343 num1.SetUnit(DimensionUnit::NONE); 1344 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anonf6e886ea0602(const Dimension& dim) 1345 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1346 num1.SetUnit(DimensionUnit::PX); 1347 num2.SetUnit(DimensionUnit::NONE); 1348 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anonf6e886ea0702(const Dimension& dim) 1349 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1350 1351 formula = "-"; 1352 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anonf6e886ea0802(const Dimension& dim) 1353 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1354 num2.SetUnit(DimensionUnit::PX); 1355 num1.SetUnit(DimensionUnit::NONE); 1356 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anonf6e886ea0902(const Dimension& dim) 1357 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1358 num1.SetUnit(DimensionUnit::PX); 1359 EXPECT_TRUE(StringExpression::CalculateFourOperationsExp( __anonf6e886ea0a02(const Dimension& dim) 1360 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1361 EXPECT_EQ(opRes, -5.0); 1362 1363 formula = "*"; 1364 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anonf6e886ea0b02(const Dimension& dim) 1365 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1366 num1.SetUnit(DimensionUnit::NONE); 1367 num2.SetUnit(DimensionUnit::PX); 1368 EXPECT_TRUE(StringExpression::CalculateFourOperationsExp( __anonf6e886ea0c02(const Dimension& dim) 1369 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1370 num1.SetUnit(DimensionUnit::PX); 1371 num2.SetUnit(DimensionUnit::NONE); 1372 EXPECT_TRUE(StringExpression::CalculateFourOperationsExp( __anonf6e886ea0d02(const Dimension& dim) 1373 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1374 EXPECT_EQ(opRes, 50.0); 1375 1376 formula = "/"; 1377 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anonf6e886ea0e02(const Dimension& dim) 1378 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1379 num1.SetUnit(DimensionUnit::NONE); 1380 EXPECT_TRUE(StringExpression::CalculateFourOperationsExp( __anonf6e886ea0f02(const Dimension& dim) 1381 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1382 num1.SetValue(0.0); 1383 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anonf6e886ea1002(const Dimension& dim) 1384 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1385 EXPECT_EQ(opRes, 0.5); 1386 } 1387 1388 /** 1389 * @tc.name: StringExpressionTest006 1390 * @tc.desc: CalculateExpImpl() 1391 * @tc.type: FUNC 1392 */ 1393 HWTEST_F(BaseUtilsTest, StringExpressionTest006, TestSize.Level1) 1394 { 1395 std::vector<std::string> rpnexp = { "2", "3", "*", "2", "3", "+", "5", "6", "/" }; 1396 std::vector<Dimension> result; 1397 double opRes = 0.0; 1398 EXPECT_TRUE(StringExpression::CalculateExpImpl( __anonf6e886ea1102(const Dimension& dim) 1399 rpnexp, [](const Dimension& dim) -> double { return dim.Value(); }, result, opRes)); 1400 rpnexp = { "2_invalid", "*", "3" }; 1401 EXPECT_FALSE(StringExpression::CalculateExpImpl( __anonf6e886ea1202(const Dimension& dim) 1402 rpnexp, [](const Dimension& dim) -> double { return dim.Value(); }, result, opRes)); 1403 result.clear(); 1404 rpnexp = { "2", "*", "3", "-", "(", "2", "3", "+", "5", "6", "/" }; 1405 EXPECT_FALSE(StringExpression::CalculateExpImpl( __anonf6e886ea1302(const Dimension& dim) 1406 rpnexp, [](const Dimension& dim) -> double { return dim.Value(); }, result, opRes)); 1407 } 1408 1409 /** 1410 * @tc.name: StringExpressionTest007 1411 * @tc.desc: ConvertDal2Rpn: ReplaceSignNumberWithUnit()/FilterCalcSpecialString 1412 * @tc.type: FUNC 1413 */ 1414 HWTEST_F(BaseUtilsTest, StringExpressionTest007, TestSize.Level1) 1415 { 1416 // replace sign number with unit with formula == "" 1417 std::string formula = ""; 1418 std::vector<std::string> ret = StringExpression::ConvertDal2Rpn(formula); 1419 EXPECT_EQ(formula, ""); 1420 EXPECT_EQ(ret.size(), 0); 1421 1422 // replace sign number with unit normal case 1423 formula = "+1.1px"; 1424 std::vector<std::string> ret2 = StringExpression::ConvertDal2Rpn(formula); 1425 EXPECT_EQ(ret2.size(), 0); 1426 1427 formula = "calc(2 * 3 - (2 + 3) / 5 + 6 / 2 + (1 + 2))"; 1428 std::vector<std::string> ret3 = StringExpression::ConvertDal2Rpn(formula); 1429 EXPECT_EQ(ret3.size(), 17); 1430 } 1431 1432 /** 1433 * @tc.name: DateUtilsTest001 1434 * @tc.desc: GetMilliSecondsByDateTime 1435 * @tc.type: FUNC 1436 */ 1437 HWTEST_F(BaseUtilsTest, DateUtilsTest001, TestSize.Level1) 1438 { 1439 std::tm dateTime; 1440 Date::GetMilliSecondsByDateTime(dateTime); 1441 EXPECT_EQ(dateTime.tm_year != 0, true); 1442 EXPECT_EQ(dateTime.tm_mday != 0, true); 1443 dateTime.tm_mday = 5; 1444 Date::GetMilliSecondsByDateTime(dateTime); 1445 EXPECT_EQ(dateTime.tm_year != 0, true); 1446 EXPECT_EQ(dateTime.tm_mday == 5, true); 1447 dateTime.tm_year = 8; 1448 Date::GetMilliSecondsByDateTime(dateTime); 1449 EXPECT_EQ(dateTime.tm_year == 8, true); 1450 EXPECT_EQ(dateTime.tm_mday == 5, true); 1451 } 1452 1453 /** 1454 * @tc.name: StringUtilsTest001 1455 * @tc.desc: IsAscii 1456 * @tc.type: FUNC 1457 */ 1458 HWTEST_F(BaseUtilsTest, StringUtilsTest001, TestSize.Level1) 1459 { 1460 std::string str = "abcde"; 1461 bool ret = StringUtils::IsAscii(str); 1462 EXPECT_EQ(ret, true); 1463 str = "中文"; 1464 bool ret2 = StringUtils::IsAscii(str); 1465 EXPECT_EQ(ret2, false); 1466 } 1467 1468 /** 1469 * @tc.name: TimeUtilsTest001 1470 * @tc.desc: ConvertTimestampToStr 1471 * @tc.type: FUNC 1472 */ 1473 HWTEST_F(BaseUtilsTest, TimeUtilsTest001, TestSize.Level1) 1474 { 1475 int64_t timestamp = 1626211200; 1476 std::string ret = ConvertTimestampToStr(timestamp); 1477 EXPECT_EQ(ret, "1970-01-20 03:43:31.200"); 1478 } 1479 1480 /** 1481 * @tc.name: UtfConversionTest001 1482 * @tc.desc: Convert a simple ASCII string to UTF-16. 1483 * @tc.type: FUNC 1484 */ 1485 HWTEST_F(BaseUtilsTest, UtfConversionTest001, TestSize.Level1) 1486 { 1487 const std::string asciiStr = "Hello World"; 1488 const size_t utf8Len = asciiStr.size(); 1489 const size_t utf16Len = utf8Len + 1; 1490 std::vector<uint8_t> utf8In(asciiStr.begin(), asciiStr.end()); 1491 std::vector<uint16_t> utf16Out(utf16Len); 1492 1493 size_t written = ConvertRegionUtf8ToUtf16( 1494 utf8In.data(), utf16Out.data(), utf8Len, utf16Len, 0); 1495 1496 std::u16string expectedStr = u"Hello World"; 1497 std::u16string actualStr(utf16Out.begin(), utf16Out.begin() + written); 1498 ASSERT_EQ(actualStr, expectedStr); 1499 ASSERT_EQ(written, expectedStr.size()); 1500 } 1501 1502 /** 1503 * @tc.name: UtfConversionTest002 1504 * @tc.desc: Convert a string with non-ASCII characters to UTF-16. 1505 * @tc.type: FUNC 1506 */ 1507 HWTEST_F(BaseUtilsTest, UtfConversionTest002, TestSize.Level1) 1508 { 1509 const std::string nonAsciiStr = "こんにちは世界"; 1510 const size_t utf8Len = nonAsciiStr.size(); 1511 const size_t utf16Len = utf8Len + 1; 1512 std::vector<uint8_t> utf8In(nonAsciiStr.begin(), nonAsciiStr.end()); 1513 std::vector<uint16_t> utf16Out(utf16Len); 1514 1515 size_t written = ConvertRegionUtf8ToUtf16( 1516 utf8In.data(), utf16Out.data(), utf8Len, utf16Len, 0); 1517 1518 std::u16string expectedStr = u"こんにちは世界"; 1519 std::u16string actualStr(utf16Out.begin(), utf16Out.begin() + written); 1520 1521 ASSERT_EQ(actualStr, expectedStr); 1522 ASSERT_EQ(written, expectedStr.size()); 1523 } 1524 1525 /** 1526 * @tc.name: UtfConversionTest003 1527 * @tc.desc: Convert a string with a mix of ASCII and non-ASCII characters to UTF-16. 1528 * @tc.type: FUNC 1529 */ 1530 HWTEST_F(BaseUtilsTest, UtfConversionTest003, TestSize.Level1) 1531 { 1532 const std::string mixedStr = "Hello, 世界!"; 1533 const size_t utf8Len = mixedStr.size(); 1534 const size_t utf16Len = utf8Len + 1; 1535 std::vector<uint8_t> utf8In(mixedStr.begin(), mixedStr.end()); 1536 std::vector<uint16_t> utf16Out(utf16Len); 1537 1538 size_t written = ConvertRegionUtf8ToUtf16( 1539 utf8In.data(), utf16Out.data(), utf8Len, utf16Len, 0); 1540 1541 std::u16string expectedStr = u"Hello, 世界!"; 1542 std::u16string actualStr(utf16Out.begin(), utf16Out.begin() + written); 1543 1544 ASSERT_EQ(actualStr, expectedStr); 1545 ASSERT_EQ(written, expectedStr.size()); 1546 } 1547 1548 /** 1549 * @tc.name: UtfConversionTest004 1550 * @tc.desc: Convert a string with a 4-byte UTF-8 character to UTF-16. 1551 * @tc.type: FUNC 1552 */ 1553 HWTEST_F(BaseUtilsTest, UtfConversionTest004, TestSize.Level1) 1554 { 1555 const std::string fourByteStr = ""; 1556 const size_t utf8Len = fourByteStr.size(); 1557 const size_t utf16Len = utf8Len * 2; 1558 std::vector<uint8_t> utf8In(fourByteStr.begin(), fourByteStr.end()); 1559 std::vector<uint16_t> utf16Out(utf16Len); 1560 1561 size_t written = ConvertRegionUtf8ToUtf16( 1562 utf8In.data(), utf16Out.data(), utf8Len, utf16Len, 0); 1563 1564 std::u16string expectedStr = u""; 1565 std::u16string actualStr(utf16Out.begin(), utf16Out.begin() + written); 1566 1567 ASSERT_EQ(actualStr, expectedStr); 1568 ASSERT_EQ(written, expectedStr.size()); 1569 } 1570 1571 /** 1572 * @tc.name: UtfConversionTest005 1573 * @tc.desc: Test conversion with a string that requires truncation. 1574 * @tc.type: FUNC 1575 */ 1576 HWTEST_F(BaseUtilsTest, UtfConversionTest005, TestSize.Level1) 1577 { 1578 const std::string fullStr = "This string will be truncated"; 1579 const size_t startOffset = 10; 1580 const size_t utf8Len = fullStr.size() - startOffset; 1581 const size_t utf16Len = utf8Len + 1; 1582 1583 std::vector<uint8_t> utf8In(fullStr.begin(), fullStr.end()); 1584 std::vector<uint16_t> utf16Out(utf16Len); 1585 1586 size_t written = ConvertRegionUtf8ToUtf16( 1587 utf8In.data() + startOffset, utf16Out.data(), utf8Len, utf16Len, 0); 1588 1589 std::u16string expectedStr = u"g will be truncated"; 1590 std::u16string actualStr(utf16Out.begin(), utf16Out.begin() + written); 1591 1592 ASSERT_EQ(actualStr, expectedStr); 1593 ASSERT_EQ(written, expectedStr.size()); 1594 } 1595 1596 /** 1597 * @tc.name: UtfConversionTest007 1598 * @tc.desc: Test conversion with a start offset. 1599 * @tc.type: FUNC 1600 */ 1601 HWTEST_F(BaseUtilsTest, UtfConversionTest007, TestSize.Level1) 1602 { 1603 const std::string offsetStr = "Start offset test string"; 1604 const size_t utf8Len = offsetStr.size(); 1605 const size_t utf16Len = utf8Len * 2; 1606 1607 std::vector<uint8_t> utf8In(offsetStr.begin(), offsetStr.end()); 1608 std::vector<uint16_t> utf16Out(utf16Len); 1609 const size_t startOffset = 10; 1610 1611 size_t written = ConvertRegionUtf8ToUtf16( 1612 utf8In.data(), utf16Out.data(), utf8Len, utf16Len, startOffset); 1613 1614 std::u16string expectedStr = u"et test string"; 1615 std::u16string actualStr(utf16Out.begin(), utf16Out.begin() + written); 1616 1617 ASSERT_EQ(actualStr, expectedStr); 1618 ASSERT_EQ(written, expectedStr.size()); 1619 } 1620 } // namespace OHOS::Ace 1621