diff --git a/unittest/commands/test_groups.cpp b/unittest/commands/test_groups.cpp index 3ae1722f4d..181f000a7c 100644 --- a/unittest/commands/test_groups.cpp +++ b/unittest/commands/test_groups.cpp @@ -22,6 +22,7 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "../testing/core.h" #include #include @@ -29,12 +30,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; namespace LAMMPS_NS { @@ -42,54 +37,22 @@ 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(); \ - if (verbose) std::cout << mesg; \ - ASSERT_THAT(mesg, MatchesRegex(errmsg)); \ - } else { \ - if (!have_openmpi) { \ - ::testing::internal::CaptureStdout(); \ - ASSERT_DEATH({__VA_ARGS__}, ""); \ - auto mesg = ::testing::internal::GetCapturedStdout(); \ - if (verbose) std::cout << mesg; \ - ASSERT_THAT(mesg, MatchesRegex(errmsg)); \ - } \ - } - -class GroupTest : public ::testing::Test { +class GroupTest : public LAMMPSTest { protected: - LAMMPS *lmp; Group *group; Domain *domain; void SetUp() override { - const char *args[] = {"GroupTest", "-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(); + testbinary = "GroupTest"; + LAMMPSTest::SetUp(); group = lmp->group; domain = lmp->domain; } - void TearDown() override - { - if (!verbose) ::testing::internal::CaptureStdout(); - delete lmp; - if (!verbose) ::testing::internal::GetCapturedStdout(); - std::cout.flush(); - } - - void command(const std::string &cmd) { lmp->input->one(cmd); } - void atomic_system() { - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("units real"); command("lattice sc 1.0 origin 0.125 0.125 0.125"); command("region box block -2 2 -2 2 -2 2"); @@ -101,23 +64,25 @@ protected: command("region top block INF INF -2.0 -1.0 INF INF"); command("set region left type 2"); command("set region right type 3"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); } void molecular_system() { - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("fix props all property/atom mol rmass q"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); + atomic_system(); - if (!verbose) ::testing::internal::CaptureStdout(); + + BEGIN_HIDE_OUTPUT(); command("variable molid atom floor(id/4)+1"); command("variable charge atom 2.0*sin(PI/32*id)"); command("set atom * mol v_molid"); command("set atom * charge v_charge"); command("set type 1 mass 0.5"); command("set type 2*4 mass 2.0"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); } }; @@ -131,7 +96,7 @@ TEST_F(GroupTest, EmptyDelete) { atomic_system(); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("group new1 empty"); command("group new2 empty"); command("group new2 empty"); @@ -143,16 +108,16 @@ TEST_F(GroupTest, EmptyDelete) command("compute 1 new3 ke"); command("dump 1 new4 atom 50 dump.melt"); command("atom_modify first new5"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->ngroup, 7); TEST_FAILURE(".*ERROR: Illegal group command.*", command("group new3 xxx");); TEST_FAILURE(".*ERROR: Illegal group command.*", command("group new3 empty xxx");); TEST_FAILURE(".*ERROR: Group command requires atom attribute molecule.*", command("group new2 include molecule");); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); group->assign("new1 delete"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->ngroup, 6); TEST_FAILURE(".*ERROR: Illegal group command.*", command("group new2 delete xxx");); @@ -172,13 +137,13 @@ TEST_F(GroupTest, RegionClear) { atomic_system(); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("group one region left"); command("group two region right"); command("group three empty"); command("group four region left"); command("group four region right"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->count(group->find("one")), 16); ASSERT_EQ(group->count(group->find("two")), 16); ASSERT_EQ(group->count(group->find("three")), 0); @@ -189,20 +154,20 @@ TEST_F(GroupTest, RegionClear) TEST_FAILURE(".*ERROR: Illegal group command.*", command("group three region left xxx");); TEST_FAILURE(".*ERROR: Group region ID does not exist.*", command("group four region dummy");); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("group one clear"); command("group two clear"); command("group three clear"); command("group four clear"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->count(group->find("one")), 0); ASSERT_EQ(group->count(group->find("two")), 0); ASSERT_EQ(group->count(group->find("three")), 0); ASSERT_EQ(group->count(group->find("four")), 0); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("delete_atoms region box"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->count(group->find("all")), 0); } @@ -214,7 +179,7 @@ TEST_F(GroupTest, SelectRestart) for (int i = 0; i < lmp->atom->natoms; ++i) flags[i] = i & 1; - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("group one region left"); command("group two region right"); group->create("half", flags); @@ -224,7 +189,7 @@ TEST_F(GroupTest, SelectRestart) command("group five subtract all half four"); command("group top region top"); command("group six intersect half top"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->count(group->find("one")), 16); ASSERT_EQ(group->count(group->find("two")), 16); ASSERT_EQ(group->count(group->find("three")), 0); @@ -235,12 +200,12 @@ TEST_F(GroupTest, SelectRestart) ASSERT_EQ(group->count(group->find("half"), domain->find_region("top")), 8); ASSERT_DOUBLE_EQ(group->mass(group->find("half"), domain->find_region("top")), 8.0); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("write_restart group.restart"); command("clear"); command("read_restart group.restart"); unlink("group.restart"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); group = lmp->group; ASSERT_EQ(group->count(group->find("one")), 16); ASSERT_EQ(group->count(group->find("two")), 16); @@ -250,11 +215,11 @@ TEST_F(GroupTest, SelectRestart) ASSERT_EQ(group->count(group->find("five")), 16); ASSERT_DOUBLE_EQ(group->mass(group->find("six")), 8.0); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("group four clear"); command("group five clear"); command("group six clear"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); TEST_FAILURE(".*ERROR: Group ID does not exist.*", command("group four union one two xxx");); TEST_FAILURE(".*ERROR: Group ID does not exist.*", @@ -267,14 +232,14 @@ TEST_F(GroupTest, Molecular) { molecular_system(); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("group one region left"); command("group two region right"); command("group half id 1:1000:2"); command("group top region top"); command("group three intersect half top"); command("group three include molecule"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->count(group->find("one")), 16); ASSERT_EQ(group->count(group->find("two")), 16); ASSERT_EQ(group->count(group->find("three")), 15); @@ -290,36 +255,36 @@ TEST_F(GroupTest, Dynamic) { atomic_system(); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("variable step atom id<=step"); command("group half id 1:1000:2"); command("group grow dynamic half var step every 1"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->count(group->find("grow")), 0); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("run 10 post no"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->count(group->find("grow")), 5); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("group grow dynamic half var step every 1"); command("run 10 post no"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->count(group->find("grow")), 10); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("group grow static"); command("run 10 post no"); command("group part variable step"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->count(group->find("grow")), 10); ASSERT_EQ(group->count(group->find("part")), 30); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("group grow dynamic half var step every 1"); command("run 10 post no"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->count(group->find("grow")), 20); TEST_FAILURE(".*ERROR: Cannot subtract groups using a dynamic group.*", command("group chunk subtract half grow");); @@ -328,10 +293,10 @@ TEST_F(GroupTest, Dynamic) TEST_FAILURE(".*ERROR: Cannot intersect groups using a dynamic group.*", command("group chunk intersect half grow");); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("group grow delete"); command("variable ramp equal step"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(group->ngroup, 4); TEST_FAILURE(".*ERROR: Group dynamic cannot reference itself.*", @@ -351,7 +316,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"; diff --git a/unittest/commands/test_simple_commands.cpp b/unittest/commands/test_simple_commands.cpp index 4261dbd61d..2ed5e7a989 100644 --- a/unittest/commands/test_simple_commands.cpp +++ b/unittest/commands/test_simple_commands.cpp @@ -24,6 +24,7 @@ #include "fmt/format.h" #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "../testing/core.h" #include #include @@ -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,43 +43,7 @@ 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(); - } - - void command(const std::string &cmd) { lmp->input->one(cmd); } +class SimpleCommandsTest : public LAMMPSTest { }; TEST_F(SimpleCommandsTest, UnknownCommand) @@ -96,27 +56,27 @@ TEST_F(SimpleCommandsTest, Echo) ASSERT_EQ(lmp->input->echo_screen, 1); ASSERT_EQ(lmp->input->echo_log, 0); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("echo none"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->input->echo_screen, 0); ASSERT_EQ(lmp->input->echo_log, 0); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("echo both"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->input->echo_screen, 1); ASSERT_EQ(lmp->input->echo_log, 1); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("echo screen"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->input->echo_screen, 1); ASSERT_EQ(lmp->input->echo_log, 0); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("echo log"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->input->echo_screen, 0); ASSERT_EQ(lmp->input->echo_log, 1); @@ -128,15 +88,15 @@ TEST_F(SimpleCommandsTest, Log) { ASSERT_EQ(lmp->logfile, nullptr); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("log simple_command_test.log"); command("print 'test1'"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_NE(lmp->logfile, nullptr); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("log none"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->logfile, nullptr); std::string text; @@ -146,14 +106,14 @@ TEST_F(SimpleCommandsTest, Log) in.close(); ASSERT_THAT(text, StrEq("test1")); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("log simple_command_test.log append"); command("print 'test2'"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_NE(lmp->logfile, nullptr); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("log none"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->logfile, nullptr); in.open("simple_command_test.log"); @@ -172,59 +132,57 @@ 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(); + BEGIN_HIDE_OUTPUT(); command("newton off"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->force->newton_pair, 0); ASSERT_EQ(lmp->force->newton_bond, 0); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("newton on off"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->force->newton_pair, 1); ASSERT_EQ(lmp->force->newton_bond, 0); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("newton off on"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->force->newton_pair, 0); ASSERT_EQ(lmp->force->newton_bond, 1); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("newton on"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + 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(); + BEGIN_HIDE_OUTPUT(); command("echo none"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + 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.*", command("partition yes 2 echo none");); - ::testing::internal::CaptureStdout(); + BEGIN_CAPTURE_OUTPUT(); command("partition yes 1 print 'test'"); - auto text = ::testing::internal::GetCapturedStdout(); - if (verbose) std::cout << text; + auto text = END_CAPTURE_OUTPUT(); ASSERT_THAT(text, StrEq("test\n")); - ::testing::internal::CaptureStdout(); + BEGIN_CAPTURE_OUTPUT(); command("partition no 1 print 'test'"); - text = ::testing::internal::GetCapturedStdout(); - if (verbose) std::cout << text; + text = END_CAPTURE_OUTPUT(); ASSERT_THAT(text, StrEq("")); } TEST_F(SimpleCommandsTest, Quit) { - ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("echo none"); - ::testing::internal::GetCapturedStdout(); + 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(); + if (Info::get_mpi_vendor() == "Open MPI") GTEST_SKIP(); ASSERT_EXIT(command("quit"), ExitedWithCode(0), ""); ASSERT_EXIT(command("quit 9"), ExitedWithCode(9), ""); } @@ -233,14 +191,14 @@ TEST_F(SimpleCommandsTest, ResetTimestep) { ASSERT_EQ(lmp->update->ntimestep, 0); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("reset_timestep 10"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->update->ntimestep, 10); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("reset_timestep 0"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->update->ntimestep, 0); TEST_FAILURE(".*ERROR: Timestep must be >= 0.*", command("reset_timestep -10");); @@ -257,31 +215,31 @@ TEST_F(SimpleCommandsTest, Suffix) TEST_FAILURE(".*ERROR: May only enable suffixes after defining one.*", command("suffix on");); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("suffix one"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_THAT(lmp->suffix, StrEq("one")); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("suffix hybrid two three"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_THAT(lmp->suffix, StrEq("two")); ASSERT_THAT(lmp->suffix2, StrEq("three")); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("suffix four"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_THAT(lmp->suffix, StrEq("four")); ASSERT_EQ(lmp->suffix2, nullptr); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("suffix off"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->suffix_enable, 0); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("suffix on"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->suffix_enable, 1); TEST_FAILURE(".*ERROR: Illegal suffix command.*", command("suffix");); @@ -293,20 +251,20 @@ TEST_F(SimpleCommandsTest, Thermo) { ASSERT_EQ(lmp->output->thermo_every, 0); ASSERT_EQ(lmp->output->var_thermo, nullptr); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("thermo 2"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->output->thermo_every, 2); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("variable step equal logfreq(10,3,10)"); command("thermo v_step"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_THAT(lmp->output->var_thermo, StrEq("step")); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("thermo 10"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->output->thermo_every, 10); ASSERT_EQ(lmp->output->var_thermo, nullptr); @@ -317,26 +275,26 @@ TEST_F(SimpleCommandsTest, Thermo) TEST_F(SimpleCommandsTest, TimeStep) { - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("timestep 1"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->update->dt, 1.0); - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("timestep 0.1"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + 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(); + BEGIN_HIDE_OUTPUT(); command("timestep 0.0"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->update->dt, 0.0); // negative timestep also creates a viable MD. - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("timestep -0.1"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_EQ(lmp->update->dt, -0.1); TEST_FAILURE(".*ERROR: Illegal timestep command.*", command("timestep");); @@ -352,16 +310,16 @@ 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(); + BEGIN_HIDE_OUTPUT(); command(fmt::format("units {}", names[i])); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_THAT(lmp->update->unit_style, StrEq(names[i])); ASSERT_EQ(lmp->update->dt, dt[i]); } - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("clear"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); ASSERT_THAT(lmp->update->unit_style, StrEq("lj")); TEST_FAILURE(".*ERROR: Illegal units command.*", command("units unknown");); @@ -369,18 +327,18 @@ TEST_F(SimpleCommandsTest, Units) TEST_F(SimpleCommandsTest, Shell) { - if (!verbose) ::testing::internal::CaptureStdout(); + BEGIN_HIDE_OUTPUT(); command("shell putenv TEST_VARIABLE=simpletest"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + 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(); + BEGIN_HIDE_OUTPUT(); command("shell putenv TEST_VARIABLE=simpletest"); command("shell putenv TEST_VARIABLE2=simpletest2 OTHER_VARIABLE=2"); - if (!verbose) ::testing::internal::GetCapturedStdout(); + END_HIDE_OUTPUT(); char *test_var2 = getenv("TEST_VARIABLE2"); char *other_var = getenv("OTHER_VARIABLE"); @@ -398,32 +356,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"); @@ -431,8 +387,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.*"))); @@ -444,7 +399,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"; diff --git a/unittest/testing/core.h b/unittest/testing/core.h index 19013bf90a..01c5872579 100644 --- a/unittest/testing/core.h +++ b/unittest/testing/core.h @@ -129,6 +129,7 @@ protected: info = nullptr; lmp = nullptr; }); + std::cout.flush(); } };