From 116ffd62dec936da768d31410c6db873fa3f354d Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Thu, 11 Mar 2021 12:11:23 -0500 Subject: [PATCH] Unify dump custom/gz and custom/zstd tests --- unittest/formats/CMakeLists.txt | 24 ++- .../formats/test_dump_custom_compressed.cpp | 122 ++++++++++++++ unittest/formats/test_dump_custom_gz.cpp | 154 ------------------ unittest/formats/test_dump_custom_zstd.cpp | 154 ------------------ 4 files changed, 133 insertions(+), 321 deletions(-) create mode 100644 unittest/formats/test_dump_custom_compressed.cpp delete mode 100644 unittest/formats/test_dump_custom_gz.cpp delete mode 100644 unittest/formats/test_dump_custom_zstd.cpp diff --git a/unittest/formats/CMakeLists.txt b/unittest/formats/CMakeLists.txt index cd02aed6b0..7932c037fa 100644 --- a/unittest/formats/CMakeLists.txt +++ b/unittest/formats/CMakeLists.txt @@ -43,17 +43,18 @@ if(PKG_COMPRESS) add_executable(test_dump_atom_compressed test_dump_atom_compressed.cpp) target_link_libraries(test_dump_atom_compressed PRIVATE lammps GTest::GMock GTest::GTest) - add_test(NAME DumpAtomGZ COMMAND test_dump_atom_compressed gz WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) - set_tests_properties(DumpAtomGZ PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR};COMPRESS_BINARY=${GZIP_BINARY}") - - add_executable(test_dump_custom_gz test_dump_custom_gz.cpp) - target_link_libraries(test_dump_custom_gz PRIVATE lammps GTest::GMock GTest::GTest) - add_test(NAME DumpCustomGZ COMMAND test_dump_custom_gz WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) - set_tests_properties(DumpCustomGZ PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR}") - set_tests_properties(DumpCustomGZ PROPERTIES ENVIRONMENT "GZIP_BINARY=${GZIP_BINARY}") + add_executable(test_dump_custom_compressed test_dump_custom_compressed.cpp) + target_link_libraries(test_dump_custom_compressed PRIVATE lammps GTest::GMock GTest::GTest) add_executable(test_dump_cfg_compressed test_dump_cfg_compressed.cpp) target_link_libraries(test_dump_cfg_compressed PRIVATE lammps GTest::GMock GTest::GTest) + + add_test(NAME DumpAtomGZ COMMAND test_dump_atom_compressed gz WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + set_tests_properties(DumpAtomGZ PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR};COMPRESS_BINARY=${GZIP_BINARY}") + + add_test(NAME DumpCustomGZ COMMAND test_dump_custom_compressed gz WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + set_tests_properties(DumpCustomGZ PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR};COMPRESS_BINARY=${GZIP_BINARY}") + add_test(NAME DumpCfgGZ COMMAND test_dump_cfg_compressed gz WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) set_tests_properties(DumpCfgGZ PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR};COMPRESS_BINARY=${GZIP_BINARY}") @@ -77,11 +78,8 @@ if(PKG_COMPRESS) add_test(NAME DumpAtomZstd COMMAND test_dump_atom_compressed zstd WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) set_tests_properties(DumpAtomZstd PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR};COMPRESS_BINARY=${ZSTD_BINARY}") - add_executable(test_dump_custom_zstd test_dump_custom_zstd.cpp) - target_link_libraries(test_dump_custom_zstd PRIVATE lammps GTest::GMock GTest::GTest) - add_test(NAME DumpCustomZstd COMMAND test_dump_custom_zstd WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) - set_tests_properties(DumpCustomZstd PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR}") - set_tests_properties(DumpCustomZstd PROPERTIES ENVIRONMENT "ZSTD_BINARY=${ZSTD_BINARY}") + add_test(NAME DumpCustomZstd COMMAND test_dump_custom_compressed zstd WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + set_tests_properties(DumpCustomZstd PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR};COMPRESS_BINARY=${ZSTD_BINARY}") add_test(NAME DumpCfgZstd COMMAND test_dump_cfg_compressed zstd WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) set_tests_properties(DumpCfgZstd PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR};COMPRESS_BINARY=${ZSTD_BINARY}") diff --git a/unittest/formats/test_dump_custom_compressed.cpp b/unittest/formats/test_dump_custom_compressed.cpp new file mode 100644 index 0000000000..0afad264fb --- /dev/null +++ b/unittest/formats/test_dump_custom_compressed.cpp @@ -0,0 +1,122 @@ +/* ---------------------------------------------------------------------- + LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator + https://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 "../testing/utils.h" +#include "compressed_dump_test.h" +#include "fmt/format.h" +#include "utils.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include + + +using ::testing::Eq; + +class DumpCustomCompressTest : public CompressedDumpTest { +public: + DumpCustomCompressTest() : CompressedDumpTest("custom") { + } +}; + +TEST_F(DumpCustomCompressTest, compressed_run1) +{ + if (!COMPRESS_BINARY) GTEST_SKIP(); + + auto base_name = "custom_run1.melt"; + auto text_file = text_dump_filename(base_name); + auto compressed_file = compressed_dump_filename(base_name); + auto fields = "id type proc x y z ix iy iz xs ys zs xu yu zu xsu ysu zsu vx vy vz fx fy fz"; + + generate_text_and_compressed_dump(text_file, compressed_file, fields, "units yes", 1); + + TearDown(); + + ASSERT_FILE_EXISTS(text_file); + ASSERT_FILE_EXISTS(compressed_file); + + auto converted_file = convert_compressed_to_text(compressed_file); + + ASSERT_FILE_EXISTS(converted_file); + ASSERT_FILE_EQUAL(text_file, converted_file); + delete_file(text_file); + delete_file(compressed_file); + delete_file(converted_file); +} + +TEST_F(DumpCustomCompressTest, compressed_triclinic_run1) +{ + if (!COMPRESS_BINARY) GTEST_SKIP(); + + auto base_name = "custom_tri_run1.melt"; + auto text_file = text_dump_filename(base_name); + auto compressed_file = compressed_dump_filename(base_name); + auto fields = "id type proc x y z xs ys zs xsu ysu zsu vx vy vz fx fy fz"; + + enable_triclinic(); + + generate_text_and_compressed_dump(text_file, compressed_file, fields, "units yes", 1); + + TearDown(); + + ASSERT_FILE_EXISTS(text_file); + ASSERT_FILE_EXISTS(compressed_file); + + auto converted_file = convert_compressed_to_text(compressed_file); + + ASSERT_FILE_EXISTS(converted_file); + ASSERT_FILE_EQUAL(text_file, converted_file); + delete_file(text_file); + delete_file(compressed_file); + delete_file(converted_file); +} + +int main(int argc, char **argv) +{ + MPI_Init(&argc, &argv); + ::testing::InitGoogleMock(&argc, argv); + + if (argc < 2) { + std::cerr << "usage: " << argv[0] << " (gz|zstd)\n\n" << std::endl; + return 1; + } + + if(strcmp(argv[1], "gz") == 0) { + COMPRESS_SUFFIX = "gz"; + COMPRESS_EXTENSION = "gz"; + } else if(strcmp(argv[1], "zstd") == 0) { + COMPRESS_SUFFIX = "zstd"; + COMPRESS_EXTENSION = "zst"; + } else { + std::cerr << "usage: " << argv[0] << " (gz|zstd)\n\n" << std::endl; + return 1; + } + + COMPRESS_BINARY = getenv("COMPRESS_BINARY"); + + // handle arguments passed via environment variable + if (const char *var = getenv("TEST_ARGS")) { + std::vector env = utils::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; +} diff --git a/unittest/formats/test_dump_custom_gz.cpp b/unittest/formats/test_dump_custom_gz.cpp deleted file mode 100644 index 67c9b535aa..0000000000 --- a/unittest/formats/test_dump_custom_gz.cpp +++ /dev/null @@ -1,154 +0,0 @@ -/* ---------------------------------------------------------------------- - LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator - https://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 "../testing/core.h" -#include "../testing/systems/melt.h" -#include "../testing/utils.h" -#include "fmt/format.h" -#include "utils.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -using ::testing::Eq; - -char *GZIP_BINARY = nullptr; - -class DumpCustomGZTest : public MeltTest { - std::string dump_style = "custom"; - -public: - void enable_triclinic() - { - if (!verbose) ::testing::internal::CaptureStdout(); - command("change_box all triclinic"); - if (!verbose) ::testing::internal::GetCapturedStdout(); - } - - void generate_dump(std::string dump_file, std::string fields, std::string dump_modify_options, - int ntimesteps) - { - if (!verbose) ::testing::internal::CaptureStdout(); - command(fmt::format("dump id all {} 1 {} {}", dump_style, dump_file, fields)); - - if (!dump_modify_options.empty()) { - command(fmt::format("dump_modify id {}", dump_modify_options)); - } - - command(fmt::format("run {}", ntimesteps)); - if (!verbose) ::testing::internal::GetCapturedStdout(); - } - - void generate_text_and_compressed_dump(std::string text_file, std::string compressed_file, - std::string compression_style, std::string fields, - std::string dump_modify_options, int ntimesteps) - { - if (!verbose) ::testing::internal::CaptureStdout(); - command(fmt::format("dump id0 all {} 1 {} {}", dump_style, text_file, fields)); - command(fmt::format("dump id1 all {} 1 {} {}", compression_style, compressed_file, fields)); - - if (!dump_modify_options.empty()) { - command(fmt::format("dump_modify id0 {}", dump_modify_options)); - command(fmt::format("dump_modify id1 {}", dump_modify_options)); - } - - command(fmt::format("run {}", ntimesteps)); - if (!verbose) ::testing::internal::GetCapturedStdout(); - } - - std::string convert_compressed_to_text(std::string compressed_file) - { - if (!verbose) ::testing::internal::CaptureStdout(); - std::string converted_file = compressed_file.substr(0, compressed_file.find_last_of('.')); - std::string cmdline = - fmt::format("{} -d -c {} > {}", GZIP_BINARY, compressed_file, converted_file); - system(cmdline.c_str()); - if (!verbose) ::testing::internal::GetCapturedStdout(); - return converted_file; - } -}; - -TEST_F(DumpCustomGZTest, compressed_run1) -{ - if (!GZIP_BINARY) GTEST_SKIP(); - - auto text_file = "dump_custom_gz_text_run1.melt"; - auto compressed_file = "dump_custom_gz_compressed_run1.melt.gz"; - auto fields = "id type proc x y z ix iy iz xs ys zs xu yu zu xsu ysu zsu vx vy vz fx fy fz"; - - generate_text_and_compressed_dump(text_file, compressed_file, "custom/gz", fields, "units yes", - 1); - - TearDown(); - - ASSERT_FILE_EXISTS(text_file); - ASSERT_FILE_EXISTS(compressed_file); - - auto converted_file = convert_compressed_to_text(compressed_file); - - ASSERT_FILE_EXISTS(converted_file); - ASSERT_FILE_EQUAL(text_file, converted_file); - delete_file(text_file); - delete_file(compressed_file); - delete_file(converted_file); -} - -TEST_F(DumpCustomGZTest, compressed_triclinic_run1) -{ - if (!GZIP_BINARY) GTEST_SKIP(); - - auto text_file = "dump_custom_gz_tri_text_run1.melt"; - auto compressed_file = "dump_custom_gz_tri_compressed_run1.melt.gz"; - auto fields = "id type proc x y z xs ys zs xsu ysu zsu vx vy vz fx fy fz"; - - enable_triclinic(); - - generate_text_and_compressed_dump(text_file, compressed_file, "custom/gz", fields, "units yes", - 1); - - TearDown(); - - ASSERT_FILE_EXISTS(text_file); - ASSERT_FILE_EXISTS(compressed_file); - - auto converted_file = convert_compressed_to_text(compressed_file); - - ASSERT_FILE_EXISTS(converted_file); - ASSERT_FILE_EQUAL(text_file, converted_file); - delete_file(text_file); - delete_file(compressed_file); - delete_file(converted_file); -} - -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 = utils::split_words(var); - for (auto arg : env) { - if (arg == "-v") { - verbose = true; - } - } - } - - GZIP_BINARY = getenv("GZIP_BINARY"); - - if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = true; - - int rv = RUN_ALL_TESTS(); - MPI_Finalize(); - return rv; -} diff --git a/unittest/formats/test_dump_custom_zstd.cpp b/unittest/formats/test_dump_custom_zstd.cpp deleted file mode 100644 index 40328d5592..0000000000 --- a/unittest/formats/test_dump_custom_zstd.cpp +++ /dev/null @@ -1,154 +0,0 @@ -/* ---------------------------------------------------------------------- - LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator - https://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 "../testing/core.h" -#include "../testing/systems/melt.h" -#include "../testing/utils.h" -#include "fmt/format.h" -#include "utils.h" -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -using ::testing::Eq; - -char *ZSTD_BINARY = nullptr; - -class DumpCustomZstdTest : public MeltTest { - std::string dump_style = "custom"; - -public: - void enable_triclinic() - { - if (!verbose) ::testing::internal::CaptureStdout(); - command("change_box all triclinic"); - if (!verbose) ::testing::internal::GetCapturedStdout(); - } - - void generate_dump(std::string dump_file, std::string fields, std::string dump_modify_options, - int ntimesteps) - { - if (!verbose) ::testing::internal::CaptureStdout(); - command(fmt::format("dump id all {} 1 {} {}", dump_style, dump_file, fields)); - - if (!dump_modify_options.empty()) { - command(fmt::format("dump_modify id {}", dump_modify_options)); - } - - command(fmt::format("run {}", ntimesteps)); - if (!verbose) ::testing::internal::GetCapturedStdout(); - } - - void generate_text_and_compressed_dump(std::string text_file, std::string compressed_file, - std::string compression_style, std::string fields, - std::string dump_modify_options, int ntimesteps) - { - if (!verbose) ::testing::internal::CaptureStdout(); - command(fmt::format("dump id0 all {} 1 {} {}", dump_style, text_file, fields)); - command(fmt::format("dump id1 all {} 1 {} {}", compression_style, compressed_file, fields)); - - if (!dump_modify_options.empty()) { - command(fmt::format("dump_modify id0 {}", dump_modify_options)); - command(fmt::format("dump_modify id1 {}", dump_modify_options)); - } - - command(fmt::format("run {}", ntimesteps)); - if (!verbose) ::testing::internal::GetCapturedStdout(); - } - - std::string convert_compressed_to_text(std::string compressed_file) - { - if (!verbose) ::testing::internal::CaptureStdout(); - std::string converted_file = compressed_file.substr(0, compressed_file.find_last_of('.')); - std::string cmdline = - fmt::format("{} -d -c {} > {}", ZSTD_BINARY, compressed_file, converted_file); - system(cmdline.c_str()); - if (!verbose) ::testing::internal::GetCapturedStdout(); - return converted_file; - } -}; - -TEST_F(DumpCustomZstdTest, compressed_run1) -{ - if (!ZSTD_BINARY) GTEST_SKIP(); - - auto text_file = "dump_custom_zstd_text_run1.melt"; - auto compressed_file = "dump_custom_zstd_compressed_run1.melt.zst"; - auto fields = "id type proc x y z ix iy iz xs ys zs xu yu zu xsu ysu zsu vx vy vz fx fy fz"; - - generate_text_and_compressed_dump(text_file, compressed_file, "custom/zstd", fields, - "units yes", 1); - - TearDown(); - - ASSERT_FILE_EXISTS(text_file); - ASSERT_FILE_EXISTS(compressed_file); - - auto converted_file = convert_compressed_to_text(compressed_file); - - ASSERT_FILE_EXISTS(converted_file); - ASSERT_FILE_EQUAL(text_file, converted_file); - delete_file(text_file); - delete_file(compressed_file); - delete_file(converted_file); -} - -TEST_F(DumpCustomZstdTest, compressed_triclinic_run1) -{ - if (!ZSTD_BINARY) GTEST_SKIP(); - - auto text_file = "dump_custom_zstd_tri_text_run1.melt"; - auto compressed_file = "dump_custom_zstd_tri_compressed_run1.melt.zst"; - auto fields = "id type proc x y z xs ys zs xsu ysu zsu vx vy vz fx fy fz"; - - enable_triclinic(); - - generate_text_and_compressed_dump(text_file, compressed_file, "custom/zstd", fields, - "units yes", 1); - - TearDown(); - - ASSERT_FILE_EXISTS(text_file); - ASSERT_FILE_EXISTS(compressed_file); - - auto converted_file = convert_compressed_to_text(compressed_file); - - ASSERT_FILE_EXISTS(converted_file); - ASSERT_FILE_EQUAL(text_file, converted_file); - delete_file(text_file); - delete_file(compressed_file); - delete_file(converted_file); -} - -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 = utils::split_words(var); - for (auto arg : env) { - if (arg == "-v") { - verbose = true; - } - } - } - - ZSTD_BINARY = getenv("ZSTD_BINARY"); - - if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = true; - - int rv = RUN_ALL_TESTS(); - MPI_Finalize(); - return rv; -}