Add tests to cover python command
This commit is contained in:
@ -506,6 +506,7 @@ int PythonImpl::create_entry(char *name)
|
|||||||
"cannot be used unless output is a string");
|
"cannot be used unless output is a string");
|
||||||
pfuncs[ifunc].length_longstr = length_longstr;
|
pfuncs[ifunc].length_longstr = length_longstr;
|
||||||
pfuncs[ifunc].longstr = new char[length_longstr+1];
|
pfuncs[ifunc].longstr = new char[length_longstr+1];
|
||||||
|
pfuncs[ifunc].longstr[length_longstr] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strstr(ostr,"v_") != ostr) error->all(FLERR,"Invalid python command");
|
if (strstr(ostr,"v_") != ostr) error->all(FLERR,"Invalid python command");
|
||||||
|
|||||||
@ -9,6 +9,11 @@ def bool_to_val(txt):
|
|||||||
return 1.0
|
return 1.0
|
||||||
return 0.0
|
return 0.0
|
||||||
|
|
||||||
|
def val_to_bool(val):
|
||||||
|
if val != 0:
|
||||||
|
return "True"
|
||||||
|
return "False"
|
||||||
|
|
||||||
def printnum():
|
def printnum():
|
||||||
print("2.25")
|
print("2.25")
|
||||||
|
|
||||||
@ -20,3 +25,6 @@ def getidxvar(lmpptr):
|
|||||||
lmp = lammps(ptr=lmpptr)
|
lmp = lammps(ptr=lmpptr)
|
||||||
val = lmp.extract_variable("idx")
|
val = lmp.extract_variable("idx")
|
||||||
print(val)
|
print(val)
|
||||||
|
|
||||||
|
def longstr():
|
||||||
|
return "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent metus."
|
||||||
|
|||||||
2
unittest/python/run.py
Normal file
2
unittest/python/run.py
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
from __future__ import print_function
|
||||||
|
print("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent metus.")
|
||||||
@ -15,6 +15,7 @@
|
|||||||
#include "info.h"
|
#include "info.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
#include "variable.h"
|
#include "variable.h"
|
||||||
|
#include "library.h"
|
||||||
#include "gmock/gmock.h"
|
#include "gmock/gmock.h"
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
@ -31,11 +32,14 @@ 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;
|
||||||
|
|
||||||
|
const char * LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent metus.";
|
||||||
|
|
||||||
using LAMMPS_NS::utils::split_words;
|
using LAMMPS_NS::utils::split_words;
|
||||||
|
|
||||||
namespace LAMMPS_NS {
|
namespace LAMMPS_NS {
|
||||||
using ::testing::MatchesRegex;
|
using ::testing::MatchesRegex;
|
||||||
using ::testing::StrEq;
|
using ::testing::StrEq;
|
||||||
|
using ::testing::Eq;
|
||||||
using ::testing::HasSubstr;
|
using ::testing::HasSubstr;
|
||||||
|
|
||||||
class PythonPackageTest : public ::testing::Test {
|
class PythonPackageTest : public ::testing::Test {
|
||||||
@ -45,6 +49,8 @@ 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 command_string(const std::string &lines) { lammps_commands_string(lmp, lines.c_str()); }
|
||||||
|
|
||||||
void HIDE_OUTPUT(std::function<void()> f) {
|
void HIDE_OUTPUT(std::function<void()> f) {
|
||||||
if (!verbose) ::testing::internal::CaptureStdout();
|
if (!verbose) ::testing::internal::CaptureStdout();
|
||||||
f();
|
f();
|
||||||
@ -66,6 +72,10 @@ protected:
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string get_variable_string(const std::string & name) {
|
||||||
|
return lmp->input->variable->retrieve(name.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
void SetUp() override
|
void SetUp() override
|
||||||
{
|
{
|
||||||
const char *args[] = {"PythonPackageTest", "-log", "none", "-echo", "screen", "-nocite"};
|
const char *args[] = {"PythonPackageTest", "-log", "none", "-echo", "screen", "-nocite"};
|
||||||
@ -121,6 +131,7 @@ TEST_F(PythonPackageTest, InvokeFunctionPassInt)
|
|||||||
HIDE_OUTPUT([&] {
|
HIDE_OUTPUT([&] {
|
||||||
command("variable sq python square");
|
command("variable sq python square");
|
||||||
command("python square input 1 2 format ii return v_sq file ${input_dir}/func.py");
|
command("python square input 1 2 format ii return v_sq file ${input_dir}/func.py");
|
||||||
|
command("python square invoke");
|
||||||
});
|
});
|
||||||
|
|
||||||
ASSERT_EQ(get_variable_value("sq"), 4.0);
|
ASSERT_EQ(get_variable_value("sq"), 4.0);
|
||||||
@ -169,6 +180,38 @@ TEST_F(PythonPackageTest, InvokeFunctionPassStringVariable)
|
|||||||
ASSERT_EQ(get_variable_value("val"), 0.0);
|
ASSERT_EQ(get_variable_value("val"), 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(PythonPackageTest, InvokeStringFunction)
|
||||||
|
{
|
||||||
|
// execute python function, passing string variable as argument
|
||||||
|
HIDE_OUTPUT([&] {
|
||||||
|
command("variable str python val_to_bool");
|
||||||
|
command("python val_to_bool input 1 v_val format is return v_str file ${input_dir}/func.py");
|
||||||
|
});
|
||||||
|
|
||||||
|
HIDE_OUTPUT([&] {
|
||||||
|
command("variable val equal 0");
|
||||||
|
});
|
||||||
|
|
||||||
|
ASSERT_THAT(get_variable_string("str"), StrEq("False"));
|
||||||
|
|
||||||
|
HIDE_OUTPUT([&] {
|
||||||
|
command("variable val equal 1");
|
||||||
|
});
|
||||||
|
|
||||||
|
ASSERT_THAT(get_variable_string("str"), StrEq("True"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(PythonPackageTest, InvokeLongStringFunction)
|
||||||
|
{
|
||||||
|
// execute python function, passing string variable as argument
|
||||||
|
HIDE_OUTPUT([&] {
|
||||||
|
command("variable str python longstr");
|
||||||
|
command("python longstr format s length 72 return v_str file ${input_dir}/func.py");
|
||||||
|
});
|
||||||
|
|
||||||
|
ASSERT_THAT(get_variable_string("str"), StrEq(LOREM_IPSUM));
|
||||||
|
}
|
||||||
|
|
||||||
TEST_F(PythonPackageTest, InvokeOtherFunctionFromFile)
|
TEST_F(PythonPackageTest, InvokeOtherFunctionFromFile)
|
||||||
{
|
{
|
||||||
// execute another python function from same file
|
// execute another python function from same file
|
||||||
@ -211,6 +254,61 @@ TEST_F(PythonPackageTest, python_variable)
|
|||||||
ASSERT_THAT(output, MatchesRegex("print.*2.25.*"));
|
ASSERT_THAT(output, MatchesRegex("print.*2.25.*"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(PythonPackageTest, InlineFunction)
|
||||||
|
{
|
||||||
|
// define variable that evaluates a python function
|
||||||
|
HIDE_OUTPUT([&] {
|
||||||
|
command("variable fact python factorial");
|
||||||
|
command("python factorial input 1 v_n return v_fact format ii here \"\"\"\n"
|
||||||
|
"def factorial(n):\n"
|
||||||
|
" if n == 0 or n == 1: return 1\n"
|
||||||
|
" return n*factorial(n-1)\n"
|
||||||
|
"\"\"\"");
|
||||||
|
});
|
||||||
|
|
||||||
|
HIDE_OUTPUT([&] {
|
||||||
|
command("variable n equal 1");
|
||||||
|
});
|
||||||
|
|
||||||
|
ASSERT_EQ(get_variable_value("fact"), 1.0);
|
||||||
|
|
||||||
|
HIDE_OUTPUT([&] {
|
||||||
|
command("variable n equal 2");
|
||||||
|
});
|
||||||
|
|
||||||
|
ASSERT_EQ(get_variable_value("fact"), 2.0);
|
||||||
|
|
||||||
|
HIDE_OUTPUT([&] {
|
||||||
|
command("variable n equal 3");
|
||||||
|
});
|
||||||
|
|
||||||
|
ASSERT_EQ(get_variable_value("fact"), 6.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(PythonPackageTest, RunSource)
|
||||||
|
{
|
||||||
|
// execute python script from file
|
||||||
|
auto output = CAPTURE_OUTPUT([&] {
|
||||||
|
command("python xyz source ${input_dir}/run.py");
|
||||||
|
});
|
||||||
|
|
||||||
|
ASSERT_THAT(output, HasSubstr(LOREM_IPSUM));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(PythonPackageTest, RunSourceInline)
|
||||||
|
{
|
||||||
|
// execute inline python script
|
||||||
|
auto output = CAPTURE_OUTPUT([&] {
|
||||||
|
command("python xyz source \"\"\"\n"
|
||||||
|
"from __future__ import print_function\n"
|
||||||
|
"print(2+2)\n"
|
||||||
|
"\"\"\""
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
ASSERT_THAT(output, HasSubstr("4"));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace LAMMPS_NS
|
} // namespace LAMMPS_NS
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
@ -220,7 +318,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
// handle arguments passed via environment variable
|
// handle arguments passed via environment variable
|
||||||
if (const char *var = getenv("TEST_ARGS")) {
|
if (const char *var = getenv("TEST_ARGS")) {
|
||||||
std::vector<std::string> env = split_words(var);
|
auto env = split_words(var);
|
||||||
for (auto arg : env) {
|
for (auto arg : env) {
|
||||||
if (arg == "-v") {
|
if (arg == "-v") {
|
||||||
verbose = true;
|
verbose = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user