reformat unittest tree with clang-format
This commit is contained in:
@ -13,38 +13,37 @@
|
||||
#ifndef TESTING_CORE__H
|
||||
#define TESTING_CORE__H
|
||||
|
||||
#include "exceptions.h"
|
||||
#include "info.h"
|
||||
#include "input.h"
|
||||
#include "lammps.h"
|
||||
#include "variable.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "exceptions.h"
|
||||
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace LAMMPS_NS;
|
||||
|
||||
using ::testing::MatchesRegex;
|
||||
|
||||
#define TEST_FAILURE(errmsg, ...) \
|
||||
if (Info::has_exceptions()) { \
|
||||
::testing::internal::CaptureStdout(); \
|
||||
ASSERT_ANY_THROW({__VA_ARGS__}); \
|
||||
auto mesg = ::testing::internal::GetCapturedStdout(); \
|
||||
ASSERT_THAT(mesg, MatchesRegex(errmsg)); \
|
||||
} else { \
|
||||
if (Info::get_mpi_vendor() != "Open MPI") { \
|
||||
::testing::internal::CaptureStdout(); \
|
||||
ASSERT_DEATH({__VA_ARGS__}, ""); \
|
||||
auto mesg = ::testing::internal::GetCapturedStdout(); \
|
||||
ASSERT_THAT(mesg, MatchesRegex(errmsg)); \
|
||||
} \
|
||||
else { \
|
||||
std::cerr << "[ ] [ INFO ] Skipping death test (no exception support) \n"; \
|
||||
} \
|
||||
#define TEST_FAILURE(errmsg, ...) \
|
||||
if (Info::has_exceptions()) { \
|
||||
::testing::internal::CaptureStdout(); \
|
||||
ASSERT_ANY_THROW({__VA_ARGS__}); \
|
||||
auto mesg = ::testing::internal::GetCapturedStdout(); \
|
||||
ASSERT_THAT(mesg, MatchesRegex(errmsg)); \
|
||||
} else { \
|
||||
if (Info::get_mpi_vendor() != "Open MPI") { \
|
||||
::testing::internal::CaptureStdout(); \
|
||||
ASSERT_DEATH({__VA_ARGS__}, ""); \
|
||||
auto mesg = ::testing::internal::GetCapturedStdout(); \
|
||||
ASSERT_THAT(mesg, MatchesRegex(errmsg)); \
|
||||
} else { \
|
||||
std::cerr << "[ ] [ INFO ] Skipping death test (no exception support) \n"; \
|
||||
} \
|
||||
}
|
||||
|
||||
// whether to print verbose output (i.e. not capturing LAMMPS screen output).
|
||||
@ -54,40 +53,43 @@ class LAMMPSTest : public ::testing::Test {
|
||||
public:
|
||||
void command(const std::string &line) { lmp->input->one(line.c_str()); }
|
||||
|
||||
void BEGIN_HIDE_OUTPUT() {
|
||||
void BEGIN_HIDE_OUTPUT()
|
||||
{
|
||||
if (!verbose) ::testing::internal::CaptureStdout();
|
||||
}
|
||||
|
||||
void END_HIDE_OUTPUT() {
|
||||
void END_HIDE_OUTPUT()
|
||||
{
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
}
|
||||
|
||||
void BEGIN_CAPTURE_OUTPUT() {
|
||||
::testing::internal::CaptureStdout();
|
||||
}
|
||||
void BEGIN_CAPTURE_OUTPUT() { ::testing::internal::CaptureStdout(); }
|
||||
|
||||
std::string END_CAPTURE_OUTPUT() {
|
||||
std::string END_CAPTURE_OUTPUT()
|
||||
{
|
||||
auto output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
return output;
|
||||
}
|
||||
|
||||
void HIDE_OUTPUT(std::function<void()> f) {
|
||||
void HIDE_OUTPUT(std::function<void()> f)
|
||||
{
|
||||
if (!verbose) ::testing::internal::CaptureStdout();
|
||||
try {
|
||||
f();
|
||||
} catch(LAMMPSException & e) {
|
||||
} catch (LAMMPSException &e) {
|
||||
if (!verbose) std::cout << ::testing::internal::GetCapturedStdout();
|
||||
throw e;
|
||||
}
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
}
|
||||
|
||||
std::string CAPTURE_OUTPUT(std::function<void()> f) {
|
||||
std::string CAPTURE_OUTPUT(std::function<void()> f)
|
||||
{
|
||||
::testing::internal::CaptureStdout();
|
||||
try {
|
||||
f();
|
||||
} catch(LAMMPSException & e) {
|
||||
} catch (LAMMPSException &e) {
|
||||
if (verbose) std::cout << ::testing::internal::GetCapturedStdout();
|
||||
throw e;
|
||||
}
|
||||
@ -96,43 +98,45 @@ public:
|
||||
return output;
|
||||
}
|
||||
|
||||
double get_variable_value(const std::string & name) {
|
||||
char * str = utils::strdup(fmt::format("v_{}", name));
|
||||
double get_variable_value(const std::string &name)
|
||||
{
|
||||
char *str = utils::strdup(fmt::format("v_{}", name));
|
||||
double value = lmp->input->variable->compute_equal(str);
|
||||
delete [] str;
|
||||
delete[] str;
|
||||
return value;
|
||||
}
|
||||
|
||||
std::string get_variable_string(const std::string & name) {
|
||||
std::string get_variable_string(const std::string &name)
|
||||
{
|
||||
return lmp->input->variable->retrieve(name.c_str());
|
||||
}
|
||||
|
||||
protected:
|
||||
std::string testbinary = "LAMMPSTest";
|
||||
std::string testbinary = "LAMMPSTest";
|
||||
std::vector<std::string> args = {"-log", "none", "-echo", "screen", "-nocite"};
|
||||
LAMMPS *lmp;
|
||||
Info *info;
|
||||
|
||||
void SetUp() override
|
||||
{
|
||||
int argc = args.size() + 1;
|
||||
char ** argv = new char*[argc];
|
||||
argv[0] = utils::strdup(testbinary);
|
||||
for(int i = 1; i < argc; i++) {
|
||||
argv[i] = utils::strdup(args[i-1]);
|
||||
int argc = args.size() + 1;
|
||||
char **argv = new char *[argc];
|
||||
argv[0] = utils::strdup(testbinary);
|
||||
for (int i = 1; i < argc; i++) {
|
||||
argv[i] = utils::strdup(args[i - 1]);
|
||||
}
|
||||
|
||||
HIDE_OUTPUT([&] {
|
||||
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
|
||||
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
|
||||
info = new Info(lmp);
|
||||
});
|
||||
InitSystem();
|
||||
|
||||
for(int i = 0; i < argc; i++) {
|
||||
delete [] argv[i];
|
||||
for (int i = 0; i < argc; i++) {
|
||||
delete[] argv[i];
|
||||
argv[i] = nullptr;
|
||||
}
|
||||
delete [] argv;
|
||||
delete[] argv;
|
||||
}
|
||||
|
||||
virtual void InitSystem() {}
|
||||
@ -143,7 +147,7 @@ protected:
|
||||
delete info;
|
||||
delete lmp;
|
||||
info = nullptr;
|
||||
lmp = nullptr;
|
||||
lmp = nullptr;
|
||||
});
|
||||
std::cout.flush();
|
||||
}
|
||||
|
||||
@ -16,78 +16,87 @@
|
||||
#include <deque>
|
||||
#include <mpi.h>
|
||||
|
||||
using ::testing::TestEventListener;
|
||||
using ::testing::TestCase;
|
||||
using ::testing::TestEventListener;
|
||||
using ::testing::TestInfo;
|
||||
using ::testing::TestPartResult;
|
||||
using ::testing::TestSuite;
|
||||
using ::testing::UnitTest;
|
||||
using ::testing::TestPartResult;
|
||||
using ::testing::TestInfo;
|
||||
|
||||
class MPIPrinter : public TestEventListener {
|
||||
MPI_Comm comm;
|
||||
TestEventListener * default_listener;
|
||||
TestEventListener *default_listener;
|
||||
int me;
|
||||
int nprocs;
|
||||
char * buffer;
|
||||
char *buffer;
|
||||
size_t buffer_size;
|
||||
std::deque<TestPartResult> results;
|
||||
bool finalize_test;
|
||||
|
||||
public:
|
||||
MPIPrinter(TestEventListener * default_listener) : default_listener(default_listener) {
|
||||
MPIPrinter(TestEventListener *default_listener) : default_listener(default_listener)
|
||||
{
|
||||
comm = MPI_COMM_WORLD;
|
||||
MPI_Comm_rank(comm, &me);
|
||||
MPI_Comm_size(comm, &nprocs);
|
||||
buffer_size = 1024;
|
||||
buffer = new char[buffer_size];
|
||||
buffer_size = 1024;
|
||||
buffer = new char[buffer_size];
|
||||
finalize_test = false;
|
||||
}
|
||||
|
||||
~MPIPrinter() override {
|
||||
~MPIPrinter() override
|
||||
{
|
||||
delete default_listener;
|
||||
default_listener = nullptr;
|
||||
|
||||
delete [] buffer;
|
||||
buffer = nullptr;
|
||||
delete[] buffer;
|
||||
buffer = nullptr;
|
||||
buffer_size = 0;
|
||||
}
|
||||
|
||||
virtual void OnTestProgramStart(const UnitTest& unit_test) override {
|
||||
if(me == 0) default_listener->OnTestProgramStart(unit_test);
|
||||
virtual void OnTestProgramStart(const UnitTest &unit_test) override
|
||||
{
|
||||
if (me == 0) default_listener->OnTestProgramStart(unit_test);
|
||||
}
|
||||
|
||||
virtual void OnTestIterationStart(const UnitTest& unit_test, int iteration) override {
|
||||
if(me == 0) default_listener->OnTestIterationStart(unit_test, iteration);
|
||||
virtual void OnTestIterationStart(const UnitTest &unit_test, int iteration) override
|
||||
{
|
||||
if (me == 0) default_listener->OnTestIterationStart(unit_test, iteration);
|
||||
}
|
||||
|
||||
virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) override {
|
||||
if(me == 0) default_listener->OnEnvironmentsSetUpStart(unit_test);
|
||||
virtual void OnEnvironmentsSetUpStart(const UnitTest &unit_test) override
|
||||
{
|
||||
if (me == 0) default_listener->OnEnvironmentsSetUpStart(unit_test);
|
||||
}
|
||||
|
||||
virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) override {
|
||||
if(me == 0) default_listener->OnEnvironmentsSetUpEnd(unit_test);
|
||||
virtual void OnEnvironmentsSetUpEnd(const UnitTest &unit_test) override
|
||||
{
|
||||
if (me == 0) default_listener->OnEnvironmentsSetUpEnd(unit_test);
|
||||
}
|
||||
|
||||
virtual void OnTestSuiteStart(const TestSuite& test_suite) override {
|
||||
if(me == 0) default_listener->OnTestSuiteStart(test_suite);
|
||||
virtual void OnTestSuiteStart(const TestSuite &test_suite) override
|
||||
{
|
||||
if (me == 0) default_listener->OnTestSuiteStart(test_suite);
|
||||
}
|
||||
|
||||
// Legacy API is deprecated but still available
|
||||
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
|
||||
virtual void OnTestCaseStart(const TestCase& test_case) override {
|
||||
if(me == 0) default_listener->OnTestSuiteStart(test_case);
|
||||
virtual void OnTestCaseStart(const TestCase &test_case) override
|
||||
{
|
||||
if (me == 0) default_listener->OnTestSuiteStart(test_case);
|
||||
}
|
||||
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
|
||||
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
|
||||
|
||||
|
||||
virtual void OnTestStart(const TestInfo& test_info) override {
|
||||
virtual void OnTestStart(const TestInfo &test_info) override
|
||||
{
|
||||
// Called before a test starts.
|
||||
if(me == 0) default_listener->OnTestStart(test_info);
|
||||
if (me == 0) default_listener->OnTestStart(test_info);
|
||||
results.clear();
|
||||
finalize_test = false;
|
||||
}
|
||||
|
||||
|
||||
virtual void OnTestPartResult(const TestPartResult& test_part_result) override {
|
||||
virtual void OnTestPartResult(const TestPartResult &test_part_result) override
|
||||
{
|
||||
// Called after a failed assertion or a SUCCESS().
|
||||
// test_part_result()
|
||||
|
||||
@ -98,52 +107,55 @@ public:
|
||||
std::istringstream msg(test_part_result.message());
|
||||
std::string line;
|
||||
|
||||
while(std::getline(msg, line)) {
|
||||
while (std::getline(msg, line)) {
|
||||
proc_message << "[Rank " << me << "] " << line << std::endl;
|
||||
}
|
||||
|
||||
results.push_back(TestPartResult(test_part_result.type(), test_part_result.file_name(), test_part_result.line_number(), proc_message.str().c_str()));
|
||||
results.push_back(TestPartResult(test_part_result.type(), test_part_result.file_name(),
|
||||
test_part_result.line_number(),
|
||||
proc_message.str().c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
virtual void OnTestEnd(const TestInfo& test_info) override {
|
||||
virtual void OnTestEnd(const TestInfo &test_info) override
|
||||
{
|
||||
// Called after a test ends.
|
||||
MPI_Barrier(comm);
|
||||
|
||||
// other procs send their test part results
|
||||
if(me != 0) {
|
||||
if (me != 0) {
|
||||
int nresults = results.size();
|
||||
MPI_Send(&nresults, 1, MPI_INT, 0, 0, comm);
|
||||
|
||||
for(auto& test_part_result : results) {
|
||||
for (auto &test_part_result : results) {
|
||||
|
||||
int type = test_part_result.type();
|
||||
MPI_Send(&type, 1, MPI_INT, 0, 0, comm);
|
||||
|
||||
const char * str = test_part_result.file_name();
|
||||
int length = 0;
|
||||
if(str) length = strlen(str)+1;
|
||||
const char *str = test_part_result.file_name();
|
||||
int length = 0;
|
||||
if (str) length = strlen(str) + 1;
|
||||
MPI_Send(&length, 1, MPI_INT, 0, 0, comm);
|
||||
if(str) MPI_Send(str, length, MPI_CHAR, 0, 0, comm);
|
||||
if (str) MPI_Send(str, length, MPI_CHAR, 0, 0, comm);
|
||||
|
||||
int lineno = test_part_result.line_number();
|
||||
MPI_Send(&lineno, 1, MPI_INT, 0, 0, comm);
|
||||
|
||||
str = test_part_result.message();
|
||||
str = test_part_result.message();
|
||||
length = 0;
|
||||
if(str) length = strlen(str)+1;
|
||||
if (str) length = strlen(str) + 1;
|
||||
MPI_Send(&length, 1, MPI_INT, 0, 0, comm);
|
||||
if(str) MPI_Send(str, length, MPI_CHAR, 0, 0, comm);
|
||||
if (str) MPI_Send(str, length, MPI_CHAR, 0, 0, comm);
|
||||
}
|
||||
}
|
||||
|
||||
if(me == 0) {
|
||||
if (me == 0) {
|
||||
// collect results from other procs
|
||||
for(int p = 1; p < nprocs; p++) {
|
||||
for (int p = 1; p < nprocs; p++) {
|
||||
int nresults = 0;
|
||||
MPI_Recv(&nresults, 1, MPI_INT, p, 0, comm, MPI_STATUS_IGNORE);
|
||||
|
||||
for(int r = 0; r < nresults; r++) {
|
||||
for (int r = 0; r < nresults; r++) {
|
||||
|
||||
int type;
|
||||
MPI_Recv(&type, 1, MPI_INT, p, 0, comm, MPI_STATUS_IGNORE);
|
||||
@ -154,8 +166,8 @@ public:
|
||||
|
||||
if (length > 0) {
|
||||
if (length > buffer_size) {
|
||||
delete [] buffer;
|
||||
buffer = new char[length];
|
||||
delete[] buffer;
|
||||
buffer = new char[length];
|
||||
buffer_size = length;
|
||||
}
|
||||
MPI_Recv(buffer, length, MPI_CHAR, p, 0, comm, MPI_STATUS_IGNORE);
|
||||
@ -170,15 +182,16 @@ public:
|
||||
|
||||
if (length > 0) {
|
||||
if (length > buffer_size) {
|
||||
delete [] buffer;
|
||||
buffer = new char[length];
|
||||
delete[] buffer;
|
||||
buffer = new char[length];
|
||||
buffer_size = length;
|
||||
}
|
||||
MPI_Recv(buffer, length, MPI_CHAR, p, 0, comm, MPI_STATUS_IGNORE);
|
||||
message = std::string(buffer);
|
||||
}
|
||||
|
||||
results.push_back(TestPartResult((TestPartResult::Type)type, file_name.c_str(), lineno, message.c_str()));
|
||||
results.push_back(TestPartResult((TestPartResult::Type)type, file_name.c_str(),
|
||||
lineno, message.c_str()));
|
||||
}
|
||||
}
|
||||
|
||||
@ -186,9 +199,9 @@ public:
|
||||
finalize_test = true;
|
||||
|
||||
// add all failures
|
||||
while(!results.empty()) {
|
||||
while (!results.empty()) {
|
||||
auto result = results.front();
|
||||
if(result.failed()) {
|
||||
if (result.failed()) {
|
||||
ADD_FAILURE_AT(result.file_name(), result.line_number()) << result.message();
|
||||
} else {
|
||||
default_listener->OnTestPartResult(result);
|
||||
@ -200,29 +213,35 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
virtual void OnTestSuiteEnd(const TestSuite& test_suite) override {
|
||||
if(me == 0) default_listener->OnTestSuiteEnd(test_suite);
|
||||
virtual void OnTestSuiteEnd(const TestSuite &test_suite) override
|
||||
{
|
||||
if (me == 0) default_listener->OnTestSuiteEnd(test_suite);
|
||||
}
|
||||
|
||||
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
|
||||
virtual void OnTestCaseEnd(const TestCase& test_case) override {
|
||||
if(me == 0) default_listener->OnTestCaseEnd(test_case);
|
||||
virtual void OnTestCaseEnd(const TestCase &test_case) override
|
||||
{
|
||||
if (me == 0) default_listener->OnTestCaseEnd(test_case);
|
||||
}
|
||||
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
|
||||
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
|
||||
|
||||
virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) override {
|
||||
if(me == 0) default_listener->OnEnvironmentsTearDownStart(unit_test);
|
||||
virtual void OnEnvironmentsTearDownStart(const UnitTest &unit_test) override
|
||||
{
|
||||
if (me == 0) default_listener->OnEnvironmentsTearDownStart(unit_test);
|
||||
}
|
||||
|
||||
virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) override {
|
||||
if(me == 0) default_listener->OnEnvironmentsTearDownEnd(unit_test);
|
||||
virtual void OnEnvironmentsTearDownEnd(const UnitTest &unit_test) override
|
||||
{
|
||||
if (me == 0) default_listener->OnEnvironmentsTearDownEnd(unit_test);
|
||||
}
|
||||
|
||||
virtual void OnTestIterationEnd(const UnitTest& unit_test, int iteration) override {
|
||||
if(me == 0) default_listener->OnTestIterationEnd(unit_test, iteration);
|
||||
virtual void OnTestIterationEnd(const UnitTest &unit_test, int iteration) override
|
||||
{
|
||||
if (me == 0) default_listener->OnTestIterationEnd(unit_test, iteration);
|
||||
}
|
||||
|
||||
virtual void OnTestProgramEnd(const UnitTest& unit_test) override {
|
||||
if(me == 0) default_listener->OnTestProgramEnd(unit_test);
|
||||
virtual void OnTestProgramEnd(const UnitTest &unit_test) override
|
||||
{
|
||||
if (me == 0) default_listener->OnTestProgramEnd(unit_test);
|
||||
}
|
||||
};
|
||||
|
||||
@ -11,10 +11,10 @@
|
||||
See the README file in the top-level LAMMPS directory.
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
#include "mpitesting.h"
|
||||
#include "utils.h"
|
||||
#include "gmock/gmock.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "mpitesting.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <mpi.h>
|
||||
@ -53,7 +53,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
auto & listeners = UnitTest::GetInstance()->listeners();
|
||||
auto &listeners = UnitTest::GetInstance()->listeners();
|
||||
|
||||
// Remove default listener
|
||||
auto default_listener = listeners.Release(listeners.default_result_printer());
|
||||
|
||||
@ -71,6 +71,4 @@ static bool file_exists(const std::string &filename)
|
||||
#define ASSERT_FILE_NOT_EXISTS(NAME) ASSERT_FALSE(file_exists(NAME))
|
||||
#define ASSERT_FILE_EQUAL(FILE_A, FILE_B) ASSERT_TRUE(equal_lines(FILE_A, FILE_B))
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user