From ed45ef301f96b734e88360a4026dab055ac5bf50 Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Wed, 5 May 2021 14:55:47 -0400 Subject: [PATCH] add unit test for error class --- unittest/cplusplus/CMakeLists.txt | 4 + unittest/cplusplus/test_error_class.cpp | 150 ++++++++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 unittest/cplusplus/test_error_class.cpp diff --git a/unittest/cplusplus/CMakeLists.txt b/unittest/cplusplus/CMakeLists.txt index aef69f3722..b0b2550e8c 100644 --- a/unittest/cplusplus/CMakeLists.txt +++ b/unittest/cplusplus/CMakeLists.txt @@ -7,3 +7,7 @@ set_tests_properties(LammpsClass PROPERTIES ENVIRONMENT "OMP_NUM_THREADS=1") add_executable(test_input_class test_input_class.cpp) target_link_libraries(test_input_class PRIVATE lammps GTest::GTest GTest::GTestMain) add_test(InputClass test_input_class) + +add_executable(test_error_class test_error_class.cpp) +target_link_libraries(test_error_class PRIVATE lammps GTest::GMock GTest::GTest) +add_test(ErrorClass test_error_class) diff --git a/unittest/cplusplus/test_error_class.cpp b/unittest/cplusplus/test_error_class.cpp new file mode 100644 index 0000000000..a7a70d351b --- /dev/null +++ b/unittest/cplusplus/test_error_class.cpp @@ -0,0 +1,150 @@ +// unit tests for issuing command to a LAMMPS instance through the Input class + +#include "error.h" +#include "info.h" +#include "lammps.h" +#include "output.h" +#include "thermo.h" + +#include "../testing/core.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include + +// whether to print verbose output (i.e. not capturing LAMMPS screen output). +bool verbose = false; + +namespace LAMMPS_NS { + +using ::testing::MatchesRegex; +using utils::split_words; + +class Error_class : public LAMMPSTest { +protected: + Error *error; + Thermo *thermo; + + void SetUp() override + { + testbinary = "ErrorClass"; + LAMMPSTest::SetUp(); + error = lmp->error; + thermo = lmp->output->thermo; + } +}; + +TEST_F(Error_class, message) +{ + auto output = CAPTURE_OUTPUT([&] { + error->message(FLERR, "one message"); + }); + EXPECT_THAT(output, MatchesRegex("one message .*test_error_class.cpp:.*")); +}; + +TEST_F(Error_class, warning) +{ + // standard warning + auto output = CAPTURE_OUTPUT([&] { + error->warning(FLERR, "one warning"); + }); + EXPECT_THAT(output, MatchesRegex("WARNING: one warning .*test_error_class.cpp:.*")); + EXPECT_THAT(error->get_maxwarn(), 100); + + // warnings disabled + HIDE_OUTPUT([&] { + command("thermo_modify warn ignore"); + }); + output = CAPTURE_OUTPUT([&] { + error->warning(FLERR, "one warning"); + }); + EXPECT_THAT(error->get_maxwarn(), -1); + + BEGIN_HIDE_OUTPUT(); + command("thermo_modify warn 2"); + error->warning(FLERR, "one warning"); + error->warning(FLERR, "one warning"); + error->warning(FLERR, "one warning"); + END_HIDE_OUTPUT(); + EXPECT_THAT(error->get_maxwarn(), 2); + EXPECT_THAT(error->get_numwarn(), 4); + + output = CAPTURE_OUTPUT([&] { + thermo->lost_check(); + }); + EXPECT_THAT(output, MatchesRegex("WARNING: Too many warnings: 4 vs 2. All future.*")); + + output = CAPTURE_OUTPUT([&] { + error->warning(FLERR, "one warning"); + }); + + EXPECT_EQ(output, ""); + + BEGIN_HIDE_OUTPUT(); + command("thermo_modify warn reset"); + thermo->lost_check(); + error->warning(FLERR, "one warning"); + END_HIDE_OUTPUT(); + EXPECT_THAT(error->get_maxwarn(), 2); + EXPECT_THAT(error->get_numwarn(), 1); + + output = CAPTURE_OUTPUT([&] { + error->warning(FLERR, "one warning"); + }); + EXPECT_THAT(output, MatchesRegex("WARNING: one warning.*")); + + BEGIN_HIDE_OUTPUT(); + command("thermo_modify warn default"); + thermo->lost_check(); + error->warning(FLERR, "one warning"); + END_HIDE_OUTPUT(); + EXPECT_THAT(error->get_maxwarn(), 100); + EXPECT_THAT(error->get_numwarn(), 1); + + BEGIN_HIDE_OUTPUT(); + command("thermo_modify warn always"); + thermo->lost_check(); + error->warning(FLERR, "one warning"); + END_HIDE_OUTPUT(); + EXPECT_THAT(error->get_maxwarn(), 0); + EXPECT_THAT(error->get_numwarn(), 1); +}; + +TEST_F(Error_class, one) +{ + TEST_FAILURE("ERROR on proc 0: one error.*test_error_class.cpp:.*", + error->one(FLERR, "one error");); +}; + +TEST_F(Error_class, all) +{ + TEST_FAILURE("ERROR: one error.*test_error_class.cpp:.*", error->all(FLERR, "one error");); +}; + +} // namespace LAMMPS_NS + +int main(int argc, char **argv) +{ + MPI_Init(&argc, &argv); + ::testing::InitGoogleMock(&argc, argv); + + if (Info::get_mpi_vendor() == "Open MPI" && !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; +}