use common main function that allows handling of common command line flags.

This commit is contained in:
Axel Kohlmeyer
2020-09-13 16:46:59 -04:00
parent 5a0623595b
commit 39255da6ae
4 changed files with 160 additions and 34 deletions

View File

@ -1,13 +1,13 @@
add_executable(test_library_open test_library_open.cpp) add_executable(test_library_open test_library_open.cpp test_main.cpp)
target_link_libraries(test_library_open PRIVATE lammps GTest::GTest GTest::GTestMain) target_link_libraries(test_library_open PRIVATE lammps GTest::GTest GTest::GMock)
add_test(LibraryOpen test_library_open) add_test(LibraryOpen test_library_open)
add_executable(test_library_commands test_library_commands.cpp) add_executable(test_library_commands test_library_commands.cpp test_main.cpp)
target_link_libraries(test_library_commands PRIVATE lammps GTest::GTest GTest::GTestMain) target_link_libraries(test_library_commands PRIVATE lammps GTest::GTest GTest::GMock)
add_test(LibraryCommands test_library_commands) add_test(LibraryCommands test_library_commands)
add_executable(test_library_properties test_library_properties.cpp) add_executable(test_library_properties test_library_properties.cpp test_main.cpp)
target_link_libraries(test_library_properties PRIVATE lammps GTest::GTest GTest::GTestMain) 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) add_test(LibraryProperties test_library_properties)

View File

@ -1,53 +1,87 @@
// unit tests for checking and changing simulation properties through the library interface // unit tests for checking and changing simulation properties through the library interface
#include "library.h"
#include "lammps.h" #include "lammps.h"
#include "library.h"
#include <string> #include <string>
#include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
const char *demo_input[] = { #include "test_main.h"
"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" };
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: protected:
void *lmp; void *lmp;
LAMMPS_properties() {}; std::string INPUT_DIR = STRINGIFY(TEST_INPUT_FOLDER);
~LAMMPS_properties() override {};
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; char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *); int argc = sizeof(args) / sizeof(char *);
::testing::internal::CaptureStdout(); ::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(); 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(); ::testing::internal::CaptureStdout();
lammps_close(lmp); lammps_close(lmp);
std::string output = ::testing::internal::GetCapturedStdout(); 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; lmp = nullptr;
} }
}; };
TEST_F(LAMMPS_properties, get_mpi_comm) { TEST_F(LAMMPS_properties, version)
int f_comm = lammps_get_mpi_comm(lmp); {
if (lammps_config_has_mpi_support()) EXPECT_GE(20200824, lammps_version(lmp));
EXPECT_GE(f_comm,0);
else
EXPECT_EQ(f_comm,-1);
}; };
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);
}; };

View File

@ -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 <iostream>
#include <mpi.h>
// 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<std::string> 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;
}

View File

@ -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