gem5  v21.0.1.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
debug.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Daniel R. Carvalho
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <gtest/gtest.h>
30 
31 #include "base/debug.hh"
32 
34 TEST(DebugFlagTest, NameDesc)
35 {
36  Debug::SimpleFlag flag_a("FlagNameDescTestKidA", "Kid A");
37  EXPECT_EQ("FlagNameDescTestKidA", flag_a.name());
38  EXPECT_EQ("Kid A", flag_a.desc());
39 
40  Debug::SimpleFlag flag_b("FlagNameDescTestKidB", "Kid B");
41  EXPECT_EQ("FlagNameDescTestKidB", flag_b.name());
42  EXPECT_EQ("Kid B", flag_b.desc());
43 
44  Debug::CompoundFlag compound_flag("FlagNameDescTest", "Compound Flag",
45  {&flag_a, &flag_b});
46  EXPECT_EQ("FlagNameDescTest", compound_flag.name());
47  EXPECT_EQ("Compound Flag", compound_flag.desc());
48 }
49 
51 TEST(DebugFlagDeathTest, UniqueNames)
52 {
53  Debug::SimpleFlag flag("FlagUniqueNamesTest", "A");
54  testing::internal::CaptureStderr();
55  EXPECT_ANY_THROW(Debug::SimpleFlag("FlagUniqueNamesTest", "B"));
56  const std::string expected = "panic: panic condition !result.second "
57  "occurred: Flag FlagUniqueNamesTest already defined!\n";
58  std::string actual = testing::internal::GetCapturedStderr().substr();
59  actual = actual.substr(actual.find(":", actual.find(":") + 1) + 2);
60  EXPECT_EQ(expected, actual);
61 }
62 
64 TEST(DebugFlagTest, IsFormat)
65 {
66  Debug::SimpleFlag flag_a("FlagIsFormatTestA", "", true);
67  EXPECT_TRUE(flag_a.isFormat());
68  Debug::SimpleFlag flag_b("FlagIsFormatTestB", "", false);
69  EXPECT_FALSE(flag_b.isFormat());
70  Debug::SimpleFlag flag_c("FlagIsFormatTestC", "");
71  EXPECT_FALSE(flag_c.isFormat());
72 }
73 
75 TEST(DebugSimpleFlagTest, Enabled)
76 {
78  Debug::SimpleFlag flag("SimpleFlagEnabledTest", "");
79 
80  // By default flags are initialized disabled
81  ASSERT_FALSE(flag.enabled());
82 
83  // Flags must be globally enabled before individual flags are enabled
84  flag.enable();
85  ASSERT_FALSE(flag.enabled());
87  ASSERT_TRUE(flag.enabled());
88 
89  // Verify that the global enabler works
91  ASSERT_FALSE(flag.enabled());
93  ASSERT_TRUE(flag.enabled());
94 
95  // Test disabling the flag with global enabled
96  flag.disable();
97  ASSERT_FALSE(flag.enabled());
98 }
99 
104 TEST(DebugCompoundFlagTest, Enabled)
105 {
107  Debug::SimpleFlag flag_a("CompoundFlagEnabledTestKidA", "");
108  Debug::SimpleFlag flag_b("CompoundFlagEnabledTestKidB", "");
109  Debug::CompoundFlag flag("CompoundFlagEnabledTest", "",
110  {&flag_a, &flag_b});
111 
112  // By default flags are initialized disabled
113  ASSERT_FALSE(flag.enabled());
114 
115  // Flags must be globally enabled before individual flags are enabled
116  flag.enable();
117  ASSERT_FALSE(flag_a.enabled());
118  ASSERT_FALSE(flag_b.enabled());
119  ASSERT_FALSE(flag.enabled());
121  for (auto &kid : flag.kids()) {
122  ASSERT_TRUE(kid->enabled());
123  }
124  ASSERT_TRUE(flag_a.enabled());
125  ASSERT_TRUE(flag_b.enabled());
126  ASSERT_TRUE(flag.enabled());
127 
128  // Test disabling the flag with global enabled
129  flag.disable();
130  for (auto &kid : flag.kids()) {
131  ASSERT_FALSE(kid->enabled());
132  }
133  ASSERT_FALSE(flag_a.enabled());
134  ASSERT_FALSE(flag_b.enabled());
135  ASSERT_FALSE(flag.enabled());
136 }
137 
139 TEST(DebugFlagTest, ConversionOperator)
140 {
142  Debug::SimpleFlag flag("FlagConversionOperatorTest", "");
143 
144  ASSERT_EQ(flag, flag.enabled());
145  flag.enable();
146  ASSERT_EQ(flag, flag.enabled());
147  flag.disable();
148 }
149 
154 TEST(DebugCompoundFlagTest, EnabledKids)
155 {
157  Debug::SimpleFlag flag_a("CompoundFlagEnabledKidsTestKidA", "");
158  Debug::SimpleFlag flag_b("CompoundFlagEnabledKidsTestKidB", "");
159  Debug::CompoundFlag flag("CompoundFlagEnabledKidsTest", "",
160  {&flag_a, &flag_b});
161 
162  // Test enabling only flag A
163  ASSERT_FALSE(flag_a.enabled());
164  ASSERT_FALSE(flag_b.enabled());
165  ASSERT_FALSE(flag.enabled());
166  flag_a.enable();
167  ASSERT_TRUE(flag_a.enabled());
168  ASSERT_FALSE(flag_b.enabled());
169  ASSERT_FALSE(flag.enabled());
170 
171  // Test that enabling both flags enables the compound flag
172  ASSERT_TRUE(flag_a.enabled());
173  ASSERT_FALSE(flag_b.enabled());
174  ASSERT_FALSE(flag.enabled());
175  flag_b.enable();
176  ASSERT_TRUE(flag_a.enabled());
177  ASSERT_TRUE(flag_b.enabled());
178  ASSERT_TRUE(flag.enabled());
179 
180  // Test that disabling one of the flags disables the compound flag
181  flag_a.disable();
182  ASSERT_FALSE(flag_a.enabled());
183  ASSERT_TRUE(flag_b.enabled());
184  ASSERT_FALSE(flag.enabled());
185 }
186 
188 TEST(DebugFlagTest, FindFlag)
189 {
191  Debug::SimpleFlag flag_a("FlagFindFlagTestA", "");
192  Debug::SimpleFlag flag_b("FlagFindFlagTestB", "");
193 
194  // Enable the found flags and verify that the original flags are
195  // enabled too
196  Debug::Flag *flag;
197  EXPECT_TRUE(flag = Debug::findFlag("FlagFindFlagTestA"));
198  ASSERT_FALSE(flag_a.enabled());
199  flag->enable();
200  ASSERT_TRUE(flag_a.enabled());
201  EXPECT_TRUE(flag = Debug::findFlag("FlagFindFlagTestB"));
202  ASSERT_FALSE(flag_b.enabled());
203  flag->enable();
204  ASSERT_TRUE(flag_b.enabled());
205 
206  // Search for a non-existent flag
207  EXPECT_FALSE(Debug::findFlag("FlagFindFlagTestC"));
208 }
209 
211 TEST(DebugFlagTest, ChangeFlag)
212 {
214  Debug::SimpleFlag flag_a("FlagChangeFlagTestA", "");
215  Debug::SimpleFlag flag_b("FlagChangeFlagTestB", "");
216 
217  // Enable the found flags and verify that the original flags are
218  // enabled too
219  ASSERT_FALSE(flag_a.enabled());
220  EXPECT_TRUE(Debug::changeFlag("FlagChangeFlagTestA", true));
221  ASSERT_TRUE(flag_a.enabled());
222  EXPECT_TRUE(Debug::changeFlag("FlagChangeFlagTestA", false));
223  ASSERT_FALSE(flag_a.enabled());
224 
225  // Disable and enable a flag
226  ASSERT_FALSE(flag_b.enabled());
227  EXPECT_TRUE(Debug::changeFlag("FlagChangeFlagTestB", false));
228  ASSERT_FALSE(flag_b.enabled());
229  EXPECT_TRUE(Debug::changeFlag("FlagChangeFlagTestB", true));
230  ASSERT_TRUE(flag_b.enabled());
231 
232  // Change a non-existent flag
233  ASSERT_FALSE(Debug::changeFlag("FlagChangeFlagTestC", true));
234 }
235 
237 TEST(DebugFlagTest, SetClearDebugFlag)
238 {
240  Debug::SimpleFlag flag_a("FlagSetClearDebugFlagTestA", "");
241  Debug::SimpleFlag flag_b("FlagSetClearDebugFlagTestB", "");
242 
243  // Enable and disable a flag
244  ASSERT_FALSE(flag_a.enabled());
245  setDebugFlag("FlagSetClearDebugFlagTestA");
246  ASSERT_TRUE(flag_a.enabled());
247  clearDebugFlag("FlagSetClearDebugFlagTestA");
248  ASSERT_FALSE(flag_a.enabled());
249 
250  // Disable and enable a flag
251  ASSERT_FALSE(flag_b.enabled());
252  clearDebugFlag("FlagSetClearDebugFlagTestB");
253  ASSERT_FALSE(flag_b.enabled());
254  setDebugFlag("FlagSetClearDebugFlagTestB");
255  ASSERT_TRUE(flag_b.enabled());
256 
257  // Change a non-existent flag
258  setDebugFlag("FlagSetClearDebugFlagTestC");
259  clearDebugFlag("FlagSetClearDebugFlagTestC");
260 }
261 
263 TEST(DebugFlagTest, NoDumpDebugFlags)
264 {
266  Debug::SimpleFlag flag("FlagDumpDebugFlagTest", "");
267 
268  // Verify that the names of the enabled flags are printed
269  testing::internal::CaptureStdout();
270  dumpDebugFlags();
271  std::string output = testing::internal::GetCapturedStdout();
272  EXPECT_EQ(output, "");
273  ASSERT_FALSE(flag.enabled());
274 }
275 
277 TEST(DebugFlagTest, DumpDebugFlags)
278 {
280  Debug::SimpleFlag flag_a("FlagDumpDebugFlagTestA", "");
281  Debug::SimpleFlag flag_b("FlagDumpDebugFlagTestB", "");
282  Debug::SimpleFlag flag_c("FlagDumpDebugFlagTestC", "");
283  Debug::SimpleFlag flag_d("FlagDumpDebugFlagTestD", "");
284  Debug::SimpleFlag flag_e("FlagDumpDebugFlagTestE", "");
285  Debug::CompoundFlag compound_flag_a("CompoundFlagDumpDebugFlagTestA", "",
286  {&flag_d});
287  Debug::CompoundFlag compound_flag_b("CompoundFlagDumpDebugFlagTestB", "",
288  {&flag_e});
289 
290  // Enable a few flags
291  ASSERT_FALSE(flag_a.enabled());
292  ASSERT_FALSE(flag_b.enabled());
293  ASSERT_FALSE(flag_c.enabled());
294  ASSERT_FALSE(flag_d.enabled());
295  ASSERT_FALSE(flag_e.enabled());
296  flag_a.enable();
297  flag_c.enable();
298  compound_flag_b.enable();
299 
300  // Verify that the names of the enabled flags are printed
301  testing::internal::CaptureStdout();
302  dumpDebugFlags();
303  std::string output = testing::internal::GetCapturedStdout();
304  EXPECT_EQ(output, "FlagDumpDebugFlagTestA\nFlagDumpDebugFlagTestC\n" \
305  "FlagDumpDebugFlagTestE\n");
306 }
output
static void output(const char *filename)
Definition: debug.cc:60
Debug::CompoundFlag
Definition: debug.hh:111
Debug::findFlag
Flag * findFlag(const std::string &name)
Definition: debug.cc:83
clearDebugFlag
void clearDebugFlag(const char *string)
Definition: debug.cc:178
Debug::Flag::desc
std::string desc() const
Definition: debug.hh:69
Debug::SimpleFlag::disable
void disable() override
Definition: debug.hh:100
Debug::SimpleFlag
Definition: debug.hh:81
Debug::SimpleFlag::enable
void enable() override
Definition: debug.hh:99
Debug::SimpleFlag::enabled
bool enabled() const override
Definition: debug.hh:97
dumpDebugFlags
void dumpDebugFlags()
Definition: debug.cc:184
debug.hh
Debug::Flag::globalEnable
static void globalEnable()
Definition: debug.cc:110
Debug::Flag
Definition: debug.hh:54
Debug::SimpleFlag::isFormat
bool isFormat() const
Checks whether this flag is a conventional debug flag, or a flag that modifies the way debug informat...
Definition: debug.hh:108
TEST
TEST(DebugFlagTest, NameDesc)
Test assignment of names and descriptions.
Definition: debug.test.cc:34
Debug::changeFlag
bool changeFlag(const char *s, bool value)
Definition: debug.cc:154
Debug::Flag::globalDisable
static void globalDisable()
Definition: debug.cc:118
Debug::Flag::name
std::string name() const
Definition: debug.hh:68
Debug::Flag::enable
virtual void enable()=0
setDebugFlag
void setDebugFlag(const char *string)
Definition: debug.cc:172
expected
std::vector< SwitchingFiber * > expected({ &a, &b, &a, &a, &a, &b, &c, &a, &c, &c, &c })

Generated on Tue Jun 22 2021 15:28:25 for gem5 by doxygen 1.8.17