• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gtest/gtest.h"
17 
18 #include "util/string_pool.h"
19 #include "util/string_helper.h"
20 #include "util/string_builder.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS::Idl;
25 
26 namespace OHOS {
27 namespace idl {
28 
29 static const int TEST_THIRTY = 13;
30 
31 class IdlTool2UtilStringTest : public testing::Test {
32 public:
IdlTool2UtilStringTest()33     IdlTool2UtilStringTest() {}
34 
~IdlTool2UtilStringTest()35     virtual ~IdlTool2UtilStringTest() {}
36 
37     static void SetUpTestCase();
38 
39     static void TearDownTestCase();
40 
41     void SetUp();
42 
43     void TearDown();
44 };
45 
SetUpTestCase()46 void IdlTool2UtilStringTest::SetUpTestCase() {}
47 
TearDownTestCase()48 void IdlTool2UtilStringTest::TearDownTestCase() {}
49 
SetUp()50 void IdlTool2UtilStringTest::SetUp() {}
51 
TearDown()52 void IdlTool2UtilStringTest::TearDown() {}
53 
54 /*
55  * @tc.name: StringPool_test_001
56  * @tc.desc: Verify the StringPool function.
57  * @tc.type: FUNC
58  * @tc.require:
59  */
HWTEST_F(IdlTool2UtilStringTest,StringPool_test_001,Level1)60 HWTEST_F(IdlTool2UtilStringTest, StringPool_test_001, Level1)
61 {
62     StringPool stringPool_;
63     StringPool *stringPoolPtr_ = new StringPool();
64     stringPool_.Add("");
65     stringPool_.Add("name");
66     stringPool_.Add("name");
67     stringPool_.Add("bananan");
68     ptrdiff_t offset = stringPool_.GetOffset("name");
69     size_t size = stringPool_.GetSize();
70     EXPECT_EQ(offset, 0);
71     EXPECT_EQ(size, TEST_THIRTY);
72     delete stringPoolPtr_;
73 }
74 
75 /*
76  * @tc.name: StringHelper_test_001
77  * @tc.desc: Verify the Split StartWith EndWith function.
78  * @tc.type: FUNC
79  * @tc.require:
80  */
HWTEST_F(IdlTool2UtilStringTest,StringHelper_test_001,Level1)81 HWTEST_F(IdlTool2UtilStringTest, StringHelper_test_001, Level1)
82 {
83     std::vector<std::string> result;
84     EXPECT_EQ(StringHelper::Split("", ""), result);
85     result = {"name"};
86     EXPECT_EQ(StringHelper::Split("name", ""), result);
87     result = {"n", "e"};
88     EXPECT_EQ(StringHelper::Split("name", "am"), result);
89 
90     EXPECT_EQ(StringHelper::StartWith("name", 'n'), true);
91 
92     EXPECT_EQ(StringHelper::StartWith("name", "m"), false);
93     EXPECT_EQ(StringHelper::StartWith("name", "n"), true);
94 
95     EXPECT_EQ(StringHelper::EndWith("", 'e'), false);
96     EXPECT_EQ(StringHelper::EndWith("name", 'e'), true);
97 
98     EXPECT_EQ(StringHelper::EndWith("name", "m"), false);
99     EXPECT_EQ(StringHelper::EndWith("name", "me"), true);
100 }
101 
102 /*
103  * @tc.name: StringHelper_test_002
104  * @tc.desc: Verify the Replace function.
105  * @tc.type: FUNC
106  * @tc.require:
107  */
HWTEST_F(IdlTool2UtilStringTest,StringHelper_test_002,Level1)108 HWTEST_F(IdlTool2UtilStringTest, StringHelper_test_002, Level1)
109 {
110     std::string value = "name";
111     EXPECT_EQ(StringHelper::Replace("", 'a', 'm'), "");
112     EXPECT_EQ(StringHelper::Replace(value, 'a', 'a'), value);
113     EXPECT_EQ(StringHelper::Replace(value, 'a', 'm'), "nmme");
114 
115     EXPECT_EQ(StringHelper::Replace(value, "a", "m"), "nmme");
116 
117     EXPECT_EQ(StringHelper::Replace(value, value.size(), "a", "m"), value);
118     EXPECT_EQ(StringHelper::Replace(value, 0, "a", "m"), "nmme");
119 
120     EXPECT_EQ(StringHelper::Replace(value, value.size(), 1, "m"), value);
121     EXPECT_EQ(StringHelper::Replace(value, 0, 0, "m"), value);
122 }
123 
124 /*
125  * @tc.name: StringHelper_test_003
126  * @tc.desc: Verify the SubStr function.
127  * @tc.type: FUNC
128  * @tc.require:
129  */
HWTEST_F(IdlTool2UtilStringTest,StringHelper_test_003,Level1)130 HWTEST_F(IdlTool2UtilStringTest, StringHelper_test_003, Level1)
131 {
132     std::string value = "name";
133     EXPECT_EQ(StringHelper::SubStr("", 0, 0), "");
134     EXPECT_EQ(StringHelper::SubStr(value, std::string::npos, 0), "");
135     EXPECT_EQ(StringHelper::SubStr(value, 1, 0), "");
136     EXPECT_EQ(StringHelper::SubStr(value, 0, std::string::npos), value);
137     EXPECT_EQ(StringHelper::SubStr(value, 0, 1), "n");
138 }
139 
140 /*
141  * @tc.name: StringHelper_test_004
142  * @tc.desc: Verify the StrToLower StrToUpper FirstToUpper function.
143  * @tc.type: FUNC
144  * @tc.require:
145  */
HWTEST_F(IdlTool2UtilStringTest,StringHelper_test_004,Level1)146 HWTEST_F(IdlTool2UtilStringTest, StringHelper_test_004, Level1)
147 {
148     std::string valueLow = "name";
149     std::string valueUpper = "NAME";
150     EXPECT_EQ(StringHelper::StrToLower(valueUpper), valueLow);
151     EXPECT_EQ(StringHelper::StrToUpper(valueLow), valueUpper);
152     EXPECT_EQ(StringHelper::FirstToUpper(valueLow), "Name");
153 }
154 
155 /*
156  * @tc.name: StringHelper_test_005
157  * @tc.desc: Verify the Format function.
158  * @tc.type: FUNC
159  * @tc.require:
160  */
HWTEST_F(IdlTool2UtilStringTest,StringHelper_test_005,Level1)161 HWTEST_F(IdlTool2UtilStringTest, StringHelper_test_005, Level1)
162 {
163     std::string value = "name";
164     EXPECT_EQ(StringHelper::Format(""), "");
165     EXPECT_EQ(StringHelper::Format("%s", "name"), value);
166 }
167 
168 /*
169  * @tc.name: StringHelper_test_006
170  * @tc.desc: Verify the GetHashCode function.
171  * @tc.type: FUNC
172  * @tc.require:
173  */
HWTEST_F(IdlTool2UtilStringTest,StringHelper_test_006,Level1)174 HWTEST_F(IdlTool2UtilStringTest, StringHelper_test_006, Level1)
175 {
176     EXPECT_EQ(StringHelper::GetHashCode(""), 0);
177 }
178 
179 /*
180  * @tc.name: StringBuilder_test_001
181  * @tc.desc: Verify the Append ToString function.
182  * @tc.type: FUNC
183  * @tc.require:
184  */
HWTEST_F(IdlTool2UtilStringTest,StringBuilder_test_001,Level1)185 HWTEST_F(IdlTool2UtilStringTest, StringBuilder_test_001, Level1)
186 {
187     StringBuilder sb;
188     sb.Append('a');
189     EXPECT_EQ(sb.ToString(), "a");
190     const char *value = "b";
191     sb.Append(value);
192     EXPECT_EQ(sb.ToString(), "ab");
193     sb.Append("c");
194     EXPECT_EQ(sb.ToString(), "abc");
195     sb.AppendFormat("%s", "d");
196     EXPECT_EQ(sb.ToString(), "abcd");
197 }
198 } // namespace idl
199 } // namespace OHOS