]> gerrit.simantics Code Review - simantics/python.git/blobdiff - org.simantics.pythonlink/test/org/simantics/pythonlink/test/TestPythonlink.java
Initial commit of Python Integration feature.
[simantics/python.git] / org.simantics.pythonlink / test / org / simantics / pythonlink / test / TestPythonlink.java
diff --git a/org.simantics.pythonlink/test/org/simantics/pythonlink/test/TestPythonlink.java b/org.simantics.pythonlink/test/org/simantics/pythonlink/test/TestPythonlink.java
new file mode 100644 (file)
index 0000000..bc6379d
--- /dev/null
@@ -0,0 +1,104 @@
+package org.simantics.pythonlink.test;\r
+\r
+import static org.junit.Assert.*;\r
+\r
+import org.junit.After;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.simantics.pythonlink.NDArray;\r
+import org.simantics.pythonlink.Python;\r
+import org.simantics.pythonlink.PythonContext;\r
+\r
+public class TestPythonlink {\r
+    PythonContext python;\r
+\r
+    @Before\r
+    public void setUp() throws Exception {\r
+        python = Python.openPythonContext();\r
+    }\r
+\r
+    @After\r
+    public void tearDown() throws Exception {\r
+        python.close();\r
+    }\r
+\r
+    @Test\r
+    public void testInteger() {\r
+        int input = 4;\r
+        python.setPythonIntegerVariable( "foo", input );\r
+        python.executePythonStatement( "bar = foo // 2" );\r
+        int output = python.getPythonIntegerVariable( "bar" );\r
+        assertEquals( input / 2, output, 0.0 );\r
+    }\r
+\r
+    @Test\r
+    public void testIntegerArray() {\r
+        int[] input = new int[] { 1, 2 };\r
+        python.setPythonIntegerArrayVariable( "foo", input );\r
+        python.executePythonStatement( "bar = foo\nbar.append(3)" );\r
+        int[] output = python.getPythonIntegerArrayVariable( "bar" );\r
+        assertArrayEquals( new int[] { 1, 2, 3 }, output );\r
+    }\r
+\r
+    @Test\r
+    public void testDouble() {\r
+        double input = 2.5;\r
+        python.setPythonDoubleVariable( "foo", input );\r
+        python.executePythonStatement( "bar = foo / 2" );\r
+        double output = python.getPythonDoubleVariable( "bar" );\r
+        assertEquals( input / 2, output, 0.0 );\r
+    }\r
+\r
+    @Test\r
+    public void testDoubleArray() {\r
+        double[] input = new double[] { 1.0, 2.0 };\r
+        python.setPythonDoubleArrayVariable( "foo", input );\r
+        python.executePythonStatement( "bar = foo\nbar.append(3.0)" );\r
+        double[] output = python.getPythonDoubleArrayVariable( "bar" );\r
+        assertArrayEquals( new double[] { 1.0, 2.0, 3.0 }, output, 0.0 );\r
+    }\r
+    \r
+    @Test\r
+    public void testString() {\r
+        String input = "Foo";\r
+        python.setPythonStringVariable( "foo", input );\r
+        python.executePythonStatement( "bar = foo + 'bar!'" );\r
+        String output = python.getPythonStringVariable( "bar" );\r
+        assertEquals( "Foobar!", output );\r
+    }    \r
+    \r
+    @Test\r
+    public void testStringArray() {\r
+        String[] input = new String[] { "Foo" };\r
+        python.setPythonStringArrayVariable( "foo", input );\r
+        python.executePythonStatement( "bar = foo + ['bar!']" );\r
+        String[] output = python.getPythonStringArrayVariable( "bar" );\r
+        assertArrayEquals( new String[] { "Foo", "bar!" }, output );\r
+    }\r
+    \r
+    @Test\r
+    public void testNumpyArrays() {\r
+        NDArray data = new NDArray(new int[] { 2, 3 }, new double[] { 1, 2, 3,  4, 5, 6 });\r
+        python.executePythonStatement( "import numpy" );\r
+        python.setPythonNDArrayVariable( "foo", data );\r
+        python.executePythonStatement( "bar = foo.cumsum(axis = 0)" );\r
+        NDArray result = python.getPythonNDArrayVariable( "bar" );\r
+        assertEquals(new NDArray(new int[] { 2, 3 }, new double[] { 1, 2, 3,  5, 7, 9 }), result);\r
+        assertEquals(5, result.getValue( 1, 0 ), 0.0);\r
+        assertEquals(3, result.getValue( 0, 2 ), 0.0);\r
+        assertEquals(9, result.getValue( 1, 2 ), 0.0);\r
+    }\r
+    \r
+    @Test\r
+    public void testNumpyArrays3() {\r
+        NDArray data = new NDArray(new int[] { 3, 2, 2 }, new double[] { 1, 2,  3, 4,   5, 6,  7, 8,   9, 10,  11, 12 });\r
+        python.executePythonStatement( "import numpy" );\r
+        python.setPythonNDArrayVariable( "foo", data );\r
+        python.executePythonStatement( "bar = numpy.ndarray(shape = (3,2,2), order='f')" );\r
+        python.executePythonStatement( "bar.flat = foo.cumsum(axis = 1).flat" );\r
+        NDArray result = python.getPythonNDArrayVariable( "bar" );\r
+        assertEquals(new NDArray(new int[] { 3, 2, 2 }, new double[] { 1, 2,  4, 6,   5, 6,  12, 14,   9, 10,  20, 22 }), result);\r
+        assertEquals(10, result.getValue( 2, 0, 1 ), 0.0);\r
+        assertEquals(12, result.getValue( 1, 1, 0 ), 0.0);\r
+    }\r
+}\r