add tests for using python invoke and python style variables
This commit is contained in:
@ -3,8 +3,10 @@
|
|||||||
# from the executable, so the shared library is not needed. The
|
# from the executable, so the shared library is not needed. The
|
||||||
# availability of the PYTHON package is tested for inside the tester.
|
# availability of the PYTHON package is tested for inside the tester.
|
||||||
|
|
||||||
|
set(TEST_INPUT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
add_executable(test_python_package test_python_package.cpp)
|
add_executable(test_python_package test_python_package.cpp)
|
||||||
target_link_libraries(test_python_package PRIVATE lammps GTest::GMock GTest::GTest)
|
target_link_libraries(test_python_package PRIVATE lammps GTest::GMock GTest::GTest)
|
||||||
|
target_compile_definitions(test_python_package PRIVATE -DTEST_INPUT_FOLDER=${TEST_INPUT_FOLDER})
|
||||||
add_test(NAME PythonPackage COMMAND test_python_package WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
add_test(NAME PythonPackage COMMAND test_python_package WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||||
set_tests_properties(PythonPackage PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR};PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}:${LAMMPS_PYTHON_DIR}:$ENV{PYTHONPATH}")
|
set_tests_properties(PythonPackage PROPERTIES ENVIRONMENT "LAMMPS_POTENTIALS=${LAMMPS_POTENTIALS_DIR};PYTHONPATH=${CMAKE_CURRENT_BINARY_DIR}:${LAMMPS_PYTHON_DIR}:$ENV{PYTHONPATH}")
|
||||||
|
|
||||||
|
|||||||
18
unittest/python/func.py
Normal file
18
unittest/python/func.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
from __future__ import print_function
|
||||||
|
|
||||||
|
def square(val):
|
||||||
|
return val*val
|
||||||
|
|
||||||
|
def printnum():
|
||||||
|
print("2.25")
|
||||||
|
|
||||||
|
def printtxt():
|
||||||
|
print("sometext")
|
||||||
|
|
||||||
|
def getidxvar(lmpptr):
|
||||||
|
from lammps import lammps, LMP_VAR_EQUAL
|
||||||
|
lmp = lammps(ptr=lmpptr)
|
||||||
|
|
||||||
|
val = lmp.extract_variable("idx",None,LMP_VAR_EQUAL)
|
||||||
|
print(val)
|
||||||
@ -20,14 +20,19 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
// location of '*.py' files required by tests
|
||||||
|
#define STRINGIFY(val) XSTR(val)
|
||||||
|
#define XSTR(val) #val
|
||||||
|
std::string INPUT_FOLDER = STRINGIFY(TEST_INPUT_FOLDER);
|
||||||
|
|
||||||
// whether to print verbose output (i.e. not capturing LAMMPS screen output).
|
// whether to print verbose output (i.e. not capturing LAMMPS screen output).
|
||||||
bool verbose = false;
|
bool verbose = false;
|
||||||
|
|
||||||
using LAMMPS_NS::utils::split_words;
|
using LAMMPS_NS::utils::split_words;
|
||||||
|
|
||||||
namespace LAMMPS_NS {
|
namespace LAMMPS_NS {
|
||||||
using ::testing::Eq;
|
using ::testing::StrEq;
|
||||||
|
using ::testing::MatchesRegex;
|
||||||
|
|
||||||
class PythonPackageTest : public ::testing::Test {
|
class PythonPackageTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
@ -52,6 +57,7 @@ protected:
|
|||||||
lmp->input->one("pair_style zero 2.0");
|
lmp->input->one("pair_style zero 2.0");
|
||||||
lmp->input->one("pair_coeff * *");
|
lmp->input->one("pair_coeff * *");
|
||||||
lmp->input->one("mass * 1.0");
|
lmp->input->one("mass * 1.0");
|
||||||
|
lmp->input->one("variable input_dir index " + INPUT_FOLDER);
|
||||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,6 +71,50 @@ protected:
|
|||||||
|
|
||||||
TEST_F(PythonPackageTest, python_invoke)
|
TEST_F(PythonPackageTest, python_invoke)
|
||||||
{
|
{
|
||||||
|
// execute python function from file
|
||||||
|
if (!verbose) ::testing::internal::CaptureStdout();
|
||||||
|
lmp->input->one("python printnum file ${input_dir}/func.py");
|
||||||
|
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||||
|
::testing::internal::CaptureStdout();
|
||||||
|
lmp->input->one("python printnum invoke");
|
||||||
|
std::string output = ::testing::internal::GetCapturedStdout();
|
||||||
|
if (verbose) std::cout << output;
|
||||||
|
ASSERT_THAT(output, MatchesRegex("python.*2.25.*"));
|
||||||
|
|
||||||
|
// execute another python function from same file
|
||||||
|
if (!verbose) ::testing::internal::CaptureStdout();
|
||||||
|
lmp->input->one("python printtxt exists");
|
||||||
|
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||||
|
::testing::internal::CaptureStdout();
|
||||||
|
lmp->input->one("python printtxt invoke");
|
||||||
|
output = ::testing::internal::GetCapturedStdout();
|
||||||
|
if (verbose) std::cout << output;
|
||||||
|
ASSERT_THAT(output, MatchesRegex("python.*sometext.*"));
|
||||||
|
|
||||||
|
// execute python function that uses the LAMMPS python module
|
||||||
|
if (!verbose) ::testing::internal::CaptureStdout();
|
||||||
|
lmp->input->one("variable idx equal 2.25");
|
||||||
|
lmp->input->one("python getidxvar input 1 SELF format p exists");
|
||||||
|
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||||
|
::testing::internal::CaptureStdout();
|
||||||
|
lmp->input->one("python getidxvar invoke");
|
||||||
|
output = ::testing::internal::GetCapturedStdout();
|
||||||
|
if (verbose) std::cout << output;
|
||||||
|
ASSERT_THAT(output, MatchesRegex("python.*2.25.*"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(PythonPackageTest, python_variable)
|
||||||
|
{
|
||||||
|
if (!verbose) ::testing::internal::CaptureStdout();
|
||||||
|
lmp->input->one("variable sq python square");
|
||||||
|
lmp->input->one("variable val index 1.5");
|
||||||
|
lmp->input->one("python square input 1 v_val return v_sq format ff file ${input_dir}/func.py");
|
||||||
|
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||||
|
::testing::internal::CaptureStdout();
|
||||||
|
lmp->input->one("print \"${sq}\"");
|
||||||
|
std::string output = ::testing::internal::GetCapturedStdout();
|
||||||
|
if (verbose) std::cout << output;
|
||||||
|
ASSERT_THAT(output, MatchesRegex("print.*2.25.*"));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace LAMMPS_NS
|
} // namespace LAMMPS_NS
|
||||||
|
|||||||
Reference in New Issue
Block a user