diff --git a/unittest/commands/CMakeLists.txt b/unittest/commands/CMakeLists.txt index f54b10dcb5..e9cdf78ab7 100644 --- a/unittest/commands/CMakeLists.txt +++ b/unittest/commands/CMakeLists.txt @@ -3,6 +3,10 @@ add_executable(test_simple_commands test_simple_commands.cpp) target_link_libraries(test_simple_commands PRIVATE lammps GTest::GMock GTest::GTest) add_test(NAME SimpleCommands COMMAND test_simple_commands WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) +add_executable(test_lattice_region test_lattice_region.cpp) +target_link_libraries(test_lattice_region PRIVATE lammps GTest::GMock GTest::GTest) +add_test(NAME LatticeRegion COMMAND test_lattice_region WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + add_executable(test_kim_commands test_kim_commands.cpp) target_link_libraries(test_kim_commands PRIVATE lammps GTest::GMock GTest::GTest) add_test(NAME KimCommands COMMAND test_kim_commands WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/unittest/commands/test_lattice_region.cpp b/unittest/commands/test_lattice_region.cpp new file mode 100644 index 0000000000..ec0317321a --- /dev/null +++ b/unittest/commands/test_lattice_region.cpp @@ -0,0 +1,185 @@ +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + http://lammps.sandia.gov, Sandia National Laboratories + Steve Plimpton, sjplimp@sandia.gov + + Copyright (2003) Sandia Corporation. Under the terms of Contract + DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains + certain rights in this software. This software is distributed under + the GNU General Public License. + + See the README file in the top-level LAMMPS directory. +------------------------------------------------------------------------- */ + +#include "domain.h" +#include "fmt/format.h" +#include "info.h" +#include "input.h" +#include "lammps.h" +#include "lattice.h" +#include "region.h" +#include "utils.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include +#include +#include +#include +#include + +// 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::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 { +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(); + } + + void TearDown() override + { + if (!verbose) ::testing::internal::CaptureStdout(); + delete lmp; + if (!verbose) ::testing::internal::GetCapturedStdout(); + } +}; + +TEST_F(LatticeRegionTest, lattice_none) +{ + if (!verbose) ::testing::internal::CaptureStdout(); + lmp->input->one("lattice none 2.0"); + if (!verbose) ::testing::internal::GetCapturedStdout(); + auto lattice = lmp->domain->lattice; + ASSERT_EQ(lattice->style, Lattice::NONE); + ASSERT_EQ(lattice->xlattice, 2.0); + ASSERT_EQ(lattice->ylattice, 2.0); + ASSERT_EQ(lattice->zlattice, 2.0); + ASSERT_EQ(lattice->nbasis, 0); + ASSERT_EQ(lattice->basis, nullptr); + + TEST_FAILURE(".*ERROR: Illegal lattice command.*", lmp->input->one("lattice");); + TEST_FAILURE(".*ERROR: Illegal lattice command.*", lmp->input->one("lattice xxx");); + TEST_FAILURE(".*ERROR: Illegal lattice command.*", lmp->input->one("lattice none 1.0 origin");); + TEST_FAILURE(".*ERROR: Expected floating point.*", lmp->input->one("lattice none xxx");); +} + +TEST_F(LatticeRegionTest, lattice_sc) +{ + ::testing::internal::CaptureStdout(); + lmp->input->one("lattice sc 2.0"); + auto output = ::testing::internal::GetCapturedStdout(); + if (verbose) std::cout << output; + ASSERT_THAT(output, MatchesRegex(".*Lattice spacing in x,y,z = 2.0* 2.0* 2.0*.*")); + + auto lattice = lmp->domain->lattice; + ASSERT_EQ(lattice->style, Lattice::SC); + ASSERT_EQ(lattice->xlattice, 2.0); + ASSERT_EQ(lattice->ylattice, 2.0); + ASSERT_EQ(lattice->zlattice, 2.0); + ASSERT_EQ(lattice->nbasis, 1); + ASSERT_NE(lattice->basis, nullptr); + ASSERT_EQ(lattice->a1[0], 1.0); + ASSERT_EQ(lattice->a1[1], 0.0); + ASSERT_EQ(lattice->a1[2], 0.0); + ASSERT_EQ(lattice->a2[0], 0.0); + ASSERT_EQ(lattice->a2[1], 1.0); + ASSERT_EQ(lattice->a2[2], 0.0); + ASSERT_EQ(lattice->a3[0], 0.0); + ASSERT_EQ(lattice->a3[1], 0.0); + ASSERT_EQ(lattice->a3[2], 1.0); + ASSERT_EQ(lattice->basis[0][0], 0.0); + ASSERT_EQ(lattice->basis[0][1], 0.0); + ASSERT_EQ(lattice->basis[0][2], 0.0); + + TEST_FAILURE(".*ERROR: Illegal lattice command.*", + lmp->input->one("lattice sc 1.0 origin 1.0 1.0 1.0");); + TEST_FAILURE(".*ERROR: Illegal lattice command.*", + lmp->input->one("lattice sc 1.0 origin 1.0");); + TEST_FAILURE(".*ERROR: Expected floating point.*", + lmp->input->one("lattice sc 1.0 origin xxx 1.0 1.0");); +} + +TEST_F(LatticeRegionTest, lattice_bcc) +{ + if (!verbose) ::testing::internal::CaptureStdout(); + lmp->input->one("lattice bcc 4.2 orient x 1 1 0 orient y -1 1 0"); + if (!verbose) ::testing::internal::GetCapturedStdout(); + auto lattice = lmp->domain->lattice; + ASSERT_EQ(lattice->style, Lattice::BCC); + ASSERT_DOUBLE_EQ(lattice->xlattice, sqrt(2.0)*4.2); + ASSERT_DOUBLE_EQ(lattice->ylattice, sqrt(2.0)*4.2); + ASSERT_DOUBLE_EQ(lattice->zlattice, 4.2); + ASSERT_EQ(lattice->nbasis, 2); + ASSERT_EQ(lattice->basis[0][0], 0.0); + ASSERT_EQ(lattice->basis[0][1], 0.0); + ASSERT_EQ(lattice->basis[0][2], 0.0); + ASSERT_EQ(lattice->basis[1][0], 0.5); + ASSERT_EQ(lattice->basis[1][1], 0.5); + ASSERT_EQ(lattice->basis[1][2], 0.5); +} + +} // namespace LAMMPS_NS + +int main(int argc, char **argv) +{ + MPI_Init(&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 + if (const char *var = getenv("TEST_ARGS")) { + std::vector env = split_words(var); + for (auto arg : env) { + if (arg == "-v") { + verbose = true; + } + } + } + + if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = true; + + int rv = RUN_ALL_TESTS(); + MPI_Finalize(); + return rv; +}