Refactoring of more tests

This commit is contained in:
Richard Berger
2021-03-29 14:27:42 -04:00
parent 1752bd0276
commit aaf9aa6d69
19 changed files with 627 additions and 847 deletions

View File

@ -22,6 +22,7 @@
#include "variable.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "../testing/core.h"
#include <cstdlib>
#include <mpi.h>
@ -29,57 +30,23 @@
// 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 {
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 KimCommandsTest : public ::testing::Test {
class KimCommandsTest : public LAMMPSTest {
protected:
LAMMPS *lmp;
Variable *variable;
void SetUp() override
{
const char *args[] = {"KimCommandsTest", "-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 = "KimCommandsTest";
LAMMPSTest::SetUp();
variable = lmp->input->variable;
}
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); }
};
TEST_F(KimCommandsTest, kim)
@ -115,9 +82,9 @@ TEST_F(KimCommandsTest, kim_init)
// TEST_FAILURE(".*ERROR: KIM Model does not support the requested unit system.*",
// command("kim init ex_model_Ar_P_Morse real"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("kim init LennardJones_Ar real");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
int ifix = lmp->modify->find_fix("KIM_MODEL_STORE");
ASSERT_GE(ifix, 0);
@ -129,81 +96,81 @@ TEST_F(KimCommandsTest, kim_interactions)
TEST_FAILURE(".*ERROR: Illegal 'kim interactions' command.*", command("kim interactions"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("kim init LennardJones_Ar real");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Must use 'kim interactions' command "
"after simulation box is defined.*",
command("kim interactions Ar"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("kim init LennardJones_Ar real");
command("lattice fcc 4.4300");
command("region box block 0 10 0 10 0 10");
command("create_box 1 box");
command("create_atoms 1 box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Illegal 'kim interactions' command.*",
command("kim interactions Ar Ar"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("lattice fcc 4.4300");
command("region box block 0 20 0 20 0 20");
command("create_box 4 box");
command("create_atoms 4 box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Illegal 'kim interactions' command.*",
command("kim interactions Ar Ar"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("lattice fcc 4.4300");
command("region box block 0 10 0 10 0 10");
command("create_box 1 box");
command("create_atoms 1 box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Must use 'kim init' before 'kim interactions'.*",
command("kim interactions Ar"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim init LennardJones_Ar real");
command("lattice fcc 4.4300");
command("region box block 0 10 0 10 0 10");
command("create_box 1 box");
command("create_atoms 1 box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: fixed_types cannot be used with a KIM Portable Model.*",
command("kim interactions fixed_types"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("units real");
command("pair_style kim LennardJones_Ar");
command("region box block 0 1 0 1 0 1");
command("create_box 4 box");
command("pair_coeff * * Ar Ar Ar Ar");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim init Sim_LAMMPS_LJcut_AkersonElliott_Alchemy_PbAu metal");
command("lattice fcc 4.920");
command("region box block 0 10 0 10 0 10");
command("create_box 1 box");
command("create_atoms 1 box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Species 'Ar' is not supported by this KIM Simulator Model.*",
command("kim interactions Ar"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim init Sim_LAMMPS_LJcut_AkersonElliott_Alchemy_PbAu metal");
command("lattice fcc 4.08");
@ -211,13 +178,13 @@ TEST_F(KimCommandsTest, kim_interactions)
command("create_box 1 box");
command("create_atoms 1 box");
command("kim interactions Au");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
// ASSERT_EQ(lmp->output->var_kim_periodic, 1);
// TEST_FAILURE(".*ERROR: Incompatible units for KIM Simulator Model.*",
// command("kim interactions Au"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim init LennardJones_Ar real");
command("lattice fcc 4.4300");
@ -226,12 +193,12 @@ TEST_F(KimCommandsTest, kim_interactions)
command("create_atoms 1 box");
command("kim interactions Ar");
command("mass 1 39.95");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
int ifix = lmp->modify->find_fix("KIM_MODEL_STORE");
ASSERT_GE(ifix, 0);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim init LennardJones_Ar real");
command("lattice fcc 4.4300");
@ -243,7 +210,7 @@ TEST_F(KimCommandsTest, kim_interactions)
command("run 1");
command("kim interactions Ar");
command("run 1");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
}
TEST_F(KimCommandsTest, kim_param)
@ -255,18 +222,18 @@ TEST_F(KimCommandsTest, kim_param)
"'kim param get/set' is mandatory.*",
command("kim param unknown shift 1 shift"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim init Sim_LAMMPS_LJcut_AkersonElliott_Alchemy_PbAu metal");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: 'kim param' can only be used with a KIM Portable Model.*",
command("kim param get shift 1 shift"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim init LennardJones612_UniversalShifted__MO_959249795837_003 real");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Illegal 'kim param get' command.\nTo get the new "
"parameter values, pair style must be assigned.\nMust use 'kim"
@ -278,7 +245,7 @@ TEST_F(KimCommandsTest, kim_param)
" interactions' or 'pair_style kim' before 'kim param set'.*",
command("kim param set shift 1 2"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim init LennardJones612_UniversalShifted__MO_959249795837_003 real");
command("lattice fcc 4.4300");
@ -287,7 +254,7 @@ TEST_F(KimCommandsTest, kim_param)
command("create_atoms 1 box");
command("kim interactions Ar");
command("mass 1 39.95");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Illegal index '0' for "
"'shift' parameter with the extent of '1'.*",
@ -312,9 +279,9 @@ TEST_F(KimCommandsTest, kim_param)
"Model does not have the requested 'unknown' parameter.*",
command("kim param get unknown 1 unknown"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("kim param get shift 1 shift");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_FALSE(variable->find("shift") == -1);
ASSERT_THAT(variable->retrieve("shift"), StrEq("1"));
@ -334,11 +301,11 @@ TEST_F(KimCommandsTest, kim_param)
"Model does not have the requested '0.4989030' parameter.*",
command("kim param set sigmas 1:1 0.5523570 0.4989030"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("variable new_shift equal 2");
command("kim param set shift 1 ${new_shift}");
command("kim param get shift 1 shift");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("shift"), StrEq("2"));
@ -363,51 +330,51 @@ TEST_F(KimCommandsTest, kim_param)
"split' is mandatory.*",
command("kim param get cutoffs 1:3 cutoffs_1 cutoffs_2"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("kim param get cutoffs 1:3 cutoffs_1 cutoffs_2 cutoffs_3");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("cutoffs_1"), StrEq("2.20943"));
ASSERT_THAT(variable->retrieve("cutoffs_2"), StrEq("2.10252"));
ASSERT_THAT(variable->retrieve("cutoffs_3"), StrEq("5.666115"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("kim param get cutoffs 1:3 cutoffs_1 cutoffs_2 cutoffs_3 explicit");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("cutoffs_1"), StrEq("2.20943"));
ASSERT_THAT(variable->retrieve("cutoffs_2"), StrEq("2.10252"));
ASSERT_THAT(variable->retrieve("cutoffs_3"), StrEq("5.666115"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("kim param get cutoffs 1:3 cutoffs split");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("cutoffs_1"), StrEq("2.20943"));
ASSERT_THAT(variable->retrieve("cutoffs_2"), StrEq("2.10252"));
ASSERT_THAT(variable->retrieve("cutoffs_3"), StrEq("5.666115"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("kim param get cutoffs 1:3 cutoffs list");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("cutoffs"), StrEq("2.20943 2.10252 5.666115"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("kim param set cutoffs 1 2.21 cutoffs 2 2.11");
command("kim param get cutoffs 1:2 cutoffs list");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("cutoffs"), StrEq("2.21 2.11"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("kim param set cutoffs 1:3 2.3 2.2 5.7");
command("kim param get cutoffs 1:3 cutoffs list");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("cutoffs"), StrEq("2.3 2.2 5.7"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("units real");
command("lattice fcc 4.4300");
@ -417,19 +384,19 @@ TEST_F(KimCommandsTest, kim_param)
command("mass 1 39.95");
command("pair_style kim LennardJones612_UniversalShifted__MO_959249795837_003");
command("pair_coeff * * Ar");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("kim param get shift 1 shift");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("shift"), StrEq("1"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("variable new_shift equal 2");
command("kim param set shift 1 ${new_shift}");
command("kim param get shift 1 shift");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("shift"), StrEq("2"));
}
@ -467,13 +434,13 @@ TEST_F(KimCommandsTest, kim_property)
command("kim property remove 1 key short-name"););
TEST_FAILURE(".*ERROR: There is no property instance to dump the content.*",
command("kim property dump results.edn"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim init LennardJones612_UniversalShifted__MO_959249795837_003 real");
command("kim property create 1 cohesive-potential-energy-cubic-crystal");
command("kim property modify 1 key short-name source-value 1 fcc");
command("kim property destroy 1");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
#endif
}
@ -608,16 +575,16 @@ TEST_F(KimCommandsTest, kim_query)
command(squery););
#if defined(KIM_EXTRA_UNITTESTS)
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim query latconst_1 get_lattice_constant_cubic "
"crystal=[fcc] species=[Al] units=[angstrom] "
"model=[EAM_Dynamo_ErcolessiAdams_1994_Al__MO_123629422045_005]");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("latconst_1"), StrEq("4.032082033157349"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim init EAM_Dynamo_ErcolessiAdams_1994_Al__MO_123629422045_005 metal");
command("kim query latconst_1 get_lattice_constant_cubic crystal=[fcc] species=[Al] "
@ -626,71 +593,71 @@ TEST_F(KimCommandsTest, kim_query)
command("kim query latconst_2 get_lattice_constant_cubic crystal=[fcc] species=[Al] "
"units=[angstrom] "
"model=[LennardJones612_UniversalShifted__MO_959249795837_003]");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("latconst_1"), StrEq("4.032082033157349"));
ASSERT_THAT(variable->retrieve("latconst_2"), StrEq("3.328125931322575"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim init EAM_Dynamo_MendelevAckland_2007v3_Zr__MO_004835508849_000 metal");
command("kim query latconst split get_lattice_constant_hexagonal crystal=[hcp] species=[Zr] "
"units=[angstrom]");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("latconst_1"), StrEq("3.234055244384789"));
ASSERT_THAT(variable->retrieve("latconst_2"), StrEq("5.167650199630013"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim query latconst index get_lattice_constant_hexagonal "
"crystal=[hcp] species=[Zr] units=[angstrom] "
"model=[EAM_Dynamo_MendelevAckland_2007v3_Zr__MO_004835508849_000]");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("latconst"), StrEq("3.234055244384789"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("variable latconst delete");
command("clear");
command("kim init EAM_Dynamo_MendelevAckland_2007v3_Zr__MO_004835508849_000 metal");
command("kim query latconst list get_lattice_constant_hexagonal crystal=[hcp] species=[Zr] "
"units=[angstrom]");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("latconst"), StrEq("3.234055244384789 5.167650199630013"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim init EAM_Dynamo_ErcolessiAdams_1994_Al__MO_123629422045_005 metal");
command("kim query alpha get_linear_thermal_expansion_coefficient_cubic "
"crystal=[fcc] species=[Al] units=[1/K] temperature=[293.15] "
"temperature_units=[K]");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_THAT(variable->retrieve("alpha"), StrEq("1.654960564704273e-05"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim query model_list list get_available_models species=[Al]");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
std::string model_list = variable->retrieve("model_list");
auto n = model_list.find("EAM_Dynamo_ErcolessiAdams_1994_Al__MO_123629422045_005");
ASSERT_TRUE(n != std::string::npos);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim query model_name index get_available_models species=[Al]");
command("variable model_name delete");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("clear");
command("kim query model_name index get_available_models "
@ -708,7 +675,7 @@ TEST_F(KimCommandsTest, kim_query)
command("kim query model_name index get_available_models "
"species=[Al] potential_type=[\"eam\",meam]");
command("variable model_name delete");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
#endif
}
} // namespace LAMMPS_NS
@ -718,7 +685,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";

View File

@ -22,6 +22,7 @@
#include "utils.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "../testing/core.h"
#include <cstdio>
#include <cstring>
@ -32,12 +33,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 {
@ -45,51 +40,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(); \
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 LatticeRegionTest : public ::testing::Test {
class LatticeRegionTest : public LAMMPSTest {
protected:
LAMMPS *lmp;
void SetUp() override
{
const char *args[] = {"LatticeRegionTest", "-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);
lmp->input->one("units metal");
if (!verbose) ::testing::internal::GetCapturedStdout();
testbinary = "LatticeRegionTest";
LAMMPSTest::SetUp();
command("units metal");
}
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); }
};
TEST_F(LatticeRegionTest, lattice_none)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice none 2.0");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
auto lattice = lmp->domain->lattice;
ASSERT_EQ(lattice->style, Lattice::NONE);
ASSERT_EQ(lattice->xlattice, 2.0);
@ -103,10 +69,10 @@ TEST_F(LatticeRegionTest, lattice_none)
TEST_FAILURE(".*ERROR: Illegal lattice command.*", command("lattice none 1.0 origin"););
TEST_FAILURE(".*ERROR: Expected floating point.*", command("lattice none xxx"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("units lj");
command("lattice none 1.0");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
lattice = lmp->domain->lattice;
ASSERT_EQ(lattice->xlattice, 1.0);
ASSERT_EQ(lattice->ylattice, 1.0);
@ -168,27 +134,27 @@ TEST_F(LatticeRegionTest, lattice_sc)
TEST_FAILURE(".*ERROR: Lattice spacings are invalid.*",
command("lattice sc 1.0 spacing 1.0 -0.1 1.0"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("units lj");
command("lattice sc 2.0");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
lattice = lmp->domain->lattice;
ASSERT_DOUBLE_EQ(lattice->xlattice, pow(0.5, 1.0 / 3.0));
ASSERT_DOUBLE_EQ(lattice->ylattice, pow(0.5, 1.0 / 3.0));
ASSERT_DOUBLE_EQ(lattice->zlattice, pow(0.5, 1.0 / 3.0));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("dimension 2");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Lattice style incompatible with simulation dimension.*",
command("lattice sc 1.0"););
}
TEST_F(LatticeRegionTest, lattice_bcc)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice bcc 4.2 orient x 1 1 0 orient y -1 1 0");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
auto lattice = lmp->domain->lattice;
ASSERT_EQ(lattice->style, Lattice::BCC);
ASSERT_DOUBLE_EQ(lattice->xlattice, sqrt(2.0) * 4.2);
@ -202,18 +168,18 @@ TEST_F(LatticeRegionTest, lattice_bcc)
ASSERT_EQ(lattice->basis[1][1], 0.5);
ASSERT_EQ(lattice->basis[1][2], 0.5);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("dimension 2");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Lattice style incompatible with simulation dimension.*",
command("lattice bcc 1.0"););
}
TEST_F(LatticeRegionTest, lattice_fcc)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice fcc 3.5 origin 0.5 0.5 0.5");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
auto lattice = lmp->domain->lattice;
ASSERT_EQ(lattice->style, Lattice::FCC);
ASSERT_DOUBLE_EQ(lattice->xlattice, 3.5);
@ -239,18 +205,18 @@ TEST_F(LatticeRegionTest, lattice_fcc)
command("lattice fcc 1.0 a1 0.0 1.0 0.0"););
TEST_FAILURE(".*ERROR: Illegal lattice command.*", command("lattice fcc 1.0 orient w 1 0 0"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("dimension 2");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Lattice style incompatible with simulation dimension.*",
command("lattice fcc 1.0"););
}
TEST_F(LatticeRegionTest, lattice_hcp)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice hcp 3.0 orient z 0 0 1");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
auto lattice = lmp->domain->lattice;
ASSERT_EQ(lattice->style, Lattice::HCP);
ASSERT_DOUBLE_EQ(lattice->xlattice, 3.0);
@ -283,18 +249,18 @@ TEST_F(LatticeRegionTest, lattice_hcp)
command("lattice hcp 1.0 a2 0.0 1.0 0.0"););
TEST_FAILURE(".*ERROR: Invalid option in lattice command for non-custom style.*",
command("lattice hcp 1.0 a3 0.0 1.0 0.0"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("dimension 2");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Lattice style incompatible with simulation dimension.*",
command("lattice hcp 1.0"););
}
TEST_F(LatticeRegionTest, lattice_diamond)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice diamond 4.1 orient x 1 1 2 orient y -1 1 0 orient z -1 -1 1");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
auto lattice = lmp->domain->lattice;
ASSERT_EQ(lattice->style, Lattice::DIAMOND);
ASSERT_DOUBLE_EQ(lattice->xlattice, 6.6952719636073539);
@ -335,19 +301,19 @@ TEST_F(LatticeRegionTest, lattice_diamond)
ASSERT_EQ(lattice->a3[1], 0.0);
ASSERT_EQ(lattice->a3[2], 1.0);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("dimension 2");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Lattice style incompatible with simulation dimension.*",
command("lattice diamond 1.0"););
}
TEST_F(LatticeRegionTest, lattice_sq)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("dimension 2");
command("lattice sq 3.0");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
auto lattice = lmp->domain->lattice;
ASSERT_EQ(lattice->style, Lattice::SQ);
ASSERT_DOUBLE_EQ(lattice->xlattice, 3.0);
@ -361,19 +327,19 @@ TEST_F(LatticeRegionTest, lattice_sq)
TEST_FAILURE(".*ERROR: Lattice settings are not compatible with 2d simulation.*",
command("lattice sq 1.0 orient x 1 1 2 orient y -1 1 0 orient z -1 -1 1"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("dimension 3");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Lattice style incompatible with simulation dimension.*",
command("lattice sq 1.0"););
}
TEST_F(LatticeRegionTest, lattice_sq2)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("dimension 2");
command("lattice sq2 2.0");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
auto lattice = lmp->domain->lattice;
ASSERT_EQ(lattice->style, Lattice::SQ2);
ASSERT_DOUBLE_EQ(lattice->xlattice, 2.0);
@ -387,19 +353,19 @@ TEST_F(LatticeRegionTest, lattice_sq2)
ASSERT_EQ(lattice->basis[1][1], 0.5);
ASSERT_EQ(lattice->basis[1][2], 0.0);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("dimension 3");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Lattice style incompatible with simulation dimension.*",
command("lattice sq2 1.0"););
}
TEST_F(LatticeRegionTest, lattice_hex)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("dimension 2");
command("lattice hex 2.0");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
auto lattice = lmp->domain->lattice;
ASSERT_EQ(lattice->style, Lattice::HEX);
ASSERT_DOUBLE_EQ(lattice->xlattice, 2.0);
@ -422,16 +388,16 @@ TEST_F(LatticeRegionTest, lattice_hex)
ASSERT_EQ(lattice->a3[1], 0.0);
ASSERT_EQ(lattice->a3[2], 1.0);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("dimension 3");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Lattice style incompatible with simulation dimension.*",
command("lattice hex 1.0"););
}
TEST_F(LatticeRegionTest, lattice_custom)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("variable a equal 4.34");
command("variable b equal $a*sqrt(3.0)");
command("variable c equal $a*sqrt(8.0/3.0)");
@ -449,7 +415,7 @@ TEST_F(LatticeRegionTest, lattice_custom)
"basis 0.5 0.5 0.625 "
"basis $t 0.0 0.125 "
"basis $f 0.5 0.125 ");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
auto lattice = lmp->domain->lattice;
ASSERT_EQ(lattice->style, Lattice::CUSTOM);
ASSERT_DOUBLE_EQ(lattice->xlattice, 4.34);
@ -495,9 +461,9 @@ TEST_F(LatticeRegionTest, lattice_custom)
TEST_FAILURE(".*ERROR: Illegal lattice command.*",
command("lattice custom 1.0 basis 0.0 1.0 0"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("dimension 2");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: No basis atoms in lattice.*", command("lattice custom 1.0"););
TEST_FAILURE(".*ERROR: Lattice settings are not compatible with 2d simulation.*",
command("lattice custom 1.0 origin 0.5 0.5 0.5 basis 0.0 0.0 0.0"););
@ -511,28 +477,28 @@ TEST_F(LatticeRegionTest, lattice_custom)
TEST_F(LatticeRegionTest, region_fail)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice none 2.0");
command("region box block 0 1 0 1 0 1");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Create_atoms command before simulation box is defined.*",
command("create_atoms 1 box"););
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("create_box 1 box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Cannot create atoms with undefined lattice.*",
command("create_atoms 1 box"););
}
TEST_F(LatticeRegionTest, region_block_lattice)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice sc 1.5");
command("region box block 0 2 0 2 0 2 units lattice");
command("create_box 1 box");
command("create_atoms 1 box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->domain->triclinic, 0);
auto x = lmp->atom->x;
@ -553,12 +519,12 @@ TEST_F(LatticeRegionTest, region_block_lattice)
TEST_F(LatticeRegionTest, region_block_box)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice sc 1.5 origin 0.75 0.75 0.75");
command("region box block 0 2 0 2 0 2 units box");
command("create_box 1 box");
command("create_atoms 1 box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->domain->triclinic, 0);
auto x = lmp->atom->x;
@ -570,84 +536,84 @@ TEST_F(LatticeRegionTest, region_block_box)
TEST_F(LatticeRegionTest, region_cone)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice fcc 2.5 origin 0.5 0.5 0.5");
command("region box cone x 1.0 1.0 0.5 2.1 0.0 2.0");
command("create_box 1 box");
command("create_atoms 1 region box");
command("write_dump all atom init.lammpstrj");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->domain->triclinic, 0);
ASSERT_EQ(lmp->atom->natoms, 42);
}
TEST_F(LatticeRegionTest, region_cylinder)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice fcc 2.5 origin 0.5 0.5 0.5");
command("region box cylinder z 1.0 1.0 2.1 0.0 2.0 ");
command("create_box 1 box");
command("create_atoms 1 region box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->domain->triclinic, 0);
ASSERT_EQ(lmp->atom->natoms, 114);
}
TEST_F(LatticeRegionTest, region_prism)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice bcc 2.5 origin 0.75 0.75 0.75");
command("region box prism 0 2 0 2 0 2 0.5 0.0 0.0");
command("create_box 1 box");
command("create_atoms 1 box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->domain->triclinic, 1);
ASSERT_EQ(lmp->atom->natoms, 16);
}
TEST_F(LatticeRegionTest, region_sphere)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice fcc 2.5 origin 0.5 0.5 0.5");
command("region box sphere 1.0 1.0 1.0 1.1");
command("create_box 1 box");
command("create_atoms 1 region box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->domain->triclinic, 0);
ASSERT_EQ(lmp->atom->natoms, 14);
}
TEST_F(LatticeRegionTest, region_union)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice fcc 2.5 origin 0.5 0.5 0.5");
command("region part1 sphere 2.0 1.0 1.0 1.1");
command("region part2 block 0.0 2.0 0.0 2.0 0.0 2.0");
command("region box union 2 part1 part2");
command("create_box 1 box");
command("create_atoms 1 region box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->domain->triclinic, 0);
ASSERT_EQ(lmp->atom->natoms, 67);
}
TEST_F(LatticeRegionTest, region_intersect)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice fcc 2.5 origin 0.5 0.5 0.5");
command("region part1 sphere 2.0 1.0 1.0 1.8");
command("region part2 block 0.0 2.0 0.0 2.0 0.0 2.0");
command("region box intersect 2 part1 part2");
command("create_box 1 box");
command("create_atoms 1 region box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->domain->triclinic, 0);
ASSERT_EQ(lmp->atom->natoms, 21);
}
TEST_F(LatticeRegionTest, region_plane)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("lattice fcc 2.5 origin 0.5 0.5 0.5");
command("region box block 0.0 2.0 0.0 2.0 0.0 2.0");
command("region part1 plane 0.5 1.0 0.0 0.75 0.0 0.0");
@ -656,7 +622,7 @@ TEST_F(LatticeRegionTest, region_plane)
command("create_box 1 box");
command("create_atoms 1 region atoms");
command("write_dump all atom init.lammpstrj");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->domain->triclinic, 0);
ASSERT_EQ(lmp->atom->natoms, 16);
}
@ -668,7 +634,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";

View File

@ -21,6 +21,7 @@
#include "utils.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "../testing/core.h"
#include <cstdio>
#include <mpi.h>
@ -28,12 +29,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 {
@ -41,52 +36,23 @@ using ::testing::MatchesRegex;
#define GETIDX(i) lmp->atom->map(i)
#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)); \
} \
}
#define STRINGIFY(val) XSTR(val)
#define XSTR(val) #val
class ResetIDsTest : public ::testing::Test {
class ResetIDsTest : public LAMMPSTest {
protected:
LAMMPS *lmp;
void SetUp() override
{
const char *args[] = {"ResetIDsTest", "-log", "none", "-nocite", "-echo", "screen"};
char **argv = (char **)args;
int argc = sizeof(args) / sizeof(char *);
if (!verbose) ::testing::internal::CaptureStdout();
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
Info *info = new Info(lmp);
testbinary = "ResetIDsTest";
LAMMPSTest::SetUp();
if (info->has_style("atom", "full")) {
lmp->input->one("variable input_dir index " STRINGIFY(TEST_INPUT_FOLDER));
lmp->input->one("include ${input_dir}/in.fourmol");
BEGIN_HIDE_OUTPUT();
command("variable input_dir index " STRINGIFY(TEST_INPUT_FOLDER));
command("include ${input_dir}/in.fourmol");
END_HIDE_OUTPUT();
}
delete info;
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); }
};
TEST_F(ResetIDsTest, MolIDAll)
@ -126,9 +92,9 @@ TEST_F(ResetIDsTest, MolIDAll)
// the original data file has two different molecule IDs
// for two residues of the same molecule/fragment.
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("reset_mol_ids all");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(molid[GETIDX(1)], 1);
ASSERT_EQ(molid[GETIDX(2)], 1);
@ -168,12 +134,12 @@ TEST_F(ResetIDsTest, DeletePlusAtomID)
auto molid = lmp->atom->molecule;
// delete two water molecules
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("group allwater molecule 3:6");
command("group twowater molecule 4:6:2");
command("delete_atoms group twowater compress no bond yes");
command("reset_mol_ids all");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->atom->natoms, 23);
ASSERT_EQ(lmp->atom->map_tag_max, 26);
@ -230,9 +196,9 @@ TEST_F(ResetIDsTest, DeletePlusAtomID)
ASSERT_GE(GETIDX(25), 0);
ASSERT_GE(GETIDX(26), 0);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("reset_atom_ids");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->atom->map_tag_max, 23);
for (int i = 1; i <= 23; ++i)
@ -246,11 +212,11 @@ TEST_F(ResetIDsTest, PartialOffset)
auto molid = lmp->atom->molecule;
// delete two water molecules
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("group allwater molecule 3:6");
command("group nowater subtract all allwater");
command("reset_mol_ids allwater offset 4");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->atom->natoms, 29);
ASSERT_EQ(lmp->atom->map_tag_max, 29);
@ -284,9 +250,9 @@ TEST_F(ResetIDsTest, PartialOffset)
ASSERT_EQ(molid[GETIDX(28)], 8);
ASSERT_EQ(molid[GETIDX(29)], 8);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("reset_mol_ids nowater offset 0");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(molid[GETIDX(1)], 1);
ASSERT_EQ(molid[GETIDX(2)], 1);
@ -326,13 +292,13 @@ TEST_F(ResetIDsTest, DeleteAdd)
auto molid = lmp->atom->molecule;
// delete two water molecules
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("group allwater molecule 3:6");
command("group twowater molecule 4:6:2");
command("group nowater subtract all allwater");
command("delete_atoms group twowater compress no bond yes mol yes");
command("reset_mol_ids allwater");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->atom->natoms, 23);
ASSERT_EQ(lmp->atom->map_tag_max, 26);
@ -389,17 +355,17 @@ TEST_F(ResetIDsTest, DeleteAdd)
ASSERT_GE(GETIDX(25), 0);
ASSERT_GE(GETIDX(26), 0);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("reset_atom_ids sort yes");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->atom->map_tag_max, 23);
for (int i = 1; i <= 23; ++i)
ASSERT_GE(GETIDX(i), 0);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("reset_mol_ids nowater offset 1");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(molid[GETIDX(1)], 2);
ASSERT_EQ(molid[GETIDX(2)], 2);
@ -425,13 +391,13 @@ TEST_F(ResetIDsTest, DeleteAdd)
ASSERT_EQ(molid[GETIDX(22)], 4);
ASSERT_EQ(molid[GETIDX(23)], 4);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("create_atoms 1 single 0.0 0.0 0.0");
command("create_atoms 2 single 1.0 0.0 0.0");
command("create_atoms 3 single 2.0 0.0 0.0");
command("create_atoms 4 single 3.0 0.0 0.0");
command("reset_mol_ids all single yes");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->atom->natoms, 27);
ASSERT_EQ(lmp->atom->map_tag_max, 27);
@ -443,9 +409,9 @@ TEST_F(ResetIDsTest, DeleteAdd)
ASSERT_EQ(molid[GETIDX(26)], 6);
ASSERT_EQ(molid[GETIDX(27)], 7);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("reset_mol_ids all single no");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(molid[GETIDX(21)], 3);
ASSERT_EQ(molid[GETIDX(22)], 3);
@ -455,9 +421,9 @@ TEST_F(ResetIDsTest, DeleteAdd)
ASSERT_EQ(molid[GETIDX(26)], 0);
ASSERT_EQ(molid[GETIDX(27)], 0);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("reset_mol_ids all compress no single yes");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(molid[GETIDX(21)], 21);
ASSERT_EQ(molid[GETIDX(22)], 21);
@ -473,12 +439,12 @@ TEST_F(ResetIDsTest, TopologyData)
if (lmp->atom->natoms == 0) GTEST_SKIP();
// delete two water molecules
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("group allwater molecule 3:6");
command("group twowater molecule 4:6:2");
command("group nowater subtract all allwater");
command("delete_atoms group twowater compress no bond yes mol yes");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(lmp->atom->natoms, 23);
ASSERT_EQ(lmp->atom->map_tag_max, 26);
@ -562,9 +528,9 @@ TEST_F(ResetIDsTest, TopologyData)
ASSERT_EQ(angle_atom2[GETIDX(24)][0], 24);
ASSERT_EQ(angle_atom3[GETIDX(24)][0], 26);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("reset_atom_ids sort yes");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
num_bond = lmp->atom->num_bond;
num_angle = lmp->atom->num_angle;
@ -679,41 +645,40 @@ TEST_F(ResetIDsTest, DeathTests)
command("reset_mol_ids all single xxx"););
}
TEST(ResetMolIds, CMDFail)
class ResetMolIDsTest : public LAMMPSTest {
protected:
void SetUp() override
{
testbinary = "ResetIDsTest";
LAMMPSTest::SetUp();
}
};
TEST_F(ResetMolIDsTest, FailBeforeBox)
{
LAMMPS *lmp;
const char *args[] = {"ResetIDsTest", "-log", "none", "-nocite", "-echo", "screen"};
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();
TEST_FAILURE(".*ERROR: Reset_mol_ids command before simulation box is.*",
lmp->input->one("reset_mol_ids all"););
command("reset_mol_ids all"););
}
auto command = [&](const std::string &line) {
lmp->input->one(line.c_str());
};
if (!verbose) ::testing::internal::CaptureStdout();
TEST_F(ResetMolIDsTest, FailMissingId)
{
BEGIN_HIDE_OUTPUT();
command("atom_modify id no");
command("region box block 0 1 0 1 0 1");
command("create_box 1 box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Cannot use reset_mol_ids unless.*", command("reset_mol_ids all"););
}
if (!verbose) ::testing::internal::CaptureStdout();
TEST_F(ResetMolIDsTest, FailOnlyMolecular)
{
BEGIN_HIDE_OUTPUT();
command("clear");
command("region box block 0 1 0 1 0 1");
command("create_box 1 box");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
TEST_FAILURE(".*ERROR: Can only use reset_mol_ids.*", command("reset_mol_ids all"););
if (!verbose) ::testing::internal::CaptureStdout();
delete lmp;
if (!verbose) ::testing::internal::GetCapturedStdout();
};
}
} // namespace LAMMPS_NS
@ -722,7 +687,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";

View File

@ -24,6 +24,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "../testing/core.h"
#include <cstring>
#include <vector>
@ -31,12 +32,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::MathConst::MY_PI;
using LAMMPS_NS::utils::split_words;
@ -45,39 +40,18 @@ 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 VariableTest : public ::testing::Test {
class VariableTest : public LAMMPSTest {
protected:
LAMMPS *lmp;
Group *group;
Domain *domain;
Variable *variable;
void SetUp() override
{
const char *args[] = {"VariableTest", "-log", "none", "-echo", "screen",
"-nocite", "-v", "num", "1"};
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 = "VariableTest";
args = {"-log", "none", "-echo", "screen", "-nocite", "-v", "num", "1"};
LAMMPSTest::SetUp();
group = lmp->group;
domain = lmp->domain;
variable = lmp->input->variable;
@ -85,19 +59,14 @@ protected:
void TearDown() override
{
if (!verbose) ::testing::internal::CaptureStdout();
delete lmp;
if (!verbose) ::testing::internal::GetCapturedStdout();
std::cout.flush();
LAMMPSTest::TearDown();
unlink("test_variable.file");
unlink("test_variable.atomfile");
}
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");
@ -109,23 +78,23 @@ 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();
}
void file_vars()
@ -152,7 +121,7 @@ TEST_F(VariableTest, CreateDelete)
{
file_vars();
ASSERT_EQ(variable->nvar, 1);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("variable one index 1 2 3 4");
command("variable two equal 1");
command("variable two equal 2");
@ -173,11 +142,11 @@ TEST_F(VariableTest, CreateDelete)
command("variable ten2 uloop 4");
command("variable ten3 uloop 4 pad");
command("variable dummy index 0");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(variable->nvar, 17);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("variable dummy delete");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(variable->nvar, 16);
ASSERT_THAT(variable->retrieve("three"), StrEq("three"));
variable->set_string("three", "four");
@ -235,7 +204,7 @@ TEST_F(VariableTest, AtomicSystem)
atomic_system();
file_vars();
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("variable one index 1 2 3 4");
command("variable id atom type");
command("variable id atom id");
@ -262,7 +231,7 @@ TEST_F(VariableTest, AtomicSystem)
command("variable rgsum equal sum(v_rg)");
command("variable loop equal v_loop+1");
command("run 0 post no");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_EQ(variable->atomstyle(variable->find("one")), 0);
ASSERT_EQ(variable->atomstyle(variable->find("id")), 1);
@ -294,7 +263,7 @@ TEST_F(VariableTest, AtomicSystem)
TEST_F(VariableTest, Expressions)
{
atomic_system();
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("variable one index 1");
command("variable two equal 2");
command("variable three equal v_one+v_two");
@ -321,7 +290,7 @@ TEST_F(VariableTest, Expressions)
command("variable err2 equal v_one%v_ten7");
command("variable err3 equal v_ten7^-v_one");
variable->set("dummy index 1 2");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
int ivar = variable->find("one");
ASSERT_FALSE(variable->equalstyle(ivar));
@ -364,7 +333,7 @@ TEST_F(VariableTest, Functions)
atomic_system();
file_vars();
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("variable one index 1");
command("variable two equal random(1,2,643532)");
command("variable three equal atan2(v_one,1)");
@ -377,7 +346,7 @@ TEST_F(VariableTest, Functions)
command("variable ten equal floor(1.85)+ceil(1.85)");
command("variable ten1 equal tan(v_eight/2.0)");
command("variable ten2 equal asin(-1.0)+acos(0.0)");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_GT(variable->compute_equal(variable->find("two")), 0.99);
ASSERT_LT(variable->compute_equal(variable->find("two")), 2.01);
@ -396,74 +365,64 @@ TEST_F(VariableTest, Functions)
TEST_F(VariableTest, IfCommand)
{
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("variable one index 1");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
command("if 1>0 then 'print \"bingo!\"'");
auto text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
auto text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, MatchesRegex(".*bingo!.*"));
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
command("if 1>2 then 'print \"bingo!\"' else 'print \"nope?\"'");
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, MatchesRegex(".*nope\?.*"));
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
command("if (1<=0) then 'print \"bingo!\"' else 'print \"nope?\"'");
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, MatchesRegex(".*nope\?.*"));
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
command("if (-1.0e-1<0.0E+0)|^(1<0) then 'print \"bingo!\"'");
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, MatchesRegex(".*bingo!.*"));
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
command("if (${one}==1.0)&&(2>=1) then 'print \"bingo!\"'");
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, MatchesRegex(".*bingo!.*"));
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
command("if !((${one}!=1.0)||(2|^1)) then 'print \"missed\"' else 'print \"bingo!\"'");
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, MatchesRegex(".*bingo!.*"));
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
command("if (1>=2)&&(0&&1) then 'print \"missed\"' else 'print \"bingo!\"'");
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, MatchesRegex(".*bingo!.*"));
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
command("if !1 then 'print \"missed\"' else 'print \"bingo!\"'");
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, MatchesRegex(".*bingo!.*"));
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
command("if !(a==b) then 'print \"bingo!\"'");
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, MatchesRegex(".*bingo!.*"));
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
command("if x==x|^1==0 then 'print \"bingo!\"'");
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, MatchesRegex(".*bingo!.*"));
::testing::internal::CaptureStdout();
BEGIN_CAPTURE_OUTPUT();
command("if x!=x|^a!=b then 'print \"bingo!\"'");
text = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << text;
text = END_CAPTURE_OUTPUT();
ASSERT_THAT(text, MatchesRegex(".*bingo!.*"));
TEST_FAILURE(".*ERROR: Invalid Boolean syntax in if command.*",
@ -492,34 +451,34 @@ TEST_F(VariableTest, NextCommand)
{
file_vars();
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("variable one index 1 2");
command("variable two equal 2");
command("variable three file test_variable.file");
command("variable four loop 2 4");
command("variable five index 1 2");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_DOUBLE_EQ(variable->compute_equal("v_one"), 1);
ASSERT_THAT(variable->retrieve("three"), StrEq("one"));
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("next one");
command("next three");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
ASSERT_DOUBLE_EQ(variable->compute_equal("v_one"), 2);
ASSERT_THAT(variable->retrieve("three"), StrEq("two"));
ASSERT_GE(variable->find("one"), 0);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("next one");
command("next three");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
// index style variable is deleted if no more next element
ASSERT_EQ(variable->find("one"), -1);
ASSERT_GE(variable->find("three"), 0);
if (!verbose) ::testing::internal::CaptureStdout();
BEGIN_HIDE_OUTPUT();
command("next three");
command("next three");
command("next three");
if (!verbose) ::testing::internal::GetCapturedStdout();
END_HIDE_OUTPUT();
// file style variable is deleted if no more next element
ASSERT_EQ(variable->find("three"), -1);
@ -536,7 +495,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";