• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "common_components/log/log.h"
17 #include "common_components/log/log_base.h"
18 #include "common_components/tests/test_helper.h"
19 
20 using namespace common;
21 
22 // ==================== Test Fixture ====================
23 namespace common::test {
24 class LogTest : public common::test::BaseTestWithScope {
25 protected:
SetUp()26     void SetUp() override
27     {
28     }
29 
TearDown()30     void TearDown() override
31     {
32     }
33 };
34 
35 // ==================== Test Case ====================
HWTEST_F_L0(LogTest,ConvertFromRuntime_Info_ReturnsInfo)36 HWTEST_F_L0(LogTest, ConvertFromRuntime_Info_ReturnsInfo) {
37     Level result = Log::ConvertFromRuntime(LOG_LEVEL::INFO);
38     EXPECT_EQ(result, Level::INFO);
39 }
40 
41 
HWTEST_F_L0(LogTest,ConvertFromRuntime_Debug_ReturnsDebug)42 HWTEST_F_L0(LogTest, ConvertFromRuntime_Debug_ReturnsDebug) {
43     Level result = Log::ConvertFromRuntime(LOG_LEVEL::DEBUG);
44     EXPECT_EQ(result, Level::DEBUG);
45 }
46 
47 
HWTEST_F_L0(LogTest,ConvertFromRuntime_Fatal_ReturnsFatal)48 HWTEST_F_L0(LogTest, ConvertFromRuntime_Fatal_ReturnsFatal) {
49     Level result = Log::ConvertFromRuntime(LOG_LEVEL::FATAL);
50     EXPECT_EQ(result, Level::FATAL);
51 }
52 
53 
HWTEST_F_L0(LogTest,ConvertFromRuntime_Default_ReturnsDebug)54 HWTEST_F_L0(LogTest, ConvertFromRuntime_Default_ReturnsDebug) {
55     Level result = Log::ConvertFromRuntime(static_cast<LOG_LEVEL>(999));
56     EXPECT_EQ(result, Level::DEBUG);
57 }
58 
HWTEST_F_L0(LogTest,PrettyOrderMathNano)59 HWTEST_F_L0(LogTest, PrettyOrderMathNano) {
60     std::string result = PrettyOrderMathNano(1000000000000, "s");
61     EXPECT_EQ(result, "1000s");
62 }
63 }
64 
65 namespace common {
66 class TestLogRedirect {
67 public:
TestLogRedirect()68     TestLogRedirect()
69     {
70 #ifndef ENABLE_HILOG
71         originalCoutBuffer = std::cout.rdbuf();
72         originalCerrBuffer = std::cerr.rdbuf();
73 
74         std::cout.rdbuf(buffer.rdbuf());
75         std::cerr.rdbuf(buffer.rdbuf());
76 #endif
77     }
78 
~TestLogRedirect()79     ~TestLogRedirect()
80     {
81 #ifndef ENABLE_HILOG
82         std::cout.rdbuf(originalCoutBuffer);
83         std::cerr.rdbuf(originalCerrBuffer);
84 #endif
85     }
86 
GetOutput() const87     std::string GetOutput() const
88     {
89         return buffer.str();
90     }
91 
ClearOutput()92     void ClearOutput()
93     {
94         buffer.str(std::string());
95     }
96 
97 private:
98     std::stringstream buffer;
99 #ifndef ENABLE_HILOG
100     std::streambuf* originalCoutBuffer;
101     std::streambuf* originalCerrBuffer;
102 #endif
103 };
104 }  // namespace common
105 
106 namespace common::test {
107 class TimerTest : public common::test::BaseTestWithScope {
108 protected:
SetUp()109     void SetUp() override
110     {
111         redirect.ClearOutput();
112 
113         LogOptions options;
114         options.level = Level::DEBUG;
115         options.component = static_cast<ComponentMark>(Component::ALL);
116 
117         Log::Initialize(options);
118     }
TearDown()119     void TearDown() override {}
120 
121     TestLogRedirect redirect;
122 };
123 
124 static constexpr uint32_t SECOND_TIME = 1000000;
HWTEST_F_L0(TimerTest,Timer_BasicUsage_LogsTime)125 HWTEST_F_L0(TimerTest, Timer_BasicUsage_LogsTime)
126 {
127     {
128         Timer t("TestScope");
129         for (volatile int i = 0; i < SECOND_TIME; ++i);
130     }
131 
132 #ifndef ENABLE_HILOG
133     std::string output = redirect.GetOutput();
134     EXPECT_NE(output.find("TestScope time:"), std::string::npos);
135     EXPECT_NE(output.find("us"), std::string::npos);
136 #endif
137 }
138 
HWTEST_F_L0(TimerTest,Timer_LevelNotDebug_NoLogging)139 HWTEST_F_L0(TimerTest, Timer_LevelNotDebug_NoLogging)
140 {
141     LogOptions options;
142     options.level = Level::INFO;
143     options.component = static_cast<ComponentMark>(Component::ALL);
144     Log::Initialize(options);
145 
146     {
147         Timer t("SilentScope");
148         for (volatile int i = 0; i < SECOND_TIME; ++i);
149     }
150 
151     std::string output = redirect.GetOutput();
152     EXPECT_EQ(output.find("SilentScope"), std::string::npos);
153 }
154 
HWTEST_F_L0(TimerTest,Timer_LongName_CorrectFormat)155 HWTEST_F_L0(TimerTest, Timer_LongName_CorrectFormat)
156 {
157     {
158         Timer t("VeryLongTimerNameForTesting");
159         for (volatile int i = 0; i < SECOND_TIME; ++i);
160     }
161 
162 #ifndef ENABLE_HILOG
163     std::string output = redirect.GetOutput();
164     EXPECT_NE(output.find("VeryLongTimerNameForTesting time:"), std::string::npos);
165 #endif
166 }
167 
HWTEST_F_L0(TimerTest,Timer_MultipleInstances_DistinctOutput)168 HWTEST_F_L0(TimerTest, Timer_MultipleInstances_DistinctOutput)
169 {
170     {
171         Timer t1("First");
172         for (volatile int i = 0; i < SECOND_TIME; ++i);
173     }
174 
175     {
176         Timer t2("Second");
177         for (volatile int i = 0; i < SECOND_TIME; ++i);
178     }
179 
180 #ifndef ENABLE_HILOG
181     std::string output = redirect.GetOutput();
182     EXPECT_NE(output.find("First time:"), std::string::npos);
183     EXPECT_NE(output.find("Second time:"), std::string::npos);
184 #endif
185 }
186 }