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 <gabe.black@gmail.com>
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
Maintainer: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Gabe Black
2021-03-10 21:55:38 -08:00
parent 56791f45fa
commit 6b1f8de43a
5 changed files with 18 additions and 32 deletions

View File

@@ -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<SimpleFlag *>(i->second);
if (f && f->enabled())
cprintf("%s\n", f->name());
ccprintf(os, "%s\n", f->name());
}
}

View File

@@ -43,6 +43,7 @@
#define __BASE_DEBUG_HH__
#include <initializer_list>
#include <iostream>
#include <map>
#include <string>
#include <vector>
@@ -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)

View File

@@ -29,6 +29,7 @@
#include <gtest/gtest.h>
#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");
}

View File

@@ -28,6 +28,7 @@
#include <gtest/gtest.h>
#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 `<file>:<line>: ` (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);
}

View File

@@ -32,6 +32,7 @@
#include <cmath>
#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