gtestify profiledata_unittest

This commit is contained in:
Aliaksey Kandratsenka 2024-02-24 21:04:15 -05:00
parent bbae941492
commit 2940450c7a
3 changed files with 17 additions and 58 deletions

View File

@ -1290,7 +1290,7 @@ if(GPERFTOOLS_BUILD_CPU_PROFILER)
src/base/logging.h
src/base/threading.h
src/base/basictypes.h)
target_link_libraries(profiledata_unittest ${LIBPROFILER})
target_link_libraries(profiledata_unittest ${LIBPROFILER} gtest)
add_test(profiledata_unittest profiledata_unittest)
add_executable(profile_handler_unittest src/tests/profile-handler_unittest.cc

View File

@ -861,8 +861,9 @@ TESTS += getpc_test
getpc_test_SOURCES = src/tests/getpc_test.cc src/getpc.h
TESTS += profiledata_unittest
profiledata_unittest_SOURCES = src/tests/profiledata_unittest.cc
profiledata_unittest_LDADD = libprofiler.la
profiledata_unittest_SOURCES = src/tests/profiledata_unittest.cc src/profiledata.cc
profiledata_unittest_CPPFLAGS = $(gtest_CPPFLAGS)
profiledata_unittest_LDADD = libstacktrace.la libcommon.la libgtest.la
TESTS += profile_handler_unittest
profile_handler_unittest_SOURCES = src/tests/profile-handler_unittest.cc src/profile-handler.cc

View File

@ -44,12 +44,8 @@
#include "base/commandlineflags.h"
#include "base/logging.h"
#include "tests/legacy_assertions.h"
using std::string;
// Some helpful macros for the test class
#define TEST_F(cls, fn) void cls :: fn()
#include "gtest/gtest.h"
namespace {
@ -118,16 +114,16 @@ class ProfileDataChecker {
if (tmpdir == NULL)
tmpdir = "/tmp";
mkdir(tmpdir, 0755); // if necessary
filename_ = string(tmpdir) + "/profiledata_unittest.tmp";
filename_ = std::string(tmpdir) + "/profiledata_unittest.tmp";
}
string filename() const { return filename_; }
std::string filename() const { return filename_; }
// Checks the first 'num_slots' profile data slots in the file
// against the data pointed to by 'slots'. Returns kNoError if the
// data matched, otherwise returns an indication of the cause of the
// mismatch.
string Check(const ProfileDataSlot* slots, int num_slots) {
std::string Check(const ProfileDataSlot* slots, int num_slots) {
return CheckWithSkips(slots, num_slots, NULL, 0);
}
@ -142,8 +138,8 @@ class ProfileDataChecker {
//
// Returns kNoError if the data matched, otherwise returns an
// indication of the cause of the mismatch.
string CheckWithSkips(const ProfileDataSlot* slots, int num_slots,
const int* skips, int num_skips);
std::string CheckWithSkips(const ProfileDataSlot* slots, int num_slots,
const int* skips, int num_skips);
// Validate that a profile is correctly formed. The profile is
// assumed to have been created by the same kind of binary (e.g.,
@ -151,15 +147,15 @@ class ProfileDataChecker {
//
// Returns kNoError if the profile appears valid, otherwise returns
// an indication of the problem with the profile.
string ValidateProfile();
std::string ValidateProfile();
private:
string filename_;
std::string filename_;
};
string ProfileDataChecker::CheckWithSkips(const ProfileDataSlot* slots,
int num_slots, const int* skips,
int num_skips) {
std::string ProfileDataChecker::CheckWithSkips(const ProfileDataSlot* slots,
int num_slots, const int* skips,
int num_skips) {
FileDescriptor fd(open(filename_.c_str(), O_RDONLY));
if (fd.get() < 0)
return "file open error";
@ -182,7 +178,7 @@ string ProfileDataChecker::CheckWithSkips(const ProfileDataSlot* slots,
return kNoError;
}
string ProfileDataChecker::ValidateProfile() {
std::string ProfileDataChecker::ValidateProfile() {
FileDescriptor fd(open(filename_.c_str(), O_RDONLY));
if (fd.get() < 0)
return "file open error";
@ -311,7 +307,7 @@ string ProfileDataChecker::ValidateProfile() {
return kNoError;
}
class ProfileDataTest {
class ProfileDataTest : public testing::Test {
protected:
void ExpectStopped() {
EXPECT_FALSE(collector_.enabled());
@ -334,38 +330,6 @@ class ProfileDataTest {
ProfileData collector_;
ProfileDataChecker checker_;
private:
// The tests to run
void OpsWhenStopped();
void StartStopEmpty();
void StartStopNoOptionsEmpty();
void StartWhenStarted();
void StartStopEmpty2();
void CollectOne();
void CollectTwoMatching();
void CollectTwoFlush();
void StartResetRestart();
public:
#define RUN(test) do { \
printf("Running %s\n", #test); \
ProfileDataTest pdt; \
pdt.test(); \
} while (0)
static int RUN_ALL_TESTS() {
RUN(OpsWhenStopped);
RUN(StartStopEmpty);
RUN(StartWhenStarted);
RUN(StartStopEmpty2);
RUN(CollectOne);
RUN(CollectTwoMatching);
RUN(CollectTwoFlush);
RUN(StartResetRestart);
RUN(StartStopNoOptionsEmpty);
return 0;
}
};
// Check that various operations are safe when stopped.
@ -601,9 +565,3 @@ TEST_F(ProfileDataTest, StartResetRestart) {
}
} // namespace
int main(int argc, char** argv) {
int rc = ProfileDataTest::RUN_ALL_TESTS();
printf("%s\n", rc == 0 ? "PASS" : "FAIL");
return rc;
}