diff options
Diffstat (limited to 'sca-cpp/trunk/modules/python/python-test.cpp')
-rw-r--r-- | sca-cpp/trunk/modules/python/python-test.cpp | 165 |
1 files changed, 59 insertions, 106 deletions
diff --git a/sca-cpp/trunk/modules/python/python-test.cpp b/sca-cpp/trunk/modules/python/python-test.cpp index 29a66cc2e7..44a72f8f88 100644 --- a/sca-cpp/trunk/modules/python/python-test.cpp +++ b/sca-cpp/trunk/modules/python/python-test.cpp @@ -37,12 +37,12 @@ const string testPythonAdd = "def add(x, y):\n" " return x + y\n"; -bool testEvalExpr() { - gc_scoped_pool pool; +const bool testEvalExpr() { + const gc_scoped_pool pool; PythonRuntime py; istringstream is(testPythonAdd); - failable<PyObject*> script = readScript("script1", "script1.py", is, py); + const failable<PyObject*> script = readScript("script1", "script1.py", is, py); assert(hasContent(script)); const value exp = mklist<value>("add", 2, 3); @@ -58,12 +58,12 @@ const string testPythonMap = "def addmap(x, y):\n" " return tuple(map(lambda i: i + y, x))\n"; -bool testEvalList() { - gc_scoped_pool pool; +const bool testEvalList() { + const gc_scoped_pool pool; PythonRuntime py; istringstream is(testPythonMap); - failable<PyObject*> script = readScript("script2", "script2.py", is, py); + const failable<PyObject*> script = readScript("script2", "script2.py", is, py); assert(hasContent(script)); const value exp = mklist<value>("addmap", mklist<value>(1, 2, 3), 1); @@ -94,8 +94,8 @@ const string testCallLambda( "def testCallLambda(l, x, y):\n" " return l(x, y)\n"); -bool testEvalLambda() { - gc_scoped_pool pool; +const bool testEvalLambda() { + const gc_scoped_pool pool; PythonRuntime py; const value trl = mklist<value>("testReturnLambda"); @@ -104,7 +104,7 @@ bool testEvalLambda() { assert(hasContent(trlv)); assert(isLambda(content(trlv))); - const lambda<value(const list<value>&)> trll(content(trlv)); + const lvvlambda trll(content(trlv)); assert(trll(mklist<value>(2, 3)) == value(6)); istringstream tclis(testCallLambda); @@ -114,20 +114,20 @@ bool testEvalLambda() { assert(content(tclv) == value(6)); istringstream tcelis(testCallLambda); - const value tcel = mklist<value>("testCallLambda", lambda<value(const list<value>&)>(mult), 3, 4); + const value tcel = mklist<value>("testCallLambda", lvvlambda(mult), 3, 4); const failable<value> tcelv = evalScript(tcel, tcelis, py); assert(hasContent(tcelv)); assert(content(tcelv) == value(12)); return true; } -struct testEvalReadAdd { - PythonRuntime& py; - testEvalReadAdd(PythonRuntime& py) : py(py) { - } - const bool operator()() const { +const bool testEvalPerf() { + const gc_scoped_pool pool; + PythonRuntime py; + + const blambda erl = [&py]() -> const bool { istringstream is(testPythonAdd); - failable<PyObject*> script = readScript("script3", "script3.py", is, py); + const failable<PyObject*> script = readScript("script3", "script3.py", is, py); assert(hasContent(script)); const value exp = mklist<value>("add", 2, 3); @@ -137,51 +137,36 @@ struct testEvalReadAdd { releaseScript(content(script), py); return true; - } -}; - -struct testEvalAdd { - PyObject* script; - PythonRuntime& py; - testEvalAdd(PyObject* script, PythonRuntime& py) : script(script), py(py) { - } - const bool operator()() const { + }; + cout << "Python read + eval test " << time(erl, 5, 10000) << " ms" << endl; + + istringstream is(testPythonAdd); + const failable<PyObject*> fscript = readScript("script4", "script4.py", is, py); + assert(hasContent(fscript)); + + PyObject* const script = content(fscript); + const blambda el = [script, &py]() -> const bool { const value exp = mklist<value>("add", 2, 3); const failable<value> r = evalScript(exp, script, py); assert(hasContent(r)); assert(content(r) == value(5)); return true; - } -}; - -bool testEvalPerf() { - gc_scoped_pool pool; - PythonRuntime py; - - const lambda<bool()> erl = lambda<bool()>(testEvalReadAdd(py)); - cout << "Python read + eval test " << time(erl, 5, 10000) << " ms" << endl; - - istringstream is(testPythonAdd); - failable<PyObject*> script = readScript("script4", "script4.py", is, py); - assert(hasContent(script)); - - const lambda<bool()> el = lambda<bool()>(testEvalAdd(content(script), py)); + }; cout << "Python eval test " << time(el, 5, 10000) << " ms" << endl; - releaseScript(content(script), py); + releaseScript(script, py); return true; } #ifdef WANT_THREADS -struct testReadEvalAddLoop { - PythonRuntime& py; - testReadEvalAddLoop(PythonRuntime& py) : py(py) { - } - const bool operator()() const { +const list<future<bool> > submitReadEvals(worker& w, const int max, const int i, PythonRuntime& py) { + if (i == max) + return list<future<bool> >(); + const blambda func = [&py]() -> const bool { for (int i = 0; i < 100; i++) { istringstream is(testPythonAdd); - failable<PyObject*> script = readScript("script6", "script6.py", is, py); + const failable<PyObject*> script = readScript("script6", "script6.py", is, py); assert(hasContent(script)); const value exp = mklist<value>("add", 2, 3); @@ -192,15 +177,14 @@ struct testReadEvalAddLoop { releaseScript(content(script), py); } return true; - } -}; - -struct testEvalAddLoop { - PyObject* script; - PythonRuntime& py; - testEvalAddLoop(PyObject* script, PythonRuntime& py) : script(script), py(py) { - } - const bool operator()() const { + }; + return cons(submit(w, func), submitReadEvals(w, max, i + 1, py)); +} + +const list<future<bool> > submitEvals(worker& w, const int max, const int i, PyObject* const script, PythonRuntime& py) { + if (i == max) + return list<future<bool> >(); + const blambda func = [script, &py]() -> const bool { for (int i = 0; i < 100; i++) { const value exp = mklist<value>("add", 2, 3); const failable<value> r = evalScript(exp, script, py); @@ -208,75 +192,44 @@ struct testEvalAddLoop { assert(content(r) == value(5)); } return true; - } -}; - -const list<future<bool> > submitReadEvals(worker& w, const int max, const int i, PythonRuntime& py) { - if (i == max) - return list<future<bool> >(); - const lambda<bool()> func = lambda<bool()>(testReadEvalAddLoop(py)); - return cons(submit(w, func), submitReadEvals(w, max, i + 1, py)); -} - -const list<future<bool> > submitEvals(worker& w, const int max, const int i, PyObject* script, PythonRuntime& py) { - if (i == max) - return list<future<bool> >(); - const lambda<bool()> func = lambda<bool()>(testEvalAddLoop(script, py)); + }; return cons(submit(w, func), submitEvals(w, max, i + 1, script, py)); } -bool checkEvalResults(const list<future<bool> > r) { +const bool checkEvalResults(const list<future<bool> > r) { if (isNil(r)) return true; assert(car(r) == true); return checkEvalResults(cdr(r)); } -struct testReadEvalThreads { - worker& w; - const int max; - PythonRuntime& py; - testReadEvalThreads(worker& w, const int max, PythonRuntime& py) : w(w), max(max), py(py) { - } - const bool operator()() const { - const list<future<bool> > r(submitReadEvals(w, max, 0, py)); - checkEvalResults(r); - return true; - } -}; - -struct testEvalThreads { - worker& w; - const int max; - PyObject* script; - PythonRuntime& py; - testEvalThreads(worker& w, const int max, PyObject* script, PythonRuntime& py) : w(w), max(max), script(script), py(py) { - } - const bool operator()() const { - const list<future<bool> > r(submitEvals(w, max, 0, script, py)); - checkEvalResults(r); - return true; - } -}; - -bool testThreads() { - gc_scoped_pool pool; +const bool testThreads() { + const gc_scoped_pool pool; PythonRuntime py; const int max = 100; worker w(max); - const lambda<bool()> elr = lambda<bool()>(testReadEvalThreads(w, max, py)); + const blambda elr = [&w, max, &py]() -> const bool { + const list<future<bool> > r(submitReadEvals(w, max, 0, py)); + checkEvalResults(r); + return true; + }; cout << "Python eval + read thread test " << time(elr, 1, 1) / 10000.0 << " ms" << endl; istringstream is(testPythonAdd); - failable<PyObject*> script = readScript("script7", "script7.py", is, py); - assert(hasContent(script)); + const failable<PyObject*> fscript = readScript("script7", "script7.py", is, py); + assert(hasContent(fscript)); - const lambda<bool()> el = lambda<bool()>(testEvalThreads(w, max, content(script), py)); + PyObject* const script = content(fscript); + const blambda el = [&w, max, script, &py]() -> const bool { + const list<future<bool> > r(submitEvals(w, max, 0, script, py)); + checkEvalResults(r); + return true; + }; cout << "Python eval thread test " << time(el, 1, 1) / 10000.0 << " ms" << endl; - releaseScript(content(script), py); + releaseScript(script, py); return true; } @@ -286,7 +239,7 @@ bool testThreads() { } int main() { - tuscany::gc_scoped_pool p; + const tuscany::gc_scoped_pool p; tuscany::cout << "Testing..." << tuscany::endl; tuscany::python::testEvalExpr(); |