1 /* 2 * Copyright (c) 2022 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 "netstack_common_utils.h" 19 20 namespace OHOS { 21 namespace NetStack { 22 namespace CommonUtils { 23 namespace { 24 using namespace testing::ext; 25 static constexpr const char SPACE = ' '; 26 static constexpr const char *STATUS_LINE_COMMA = ","; 27 static constexpr const char *STATUS_LINE_SEP = " "; 28 static constexpr const size_t STATUS_LINE_ELEM_NUM = 2; 29 static constexpr const char *TEST_STR = "HOWDOYOUDO"; 30 static constexpr const char *TEST_LOWER_STR = "howdoyoudo"; 31 static constexpr const char *TEST_STR_SPLIT = "The,weather,is,fine,today"; 32 static constexpr const char *TEST_STR_THE = "The"; 33 static constexpr const char *TEST_STR_WEATHER = "weather"; 34 static constexpr const char *TEST_STR_IS = "is"; 35 static constexpr const char *TEST_STR_FINE = "fine"; 36 static constexpr const char *TEST_STR_TODAY = "today"; 37 static constexpr const uint32_t SUB_STR_SIZE = 5; 38 static constexpr const char *TEST_STR_STRIP = " The weather is fine today"; 39 static constexpr const char *STR_STRIP = "The weather is fine today"; 40 static constexpr const char *SPLIT_WITH_SIZE = "fine today"; 41 static constexpr const char *SPLIT_WITH_SIZE_FINE = "fine"; 42 static constexpr const char *SPLIT_WITH_SIZE_TODAY = "today"; 43 static constexpr const uint32_t SPLIT_SIZE = 2; 44 } // namespace 45 46 class NetStackCommonUtilsTest : public testing::Test { 47 public: SetUpTestCase()48 static void SetUpTestCase() {} 49 TearDownTestCase()50 static void TearDownTestCase() {} 51 SetUp()52 virtual void SetUp() {} 53 TearDown()54 virtual void TearDown() {} 55 }; 56 57 HWTEST_F(NetStackCommonUtilsTest, CommonUtils, TestSize.Level2) 58 { 59 std::vector<std::string> subStr = Split(TEST_STR_SPLIT, STATUS_LINE_COMMA); 60 EXPECT_STREQ(subStr[0].data(), TEST_STR_THE); 61 EXPECT_STREQ(subStr[1].data(), TEST_STR_WEATHER); 62 EXPECT_STREQ(subStr[2].data(), TEST_STR_IS); 63 EXPECT_STREQ(subStr[3].data(), TEST_STR_FINE); 64 EXPECT_STREQ(subStr[4].data(), TEST_STR_TODAY); 65 EXPECT_EQ(subStr.size(), SUB_STR_SIZE); 66 } 67 68 HWTEST_F(NetStackCommonUtilsTest, CommonUtils1, TestSize.Level2) 69 { 70 std::string str = TEST_STR_STRIP; 71 std::string subStr = Strip(str, SPACE); 72 EXPECT_STREQ(subStr.data(), STR_STRIP); 73 } 74 75 HWTEST_F(NetStackCommonUtilsTest, CommonUtils2, TestSize.Level2) 76 { 77 std::string strLower = ToLower(TEST_STR); 78 EXPECT_STREQ(strLower.data(), TEST_LOWER_STR); 79 } 80 81 HWTEST_F(NetStackCommonUtilsTest, CommonUtils3, TestSize.Level2) 82 { 83 std::vector<std::string> strList = Split(SPLIT_WITH_SIZE, STATUS_LINE_SEP, STATUS_LINE_ELEM_NUM); 84 85 EXPECT_STREQ(strList[0].data(), SPLIT_WITH_SIZE_FINE); 86 EXPECT_STREQ(strList[1].data(), SPLIT_WITH_SIZE_TODAY); 87 EXPECT_EQ(strList.size(), SPLIT_SIZE); 88 } 89 } // namespace CommonUtils 90 } // namespace NetStack 91 } // namespace OHOS