1 /* 2 * Copyright (C) 2025 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 <cstdint> 17 #include <gtest/gtest.h> 18 #include <string> 19 20 #include "global.h" 21 #include "string_utils.h" 22 23 24 using namespace testing::ext; 25 namespace OHOS { 26 namespace MiscServices { 27 class StringUtilsTest : public testing::Test { 28 public: SetUp()29 void SetUp() 30 { 31 IMSA_HILOGI("StringUtils::SetUp"); 32 } TearDown()33 void TearDown() 34 { 35 IMSA_HILOGI("StringUtils::TearDown"); 36 } 37 }; 38 39 /** 40 * @tc.name: testToHex_001 41 * @tc.desc: IMA 42 * @tc.type: FUNC 43 * @tc.require: 44 */ 45 HWTEST_F(StringUtilsTest, testToHex_001, TestSize.Level0) 46 { 47 std::string out = StringUtils::ToHex(std::string("a")); 48 std::string result = "61"; 49 IMSA_HILOGI("out:%{public}s,result:%{public}s", out.c_str(), result.c_str()); 50 EXPECT_TRUE(out.compare(result) == 0); 51 out = StringUtils::ToHex(std::u16string(u"a")); 52 IMSA_HILOGI("out:%{public}s,result:%{public}s", out.c_str(), result.c_str()); 53 result = "0061"; 54 EXPECT_TRUE(out.compare(result) == 0); 55 } 56 57 /** 58 * @tc.name: testCountUtf16Chars_001 59 * @tc.desc: IMA 60 * @tc.type: FUNC 61 * @tc.require: 62 */ 63 HWTEST_F(StringUtilsTest, testCountUtf16Chars_001, TestSize.Level0) 64 { 65 char16_t inputChar[] = u"abcdefg\0"; 66 size_t inputLen = sizeof(inputChar) / sizeof(char16_t); 67 IMSA_HILOGI("out:%{public}zu", inputLen); 68 std::u16string out(inputChar, inputLen); 69 IMSA_HILOGI("out:%{public}s", StringUtils::ToHex(out).c_str()); 70 auto charNum = StringUtils::CountUtf16Chars(out); 71 StringUtils::TruncateUtf16String(out, charNum -1); 72 std::u16string checkOut(inputChar, inputLen - 1); 73 IMSA_HILOGI("out:%{public}s", StringUtils::ToHex(checkOut).c_str()); 74 EXPECT_TRUE(out.compare(checkOut) == 0); 75 } 76 77 } // namespace MiscServices 78 } // namespace OHOS 79