From 71ec14087da1f29e3e8ab33019ba455982f99e8b Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Thu, 13 Aug 2020 13:00:37 -0400 Subject: [PATCH] Reduce code duplication in tests --- unittest/formats/test_dump_atom.cpp | 141 +------------------------- unittest/formats/test_dump_custom.cpp | 141 +------------------------- unittest/testing/core.h | 79 +++++++++++++++ unittest/testing/systems/melt.h | 40 ++++++++ unittest/testing/utils.h | 73 +++++++++++++ 5 files changed, 200 insertions(+), 274 deletions(-) create mode 100644 unittest/testing/core.h create mode 100644 unittest/testing/systems/melt.h create mode 100644 unittest/testing/utils.h diff --git a/unittest/formats/test_dump_atom.cpp b/unittest/formats/test_dump_atom.cpp index f30fdf3305..a5e7c39ade 100644 --- a/unittest/formats/test_dump_atom.cpp +++ b/unittest/formats/test_dump_atom.cpp @@ -11,149 +11,16 @@ See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ -#include "info.h" -#include "input.h" -#include "lammps.h" -#include "utils.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include "fmt/format.h" -#include "potential_file_reader.h" - -#include -#include -#include -#include - -using namespace LAMMPS_NS; - -using ::testing::MatchesRegex; - -#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 (Info::get_mpi_vendor() != "Open MPI") { \ - ::testing::internal::CaptureStdout(); \ - ASSERT_DEATH({__VA_ARGS__}, ""); \ - auto mesg = ::testing::internal::GetCapturedStdout(); \ - ASSERT_THAT(mesg, MatchesRegex(errmsg)); \ - } \ - } - -// whether to print verbose output (i.e. not capturing LAMMPS screen output). -bool verbose = false; +#include "utils.h" +#include "../testing/core.h" +#include "../testing/systems/melt.h" +#include "../testing/utils.h" char * BINARY2TXT_BINARY = nullptr; -static void delete_file(const std::string &filename) -{ - remove(filename.c_str()); -} - -static size_t count_lines(const std::string &filename) -{ - std::ifstream infile(filename); - std::string line; - size_t nlines = 0; - - while (std::getline(infile, line)) - ++nlines; - - return nlines; -} - -static bool equal_lines(const std::string &fileA, const std::string &fileB) -{ - std::ifstream afile(fileA); - std::ifstream bfile(fileB); - std::string lineA, lineB; - - while (std::getline(afile, lineA)) { - if(!std::getline(bfile, lineB)) return false; - if(lineA != lineB) return false; - } - - return true; -} - -static std::vector read_lines(const std::string &filename) { - std::vector lines; - std::ifstream infile(filename); - std::string line; - - while (std::getline(infile, line)) - lines.push_back(line); - - return lines; -} - -static bool file_exists(const std::string &filename) { - struct stat result; - return stat(filename.c_str(), &result) == 0; -} - -#define ASSERT_FILE_EXISTS(NAME) ASSERT_TRUE(file_exists(NAME)) -#define ASSERT_FILE_EQUAL(FILE_A, FILE_B) ASSERT_TRUE(equal_lines(FILE_A, FILE_B)) - -class LAMMPSTest : public ::testing::Test { -public: - void command(const std::string &line) { - lmp->input->one(line.c_str()); - } - -protected: - const char * testbinary = "LAMMPSTest"; - LAMMPS *lmp; - - void SetUp() override - { - const char *args[] = { testbinary, "-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); - InitSystem(); - if (!verbose) ::testing::internal::GetCapturedStdout(); - } - - - virtual void InitSystem() { - } - - void TearDown() override - { - if (!verbose) ::testing::internal::CaptureStdout(); - delete lmp; - if (!verbose) ::testing::internal::GetCapturedStdout(); - } -}; - -class MeltTest : public LAMMPSTest { -protected: - virtual void InitSystem() override { - command("units lj"); - command("atom_style atomic"); - - command("lattice fcc 0.8442"); - command("region box block 0 2 0 2 0 2"); - command("create_box 1 box"); - command("create_atoms 1 box"); - command("mass 1 1.0"); - - command("velocity all create 3.0 87287"); - - command("pair_style lj/cut 2.5"); - command("pair_coeff 1 1 1.0 1.0 2.5"); - - command("neighbor 0.3 bin"); - command("neigh_modify every 20 delay 0 check no"); - } -}; - class DumpAtomTest : public MeltTest { }; diff --git a/unittest/formats/test_dump_custom.cpp b/unittest/formats/test_dump_custom.cpp index 88feaacaca..da700c7b8a 100644 --- a/unittest/formats/test_dump_custom.cpp +++ b/unittest/formats/test_dump_custom.cpp @@ -11,149 +11,16 @@ See the README file in the top-level LAMMPS directory. ------------------------------------------------------------------------- */ -#include "info.h" -#include "input.h" -#include "lammps.h" -#include "utils.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include "fmt/format.h" -#include "potential_file_reader.h" - -#include -#include -#include -#include - -using namespace LAMMPS_NS; - -using ::testing::MatchesRegex; - -#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 (Info::get_mpi_vendor() != "Open MPI") { \ - ::testing::internal::CaptureStdout(); \ - ASSERT_DEATH({__VA_ARGS__}, ""); \ - auto mesg = ::testing::internal::GetCapturedStdout(); \ - ASSERT_THAT(mesg, MatchesRegex(errmsg)); \ - } \ - } - -// whether to print verbose output (i.e. not capturing LAMMPS screen output). -bool verbose = false; +#include "utils.h" +#include "../testing/core.h" +#include "../testing/systems/melt.h" +#include "../testing/utils.h" char * BINARY2TXT_BINARY = nullptr; -static void delete_file(const std::string &filename) -{ - remove(filename.c_str()); -} - -static size_t count_lines(const std::string &filename) -{ - std::ifstream infile(filename); - std::string line; - size_t nlines = 0; - - while (std::getline(infile, line)) - ++nlines; - - return nlines; -} - -static bool equal_lines(const std::string &fileA, const std::string &fileB) -{ - std::ifstream afile(fileA); - std::ifstream bfile(fileB); - std::string lineA, lineB; - - while (std::getline(afile, lineA)) { - if(!std::getline(bfile, lineB)) return false; - if(lineA != lineB) return false; - } - - return true; -} - -static std::vector read_lines(const std::string &filename) { - std::vector lines; - std::ifstream infile(filename); - std::string line; - - while (std::getline(infile, line)) - lines.push_back(line); - - return lines; -} - -static bool file_exists(const std::string &filename) { - struct stat result; - return stat(filename.c_str(), &result) == 0; -} - -#define ASSERT_FILE_EXISTS(NAME) ASSERT_TRUE(file_exists(NAME)) -#define ASSERT_FILE_EQUAL(FILE_A, FILE_B) ASSERT_TRUE(equal_lines(FILE_A, FILE_B)) - -class LAMMPSTest : public ::testing::Test { -public: - void command(const std::string &line) { - lmp->input->one(line.c_str()); - } - -protected: - const char * testbinary = "LAMMPSTest"; - LAMMPS *lmp; - - void SetUp() override - { - const char *args[] = { testbinary, "-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); - InitSystem(); - if (!verbose) ::testing::internal::GetCapturedStdout(); - } - - - virtual void InitSystem() { - } - - void TearDown() override - { - if (!verbose) ::testing::internal::CaptureStdout(); - delete lmp; - if (!verbose) ::testing::internal::GetCapturedStdout(); - } -}; - -class MeltTest : public LAMMPSTest { -protected: - virtual void InitSystem() override { - command("units lj"); - command("atom_style atomic"); - - command("lattice fcc 0.8442"); - command("region box block 0 2 0 2 0 2"); - command("create_box 1 box"); - command("create_atoms 1 box"); - command("mass 1 1.0"); - - command("velocity all create 3.0 87287"); - - command("pair_style lj/cut 2.5"); - command("pair_coeff 1 1 1.0 1.0 2.5"); - - command("neighbor 0.3 bin"); - command("neigh_modify every 20 delay 0 check no"); - } -}; - class DumpCustomTest : public MeltTest { }; diff --git a/unittest/testing/core.h b/unittest/testing/core.h new file mode 100644 index 0000000000..73e5cfbbb9 --- /dev/null +++ b/unittest/testing/core.h @@ -0,0 +1,79 @@ +/* ---------------------------------------------------------------------- + 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 TESTING_CORE__H +#define TESTING_CORE__H + +#include "info.h" +#include "input.h" +#include "lammps.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +using namespace LAMMPS_NS; + +using ::testing::MatchesRegex; + +#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 (Info::get_mpi_vendor() != "Open MPI") { \ + ::testing::internal::CaptureStdout(); \ + ASSERT_DEATH({__VA_ARGS__}, ""); \ + auto mesg = ::testing::internal::GetCapturedStdout(); \ + ASSERT_THAT(mesg, MatchesRegex(errmsg)); \ + } \ + } + +// whether to print verbose output (i.e. not capturing LAMMPS screen output). +bool verbose = false; + + +class LAMMPSTest : public ::testing::Test { +public: + void command(const std::string &line) { + lmp->input->one(line.c_str()); + } + +protected: + const char * testbinary = "LAMMPSTest"; + LAMMPS *lmp; + + void SetUp() override + { + const char *args[] = { testbinary, "-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); + InitSystem(); + if (!verbose) ::testing::internal::GetCapturedStdout(); + } + + + virtual void InitSystem() { + } + + void TearDown() override + { + if (!verbose) ::testing::internal::CaptureStdout(); + delete lmp; + if (!verbose) ::testing::internal::GetCapturedStdout(); + } +}; + + +#endif diff --git a/unittest/testing/systems/melt.h b/unittest/testing/systems/melt.h new file mode 100644 index 0000000000..084e81cf74 --- /dev/null +++ b/unittest/testing/systems/melt.h @@ -0,0 +1,40 @@ +/* ---------------------------------------------------------------------- + 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_EXAMPLE_MELT__H +#define TEST_EXAMPLE_MELT__H + +#include "../core.h" + +class MeltTest : public LAMMPSTest { +protected: + virtual void InitSystem() override { + command("units lj"); + command("atom_style atomic"); + + command("lattice fcc 0.8442"); + command("region box block 0 2 0 2 0 2"); + command("create_box 1 box"); + command("create_atoms 1 box"); + command("mass 1 1.0"); + + command("velocity all create 3.0 87287"); + + command("pair_style lj/cut 2.5"); + command("pair_coeff 1 1 1.0 1.0 2.5"); + + command("neighbor 0.3 bin"); + command("neigh_modify every 20 delay 0 check no"); + } +}; + +#endif diff --git a/unittest/testing/utils.h b/unittest/testing/utils.h new file mode 100644 index 0000000000..f99856d921 --- /dev/null +++ b/unittest/testing/utils.h @@ -0,0 +1,73 @@ +/* ---------------------------------------------------------------------- + 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_EXTENSIONS__H +#define TEST_EXTENSIONS__H + +#include +#include +#include +#include +#include +#include + +static void delete_file(const std::string &filename) +{ + remove(filename.c_str()); +} + +static size_t count_lines(const std::string &filename) +{ + std::ifstream infile(filename); + std::string line; + size_t nlines = 0; + + while (std::getline(infile, line)) + ++nlines; + + return nlines; +} + +static bool equal_lines(const std::string &fileA, const std::string &fileB) +{ + std::ifstream afile(fileA); + std::ifstream bfile(fileB); + std::string lineA, lineB; + + while (std::getline(afile, lineA)) { + if(!std::getline(bfile, lineB)) return false; + if(lineA != lineB) return false; + } + + return true; +} + +static std::vector read_lines(const std::string &filename) { + std::vector lines; + std::ifstream infile(filename); + std::string line; + + while (std::getline(infile, line)) + lines.push_back(line); + + return lines; +} + +static bool file_exists(const std::string &filename) { + struct stat result; + return stat(filename.c_str(), &result) == 0; +} + +#define ASSERT_FILE_EXISTS(NAME) ASSERT_TRUE(file_exists(NAME)) +#define ASSERT_FILE_EQUAL(FILE_A, FILE_B) ASSERT_TRUE(equal_lines(FILE_A, FILE_B)) + +#endif