Debian Codegraph @

test/jpypetest/test_buffer.py

raw symbols SCIP
1 # *****************************************************************************
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 # See NOTICE file for details.
16 #
17 # *****************************************************************************
18 import jpype
19 import sys
20 import logging
21 import time
22 import common
23 from jpype.types import *
24
25
26 try:
27 import numpy as np
28 gotNP = True
29 except ImportError:
30 gotNP = False
31
32
33 def haveNumpy():
34 return gotNP
35
36
37 class BufferTestCase(common.JPypeTestCase):
38 def setUp(self):
39 common.JPypeTestCase.setUp(self)
40
41 def testToMemoryview(self):
42 data = [1, 2, 3, 4, 5]
43 ja = JArray(JInt)(data)
44 m1 = memoryview(ja)
45
46 def testDoubleOpen(self):
47 data = [1, 2, 3, 4, 5]
48 ja = JArray(JInt)(data)
49 m1 = memoryview(ja)
50 m2 = memoryview(ja)
51 del m1
52 del m2
53
54 def testBytesToBytes(self):
55 data = [0, 1, 2, 3, 4, 5]
56 ja = JArray(JByte)(data)
57 self.assertEqual(len(bytes(ja)), 6)
58
59 def testShortToBytes(self):
60 data = [0, 1, 2, 3, 4, 5]
61 ja = JArray(JShort)(data)
62 self.assertEqual(len(bytes(ja)), 6 * 2)
63
64 def testIntToBytes(self):
65 data = [0, 1, 2, 3, 4, 5]
66 ja = JArray(JInt)(data)
67 self.assertEqual(len(bytes(ja)), 6 * 4)
68
69 def testLongToBytes(self):
70 data = [0, 1, 2, 3, 4, 5]
71 ja = JArray(JLong)(data)
72 self.assertEqual(len(bytes(ja)), 6 * 8)
73
74 def testMemoryViewWriteShort(self):
75 data = [1, 2, 3, 4, 5]
76 ja = JArray(JShort)(data)
77 b = memoryview(ja).cast("B") # Create a view
78 with self.assertRaises(TypeError):
79 b[0] = 123 # Alter the memory using the view
80
81 def testMemoryViewWriteInt(self):
82 data = [1, 2, 3, 4, 5]
83 ja = JArray(JInt)(data)
84 b = memoryview(ja).cast("B") # Create a view
85 with self.assertRaises(TypeError):
86 b[0] = 123 # Alter the memory using the view
87
88 def testMemoryViewWriteLong(self):
89 data = [1, 2, 3, 4, 5]
90 ja = JArray(JLong)(data)
91 b = memoryview(ja).cast("B") # Create a view
92 with self.assertRaises(TypeError):
93 b[0] = 123 # Alter the memory using the view
94
95 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
96 def testSlice(self):
97 data = [0, 1, 2, 3, 4, 5]
98 ja = JArray(JInt)(data)
99 self.assertTrue(bytes(ja[1:3]) == bytes(
100 np.array(data[1:3], dtype=np.int32)))
101 self.assertTrue(bytes(ja[1:]) == bytes(
102 np.array(data[1:], dtype=np.int32)))
103 self.assertTrue(bytes(ja[:-1]) ==
104 bytes(np.array(data[:-1], dtype=np.int32)))
105 self.assertTrue(bytes(ja[::2]) == bytes(
106 np.array(data[::2], dtype=np.int32)))
107 self.assertTrue(bytes(ja[::-1]) ==
108 bytes(np.array(data[::-1], dtype=np.int32)))
109 self.assertTrue(bytes(ja[::-2]) ==
110 bytes(np.array(data[::-2], dtype=np.int32)))
111
112 def executeConvert(self, jtype, dtype):
113 n = 100
114 na = np.random.randint(0, 1, size=n).astype(np.bool_)
115 self.assertTrue(np.all(np.array(jtype(na), dtype=dtype)
116 == np.array(na, dtype=dtype)))
117 na = np.random.randint(-2**7, 2**7 - 1, size=n, dtype=np.int8)
118 self.assertTrue(np.all(np.array(jtype(na), dtype=dtype)
119 == np.array(na, dtype=dtype)))
120 na = np.random.randint(0, 2**8 - 1, size=n, dtype=np.uint8)
121 self.assertTrue(np.all(np.array(jtype(na), dtype=dtype)
122 == np.array(na, dtype=dtype)))
123 na = np.random.randint(-2**15, 2**15 - 1, size=n, dtype=np.int16)
124 self.assertTrue(np.all(np.array(jtype(na), dtype=dtype)
125 == np.array(na, dtype=dtype)))
126 na = np.random.randint(0, 2**16 - 1, size=n, dtype=np.uint16)
127 self.assertTrue(np.all(np.array(jtype(na), dtype=dtype)
128 == np.array(na, dtype=dtype)))
129 na = np.random.randint(-2**31, 2**31 - 1, size=n, dtype=np.int32)
130 self.assertTrue(np.all(np.array(jtype(na), dtype=dtype)
131 == np.array(na, dtype=dtype)))
132 na = np.random.randint(0, 2**32 - 1, size=n, dtype=np.uint32)
133 self.assertTrue(np.all(np.array(jtype(na), dtype=dtype)
134 == np.array(na, dtype=dtype)))
135 na = np.random.randint(-2**63, 2**63 - 1, size=n, dtype=np.int64)
136 self.assertTrue(np.all(np.array(jtype(na), dtype=dtype)
137 == np.array(na, dtype=dtype)))
138 na = np.random.randint(0, 2**64 - 1, size=n, dtype=np.uint64)
139 self.assertTrue(np.allclose(np.array(jtype(na), dtype=dtype),
140 np.array(na, dtype=dtype), atol=1e-8))
141 na = np.random.random(n).astype(np.float32)
142 self.assertTrue(np.all(np.array(jtype(na), dtype=dtype)
143 == np.array(na, dtype=dtype)))
144 na = np.random.random(n).astype(np.float64)
145 self.assertTrue(np.all(np.array(jtype(na), dtype=dtype)
146 == np.array(na, dtype=dtype)))
147
148 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
149 def testBoolConvert(self):
150 self.executeConvert(JArray(JBoolean), np.bool_)
151
152 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
153 def testByteConvert(self):
154 self.executeConvert(JArray(JByte), np.int8)
155
156 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
157 def testShortConvert(self):
158 self.executeConvert(JArray(JShort), np.int16)
159
160 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
161 def testIntConvert(self):
162 self.executeConvert(JArray(JInt), np.int32)
163
164 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
165 def testLongConvert(self):
166 self.executeConvert(JArray(JLong), np.int64)
167
168 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
169 def testFloatConvert(self):
170 self.executeConvert(JArray(JFloat), np.float32)
171
172 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
173 def testDoubleConvert(self):
174 self.executeConvert(JArray(JDouble), np.float64)
175
176 def executeIntTest(self, jtype, limits, size, dtype, code):
177 data = np.random.randint(limits[0], limits[1], size=size, dtype=dtype)
178 a = JArray(jtype, data.ndim)(data.tolist())
179 u = np.array(a)
180 mv = memoryview(a)
181 self.assertEqual(mv.format, code, "Type issue %s" % type(a))
182 self.assertEqual(mv.shape, data.shape)
183 self.assertTrue(mv.readonly)
184 self.assertEqual(u.dtype.type, dtype, "Problem with %s %s" % (jtype, dtype))
185 self.assertTrue(np.all(data == u))
186
187 def executeFloatTest(self, jtype, size, dtype, code):
188 data = np.random.rand(*size).astype(dtype)
189 a = JArray(jtype, data.ndim)(data)
190 u = np.array(a)
191 self.assertTrue(np.all(data == u))
192 mv = memoryview(a)
193 self.assertEqual(mv.format, code)
194 self.assertEqual(mv.shape, data.shape)
195 self.assertTrue(mv.readonly)
196
197 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
198 def testBooleanToNP1D(self):
199 self.executeIntTest(JBoolean, [0, 1], (100,), np.bool_, "?")
200
201 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
202 def testCharToNP1D(self):
203 self.executeIntTest(JChar, [0, 2**16], (100,), np.uint16, "H")
204
205 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
206 def testByteToNP1D(self):
207 self.executeIntTest(JByte, [-2**7, 2**7 - 1], (100,), np.int8, "b")
208
209 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
210 def testShortToNP1D(self):
211 self.executeIntTest(JShort, [-2**15, 2**15 - 1], (100,), np.int16, "h")
212
213 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
214 def testIntToNP1D(self):
215 self.executeIntTest(JInt, [-2**31, 2**31 - 1], (100,), np.int32, "=i")
216
217 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
218 def testLongToNP1D(self):
219 self.executeIntTest(JLong, [-2**63, 2**63 - 1], (100,), np.int64, "=q")
220
221 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
222 def testFloatToNP1D(self):
223 self.executeFloatTest(JFloat, (100,), np.float32, "f")
224
225 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
226 def testDoubleToNP1D(self):
227 self.executeFloatTest(JDouble, (100,), np.float64, "d")
228
229 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
230 def testBooleanToNP2D(self):
231 self.executeIntTest(JBoolean, [0, 1], (11, 10), np.bool_, "?")
232
233 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
234 def testCharToNP2D(self):
235 self.executeIntTest(JChar, [0, 2**16], (11, 10), np.uint16, "H")
236
237 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
238 def testByteToNP2D(self):
239 self.executeIntTest(JByte, [-2**7, 2**7 - 1], (11, 10), np.int8, "b")
240
241 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
242 def testShortToNP2D(self):
243 self.executeIntTest(JShort, [-2**15, 2**15 - 1], (11, 10), np.int16, "h")
244
245 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
246 def testIntToNP2D(self):
247 self.executeIntTest(JInt, [-2**31, 2**31 - 1], (11, 10), np.int32, "=i")
248
249 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
250 def testLongToNP2D(self):
251 self.executeIntTest(JLong, [-2**63, 2**63 - 1], (11, 10), np.int64, "=q")
252
253 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
254 def testFloatToNP2D(self):
255 self.executeFloatTest(JFloat, (11, 10), np.float32, "f")
256
257 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
258 def testDoubleToNP2D(self):
259 self.executeFloatTest(JDouble, (11, 10), np.float64, "d")
260
261 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
262 def testBooleanToNP3D(self):
263 self.executeIntTest(JBoolean, [0, 1], (11, 10, 9), np.bool_, "?")
264
265 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
266 def testCharToNP3D(self):
267 self.executeIntTest(JChar, [0, 2**16], (11, 10, 9), np.uint16, "H")
268
269 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
270 def testByteToNP3D(self):
271 self.executeIntTest(JByte, [-2**7, 2**7 - 1], (11, 10, 9), np.int8, "b")
272
273 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
274 def testShortToNP3D(self):
275 self.executeIntTest(JShort, [-2**15, 2**15 - 1],
276 (11, 10, 9), np.int16, "h")
277
278 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
279 def testIntToNP3D(self):
280 self.executeIntTest(JInt, [-2**31, 2**31 - 1],
281 (11, 10, 9), np.int32, "=i")
282
283 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
284 def testLongToNP3D(self):
285 self.executeIntTest(JLong, [-2**63, 2**63 - 1],
286 (11, 10, 9), np.int64, "=q")
287
288 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
289 def testFloatToNP3D(self):
290 self.executeFloatTest(JFloat, (11, 10, 9), np.float32, "f")
291
292 @common.unittest.skipUnless(haveNumpy(), "numpy not available")
293 def testDoubleToNP3D(self):
294 self.executeFloatTest(JDouble, (11, 10, 9), np.float64, "d")
295
296 def executeOrder(self, jtype, dtype):
297 a = np.array([1, 2, 3])
298 ja2 = jtype[:](a)
299 for order in ("=", "<", ">"):
300 dt = np.dtype(dtype).newbyteorder(order)
301 a = np.array([1, 2, 3], dtype=dt)
302 ja = jtype[:](a)
303 self.assertTrue(jpype.java.util.Arrays.equals(ja, ja2), "Order issue with %s %s" % (jtype, dtype))
304
305 @common.requireNumpy
306 def testOrder(self):
307 for i in (jpype.JBoolean, jpype.JByte, jpype.JShort, jpype.JInt, jpype.JLong, jpype.JFloat, jpype.JDouble):
308 for j in (np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64, np.float32, np.float64):
309 self.executeOrder(i, j)
310 for j in (np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64):
311 self.executeOrder(jpype.JChar, j)
312
313 def testMemoryByte(self):
314 mv = memoryview(bytes(256))
315 jtype = jpype.JByte[:]
316
317 # Simple checks
318 for dtype in "c?bBhHiIlLqQfdnN":
319 jtype(mv.cast(dtype))
320 jtype(mv.cast("@" + dtype))
321
322 for dtype in "spP":
323 with self.assertRaises(Exception):
324 jtype(mv.cast(dtype))
325
326 def testMemoryInt(self):
327 mv = memoryview(bytes(256))
328 jtype = jpype.JInt[:]
329
330 # Simple checks
331 for dtype in "c?bBhHiIlLqQfdnN":
332 print(dtype)
333 jtype(mv.cast(dtype))
334 jtype(mv.cast("@" + dtype))
335
336 for dtype in "spP":
337 with self.assertRaises(Exception):
338 jtype(mv.cast(dtype))
339
340 def testMemoryShort(self):
341 mv = memoryview(bytes(256))
342 jtype = jpype.JShort[:]
343
344 # Simple checks
345 for dtype in "c?bBhHiIlLqQfdnN":
346 jtype(mv.cast(dtype))
347 jtype(mv.cast("@" + dtype))
348
349 for dtype in "spP":
350 with self.assertRaises(Exception):
351 jtype(mv.cast(dtype))
352
353 def testMemoryLong(self):
354 mv = memoryview(bytes(256))
355 jtype = jpype.JLong[:]
356
357 # Simple checks
358 for dtype in "c?bBhHiIlLqQfdnN":
359 jtype(mv.cast(dtype))
360 jtype(mv.cast("@" + dtype))
361
362 for dtype in "spP":
363 with self.assertRaises(Exception):
364 jtype(mv.cast(dtype))
365
366 def testMemoryFloat(self):
367 mv = memoryview(bytes(256))
368 jtype = jpype.JFloat[:]
369
370 # Simple checks
371 for dtype in "c?bBhHiIlLqQfdnN":
372 jtype(mv.cast(dtype))
373 jtype(mv.cast("@" + dtype))
374
375 for dtype in "spP":
376 with self.assertRaises(Exception):
377 jtype(mv.cast(dtype))
378
379 def testMemoryDouble(self):
380 mv = memoryview(bytes(256))
381 jtype = jpype.JDouble[:]
382
383 # Simple checks
384 for dtype in "c?bBhHiIlLqQfdnN":
385 jtype(mv.cast(dtype))
386 jtype(mv.cast("@" + dtype))
387
388 for dtype in "spP":
389 with self.assertRaises(Exception):
390 jtype(mv.cast(dtype))
391
392 def testMemoryBoolean(self):
393 mv = memoryview(bytes(256))
394 jtype = jpype.JBoolean[:]
395
396 # Simple checks
397 for dtype in "c?bBhHiIlLqQfdnN":
398 jtype(mv.cast(dtype))
399 jtype(mv.cast("@" + dtype))
400
401 for dtype in "spP":
402 with self.assertRaises(Exception):
403 jtype(mv.cast(dtype))
404
405 def testMemoryChar(self):
406 mv = memoryview(bytes(256))
407 jtype = jpype.JChar[:]
408
409 # Simple checks
410 for dtype in "c?bBhHiIlLqQnN":
411 jtype(mv.cast(dtype))
412 jtype(mv.cast("@" + dtype))
413
414 jtype(mv.cast("P"))
415 for dtype in ("s", "p", "f", "d", "@f", "@d"):
416 with self.assertRaises(Exception):
417 jtype(mv.cast(dtype))