diff --git a/unittest/c-library/CMakeLists.txt b/unittest/c-library/CMakeLists.txt index 90c57f1fa7..7bbac4c430 100644 --- a/unittest/c-library/CMakeLists.txt +++ b/unittest/c-library/CMakeLists.txt @@ -1,13 +1,13 @@ -add_executable(test_library_open test_library_open.cpp) -target_link_libraries(test_library_open PRIVATE lammps GTest::GTest GTest::GTestMain) +add_executable(test_library_open test_library_open.cpp test_main.cpp) +target_link_libraries(test_library_open PRIVATE lammps GTest::GTest GTest::GMock) add_test(LibraryOpen test_library_open) -add_executable(test_library_commands test_library_commands.cpp) -target_link_libraries(test_library_commands PRIVATE lammps GTest::GTest GTest::GTestMain) +add_executable(test_library_commands test_library_commands.cpp test_main.cpp) +target_link_libraries(test_library_commands PRIVATE lammps GTest::GTest GTest::GMock) add_test(LibraryCommands test_library_commands) -add_executable(test_library_properties test_library_properties.cpp) -target_link_libraries(test_library_properties PRIVATE lammps GTest::GTest GTest::GTestMain) +add_executable(test_library_properties test_library_properties.cpp test_main.cpp) +target_link_libraries(test_library_properties PRIVATE lammps GTest::GTest GTest::GMock) +target_compile_definitions(test_library_properties PRIVATE -DTEST_INPUT_FOLDER=${CMAKE_CURRENT_SOURCE_DIR}) add_test(LibraryProperties test_library_properties) - diff --git a/unittest/c-library/test_library_properties.cpp b/unittest/c-library/test_library_properties.cpp index 5e8c410cce..030ff96795 100644 --- a/unittest/c-library/test_library_properties.cpp +++ b/unittest/c-library/test_library_properties.cpp @@ -1,53 +1,87 @@ // unit tests for checking and changing simulation properties through the library interface -#include "library.h" #include "lammps.h" +#include "library.h" #include +#include "gmock/gmock.h" #include "gtest/gtest.h" -const char *demo_input[] = { - "region box block 0 $x 0 2 0 2", - "create_box 1 box", - "create_atoms 1 single 1.0 1.0 ${zpos}" }; -const char *cont_input[] = { - "create_atoms 1 single &", - "0.2 0.1 0.1" }; +#include "test_main.h" -class LAMMPS_properties : public ::testing::Test -{ +#define STRINGIFY(val) XSTR(val) +#define XSTR(val) #val + +using ::testing::HasSubstr; +using ::testing::StartsWith; + +class LAMMPS_properties : public ::testing::Test { protected: void *lmp; - LAMMPS_properties() {}; - ~LAMMPS_properties() override {}; + std::string INPUT_DIR = STRINGIFY(TEST_INPUT_FOLDER); + + LAMMPS_properties(){}; + ~LAMMPS_properties() override{}; + + void SetUp() override + { + const char *args[] = {"LAMMPS_test", "-log", "none", + "-echo", "screen", "-nocite", + "-var", "input_dir", STRINGIFY(TEST_INPUT_FOLDER)}; - void SetUp() override { - const char *args[] = {"LAMMPS_test", "-log", "none", - "-echo", "screen", "-nocite" }; char **argv = (char **)args; - int argc = sizeof(args)/sizeof(char *); + int argc = sizeof(args) / sizeof(char *); ::testing::internal::CaptureStdout(); - lmp = lammps_open_no_mpi(argc, argv, NULL); + lmp = lammps_open_no_mpi(argc, argv, NULL); std::string output = ::testing::internal::GetCapturedStdout(); - EXPECT_STREQ(output.substr(0,8).c_str(), "LAMMPS ("); + if (verbose) std::cout << output; + EXPECT_THAT(output, StartsWith("LAMMPS (")); } - void TearDown() override { + void TearDown() override + { ::testing::internal::CaptureStdout(); lammps_close(lmp); std::string output = ::testing::internal::GetCapturedStdout(); - EXPECT_STREQ(output.substr(0,16).c_str(), "Total wall time:"); + EXPECT_THAT(output, HasSubstr("Total wall time:")); + if (verbose) std::cout << output; lmp = nullptr; } }; -TEST_F(LAMMPS_properties, get_mpi_comm) { - int f_comm = lammps_get_mpi_comm(lmp); - if (lammps_config_has_mpi_support()) - EXPECT_GE(f_comm,0); - else - EXPECT_EQ(f_comm,-1); +TEST_F(LAMMPS_properties, version) +{ + EXPECT_GE(20200824, lammps_version(lmp)); }; -TEST_F(LAMMPS_properties, box) { +TEST_F(LAMMPS_properties, get_mpi_comm) +{ + int f_comm = lammps_get_mpi_comm(lmp); + if (lammps_config_has_mpi_support()) + EXPECT_GE(f_comm, 0); + else + EXPECT_EQ(f_comm, -1); +}; + +TEST_F(LAMMPS_properties, natoms) +{ + std::string input = INPUT_DIR + PATH_SEP + "in.fourmol"; + if (!verbose) ::testing::internal::CaptureStdout(); + lammps_file(lmp, input.c_str()); + if (!verbose) ::testing::internal::GetCapturedStdout(); + EXPECT_EQ(lammps_get_natoms(lmp), 29); +}; + +TEST_F(LAMMPS_properties, thermo) +{ + std::string input = INPUT_DIR + PATH_SEP + "in.fourmol"; + if (!verbose) ::testing::internal::CaptureStdout(); + lammps_file(lmp, input.c_str()); + lammps_command(lmp, "run 2 post no"); + if (!verbose) ::testing::internal::GetCapturedStdout(); + EXPECT_EQ(lammps_get_thermo(lmp, "step"), 2); + EXPECT_EQ(lammps_get_thermo(lmp, "atoms"), 29); + EXPECT_DOUBLE_EQ(lammps_get_thermo(lmp, "vol"), 3375.0); + EXPECT_DOUBLE_EQ(lammps_get_thermo(lmp, "density"), 0.12211250945013695); + EXPECT_DOUBLE_EQ(lammps_get_thermo(lmp, "cellalpha"), 90.0); }; diff --git a/unittest/c-library/test_main.cpp b/unittest/c-library/test_main.cpp new file mode 100644 index 0000000000..f32e222e1e --- /dev/null +++ b/unittest/c-library/test_main.cpp @@ -0,0 +1,59 @@ +/* ---------------------------------------------------------------------- + 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 "test_main.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; + +int main(int argc, char **argv) +{ + MPI_Init(&argc, &argv); + ::testing::InitGoogleMock(&argc, argv); + + if (argc < 1) { + return 1; + } + + // handle arguments passed via environment variable + if (const char *var = getenv("TEST_ARGS")) { + std::vector env = LAMMPS_NS::utils::split_words(var); + for (auto arg : env) { + if (arg == "-v") { + verbose = true; + } + } + } + + int iarg = 1; + while (iarg < argc) { + if (strcmp(argv[iarg], "-v") == 0) { + verbose = true; + ++iarg; + } else { + std::cerr << "unknown option: " << argv[iarg] << "\n\n"; + MPI_Finalize(); + return 1; + } + } + + int rv = RUN_ALL_TESTS(); + MPI_Finalize(); + return rv; +} diff --git a/unittest/c-library/test_main.h b/unittest/c-library/test_main.h new file mode 100644 index 0000000000..ac9bc0d26a --- /dev/null +++ b/unittest/c-library/test_main.h @@ -0,0 +1,33 @@ +/* -*- c++ -*- ---------------------------------------------------------- + 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. +------------------------------------------------------------------------- */ + +#ifndef TEST_MAIN_H +#define TEST_MAIN_H + +extern bool verbose; + +#define EXPECT_FP_LE_WITH_EPS(val1, val2, eps) \ + do { \ + const double diff = fabs(val1 - val2); \ + const double div = std::min(fabs(val1), fabs(val2)); \ + const double err = (div == 0.0) ? diff : diff / div; \ + EXPECT_PRED_FORMAT2(::testing::DoubleLE, err, eps); \ + } while (0); + +#endif + +#if defined _WIN32 +static const char PATH_SEP = '\\'; +#else +static const char PATH_SEP = '/'; +#endif