• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <climits>
17 #include <gtest/gtest.h>
18 
19 #define private public
20 #include "util/string.h"
21 #undef private
22 
23 using namespace testing;
24 using namespace testing::ext;
25 using namespace OHOS::Idl;
26 
27 namespace OHOS {
28 namespace idl {
29 
30 class UtilStringUnitTest : public testing::Test {
31 public:
UtilStringUnitTest()32     UtilStringUnitTest() {}
33 
~UtilStringUnitTest()34     virtual ~UtilStringUnitTest() {}
35 
36     static void SetUpTestCase();
37 
38     static void TearDownTestCase();
39 
40     void SetUp();
41 
42     void TearDown();
43 };
44 
SetUpTestCase()45 void UtilStringUnitTest::SetUpTestCase() {}
46 
TearDownTestCase()47 void UtilStringUnitTest::TearDownTestCase() {}
48 
SetUp()49 void UtilStringUnitTest::SetUp() {}
50 
TearDown()51 void UtilStringUnitTest::TearDown() {}
52 
53 /**
54  * @tc.name: IndexOf_test_001
55  * @tc.desc: Verify the IndexOf function.
56  * @tc.type: FUNC
57  * @tc.require: #I72EZC
58  */
59 HWTEST_F(UtilStringUnitTest, IndexOf_test_001, TestSize.Level1)
60 {
61     GTEST_LOG_(INFO) << "UtilStringUnitTest, IndexOf_test_001, TestSize.Level1";
62 
63     EXPECT_EQ(String().IndexOf(String("name"), 1), -1);
64     EXPECT_EQ(String("name").IndexOf(String(), 1), -1);
65     EXPECT_EQ(String("bananan").IndexOf("ana", 1), 1);
66 }
67 
68 /**
69  * @tc.name: LastIndexOf_test_001
70  * @tc.desc: Verify the LastIndexOf function.
71  * @tc.type: FUNC
72  * @tc.require: #I72EZC
73  */
74 HWTEST_F(UtilStringUnitTest, LastIndexOf_test_001, TestSize.Level1)
75 {
76     GTEST_LOG_(INFO) << "UtilStringUnitTest, LastIndexOf_test_001, TestSize.Level1";
77 
78     EXPECT_EQ(String().LastIndexOf(String("name"), 1), -1);
79     EXPECT_EQ(String("bananan").LastIndexOf(String("ana"), 1), 1);
80     EXPECT_EQ(String("bananan").LastIndexOf(String("ana"), -2), -1);
81     EXPECT_EQ(String("bananana").LastIndexOf(String("ana"), 0), 5);
82     EXPECT_EQ(String("bananana").LastIndexOf(String("ana"), 1), 1);
83 }
84 
85 /**
86  * @tc.name: StartsWith_test_001
87  * @tc.desc: Verify the StartsWith function.
88  * @tc.type: FUNC
89  * @tc.require: #I72EZC
90  */
91 HWTEST_F(UtilStringUnitTest, StartsWith_test_001, TestSize.Level1)
92 {
93     GTEST_LOG_(INFO) << "UtilStringUnitTest, StartsWith_test_001, TestSize.Level1";
94 
95     EXPECT_FALSE(String().StartsWith(String("name")));
96     EXPECT_FALSE(String("name").StartsWith(String()));
97     EXPECT_FALSE(String("name").StartsWith(String("names")));
98 
99     EXPECT_TRUE(String("name").StartsWith(String("")));
100     EXPECT_TRUE(String("name").StartsWith(String("na")));
101     EXPECT_TRUE(String("n.a.m.e").StartsWith(String("n.a")));
102 }
103 
104 /**
105  * @tc.name: EndsWith_test_001
106  * @tc.desc: Verify the EndsWith function.
107  * @tc.type: FUNC
108  * @tc.require: #I72EZC
109  */
110 HWTEST_F(UtilStringUnitTest, EndsWith_test_001, TestSize.Level1)
111 {
112     GTEST_LOG_(INFO) << "UtilStringUnitTest, EndsWith_test_001, TestSize.Level1";
113 
114     EXPECT_FALSE(String().EndsWith(String("name")));
115     EXPECT_FALSE(String("name").EndsWith(String()));
116     EXPECT_FALSE(String("name").EndsWith(String("names")));
117     EXPECT_FALSE(String("name").EndsWith(String("ma")));
118 
119     EXPECT_TRUE(String("name").EndsWith(String("")));
120     EXPECT_TRUE(String("name").EndsWith(String("me")));
121     EXPECT_TRUE(String("n.a.m.e").EndsWith(String("m.e")));
122 }
123 
124 /**
125  * @tc.name: ToLowerCase_test_001
126  * @tc.desc: Verify the ToLowerCase function.
127  * @tc.type: FUNC
128  * @tc.require: #I72EZC
129  */
130 HWTEST_F(UtilStringUnitTest, ToLowerCase_test_001, TestSize.Level1)
131 {
132     GTEST_LOG_(INFO) << "UtilStringUnitTest, ToLowerCase_test_001, TestSize.Level1";
133 
134     EXPECT_STREQ(String().ToLowerCase(), String());
135     EXPECT_STREQ(String("name").ToLowerCase(), String("name"));
136     EXPECT_STREQ(String("NAME").ToLowerCase(), String("name"));
137     EXPECT_STREQ(String("n.A.m.E").ToLowerCase(), String("n.a.m.e"));
138     EXPECT_STREQ(String("n.a.m.e").ToLowerCase(), String("n.a.m.e"));
139 }
140 
141 /**
142  * @tc.name: ToUpperCase_test_001
143  * @tc.desc: Verify the ToUpperCase function.
144  * @tc.type: FUNC
145  * @tc.require: #I72EZC
146  */
147 HWTEST_F(UtilStringUnitTest, ToUpperCase_test_001, TestSize.Level1)
148 {
149     GTEST_LOG_(INFO) << "UtilStringUnitTest, ToUpperCase_test_001, TestSize.Level1";
150 
151     EXPECT_STREQ(String().ToUpperCase(), String());
152     EXPECT_STREQ(String("name").ToUpperCase(), String("NAME"));
153     EXPECT_STREQ(String("NAME").ToUpperCase(), String("NAME"));
154     EXPECT_STREQ(String("n.a.m.e").ToUpperCase(), String("N.A.M.E"));
155     EXPECT_STREQ(String("n.A.m.E").ToUpperCase(), String("N.A.M.E"));
156 }
157 
158 /**
159  * @tc.name: Replace_test_001
160  * @tc.desc: Verify the Replace function.
161  * @tc.type: FUNC
162  * @tc.require: #I72EZC
163  */
164 HWTEST_F(UtilStringUnitTest, Replace_test_001, TestSize.Level1)
165 {
166     GTEST_LOG_(INFO) << "UtilStringUnitTest, Replace_test_001, TestSize.Level1";
167 
168     String name("n.a.m.e");
169     EXPECT_STREQ(name.Replace('.', '.'), name);
170     EXPECT_STREQ(String("n.a.m.e").Replace('.', '|'), String("n|a|m|e"));
171     EXPECT_STREQ(String().Replace('.', '|'), String());
172     EXPECT_STREQ(String("n.a.m.e").Replace('d', '|'), String("n.a.m.e"));
173     EXPECT_STREQ(String("n.a.m.e").Replace(String("ab"), String("cd")), String("n.a.m.e"));
174 }
175 
176 /**
177  * @tc.name: Replace_test_002
178  * @tc.desc: Verify the Replace function.
179  * @tc.type: FUNC
180  * @tc.require: #I72EZC
181  */
182 HWTEST_F(UtilStringUnitTest, Replace_test_002, TestSize.Level1)
183 {
184     GTEST_LOG_(INFO) << "UtilStringUnitTest, Replace_test_002, TestSize.Level1";
185 
186     String name("n..a..m..e");
187     EXPECT_STREQ(name.Replace(nullptr, "replacemenet"), name);
188     EXPECT_STREQ(name.Replace("", "replacemenet"), name);
189     EXPECT_STREQ(name.Replace("target", nullptr), name);
190     EXPECT_STREQ(name.Replace("..", "//"), String("n//a//m//e"));
191 }
192 
193 /**
194  * @tc.name: Replace_test_003
195  * @tc.desc: Verify the Replace function.
196  * @tc.type: FUNC
197  * @tc.require: #I72EZC
198  */
199 HWTEST_F(UtilStringUnitTest, Replace_test_003, TestSize.Level1)
200 {
201     GTEST_LOG_(INFO) << "UtilStringUnitTest, Replace_test_003, TestSize.Level1";
202 
203     String name("n..a..m..e");
204     EXPECT_STREQ(name.Replace(String(), String("replacemenet")), name);
205     EXPECT_STREQ(name.Replace(String(".."), String("||")), String("n||a||m||e"));
206 }
207 
208 /**
209  * @tc.name: Operator_test_001
210  * @tc.desc: Verify the Operator function.
211  * @tc.type: FUNC
212  * @tc.require: #I72EZC
213  */
214 HWTEST_F(UtilStringUnitTest, Operator_test_001, TestSize.Level1)
215 {
216     GTEST_LOG_(INFO) << "UtilStringUnitTest, Operator_test_001, TestSize.Level1";
217 
218     EXPECT_STREQ(String("str1") += String("str2"), String("str1str2"));
219     EXPECT_STREQ(String("str1") += String(), String("str1"));
220     EXPECT_STREQ(String("str1") = static_cast<const char *>(nullptr), nullptr);
221 }
222 
223 /**
224  * @tc.name: Allocate_test_001
225  * @tc.desc: Verify the Allocate function.
226  * @tc.type: FUNC
227  * @tc.require: #I72EZC
228  */
229 HWTEST_F(UtilStringUnitTest, Allocate_test_001, TestSize.Level1)
230 {
231     EXPECT_EQ(String("name", -1), nullptr);
232     EXPECT_EQ(String("name", INT_MAX), nullptr);
233 
234     String name("a..a..m..e");
235     const char* strChar = nullptr;
236     EXPECT_EQ(name.Equals(strChar), false);
237     EXPECT_EQ(String().Equals(strChar), true);
238     EXPECT_EQ(name.Equals(String(nullptr)), false);
239     EXPECT_EQ(String().Equals(String(nullptr)), true);
240     EXPECT_EQ(name[-1], '\0');
241     EXPECT_EQ(name[INT8_MAX], '\0');
242 
243     EXPECT_EQ(String().IndexOf('a', -1), -1);
244     EXPECT_EQ(name.IndexOf(strChar, -1), -1);
245     EXPECT_EQ(name.IndexOf('\0', -1), -1);
246     EXPECT_EQ(name.IndexOf('a', -1), 0);
247     EXPECT_EQ(name.IndexOf('a', INT_MAX), -1);
248     EXPECT_EQ(name.IndexOf(String("ab"), -1), -1);
249     EXPECT_EQ(name.IndexOf(String("ab"), INT_MAX), -1);
250 
251     EXPECT_EQ(String().LastIndexOf('\0', 0), -1);
252     EXPECT_EQ(name.LastIndexOf(strChar, 0), -1);
253     EXPECT_EQ(name.LastIndexOf('\0', 0), -1);
254     EXPECT_EQ(name.LastIndexOf('a', -1), -1);
255 
256     EXPECT_EQ(String().StartsWith(strChar), false);
257     EXPECT_EQ(name.StartsWith(strChar), false);
258     EXPECT_EQ(name.StartsWith('\0'), true);
259     EXPECT_EQ(String().StartsWith('\0'), false);
260     EXPECT_EQ(name.StartsWith("n..a..m..e..long"), false);
261     EXPECT_EQ(String().StartsWith(String('\0')), false);
262     EXPECT_EQ(name.StartsWith(String('\0')), true);
263 
264     EXPECT_EQ(String().EndsWith(strChar), false);
265     EXPECT_EQ(name.EndsWith(strChar), false);
266     EXPECT_EQ(name.EndsWith('\0'), true);
267     EXPECT_EQ(name.EndsWith("n..a..m..e..long"), false);
268 
269     EXPECT_EQ(name.Substring(-1), String());
270 }
271 
272 } // namespace idl
273 } // namespace OHOS
274