1 /*
2 * Copyright (c) 2021-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 "string_utils_test.h"
17 #include <climits>
18 #include <gtest/gtest.h>
19 #include <thread>
20 #include "auto_mutex.h"
21 #include "test_common.h"
22 #include "utils/string_utils.h"
23
24 using namespace OHOS::Global::Resource;
25 using namespace testing::ext;
26 using namespace OHOS::I18N;
27 namespace {
28 class StringUtilsTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31
32 static void TearDownTestCase(void);
33
34 void SetUp();
35
36 void TearDown();
37 };
38
SetUpTestCase(void)39 void StringUtilsTest::SetUpTestCase(void)
40 {
41 // step 1: input testsuit setup step
42 g_logLevel = LOG_DEBUG;
43 }
44
TearDownTestCase(void)45 void StringUtilsTest::TearDownTestCase(void)
46 {
47 // step 2: input testsuit teardown step
48 }
49
SetUp()50 void StringUtilsTest::SetUp()
51 {
52 // step 3: input testcase setup step
53 }
54
TearDown()55 void StringUtilsTest::TearDown()
56 {
57 // step 4: input testcase teardown step
58 }
59
60 /*
61 * @tc.name: StringUtilsFuncTest001
62 * @tc.desc: Test FormatString, none file case.
63 * @tc.type: FUNC
64 */
65 HWTEST_F(StringUtilsTest, StringUtilsFuncTest001, TestSize.Level1)
66 {
67 std::string result = FormatString("%d", 10001);
68 EXPECT_EQ("10001", result);
69
70 result = FormatString("I'm %s, I'm %d", "cici", 5);
71 EXPECT_EQ("I'm cici, I'm 5", result);
72 }
73
FuncMultiThread(int * num,Lock * lock)74 void FuncMultiThread(int* num, Lock* lock)
75 {
76 AutoMutex mtx(*lock);
77 auto tmp = (*num);
78 std::this_thread::sleep_for(std::chrono::milliseconds(1));
79 (*num) = tmp + 1;
80 }
81
TestThread(int * num,int threadNum,Lock * lock)82 void TestThread(int* num, int threadNum, Lock* lock)
83 {
84 std::vector<std::thread> threads;
85 for (int i = 0; i < threadNum; ++i) {
86 threads.push_back(std::thread(FuncMultiThread, num, lock));
87 }
88 for (auto &thread : threads) {
89 thread.join();
90 }
91 }
92
93 /*
94 * @tc.name: LockFuncTest001
95 * @tc.desc: Test lock, none file case.
96 * @tc.type: FUNC
97 */
98 HWTEST_F(StringUtilsTest, LockFuncTest001, TestSize.Level1)
99 {
100 int num = 0;
101 int threadNum = 3;
102 int result = num + threadNum;
103 Lock lock;
104 TestThread(&num, threadNum, &lock);
105 EXPECT_EQ(result, num);
106 }
107 }