From 92685a02167b3a0fde1d34fb8b81b26cdf61bc35 Mon Sep 17 00:00:00 2001
From: Elias Pipping <elias.pipping@fu-berlin.de>
Date: Mon, 10 Oct 2011 14:45:40 +0200
Subject: [PATCH] Add python test

---
 src/Makefile.am     | 15 +++++++++++----
 src/nicefunction.py |  8 ++++++++
 src/test-python.cc  | 47 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 66 insertions(+), 4 deletions(-)
 create mode 100644 src/nicefunction.py
 create mode 100644 src/test-python.cc

diff --git a/src/Makefile.am b/src/Makefile.am
index 0eba1530..dcddfb5f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -2,21 +2,26 @@
 SUBDIRS =
 
 check_PROGRAMS = \
+	test-python \
 	test-gradient-method
 
+test_python_SOURCES = \
+	test-python.cc
+
 test_gradient_method_SOURCES = \
 	test-gradient-method.cc \
 	mynonlinearity.hh \
 	nicefunction.hh \
 	samplefunctional.hh
 
-TESTS= test-gradient-method
+TESTS= test-python test-gradient-method
 
 AM_CXXFLAGS = -Wall -Wextra -Wno-unused-parameter
 
 AM_CPPFLAGS = \
 	$(DUNE_CPPFLAGS) \
-	$(ALUGRID_CPPFLAGS)
+	$(ALUGRID_CPPFLAGS) \
+	$(PYTHON_CPPFLAGS)
 
 # The libraries have to be given in reverse order (most basic libraries
 # last).  Also, due to some misunderstanding, a lot of libraries include the
@@ -24,10 +29,12 @@ AM_CPPFLAGS = \
 # here as well.
 LDADD = \
 	$(DUNE_LDFLAGS) $(DUNE_LIBS) \
-	$(ALUGRID_LDFLAGS) $(ALUGRID_LIBS)
+	$(ALUGRID_LDFLAGS) $(ALUGRID_LIBS) \
+	$(PYTHON_LIBS)
 AM_LDFLAGS = \
 	$(ALUGRID_LDFLAGS) \
-	$(DUNE_LDFLAGS)
+	$(DUNE_LDFLAGS) \
+	$(PYTHON_LDFLAGS)
 
 # don't follow the full GNU-standard
 # we need automake 1.5
diff --git a/src/nicefunction.py b/src/nicefunction.py
new file mode 100644
index 00000000..f4c4bbe6
--- /dev/null
+++ b/src/nicefunction.py
@@ -0,0 +1,8 @@
+class sampleFunction:
+    def __call__(self, x):
+        if x < 1:
+            return x
+        else:
+            return 2*x - 1
+
+Functions = {'sampleFunction' : sampleFunction()}
diff --git a/src/test-python.cc b/src/test-python.cc
new file mode 100644
index 00000000..28a3658a
--- /dev/null
+++ b/src/test-python.cc
@@ -0,0 +1,47 @@
+/* -*- mode:c++; mode:semantic -*- */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#ifndef HAVE_PYTHON
+#error Python is necessary
+#else
+#define EMBEDDED_PYTHON 1
+#endif
+
+#include <Python.h>
+
+#include <dune/common/exceptions.hh>
+#include <dune/common/stdstreams.hh>
+
+#include <dune/fufem/dunepython.hh>
+#include <dune/fufem/sharedpointermap.hh>
+
+int main() {
+  typedef Dune::VirtualFunction<double, double> Function;
+  typedef SharedPointerMap<std::string, Function> FunctionMap;
+  FunctionMap functions;
+
+  try {
+    Python::start();
+
+    Python::run("import sys");
+    Python::run(
+        "sys.path.append('../../src')"); // FIXME: use path to source here
+
+    Python::Reference module = Python::import("nicefunction");
+    Python::Reference funcRef = module.get("Functions");
+    funcRef.toC<FunctionMap::Base>(functions);
+
+    double const in(2.0);
+    Function const &foo = functions.get("sampleFunction");
+    double out;
+    foo.evaluate(in, out);
+
+    Python::stop();
+  }
+  catch (Dune::Exception &e) {
+    Dune::derr << "Dune reported error: " << e << std::endl;
+  }
+}
-- 
GitLab