Debian Codegraph @

test/jpypetest/test_jlong.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 sys
19 import jpype
20 import common
21 import random
22 import _jpype
23 import jpype
24 from jpype import java
25 from jpype.types import *
26 try:
27 import numpy as np
28 except ImportError:
29 pass
30
31 VALUES = [random.randint(-2**63, 2**63 - 1) for i in range(10)]
32
33
34 class JLongTestCase(common.JPypeTestCase):
35 def setUp(self):
36 common.JPypeTestCase.setUp(self)
37 self.cls = JClass("jpype.common.Fixture")
38 self.fixture = self.cls()
39
40 @common.requireInstrumentation
41 def testJPNumberLong_int(self):
42 jd = JLong(1)
43 _jpype.fault("PyJPNumberLong_int")
44 with self.assertRaisesRegex(SystemError, "fault"):
45 int(jd)
46 _jpype.fault("PyJPModule_getContext")
47 with self.assertRaisesRegex(SystemError, "fault"):
48 int(jd)
49 int(jd)
50
51 @common.requireInstrumentation
52 def testJPNumberLong_float(self):
53 jd = JLong(1)
54 _jpype.fault("PyJPNumberLong_float")
55 with self.assertRaisesRegex(SystemError, "fault"):
56 float(jd)
57 _jpype.fault("PyJPModule_getContext")
58 with self.assertRaisesRegex(SystemError, "fault"):
59 float(jd)
60 float(jd)
61
62 @common.requireInstrumentation
63 def testJPNumberLong_str(self):
64 jd = JLong(1)
65 _jpype.fault("PyJPNumberLong_str")
66 with self.assertRaisesRegex(SystemError, "fault"):
67 str(jd)
68 _jpype.fault("PyJPModule_getContext")
69 with self.assertRaisesRegex(SystemError, "fault"):
70 str(jd)
71 str(jd)
72
73 @common.requireInstrumentation
74 def testJPNumberLong_repr(self):
75 jd = JLong(1)
76 _jpype.fault("PyJPNumberLong_repr")
77 with self.assertRaisesRegex(SystemError, "fault"):
78 repr(jd)
79 _jpype.fault("PyJPModule_getContext")
80 with self.assertRaisesRegex(SystemError, "fault"):
81 repr(jd)
82 repr(jd)
83
84 @common.requireInstrumentation
85 def testJPNumberLong_compare(self):
86 jd = JLong(1)
87 _jpype.fault("PyJPNumberLong_compare")
88 with self.assertRaisesRegex(SystemError, "fault"):
89 jd == 1
90 _jpype.fault("PyJPModule_getContext")
91 with self.assertRaisesRegex(SystemError, "fault"):
92 jd == 1
93 jd == 1
94
95 @common.requireInstrumentation
96 def testJPNumberLong_hash(self):
97 jd = JLong(1)
98 _jpype.fault("PyJPNumberLong_hash")
99 with self.assertRaises(SystemError):
100 hash(jd)
101 _jpype.fault("PyJPModule_getContext")
102 with self.assertRaises(SystemError):
103 hash(jd)
104 hash(jd)
105
106 @common.requireInstrumentation
107 def testFault(self):
108 _jpype.fault("JPLongType::findJavaConversion")
109 with self.assertRaises(SystemError):
110 JLong(1.0)
111
112 @common.requireInstrumentation
113 def testConversionFault(self):
114 _jpype.fault("JPLongType::findJavaConversion")
115 with self.assertRaisesRegex(SystemError, "fault"):
116 JLong._canConvertToJava(object())
117
118 @common.requireInstrumentation
119 def testArrayFault(self):
120 ja = JArray(JLong)(5)
121 _jpype.fault("JPJavaFrame::NewLongArray")
122 with self.assertRaisesRegex(SystemError, "fault"):
123 JArray(JLong)(1)
124 _jpype.fault("JPJavaFrame::SetLongArrayRegion")
125 with self.assertRaisesRegex(SystemError, "fault"):
126 ja[0] = 0
127 _jpype.fault("JPJavaFrame::GetLongArrayRegion")
128 with self.assertRaisesRegex(SystemError, "fault"):
129 print(ja[0])
130 _jpype.fault("JPJavaFrame::GetLongArrayElements")
131 # Special case, only BufferError is allowed from getBuffer
132 with self.assertRaises(BufferError):
133 memoryview(ja[0:3])
134 _jpype.fault("JPJavaFrame::ReleaseLongArrayElements")
135 with self.assertRaisesRegex(SystemError, "fault"):
136 ja[0:3] = bytes([1, 2, 3])
137 _jpype.fault("JPJavaFrame::ReleaseLongArrayElements")
138 with self.assertRaisesRegex(SystemError, "fault"):
139 jpype.JObject(ja[::2], jpype.JObject)
140 _jpype.fault("JPJavaFrame::ReleaseLongArrayElements")
141
142 def f():
143 # Special case no fault is allowed
144 memoryview(ja[0:3])
145 f()
146 _jpype.fault("JPLongType::setArrayRange")
147 with self.assertRaisesRegex(SystemError, "fault"):
148 ja[1:3] = [0, 0]
149
150 def testFromJLongWiden_2(self):
151 self.assertEqual(JLong(JByte(123)), 123)
152 self.assertEqual(JLong(JShort(12345)), 12345)
153 self.assertEqual(JLong(JInt(12345678)), 12345678)
154 self.assertEqual(JLong(JLong(12345678)), 12345678)
155
156 def testFromJLongWiden_3(self):
157 self.assertEqual(JLong(JDouble(12345678)), 12345678)
158
159 def testFromNone(self):
160 with self.assertRaises(TypeError):
161 JLong(None)
162 self.assertEqual(JLong._canConvertToJava(None), "none")
163
164 def testUnBox(self):
165 self.assertEqual(JLong(java.lang.Double(1.2345)), 1)
166
167 def testFromFloat(self):
168 self.assertEqual(JLong._canConvertToJava(1.2345), "explicit")
169
170 @jpype.JImplements("java.util.function.LongSupplier")
171 class q(object):
172 @jpype.JOverride
173 def getAsLong(self):
174 return 4.5 # this will hit explicit conversion
175 self.assertEqual(JObject(q()).getAsLong(), 4)
176
177 def testFromLong(self):
178 self.assertEqual(JLong(12345), 12345)
179 self.assertEqual(JLong._canConvertToJava(12345), "implicit")
180
181 def testFromObject(self):
182 with self.assertRaises(TypeError):
183 JLong(object())
184 with self.assertRaises(TypeError):
185 JLong(JObject())
186 with self.assertRaises(TypeError):
187 JLong(JString("A"))
188 self.assertEqual(JLong._canConvertToJava(object()), "none")
189 ja = JArray(JLong)(5)
190 with self.assertRaises(TypeError):
191 ja[1] = object()
192 jf = JClass("jpype.common.Fixture")
193 with self.assertRaises(TypeError):
194 jf.static_long_field = object()
195 with self.assertRaises(TypeError):
196 jf().long_field = object()
197
198 def testCallFloatFromNone(self):
199 with self.assertRaises(TypeError):
200 self.fixture.callFloat(None)
201 with self.assertRaises(TypeError):
202 self.fixture.static_long_field = None
203 with self.assertRaises(TypeError):
204 self.fixture.long_field = None
205
206 def testThrow(self):
207 # Check throw
208 with self.assertRaises(JException):
209 self.fixture.throwInt()
210 with self.assertRaises(JException):
211 self.cls.throwStaticInt()
212 with self.assertRaises(JException):
213 self.fixture.throwStaticInt()
214
215 def checkType(self, q):
216 # Check field
217 self.fixture.long_field = q
218 self.assertEqual(self.fixture.long_field, q)
219 self.assertEqual(self.fixture.getLong(), q)
220 # Check static field
221 self.cls.static_long_field = q
222 self.assertEqual(self.fixture.static_long_field, q)
223 self.assertEqual(self.fixture.getStaticLong(), q)
224 self.assertEqual(self.cls.getStaticLong(), q)
225 # Check call
226 self.assertEqual(self.fixture.callLong(q), q)
227 self.assertEqual(self.cls.callStaticLong(q), q)
228
229 def checkTypeFail(self, q, exc=TypeError):
230 with self.assertRaises(exc):
231 self.fixture.long_field = q
232 with self.assertRaises(exc):
233 self.fixture.callLong(q)
234 with self.assertRaises(exc):
235 self.fixture.callStaticLong(q)
236
237 def testCastFloat(self):
238 self.fixture.long_field = JLong(6.0)
239 self.assertEqual(self.fixture.long_field, 6)
240
241 def testCheckInt(self):
242 self.checkType(1)
243
244 def testCheckFloat(self):
245 self.checkTypeFail(2.0)
246
247 def testCheckRange(self):
248 self.checkType(2**63 - 1)
249 self.checkType(-2**63)
250 self.checkTypeFail(2**63, exc=OverflowError)
251 self.checkTypeFail(-2**63 - 1, exc=OverflowError)
252
253 def testExplicitRange(self):
254 # These will not overflow as they are explicit casts
255 self.assertEqual(JLong(2**64), 0)
256 self.assertEqual(JLong(-2**64), 0)
257
258 def testCheckBool(self):
259 self.checkType(True)
260 self.checkType(False)
261
262 def testCheckJBoolean(self):
263 self.checkTypeFail(JBoolean(True))
264 self.checkTypeFail(JBoolean(False))
265
266 def testCheckJChar(self):
267 self.checkType(JChar("A"))
268
269 def testCheckJByte(self):
270 self.checkType(JByte(-128))
271 self.checkType(JByte(127))
272
273 def testCheckJShort(self):
274 self.checkType(JShort(-2**15))
275 self.checkType(JShort(2**15 - 1))
276
277 def testCheckJLong(self):
278 self.checkType(JLong(-2**31 + 1))
279 self.checkType(JLong(2**31 - 1))
280
281 def testCheckJLong_2(self):
282 self.checkType(JLong(-2**63 + 1))
283 self.checkType(JLong(2**63 - 1))
284
285 @common.requireNumpy
286 def testCheckNumpyInt8(self):
287 self.checkType(np.random.randint(-127, 128, dtype=np.int8))
288 self.checkType(np.random.randint(0, 255, dtype=np.uint8))
289 self.checkType(np.uint8(0))
290 self.checkType(np.uint8(255))
291 self.checkType(np.int8(-128))
292 self.checkType(np.int8(127))
293
294 @common.requireNumpy
295 def testCheckNumpyInt16(self):
296 self.checkType(np.random.randint(-2**15, 2**15 - 1, dtype=np.int16))
297 self.checkType(np.random.randint(0, 2**16 - 1, dtype=np.uint16))
298 self.checkType(np.uint16(0))
299 self.checkType(np.uint16(2**16 - 1))
300 self.checkType(np.int16(-2**15))
301 self.checkType(np.int16(2**15 - 1))
302
303 @common.requireNumpy
304 def testCheckNumpyInt32(self):
305 self.checkType(np.uint32(0))
306 self.checkType(np.uint32(2**32 - 1))
307 self.checkType(np.int32(-2**31))
308 self.checkType(np.int32(2**31 - 1))
309
310 @common.requireNumpy
311 def testCheckNumpyInt64(self):
312 #self.checkTypeFail(np.random.randint(-2**63,2**63-1, dtype=np.int64))
313 # FIXME OverflowError
314 #self.checkType(np.uint64(np.random.randint(0,2**64-1, dtype=np.uint64)))
315 # FIXME OverflowError
316 # self.checkType(np.uint64(2**64-1))
317 self.checkType(np.int64(-2**63))
318 self.checkType(np.int64(2**63 - 1))
319
320 @common.requireNumpy
321 def testCheckNumpyFloat32(self):
322 self.checkTypeFail(np.float32(np.random.rand()))
323
324 @common.requireNumpy
325 def testCheckNumpyFloat64(self):
326 self.checkTypeFail(np.float64(np.random.rand()))
327
328 def checkArrayType(self, a, expected):
329 # Check init
330 ja = JArray(JLong)(a)
331 self.assertElementsEqual(ja, expected)
332 ja = JArray(JLong)(len(a))
333 ja[:] = a
334 self.assertElementsEqual(ja, expected)
335 return ja
336
337 def checkArrayTypeFail(self, a):
338 # Check init
339 ja = JArray(JLong)(a)
340 ja = JArray(JLong)(len(a))
341 ja[:] = a
342
343 def testArrayConversion(self):
344 a = [random.randint(-2**31, 2**31) for i in range(100)]
345 jarr = self.checkArrayType(a, a)
346 result = jarr[2:10]
347 self.assertEqual(len(a[2:10]), len(result))
348 self.assertElementsAlmostEqual(a[2:10], result)
349
350 # empty slice
351 result = jarr[-1:3]
352 expected = a[-1:3]
353 self.assertElementsAlmostEqual(expected, result)
354
355 result = jarr[3:-2]
356 expected = a[3:-2]
357 self.assertElementsAlmostEqual(expected, result)
358
359 @common.requireNumpy
360 def testArrayInitFromNPInt(self):
361 a = np.random.randint(-2**31, 2**31 - 1, size=100, dtype=np.int_)
362 self.checkArrayType(a, a)
363
364 @common.requireNumpy
365 def testArrayInitFromNPInt8(self):
366 a = np.random.randint(-2**7, 2**7 - 1, size=100, dtype=np.int8)
367 self.checkArrayType(a, a)
368
369 @common.requireNumpy
370 def testArrayInitFromNPInt16(self):
371 a = np.random.randint(-2**15, 2**15 - 1, size=100, dtype=np.int16)
372 self.checkArrayType(a, a)
373
374 @common.requireNumpy
375 def testArrayInitFromNPInt32(self):
376 a = np.random.randint(-2**31, 2**31 - 1, size=100, dtype=np.int32)
377 self.checkArrayType(a, a)
378
379 @common.requireNumpy
380 def testArrayInitFromNPInt64(self):
381 a = np.random.randint(-2**63, 2**63 - 1, size=100, dtype=np.int64)
382 self.checkArrayType(a, a.astype(np.int64))
383
384 @common.requireNumpy
385 def testArrayInitFromNPFloat32(self):
386 a = np.random.random(100).astype(np.float32)
387 self.checkArrayType(a, a.astype(np.int64))
388
389 @common.requireNumpy
390 def testArrayInitFromNPFloat64(self):
391 a = np.random.random(100).astype(np.float64)
392 self.checkArrayType(a, a.astype(np.int64))
393
394 def testArraySetRange(self):
395 ja = JArray(JLong)(3)
396 ja[0:1] = [123]
397 self.assertEqual(ja[0], 123)
398 ja[0:1] = [-1]
399 self.assertEqual(ja[0], -1)
400 with self.assertRaises(TypeError):
401 ja[0:1] = [1.000]
402 with self.assertRaises(TypeError):
403 ja[0:1] = [java.lang.Double(321)]
404 with self.assertRaises(TypeError):
405 ja[0:1] = [object()]
406
407 def testArrayConversionFail(self):
408 jarr = JArray(JLong)(VALUES)
409 with self.assertRaises(TypeError):
410 jarr[1] = 'a'
411
412 def testArraySliceLength(self):
413 jarr = JArray(JLong)(VALUES)
414 jarr[1:2] = [1]
415 with self.assertRaises(ValueError):
416 jarr[1:2] = [1, 2, 3]
417
418 def testArrayConversionInt(self):
419 jarr = JArray(JLong)(VALUES)
420 result = jarr[0: len(jarr)]
421 self.assertElementsEqual(VALUES, result)
422 result = jarr[2:10]
423 self.assertElementsEqual(VALUES[2:10], result)
424
425 def testArrayConversionError(self):
426 jarr = JArray(JLong, 1)(10)
427 with self.assertRaises(TypeError):
428 jarr[1:2] = [dict()]
429 # -1 is returned by python, if conversion fails also, ensure this works
430 jarr[1:2] = [-1]
431
432 def testArrayClone(self):
433 array = JArray(JLong, 2)([[1, 2], [3, 4]])
434 carray = array.clone()
435 # Verify the first dimension is cloned
436 self.assertFalse(array.equals(carray))
437 # Copy is shallow
438 self.assertTrue(array[0].equals(carray[0]))
439
440 def testArrayGetSlice(self):
441 contents = VALUES
442 array = JArray(JLong)(contents)
443 self.assertEqual(list(array[1:]), contents[1:])
444 self.assertEqual(list(array[:-1]), contents[:-1])
445 self.assertEqual(list(array[1:-1]), contents[1:-1])
446
447 def testArraySetSlice(self):
448 contents = [1, 2, 3, 4]
449 array = JArray(JLong)(contents)
450 array[1:] = [5, 6, 7]
451 contents[1:] = [5, 6, 7]
452 self.assertEqual(list(array[:]), contents[:])
453 array[:-1] = [8, 9, 10]
454 contents[:-1] = [8, 9, 10]
455 self.assertEqual(list(array[:]), contents[:])
456
457 def testArrayGetSliceStep(self):
458 contents = VALUES
459 array = JArray(JLong)(contents)
460 self.assertEqual(list(array[::2]), contents[::2])
461 self.assertEqual(list(array[::3]), contents[::3])
462 self.assertEqual(list(array[::4]), contents[::4])
463 self.assertEqual(list(array[::5]), contents[::5])
464 self.assertEqual(list(array[::6]), contents[::6])
465 self.assertEqual(list(array[::7]), contents[::7])
466 self.assertEqual(list(array[::8]), contents[::8])
467 self.assertEqual(list(array[1::3]), contents[1::3])
468 self.assertEqual(list(array[1:-2:3]), contents[1:-2:3])
469
470 def testArraySliceStepNeg(self):
471 contents = VALUES
472 array = JArray(JLong)(contents)
473 self.assertEqual(list(array[::-1]), contents[::-1])
474 self.assertEqual(list(array[::-2]), contents[::-2])
475 self.assertEqual(list(array[::-3]), contents[::-3])
476 self.assertEqual(list(array[::-4]), contents[::-4])
477 self.assertEqual(list(array[::-5]), contents[::-5])
478 self.assertEqual(list(array[::-6]), contents[::-6])
479 self.assertEqual(list(array[2::-3]), contents[2::-3])
480 self.assertEqual(list(array[-2::-3]), contents[-2::-3])
481
482 def testArraySetArraySliceStep(self):
483 contents = [1, 2, 3, 4, 5, 6]
484 array = JArray(JLong)(contents)
485 array[::2] = [5, 6, 7]
486 contents[::2] = [5, 6, 7]
487 self.assertEqual(list(array[:]), contents[:])
488
489 def testArrayEquals(self):
490 contents = VALUES
491 array = JArray(JLong)(contents)
492 array2 = JArray(JLong)(contents)
493 self.assertEqual(array, array)
494 self.assertNotEqual(array, array2)
495
496 def testArrayIter(self):
497 contents = VALUES
498 array = JArray(JLong)(contents)
499 contents2 = [i for i in array]
500 self.assertEqual(contents, contents2)
501
502 def testArrayGetOutOfBounds(self):
503 contents = [1, 2, 3, 4]
504 array = JArray(JLong)(contents)
505 with self.assertRaises(IndexError):
506 array[5]
507 self.assertEqual(array[-1], contents[-1])
508 self.assertEqual(array[-4], contents[-4])
509 with self.assertRaises(IndexError):
510 array[-5]
511
512 def testArraySetOutOfBounds(self):
513 contents = [1, 2, 3, 4]
514 array = JArray(JLong)(contents)
515 with self.assertRaises(IndexError):
516 array[5] = 1
517 array[-1] = 5
518 contents[-1] = 5
519 array[-4] = 6
520 contents[-4] = 6
521 self.assertEqual(list(array[:]), contents)
522 with self.assertRaises(IndexError):
523 array[-5] = 1
524
525 def testArraySliceCast(self):
526 JA = JArray(JLong)
527 ja = JA(VALUES)
528 ja2 = ja[::2]
529 jo = jpype.JObject(ja2, jpype.JObject)
530 ja3 = jpype.JObject(jo, JA)
531 self.assertEqual(type(jo), jpype.JClass("java.lang.Object"))
532 self.assertEqual(type(ja2), JA)
533 self.assertEqual(type(ja3), JA)
534 self.assertEqual(list(ja2), list(ja3))
535
536 def testArrayReverse(self):
537 n = list(VALUES)
538 ja = JArray(JLong)(n)
539 a = [i for i in reversed(ja)]
540 n = [i for i in reversed(n)]
541 self.assertEqual(a, n)
542
543 def testArrayHash(self):
544 ja = JArray(JLong)([1, 2, 3])
545 self.assertIsInstance(hash(ja), int)
546
547 @common.requireNumpy
548 def testArrayBufferDims(self):
549 ja = JArray(JLong)(5)
550 a = np.zeros((5, 2))
551 with self.assertRaisesRegex(TypeError, "incorrect"):
552 ja[:] = a
553
554 def testArrayBadItem(self):
555 class q(object):
556 def __int__(self):
557 raise SystemError("nope")
558
559 def __index__(self):
560 raise SystemError("nope")
561 ja = JArray(JLong)(5)
562 a = [1, -1, q(), 3, 4]
563 with self.assertRaisesRegex(SystemError, "nope"):
564 ja[:] = a
565
566 def testArrayBadDims(self):
567 class q(bytes):
568 # Lie about our length
569 def __len__(self):
570 return 5
571 a = q([1, 2, 3])
572 ja = JArray(JInt)(5)
573 with self.assertRaisesRegex(ValueError, "Slice"):
574 ja[:] = [1, 2, 3]
575 with self.assertRaisesRegex(ValueError, "mismatch"):
576 ja[:] = a