Debian Codegraph @

test/jpypetest/test_jbyte.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 jpype
20 from jpype.types import *
21 from jpype import java
22 import sys
23 import logging
24 import time
25 import common
26 try:
27 import numpy as np
28 except ImportError:
29 pass
30
31
32 class JByteTestCase(common.JPypeTestCase):
33 def setUp(self):
34 common.JPypeTestCase.setUp(self)
35 self.fixture = jpype.JClass("jpype.common.Fixture")()
36
37 @common.requireInstrumentation
38 def testConversionFault(self):
39 _jpype.fault("JPByteType::findJavaConversion")
40 with self.assertRaisesRegex(SystemError, "fault"):
41 JByte._canConvertToJava(object())
42
43 @common.requireInstrumentation
44 def testArrayFaults(self):
45 ja = JArray(JByte)(5)
46 _jpype.fault("JPByteType::setArrayRange")
47 with self.assertRaisesRegex(SystemError, "fault"):
48 ja[1:3] = [0, 0]
49 _jpype.fault("JPJavaFrame::NewByteArray")
50 with self.assertRaisesRegex(SystemError, "fault"):
51 JArray(JByte)(1)
52 _jpype.fault("JPJavaFrame::SetByteArrayRegion")
53 with self.assertRaisesRegex(SystemError, "fault"):
54 ja[0] = 0
55 _jpype.fault("JPJavaFrame::GetByteArrayRegion")
56 with self.assertRaisesRegex(SystemError, "fault"):
57 print(ja[0])
58 _jpype.fault("JPJavaFrame::GetByteArrayElements")
59 with self.assertRaises(BufferError):
60 memoryview(ja[0:3])
61 _jpype.fault("JPJavaFrame::ReleaseByteArrayElements")
62 with self.assertRaisesRegex(SystemError, "fault"):
63 ja[0:3] = bytes([1, 2, 3])
64 _jpype.fault("JPJavaFrame::ReleaseByteArrayElements")
65 with self.assertRaisesRegex(SystemError, "fault"):
66 jpype.JObject(ja[::2], jpype.JObject)
67 _jpype.fault("JPJavaFrame::ReleaseByteArrayElements")
68
69 def f():
70 # Special case no fault is allowed
71 memoryview(ja[0:3])
72 f()
73
74 def testByteFromInt(self):
75 self.assertEqual(self.fixture.callByte(int(123)), 123)
76
77 @common.requireNumpy
78 def testByteFromNPInt(self):
79 import numpy as np
80 self.assertEqual(self.fixture.callByte(np.int_(123)), 123)
81
82 @common.requireNumpy
83 def testByteFromNPInt8(self):
84 import numpy as np
85 self.assertEqual(self.fixture.callByte(np.int8(123)), 123)
86 self.assertEqual(self.fixture.callByte(np.uint8(123)), 123)
87
88 @common.requireNumpy
89 def testByteFromNPInt16(self):
90 import numpy as np
91 self.assertEqual(self.fixture.callByte(np.int16(123)), 123)
92 self.assertEqual(self.fixture.callByte(np.uint16(123)), 123)
93
94 @common.requireNumpy
95 def testByteFromNPInt32(self):
96 import numpy as np
97 self.assertEqual(self.fixture.callByte(np.int32(123)), 123)
98 self.assertEqual(self.fixture.callByte(np.uint32(123)), 123)
99
100 @common.requireNumpy
101 def testByteFromNPInt64(self):
102 import numpy as np
103 self.assertEqual(self.fixture.callByte(np.int64(123)), 123)
104 self.assertEqual(self.fixture.callByte(np.uint64(123)), 123)
105
106 def testByteFromFloat(self):
107 with self.assertRaises(TypeError):
108 self.fixture.callByte(float(2))
109
110 @common.requireNumpy
111 def testByteFromNPFloat16(self):
112 import numpy as np
113 with self.assertRaises(TypeError):
114 self.fixture.callByte(np.float16(2))
115
116 @common.requireNumpy
117 def testByteFromNPFloat32(self):
118 import numpy as np
119 with self.assertRaises(TypeError):
120 self.fixture.callByte(np.float32(2))
121
122 @common.requireNumpy
123 def testByteFromNPFloat64(self):
124 import numpy as np
125 with self.assertRaises(TypeError):
126 self.fixture.callByte(np.float64(2))
127
128 def testByteRange(self):
129 with self.assertRaises(OverflowError):
130 self.fixture.callByte(int(1e10))
131 with self.assertRaises(OverflowError):
132 self.fixture.callByte(int(-1e10))
133
134 def testExplicitRange(self):
135 # These will not overflow as they are explicit casts
136 self.assertEqual(JByte(2**8), 0)
137 self.assertEqual(JByte(-2**8), 0)
138
139 def testByteFromNone(self):
140 with self.assertRaises(TypeError):
141 self.fixture.callByte(None)
142
143 def testByteArrayAsString(self):
144 t = JClass("jpype.array.TestArray")()
145 v = t.getByteArray()
146 self.assertEqual(str(v), 'avcd')
147
148 def testByteArrayIntoVector(self):
149 ba = jpype.JArray(jpype.JByte)(b'123')
150 v = jpype.java.util.Vector(1)
151 v.add(ba)
152 self.assertEqual(len(v), 1)
153 self.assertNotEqual(v[0], None)
154
155 def testByteArraySimple(self):
156 a = JArray(JByte)(2)
157 a[1] = 2
158 self.assertEqual(a[1], 2)
159
160 def testJArrayConversionByte(self):
161 expected = (0, 1, 2, 3)
162 ByteBuffer = jpype.java.nio.ByteBuffer
163 bb = ByteBuffer.allocate(4)
164 buf = bb.array()
165 for i in range(len(expected)):
166 buf[i] = expected[i]
167 for i in range(len(expected)):
168 self.assertEqual(expected[i], buf[i])
169
170 def testFromObject(self):
171 ja = JArray(JByte)(5)
172 with self.assertRaises(TypeError):
173 ja[1] = object()
174 jf = JClass("jpype.common.Fixture")
175 with self.assertRaises(TypeError):
176 jf.static_byte_field = object()
177 with self.assertRaises(TypeError):
178 jf().byte_field = object()
179
180 def testArrayHash(self):
181 ja = JArray(JByte)([1, 2, 3])
182 self.assertIsInstance(hash(ja), int)
183
184 @common.requireNumpy
185 def testArrayBufferDims(self):
186 ja = JArray(JByte)(5)
187 a = np.zeros((5, 2))
188 with self.assertRaisesRegex(TypeError, "incorrect"):
189 ja[:] = a
190
191 def testArrayBadItem(self):
192 class q(object):
193 def __int__(self):
194 raise SystemError("nope")
195
196 def __index__(self):
197 raise SystemError("nope")
198 ja = JArray(JByte)(5)
199 a = [1, -1, q(), 3, 4]
200 with self.assertRaisesRegex(SystemError, "nope"):
201 ja[:] = a
202
203 def testArrayBadDims(self):
204 class q(bytes):
205 # Lie about our length
206 def __len__(self):
207 return 5
208 a = q([1, 2, 3])
209 ja = JArray(JByte)(5)
210 with self.assertRaisesRegex(ValueError, "Slice"):
211 ja[:] = [1, 2, 3]
212 with self.assertRaisesRegex(ValueError, "mismatch"):
213 ja[:] = a
214
215 def testArraySetRange(self):
216 ja = JArray(JByte)(3)
217 ja[0:1] = [123]
218 self.assertEqual(ja[0], 123)
219 ja[0:1] = [-1]
220 self.assertEqual(ja[0], -1)
221 with self.assertRaises(TypeError):
222 ja[0:1] = [1.000]
223 with self.assertRaises(TypeError):
224 ja[0:1] = [java.lang.Double(321)]
225 with self.assertRaises(TypeError):
226 ja[0:1] = [object()]