Start refactoring tests

This commit is contained in:
Richard Berger
2021-03-26 11:50:58 -04:00
parent b1efdda75c
commit 664335420a
2 changed files with 107 additions and 88 deletions

View File

@ -28,37 +28,17 @@
#include "potential_file_reader.h" #include "potential_file_reader.h"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "../testing/core.h"
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <mpi.h> #include <mpi.h>
#include <vector> #include <vector>
#if defined(OMPI_MAJOR_VERSION)
const bool have_openmpi = true;
#else
const bool have_openmpi = false;
#endif
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
using ::testing::MatchesRegex; using ::testing::MatchesRegex;
using utils::split_words; using utils::split_words;
#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 (!have_openmpi) { \
::testing::internal::CaptureStdout(); \
ASSERT_DEATH({__VA_ARGS__}, ""); \
auto mesg = ::testing::internal::GetCapturedStdout(); \
ASSERT_THAT(mesg, MatchesRegex(errmsg)); \
} \
}
// whether to print verbose output (i.e. not capturing LAMMPS screen output). // whether to print verbose output (i.e. not capturing LAMMPS screen output).
bool verbose = false; bool verbose = false;
@ -75,38 +55,16 @@ const int LAMMPS_NS::PairNb3bHarmonic::NPARAMS_PER_LINE;
const int LAMMPS_NS::PairVashishta::NPARAMS_PER_LINE; const int LAMMPS_NS::PairVashishta::NPARAMS_PER_LINE;
const int LAMMPS_NS::PairTersoffTable::NPARAMS_PER_LINE; const int LAMMPS_NS::PairTersoffTable::NPARAMS_PER_LINE;
class PotentialFileReaderTest : public ::testing::Test { class PotentialFileReaderTest : public LAMMPSTest {
protected:
LAMMPS *lmp;
void SetUp() override
{
const char *args[] = {
"PotentialFileReaderTest", "-log", "none", "-echo", "screen", "-nocite"};
char **argv = (char **)args;
int argc = sizeof(args) / sizeof(char *);
if (!verbose) ::testing::internal::CaptureStdout();
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
if (!verbose) ::testing::internal::GetCapturedStdout();
}
void TearDown() override
{
if (!verbose) ::testing::internal::CaptureStdout();
delete lmp;
if (!verbose) ::testing::internal::GetCapturedStdout();
}
void command(const std::string &cmd) { lmp->input->one(cmd); }
}; };
// open for native units // open for native units
TEST_F(PotentialFileReaderTest, Sw_native) TEST_F(PotentialFileReaderTest, Sw_native)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units metal"); command("units metal");
PotentialFileReader reader(lmp, "Si.sw", "Stillinger-Weber"); PotentialFileReader reader(lmp, "Si.sw", "Stillinger-Weber");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairSW::NPARAMS_PER_LINE); auto line = reader.next_line(PairSW::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairSW::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairSW::NPARAMS_PER_LINE);
@ -115,10 +73,10 @@ TEST_F(PotentialFileReaderTest, Sw_native)
// open with supported conversion enabled // open with supported conversion enabled
TEST_F(PotentialFileReaderTest, Sw_conv) TEST_F(PotentialFileReaderTest, Sw_conv)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units real"); command("units real");
PotentialFileReader reader(lmp, "Si.sw", "Stillinger-Weber", utils::METAL2REAL); PotentialFileReader reader(lmp, "Si.sw", "Stillinger-Weber", utils::METAL2REAL);
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairSW::NPARAMS_PER_LINE); auto line = reader.next_line(PairSW::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairSW::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairSW::NPARAMS_PER_LINE);
@ -127,9 +85,9 @@ TEST_F(PotentialFileReaderTest, Sw_conv)
// open without conversion enabled // open without conversion enabled
TEST_F(PotentialFileReaderTest, Sw_noconv) TEST_F(PotentialFileReaderTest, Sw_noconv)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units real"); command("units real");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR on proc.*potential.*requires metal units but real.*", TEST_FAILURE(".*ERROR on proc.*potential.*requires metal units but real.*",
PotentialFileReader reader(lmp, "Si.sw", "Stillinger-Weber", utils::REAL2METAL);); PotentialFileReader reader(lmp, "Si.sw", "Stillinger-Weber", utils::REAL2METAL););
@ -137,10 +95,10 @@ TEST_F(PotentialFileReaderTest, Sw_noconv)
TEST_F(PotentialFileReaderTest, Comb) TEST_F(PotentialFileReaderTest, Comb)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units metal"); command("units metal");
PotentialFileReader reader(lmp, "ffield.comb", "COMB"); PotentialFileReader reader(lmp, "ffield.comb", "COMB");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairComb::NPARAMS_PER_LINE); auto line = reader.next_line(PairComb::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairComb::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairComb::NPARAMS_PER_LINE);
@ -148,10 +106,10 @@ TEST_F(PotentialFileReaderTest, Comb)
TEST_F(PotentialFileReaderTest, Comb3) TEST_F(PotentialFileReaderTest, Comb3)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units metal"); command("units metal");
PotentialFileReader reader(lmp, "ffield.comb3", "COMB3"); PotentialFileReader reader(lmp, "ffield.comb3", "COMB3");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairComb3::NPARAMS_PER_LINE); auto line = reader.next_line(PairComb3::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairComb3::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairComb3::NPARAMS_PER_LINE);
@ -159,10 +117,10 @@ TEST_F(PotentialFileReaderTest, Comb3)
TEST_F(PotentialFileReaderTest, Tersoff) TEST_F(PotentialFileReaderTest, Tersoff)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units metal"); command("units metal");
PotentialFileReader reader(lmp, "Si.tersoff", "Tersoff"); PotentialFileReader reader(lmp, "Si.tersoff", "Tersoff");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairTersoff::NPARAMS_PER_LINE); auto line = reader.next_line(PairTersoff::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairTersoff::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairTersoff::NPARAMS_PER_LINE);
@ -170,10 +128,10 @@ TEST_F(PotentialFileReaderTest, Tersoff)
TEST_F(PotentialFileReaderTest, TersoffMod) TEST_F(PotentialFileReaderTest, TersoffMod)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units metal"); command("units metal");
PotentialFileReader reader(lmp, "Si.tersoff.mod", "Tersoff/Mod"); PotentialFileReader reader(lmp, "Si.tersoff.mod", "Tersoff/Mod");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairTersoffMOD::NPARAMS_PER_LINE); auto line = reader.next_line(PairTersoffMOD::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairTersoffMOD::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairTersoffMOD::NPARAMS_PER_LINE);
@ -181,10 +139,10 @@ TEST_F(PotentialFileReaderTest, TersoffMod)
TEST_F(PotentialFileReaderTest, TersoffModC) TEST_F(PotentialFileReaderTest, TersoffModC)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units metal"); command("units metal");
PotentialFileReader reader(lmp, "Si.tersoff.modc", "Tersoff/ModC"); PotentialFileReader reader(lmp, "Si.tersoff.modc", "Tersoff/ModC");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairTersoffMODC::NPARAMS_PER_LINE); auto line = reader.next_line(PairTersoffMODC::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairTersoffMODC::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairTersoffMODC::NPARAMS_PER_LINE);
@ -192,10 +150,10 @@ TEST_F(PotentialFileReaderTest, TersoffModC)
TEST_F(PotentialFileReaderTest, TersoffTable) TEST_F(PotentialFileReaderTest, TersoffTable)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units metal"); command("units metal");
PotentialFileReader reader(lmp, "Si.tersoff", "TersoffTable"); PotentialFileReader reader(lmp, "Si.tersoff", "TersoffTable");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairTersoffTable::NPARAMS_PER_LINE); auto line = reader.next_line(PairTersoffTable::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairTersoffTable::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairTersoffTable::NPARAMS_PER_LINE);
@ -203,10 +161,10 @@ TEST_F(PotentialFileReaderTest, TersoffTable)
TEST_F(PotentialFileReaderTest, TersoffZBL) TEST_F(PotentialFileReaderTest, TersoffZBL)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units metal"); command("units metal");
PotentialFileReader reader(lmp, "SiC.tersoff.zbl", "Tersoff/ZBL"); PotentialFileReader reader(lmp, "SiC.tersoff.zbl", "Tersoff/ZBL");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairTersoffZBL::NPARAMS_PER_LINE); auto line = reader.next_line(PairTersoffZBL::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairTersoffZBL::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairTersoffZBL::NPARAMS_PER_LINE);
@ -214,10 +172,10 @@ TEST_F(PotentialFileReaderTest, TersoffZBL)
TEST_F(PotentialFileReaderTest, GW) TEST_F(PotentialFileReaderTest, GW)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units metal"); command("units metal");
PotentialFileReader reader(lmp, "SiC.gw", "GW"); PotentialFileReader reader(lmp, "SiC.gw", "GW");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairGW::NPARAMS_PER_LINE); auto line = reader.next_line(PairGW::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairGW::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairGW::NPARAMS_PER_LINE);
@ -225,10 +183,10 @@ TEST_F(PotentialFileReaderTest, GW)
TEST_F(PotentialFileReaderTest, GWZBL) TEST_F(PotentialFileReaderTest, GWZBL)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units metal"); command("units metal");
PotentialFileReader reader(lmp, "SiC.gw.zbl", "GW/ZBL"); PotentialFileReader reader(lmp, "SiC.gw.zbl", "GW/ZBL");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairGWZBL::NPARAMS_PER_LINE); auto line = reader.next_line(PairGWZBL::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairGWZBL::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairGWZBL::NPARAMS_PER_LINE);
@ -236,10 +194,10 @@ TEST_F(PotentialFileReaderTest, GWZBL)
TEST_F(PotentialFileReaderTest, Nb3bHarmonic) TEST_F(PotentialFileReaderTest, Nb3bHarmonic)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units real"); command("units real");
PotentialFileReader reader(lmp, "MOH.nb3b.harmonic", "NB3B Harmonic"); PotentialFileReader reader(lmp, "MOH.nb3b.harmonic", "NB3B Harmonic");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairNb3bHarmonic::NPARAMS_PER_LINE); auto line = reader.next_line(PairNb3bHarmonic::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairNb3bHarmonic::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairNb3bHarmonic::NPARAMS_PER_LINE);
@ -247,10 +205,10 @@ TEST_F(PotentialFileReaderTest, Nb3bHarmonic)
TEST_F(PotentialFileReaderTest, Vashishta) TEST_F(PotentialFileReaderTest, Vashishta)
{ {
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units metal"); command("units metal");
PotentialFileReader reader(lmp, "SiC.vashishta", "Vashishta"); PotentialFileReader reader(lmp, "SiC.vashishta", "Vashishta");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
auto line = reader.next_line(PairVashishta::NPARAMS_PER_LINE); auto line = reader.next_line(PairVashishta::NPARAMS_PER_LINE);
ASSERT_EQ(utils::count_words(line), PairVashishta::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairVashishta::NPARAMS_PER_LINE);
@ -261,38 +219,38 @@ TEST_F(PotentialFileReaderTest, UnitConvert)
PotentialFileReader *reader; PotentialFileReader *reader;
int unit_convert, flag; int unit_convert, flag;
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
command("units metal"); command("units metal");
reader = new PotentialFileReader(lmp, "Si.sw", "Stillinger-Weber"); reader = new PotentialFileReader(lmp, "Si.sw", "Stillinger-Weber");
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
unit_convert = reader->get_unit_convert(); unit_convert = reader->get_unit_convert();
ASSERT_EQ(unit_convert, 0); ASSERT_EQ(unit_convert, 0);
delete reader; delete reader;
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
flag = utils::get_supported_conversions(utils::UNKNOWN); flag = utils::get_supported_conversions(utils::UNKNOWN);
reader = new PotentialFileReader(lmp, "Si.sw", "Stillinger-Weber", flag); reader = new PotentialFileReader(lmp, "Si.sw", "Stillinger-Weber", flag);
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
unit_convert = reader->get_unit_convert(); unit_convert = reader->get_unit_convert();
ASSERT_EQ(unit_convert, 0); ASSERT_EQ(unit_convert, 0);
delete reader; delete reader;
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
flag = utils::get_supported_conversions(utils::ENERGY); flag = utils::get_supported_conversions(utils::ENERGY);
reader = new PotentialFileReader(lmp, "Si.sw", "Stillinger-Weber", flag); reader = new PotentialFileReader(lmp, "Si.sw", "Stillinger-Weber", flag);
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
unit_convert = reader->get_unit_convert(); unit_convert = reader->get_unit_convert();
ASSERT_EQ(unit_convert, 0); ASSERT_EQ(unit_convert, 0);
delete reader; delete reader;
if (!verbose) ::testing::internal::CaptureStdout(); BEGIN_HIDE_OUTPUT();
flag = utils::get_supported_conversions(utils::ENERGY); flag = utils::get_supported_conversions(utils::ENERGY);
command("units real"); command("units real");
reader = new PotentialFileReader(lmp, "Si.sw", "Stillinger-Weber", flag); reader = new PotentialFileReader(lmp, "Si.sw", "Stillinger-Weber", flag);
if (!verbose) ::testing::internal::GetCapturedStdout(); END_HIDE_OUTPUT();
unit_convert = reader->get_unit_convert(); unit_convert = reader->get_unit_convert();
ASSERT_EQ(unit_convert, utils::METAL2REAL); ASSERT_EQ(unit_convert, utils::METAL2REAL);
@ -304,7 +262,7 @@ int main(int argc, char **argv)
MPI_Init(&argc, &argv); MPI_Init(&argc, &argv);
::testing::InitGoogleMock(&argc, argv); ::testing::InitGoogleMock(&argc, argv);
if (have_openmpi && !LAMMPS_NS::Info::has_exceptions()) if (Info::get_mpi_vendor() == "Open MPI" && !LAMMPS_NS::Info::has_exceptions())
std::cout << "Warning: using OpenMPI without exceptions. " std::cout << "Warning: using OpenMPI without exceptions. "
"Death tests will be skipped\n"; "Death tests will be skipped\n";

View File

@ -16,8 +16,12 @@
#include "info.h" #include "info.h"
#include "input.h" #include "input.h"
#include "lammps.h" #include "lammps.h"
#include "variable.h"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "exceptions.h"
#include <functional>
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
@ -45,29 +49,86 @@ class LAMMPSTest : public ::testing::Test {
public: public:
void command(const std::string &line) { lmp->input->one(line.c_str()); } void command(const std::string &line) { lmp->input->one(line.c_str()); }
void BEGIN_HIDE_OUTPUT() {
if (!verbose) ::testing::internal::CaptureStdout();
}
void END_HIDE_OUTPUT() {
if (!verbose) ::testing::internal::GetCapturedStdout();
}
void BEGIN_CAPTURE_OUTPUT() {
if (!verbose) ::testing::internal::CaptureStdout();
}
std::string END_CAPTURE_OUTPUT() {
auto output = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << output;
return output;
}
void HIDE_OUTPUT(std::function<void()> f) {
if (!verbose) ::testing::internal::CaptureStdout();
try {
f();
} 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) {
::testing::internal::CaptureStdout();
try {
f();
} catch(LAMMPSException & e) {
if (verbose) std::cout << ::testing::internal::GetCapturedStdout();
throw e;
}
auto output = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << output;
return output;
}
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;
return value;
}
std::string get_variable_string(const std::string & name) {
return lmp->input->variable->retrieve(name.c_str());
}
protected: protected:
const char *testbinary = "LAMMPSTest"; const char *testbinary = "LAMMPSTest";
LAMMPS *lmp; LAMMPS *lmp;
Info *info;
void SetUp() override void SetUp() override
{ {
const char *args[] = {testbinary, "-log", "none", "-echo", "screen", "-nocite"}; const char *args[] = {testbinary, "-log", "none", "-echo", "screen", "-nocite"};
char **argv = (char **)args; char **argv = (char **)args;
int argc = sizeof(args) / sizeof(char *); int argc = sizeof(args) / sizeof(char *);
if (!verbose) ::testing::internal::CaptureStdout(); HIDE_OUTPUT([&] {
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD); lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
info = new Info(lmp);
});
InitSystem(); InitSystem();
if (!verbose) ::testing::internal::GetCapturedStdout();
} }
virtual void InitSystem() {} virtual void InitSystem() {}
void TearDown() override void TearDown() override
{ {
if (!verbose) ::testing::internal::CaptureStdout(); HIDE_OUTPUT([&] {
delete lmp; delete info;
lmp = nullptr; delete lmp;
if (!verbose) ::testing::internal::GetCapturedStdout(); info = nullptr;
lmp = nullptr;
});
} }
}; };