diff --git a/src/KIM/kim_init.cpp b/src/KIM/kim_init.cpp index 511024c7f8..3253c09af7 100644 --- a/src/KIM/kim_init.cpp +++ b/src/KIM/kim_init.cpp @@ -365,16 +365,16 @@ void KimInit::do_init(char *model_name, char *user_units, char *model_units, KIM KIM_Model_GetNumberOfParameters(pkim, &numberOfParameters); std::string mesg = "\nThis model has "; - if (numberOfParameters) - { + if (numberOfParameters) { KIM_DataType kim_DataType; int extent; char const *str_name = NULL; char const *str_desc = NULL; - mesg += std::to_string(numberOfParameters); - mesg += " mutable parameters. \n"; + mesg += std::to_string(numberOfParameters) + " mutable parameters. \n"; + // TODO: aligned output is easy to do with fmtlib + // https://fmt.dev/latest/syntax.html int max_len(0); for (int i = 0; i < numberOfParameters; ++i) { KIM_Model_GetParameterMetadata(pkim, i, &kim_DataType, diff --git a/unittest/commands/CMakeLists.txt b/unittest/commands/CMakeLists.txt index 275e4b973f..f54b10dcb5 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_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}) + add_executable(test_reset_ids test_reset_ids.cpp) target_compile_definitions(test_reset_ids PRIVATE -DTEST_INPUT_FOLDER=${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(test_reset_ids PRIVATE lammps GTest::GMock GTest::GTest) diff --git a/unittest/commands/test_kim_commands.cpp b/unittest/commands/test_kim_commands.cpp new file mode 100644 index 0000000000..b261672cbc --- /dev/null +++ b/unittest/commands/test_kim_commands.cpp @@ -0,0 +1,127 @@ +/* ---------------------------------------------------------------------- + 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 "fmt/format.h" +#include "info.h" +#include "input.h" +#include "lammps.h" +#include "modify.h" +#include "utils.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include +#include + +// whether to print verbose output (i.e. not capturing LAMMPS screen output). +bool verbose = false; + +using LAMMPS_NS::utils::split_words; + +namespace LAMMPS_NS { +using ::testing::ExitedWithCode; +using ::testing::MatchesRegex; +using ::testing::StrEq; + +#define TEST_FAILURE(...) \ + if (Info::has_exceptions()) { \ + ASSERT_ANY_THROW({__VA_ARGS__}); \ + } else { \ + ASSERT_DEATH({__VA_ARGS__}, ""); \ + } + +class KimCommandsTest : public ::testing::Test { +protected: + LAMMPS *lmp; + + 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(); + } + + void TearDown() override + { + if (!verbose) ::testing::internal::CaptureStdout(); + delete lmp; + if (!verbose) ::testing::internal::GetCapturedStdout(); + } +}; + +TEST_F(KimCommandsTest, kim_init) +{ + if (!LAMMPS::is_installed_pkg("KIM")) GTEST_SKIP(); + + ::testing::internal::CaptureStdout(); + lmp->input->one("kim_init LennardJones_Ar real"); + ::testing::internal::GetCapturedStdout(); + + int ifix = lmp->modify->find_fix("KIM_MODEL_STORE"); + ASSERT_GE(ifix, 0); + + ::testing::internal::CaptureStdout(); + TEST_FAILURE(lmp->input->one("kim_init");); + auto mesg = ::testing::internal::GetCapturedStdout(); + ASSERT_THAT(mesg, MatchesRegex(".*ERROR: Illegal kim_init command.*")); + + ::testing::internal::CaptureStdout(); + TEST_FAILURE(lmp->input->one("kim_init Unknown_Model real");); + mesg = ::testing::internal::GetCapturedStdout(); + ASSERT_THAT(mesg, MatchesRegex(".*ERROR: KIM Model name not found.*")); +} + +TEST_F(KimCommandsTest, kim_interactions_ar) +{ + if (!LAMMPS::is_installed_pkg("KIM")) GTEST_SKIP(); + + ::testing::internal::CaptureStdout(); + lmp->input->one("kim_init LennardJones_Ar real"); + lmp->input->one("lattice fcc 4.4300"); + lmp->input->one("region box block 0 10 0 10 0 10"); + lmp->input->one("create_box 1 box"); + lmp->input->one("create_atoms 1 box"); + lmp->input->one("kim_interactions Ar"); + lmp->input->one("mass 1 39.95"); + ::testing::internal::GetCapturedStdout(); + + int ifix = lmp->modify->find_fix("KIM_MODEL_STORE"); + ASSERT_GE(ifix, 0); +} + +} // namespace LAMMPS_NS + +int main(int argc, char **argv) +{ + MPI_Init(&argc, &argv); + ::testing::InitGoogleMock(&argc, argv); + + // 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; +}