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 = __anon2f39384c0202(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 = __anon2f39384c0302(const Dimension& dim) 177 StringExpression::CalculateExp(FORMULA_TWO, [](const Dimension& dim) -> double { return dim.Value(); }); 178 auto errResultThree = __anon2f39384c0402(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: BaseUtilsTest077 1266 * @tc.desc: Test FormatString with basic format strings 1267 * @tc.type: FUNC 1268 */ 1269 HWTEST_F(BaseUtilsTest, BaseUtilsTest077, TestSize.Level1) 1270 { 1271 EXPECT_EQ(StringUtils::FormatString("Hello %s", "world"), "Hello world"); 1272 EXPECT_EQ(StringUtils::FormatString("%d", 42), "42"); 1273 EXPECT_EQ(StringUtils::FormatString("%.2f", 3.14159), "3.14"); 1274 EXPECT_EQ(StringUtils::FormatString("%p", reinterpret_cast<void*>(42)), "0x2a"); 1275 } 1276 1277 /** 1278 * @tc.name: BaseUtilsTest078 1279 * @tc.desc: Test FormatString with no arguments 1280 * @tc.type: FUNC 1281 */ 1282 HWTEST_F(BaseUtilsTest, BaseUtilsTest078, TestSize.Level1) 1283 { 1284 EXPECT_EQ(StringUtils::FormatString("Hello"), "Hello"); 1285 EXPECT_EQ(StringUtils::FormatString("No vars"), "No vars"); 1286 } 1287 1288 /** 1289 * @tc.name: BaseUtilsTest079 1290 * @tc.desc: Test FormatString with max size boundary 1291 * @tc.type: FUNC 1292 */ 1293 HWTEST_F(BaseUtilsTest, BaseUtilsTest079, TestSize.Level1) 1294 { 1295 std::string longStr(MAX_STRING_SIZE - 1, 'a'); 1296 std::string result = StringUtils::FormatString("%s", longStr.c_str()); 1297 EXPECT_EQ(result, longStr); 1298 } 1299 1300 /** 1301 * @tc.name: BaseUtilsTest080 1302 * @tc.desc: Test FormatString exceeding max size 1303 * @tc.type: FUNC 1304 */ 1305 HWTEST_F(BaseUtilsTest, BaseUtilsTest080, TestSize.Level1) 1306 { 1307 std::string input(300, 'B'); 1308 std::string result = StringUtils::FormatString("%s", input.c_str()); 1309 EXPECT_TRUE(result.empty()); 1310 } 1311 1312 /** 1313 * @tc.name: BaseUtilsTest081 1314 * @tc.desc: Test FormatString with invalid format specifier 1315 * @tc.type: FUNC 1316 */ 1317 HWTEST_F(BaseUtilsTest, BaseUtilsTest081, TestSize.Level1) 1318 { 1319 EXPECT_TRUE(StringUtils::FormatString("%q").empty()); 1320 EXPECT_TRUE(StringUtils::FormatString("%z", 123).empty()); 1321 EXPECT_TRUE(StringUtils::FormatString("%", 123).empty()); 1322 } 1323 1324 /** 1325 * @tc.name: BaseUtilsTest082 1326 * @tc.desc: Test FormatString with escape characters 1327 * @tc.type: FUNC 1328 */ 1329 HWTEST_F(BaseUtilsTest, BaseUtilsTest082, TestSize.Level1) 1330 { 1331 EXPECT_EQ(StringUtils::FormatString("Hello\tworld\n"), "Hello\tworld\n"); 1332 EXPECT_EQ(StringUtils::FormatString("%c", '\0'), "\0"); 1333 } 1334 1335 /** 1336 * @tc.name: BaseUtilsTest083 1337 * @tc.desc: Test FormatString with multiple arguments 1338 * @tc.type: FUNC 1339 */ 1340 HWTEST_F(BaseUtilsTest, BaseUtilsTest083, TestSize.Level1) 1341 { 1342 std::string result = StringUtils::FormatString("%d + %f = %s", 2, 3.0, "5"); 1343 EXPECT_EQ(result, "2 + 3.000000 = 5"); 1344 } 1345 1346 /** 1347 * @tc.name: BaseUtilsTest084 1348 * @tc.desc: Test FormatString with empty format 1349 * @tc.type: FUNC 1350 */ 1351 HWTEST_F(BaseUtilsTest, BaseUtilsTest084, TestSize.Level1) 1352 { 1353 std::string result = StringUtils::FormatString("%999s", "test"); 1354 EXPECT_TRUE(result.empty()); 1355 } 1356 1357 /** 1358 * @tc.name: BaseUtilsTest085 1359 * @tc.desc: Test FormatString with pointer formatting 1360 * @tc.type: FUNC 1361 */ 1362 HWTEST_F(BaseUtilsTest, BaseUtilsTest085, TestSize.Level1) 1363 { 1364 int x = 42; 1365 std::string result = StringUtils::FormatString("%p", &x); 1366 EXPECT_FALSE(result.empty()); 1367 EXPECT_TRUE(result.length() > 2); 1368 EXPECT_EQ(result.c_str()[0], '0'); 1369 EXPECT_EQ(result.c_str()[1], 'x'); 1370 } 1371 1372 /** 1373 * @tc.name: BaseUtilsTest086 1374 * @tc.desc: Test FormatString with mixed types 1375 * @tc.type: FUNC 1376 */ 1377 HWTEST_F(BaseUtilsTest, BaseUtilsTest086, TestSize.Level1) 1378 { 1379 std::string result = StringUtils::FormatString("%d %f %s %c", 123, 45.67, "test", 'K'); 1380 EXPECT_EQ(result, "123 45.670000 test K"); 1381 } 1382 1383 /** 1384 * @tc.name: BaseUtilsTest087 1385 * @tc.desc: Test FormatString with special characters 1386 * @tc.type: FUNC 1387 */ 1388 HWTEST_F(BaseUtilsTest, BaseUtilsTest087, TestSize.Level1) 1389 { 1390 std::string result = StringUtils::FormatString("100%%"); 1391 EXPECT_EQ(result, "100%"); 1392 } 1393 1394 /** 1395 * @tc.name: BaseUtilsTest088 1396 * @tc.desc: Test FormatString with float formatting 1397 * @tc.type: FUNC 1398 */ 1399 HWTEST_F(BaseUtilsTest, BaseUtilsTest088, TestSize.Level1) 1400 { 1401 std::string result = StringUtils::FormatString("%.2f", 3.1415); 1402 EXPECT_EQ(result, "3.14"); 1403 } 1404 1405 /** 1406 * @tc.name: BaseUtilsTest089 1407 * @tc.desc: Test FormatString with integer padding 1408 * @tc.type: FUNC 1409 */ 1410 HWTEST_F(BaseUtilsTest, BaseUtilsTest089, TestSize.Level1) 1411 { 1412 std::string expected(255, '0'); 1413 std::string result = StringUtils::FormatString("%0255d", 0); 1414 EXPECT_EQ(result, expected); 1415 } 1416 1417 /** 1418 * @tc.name: BaseUtilsTest090 1419 * @tc.desc: Check string is a float 1420 * @tc.type: FUNC 1421 */ 1422 HWTEST_F(BaseUtilsTest, BaseUtilsTest090, TestSize.Level1) 1423 { 1424 std::string testStr = "0.125"; 1425 ASSERT_EQ(StringUtils::IsFloat(testStr), true); 1426 testStr = "0.0.1"; 1427 ASSERT_EQ(StringUtils::IsFloat(testStr), false); 1428 testStr = "0.A5"; 1429 ASSERT_EQ(StringUtils::IsFloat(testStr), false); 1430 testStr = "0.%5"; 1431 ASSERT_EQ(StringUtils::IsFloat(testStr), false); 1432 testStr = ".123"; 1433 ASSERT_EQ(StringUtils::IsFloat(testStr), true); 1434 testStr = "123."; 1435 ASSERT_EQ(StringUtils::IsFloat(testStr), true); 1436 } 1437 1438 /** 1439 * @tc.name: StringExpressionTest001 1440 * @tc.desc: InitMapping() 1441 * @tc.type: FUNC 1442 */ 1443 HWTEST_F(BaseUtilsTest, StringExpressionTest001, TestSize.Level1) 1444 { 1445 std::map<std::string, int> mapping; 1446 StringExpression::InitMapping(mapping); 1447 1448 EXPECT_EQ(mapping.find("+")->second, 0); 1449 EXPECT_EQ(mapping.find("-")->second, 0); 1450 EXPECT_EQ(mapping.find("*")->second, 1); 1451 EXPECT_EQ(mapping.find("/")->second, 1); 1452 EXPECT_EQ(mapping.find("(")->second, 2); 1453 EXPECT_EQ(mapping.find(")")->second, 2); 1454 } 1455 1456 /** 1457 * @tc.name: StringExpressionTest002 1458 * @tc.desc: CheckCalcIsValid() 1459 * @tc.type: FUNC 1460 */ 1461 HWTEST_F(BaseUtilsTest, StringExpressionTest002, TestSize.Level1) 1462 { 1463 std::string formula; 1464 EXPECT_TRUE(StringExpression::CheckCalcIsValid(formula)); 1465 formula = "+calc(1,1)-calc(1,1)"; 1466 EXPECT_FALSE(StringExpression::CheckCalcIsValid(formula)); 1467 formula = "(calc{1,1}+calc{1,1}-calc{1,1})"; 1468 EXPECT_TRUE(StringExpression::CheckCalcIsValid(formula)); 1469 } 1470 1471 /** 1472 * @tc.name: StringExpressionTest003 1473 * @tc.desc: CheckCalcIsValid() 1474 * @tc.type: FUNC 1475 */ 1476 HWTEST_F(BaseUtilsTest, StringExpressionTest003, TestSize.Level1) 1477 { 1478 std::string formula; 1479 StringExpression::ReplaceSignNumber(formula); 1480 EXPECT_EQ(formula, ""); 1481 formula = "+10-10+1.0+2.0+5-1.0"; 1482 StringExpression::ReplaceSignNumber(formula); 1483 EXPECT_EQ(formula, " (0 + 10) (0 - 10) (0 + 1.0) (0 + 2.0) (0 + 5) (0 - 1.0)"); 1484 } 1485 1486 /** 1487 * @tc.name: StringExpressionTest004 1488 * @tc.desc: PushOpStack() 1489 * @tc.type: FUNC 1490 */ 1491 HWTEST_F(BaseUtilsTest, StringExpressionTest004, TestSize.Level1) 1492 { 1493 std::vector<std::string> result; 1494 std::vector<std::string> opStack; 1495 std::string curNum; 1496 std::string formula = "2 * 3 - (2 + 3) / 5 + 6 / 2"; 1497 EXPECT_TRUE(StringExpression::PushOpStack(formula, curNum, result, opStack)); 1498 EXPECT_TRUE(!result.empty()); 1499 EXPECT_TRUE(!opStack.empty()); 1500 EXPECT_TRUE(!curNum.empty()); 1501 } 1502 1503 /** 1504 * @tc.name: StringExpressionTest005 1505 * @tc.desc: CalculateFourOperationsExp() 1506 * @tc.type: FUNC 1507 */ 1508 HWTEST_F(BaseUtilsTest, StringExpressionTest005, TestSize.Level1) 1509 { 1510 double opRes = 0.0; 1511 Dimension num1 = 10.0_px; 1512 Dimension num2 = 5.0_px; 1513 std::string formula = "+"; 1514 EXPECT_TRUE(StringExpression::CalculateFourOperationsExp( __anon2f39384c0502(const Dimension& dim) 1515 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1516 EXPECT_EQ(opRes, 15.0); 1517 num1.SetUnit(DimensionUnit::NONE); 1518 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anon2f39384c0602(const Dimension& dim) 1519 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1520 num1.SetUnit(DimensionUnit::PX); 1521 num2.SetUnit(DimensionUnit::NONE); 1522 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anon2f39384c0702(const Dimension& dim) 1523 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1524 1525 formula = "-"; 1526 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anon2f39384c0802(const Dimension& dim) 1527 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1528 num2.SetUnit(DimensionUnit::PX); 1529 num1.SetUnit(DimensionUnit::NONE); 1530 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anon2f39384c0902(const Dimension& dim) 1531 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1532 num1.SetUnit(DimensionUnit::PX); 1533 EXPECT_TRUE(StringExpression::CalculateFourOperationsExp( __anon2f39384c0a02(const Dimension& dim) 1534 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1535 EXPECT_EQ(opRes, -5.0); 1536 1537 formula = "*"; 1538 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anon2f39384c0b02(const Dimension& dim) 1539 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1540 num1.SetUnit(DimensionUnit::NONE); 1541 num2.SetUnit(DimensionUnit::PX); 1542 EXPECT_TRUE(StringExpression::CalculateFourOperationsExp( __anon2f39384c0c02(const Dimension& dim) 1543 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1544 num1.SetUnit(DimensionUnit::PX); 1545 num2.SetUnit(DimensionUnit::NONE); 1546 EXPECT_TRUE(StringExpression::CalculateFourOperationsExp( __anon2f39384c0d02(const Dimension& dim) 1547 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1548 EXPECT_EQ(opRes, 50.0); 1549 1550 formula = "/"; 1551 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anon2f39384c0e02(const Dimension& dim) 1552 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1553 num1.SetUnit(DimensionUnit::NONE); 1554 EXPECT_TRUE(StringExpression::CalculateFourOperationsExp( __anon2f39384c0f02(const Dimension& dim) 1555 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1556 num1.SetValue(0.0); 1557 EXPECT_FALSE(StringExpression::CalculateFourOperationsExp( __anon2f39384c1002(const Dimension& dim) 1558 formula, num1, num2, [](const Dimension& dim) -> double { return dim.Value(); }, opRes)); 1559 EXPECT_EQ(opRes, 0.5); 1560 } 1561 1562 /** 1563 * @tc.name: StringExpressionTest006 1564 * @tc.desc: CalculateExpImpl() 1565 * @tc.type: FUNC 1566 */ 1567 HWTEST_F(BaseUtilsTest, StringExpressionTest006, TestSize.Level1) 1568 { 1569 std::vector<std::string> rpnexp = { "2", "3", "*", "2", "3", "+", "5", "6", "/" }; 1570 std::vector<Dimension> result; 1571 double opRes = 0.0; 1572 EXPECT_TRUE(StringExpression::CalculateExpImpl( __anon2f39384c1102(const Dimension& dim) 1573 rpnexp, [](const Dimension& dim) -> double { return dim.Value(); }, result, opRes)); 1574 rpnexp = { "2_invalid", "*", "3" }; 1575 EXPECT_FALSE(StringExpression::CalculateExpImpl( __anon2f39384c1202(const Dimension& dim) 1576 rpnexp, [](const Dimension& dim) -> double { return dim.Value(); }, result, opRes)); 1577 result.clear(); 1578 rpnexp = { "2", "*", "3", "-", "(", "2", "3", "+", "5", "6", "/" }; 1579 EXPECT_FALSE(StringExpression::CalculateExpImpl( __anon2f39384c1302(const Dimension& dim) 1580 rpnexp, [](const Dimension& dim) -> double { return dim.Value(); }, result, opRes)); 1581 } 1582 1583 /** 1584 * @tc.name: StringExpressionTest007 1585 * @tc.desc: ConvertDal2Rpn: ReplaceSignNumberWithUnit()/FilterCalcSpecialString 1586 * @tc.type: FUNC 1587 */ 1588 HWTEST_F(BaseUtilsTest, StringExpressionTest007, TestSize.Level1) 1589 { 1590 // replace sign number with unit with formula == "" 1591 std::string formula = ""; 1592 std::vector<std::string> ret; 1593 StringExpression::ConvertDal2Rpn(formula, ret); 1594 EXPECT_EQ(formula, ""); 1595 EXPECT_EQ(ret.size(), 0); 1596 1597 // replace sign number with unit normal case 1598 formula = "+1.1px"; 1599 std::vector<std::string> ret2; 1600 StringExpression::ConvertDal2Rpn(formula, ret2); 1601 EXPECT_EQ(ret2.size(), 0); 1602 1603 formula = "calc(2 * 3 - (2 + 3) / 5 + 6 / 2 + (1 + 2))"; 1604 std::vector<std::string> ret3; 1605 StringExpression::ConvertDal2Rpn(formula, ret3); 1606 EXPECT_EQ(ret3.size(), 17); 1607 } 1608 1609 /** 1610 * @tc.name: StringExpressionTest008 1611 * @tc.desc: Test ReplaceSignNumber HandlesSinglePositiveInteger 1612 * @tc.type: FUNC 1613 */ 1614 HWTEST_F(BaseUtilsTest, StringExpressionTest008, TestSize.Level1) 1615 { 1616 std::string input = "+123"; 1617 StringExpression::ReplaceSignNumber(input); 1618 EXPECT_EQ(input, " (0 + 123)"); 1619 } 1620 1621 /** 1622 * @tc.name: StringExpressionTest009 1623 * @tc.desc: Test ReplaceSignNumber HandlesSingleNegativeInteger 1624 * @tc.type: FUNC 1625 */ 1626 HWTEST_F(BaseUtilsTest, StringExpressionTest009, TestSize.Level1) 1627 { 1628 std::string input = "-456"; 1629 StringExpression::ReplaceSignNumber(input); 1630 EXPECT_EQ(input, " (0 - 456)"); 1631 } 1632 1633 /** 1634 * @tc.name: StringExpressionTest010 1635 * @tc.desc: Test ReplaceSignNumber HandlesPositiveDecimal 1636 * @tc.type: FUNC 1637 */ 1638 HWTEST_F(BaseUtilsTest, StringExpressionTest010, TestSize.Level1) 1639 { 1640 std::string input = "+7.89"; 1641 StringExpression::ReplaceSignNumber(input); 1642 EXPECT_EQ(input, " (0 + 7.89)"); 1643 } 1644 1645 /** 1646 * @tc.name: StringExpressionTest011 1647 * @tc.desc: Test ReplaceSignNumber HandlesNegativeDecimal 1648 * @tc.type: FUNC 1649 */ 1650 HWTEST_F(BaseUtilsTest, StringExpressionTest011, TestSize.Level1) 1651 { 1652 std::string input = "-3.14"; 1653 StringExpression::ReplaceSignNumber(input); 1654 EXPECT_EQ(input, " (0 - 3.14)"); 1655 } 1656 1657 /** 1658 * @tc.name: StringExpressionTest012 1659 * @tc.desc: Test ReplaceSignNumber MultipleReplacementsWithText 1660 * @tc.type: FUNC 1661 */ 1662 HWTEST_F(BaseUtilsTest, StringExpressionTest012, TestSize.Level1) 1663 { 1664 std::string input = "a+12-b-34.5"; 1665 StringExpression::ReplaceSignNumber(input); 1666 EXPECT_EQ(input, "a (0 + 12)-b (0 - 34.5)"); 1667 } 1668 1669 /** 1670 * @tc.name: StringExpressionTest013 1671 * @tc.desc: Test ReplaceSignNumber NoSignNumberUnchanged 1672 * @tc.type: FUNC 1673 */ 1674 HWTEST_F(BaseUtilsTest, StringExpressionTest013, TestSize.Level1) 1675 { 1676 std::string input = "123abc"; 1677 StringExpression::ReplaceSignNumber(input); 1678 EXPECT_EQ(input, "123abc"); 1679 } 1680 1681 /** 1682 * @tc.name: StringExpressionTest014 1683 * @tc.desc: Test ReplaceSignNumber MixedOperatorsAndNumbers 1684 * @tc.type: FUNC 1685 */ 1686 HWTEST_F(BaseUtilsTest, StringExpressionTest014, TestSize.Level1) 1687 { 1688 std::string input = "x-5.6+y+7-z"; 1689 StringExpression::ReplaceSignNumber(input); 1690 EXPECT_EQ(input, "x (0 - 5.6)+y (0 + 7)-z"); 1691 } 1692 1693 /** 1694 * @tc.name: StringExpressionTest015 1695 * @tc.desc: Test ReplaceSignNumber ConsecutiveSignNumbers 1696 * @tc.type: FUNC 1697 */ 1698 HWTEST_F(BaseUtilsTest, StringExpressionTest015, TestSize.Level1) 1699 { 1700 std::string input = "-1+2-3.4"; 1701 StringExpression::ReplaceSignNumber(input); 1702 EXPECT_EQ(input, " (0 - 1) (0 + 2) (0 - 3.4)"); 1703 } 1704 1705 /** 1706 * @tc.name: StringExpressionTest016 1707 * @tc.desc: Test ReplaceSignNumber SignWithoutNumberIgnored 1708 * @tc.type: FUNC 1709 */ 1710 HWTEST_F(BaseUtilsTest, StringExpressionTest016, TestSize.Level1) 1711 { 1712 std::string input = "+-*/"; 1713 StringExpression::ReplaceSignNumber(input); 1714 EXPECT_EQ(input, "+-*/"); 1715 } 1716 1717 /** 1718 * @tc.name: StringExpressionTest017 1719 * @tc.desc: Test ReplaceSignNumber NumberAfterNonDigitSignIgnored 1720 * @tc.type: FUNC 1721 */ 1722 HWTEST_F(BaseUtilsTest, StringExpressionTest017, TestSize.Level1) 1723 { 1724 std::string input = "a-b123+45"; 1725 StringExpression::ReplaceSignNumber(input); 1726 EXPECT_EQ(input, "a-b123 (0 + 45)"); 1727 } 1728 1729 /** 1730 * @tc.name: StringExpressionTest018 1731 * @tc.desc: Test ReplaceSignNumber ReplacementAtStringEnd 1732 * @tc.type: FUNC 1733 */ 1734 HWTEST_F(BaseUtilsTest, StringExpressionTest018, TestSize.Level1) 1735 { 1736 std::string input = "xyz-78.9"; 1737 StringExpression::ReplaceSignNumber(input); 1738 EXPECT_EQ(input, "xyz (0 - 78.9)"); 1739 } 1740 1741 /** 1742 * @tc.name: StringExpressionTest019 1743 * @tc.desc: Test ReplaceSignNumber MultipleSignsBeforeNumber 1744 * @tc.type: FUNC 1745 */ 1746 HWTEST_F(BaseUtilsTest, StringExpressionTest019, TestSize.Level1) 1747 { 1748 std::string input = "++123--45.6"; 1749 StringExpression::ReplaceSignNumber(input); 1750 EXPECT_EQ(input, "+ (0 + 123)- (0 - 45.6)"); 1751 } 1752 1753 /** 1754 * @tc.name: StringExpressionTest020 1755 * @tc.desc: Test ConvertDal2Rpn: invalid formula with unbalanced parentheses 1756 * @tc.type: FUNC 1757 */ 1758 HWTEST_F(BaseUtilsTest, StringExpressionTest020, TestSize.Level1) 1759 { 1760 std::string formula = "2+3*(4"; 1761 std::vector<std::string> ret; 1762 StringExpression::ConvertDal2Rpn(formula, ret); 1763 EXPECT_EQ(ret.size(), 0); 1764 } 1765 1766 /** 1767 * @tc.name: StringExpressionTest021 1768 * @tc.desc: Test ConvertDal2Rpn: decimal numbers and negative values 1769 * @tc.type: FUNC 1770 */ 1771 HWTEST_F(BaseUtilsTest, StringExpressionTest021, TestSize.Level1) 1772 { 1773 std::string formula = "calc(-2.5 + 3.1)"; 1774 std::vector<std::string> expected = {"0", "2.5", "-", "3.1", "+"}; 1775 std::vector<std::string> ret; 1776 StringExpression::ConvertDal2Rpn(formula, ret); 1777 EXPECT_EQ(ret, expected); 1778 } 1779 1780 /** 1781 * @tc.name: StringExpressionTest022 1782 * @tc.desc: Test ConvertDal2Rpn: consecutive operators validation 1783 * @tc.type: FUNC 1784 */ 1785 HWTEST_F(BaseUtilsTest, StringExpressionTest022, TestSize.Level1) 1786 { 1787 std::string formula = "3 + + 4"; 1788 std::vector<std::string> ret; 1789 StringExpression::ConvertDal2Rpn(formula, ret); 1790 EXPECT_EQ(ret.size(), 0); 1791 } 1792 1793 /** 1794 * @tc.name: StringExpressionTest023 1795 * @tc.desc: Test ConvertDal2Rpn: formula with illegal special characters 1796 * @tc.type: FUNC 1797 */ 1798 HWTEST_F(BaseUtilsTest, StringExpressionTest023, TestSize.Level1) 1799 { 1800 std::string formula = "calc(1 + 2#3)"; 1801 std::vector<std::string> expected = { "1", "2#3", "+" }; 1802 std::vector<std::string> ret; 1803 StringExpression::ConvertDal2Rpn(formula, ret); 1804 EXPECT_EQ(ret, expected); 1805 } 1806 1807 /** 1808 * @tc.name: StringExpressionTest024 1809 * @tc.desc: Test ConvertDal2Rpn: negative numbers and operator precedence 1810 * @tc.type: FUNC 1811 */ 1812 HWTEST_F(BaseUtilsTest, StringExpressionTest024, TestSize.Level1) 1813 { 1814 std::string formula = "calc(-5 + 3 * -2)"; 1815 std::vector<std::string> expected = { "0", "5", "-", "3", "0", "2", "-", "*", "+" }; 1816 std::vector<std::string> ret; 1817 StringExpression::ConvertDal2Rpn(formula, ret); 1818 EXPECT_EQ(ret, expected); 1819 } 1820 1821 /** 1822 * @tc.name: StringExpressionTest025 1823 * @tc.desc: Test ConvertDal2Rpn: valid formula 1824 * @tc.type: FUNC 1825 */ 1826 HWTEST_F(BaseUtilsTest, StringExpressionTest025, TestSize.Level1) 1827 { 1828 std::string formula = "calc(+10px)"; 1829 std::vector<std::string> expected = { "0px", "10px", "+" }; 1830 std::vector<std::string> ret; 1831 StringExpression::ConvertDal2Rpn(formula, ret); 1832 EXPECT_EQ(ret, expected); 1833 } 1834 1835 /** 1836 * @tc.name: StringExpressionTest026 1837 * @tc.desc: Test ConvertDal2Rpn: verify space removal and nested operations 1838 * @tc.type: FUNC 1839 */ 1840 HWTEST_F(BaseUtilsTest, StringExpressionTest026, TestSize.Level1) 1841 { 1842 std::string formula = "calc( 2 * ( 3 % 2 + 4 ))"; 1843 std::vector<std::string> expected = {"2", "3%2", "4", "+", "*"}; 1844 std::vector<std::string> ret; 1845 StringExpression::ConvertDal2Rpn(formula, ret); 1846 EXPECT_EQ(ret, expected); 1847 } 1848 1849 /** 1850 * @tc.name: DateUtilsTest001 1851 * @tc.desc: GetMilliSecondsByDateTime 1852 * @tc.type: FUNC 1853 */ 1854 HWTEST_F(BaseUtilsTest, DateUtilsTest001, TestSize.Level1) 1855 { 1856 std::tm dateTime; 1857 Date::GetMilliSecondsByDateTime(dateTime); 1858 EXPECT_EQ(dateTime.tm_year != 0, true); 1859 EXPECT_EQ(dateTime.tm_mday != 0, true); 1860 dateTime.tm_mday = 5; 1861 Date::GetMilliSecondsByDateTime(dateTime); 1862 EXPECT_EQ(dateTime.tm_year != 0, true); 1863 EXPECT_EQ(dateTime.tm_mday == 5, true); 1864 dateTime.tm_year = 8; 1865 Date::GetMilliSecondsByDateTime(dateTime); 1866 EXPECT_EQ(dateTime.tm_year == 8, true); 1867 EXPECT_EQ(dateTime.tm_mday == 5, true); 1868 } 1869 1870 /** 1871 * @tc.name: StringUtilsTest001 1872 * @tc.desc: IsAscii 1873 * @tc.type: FUNC 1874 */ 1875 HWTEST_F(BaseUtilsTest, StringUtilsTest001, TestSize.Level1) 1876 { 1877 std::string str = "abcde"; 1878 bool ret = StringUtils::IsAscii(str); 1879 EXPECT_EQ(ret, true); 1880 str = "中文"; 1881 bool ret2 = StringUtils::IsAscii(str); 1882 EXPECT_EQ(ret2, false); 1883 } 1884 1885 /** 1886 * @tc.name: StringUtilsTest002 1887 * @tc.desc: Test Empty String 1888 * @tc.type: FUNC 1889 */ 1890 HWTEST_F(BaseUtilsTest, StringUtilsTest002, TestSize.Level1) 1891 { 1892 std::string emptyStr = ""; 1893 EXPECT_FALSE(IsUTF8(emptyStr)); 1894 } 1895 1896 /** 1897 * @tc.name: StringUtilsTest003 1898 * @tc.desc: Test One Ascii Character String 1899 * @tc.type: FUNC 1900 */ 1901 HWTEST_F(BaseUtilsTest, StringUtilsTest003, TestSize.Level1) 1902 { 1903 std::string asciiA = "a"; 1904 EXPECT_TRUE(IsUTF8(asciiA)); 1905 } 1906 1907 /** 1908 * @tc.name: StringUtilsTest004 1909 * @tc.desc: Test String "a + é + €" 1910 * @tc.type: FUNC 1911 */ 1912 HWTEST_F(BaseUtilsTest, StringUtilsTest004, TestSize.Level1) 1913 { 1914 std::string validStr = "a\xC3\xA9\xE2\x82\xAC"; 1915 EXPECT_TRUE(IsUTF8(validStr)); 1916 } 1917 1918 /** 1919 * @tc.name: StringUtilsTest005 1920 * @tc.desc: Test String "é" 1921 * @tc.type: FUNC 1922 */ 1923 HWTEST_F(BaseUtilsTest, StringUtilsTest005, TestSize.Level1) 1924 { 1925 std::string twoByteStr = "\xC3\xA9"; 1926 EXPECT_TRUE(IsUTF8(twoByteStr)); 1927 } 1928 1929 /** 1930 * @tc.name: StringUtilsTest006 1931 * @tc.desc: Test Half Long Character String 1932 * @tc.type: FUNC 1933 */ 1934 HWTEST_F(BaseUtilsTest, StringUtilsTest006, TestSize.Level1) 1935 { 1936 std::string halfTwoByteStr = "\xC3"; 1937 EXPECT_FALSE(IsUTF8(halfTwoByteStr)); 1938 } 1939 1940 /** 1941 * @tc.name: StringUtilsTest007 1942 * @tc.desc: Test String with Invalid Continuation 1943 * @tc.type: FUNC 1944 */ 1945 HWTEST_F(BaseUtilsTest, StringUtilsTest007, TestSize.Level1) 1946 { 1947 std::string invalidStr = "\xC3\x40"; 1948 EXPECT_FALSE(IsUTF8(invalidStr)); 1949 } 1950 1951 /** 1952 * @tc.name: StringUtilsTest008 1953 * @tc.desc: Test Three Byte String 1954 * @tc.type: FUNC 1955 */ 1956 HWTEST_F(BaseUtilsTest, StringUtilsTest008, TestSize.Level1) 1957 { 1958 std::string threeByteStr = "\xE2\x82\xAC"; 1959 EXPECT_TRUE(IsUTF8(threeByteStr)); 1960 } 1961 1962 /** 1963 * @tc.name: StringUtilsTest009 1964 * @tc.desc: Test Three Byte String with Invalid Continuation 1965 * @tc.type: FUNC 1966 */ 1967 HWTEST_F(BaseUtilsTest, StringUtilsTest009, TestSize.Level1) 1968 { 1969 std::string missingThirdStr = "\xE2\x82"; 1970 EXPECT_FALSE(IsUTF8(missingThirdStr)); 1971 } 1972 1973 /** 1974 * @tc.name: StringUtilsTest010 1975 * @tc.desc: Test Three Byte String with One Invalid Character 1976 * @tc.type: FUNC 1977 */ 1978 HWTEST_F(BaseUtilsTest, StringUtilsTest010, TestSize.Level1) 1979 { 1980 std::string invalidStr = "\xE2\x40\xAC"; 1981 EXPECT_FALSE(IsUTF8(invalidStr)); 1982 } 1983 1984 /** 1985 * @tc.name: StringUtilsTest011 1986 * @tc.desc: Test Four Byte Stringg Smile Face 1987 * @tc.type: FUNC 1988 */ 1989 HWTEST_F(BaseUtilsTest, StringUtilsTest011, TestSize.Level1) 1990 { 1991 std::string fourByteStr = "\xF0\x9F\x98\x8A"; 1992 EXPECT_TRUE(IsUTF8(fourByteStr)); 1993 } 1994 1995 /** 1996 * @tc.name: StringUtilsTest012 1997 * @tc.desc: Test Four Byte String without Continuation 1998 * @tc.type: FUNC 1999 */ 2000 HWTEST_F(BaseUtilsTest, StringUtilsTest012, TestSize.Level1) 2001 { 2002 std::string missingFourth = "\xF0\x9F\x98"; 2003 EXPECT_FALSE(IsUTF8(missingFourth)); 2004 } 2005 2006 /** 2007 * @tc.name: StringUtilsTest013 2008 * @tc.desc: Test Four Byte String with Invalid Character 2009 * @tc.type: FUNC 2010 */ 2011 HWTEST_F(BaseUtilsTest, StringUtilsTest013, TestSize.Level1) 2012 { 2013 std::string invalidSecond = "\xF0\xC0\x80\x80"; 2014 EXPECT_FALSE(IsUTF8(invalidSecond)); 2015 } 2016 2017 /** 2018 * @tc.name: StringUtilsTest014 2019 * @tc.desc: Test Four Byte String with Invalid Start Character 2020 * @tc.type: FUNC 2021 */ 2022 HWTEST_F(BaseUtilsTest, StringUtilsTest014, TestSize.Level1) 2023 { 2024 std::string invalidStart = "\xF8\x80\x80\x80"; 2025 EXPECT_FALSE(IsUTF8(invalidStart)); 2026 } 2027 2028 /** 2029 * @tc.name: StringUtilsTest015 2030 * @tc.desc: Test String Mixed with Invalid Character 2031 * @tc.type: FUNC 2032 */ 2033 HWTEST_F(BaseUtilsTest, StringUtilsTest015, TestSize.Level1) 2034 { 2035 std::string mixedStr = "a\xC3\xA9\xE2\x82"; 2036 EXPECT_FALSE(IsUTF8(mixedStr)); 2037 } 2038 2039 /** 2040 * @tc.name: TimeUtilsTest001 2041 * @tc.desc: ConvertTimestampToStr 2042 * @tc.type: FUNC 2043 */ 2044 HWTEST_F(BaseUtilsTest, TimeUtilsTest001, TestSize.Level1) 2045 { 2046 int64_t timestamp = 1626211200; 2047 std::string ret = ConvertTimestampToStr(timestamp); 2048 EXPECT_EQ(ret, "1970-01-20 03:43:31.200"); 2049 } 2050 2051 /** 2052 * @tc.name: TimeUtilsTest002 2053 * @tc.desc: Test IsHoursWestValid Valid Lower Boundary 2054 * @tc.type: FUNC 2055 */ 2056 HWTEST_F(BaseUtilsTest, TimeUtilsTest002, TestSize.Level1) 2057 { 2058 int32_t hoursWest = -14; 2059 EXPECT_TRUE(IsHoursWestValid(hoursWest)); 2060 EXPECT_EQ(hoursWest, 10); 2061 } 2062 2063 /** 2064 * @tc.name: TimeUtilsTest003 2065 * @tc.desc: Test IsHoursWestValid Valid Special Case 2066 * @tc.type: FUNC 2067 */ 2068 HWTEST_F(BaseUtilsTest, TimeUtilsTest003, TestSize.Level1) 2069 { 2070 int32_t hoursWest = -13; 2071 EXPECT_TRUE(IsHoursWestValid(hoursWest)); 2072 EXPECT_EQ(hoursWest, 11); 2073 } 2074 2075 /** 2076 * @tc.name: TimeUtilsTest004 2077 * @tc.desc: Test IsHoursWestValid Valid Geographical LowerBoundary 2078 * @tc.type: FUNC 2079 */ 2080 HWTEST_F(BaseUtilsTest, TimeUtilsTest004, TestSize.Level1) 2081 { 2082 int32_t hoursWest = -12; 2083 EXPECT_TRUE(IsHoursWestValid(hoursWest)); 2084 EXPECT_EQ(hoursWest, -12); 2085 } 2086 2087 /** 2088 * @tc.name: TimeUtilsTest005 2089 * @tc.desc: Test IsHoursWestValid Valid Upper Boundary 2090 * @tc.type: FUNC 2091 */ 2092 HWTEST_F(BaseUtilsTest, TimeUtilsTest005, TestSize.Level1) 2093 { 2094 int32_t hoursWest = 12; 2095 EXPECT_TRUE(IsHoursWestValid(hoursWest)); 2096 EXPECT_EQ(hoursWest, 12); 2097 } 2098 2099 /** 2100 * @tc.name: TimeUtilsTest006 2101 * @tc.desc: Test IsHoursWestValid Invalid BelowLower Limit 2102 * @tc.type: FUNC 2103 */ 2104 HWTEST_F(BaseUtilsTest, TimeUtilsTest006, TestSize.Level1) 2105 { 2106 int32_t hoursWest = -15; 2107 int32_t original = hoursWest; 2108 EXPECT_FALSE(IsHoursWestValid(hoursWest)); 2109 EXPECT_EQ(hoursWest, original); 2110 } 2111 2112 /** 2113 * @tc.name: TimeUtilsTest007 2114 * @tc.desc: Test IsHoursWestValid Invalid Above Upper Limit 2115 * @tc.type: FUNC 2116 */ 2117 HWTEST_F(BaseUtilsTest, TimeUtilsTest007, TestSize.Level1) 2118 { 2119 int32_t hoursWest = 13; 2120 int32_t original = hoursWest; 2121 EXPECT_FALSE(IsHoursWestValid(hoursWest)); 2122 EXPECT_EQ(hoursWest, original); 2123 } 2124 2125 /** 2126 * @tc.name: TimeUtilsTest008 2127 * @tc.desc: Test IsHoursWestValid Normal Value Zero 2128 * @tc.type: FUNC 2129 */ 2130 HWTEST_F(BaseUtilsTest, TimeUtilsTest008, TestSize.Level1) 2131 { 2132 int32_t hoursWest = 0; 2133 EXPECT_TRUE(IsHoursWestValid(hoursWest)); 2134 EXPECT_EQ(hoursWest, 0); 2135 } 2136 2137 /** 2138 * @tc.name: TimeUtilsTest009 2139 * @tc.desc: Test IsHoursWestValid Normal Value Mid Range 2140 * @tc.type: FUNC 2141 */ 2142 HWTEST_F(BaseUtilsTest, TimeUtilsTest009, TestSize.Level1) 2143 { 2144 int32_t hoursWest = -5; 2145 EXPECT_TRUE(IsHoursWestValid(hoursWest)); 2146 EXPECT_EQ(hoursWest, -5); 2147 } 2148 2149 /** 2150 * @tc.name: TimeUtilsTest010 2151 * @tc.desc: Test IsHoursWestValid At Geographical Lower Limit 2152 * @tc.type: FUNC 2153 */ 2154 HWTEST_F(BaseUtilsTest, TimeUtilsTest010, TestSize.Level1) 2155 { 2156 int32_t hoursWest = -12; 2157 EXPECT_TRUE(IsHoursWestValid(hoursWest)); 2158 EXPECT_EQ(hoursWest, -12); 2159 } 2160 2161 /** 2162 * @tc.name: TimeUtilsTest011 2163 * @tc.desc: Test IsHoursWestValid Just Below Geographical Limit 2164 * @tc.type: FUNC 2165 */ 2166 HWTEST_F(BaseUtilsTest, TimeUtilsTest011, TestSize.Level1) 2167 { 2168 int32_t hoursWest = -14; 2169 EXPECT_TRUE(IsHoursWestValid(hoursWest)); 2170 EXPECT_EQ(hoursWest, 10); 2171 } 2172 2173 /** 2174 * @tc.name: TimeUtilsTest012 2175 * @tc.desc: Test IsHoursWestValid Valid Non Adjusting Value 2176 * @tc.type: FUNC 2177 */ 2178 HWTEST_F(BaseUtilsTest, TimeUtilsTest012, TestSize.Level1) 2179 { 2180 int32_t hoursWest = 5; 2181 EXPECT_TRUE(IsHoursWestValid(hoursWest)); 2182 EXPECT_EQ(hoursWest, 5); 2183 } 2184 2185 /** 2186 * @tc.name: TimeUtilsTest013 2187 * @tc.desc: Test IsDayTime Default 2188 * @tc.type: FUNC 2189 */ 2190 HWTEST_F(BaseUtilsTest, TimeUtilsTest013, TestSize.Level1) 2191 { 2192 TimeOfZone time; 2193 time.hour24_ = 6; 2194 EXPECT_TRUE(IsDayTime(time)); 2195 } 2196 2197 /** 2198 * @tc.name: TimeUtilsTest014 2199 * @tc.desc: Test IsDayTime Lower Boundary 2200 * @tc.type: FUNC 2201 */ 2202 HWTEST_F(BaseUtilsTest, TimeUtilsTest014, TestSize.Level1) 2203 { 2204 TimeOfZone time; 2205 time.hour24_ = 6; 2206 EXPECT_TRUE(IsDayTime(time)); 2207 } 2208 2209 /** 2210 * @tc.name: TimeUtilsTest016 2211 * @tc.desc: Test IsDayTime Upper Boundary Edge 2212 * @tc.type: FUNC 2213 */ 2214 HWTEST_F(BaseUtilsTest, TimeUtilsTest016, TestSize.Level1) 2215 { 2216 TimeOfZone time; 2217 time.hour24_ = 17; 2218 EXPECT_TRUE(IsDayTime(time)); 2219 } 2220 2221 /** 2222 * @tc.name: TimeUtilsTest017 2223 * @tc.desc: Test IsDayTime Upper Boundary 2224 * @tc.type: FUNC 2225 */ 2226 HWTEST_F(BaseUtilsTest, TimeUtilsTest017, TestSize.Level1) 2227 { 2228 TimeOfZone time; 2229 time.hour24_ = 18; 2230 EXPECT_FALSE(IsDayTime(time)); 2231 } 2232 2233 /** 2234 * @tc.name: TimeUtilsTest018 2235 * @tc.desc: Test IsDayTime Below Lower Boundary 2236 * @tc.type: FUNC 2237 */ 2238 HWTEST_F(BaseUtilsTest, TimeUtilsTest018, TestSize.Level1) 2239 { 2240 TimeOfZone time; 2241 time.hour24_ = 5; 2242 EXPECT_FALSE(IsDayTime(time)); 2243 } 2244 2245 /** 2246 * @tc.name: TimeUtilsTest019 2247 * @tc.desc: Test IsDayTime Midday 2248 * @tc.type: FUNC 2249 */ 2250 HWTEST_F(BaseUtilsTest, TimeUtilsTest019, TestSize.Level1) 2251 { 2252 TimeOfZone time; 2253 time.hour24_ = 12; 2254 EXPECT_TRUE(IsDayTime(time)); 2255 } 2256 2257 /** 2258 * @tc.name: TimeUtilsTest020 2259 * @tc.desc: Test IsDayTime Midnight 2260 * @tc.type: FUNC 2261 */ 2262 HWTEST_F(BaseUtilsTest, TimeUtilsTest020, TestSize.Level1) 2263 { 2264 TimeOfZone time; 2265 time.hour24_ = 0; 2266 EXPECT_FALSE(IsDayTime(time)); 2267 } 2268 2269 /** 2270 * @tc.name: TimeUtilsTest021 2271 * @tc.desc: Test IsDayTime LateNight 2272 * @tc.type: FUNC 2273 */ 2274 HWTEST_F(BaseUtilsTest, TimeUtilsTest021, TestSize.Level1) 2275 { 2276 TimeOfZone time; 2277 time.hour24_ = 23; 2278 EXPECT_FALSE(IsDayTime(time)); 2279 } 2280 2281 /** 2282 * @tc.name: TimeUtilsTest022 2283 * @tc.desc: Test IsDayTime Negative 2284 * @tc.type: FUNC 2285 */ 2286 HWTEST_F(BaseUtilsTest, TimeUtilsTest022, TestSize.Level1) 2287 { 2288 TimeOfZone time; 2289 time.hour24_ = -3; 2290 EXPECT_FALSE(IsDayTime(time)); 2291 } 2292 2293 /** 2294 * @tc.name: TimeUtilsTest023 2295 * @tc.desc: Test IsDayTime Over24 2296 * @tc.type: FUNC 2297 */ 2298 HWTEST_F(BaseUtilsTest, TimeUtilsTest023, TestSize.Level1) 2299 { 2300 TimeOfZone time; 2301 time.hour24_ = 25; 2302 EXPECT_FALSE(IsDayTime(time)); 2303 } 2304 2305 /** 2306 * @tc.name: TimeUtilsTest024 2307 * @tc.desc: Test HoursWestIsValid Valid With Adjustment Lower Edge 2308 * @tc.type: FUNC 2309 */ 2310 HWTEST_F(BaseUtilsTest, TimeUtilsTest024, TestSize.Level1) 2311 { 2312 int32_t hoursWest = -14; 2313 EXPECT_TRUE(HoursWestIsValid(hoursWest)); 2314 EXPECT_EQ(hoursWest, 10); 2315 } 2316 2317 /** 2318 * @tc.name: TimeUtilsTest025 2319 * @tc.desc: Test HoursWestIsValid Valid With Adjustment 2320 * @tc.type: FUNC 2321 */ 2322 HWTEST_F(BaseUtilsTest, TimeUtilsTest025, TestSize.Level1) 2323 { 2324 int32_t hoursWest = -13; 2325 EXPECT_TRUE(HoursWestIsValid(hoursWest)); 2326 EXPECT_EQ(hoursWest, 11); 2327 } 2328 2329 /** 2330 * @tc.name: TimeUtilsTest026 2331 * @tc.desc: Test HoursWestIsValid Valid No Adjustment Lower Bound 2332 * @tc.type: FUNC 2333 */ 2334 HWTEST_F(BaseUtilsTest, TimeUtilsTest026, TestSize.Level1) 2335 { 2336 int32_t hoursWest = -12; 2337 EXPECT_TRUE(HoursWestIsValid(hoursWest)); 2338 EXPECT_EQ(hoursWest, -12); 2339 } 2340 2341 /** 2342 * @tc.name: TimeUtilsTest027 2343 * @tc.desc: Test HoursWestIsValid Valid No Adjustment Middle 2344 * @tc.type: FUNC 2345 */ 2346 HWTEST_F(BaseUtilsTest, TimeUtilsTest027, TestSize.Level1) 2347 { 2348 int32_t hoursWest = 0; 2349 EXPECT_TRUE(HoursWestIsValid(hoursWest)); 2350 EXPECT_EQ(hoursWest, 0); 2351 } 2352 2353 /** 2354 * @tc.name: TimeUtilsTest028 2355 * @tc.desc: Test HoursWestIsValid Valid No Adjustment Upper Bound 2356 * @tc.type: FUNC 2357 */ 2358 HWTEST_F(BaseUtilsTest, TimeUtilsTest028, TestSize.Level1) 2359 { 2360 int32_t hoursWest = 12; 2361 EXPECT_TRUE(HoursWestIsValid(hoursWest)); 2362 EXPECT_EQ(hoursWest, 12); 2363 } 2364 2365 /** 2366 * @tc.name: TimeUtilsTest029 2367 * @tc.desc: Test HoursWestIsValid Invalid Below Lower Limit 2368 * @tc.type: FUNC 2369 */ 2370 HWTEST_F(BaseUtilsTest, TimeUtilsTest029, TestSize.Level1) 2371 { 2372 int32_t hoursWest = -15; 2373 const int32_t original = hoursWest; 2374 EXPECT_FALSE(HoursWestIsValid(hoursWest)); 2375 EXPECT_EQ(hoursWest, original); 2376 } 2377 2378 /** 2379 * @tc.name: TimeUtilsTest030 2380 * @tc.desc: Test HoursWestIsValid Invalid Above Upper Limit 2381 * @tc.type: FUNC 2382 */ 2383 HWTEST_F(BaseUtilsTest, TimeUtilsTest030, TestSize.Level1) 2384 { 2385 int32_t hoursWest = 13; 2386 const int32_t original = hoursWest; 2387 EXPECT_FALSE(HoursWestIsValid(hoursWest)); 2388 EXPECT_EQ(hoursWest, original); 2389 } 2390 2391 /** 2392 * @tc.name: UtfConversionTest001 2393 * @tc.desc: Convert a simple ASCII string to UTF-16. 2394 * @tc.type: FUNC 2395 */ 2396 HWTEST_F(BaseUtilsTest, UtfConversionTest001, TestSize.Level1) 2397 { 2398 const std::string asciiStr = "Hello World"; 2399 const size_t utf8Len = asciiStr.size(); 2400 const size_t utf16Len = utf8Len + 1; 2401 std::vector<uint8_t> utf8In(asciiStr.begin(), asciiStr.end()); 2402 std::vector<uint16_t> utf16Out(utf16Len); 2403 2404 size_t written = ConvertRegionUtf8ToUtf16( 2405 utf8In.data(), utf16Out.data(), utf8Len, utf16Len, 0); 2406 2407 std::u16string expectedStr = u"Hello World"; 2408 std::u16string actualStr(utf16Out.begin(), utf16Out.begin() + written); 2409 ASSERT_EQ(actualStr, expectedStr); 2410 ASSERT_EQ(written, expectedStr.size()); 2411 } 2412 2413 /** 2414 * @tc.name: UtfConversionTest002 2415 * @tc.desc: Convert a string with non-ASCII characters to UTF-16. 2416 * @tc.type: FUNC 2417 */ 2418 HWTEST_F(BaseUtilsTest, UtfConversionTest002, TestSize.Level1) 2419 { 2420 const std::string nonAsciiStr = "こんにちは世界"; 2421 const size_t utf8Len = nonAsciiStr.size(); 2422 const size_t utf16Len = utf8Len + 1; 2423 std::vector<uint8_t> utf8In(nonAsciiStr.begin(), nonAsciiStr.end()); 2424 std::vector<uint16_t> utf16Out(utf16Len); 2425 2426 size_t written = ConvertRegionUtf8ToUtf16( 2427 utf8In.data(), utf16Out.data(), utf8Len, utf16Len, 0); 2428 2429 std::u16string expectedStr = u"こんにちは世界"; 2430 std::u16string actualStr(utf16Out.begin(), utf16Out.begin() + written); 2431 2432 ASSERT_EQ(actualStr, expectedStr); 2433 ASSERT_EQ(written, expectedStr.size()); 2434 } 2435 2436 /** 2437 * @tc.name: UtfConversionTest003 2438 * @tc.desc: Convert a string with a mix of ASCII and non-ASCII characters to UTF-16. 2439 * @tc.type: FUNC 2440 */ 2441 HWTEST_F(BaseUtilsTest, UtfConversionTest003, TestSize.Level1) 2442 { 2443 const std::string mixedStr = "Hello, 世界!"; 2444 const size_t utf8Len = mixedStr.size(); 2445 const size_t utf16Len = utf8Len + 1; 2446 std::vector<uint8_t> utf8In(mixedStr.begin(), mixedStr.end()); 2447 std::vector<uint16_t> utf16Out(utf16Len); 2448 2449 size_t written = ConvertRegionUtf8ToUtf16( 2450 utf8In.data(), utf16Out.data(), utf8Len, utf16Len, 0); 2451 2452 std::u16string expectedStr = u"Hello, 世界!"; 2453 std::u16string actualStr(utf16Out.begin(), utf16Out.begin() + written); 2454 2455 ASSERT_EQ(actualStr, expectedStr); 2456 ASSERT_EQ(written, expectedStr.size()); 2457 } 2458 2459 /** 2460 * @tc.name: UtfConversionTest004 2461 * @tc.desc: Convert a string with a 4-byte UTF-8 character to UTF-16. 2462 * @tc.type: FUNC 2463 */ 2464 HWTEST_F(BaseUtilsTest, UtfConversionTest004, TestSize.Level1) 2465 { 2466 const std::string fourByteStr = ""; 2467 const size_t utf8Len = fourByteStr.size(); 2468 const size_t utf16Len = utf8Len * 2; 2469 std::vector<uint8_t> utf8In(fourByteStr.begin(), fourByteStr.end()); 2470 std::vector<uint16_t> utf16Out(utf16Len); 2471 2472 size_t written = ConvertRegionUtf8ToUtf16( 2473 utf8In.data(), utf16Out.data(), utf8Len, utf16Len, 0); 2474 2475 std::u16string expectedStr = u""; 2476 std::u16string actualStr(utf16Out.begin(), utf16Out.begin() + written); 2477 2478 ASSERT_EQ(actualStr, expectedStr); 2479 ASSERT_EQ(written, expectedStr.size()); 2480 } 2481 2482 /** 2483 * @tc.name: UtfConversionTest005 2484 * @tc.desc: Test conversion with a string that requires truncation. 2485 * @tc.type: FUNC 2486 */ 2487 HWTEST_F(BaseUtilsTest, UtfConversionTest005, TestSize.Level1) 2488 { 2489 const std::string fullStr = "This string will be truncated"; 2490 const size_t startOffset = 10; 2491 const size_t utf8Len = fullStr.size() - startOffset; 2492 const size_t utf16Len = utf8Len + 1; 2493 2494 std::vector<uint8_t> utf8In(fullStr.begin(), fullStr.end()); 2495 std::vector<uint16_t> utf16Out(utf16Len); 2496 2497 size_t written = ConvertRegionUtf8ToUtf16( 2498 utf8In.data() + startOffset, utf16Out.data(), utf8Len, utf16Len, 0); 2499 2500 std::u16string expectedStr = u"g will be truncated"; 2501 std::u16string actualStr(utf16Out.begin(), utf16Out.begin() + written); 2502 2503 ASSERT_EQ(actualStr, expectedStr); 2504 ASSERT_EQ(written, expectedStr.size()); 2505 } 2506 2507 /** 2508 * @tc.name: UtfConversionTest007 2509 * @tc.desc: Test conversion with a start offset. 2510 * @tc.type: FUNC 2511 */ 2512 HWTEST_F(BaseUtilsTest, UtfConversionTest007, TestSize.Level1) 2513 { 2514 const std::string offsetStr = "Start offset test string"; 2515 const size_t utf8Len = offsetStr.size(); 2516 const size_t utf16Len = utf8Len * 2; 2517 2518 std::vector<uint8_t> utf8In(offsetStr.begin(), offsetStr.end()); 2519 std::vector<uint16_t> utf16Out(utf16Len); 2520 const size_t startOffset = 10; 2521 2522 size_t written = ConvertRegionUtf8ToUtf16( 2523 utf8In.data(), utf16Out.data(), utf8Len, utf16Len, startOffset); 2524 2525 std::u16string expectedStr = u"et test string"; 2526 std::u16string actualStr(utf16Out.begin(), utf16Out.begin() + written); 2527 2528 ASSERT_EQ(actualStr, expectedStr); 2529 ASSERT_EQ(written, expectedStr.size()); 2530 } 2531 } // namespace OHOS::Ace 2532