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