Debian Codegraph @

src/test/python/jpy_modretparam_test.py

raw symbols SCIP
1 import unittest
2 import array
3 import sys
4
5 import jpyutil
6
7
8 jpyutil.init_jvm(jvm_maxmem='512M', jvm_classpath=['target/test-classes'])
9 import jpy
10
11 try:
12 import numpy as np
13 except:
14 np = None
15
16
17 def annotate_fixture_methods(type, method):
18 # print('annotate_fixture_methods: type =', type, ', method =', method.name)
19 if method.name == 'modifyThing':
20 method.set_param_mutable(0, True)
21 elif method.name == 'returnThing':
22 method.set_param_return(0, True)
23 elif method.name == 'modifyAndReturnThing':
24 method.set_param_mutable(0, True)
25 method.set_param_return(0, True)
26 elif method.name == 'modifyIntArray':
27 method.set_param_mutable(0, True)
28 elif method.name == 'returnIntArray':
29 method.set_param_return(0, False)
30 method.set_param_return(0, True)
31 elif method.name == 'modifyAndReturnIntArray':
32 method.set_param_mutable(0, True)
33 method.set_param_return(0, True)
34 elif method.name == 'modifyAndOutputIntArray':
35 method.set_param_mutable(0, True)
36 method.set_param_output(0, True)
37 return True
38
39
40 jpy.type_callbacks['org.jpy.fixtures.ModifyAndReturnParametersTestFixture'] = annotate_fixture_methods
41
42
43 class TestMutableAndReturnParameters(unittest.TestCase):
44 def setUp(self):
45 self.Fixture = jpy.get_type('org.jpy.fixtures.ModifyAndReturnParametersTestFixture')
46 self.assertIsNotNone(self.Fixture)
47 self.Thing = jpy.get_type('org.jpy.fixtures.Thing')
48 self.assertIsNotNone(self.Thing)
49
50
51 def test_modifyThing(self):
52 fixture = self.Fixture()
53 t = self.Thing()
54 self.assertEqual(t.getValue(), 0)
55 fixture.modifyThing(t, 11)
56 self.assertEqual(t.getValue(), 11)
57
58
59 def test_returnThing(self):
60 fixture = self.Fixture()
61
62 t1 = self.Thing(21)
63 t2 = fixture.returnThing(t1)
64 self.assertEqual(t1.getValue(), 21)
65 self.assertEqual(t2.getValue(), 21)
66 self.assertEqual(t1, t2)
67 self.assertTrue(t1 is t2)
68
69 t1 = None
70 t2 = fixture.returnThing(t1)
71 self.assertIsNotNone(t2)
72 self.assertEqual(t2.getValue(), 0)
73
74
75 def test_modifyAndReturnThing(self):
76 fixture = self.Fixture()
77
78 t1 = self.Thing()
79 t2 = fixture.modifyAndReturnThing(t1, 31)
80 self.assertEqual(t1.getValue(), 31)
81 self.assertEqual(t2.getValue(), 31)
82 self.assertEqual(t1, t2)
83 self.assertTrue(t1 is t2)
84
85 t1 = None
86 t2 = fixture.modifyAndReturnThing(t1, 32)
87 self.assertIsNotNone(t2)
88 self.assertEqual(t2.getValue(), 32)
89
90
91 def test_modifyIntArray(self):
92 fixture = self.Fixture()
93
94 # See https://docs.python.org/2/c-api/buffer.html
95 # "An array can only expose its contents via the old-style buffer interface.
96 # This limitation does not apply to Python 3, where memoryview objects can be
97 # constructed from arrays, too."
98 # >>> import array
99 # >>> a = array.array('i', [1,2,3])
100 # >>> m = memoryview(a)
101 # TypeError: cannot make memory view because object does not have the buffer interface
102 #
103 if sys.version_info >= (3, 0, 0):
104 a = array.array('i', [0, 0, 0])
105 fixture.modifyIntArray(a, 12, 13, 14)
106 self.assertEqual(a[0], 12)
107 self.assertEqual(a[1], 13)
108 self.assertEqual(a[2], 14)
109
110 if np:
111 a = np.array([0, 0, 0], dtype='int32')
112 fixture.modifyIntArray(a, 10, 11, 12)
113 self.assertEqual(a[0], 10)
114 self.assertEqual(a[1], 11)
115 self.assertEqual(a[2], 12)
116
117 a = jpy.array('int', 3)
118 fixture.modifyIntArray(a, 12, 13, 14)
119 self.assertEqual(a[0], 12)
120 self.assertEqual(a[1], 13)
121 self.assertEqual(a[2], 14)
122
123 a = [0, 0, 0]
124 fixture.modifyIntArray(a, 12, 13, 14)
125 self.assertEqual(a[0], 0)
126 self.assertEqual(a[1], 0)
127 self.assertEqual(a[2], 0)
128
129 with self.assertRaises(RuntimeError, msg='RuntimeError expected') as e:
130 a = None
131 fixture.modifyIntArray(a, 14, 15, 16)
132 self.assertTrue(str(e.exception).startswith('java.lang.NullPointerException'))
133
134
135 def test_returnIntArray(self):
136 fixture = self.Fixture()
137
138 # See https://docs.python.org/2/c-api/buffer.html
139 # "An array can only expose its contents via the old-style buffer interface.
140 # This limitation does not apply to Python 3, where memoryview objects can be
141 # constructed from arrays, too."
142 # >>> import array
143 # >>> a = array.array('i', [1,2,3])
144 # >>> m = memoryview(a)
145 # TypeError: cannot make memory view because object does not have the buffer interface
146 #
147 if sys.version_info >= (3, 0, 0):
148 a1 = array.array('i', [0, 0, 0])
149 a2 = fixture.returnIntArray(a1)
150 # AssertionError: array('i', [0, 0, 0]) is not [I(objectRef=0x0778C364)
151 self.assertIs(a1, a2)
152
153 if np:
154 a1 = np.array([0, 0, 0], dtype='int32')
155 a2 = fixture.returnIntArray(a1)
156 self.assertIs(a1, a2)
157
158 a1 = jpy.array('int', 3)
159 a2 = fixture.returnIntArray(a1)
160 self.assertIs(a1, a2)
161
162 a1 = None
163 a2 = fixture.returnIntArray(a1)
164 self.assertEqual(type(a2), jpy.get_type('[I'))
165
166 a1 = [0, 0, 0]
167 a2 = fixture.returnIntArray(a1)
168 self.assertEqual(type(a2), jpy.get_type('[I'))
169
170
171 def test_modifyAndReturnIntArray(self):
172 fixture = self.Fixture()
173
174 # See https://docs.python.org/2/c-api/buffer.html
175 # "An array can only expose its contents via the old-style buffer interface.
176 # This limitation does not apply to Python 3, where memoryview objects can be
177 # constructed from arrays, too."
178 # >>> import array
179 # >>> a = array.array('i', [1,2,3])
180 # >>> m = memoryview(a)
181 # TypeError: cannot make memory view because object does not have the buffer interface
182 #
183 if sys.version_info >= (3, 0, 0):
184 a1 = array.array('i', [0, 0, 0])
185 # Python 2.7: TypeError: must be impossible<bad format char>, not bool
186 a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18)
187 self.assertIs(a1, a2)
188 self.assertEqual(a2[0], 16)
189 self.assertEqual(a2[1], 17)
190 self.assertEqual(a2[2], 18)
191
192 if np:
193 a1 = np.array([0, 0, 0], dtype='int32')
194 a2 = fixture.modifyAndReturnIntArray(a1, 10, 11, 12)
195 self.assertIs(a1, a2)
196 self.assertEqual(a2[0], 10)
197 self.assertEqual(a2[1], 11)
198 self.assertEqual(a2[2], 12)
199
200 a1 = jpy.array('int', 3)
201 a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18)
202 self.assertIs(a1, a2)
203 self.assertEqual(a2[0], 16)
204 self.assertEqual(a2[1], 17)
205 self.assertEqual(a2[2], 18)
206
207 a1 = None
208 a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18)
209 self.assertEqual(type(a2), jpy.get_type('[I'))
210 self.assertEqual(a2[0], 16)
211 self.assertEqual(a2[1], 17)
212 self.assertEqual(a2[2], 18)
213
214 a1 = [0, 0, 0]
215 a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18)
216 self.assertEqual(type(a2), jpy.get_type('[I'))
217 self.assertEqual(a2[0], 16)
218 self.assertEqual(a2[1], 17)
219 self.assertEqual(a2[2], 18)
220
221
222 # See https://github.com/bcdev/jpy/issues/36
223 #
224 def test_modifyAndOutputIntArray(self):
225 fixture = self.Fixture()
226
227 # See https://docs.python.org/2/c-api/buffer.html
228 # "An array can only expose its contents via the old-style buffer interface.
229 # This limitation does not apply to Python 3, where memoryview objects can be
230 # constructed from arrays, too."
231 # >>> import array
232 # >>> a = array.array('i', [1,2,3])
233 # >>> m = memoryview(a)
234 # TypeError: cannot make memory view because object does not have the buffer interface
235 #
236 if sys.version_info >= (3, 0, 0):
237 a = array.array('i', [0, 0, 0])
238 fixture.modifyAndOutputIntArray(a, 16, 17, 18)
239 self.assertEqual(a[0], 16)
240 self.assertEqual(a[1], 17)
241 self.assertEqual(a[2], 18)
242
243 if np:
244 a = np.array([0, 0, 0], dtype='int32')
245 fixture.modifyIntArray(a, 10, 11, 12)
246 self.assertEqual(a[0], 10)
247 self.assertEqual(a[1], 11)
248 self.assertEqual(a[2], 12)
249
250 a = jpy.array('int', 3)
251 fixture.modifyAndOutputIntArray(a, 15, 16, 17)
252 self.assertEqual(a[0], 15)
253 self.assertEqual(a[1], 16)
254 self.assertEqual(a[2], 17)
255
256 a = None
257 fixture.modifyAndOutputIntArray(a, 16, 17, 18)
258
259 a = [0, 0, 0]
260 fixture.modifyAndOutputIntArray(a, 16, 17, 18)
261 self.assertEqual(a[0], 0)
262 self.assertEqual(a[1], 0)
263 self.assertEqual(a[2], 0)
264
265
266 if __name__ == '__main__':
267 print('\nRunning ' + __file__)
268 unittest.main()