1 /* 2 * Copyright (c) 2021 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 #include <hwext/gtest-ext.h> 16 #include <hwext/gtest-tag.h> 17 #include <random> 18 #include <string> 19 #include "string_utils.h" 20 21 namespace { 22 using testing::ext::TestSize; 23 constexpr int ROUNDS = 20; 24 constexpr int STR_MAX_SIZE = 10; 25 constexpr char RANDOM_CHAR_TABLE[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 26 constexpr long RANDOM_CHAR_NUMBER = std::size(RANDOM_CHAR_TABLE) - 1; 27 28 class StringUtilsTest : public ::testing::Test { 29 std::mt19937 gen_; 30 31 protected: SetUp()32 void SetUp() override {} 33 TearDown()34 void TearDown() override {} 35 RandomInt(int a,int b)36 int RandomInt(int a, int b) 37 { 38 std::uniform_int_distribution<int> distrib(a, b); 39 return distrib(gen_); 40 } 41 RandomChar()42 char RandomChar() 43 { 44 return RANDOM_CHAR_TABLE[RandomInt(0, RANDOM_CHAR_NUMBER - 1)]; 45 } 46 RandomString(int len)47 std::string RandomString(int len) 48 { 49 std::string str; 50 str.reserve(len); 51 for (int i = 0; i < len; i++) { 52 str.push_back(RandomChar()); 53 } 54 return str; 55 } 56 }; 57 58 /* 59 * @tc.name: EndsWithNormal 60 * @tc.desc: test StringUtils::EndsWith with normal case. 61 * @tc.type: FUNC 62 */ 63 HWTEST_F(StringUtilsTest, EndsWithNormal, TestSize.Level1) 64 { 65 std::string str = "abbcccdefgh.txt"; 66 std::string ext = ".txt"; 67 EXPECT_TRUE(StringUtils::EndsWith(str, ext)); 68 EXPECT_TRUE(StringUtils::EndsWith(str, str)); 69 EXPECT_TRUE(StringUtils::EndsWith(ext, ext)); 70 } 71 72 /* 73 * @tc.name: EndsWithFalse 74 * @tc.desc: test StringUtils::EndsWith with false case. 75 * @tc.type: FUNC 76 */ 77 HWTEST_F(StringUtilsTest, EndsWithFalse, TestSize.Level1) 78 { 79 std::string str = "abbcccdefgh.txt"; 80 std::string ext = "txt"; 81 EXPECT_FALSE(StringUtils::EndsWith(str, +".")); 82 EXPECT_FALSE(StringUtils::EndsWith(ext, str)); 83 } 84 85 /* 86 * @tc.name: StartsWithNormal 87 * @tc.desc: test StringUtils::StartsWith with normal case. 88 * @tc.type: FUNC 89 */ 90 HWTEST_F(StringUtilsTest, StartsWithNormal, TestSize.Level1) 91 { 92 std::string str = "abbcccdefgh.txt"; 93 std::string pre = "abb"; 94 EXPECT_TRUE(StringUtils::StartsWith(str, pre)); 95 EXPECT_TRUE(StringUtils::StartsWith(str, "a")); 96 EXPECT_TRUE(StringUtils::StartsWith(str, "ab")); 97 } 98 99 /* 100 * @tc.name: StartsWithFalse 101 * @tc.desc: test StringUtils::StartsWith with false case. 102 * @tc.type: FUNC 103 */ 104 HWTEST_F(StringUtilsTest, StartsWithFalse, TestSize.Level1) 105 { 106 std::string str = "abbcccdefgh.txt"; 107 std::string pre = "abb"; 108 EXPECT_FALSE(StringUtils::StartsWith(str, "b")); 109 EXPECT_FALSE(StringUtils::StartsWith(str, "bb")); 110 EXPECT_FALSE(StringUtils::StartsWith(str, "bbc")); 111 } 112 113 /* 114 * @tc.name: StartsWithRandomized 115 * @tc.desc: test StringUtils::StartsWith with random input. 116 * @tc.type: FUNC 117 */ 118 HWTEST_F(StringUtilsTest, StartsWithRandomize, TestSize.Level1) 119 { 120 for (int i = 0; i < ROUNDS; i++) { 121 std::string str = RandomString(STR_MAX_SIZE); 122 std::string part = str.substr(0, RandomInt(1, static_cast<int>(str.size()))); 123 EXPECT_TRUE(StringUtils::StartsWith(str, part)); 124 125 std::string target = RandomString(RandomInt(1, STR_MAX_SIZE)); 126 bool isPrefix = (str.substr(0, target.size()) == target); 127 EXPECT_EQ(StringUtils::StartsWith(str, target), isPrefix); 128 } 129 } 130 131 /* 132 * @tc.name: ContainsNormal 133 * @tc.desc: test StringUtils::Contains with normal input. 134 * @tc.type: FUNC 135 */ 136 HWTEST_F(StringUtilsTest, ContainsNormal, TestSize.Level1) 137 { 138 std::string str = "abbcccdefgh.txt"; 139 std::string target = "de"; 140 EXPECT_TRUE(StringUtils::Contains(str, target)); 141 EXPECT_TRUE(StringUtils::Contains(str, "a")); 142 EXPECT_TRUE(StringUtils::Contains(str, "b")); 143 EXPECT_TRUE(StringUtils::Contains(str, "c")); 144 EXPECT_TRUE(StringUtils::Contains(str, "t")); 145 EXPECT_TRUE(StringUtils::Contains(str, "txt")); 146 } 147 148 /* 149 * @tc.name: ContainsNormal 150 * @tc.desc: test StringUtils::Contains with false case. 151 * @tc.type: FUNC 152 */ 153 HWTEST_F(StringUtilsTest, ContainsFalse, TestSize.Level1) 154 { 155 std::string str = "abbcccdefgh.txt"; 156 std::string target = "de"; 157 EXPECT_FALSE(StringUtils::Contains(str, target + ".")); 158 EXPECT_FALSE(StringUtils::Contains(str, "a.")); 159 EXPECT_FALSE(StringUtils::Contains(str, "b.")); 160 EXPECT_FALSE(StringUtils::Contains(str, "c.")); 161 EXPECT_FALSE(StringUtils::Contains(str, "t.")); 162 EXPECT_FALSE(StringUtils::Contains(str, "txt.")); 163 } 164 165 /* 166 * @tc.name: ContainsRandomize 167 * @tc.desc: test StringUtils::Contains with random input. 168 * @tc.type: FUNC 169 */ 170 HWTEST_F(StringUtilsTest, ContainsRandomize, TestSize.Level1) 171 { 172 for (int i = 0; i < ROUNDS; i++) { 173 std::string str = RandomString(STR_MAX_SIZE); 174 std::string part = str.substr(RandomInt(1, static_cast<int>(str.size()))); 175 EXPECT_TRUE(StringUtils::Contains(str, part)); 176 177 std::string target = RandomString(RandomInt(1, static_cast<int>(str.size()))); 178 bool isContains = (str.find(target) != std::string::npos); 179 EXPECT_EQ(StringUtils::Contains(str, target), isContains); 180 } 181 } 182 183 /* 184 * @tc.name: StripNormal 185 * @tc.desc: test StringUtils::Strip with normal input. 186 * @tc.type: FUNC 187 */ 188 HWTEST_F(StringUtilsTest, StripNormal, TestSize.Level1) 189 { 190 std::string str = "abbcccdefgh.txt"; 191 EXPECT_EQ(StringUtils::Strip(str + " "), str); 192 EXPECT_EQ(StringUtils::Strip(" " + str), str); 193 EXPECT_EQ(StringUtils::Strip(str + "\t"), str); 194 EXPECT_EQ(StringUtils::Strip("\t" + str), str); 195 EXPECT_EQ(StringUtils::Strip(str + "\n"), str); 196 EXPECT_EQ(StringUtils::Strip("\n" + str), str); 197 } 198 199 /* 200 * @tc.name: StripRandomize 201 * @tc.desc: test StringUtils::Strip with random input. 202 * @tc.type: FUNC 203 */ 204 HWTEST_F(StringUtilsTest, StripRandomize, TestSize.Level1) 205 { 206 for (int i = 0; i < ROUNDS; i++) { 207 std::string str = RandomString(STR_MAX_SIZE); 208 std::string original = str; 209 int leftPadding = RandomInt(0, static_cast<int>(str.size())); 210 if (leftPadding > 0) { 211 str = std::string(leftPadding, ' ') + str; 212 } 213 214 int rightPadding = RandomInt(0, static_cast<int>(str.size())); 215 if (rightPadding > 0) { 216 str += std::string(rightPadding, ' '); 217 } 218 EXPECT_EQ(StringUtils::Strip(str), original); 219 } 220 } 221 222 /* 223 * @tc.name: JoinNormal 224 * @tc.desc: test StringUtils::Join with normal input. 225 * @tc.type: FUNC 226 */ 227 HWTEST_F(StringUtilsTest, JoinNormal, TestSize.Level1) 228 { 229 std::vector<std::string> parts = {"a", "bb", "ccc", "dddd"}; 230 std::string joined; 231 for (auto& str : parts) { 232 joined += str; 233 joined += " "; 234 } 235 joined.pop_back(); 236 EXPECT_EQ(StringUtils::Join(parts, " "), joined); 237 } 238 239 /* 240 * @tc.name: JoinRandomize 241 * @tc.desc: test StringUtils::Join with random input. 242 * @tc.type: FUNC 243 */ 244 HWTEST_F(StringUtilsTest, JoinRandomize, TestSize.Level1) 245 { 246 for (int i = 0; i < ROUNDS; i++) { 247 std::vector<std::string> parts; 248 for (int j = 0; j < STR_MAX_SIZE; j++) { 249 parts.push_back(RandomString(RandomInt(1, STR_MAX_SIZE))); 250 } 251 std::string joined; 252 for (auto& str : parts) { 253 joined += str; 254 joined += " "; 255 } 256 joined.pop_back(); 257 EXPECT_EQ(StringUtils::Join(parts, " "), joined); 258 } 259 } 260 261 /* 262 * @tc.name: SplitNormal 263 * @tc.desc: test StringUtils::Split with normal input. 264 * @tc.type: FUNC 265 */ 266 HWTEST_F(StringUtilsTest, SplitNormal, TestSize.Level1) 267 { 268 std::vector<std::string> parts = {"a", "bb", "ccc", "dddd"}; 269 std::string joined; 270 for (auto& str : parts) { 271 joined += str; 272 joined += " "; 273 } 274 joined.pop_back(); 275 EXPECT_EQ(StringUtils::Split(joined, " "), parts); 276 } 277 278 /* 279 * @tc.name: SplitRandomize 280 * @tc.desc: test StringUtils::Split with random input. 281 * @tc.type: FUNC 282 */ 283 HWTEST_F(StringUtilsTest, SplitRandomize, TestSize.Level1) 284 { 285 for (int i = 0; i < ROUNDS; i++) { 286 std::vector<std::string> parts; 287 for (int j = 0; j < STR_MAX_SIZE; j++) { 288 parts.push_back(RandomString(RandomInt(1, STR_MAX_SIZE))); 289 } 290 std::string joined; 291 for (auto& str : parts) { 292 joined += str; 293 joined += " "; 294 } 295 EXPECT_EQ(StringUtils::Split(joined, " "), parts); 296 joined.pop_back(); 297 EXPECT_EQ(StringUtils::Split(joined, " "), parts); 298 } 299 } 300 } // namespace