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