Merge pull request #2679 from akohlmey/more-unittests

Expand unit tests
This commit is contained in:
Axel Kohlmeyer
2021-04-02 17:51:29 -04:00
committed by GitHub
67 changed files with 7451 additions and 2485 deletions

View File

@ -24,6 +24,7 @@
#include "fmt/format.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "../testing/core.h"
#include <cstdio>
#include <cstring>
@ -34,11 +35,6 @@
// whether to print verbose output (i.e. not capturing LAMMPS screen output).
bool verbose = false;
#if defined(OMPI_MAJOR_VERSION)
const bool have_openmpi = true;
#else
const bool have_openmpi = false;
#endif
using LAMMPS_NS::utils::split_words;
@ -47,46 +43,12 @@ using ::testing::ExitedWithCode;
using ::testing::MatchesRegex;
using ::testing::StrEq;
#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)); \
} \
}
class SimpleCommandsTest : public ::testing::Test {
protected:
LAMMPS *lmp;
void SetUp() override
{
const char *args[] = {"SimpleCommandsTest", "-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();
}
class SimpleCommandsTest : public LAMMPSTest {
};
TEST_F(SimpleCommandsTest, UnknownCommand)
{
TEST_FAILURE(".*ERROR: Unknown command.*", lmp->input->one("XXX one two"););
TEST_FAILURE(".*ERROR: Unknown command.*", command("XXX one two"););
}
TEST_F(SimpleCommandsTest, Echo)
@ -94,47 +56,47 @@ TEST_F(SimpleCommandsTest, Echo)
ASSERT_EQ(lmp->input->echo_screen, 1);
ASSERT_EQ(lmp->input->echo_log, 0);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("echo none");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("echo none");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->input->echo_screen, 0);
ASSERT_EQ(lmp->input->echo_log, 0);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("echo both");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("echo both");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->input->echo_screen, 1);
ASSERT_EQ(lmp->input->echo_log, 1);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("echo screen");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("echo screen");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->input->echo_screen, 1);
ASSERT_EQ(lmp->input->echo_log, 0);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("echo log");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("echo log");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->input->echo_screen, 0);
ASSERT_EQ(lmp->input->echo_log, 1);
TEST_FAILURE(".*ERROR: Illegal echo command.*", lmp->input->one("echo"););
TEST_FAILURE(".*ERROR: Illegal echo command.*", lmp->input->one("echo xxx"););
TEST_FAILURE(".*ERROR: Illegal echo command.*", command("echo"););
TEST_FAILURE(".*ERROR: Illegal echo command.*", command("echo xxx"););
}
TEST_F(SimpleCommandsTest, Log)
{
ASSERT_EQ(lmp->logfile, nullptr);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("log simple_command_test.log");
lmp->input->one("print 'test1'");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("log simple_command_test.log");
command("print 'test1'");
END_HIDE_OUTPUT();
ASSERT_NE(lmp->logfile, nullptr);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("log none");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("log none");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->logfile, nullptr);
std::string text;
@ -144,14 +106,14 @@ TEST_F(SimpleCommandsTest, Log)
in.close();
ASSERT_THAT(text, StrEq("test1"));
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("log simple_command_test.log append");
lmp->input->one("print 'test2'");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("log simple_command_test.log append");
command("print 'test2'");
END_HIDE_OUTPUT();
ASSERT_NE(lmp->logfile, nullptr);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("log none");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("log none");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->logfile, nullptr);
in.open("simple_command_test.log");
@ -162,7 +124,7 @@ TEST_F(SimpleCommandsTest, Log)
in.close();
remove("simple_command_test.log");
TEST_FAILURE(".*ERROR: Illegal log command.*", lmp->input->one("log"););
TEST_FAILURE(".*ERROR: Illegal log command.*", command("log"););
}
TEST_F(SimpleCommandsTest, Newton)
@ -170,82 +132,79 @@ TEST_F(SimpleCommandsTest, Newton)
// default setting is "on" for both
ASSERT_EQ(lmp->force->newton_pair, 1);
ASSERT_EQ(lmp->force->newton_bond, 1);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("newton off");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("newton off");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->force->newton_pair, 0);
ASSERT_EQ(lmp->force->newton_bond, 0);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("newton on off");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("newton on off");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->force->newton_pair, 1);
ASSERT_EQ(lmp->force->newton_bond, 0);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("newton off on");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("newton off on");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->force->newton_pair, 0);
ASSERT_EQ(lmp->force->newton_bond, 1);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("newton on");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("newton on");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->force->newton_pair, 1);
ASSERT_EQ(lmp->force->newton_bond, 1);
}
TEST_F(SimpleCommandsTest, Partition)
{
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("echo none");
if (!verbose) ::testing::internal::GetCapturedStdout();
TEST_FAILURE(".*ERROR: Illegal partition command .*",
lmp->input->one("partition xxx 1 echo none"););
BEGIN_HIDE_OUTPUT();
command("echo none");
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Illegal partition command .*", command("partition xxx 1 echo none"););
TEST_FAILURE(".*ERROR: Numeric index 2 is out of bounds.*",
lmp->input->one("partition yes 2 echo none"););
command("partition yes 2 echo none"););
::testing::internal::CaptureStdout();
lmp->input->one("partition yes 1 print 'test'");
auto text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
BEGIN_CAPTURE_OUTPUT();
command("partition yes 1 print 'test'");
auto text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, StrEq("test\n"));
::testing::internal::CaptureStdout();
lmp->input->one("partition no 1 print 'test'");
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
BEGIN_CAPTURE_OUTPUT();
command("partition no 1 print 'test'");
text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, StrEq(""));
}
TEST_F(SimpleCommandsTest, Quit)
{
::testing::internal::CaptureStdout();
lmp->input->one("echo none");
::testing::internal::GetCapturedStdout();
TEST_FAILURE(".*ERROR: Expected integer .*", lmp->input->one("quit xxx"););
BEGIN_HIDE_OUTPUT();
command("echo none");
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Expected integer .*", command("quit xxx"););
// the following tests must be skipped with OpenMPI due to using threads
if (have_openmpi) GTEST_SKIP();
ASSERT_EXIT(lmp->input->one("quit"), ExitedWithCode(0), "");
ASSERT_EXIT(lmp->input->one("quit 9"), ExitedWithCode(9), "");
if (Info::get_mpi_vendor() == "Open MPI") GTEST_SKIP();
ASSERT_EXIT(command("quit"), ExitedWithCode(0), "");
ASSERT_EXIT(command("quit 9"), ExitedWithCode(9), "");
}
TEST_F(SimpleCommandsTest, ResetTimestep)
{
ASSERT_EQ(lmp->update->ntimestep, 0);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("reset_timestep 10");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("reset_timestep 10");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->update->ntimestep, 10);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("reset_timestep 0");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("reset_timestep 0");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->update->ntimestep, 0);
TEST_FAILURE(".*ERROR: Timestep must be >= 0.*", lmp->input->one("reset_timestep -10"););
TEST_FAILURE(".*ERROR: Illegal reset_timestep .*", lmp->input->one("reset_timestep"););
TEST_FAILURE(".*ERROR: Illegal reset_timestep .*", lmp->input->one("reset_timestep 10 10"););
TEST_FAILURE(".*ERROR: Expected integer .*", lmp->input->one("reset_timestep xxx"););
TEST_FAILURE(".*ERROR: Timestep must be >= 0.*", command("reset_timestep -10"););
TEST_FAILURE(".*ERROR: Illegal reset_timestep .*", command("reset_timestep"););
TEST_FAILURE(".*ERROR: Illegal reset_timestep .*", command("reset_timestep 10 10"););
TEST_FAILURE(".*ERROR: Expected integer .*", command("reset_timestep xxx"););
}
TEST_F(SimpleCommandsTest, Suffix)
@ -254,93 +213,92 @@ TEST_F(SimpleCommandsTest, Suffix)
ASSERT_EQ(lmp->suffix, nullptr);
ASSERT_EQ(lmp->suffix2, nullptr);
TEST_FAILURE(".*ERROR: May only enable suffixes after defining one.*",
lmp->input->one("suffix on"););
TEST_FAILURE(".*ERROR: May only enable suffixes after defining one.*", command("suffix on"););
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("suffix one");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("suffix one");
END_HIDE_OUTPUT();
ASSERT_THAT(lmp->suffix, StrEq("one"));
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("suffix hybrid two three");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("suffix hybrid two three");
END_HIDE_OUTPUT();
ASSERT_THAT(lmp->suffix, StrEq("two"));
ASSERT_THAT(lmp->suffix2, StrEq("three"));
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("suffix four");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("suffix four");
END_HIDE_OUTPUT();
ASSERT_THAT(lmp->suffix, StrEq("four"));
ASSERT_EQ(lmp->suffix2, nullptr);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("suffix off");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("suffix off");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->suffix_enable, 0);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("suffix on");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("suffix on");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->suffix_enable, 1);
TEST_FAILURE(".*ERROR: Illegal suffix command.*", lmp->input->one("suffix"););
TEST_FAILURE(".*ERROR: Illegal suffix command.*", lmp->input->one("suffix hybrid"););
TEST_FAILURE(".*ERROR: Illegal suffix command.*", lmp->input->one("suffix hybrid one"););
TEST_FAILURE(".*ERROR: Illegal suffix command.*", command("suffix"););
TEST_FAILURE(".*ERROR: Illegal suffix command.*", command("suffix hybrid"););
TEST_FAILURE(".*ERROR: Illegal suffix command.*", command("suffix hybrid one"););
}
TEST_F(SimpleCommandsTest, Thermo)
{
ASSERT_EQ(lmp->output->thermo_every, 0);
ASSERT_EQ(lmp->output->var_thermo, nullptr);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("thermo 2");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("thermo 2");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->output->thermo_every, 2);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("variable step equal logfreq(10,3,10)");
lmp->input->one("thermo v_step");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("variable step equal logfreq(10,3,10)");
command("thermo v_step");
END_HIDE_OUTPUT();
ASSERT_THAT(lmp->output->var_thermo, StrEq("step"));
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("thermo 10");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("thermo 10");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->output->thermo_every, 10);
ASSERT_EQ(lmp->output->var_thermo, nullptr);
TEST_FAILURE(".*ERROR: Illegal thermo command.*", lmp->input->one("thermo"););
TEST_FAILURE(".*ERROR: Illegal thermo command.*", lmp->input->one("thermo -1"););
TEST_FAILURE(".*ERROR: Expected integer.*", lmp->input->one("thermo xxx"););
TEST_FAILURE(".*ERROR: Illegal thermo command.*", command("thermo"););
TEST_FAILURE(".*ERROR: Illegal thermo command.*", command("thermo -1"););
TEST_FAILURE(".*ERROR: Expected integer.*", command("thermo xxx"););
}
TEST_F(SimpleCommandsTest, TimeStep)
{
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("timestep 1");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("timestep 1");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->update->dt, 1.0);
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("timestep 0.1");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("timestep 0.1");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->update->dt, 0.1);
// zero timestep is legal and works (atoms don't move)
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("timestep 0.0");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("timestep 0.0");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->update->dt, 0.0);
// negative timestep also creates a viable MD.
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("timestep -0.1");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("timestep -0.1");
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->update->dt, -0.1);
TEST_FAILURE(".*ERROR: Illegal timestep command.*", lmp->input->one("timestep"););
TEST_FAILURE(".*ERROR: Expected floating point.*", lmp->input->one("timestep xxx"););
TEST_FAILURE(".*ERROR: Illegal timestep command.*", command("timestep"););
TEST_FAILURE(".*ERROR: Expected floating point.*", command("timestep xxx"););
}
TEST_F(SimpleCommandsTest, Units)
@ -352,19 +310,19 @@ TEST_F(SimpleCommandsTest, Units)
ASSERT_THAT(lmp->update->unit_style, StrEq("lj"));
for (std::size_t i = 0; i < num; ++i) {
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one(fmt::format("units {}", names[i]));
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command(fmt::format("units {}", names[i]));
END_HIDE_OUTPUT();
ASSERT_THAT(lmp->update->unit_style, StrEq(names[i]));
ASSERT_EQ(lmp->update->dt, dt[i]);
}
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("clear");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
END_HIDE_OUTPUT();
ASSERT_THAT(lmp->update->unit_style, StrEq("lj"));
TEST_FAILURE(".*ERROR: Illegal units command.*", lmp->input->one("units unknown"););
TEST_FAILURE(".*ERROR: Illegal units command.*", command("units unknown"););
}
#if defined(LMP_PLUGIN)
@ -442,18 +400,18 @@ TEST_F(SimpleCommandsTest, Plugin)
TEST_F(SimpleCommandsTest, Shell)
{
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("shell putenv TEST_VARIABLE=simpletest");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("shell putenv TEST_VARIABLE=simpletest");
END_HIDE_OUTPUT();
char *test_var = getenv("TEST_VARIABLE");
ASSERT_NE(test_var, nullptr);
ASSERT_THAT(test_var, StrEq("simpletest"));
if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("shell putenv TEST_VARIABLE=simpletest");
lmp->input->one("shell putenv TEST_VARIABLE2=simpletest2 OTHER_VARIABLE=2");
if (!verbose) ::testing::internal::GetCapturedStdout();
BEGIN_HIDE_OUTPUT();
command("shell putenv TEST_VARIABLE=simpletest");
command("shell putenv TEST_VARIABLE2=simpletest2 OTHER_VARIABLE=2");
END_HIDE_OUTPUT();
char *test_var2 = getenv("TEST_VARIABLE2");
char *other_var = getenv("OTHER_VARIABLE");
@ -471,32 +429,30 @@ TEST_F(SimpleCommandsTest, CiteMe)
lmp->citeme = new LAMMPS_NS::CiteMe(lmp, CiteMe::TERSE, CiteMe::TERSE, nullptr);
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
lmp->citeme->add("test citation one:\n 1\n");
lmp->citeme->add("test citation two:\n 2\n");
lmp->citeme->add("test citation one:\n 1\n");
lmp->citeme->flush();
std::string text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
std::string text = END_CAPTURE_OUTPUT();
// find the two unique citations, but not the third
ASSERT_THAT(text, MatchesRegex(".*one.*two.*"));
ASSERT_THAT(text, Not(MatchesRegex(".*one.*two.*one.*")));
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
lmp->citeme->add("test citation one:\n 0\n");
lmp->citeme->add("test citation two:\n 2\n");
lmp->citeme->add("test citation three:\n 3\n");
lmp->citeme->flush();
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
text = END_CAPTURE_OUTPUT();
// find the forth (only differs in long citation) and sixth added citation
ASSERT_THAT(text, MatchesRegex(".*one.*three.*"));
ASSERT_THAT(text, Not(MatchesRegex(".*two.*")));
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
lmp->citeme->add("test citation one:\n 1\n");
lmp->citeme->add("test citation two:\n 2\n");
lmp->citeme->add("test citation one:\n 0\n");
@ -504,8 +460,7 @@ TEST_F(SimpleCommandsTest, CiteMe)
lmp->citeme->add("test citation three:\n 3\n");
lmp->citeme->flush();
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
text = END_CAPTURE_OUTPUT();
// no new citation. no CITE-CITE-CITE- lines
ASSERT_THAT(text, Not(MatchesRegex(".*CITE-CITE-CITE-CITE.*")));
@ -517,7 +472,7 @@ int main(int argc, char **argv)
MPI_Init(&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. "
"Death tests will be skipped\n";