diff --git a/src/COMPRESS/dump_atom_zstd.cpp b/src/COMPRESS/dump_atom_zstd.cpp index f4660bec84..427aede016 100644 --- a/src/COMPRESS/dump_atom_zstd.cpp +++ b/src/COMPRESS/dump_atom_zstd.cpp @@ -129,11 +129,11 @@ void DumpAtomZstd::write_header(bigint ndump) if ((multiproc) || (!multiproc && me == 0)) { if (unit_flag && !unit_count) { ++unit_count; - header = fmt::format("ITEM: UNITS\n%s\n",update->unit_style); + header = fmt::format("ITEM: UNITS\n{}\n",update->unit_style); } if (time_flag) { - header += fmt::format("ITEM: TIME\n%.16g\n", compute_time()); + header += fmt::format("ITEM: TIME\n{0:.16g}\n", compute_time()); } header += fmt::format("ITEM: TIMESTEP\n{}\n", update->ntimestep); diff --git a/unittest/formats/CMakeLists.txt b/unittest/formats/CMakeLists.txt index fa09a69149..27c8a449d0 100644 --- a/unittest/formats/CMakeLists.txt +++ b/unittest/formats/CMakeLists.txt @@ -36,6 +36,16 @@ if (PKG_COMPRESS) add_test(NAME DumpAtomGZ COMMAND test_dump_atom_gz WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) set_tests_properties(DumpAtomGZ PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR}") set_tests_properties(DumpAtomGZ PROPERTIES ENVIRONMENT "GZIP_BINARY=${GZIP_BINARY}") + + find_program(ZSTD_BINARY NAMES zstd) + + if (ZSTD_BINARY) + add_executable(test_dump_atom_zstd test_dump_atom_zstd.cpp) + target_link_libraries(test_dump_atom_zstd PRIVATE lammps GTest::GMock GTest::GTest) + add_test(NAME DumpAtomZstd COMMAND test_dump_atom_zstd WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) + set_tests_properties(DumpAtomZstd PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR}") + set_tests_properties(DumpAtomZstd PROPERTIES ENVIRONMENT "ZSTD_BINARY=${ZSTD_BINARY}") + endif() endif() add_executable(test_dump_custom test_dump_custom.cpp) diff --git a/unittest/formats/test_dump_atom_zstd.cpp b/unittest/formats/test_dump_atom_zstd.cpp new file mode 100644 index 0000000000..470634a185 --- /dev/null +++ b/unittest/formats/test_dump_atom_zstd.cpp @@ -0,0 +1,273 @@ +/* ---------------------------------------------------------------------- + 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 "gtest/gtest.h" +#include "gmock/gmock.h" +#include "fmt/format.h" +#include "utils.h" +#include "../testing/core.h" +#include "../testing/systems/melt.h" +#include "../testing/utils.h" + +#include + +char * ZSTD_BINARY = nullptr; + +using ::testing::Eq; + +class DumpAtomZSTDTest : public MeltTest { + std::string dump_style = "atom"; +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 dump_modify_options, int ntimesteps) { + if (!verbose) ::testing::internal::CaptureStdout(); + command(fmt::format("dump id all {} 1 {}", dump_style, dump_file)); + + 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 dump_modify_options, int ntimesteps) { + if (!verbose) ::testing::internal::CaptureStdout(); + command(fmt::format("dump id0 all {} 1 {}", dump_style, text_file)); + command(fmt::format("dump id1 all {} 1 {}", compression_style, compressed_file)); + + 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; + } +}; + + +//------------------------------------------------------------------------------------------------- +// ZSTD compressed files +//------------------------------------------------------------------------------------------------- + +TEST_F(DumpAtomZSTDTest, compressed_run0) +{ + if(!ZSTD_BINARY) GTEST_SKIP(); + + auto text_file = "dump_text_run0.melt"; + auto compressed_file = "dump_compressed_run0.melt.zst"; + + generate_text_and_compressed_dump(text_file, compressed_file, "atom/zstd", "", 0); + + // make sure file is closed + TearDown(); + + ASSERT_FILE_EXISTS(text_file); + ASSERT_FILE_EXISTS(compressed_file); + + auto converted_file = convert_compressed_to_text(compressed_file); + + ASSERT_THAT(converted_file, Eq("dump_compressed_run0.melt")); + 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(DumpAtomZSTDTest, compressed_with_units_run0) +{ + if(!ZSTD_BINARY) GTEST_SKIP(); + + auto text_file = "dump_text_with_units_run0.melt"; + auto compressed_file = "dump_compressed_with_units_run0.melt.zst"; + + generate_text_and_compressed_dump(text_file, compressed_file, "atom/zstd", "scale no units yes", 0); + + // make sure file is closed + 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(DumpAtomZSTDTest, compressed_with_time_run0) +{ + if(!ZSTD_BINARY) GTEST_SKIP(); + + auto text_file = "dump_text_with_time_run0.melt"; + auto compressed_file = "dump_compressed_with_time_run0.melt.zst"; + + generate_text_and_compressed_dump(text_file, compressed_file, "atom/zstd", "scale no time yes", 0); + + // make sure file is closed + 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(DumpAtomZSTDTest, compressed_triclinic_run0) +{ + if(!ZSTD_BINARY) GTEST_SKIP(); + + auto text_file = "dump_text_tri_run0.melt"; + auto compressed_file = "dump_compressed_tri_run0.melt.zst"; + + enable_triclinic(); + generate_text_and_compressed_dump(text_file, compressed_file, "atom/zstd", "", 0); + + // make sure file is closed + 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(DumpAtomZSTDTest, compressed_triclinic_with_units_run0) +{ + if(!ZSTD_BINARY) GTEST_SKIP(); + + auto text_file = "dump_text_tri_with_units_run0.melt"; + auto compressed_file = "dump_compressed_tri_with_units_run0.melt.zst"; + + enable_triclinic(); + generate_text_and_compressed_dump(text_file, compressed_file, "atom/zstd", "scale no units yes", 0); + + // make sure file is closed + 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(DumpAtomZSTDTest, compressed_triclinic_with_time_run0) +{ + if(!ZSTD_BINARY) GTEST_SKIP(); + + auto text_file = "dump_text_tri_with_time_run0.melt"; + auto compressed_file = "dump_compressed_tri_with_time_run0.melt.zst"; + + enable_triclinic(); + generate_text_and_compressed_dump(text_file, compressed_file, "atom/zstd", "scale no time yes", 0); + + // make sure file is closed + 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(DumpAtomZSTDTest, compressed_triclinic_with_image_run0) +{ + if(!ZSTD_BINARY) GTEST_SKIP(); + + auto text_file = "dump_text_tri_with_image_run0.melt"; + auto compressed_file = "dump_compressed_tri_with_image_run0.melt.zst"; + + enable_triclinic(); + generate_text_and_compressed_dump(text_file, compressed_file, "atom/zstd", "image yes", 0); + + // make sure file is closed + 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; +}