Refactor existing tests
This commit is contained in:
@ -20,6 +20,7 @@
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
// location of '*.py' files required by tests
|
// location of '*.py' files required by tests
|
||||||
#define STRINGIFY(val) XSTR(val)
|
#define STRINGIFY(val) XSTR(val)
|
||||||
@ -43,86 +44,107 @@ protected:
|
|||||||
|
|
||||||
void command(const std::string &line) { lmp->input->one(line.c_str()); }
|
void command(const std::string &line) { lmp->input->one(line.c_str()); }
|
||||||
|
|
||||||
|
void HIDE_OUTPUT(std::function<void()> f) {
|
||||||
|
if (!verbose) ::testing::internal::CaptureStdout();
|
||||||
|
f();
|
||||||
|
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string CAPTURE_OUTPUT(std::function<void()> f) {
|
||||||
|
::testing::internal::CaptureStdout();
|
||||||
|
f();
|
||||||
|
auto output = ::testing::internal::GetCapturedStdout();
|
||||||
|
if (verbose) std::cout << output;
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
void SetUp() override
|
void SetUp() override
|
||||||
{
|
{
|
||||||
const char *args[] = {"PythonPackageTest", "-log", "none", "-echo", "screen", "-nocite"};
|
const char *args[] = {"PythonPackageTest", "-log", "none", "-echo", "screen", "-nocite"};
|
||||||
char **argv = (char **)args;
|
char **argv = (char **)args;
|
||||||
int argc = sizeof(args) / sizeof(char *);
|
int argc = sizeof(args) / sizeof(char *);
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
HIDE_OUTPUT([&] {
|
||||||
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
|
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
|
||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
});
|
||||||
ASSERT_NE(lmp, nullptr);
|
ASSERT_NE(lmp, nullptr);
|
||||||
info = new Info(lmp);
|
info = new Info(lmp);
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
if (!info->has_package("PYTHON")) GTEST_SKIP();
|
||||||
command("units real");
|
HIDE_OUTPUT([&] {
|
||||||
command("dimension 3");
|
command("units real");
|
||||||
command("region box block -4 4 -4 4 -4 4");
|
command("dimension 3");
|
||||||
command("create_box 1 box");
|
command("region box block -4 4 -4 4 -4 4");
|
||||||
command("create_atoms 1 single 0.0 0.0 0.0 units box");
|
command("create_box 1 box");
|
||||||
command("create_atoms 1 single 1.9 -1.9 1.9999 units box");
|
command("create_atoms 1 single 0.0 0.0 0.0 units box");
|
||||||
command("pair_style zero 2.0");
|
command("create_atoms 1 single 1.9 -1.9 1.9999 units box");
|
||||||
command("pair_coeff * *");
|
command("pair_style zero 2.0");
|
||||||
command("mass * 1.0");
|
command("pair_coeff * *");
|
||||||
command("variable input_dir index " + INPUT_FOLDER);
|
command("mass * 1.0");
|
||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
command("variable input_dir index " + INPUT_FOLDER);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void TearDown() override
|
void TearDown() override
|
||||||
{
|
{
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
HIDE_OUTPUT([&] {
|
||||||
delete info;
|
delete info;
|
||||||
delete lmp;
|
delete lmp;
|
||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
info = nullptr;
|
||||||
|
lmp = nullptr;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(PythonPackageTest, python_invoke)
|
TEST_F(PythonPackageTest, InvokeFunctionFromFile)
|
||||||
{
|
{
|
||||||
if (!info->has_package("PYTHON")) GTEST_SKIP();
|
|
||||||
// execute python function from file
|
// execute python function from file
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
HIDE_OUTPUT([&] {
|
||||||
command("python printnum file ${input_dir}/func.py");
|
command("python printnum file ${input_dir}/func.py");
|
||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
});
|
||||||
::testing::internal::CaptureStdout();
|
|
||||||
command("python printnum invoke");
|
auto output = CAPTURE_OUTPUT([&]() {
|
||||||
std::string output = ::testing::internal::GetCapturedStdout();
|
command("python printnum invoke");
|
||||||
if (verbose) std::cout << output;
|
});
|
||||||
ASSERT_THAT(output, HasSubstr("2.25\n"));
|
ASSERT_THAT(output, HasSubstr("2.25\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(PythonPackageTest, InvokeOtherFunctionFromFile)
|
||||||
|
{
|
||||||
// execute another python function from same file
|
// execute another python function from same file
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
HIDE_OUTPUT([&] {
|
||||||
command("python printtxt exists");
|
command("python printnum file ${input_dir}/func.py");
|
||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
command("python printtxt exists");
|
||||||
::testing::internal::CaptureStdout();
|
});
|
||||||
command("python printtxt invoke");
|
|
||||||
output = ::testing::internal::GetCapturedStdout();
|
|
||||||
if (verbose) std::cout << output;
|
|
||||||
ASSERT_THAT(output, HasSubstr("sometext\n"));
|
|
||||||
|
|
||||||
|
auto output = CAPTURE_OUTPUT([&] {
|
||||||
|
command("python printtxt invoke");
|
||||||
|
});
|
||||||
|
ASSERT_THAT(output, HasSubstr("sometext\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(PythonPackageTest, InvokeFunctionThatUsesLAMMPSModule)
|
||||||
|
{
|
||||||
// execute python function that uses the LAMMPS python module
|
// execute python function that uses the LAMMPS python module
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
HIDE_OUTPUT([&] {
|
||||||
command("variable idx equal 2.25");
|
command("python printnum file ${input_dir}/func.py");
|
||||||
command("python getidxvar input 1 SELF format p exists");
|
command("variable idx equal 2.25");
|
||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
command("python getidxvar input 1 SELF format p exists");
|
||||||
::testing::internal::CaptureStdout();
|
});
|
||||||
command("python getidxvar invoke");
|
auto output = CAPTURE_OUTPUT([&] {
|
||||||
output = ::testing::internal::GetCapturedStdout();
|
command("python getidxvar invoke");
|
||||||
if (verbose) std::cout << output;
|
});
|
||||||
ASSERT_THAT(output, HasSubstr("2.25\n"));
|
ASSERT_THAT(output, HasSubstr("2.25\n"));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(PythonPackageTest, python_variable)
|
TEST_F(PythonPackageTest, python_variable)
|
||||||
{
|
{
|
||||||
if (!info->has_package("PYTHON")) GTEST_SKIP();
|
HIDE_OUTPUT([&] {
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
command("variable sq python square");
|
||||||
command("variable sq python square");
|
command("variable val index 1.5");
|
||||||
command("variable val index 1.5");
|
command("python square input 1 v_val return v_sq format ff file ${input_dir}/func.py");
|
||||||
command("python square input 1 v_val return v_sq format ff file ${input_dir}/func.py");
|
});
|
||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
std::string output = CAPTURE_OUTPUT([&] {
|
||||||
::testing::internal::CaptureStdout();
|
command("print \"${sq}\"");
|
||||||
command("print \"${sq}\"");
|
});
|
||||||
std::string output = ::testing::internal::GetCapturedStdout();
|
|
||||||
if (verbose) std::cout << output;
|
|
||||||
ASSERT_THAT(output, MatchesRegex("print.*2.25.*"));
|
ASSERT_THAT(output, MatchesRegex("print.*2.25.*"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user