Debian Codegraph @

src/test/python/jpy_array_test.py

raw symbols SCIP
1 import unittest
2 import sys
3
4 import jpyutil
5
6
7 jpyutil.init_jvm(jvm_maxmem='8g', jvm_classpath=['target/test-classes'])
8 import jpy
9
10
11 class TestJavaArrays(unittest.TestCase):
12 def do_test_basic_array_protocol_with_length(self, type, initial, expected):
13 a = jpy.array(type, 3)
14 self.assertEqual(len(a), 3)
15 self.assertEqual(a[0], initial[0])
16 self.assertEqual(a[1], initial[1])
17 self.assertEqual(a[2], initial[2])
18 a[0] = expected[0]
19 a[1] = expected[1]
20 a[2] = expected[2]
21 return a
22
23
24 def do_test_array_with_initializer(self, type, expected):
25 a = jpy.array(type, expected)
26 self.assertEqual(len(a), 3)
27 self.assertEqual(a[0], expected[0])
28 self.assertEqual(a[1], expected[1])
29 self.assertEqual(a[2], expected[2])
30
31
32 def do_test_array_protocol(self, type_name, initial, expected):
33 self.do_test_array_protocol2(type_name, initial, expected)
34 self.do_test_array_protocol2(jpy.get_type(type_name), initial, expected)
35 self.do_test_array_with_initializer(type_name, expected)
36 # self.do_test_array_with_initializer(jpy.get_type(type_name), expected)
37
38
39 def do_test_array_protocol2(self, type, initial, expected):
40 a = self.do_test_basic_array_protocol_with_length(type, initial, expected)
41 self.assertEqual(a[0], expected[0])
42 self.assertEqual(a[1], expected[1])
43 self.assertEqual(a[2], expected[2])
44
45
46 def do_test_array_protocol_float(self, type_name, initial, expected, places):
47 self.do_test_array_protocol_float2(type_name, initial, expected, places)
48 self.do_test_array_protocol_float2(jpy.get_type(type_name), initial, expected, places)
49
50
51 def do_test_array_protocol_float2(self, type, initial, expected, places):
52 a = self.do_test_basic_array_protocol_with_length(type, initial, expected)
53 self.assertAlmostEqual(a[0], expected[0], places=places)
54 self.assertAlmostEqual(a[1], expected[1], places=places)
55 self.assertAlmostEqual(a[2], expected[2], places=places)
56
57
58 def test_array_boolean(self):
59 self.do_test_array_protocol('boolean', [False, False, False], [True, False, True])
60
61 # This seems a bit weird but numpy.ndarray allows this too, also but anyone familiar with strong typed languages
62 # shouldn't expect that values of types other than boolean can be assigned to a boolean array element.
63 jba = jpy.array("boolean", 3)
64 jba[0] = []
65 jba[1] = [1]
66 jba[2] = False
67 self.assertEqual(jba[0], False)
68 self.assertEqual(jba[1], True)
69 self.assertEqual(jba[2], False)
70
71 def test_array_char(self):
72 self.do_test_array_protocol('char', [0, 0, 0], [0, 100, 32767])
73
74
75 def test_array_byte(self):
76 self.do_test_array_protocol('byte', [0, 0, 0], [-128, 100, 127])
77
78
79 def test_array_short(self):
80 self.do_test_array_protocol('short', [0, 0, 0], [-32768, 100, 32767])
81
82
83 def test_array_int(self):
84 self.do_test_array_protocol('int', [0, 0, 0], [-2147483648, 100, 2147483647])
85
86
87 def test_array_long(self):
88 self.do_test_array_protocol('long', [0, 0, 0], [-9223372036854775808, 100, 9223372036854775807])
89
90
91 def test_array_float(self):
92 self.do_test_array_protocol_float('float', [0, 0, 0], [-1.001, 0.001, 1.001], places=5)
93
94
95 def test_array_double(self):
96 self.do_test_array_protocol_float('double', [0, 0, 0], [-1.001, 0.001, 1.001], places=10)
97
98
99 def test_array_object(self):
100 File = jpy.get_type('java.io.File')
101 String = jpy.get_type('java.lang.String')
102 Integer = jpy.get_type('java.lang.Integer')
103 self.do_test_array_protocol('java.lang.Integer', [None, None, None], [1, None, 3])
104 self.do_test_array_protocol('java.lang.String', [None, None, None], ['A', 'B', 'C'])
105 self.do_test_array_protocol('java.io.File', [None, None, None], [File('A'), File('B'), File('C')])
106 self.do_test_array_protocol('java.lang.Object', [None, None, None], [None, None, None])
107 self.do_test_array_protocol('java.lang.Object', [None, None, None], [File('A'), 'B', 3])
108
109
110 # see https://github.com/bcdev/jpy/issues/52
111 def test_array_item_del(self):
112 Integer = jpy.get_type('java.lang.Integer')
113 a = jpy.array(Integer, 3)
114 try:
115 del a[1]
116 except RuntimeError as err:
117 self.assertEqual(err.args[0], 'cannot delete items of Java arrays')
118
119
120 def do_test_basic_buffer_protocol(self, type, itemsize, values):
121
122 a = jpy.array(type, 4)
123 self.assertEqual(len(a), 4)
124
125 a[0] = values[0]
126 a[1] = values[1]
127 a[2] = values[2]
128 a[3] = values[3]
129
130 m = memoryview(a)
131 self.assertEqual(len(m), 4)
132 self.assertEqual(m.ndim, 1)
133 self.assertEqual(m.itemsize, itemsize)
134 self.assertEqual(m.shape, (4,))
135 self.assertEqual(m.strides, (itemsize,))
136 self.assertEqual(m.readonly, True)
137 if sys.version_info >= (3, 0, 0):
138 # Python 2.7: AttributeError: 'memoryview' object has no attribute 'nbytes'
139 self.assertEqual(m.nbytes, 4 * itemsize)
140 # Python 2.7: AttributeError: 'memoryview' object has no attribute 'contiguous'
141 self.assertEqual(m.contiguous, True)
142 # Python 2.7: AttributeError: 'memoryview' object has no attribute 'c_contiguous'
143 self.assertEqual(m.c_contiguous, True)
144 return m
145
146
147 def do_test_buffer_protocol(self, type_name, itemsize, values):
148 self.do_test_buffer_protocol2(type_name, itemsize, values)
149 self.do_test_buffer_protocol2(jpy.get_type(type_name), itemsize, values)
150
151
152 def do_test_buffer_protocol2(self, type, itemsize, values):
153 m = self.do_test_basic_buffer_protocol(type, itemsize, values)
154 # With Python 2.7, we cannot use the returned memoryview object for further tests
155 if sys.version_info >= (3, 0, 0):
156 # Python 2.7: NotImplementedError: tolist() only supports byte views
157 self.assertEqual(m.tolist(), values)
158 # Python 2.7: AttributeError: 'memoryview' object has no attribute 'release'
159 m.release()
160
161
162 def do_test_buffer_protocol_float(self, type_name, itemsize, values, places):
163 self.do_test_buffer_protocol_float2(type_name, itemsize, values, places)
164 self.do_test_buffer_protocol_float2(jpy.get_type(type_name), itemsize, values, places)
165 pass
166
167
168 def do_test_buffer_protocol_float2(self, type, itemsize, values, places):
169 m = self.do_test_basic_buffer_protocol(type, itemsize, values)
170 # With Python 2.7, we cannot use the returned memoryview object for further tests
171 if sys.version_info >= (3, 0, 0):
172 # Python 2.7: TypeError: unsupported operand type(s) for -: 'float' and 'str'
173 self.assertAlmostEqual(m[0], values[0], places=places)
174 self.assertAlmostEqual(m[1], values[1], places=places)
175 self.assertAlmostEqual(m[2], values[2], places=places)
176 self.assertAlmostEqual(m[3], values[3], places=places)
177 # Python 2.7: AttributeError: 'memoryview' object has no attribute 'release'
178 m.release()
179
180
181 def test_buffer_boolean(self):
182 self.do_test_buffer_protocol('boolean', 1, [True, False, True, True])
183
184
185 def test_buffer_char(self):
186 self.do_test_buffer_protocol('char', 2, [65, 0, 67, 32])
187
188
189 def test_buffer_byte(self):
190 self.do_test_buffer_protocol('byte', 1, [65, 0, -110, -1])
191
192
193 def test_buffer_short(self):
194 self.do_test_buffer_protocol('short', 2, [651, 0, -1102, -1])
195
196
197 def test_buffer_int(self):
198 self.do_test_buffer_protocol('int', 4, [65123, 0, -110123, -1])
199
200
201 def test_buffer_long(self):
202 self.do_test_buffer_protocol('long', 8, [65123456789, 0, -110123456789, -1])
203
204
205 def test_buffer_float(self):
206 self.do_test_buffer_protocol_float('float', 4, [0.12345, 0.0, -100.123, 54.3], 5)
207
208
209 def test_buffer_double(self):
210 self.do_test_buffer_protocol_float('double', 8, [0.12345678, 0.0, -100.123456, 54.3], 8)
211
212 def test_large_array_by_size_alloc(self):
213 # 100 * 1MB
214 for _ in range(100):
215 java_array = jpy.array('byte', 1000000) # 1MB
216
217 def test_large_array_by_sequence_alloc(self):
218 sequence = list(range(250000)) # 1MB
219 # 100 * 1MB
220 for _ in range(100):
221 java_array = jpy.array('int', sequence)
222
223 def test_java_constructed_array_alloc(self):
224 fixture = jpy.get_type('org.jpy.fixtures.JavaArrayTestFixture')
225 # 100 * 1MB
226 for _ in range(100):
227 java_array = fixture.createByteArray(1000000) # 1MB
228
229 def test_leak(self):
230 '''
231 This isn't a very good "unit"-test - the failure of this test depends
232 on the amount of RAM and specifics of the OS. On my machine I've been
233 able to demonstrate failure with the following constants.
234 '''
235 # skip the test unless you need to stress test array release logic
236 if True:
237 return
238 j_int_array = jpy.array('int', range(100000))
239 # ensure that the bufferExportCount doesn't go to 0
240 keep_around = memoryview(j_int_array)
241 for i in range(1000000):
242 memory_view = memoryview(j_int_array)
243
244 def test_size_greater_than_maxint(self):
245 jarr = jpy.array("int", 2**30)
246 mv = memoryview(jarr)
247 self.assertEqual(mv.nbytes, 2**32)
248
249
250 if __name__ == '__main__':
251 print('\nRunning ' + __file__)
252 unittest.main()