From 6b1f8de43a10de54e31626429c73e55bf4dc564b Mon Sep 17 00:00:00 2001 From: Gabe Black Date: Wed, 10 Mar 2021 21:55:38 -0800 Subject: [PATCH] base: Stop using testing::internal:: in tests. The name strongly suggests that this namespace isn't for our use. Instead, use the new gtestLogOutput string stream, or in the debug test add an optional parameter to debugDumpFlags which lets us direct the output to a string stream we can inspect. Change-Id: I51d3c0ec42981b70736e7b3bbedfb49f82dc7f95 Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/42723 Reviewed-by: Gabe Black Reviewed-by: Daniel Carvalho Maintainer: Gabe Black Tested-by: kokoro --- src/base/debug.cc | 4 ++-- src/base/debug.hh | 3 ++- src/base/debug.test.cc | 16 ++++++++-------- src/base/socket.test.cc | 20 +++++--------------- src/base/stats/storage.test.cc | 7 +------ 5 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/base/debug.cc b/src/base/debug.cc index 925c16ff50..38eb25fbb5 100644 --- a/src/base/debug.cc +++ b/src/base/debug.cc @@ -181,7 +181,7 @@ clearDebugFlag(const char *string) } void -dumpDebugFlags() +dumpDebugFlags(std::ostream &os) { using namespace Debug; FlagsMap::iterator i = allFlags().begin(); @@ -189,6 +189,6 @@ dumpDebugFlags() for (; i != end; ++i) { SimpleFlag *f = dynamic_cast(i->second); if (f && f->enabled()) - cprintf("%s\n", f->name()); + ccprintf(os, "%s\n", f->name()); } } diff --git a/src/base/debug.hh b/src/base/debug.hh index be5fa364fd..38d92f9087 100644 --- a/src/base/debug.hh +++ b/src/base/debug.hh @@ -43,6 +43,7 @@ #define __BASE_DEBUG_HH__ #include +#include #include #include #include @@ -142,7 +143,7 @@ void setDebugFlag(const char *string); void clearDebugFlag(const char *string); -void dumpDebugFlags(); +void dumpDebugFlags(std::ostream &os=std::cout); /** * \def DTRACE(x) diff --git a/src/base/debug.test.cc b/src/base/debug.test.cc index f995a33767..22320d1a32 100644 --- a/src/base/debug.test.cc +++ b/src/base/debug.test.cc @@ -29,6 +29,7 @@ #include #include "base/debug.hh" +#include "base/gtest/logging.hh" /** Test assignment of names and descriptions. */ TEST(DebugFlagTest, NameDesc) @@ -51,12 +52,11 @@ TEST(DebugFlagTest, NameDesc) TEST(DebugFlagDeathTest, UniqueNames) { Debug::SimpleFlag flag("FlagUniqueNamesTest", "A"); - testing::internal::CaptureStderr(); + gtestLogOutput.str(""); EXPECT_ANY_THROW(Debug::SimpleFlag("FlagUniqueNamesTest", "B")); const std::string expected = "panic: panic condition !result.second " "occurred: Flag FlagUniqueNamesTest already defined!\n"; - std::string actual = testing::internal::GetCapturedStderr().substr(); - actual = actual.substr(actual.find(":", actual.find(":") + 1) + 2); + std::string actual = gtestLogOutput.str(); EXPECT_EQ(expected, actual); } @@ -266,9 +266,9 @@ TEST(DebugFlagTest, NoDumpDebugFlags) Debug::SimpleFlag flag("FlagDumpDebugFlagTest", ""); // Verify that the names of the enabled flags are printed - testing::internal::CaptureStdout(); + gtestLogOutput.str(""); dumpDebugFlags(); - std::string output = testing::internal::GetCapturedStdout(); + std::string output = gtestLogOutput.str(); EXPECT_EQ(output, ""); ASSERT_FALSE(flag.enabled()); } @@ -298,9 +298,9 @@ TEST(DebugFlagTest, DumpDebugFlags) compound_flag_b.enable(); // Verify that the names of the enabled flags are printed - testing::internal::CaptureStdout(); - dumpDebugFlags(); - std::string output = testing::internal::GetCapturedStdout(); + std::ostringstream os; + dumpDebugFlags(os); + std::string output = os.str(); EXPECT_EQ(output, "FlagDumpDebugFlagTestA\nFlagDumpDebugFlagTestC\n" \ "FlagDumpDebugFlagTestE\n"); } diff --git a/src/base/socket.test.cc b/src/base/socket.test.cc index 7262a025bf..7372911133 100644 --- a/src/base/socket.test.cc +++ b/src/base/socket.test.cc @@ -28,6 +28,7 @@ #include +#include "base/gtest/logging.hh" #include "base/socket.hh" #define TEST_PORT_1 7893 @@ -103,20 +104,10 @@ TEST(SocketTest, RelistenWithSameInstanceSamePort) /* * You cannot listen to another port if you are already listening to one. */ - testing::internal::CaptureStderr(); + gtestLogOutput.str(""); EXPECT_ANY_THROW(listen_socket.listen(TEST_PORT_1)); std::string expected = "panic: Socket already listening!\n"; - std::string actual = testing::internal::GetCapturedStderr().substr(); - - /* - * The GoogleExitLogger will output using the following: - * `std::cerr << loc.file << ":" << loc.line << ": " << s;` - * As we do not care about the file and line where the error originated - * (this may change, and it shouldn't break the test when this happens), - * we strip out the leading `:: ` (we simply remove everything - * prior to two characters after the second colon in the string). - */ - actual = actual.substr(actual.find(":", actual.find(":") + 1) + 2); + std::string actual = gtestLogOutput.str(); EXPECT_EQ(expected, actual); } @@ -128,12 +119,11 @@ TEST(SocketTest, RelistenWithSameInstanceDifferentPort) /* * You cannot listen to another port if you are already listening to one. */ - testing::internal::CaptureStderr(); + gtestLogOutput.str(""); EXPECT_ANY_THROW(listen_socket.listen(TEST_PORT_2)); std::string expected = "panic: Socket already listening!\n"; - std::string actual = testing::internal::GetCapturedStderr().substr(); - actual = actual.substr(actual.find(":", actual.find(":") + 1) + 2); + std::string actual = gtestLogOutput.str(); EXPECT_EQ(expected, actual); } diff --git a/src/base/stats/storage.test.cc b/src/base/stats/storage.test.cc index 3218438b4a..36f73a60c3 100644 --- a/src/base/stats/storage.test.cc +++ b/src/base/stats/storage.test.cc @@ -32,6 +32,7 @@ #include #include "base/gtest/cur_tick_fake.hh" +#include "base/gtest/logging.hh" #include "base/stats/storage.hh" // Instantiate the fake class to have a valid curTick of 0 @@ -272,9 +273,7 @@ TEST(StatsAvgStorTest, ZeroReset) /** Test that an assertion is thrown when bucket size is 0. */ TEST(StatsDistStorDeathTest, BucketSize0) { - testing::internal::CaptureStderr(); EXPECT_ANY_THROW(Stats::DistStor::Params params(0, 5, 0)); - testing::internal::GetCapturedStderr(); } #endif @@ -500,17 +499,13 @@ TEST(StatsDistStorTest, Reset) /** Test that an assertion is thrown when not enough buckets are provided. */ TEST(StatsHistStorDeathTest, NotEnoughBuckets0) { - testing::internal::CaptureStderr(); EXPECT_ANY_THROW(Stats::HistStor::Params params(0)); - testing::internal::GetCapturedStderr(); } /** Test that an assertion is thrown when not enough buckets are provided. */ TEST(StatsHistStorDeathTest, NotEnoughBuckets1) { - testing::internal::CaptureStderr(); EXPECT_ANY_THROW(Stats::HistStor::Params params(1)); - testing::internal::GetCapturedStderr(); } #endif