1 package org.simantics.pythonlink.test;
\r
3 import static org.junit.Assert.*;
\r
5 import org.junit.After;
\r
6 import org.junit.Before;
\r
7 import org.junit.Test;
\r
8 import org.simantics.pythonlink.NDArray;
\r
9 import org.simantics.pythonlink.Python;
\r
10 import org.simantics.pythonlink.PythonContext;
\r
12 public class TestPythonlink {
\r
13 PythonContext python;
\r
16 public void setUp() throws Exception {
\r
17 python = Python.openPythonContext();
\r
21 public void tearDown() throws Exception {
\r
26 public void testInteger() {
\r
28 python.setPythonIntegerVariable( "foo", input );
\r
29 python.executePythonStatement( "bar = foo // 2" );
\r
30 int output = python.getPythonIntegerVariable( "bar" );
\r
31 assertEquals( input / 2, output, 0.0 );
\r
35 public void testIntegerArray() {
\r
36 int[] input = new int[] { 1, 2 };
\r
37 python.setPythonIntegerArrayVariable( "foo", input );
\r
38 python.executePythonStatement( "bar = foo\nbar.append(3)" );
\r
39 int[] output = python.getPythonIntegerArrayVariable( "bar" );
\r
40 assertArrayEquals( new int[] { 1, 2, 3 }, output );
\r
44 public void testDouble() {
\r
46 python.setPythonDoubleVariable( "foo", input );
\r
47 python.executePythonStatement( "bar = foo / 2" );
\r
48 double output = python.getPythonDoubleVariable( "bar" );
\r
49 assertEquals( input / 2, output, 0.0 );
\r
53 public void testDoubleArray() {
\r
54 double[] input = new double[] { 1.0, 2.0 };
\r
55 python.setPythonDoubleArrayVariable( "foo", input );
\r
56 python.executePythonStatement( "bar = foo\nbar.append(3.0)" );
\r
57 double[] output = python.getPythonDoubleArrayVariable( "bar" );
\r
58 assertArrayEquals( new double[] { 1.0, 2.0, 3.0 }, output, 0.0 );
\r
62 public void testString() {
\r
63 String input = "Foo";
\r
64 python.setPythonStringVariable( "foo", input );
\r
65 python.executePythonStatement( "bar = foo + 'bar!'" );
\r
66 String output = python.getPythonStringVariable( "bar" );
\r
67 assertEquals( "Foobar!", output );
\r
71 public void testStringArray() {
\r
72 String[] input = new String[] { "Foo" };
\r
73 python.setPythonStringArrayVariable( "foo", input );
\r
74 python.executePythonStatement( "bar = foo + ['bar!']" );
\r
75 String[] output = python.getPythonStringArrayVariable( "bar" );
\r
76 assertArrayEquals( new String[] { "Foo", "bar!" }, output );
\r
80 public void testNumpyArrays() {
\r
81 NDArray data = new NDArray(new int[] { 2, 3 }, new double[] { 1, 2, 3, 4, 5, 6 });
\r
82 python.executePythonStatement( "import numpy" );
\r
83 python.setPythonNDArrayVariable( "foo", data );
\r
84 python.executePythonStatement( "bar = foo.cumsum(axis = 0)" );
\r
85 NDArray result = python.getPythonNDArrayVariable( "bar" );
\r
86 assertEquals(new NDArray(new int[] { 2, 3 }, new double[] { 1, 2, 3, 5, 7, 9 }), result);
\r
87 assertEquals(5, result.getValue( 1, 0 ), 0.0);
\r
88 assertEquals(3, result.getValue( 0, 2 ), 0.0);
\r
89 assertEquals(9, result.getValue( 1, 2 ), 0.0);
\r
93 public void testNumpyArrays3() {
\r
94 NDArray data = new NDArray(new int[] { 3, 2, 2 }, new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
\r
95 python.executePythonStatement( "import numpy" );
\r
96 python.setPythonNDArrayVariable( "foo", data );
\r
97 python.executePythonStatement( "bar = numpy.ndarray(shape = (3,2,2), order='f')" );
\r
98 python.executePythonStatement( "bar.flat = foo.cumsum(axis = 1).flat" );
\r
99 NDArray result = python.getPythonNDArrayVariable( "bar" );
\r
100 assertEquals(new NDArray(new int[] { 3, 2, 2 }, new double[] { 1, 2, 4, 6, 5, 6, 12, 14, 9, 10, 20, 22 }), result);
\r
101 assertEquals(10, result.getValue( 2, 0, 1 ), 0.0);
\r
102 assertEquals(12, result.getValue( 1, 1, 0 ), 0.0);
\r