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";