consistently skip death tests at runtime when using OpenMPI without exceptions

This commit is contained in:
Axel Kohlmeyer
2020-07-18 12:55:41 -04:00
parent bf5c1dbc9e
commit e0200cf3d7
4 changed files with 153 additions and 177 deletions

View File

@ -28,6 +28,12 @@
// 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;
#if defined(OMPI_MAJOR_VERSION)
const bool have_openmpi = true;
#else
const bool have_openmpi = false;
#endif
using LAMMPS_NS::utils::split_words; using LAMMPS_NS::utils::split_words;
namespace LAMMPS_NS { namespace LAMMPS_NS {
@ -35,11 +41,19 @@ using ::testing::MatchesRegex;
#define GETIDX(i) lmp->atom->map(i) #define GETIDX(i) lmp->atom->map(i)
#define TEST_FAILURE(...) \ #define TEST_FAILURE(errmsg, ...) \
if (Info::has_exceptions()) { \ if (Info::has_exceptions()) { \
ASSERT_ANY_THROW({__VA_ARGS__}); \ ::testing::internal::CaptureStdout(); \
} else { \ ASSERT_ANY_THROW({__VA_ARGS__}); \
ASSERT_DEATH({__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)); \
} \
} }
#define STRINGIFY(val) XSTR(val) #define STRINGIFY(val) XSTR(val)
@ -454,51 +468,26 @@ TEST_F(ResetIDsTest, DeleteAdd)
TEST_F(ResetIDsTest, DeathTests) TEST_F(ResetIDsTest, DeathTests)
{ {
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR: Illegal reset_mol_ids command.*", lmp->input->one("reset_mol_ids"););
TEST_FAILURE(lmp->input->one("reset_mol_ids");); TEST_FAILURE(".*ERROR: Illegal reset_mol_ids command.*",
auto mesg = ::testing::internal::GetCapturedStdout(); lmp->input->one("reset_mol_ids all offset 1 1"););
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal reset_mol_ids command.*")); TEST_FAILURE(".*ERROR: Illegal reset_mol_ids command.*",
lmp->input->one("reset_mol_ids all offset -2"););
TEST_FAILURE(".*ERROR on proc 0: Expected integer.*",
lmp->input->one("reset_mol_ids all offset xxx"););
TEST_FAILURE(".*ERROR on proc 0: Expected integer.*",
lmp->input->one("reset_mol_ids all compress yes single no offset xxx"););
TEST_FAILURE(".*ERROR: Illegal reset_mol_ids command.*",
lmp->input->one("reset_mol_ids all offset"););
TEST_FAILURE(".*ERROR: Illegal reset_mol_ids command.*",
lmp->input->one("reset_mol_ids all compress"););
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR: Illegal reset_mol_ids command.*",
TEST_FAILURE(lmp->input->one("reset_mol_ids all offset 1 1");); lmp->input->one("reset_mol_ids all compress xxx"););
mesg = ::testing::internal::GetCapturedStdout(); TEST_FAILURE(".*ERROR: Illegal reset_mol_ids command.*",
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal reset_mol_ids command.*")); lmp->input->one("reset_mol_ids all single"););
TEST_FAILURE(".*ERROR: Illegal reset_mol_ids command.*",
::testing::internal::CaptureStdout(); lmp->input->one("reset_mol_ids all single xxx"););
TEST_FAILURE(lmp->input->one("reset_mol_ids all offset -2"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal reset_mol_ids command.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("reset_mol_ids all offset xxx"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR on proc 0: Expected integer.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("reset_mol_ids all compress yes single no offset xxx"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR on proc 0: Expected integer.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("reset_mol_ids all offset"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal reset_mol_ids command.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("reset_mol_ids all compress"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal reset_mol_ids command.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("reset_mol_ids all compress xxx"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal reset_mol_ids command.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("reset_mol_ids all single"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal reset_mol_ids command.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("reset_mol_ids all single xxx"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal reset_mol_ids command.*"));
} }
TEST(ResetMolIds, CMDFail) TEST(ResetMolIds, CMDFail)
@ -511,26 +500,23 @@ TEST(ResetMolIds, CMDFail)
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD); lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR: Reset_mol_ids command before box is.*",
TEST_FAILURE(lmp->input->one("reset_mol_ids all");); lmp->input->one("reset_mol_ids all"););
auto mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Reset_mol_ids command before.*"));
::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("atom_modify id no"); lmp->input->one("atom_modify id no");
lmp->input->one("region box block 0 1 0 1 0 1"); lmp->input->one("region box block 0 1 0 1 0 1");
lmp->input->one("create_box 1 box"); lmp->input->one("create_box 1 box");
TEST_FAILURE(lmp->input->one("reset_mol_ids all");); if (!verbose) ::testing::internal::GetCapturedStdout();
mesg = ::testing::internal::GetCapturedStdout(); TEST_FAILURE(".*ERROR: Cannot use reset_mol_ids unless.*",
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Cannot use reset_mol_ids unl.*")); lmp->input->one("reset_mol_ids all"););
::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("clear"); lmp->input->one("clear");
lmp->input->one("region box block 0 1 0 1 0 1"); lmp->input->one("region box block 0 1 0 1 0 1");
lmp->input->one("create_box 1 box"); lmp->input->one("create_box 1 box");
TEST_FAILURE(lmp->input->one("reset_mol_ids all");); if (!verbose) ::testing::internal::GetCapturedStdout();
mesg = ::testing::internal::GetCapturedStdout(); TEST_FAILURE(".*ERROR: Can only use reset_mol_ids.*", lmp->input->one("reset_mol_ids all"););
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Can only use reset_mol_ids.*"));
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
delete lmp; delete lmp;
@ -544,6 +530,10 @@ 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())
std::cout << "Warning: using OpenMPI without exceptions. "
"Death tests will be skipped\n";
// handle arguments passed via environment variable // handle arguments passed via environment variable
if (const char *var = getenv("TEST_ARGS")) { if (const char *var = getenv("TEST_ARGS")) {
std::vector<std::string> env = split_words(var); std::vector<std::string> env = split_words(var);

View File

@ -30,6 +30,12 @@
// 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;
#if defined(OMPI_MAJOR_VERSION)
const bool have_openmpi = true;
#else
const bool have_openmpi = false;
#endif
using LAMMPS_NS::utils::split_words; using LAMMPS_NS::utils::split_words;
namespace LAMMPS_NS { namespace LAMMPS_NS {
@ -37,11 +43,19 @@ using ::testing::ExitedWithCode;
using ::testing::MatchesRegex; using ::testing::MatchesRegex;
using ::testing::StrEq; using ::testing::StrEq;
#define TEST_FAILURE(...) \ #define TEST_FAILURE(errmsg, ...) \
if (Info::has_exceptions()) { \ if (Info::has_exceptions()) { \
ASSERT_ANY_THROW({__VA_ARGS__}); \ ::testing::internal::CaptureStdout(); \
} else { \ ASSERT_ANY_THROW({__VA_ARGS__}); \
ASSERT_DEATH({__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 { class SimpleCommandsTest : public ::testing::Test {
@ -68,10 +82,7 @@ protected:
TEST_F(SimpleCommandsTest, UnknownCommand) TEST_F(SimpleCommandsTest, UnknownCommand)
{ {
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR: Unknown command.*", lmp->input->one("XXX one two"););
TEST_FAILURE(lmp->input->one("XXX one two three"););
auto mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Unknown command.*"));
} }
TEST_F(SimpleCommandsTest, Echo) TEST_F(SimpleCommandsTest, Echo)
@ -103,15 +114,8 @@ TEST_F(SimpleCommandsTest, Echo)
ASSERT_EQ(lmp->input->echo_screen, 0); ASSERT_EQ(lmp->input->echo_screen, 0);
ASSERT_EQ(lmp->input->echo_log, 1); ASSERT_EQ(lmp->input->echo_log, 1);
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR: Illegal echo command.*", lmp->input->one("echo"););
TEST_FAILURE(lmp->input->one("echo");); TEST_FAILURE(".*ERROR: Illegal echo command.*", lmp->input->one("echo xxx"););
auto mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex("^ERROR: Illegal echo command.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("echo xxx"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex("^ERROR: Illegal echo command.*"));
} }
TEST_F(SimpleCommandsTest, Log) TEST_F(SimpleCommandsTest, Log)
@ -154,24 +158,20 @@ TEST_F(SimpleCommandsTest, Log)
in.close(); in.close();
remove("simple_command_test.log"); remove("simple_command_test.log");
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR: Illegal log command.*", lmp->input->one("log"););
TEST_FAILURE(lmp->input->one("log"););
auto mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal log command.*"));
} }
TEST_F(SimpleCommandsTest, Quit) TEST_F(SimpleCommandsTest, Quit)
{ {
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
lmp->input->one("echo none"); lmp->input->one("echo none");
TEST_FAILURE(lmp->input->one("quit xxx");); ::testing::internal::GetCapturedStdout();
auto mesg = ::testing::internal::GetCapturedStdout(); TEST_FAILURE(".*ERROR: Expected integer .*", lmp->input->one("quit xxx"););
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Expected integer .*"));
#if !defined(OMPI_MAJOR_VERSION) // this stalls with OpenMPI. skip. // 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"), ExitedWithCode(0), "");
ASSERT_EXIT(lmp->input->one("quit 9"), ExitedWithCode(9), ""); ASSERT_EXIT(lmp->input->one("quit 9"), ExitedWithCode(9), "");
#endif
} }
TEST_F(SimpleCommandsTest, ResetTimestep) TEST_F(SimpleCommandsTest, ResetTimestep)
@ -188,25 +188,10 @@ TEST_F(SimpleCommandsTest, ResetTimestep)
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
ASSERT_EQ(lmp->update->ntimestep, 0); ASSERT_EQ(lmp->update->ntimestep, 0);
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR: Timestep must be >= 0.*", lmp->input->one("reset_timestep -10"););
TEST_FAILURE(lmp->input->one("reset_timestep -10");); TEST_FAILURE(".*ERROR: Illegal reset_timestep .*", lmp->input->one("reset_timestep"););
auto mesg = ::testing::internal::GetCapturedStdout(); TEST_FAILURE(".*ERROR: Illegal reset_timestep .*", lmp->input->one("reset_timestep 10 10"););
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Timestep must be >= 0.*")); TEST_FAILURE(".*ERROR: Expected integer .*", lmp->input->one("reset_timestep xxx"););
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("reset_timestep"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal reset_timestep .*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("reset_timestep 10 10"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal reset_timestep .*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("reset_timestep xxx"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Expected integer .*"));
} }
TEST_F(SimpleCommandsTest, Suffix) TEST_F(SimpleCommandsTest, Suffix)
@ -215,10 +200,8 @@ TEST_F(SimpleCommandsTest, Suffix)
ASSERT_EQ(lmp->suffix, nullptr); ASSERT_EQ(lmp->suffix, nullptr);
ASSERT_EQ(lmp->suffix2, nullptr); ASSERT_EQ(lmp->suffix2, nullptr);
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR: May only enable suffixes after defining one.*",
TEST_FAILURE(lmp->input->one("suffix on");); lmp->input->one("suffix on"););
auto mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: May only enable suffixes after defining one.*"));
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("suffix one"); lmp->input->one("suffix one");
@ -247,20 +230,9 @@ TEST_F(SimpleCommandsTest, Suffix)
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
ASSERT_EQ(lmp->suffix_enable, 1); ASSERT_EQ(lmp->suffix_enable, 1);
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR: Illegal suffix command.*", lmp->input->one("suffix"););
TEST_FAILURE(lmp->input->one("suffix");); TEST_FAILURE(".*ERROR: Illegal suffix command.*", lmp->input->one("suffix hybrid"););
mesg = ::testing::internal::GetCapturedStdout(); TEST_FAILURE(".*ERROR: Illegal suffix command.*", lmp->input->one("suffix hybrid one"););
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal suffix command.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("suffix hybrid"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal suffix command.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("suffix hybrid one"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal suffix command.*"));
} }
TEST_F(SimpleCommandsTest, Thermo) TEST_F(SimpleCommandsTest, Thermo)
@ -284,20 +256,9 @@ TEST_F(SimpleCommandsTest, Thermo)
ASSERT_EQ(lmp->output->thermo_every, 10); ASSERT_EQ(lmp->output->thermo_every, 10);
ASSERT_EQ(lmp->output->var_thermo, nullptr); ASSERT_EQ(lmp->output->var_thermo, nullptr);
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR: Illegal thermo command.*", lmp->input->one("thermo"););
TEST_FAILURE(lmp->input->one("thermo");); TEST_FAILURE(".*ERROR: Illegal thermo command.*", lmp->input->one("thermo -1"););
auto mesg = ::testing::internal::GetCapturedStdout(); TEST_FAILURE(".*ERROR: Expected integer.*", lmp->input->one("thermo xxx"););
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal thermo command.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("thermo -1"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal thermo command.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("thermo xxx"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Expected integer.*"));
} }
TEST_F(SimpleCommandsTest, TimeStep) TEST_F(SimpleCommandsTest, TimeStep)
@ -324,15 +285,8 @@ TEST_F(SimpleCommandsTest, TimeStep)
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
ASSERT_EQ(lmp->update->dt, -0.1); ASSERT_EQ(lmp->update->dt, -0.1);
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR: Illegal timestep command.*", lmp->input->one("timestep"););
TEST_FAILURE(lmp->input->one("timestep");); TEST_FAILURE(".*ERROR: Expected floating point.*", lmp->input->one("timestep xxx"););
auto mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal timestep command.*"));
::testing::internal::CaptureStdout();
TEST_FAILURE(lmp->input->one("timestep xxx"););
mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Expected floating point.*"));
} }
TEST_F(SimpleCommandsTest, Units) TEST_F(SimpleCommandsTest, Units)
@ -356,10 +310,7 @@ TEST_F(SimpleCommandsTest, Units)
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
ASSERT_THAT(lmp->update->unit_style, StrEq("lj")); ASSERT_THAT(lmp->update->unit_style, StrEq("lj"));
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR: Illegal units command.*", lmp->input->one("units unknown"););
TEST_FAILURE(lmp->input->one("units unknown"););
auto mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal units command.*"));
} }
} // namespace LAMMPS_NS } // namespace LAMMPS_NS
@ -368,6 +319,10 @@ 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())
std::cout << "Warning: using OpenMPI without exceptions. "
"Death tests will be skipped\n";
// handle arguments passed via environment variable // handle arguments passed via environment variable
if (const char *var = getenv("TEST_ARGS")) { if (const char *var = getenv("TEST_ARGS")) {
std::vector<std::string> env = split_words(var); std::vector<std::string> env = split_words(var);

View File

@ -23,17 +23,33 @@
#include <string> #include <string>
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
using testing::StartsWith;
using testing::MatchesRegex;
using testing::StrEq; using testing::StrEq;
using utils::sfgets; using utils::sfgets;
using utils::sfread; using utils::sfread;
using utils::split_words; using utils::split_words;
#define TEST_FAILURE(...) \ #if defined(OMPI_MAJOR_VERSION)
if (Info::has_exceptions()) { \ const bool have_openmpi = true;
ASSERT_ANY_THROW({__VA_ARGS__}); \ #else
} else { \ const bool have_openmpi = false;
ASSERT_DEATH({__VA_ARGS__}, ""); \ #endif
#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).
@ -99,13 +115,10 @@ TEST_F(FileOperationsTest, safe_fgets)
ASSERT_THAT(buf, StrEq("newline")); ASSERT_THAT(buf, StrEq("newline"));
memset(buf, 0, MAX_BUF_SIZE); memset(buf, 0, MAX_BUF_SIZE);
::testing::internal::CaptureStdout();
TEST_FAILURE( TEST_FAILURE(
".*ERROR on proc 0: Unexpected end of file while "
"reading file 'safe_file_read_test.txt'.*",
utils::sfgets(FLERR, buf, MAX_BUF_SIZE, fp, "safe_file_read_test.txt", lmp->error);); utils::sfgets(FLERR, buf, MAX_BUF_SIZE, fp, "safe_file_read_test.txt", lmp->error););
auto mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, StartsWith("ERROR on proc 0: Unexpected end of file while "
"reading file 'safe_file_read_test.txt'"));
fclose(fp); fclose(fp);
} }
@ -125,11 +138,9 @@ TEST_F(FileOperationsTest, safe_fread)
utils::sfread(FLERR, buf, 1, 10, fp, "safe_file_read_test.txt", nullptr); utils::sfread(FLERR, buf, 1, 10, fp, "safe_file_read_test.txt", nullptr);
ASSERT_THAT(buf, StrEq("two_lines\n")); ASSERT_THAT(buf, StrEq("two_lines\n"));
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR on proc 0: Unexpected end of file while "
TEST_FAILURE(utils::sfread(FLERR, buf, 1, 100, fp, "safe_file_read_test.txt", lmp->error);); "reading file 'safe_file_read_test.txt'.*",
auto mesg = ::testing::internal::GetCapturedStdout(); utils::sfread(FLERR, buf, 1, 100, fp, "safe_file_read_test.txt", lmp->error););
ASSERT_THAT(mesg, StartsWith("ERROR on proc 0: Unexpected end of file while "
"reading file 'safe_file_read_test.txt'"));
// short read but no error triggered due to passing a NULL pointer // short read but no error triggered due to passing a NULL pointer
memset(buf, 0, MAX_BUF_SIZE); memset(buf, 0, MAX_BUF_SIZE);
@ -166,6 +177,10 @@ 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())
std::cout << "Warning: using OpenMPI without exceptions. "
"Death tests will be skipped\n";
// handle arguments passed via environment variable // handle arguments passed via environment variable
if (const char *var = getenv("TEST_ARGS")) { if (const char *var = getenv("TEST_ARGS")) {
std::vector<std::string> env = split_words(var); std::vector<std::string> env = split_words(var);

View File

@ -35,15 +35,29 @@
#include <cstring> #include <cstring>
#include <mpi.h> #include <mpi.h>
#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(...) \ #define TEST_FAILURE(errmsg, ...) \
if (Info::has_exceptions()) { \ if (Info::has_exceptions()) { \
ASSERT_ANY_THROW({__VA_ARGS__}); \ ::testing::internal::CaptureStdout(); \
} else { \ ASSERT_ANY_THROW({__VA_ARGS__}); \
ASSERT_DEATH({__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).
@ -116,10 +130,8 @@ TEST_F(PotentialFileReaderTest, Sw_noconv)
lmp->input->one("units real"); lmp->input->one("units real");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
::testing::internal::CaptureStdout(); TEST_FAILURE(".*ERROR on proc.*potential.*requires metal units but real.*",
TEST_FAILURE(PotentialFileReader reader(lmp, "Si.sw", "Stillinger-Weber", utils::REAL2METAL);); PotentialFileReader reader(lmp, "Si.sw", "Stillinger-Weber", utils::REAL2METAL););
std::string mesg = ::testing::internal::GetCapturedStdout();
ASSERT_THAT(mesg, MatchesRegex(".*ERROR on proc.*potential.*requires metal units but real.*"));
} }
TEST_F(PotentialFileReaderTest, Comb) TEST_F(PotentialFileReaderTest, Comb)
@ -291,6 +303,10 @@ 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())
std::cout << "Warning: using OpenMPI without exceptions. "
"Death tests will be skipped\n";
// handle arguments passed via environment variable // handle arguments passed via environment variable
if (const char *var = getenv("TEST_ARGS")) { if (const char *var = getenv("TEST_ARGS")) {
std::vector<std::string> env = split_words(var); std::vector<std::string> env = split_words(var);