File: test_array.py

package info (click to toggle)
python-jpype 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,984 kB
  • sloc: python: 18,767; cpp: 17,931; java: 8,448; xml: 1,305; makefile: 154; sh: 35
file content (603 lines) | stat: -rw-r--r-- 21,660 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# *****************************************************************************
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#   See NOTICE file for details.
#
# *****************************************************************************
import sys
import _jpype
import jpype
from jpype.types import *
from jpype import JPackage, java
import common
import pytest
try:
    import numpy as np
except ImportError:
    pass


class ArrayTestCase(common.JPypeTestCase):

    def setUp(self):
        common.JPypeTestCase.setUp(self)
        self.VALUES = [12234, 1234, 234, 1324, 424, 234, 234, 142, 5, 251, 242, 35, 235, 62,
                       1235, 46, 245132, 51, 2, 3, 4]

    @common.requireInstrumentation
    def testJPArray_init(self):
        _jpype.fault("PyJPArray_init")
        with self.assertRaisesRegex(SystemError, "fault"):
            JArray(JInt)(5)
        _jpype.fault("PyJPModule_getContext")
        with self.assertRaisesRegex(SystemError, "fault"):
            JArray(JInt)(5)

    def testJPArray_initExc(self):
        with self.assertRaises(TypeError):
            _jpype._JArray("foo")
        with self.assertRaises(TypeError):
            JArray(JInt)(JArray(JDouble)([1, 2]))
        with self.assertRaises(TypeError):
            JArray(JInt)(JString)
        with self.assertRaises(ValueError):
            JArray(JInt)(-1)
        with self.assertRaises(ValueError):
            JArray(JInt)(10000000000)
        with self.assertRaises(TypeError):
            JArray(JInt)(object())
        self.assertEqual(len(JArray(JInt)(0)), 0)
        self.assertEqual(len(JArray(JInt)(10)), 10)
        self.assertEqual(len(JArray(JInt)([1, 2, 3])), 3)
        self.assertEqual(len(JArray(JInt)(JArray(JInt)([1, 2, 3]))), 3)

        class badlist(list):
            def __len__(self):
                return -1
        with self.assertRaises(ValueError):
            JArray(JInt)(badlist([1, 2, 3]))

    @common.requireInstrumentation
    def testJPArray_repr(self):
        ja = JArray(JInt)(5)
        _jpype.fault("PyJPArray_repr")
        with self.assertRaisesRegex(SystemError, "fault"):
            repr(ja)

    @common.requireInstrumentation
    def testJPArray_len(self):
        ja = JArray(JInt)(5)
        _jpype.fault("PyJPArray_len")
        with self.assertRaisesRegex(SystemError, "fault"):
            len(ja)
        _jpype.fault("PyJPModule_getContext")
        with self.assertRaisesRegex(SystemError, "fault"):
            len(ja)

    @common.requireInstrumentation
    def testJPArray_getArrayItem(self):
        ja = JArray(JInt)(5)
        _jpype.fault("PyJPArray_getArrayItem")
        with self.assertRaisesRegex(SystemError, "fault"):
            print(ja[0])
        _jpype.fault("PyJPModule_getContext")
        with self.assertRaisesRegex(SystemError, "fault"):
            print(ja[0])

    def testJPArray_getArrayItemExc(self):
        ja = JArray(JInt)(5)
        with self.assertRaises(TypeError):
            ja[object()]
        with self.assertRaises(ValueError):
            ja[slice(0, 0, 0)]
        self.assertEqual(len(JArray(JInt)(5)[4:1]), 0)

    @common.requireInstrumentation
    def testJPArray_assignSubscript(self):
        ja = JArray(JInt)(5)
        _jpype.fault("PyJPArray_assignSubscript")
        with self.assertRaisesRegex(SystemError, "fault"):
            ja[0:2] = 1
        _jpype.fault("PyJPArray_assignSubscript")
        with self.assertRaisesRegex(SystemError, "fault"):
            ja[0] = 1
        _jpype.fault("PyJPModule_getContext")
        with self.assertRaisesRegex(SystemError, "fault"):
            ja[0:2] = 1

    def testJPArray_assignSubscriptExc(self):
        ja = JArray(JInt)(5)
        with self.assertRaises(ValueError):
            del ja[0:2]
        with self.assertRaises(ValueError):
            ja[slice(0, 0, 0)] = 1

    @common.requireInstrumentation
    def testJPArray_getBuffer(self):
        _jpype.fault("PyJPArray_getBuffer")
        with self.assertRaisesRegex(SystemError, "fault"):
            ja = JArray(JInt, 2)(5)
            m = memoryview(ja)
            del m  # lgtm [py/unnecessary-delete]

    @common.requireInstrumentation
    def testJPArrayPrimitive_getBuffer(self):
        _jpype.fault("PyJPArrayPrimitive_getBuffer")

        def f():
            ja = JArray(JInt)(5)
            m = memoryview(ja)
            del m  # lgtm [py/unnecessary-delete]
        with self.assertRaisesRegex(SystemError, "fault"):
            f()
        with self.assertRaises(BufferError):
            memoryview(JArray(JInt, 2)([[1, 2], [1], [1, 2, 3]]))
        _jpype.fault("PyJPModule_getContext")
        with self.assertRaisesRegex(SystemError, "fault"):
            f()

    @common.requireInstrumentation
    def testJPArray_null(self):
        _jpype.fault("PyJPArray_init.null")
        null = JArray(JInt)(object())
        with self.assertRaises(ValueError):
            len(null)
        with self.assertRaises(ValueError):
            null[0]
        with self.assertRaises(ValueError):
            null[0] = 1
        with self.assertRaises(ValueError):
            memoryview(null)
        null = JArray(JObject)(object())
        with self.assertRaises(ValueError):
            memoryview(null)
        _jpype.fault("PyJPModule_getContext")
        with self.assertRaisesRegex(SystemError, "fault"):
            memoryview(null)
        with self.assertRaisesRegex(TypeError, 'Not a Java value'):
            _jpype._JObject.__str__(null)
        self.assertEqual(hash(null), hash(None))

    @common.requireInstrumentation
    def testJPArrayNew(self):
        ja = JArray(JInt)
        _jpype.fault("JPArray::JPArray")
        with self.assertRaisesRegex(SystemError, "fault"):
            ja(5)
        j = ja(5)
        _jpype.fault("JPArray::JPArraySlice")
        with self.assertRaisesRegex(SystemError, "fault"):
            j[0:2:1]

    @common.requireInstrumentation
    def testJArrayClassConvertToVector(self):
        Path = JClass("java.nio.file.Paths")
        _jpype.fault("JPArrayClass::convertToJavaVector")
        with self.assertRaisesRegex(SystemError, "fault"):
            Path.get("foo", "bar")

    @common.requireInstrumentation
    def testJArrayGetJavaConversion(self):
        ja = JArray(JInt)
        _jpype.fault("JPArrayClass::findJavaConversion")
        with self.assertRaisesRegex(SystemError, "fault"):
            ja._canConvertToJava(object())

    @common.requireInstrumentation
    def testJArrayConvertToPythonObject(self):
        jl = JClass('java.util.ArrayList')()
        jl.add(JArray(JInt)(3))
        _jpype.fault("JPArrayClass::convertToPythonObject")
        with self.assertRaisesRegex(SystemError, "fault"):
            jl.get(0)

    def testReadArray(self):
        t = JClass("jpype.array.TestArray")()
        self.assertNotIsInstance(t, JPackage)

        self.assertCountEqual(self.VALUES, t.i)

        self.assertEqual(t.i[0], self.VALUES[0])
        self.assertCountEqual(self.VALUES[1:-2], t.i[1:-2])

    def testEmptyObjectArray(self):
        ''' Test for strange crash reported in bug #1089302'''
        Test2 = jpype.JPackage('jpype.array').Test2
        test = Test2()
        test.test(test.getValue())

    def testWriteArray(self):
        t = JClass("jpype.array.TestArray")()
        self.assertNotIsInstance(t, JPackage)

        t.i[0] = 32
        self.assertEqual(t.i[0], 32)

        t.i[1:3] = (33, 34)
        self.assertEqual(t.i[1], 33)
        self.assertEqual(t.i[2], 34)

        self.assertCountEqual(t.i[:5], (32, 33, 34, 1324, 424))

    def testObjectArraySimple(self):
        a = JArray(java.lang.String, 1)(2)
        a[1] = "Foo"
        self.assertEqual("Foo", a[1])

    def testIterateArray(self):
        t = JClass("jpype.array.TestArray")()
        self.assertFalse(isinstance(t, JPackage))

        for i in t.i:
            self.assertNotEqual(i, 0)

    def testGetSubclass(self):
        t = JClass("jpype.array.TestArray")()
        v = t.getSubClassArray()
        self.assertTrue(isinstance(v[0], jpype.java.lang.Integer))

    def testGetArrayAsObject(self):
        t = JClass("jpype.array.TestArray")()
        v = t.getArrayAsObject()

    def testJArrayPythonTypes(self):
        self.assertEqual(jpype.JArray(
            object).class_.getComponentType(), JClass('java.lang.Object'))
        self.assertEqual(jpype.JArray(
            float).class_.getComponentType(), JClass('java.lang.Double').TYPE)
        self.assertEqual(jpype.JArray(
            str).class_.getComponentType(), JClass('java.lang.String'))
        self.assertEqual(jpype.JArray(
            type).class_.getComponentType(), JClass('java.lang.Class'))

    def testObjectArrayInitial(self):
        l1 = java.util.ArrayList()
        l1.add(0)
        l2 = java.util.ArrayList()
        l2.add(42)
        l3 = java.util.ArrayList()
        l3.add(13)
        jarr = jpype.JArray(java.util.ArrayList, 1)([l1, l2, l3])

        self.assertEqual(l1, jarr[0])
        self.assertEqual(l2, jarr[1])
        self.assertEqual(l3, jarr[2])

    @common.requireNumpy
    def testSetFromNPByteArray(self):
        import numpy as np
        n = 100
        a = np.random.randint(-128, 127, size=n).astype(np.byte)
        jarr = jpype.JArray(jpype.JByte)(n)
        jarr[:] = a
        self.assertCountEqual(a, jarr)

    def testArrayCtor1(self):
        jobject = jpype.JClass('java.lang.Object')
        jarray = jpype.JArray(jobject)
        self.assertTrue(issubclass(jarray, jpype.JArray))
        self.assertTrue(isinstance(jarray(10), jpype.JArray))

    def testArrayCtor2(self):
        jobject = jpype.JClass('java.util.List')
        jarray = jpype.JArray(jobject)
        self.assertTrue(issubclass(jarray, jpype.JArray))
        self.assertTrue(isinstance(jarray(10), jpype.JArray))

    def testArrayCtor3(self):
        jarray = jpype.JArray("java.lang.Object")
        self.assertTrue(issubclass(jarray, jpype.JArray))
        self.assertTrue(isinstance(jarray(10), jpype.JArray))

    def testArrayCtor4(self):
        jarray = jpype.JArray(jpype.JObject)
        self.assertTrue(issubclass(jarray, jpype.JArray))
        self.assertTrue(isinstance(jarray(10), jpype.JArray))

    def testArrayCtor5(self):
        jarray0 = jpype.JArray("java.lang.Object")
        jarray = jpype.JArray(jarray0)
        self.assertTrue(issubclass(jarray, jpype.JArray))
        self.assertTrue(isinstance(jarray(10), jpype.JArray))

    def testObjectNullArraySlice(self):
        # Check for bug in 0.7.0
        array = jpype.JArray(jpype.JObject)([None, ])
        self.assertEqual(tuple(array[:]), (None,))

    def testJArrayBadType(self):
        class Bob(object):
            pass
        with self.assertRaises(TypeError):
            jpype.JArray(Bob)

    def testJArrayBadSlice(self):
        ja = JArray(JObject)(5)
        with self.assertRaises(TypeError):
            ja[dict()] = 1

    def testJArrayBadAssignment(self):
        ja = JArray(JObject)(5)
        with self.assertRaises(TypeError):
            ja[1:3] = dict()

    def testJArrayIncorrectSliceLen(self):
        a = JArray(JObject)(10)
        b = JArray(JObject)(10)
        with self.assertRaises(ValueError):
            a[1:3] = b[1:4]

    def testJArrayIndexOutOfBounds(self):
        a = JArray(JObject)(10)
        with self.assertRaises(IndexError):
            a[50] = 1
        with self.assertRaises(IndexError):
            a[-50] = 1

    def testJArrayBadItem(self):
        a = JArray(JObject)(10)

        class Larry(object):
            pass
        with self.assertRaises(TypeError):
            a[1] = Larry()

    def testJArrayCopyRange(self):
        a = JArray(JObject)(['a', 'b', 'c', 'd', 'e', 'f'])
        a[1:4] = ['x', 'y', 'z']
        self.assertEqual(list(a), ['a', 'x', 'y', 'z', 'e', 'f'])

    def checkArrayOf(self, jtype, dtype, mn=None, mx=None):
        if mn and mx:
            a = np.random.randint(mn, mx, size=100, dtype=dtype)
        else:
            a = np.random.random(100).astype(dtype)
        ja = JArray.of(a)
        self.assertIsInstance(ja, JArray(jtype))
        self.assertEqual(len(ja), 100)
        self.assertTrue(np.all(a == ja))

        a = np.reshape(a, (20, 5))
        ja = JArray.of(a)
        self.assertIsInstance(ja, JArray(jtype, 2))
        self.assertEqual(len(ja), 20)
        self.assertEqual(len(ja[0]), 5)
        self.assertTrue(np.all(a == ja))

        a = np.reshape(a, (5, 2, 10))
        ja = JArray.of(a)
        self.assertIsInstance(ja, JArray(jtype, 3))
        self.assertEqual(len(ja), 5)
        self.assertEqual(len(ja[0]), 2)
        self.assertEqual(len(ja[0][0]), 10)
        self.assertTrue(np.all(a == ja))
        self.assertTrue(np.all(a[1, :, :] == JArray.of(a[1, :, :])))
        self.assertTrue(np.all(a[2::2, :, :] == JArray.of(a[2::2, :, :])))
        self.assertTrue(np.all(a[:, :, 4:-2] == JArray.of(a[:, :, 4:-2])))

    def checkArrayOfCast(self, jtype, dtype):
        a = np.random.randint(0, 1, size=100, dtype=np.bool_)
        ja = JArray.of(a, dtype=jtype)
        self.assertIsInstance(ja, JArray(jtype))
        self.assertTrue(np.all(a.astype(dtype) == ja))

        a = np.random.randint(0, 2 * 8 - 1, size=100, dtype=np.uint8)
        ja = JArray.of(a, dtype=jtype)
        self.assertIsInstance(ja, JArray(jtype))
        self.assertTrue(np.all(a.astype(dtype) == ja))

        a = np.random.randint(-2 * 7, 2 * 7 - 1, size=100, dtype=np.int8)
        ja = JArray.of(a, dtype=jtype)
        self.assertIsInstance(ja, JArray(jtype))
        self.assertTrue(np.all(a.astype(dtype) == ja))

        a = np.random.randint(0, 2 * 16 - 1, size=100, dtype=np.uint16)
        ja = JArray.of(a, dtype=jtype)
        self.assertIsInstance(ja, JArray(jtype))
        self.assertTrue(np.all(a.astype(dtype) == ja))

        a = np.random.randint(-2 * 15, 2 * 15 - 1, size=100, dtype=np.int16)
        ja = JArray.of(a, dtype=jtype)
        self.assertIsInstance(ja, JArray(jtype))
        self.assertTrue(np.all(a.astype(dtype) == ja))

        a = np.random.randint(0, 2 * 32 - 1, size=100, dtype=np.uint32)
        ja = JArray.of(a, dtype=jtype)
        self.assertIsInstance(ja, JArray(jtype))
        self.assertTrue(np.all(a.astype(dtype) == ja))

        a = np.random.randint(-2 * 31, 2 * 31 - 1, size=100, dtype=np.int32)
        ja = JArray.of(a, dtype=jtype)
        self.assertIsInstance(ja, JArray(jtype))
        self.assertTrue(np.all(a.astype(dtype) == ja))

        a = np.random.randint(0, 2 * 64 - 1, size=100, dtype=np.uint64)
        ja = JArray.of(a, dtype=jtype)
        self.assertIsInstance(ja, JArray(jtype))
        self.assertTrue(np.all(a.astype(dtype) == ja))

        a = np.random.randint(-2 * 63, 2 * 63 - 1, size=100, dtype=np.int64)
        ja = JArray.of(a, dtype=jtype)
        self.assertIsInstance(ja, JArray(jtype))
        self.assertTrue(np.all(a.astype(dtype) == ja))

    @common.requireNumpy
    def testArrayOfBoolean(self):
        self.checkArrayOf(JBoolean, np.bool_, 0, 1)

    @common.requireNumpy
    def testArrayOfByte(self):
        self.checkArrayOf(JByte, np.int8, -2**7, 2**7 - 1)

    @common.requireNumpy
    def testArrayOfShort(self):
        self.checkArrayOf(JShort, np.int16, -2**15, 2**15 - 1)

    @common.requireNumpy
    def testArrayOfInt(self):
        self.checkArrayOf(JInt, np.int32, -2**31, 2**31 - 1)

    @common.requireNumpy
    def testArrayOfLong(self):
        self.checkArrayOf(JLong, np.int64, -2**63, 2**63 - 1)

    @common.requireNumpy
    def testArrayOfFloat(self):
        self.checkArrayOf(JFloat, np.float32)

    @common.requireNumpy
    def testArrayOfDouble(self):
        self.checkArrayOf(JDouble, np.float64)

    @common.requireNumpy
    def testArrayOfBooleanCast(self):
        self.checkArrayOfCast(JBoolean, np.bool_)

    @common.requireNumpy
    def testArrayOfByteCast(self):
        self.checkArrayOfCast(JByte, np.int8)

    @common.requireNumpy
    def testArrayOfShortCast(self):
        self.checkArrayOfCast(JShort, np.int16)

    @common.requireNumpy
    def testArrayOfIntCast(self):
        self.checkArrayOfCast(JInt, np.int32)

    @common.requireNumpy
    def testArrayOfLongCast(self):
        self.checkArrayOfCast(JLong, np.int64)

    @common.requireNumpy
    def testArrayOfFloatCast(self):
        self.checkArrayOfCast(JFloat, np.float32)

    @common.requireNumpy
    def testArrayOfDoubleCast(self):
        self.checkArrayOfCast(JDouble, np.float64)

    @common.requireNumpy
    def testArrayOfExc(self):
        a = np.random.randint(0, 2 * 8 - 1, size=1, dtype=np.uint8)
        with self.assertRaises(TypeError):
            JArray.of(a)
        a = np.random.randint(0, 2 * 16 - 1, size=1, dtype=np.uint16)
        with self.assertRaises(TypeError):
            JArray.of(a)
        a = np.random.randint(0, 2 * 32 - 1, size=1, dtype=np.uint32)
        with self.assertRaises(TypeError):
            JArray.of(a)
        a = np.random.randint(0, 2 * 64 - 1, size=1, dtype=np.uint64)
        with self.assertRaises(TypeError):
            JArray.of(a)

    @common.requireInstrumentation
    def testArrayOfFaults(self):
        b = bytes([1, 2, 3])
        _jpype.fault("JPJavaFrame::assemble")
        with self.assertRaisesRegex(SystemError, "fault"):
            JArray.of(b, JInt)
        _jpype.fault("JPBooleanType::newMultiArray")
        with self.assertRaisesRegex(SystemError, "fault"):
            JArray.of(b, JBoolean)
        _jpype.fault("JPCharType::newMultiArray")
        with self.assertRaisesRegex(SystemError, "fault"):
            JArray.of(b, JChar)
        _jpype.fault("JPByteType::newMultiArray")
        with self.assertRaisesRegex(SystemError, "fault"):
            JArray.of(b, JByte)
        _jpype.fault("JPShortType::newMultiArray")
        with self.assertRaisesRegex(SystemError, "fault"):
            JArray.of(b, JShort)
        _jpype.fault("JPIntType::newMultiArray")
        with self.assertRaisesRegex(SystemError, "fault"):
            JArray.of(b, JInt)
        _jpype.fault("JPLongType::newMultiArray")
        with self.assertRaisesRegex(SystemError, "fault"):
            JArray.of(b, JLong)
        _jpype.fault("JPFloatType::newMultiArray")
        with self.assertRaisesRegex(SystemError, "fault"):
            JArray.of(b, JFloat)
        _jpype.fault("JPDoubleType::newMultiArray")
        with self.assertRaisesRegex(SystemError, "fault"):
            JArray.of(b, JDouble)

    def testJArrayDimTooBig(self):
        with self.assertRaises(ValueError):
            jpype.JArray(jpype.JInt, 10000)

    def testJArrayDimWrong(self):
        with self.assertRaises(TypeError):
            jpype.JArray(jpype.JInt, 1.5)

    def testJArrayArgs(self):
        with self.assertRaises(TypeError):
            jpype.JArray(jpype.JInt, 1, 'f')

    def testJArrayTypeBad(self):
        class John(object):
            pass
        with self.assertRaises(TypeError):
            jpype.JArray(John)

    @common.requireNumpy
    def testZeroArray(self):
        ls = []
        ja = JArray(JInt)(ls)
        self.assertEqual(len(ja), 0)
        na = np.array(ja)
        self.assertEqual(len(ja), 0)
        ja = JArray(JString)(ls)
        with self.assertRaisesRegex(BufferError, "not primitive array"):
            memoryview(ja)
        self.assertEqual(len(ja), 0)
        na = np.array(ja)
        self.assertEqual(len(ja), 0)
        ja = JArray(JInt, 2)([[], [1]])
        with self.assertRaisesRegex(BufferError, "not rectangular"):
            memoryview(ja)
        ja = JArray(JInt, 2)([[1], []])
        with self.assertRaisesRegex(BufferError, "not rectangular"):
            memoryview(ja)

    def testLengthProperty(self):
        ja = JArray(JInt)([1, 2, 3])
        self.assertEqual(ja.length, len(ja))

    def testShortcut(self):
        # Test for odd bug introduced in 1.0.0
        # This is unlikely to be reintroduced, but we can check anyway.
        # The class of when created using the shortcut should always match the type created using the old method

        # Check primitives
        self.assertEqual(JBoolean[5].getClass(), JArray(JBoolean)(5).getClass())
        self.assertEqual(JChar[5].getClass(), JArray(JChar)(5).getClass())
        self.assertEqual(JByte[5].getClass(), JArray(JByte)(5).getClass())
        self.assertEqual(JShort[5].getClass(), JArray(JShort)(5).getClass())
        self.assertEqual(JInt[5].getClass(), JArray(JInt)(5).getClass())
        self.assertEqual(JFloat[5].getClass(), JArray(JFloat)(5).getClass())
        self.assertEqual(JDouble[5].getClass(), JArray(JDouble)(5).getClass())

        # Check Objects
        self.assertEqual(JString[5].getClass(), JArray(JString)(5).getClass())
        self.assertEqual(JObject[5].getClass(), JArray(JObject)(5).getClass())

        # Test multidimensional
        self.assertEqual(JDouble[5, 5].getClass(), JArray(JDouble, 2)(5).getClass())
        self.assertEqual(JObject[5, 5].getClass(), JArray(JObject, 2)(5).getClass())

    def testJArrayIndex(self):
        with self.assertRaises(TypeError):
            jpype.JArray[10]