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