diff --git a/unittest/force-styles/test_angle_style.cpp b/unittest/force-styles/test_angle_style.cpp index ff391127bd..05646a93a9 100644 --- a/unittest/force-styles/test_angle_style.cpp +++ b/unittest/force-styles/test_angle_style.cpp @@ -38,7 +38,6 @@ #include #include #include -#include #include #include @@ -235,48 +234,8 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) YamlWriter writer(outfile); - // lammps_version - writer.emit("lammps_version", lmp->version); - - // date_generated - std::time_t now = time(NULL); - block = utils::trim(ctime(&now)); - writer.emit("date_generated", block); - - // epsilon - writer.emit("epsilon", config.epsilon); - - // skip tests - block.clear(); - for (auto &skip_tests : config.skip_tests) { - if (block.empty()) block = skip_tests; - else block += " " + skip_tests; - } - writer.emit("skip_tests", block); - - // prerequisites - block.clear(); - for (auto &prerequisite : config.prerequisites) { - block += prerequisite.first + " " + prerequisite.second + "\n"; - } - writer.emit_block("prerequisites", block); - - // pre_commands - block.clear(); - for (auto &command : config.pre_commands) { - block += command + "\n"; - } - writer.emit_block("pre_commands", block); - - // post_commands - block.clear(); - for (auto &command : config.post_commands) { - block += command + "\n"; - } - writer.emit_block("post_commands", block); - - // input_file - writer.emit("input_file", config.input_file); + // write yaml header + write_yaml_header(&writer, &test_config, lmp->version); // angle_style writer.emit("angle_style", config.angle_style); diff --git a/unittest/force-styles/test_bond_style.cpp b/unittest/force-styles/test_bond_style.cpp index 94d2344c03..f3aab53b12 100644 --- a/unittest/force-styles/test_bond_style.cpp +++ b/unittest/force-styles/test_bond_style.cpp @@ -38,7 +38,6 @@ #include #include #include -#include #include #include @@ -235,48 +234,8 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) YamlWriter writer(outfile); - // lammps_version - writer.emit("lammps_version", lmp->version); - - // date_generated - std::time_t now = time(NULL); - block = utils::trim(ctime(&now)); - writer.emit("date_generated", block); - - // epsilon - writer.emit("epsilon", config.epsilon); - - // skip tests - block.clear(); - for (auto &skip_tests : config.skip_tests) { - if (block.empty()) block = skip_tests; - else block += " " + skip_tests; - } - writer.emit("skip_tests", block); - - // prerequisites - block.clear(); - for (auto &prerequisite : config.prerequisites) { - block += prerequisite.first + " " + prerequisite.second + "\n"; - } - writer.emit_block("prerequisites", block); - - // pre_commands - block.clear(); - for (auto &command : config.pre_commands) { - block += command + "\n"; - } - writer.emit_block("pre_commands", block); - - // post_commands - block.clear(); - for (auto &command : config.post_commands) { - block += command + "\n"; - } - writer.emit_block("post_commands", block); - - // input_file - writer.emit("input_file", config.input_file); + // write yaml header + write_yaml_header(&writer, &test_config, lmp->version); // bond_style writer.emit("bond_style", config.bond_style); diff --git a/unittest/force-styles/test_dihedral_style.cpp b/unittest/force-styles/test_dihedral_style.cpp index f8ad0a60eb..95f64d7896 100644 --- a/unittest/force-styles/test_dihedral_style.cpp +++ b/unittest/force-styles/test_dihedral_style.cpp @@ -38,7 +38,6 @@ #include #include #include -#include #include #include @@ -244,40 +243,8 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) YamlWriter writer(outfile); - // lammps_version - writer.emit("lammps_version", lmp->version); - - // date_generated - std::time_t now = time(NULL); - block = utils::trim(ctime(&now)); - writer.emit("date_generated", block); - - // epsilon - writer.emit("epsilon", config.epsilon); - - // prerequisites - block.clear(); - for (auto &prerequisite : config.prerequisites) { - block += prerequisite.first + " " + prerequisite.second + "\n"; - } - writer.emit_block("prerequisites", block); - - // pre_commands - block.clear(); - for (auto &command : config.pre_commands) { - block += command + "\n"; - } - writer.emit_block("pre_commands", block); - - // post_commands - block.clear(); - for (auto &command : config.post_commands) { - block += command + "\n"; - } - writer.emit_block("post_commands", block); - - // input_file - writer.emit("input_file", config.input_file); + // write yaml header + write_yaml_header(&writer, &test_config, lmp->version); // dihedral_style writer.emit("dihedral_style", config.dihedral_style); diff --git a/unittest/force-styles/test_fix_timestep.cpp b/unittest/force-styles/test_fix_timestep.cpp index 3fa2e78813..6ba3f7f6d7 100644 --- a/unittest/force-styles/test_fix_timestep.cpp +++ b/unittest/force-styles/test_fix_timestep.cpp @@ -42,7 +42,6 @@ #include #include #include -#include #include #include @@ -193,48 +192,8 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) std::string block(""); YamlWriter writer(outfile); - // lammps_version - writer.emit("lammps_version", lmp->version); - - // date_generated - std::time_t now = time(NULL); - block = utils::trim(ctime(&now)); - writer.emit("date_generated", block); - - // epsilon - writer.emit("epsilon", config.epsilon); - - // skip tests - block.clear(); - for (auto &skip_tests : config.skip_tests) { - if (block.empty()) block = skip_tests; - else block += " " + skip_tests; - } - writer.emit("skip_tests", block); - - // prerequisites - block.clear(); - for (auto &prerequisite : config.prerequisites) { - block += prerequisite.first + " " + prerequisite.second + "\n"; - } - writer.emit_block("prerequisites", block); - - // pre_commands - block.clear(); - for (auto &command : config.pre_commands) { - block += command + "\n"; - } - writer.emit_block("pre_commands", block); - - // post_commands - block.clear(); - for (auto &command : config.post_commands) { - block += command + "\n"; - } - writer.emit_block("post_commands", block); - - // input_file - writer.emit("input_file", config.input_file); + // write yaml header + write_yaml_header(&writer, &test_config, lmp->version); // natoms writer.emit("natoms", natoms); diff --git a/unittest/force-styles/test_improper_style.cpp b/unittest/force-styles/test_improper_style.cpp index 9e38c03cd1..2e61a1e0cb 100644 --- a/unittest/force-styles/test_improper_style.cpp +++ b/unittest/force-styles/test_improper_style.cpp @@ -38,7 +38,6 @@ #include #include #include -#include #include #include @@ -235,48 +234,8 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) YamlWriter writer(outfile); - // lammps_version - writer.emit("lammps_version", lmp->version); - - // date_generated - std::time_t now = time(NULL); - block = utils::trim(ctime(&now)); - writer.emit("date_generated", block); - - // epsilon - writer.emit("epsilon", config.epsilon); - - // skip tests - block.clear(); - for (auto &skip_tests : config.skip_tests) { - if (block.empty()) block = skip_tests; - else block += " " + skip_tests; - } - writer.emit("skip_tests", block); - - // prerequisites - block.clear(); - for (auto &prerequisite : config.prerequisites) { - block += prerequisite.first + " " + prerequisite.second + "\n"; - } - writer.emit_block("prerequisites", block); - - // pre_commands - block.clear(); - for (auto &command : config.pre_commands) { - block += command + "\n"; - } - writer.emit_block("pre_commands", block); - - // post_commands - block.clear(); - for (auto &command : config.post_commands) { - block += command + "\n"; - } - writer.emit_block("post_commands", block); - - // input_file - writer.emit("input_file", config.input_file); + // write yaml header + write_yaml_header(&writer, &test_config, lmp->version); // improper_style writer.emit("improper_style", config.improper_style); diff --git a/unittest/force-styles/test_main.cpp b/unittest/force-styles/test_main.cpp index 69a2b96822..e26eb6a8fb 100644 --- a/unittest/force-styles/test_main.cpp +++ b/unittest/force-styles/test_main.cpp @@ -16,16 +16,19 @@ #include "test_config.h" #include "test_config_reader.h" #include "utils.h" +#include "yaml_writer.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include #include +#include #include #include #include using LAMMPS_NS::utils::split_words; +using LAMMPS_NS::utils::trim; // common read_yaml_file function bool read_yaml_file(const char *infile, TestConfig &config) @@ -37,6 +40,55 @@ bool read_yaml_file(const char *infile, TestConfig &config) return true; } +// write out common header items for yaml files +void write_yaml_header(YamlWriter *writer, + TestConfig *cfg, + const char *version) +{ + // lammps_version + writer->emit("lammps_version", version); + + // date_generated + std::time_t now = time(NULL); + std::string block = trim(ctime(&now)); + writer->emit("date_generated", block); + + // epsilon + writer->emit("epsilon", cfg->epsilon); + + // skip tests + block.clear(); + for (auto &skip : cfg->skip_tests) { + if (block.empty()) block = skip; + else block += " " + skip; + } + writer->emit("skip_tests", block); + + // prerequisites + block.clear(); + for (auto &prerequisite : cfg->prerequisites) { + block += prerequisite.first + " " + prerequisite.second + "\n"; + } + writer->emit_block("prerequisites", block); + + // pre_commands + block.clear(); + for (auto &command : cfg->pre_commands) { + block += command + "\n"; + } + writer->emit_block("pre_commands", block); + + // post_commands + block.clear(); + for (auto &command : cfg->post_commands) { + block += command + "\n"; + } + writer->emit_block("post_commands", block); + + // input_file + writer->emit("input_file", cfg->input_file); +} + // need to be defined in unit test body extern void generate_yaml_file(const char *, const TestConfig &); diff --git a/unittest/force-styles/test_main.h b/unittest/force-styles/test_main.h index ee0be2a637..0f30467f6b 100644 --- a/unittest/force-styles/test_main.h +++ b/unittest/force-styles/test_main.h @@ -22,6 +22,10 @@ extern bool print_stats; extern bool verbose; extern std::string INPUT_FOLDER; +// convenience method to write out common entries +void write_yaml_header(class YamlWriter *writer, TestConfig *cfg, + const char *version); + #define EXPECT_FP_LE_WITH_EPS(val1, val2, eps) \ do { \ const double diff = fabs(val1 - val2); \ @@ -31,10 +35,11 @@ extern std::string INPUT_FOLDER; 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 + +#endif + diff --git a/unittest/force-styles/test_pair_style.cpp b/unittest/force-styles/test_pair_style.cpp index 0e61bacfbd..6a99fb72cd 100644 --- a/unittest/force-styles/test_pair_style.cpp +++ b/unittest/force-styles/test_pair_style.cpp @@ -40,7 +40,6 @@ #include #include #include -#include #include #include @@ -244,48 +243,8 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) YamlWriter writer(outfile); - // lammps_version - writer.emit("lammps_version", lmp->version); - - // date_generated - std::time_t now = time(NULL); - block = utils::trim(ctime(&now)); - writer.emit("date_generated", block); - - // epsilon - writer.emit("epsilon", config.epsilon); - - // skip tests - block.clear(); - for (auto &skip_tests : config.skip_tests) { - if (block.empty()) block = skip_tests; - else block += " " + skip_tests; - } - writer.emit("skip_tests", block); - - // prerequisites - block.clear(); - for (auto &prerequisite : config.prerequisites) { - block += prerequisite.first + " " + prerequisite.second + "\n"; - } - writer.emit_block("prerequisites", block); - - // pre_commands - block.clear(); - for (auto &command : config.pre_commands) { - block += command + "\n"; - } - writer.emit_block("pre_commands", block); - - // post_commands - block.clear(); - for (auto &command : config.post_commands) { - block += command + "\n"; - } - writer.emit_block("post_commands", block); - - // input_file - writer.emit("input_file", config.input_file); + // write yaml header + write_yaml_header(&writer, &test_config, lmp->version); // pair_style writer.emit("pair_style", config.pair_style);