| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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**31, 2**31 - 1) for i in range(10)] |
| 32 |
|
| 33 |
|
| 34 |
class JIntTestCase(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 = JInt(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 = JInt(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 = JInt(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 = JInt(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 = JInt(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 = JInt(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("JPIntType::findJavaConversion") |
| 109 |
with self.assertRaises(SystemError): |
| 110 |
JInt(1.0) |
| 111 |
|
| 112 |
@common.requireInstrumentation |
| 113 |
def testConversionFault(self): |
| 114 |
_jpype.fault("JPIntType::findJavaConversion") |
| 115 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 116 |
JInt._canConvertToJava(object()) |
| 117 |
|
| 118 |
@common.requireInstrumentation |
| 119 |
def testArrayFault(self): |
| 120 |
ja = JArray(JInt)(5) |
| 121 |
_jpype.fault("JPJavaFrame::NewIntArray") |
| 122 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 123 |
JArray(JInt)(1) |
| 124 |
_jpype.fault("JPJavaFrame::SetIntArrayRegion") |
| 125 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 126 |
ja[0] = 0 |
| 127 |
_jpype.fault("JPJavaFrame::GetIntArrayRegion") |
| 128 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 129 |
print(ja[0]) |
| 130 |
_jpype.fault("JPJavaFrame::GetIntArrayElements") |
| 131 |
|
| 132 |
with self.assertRaises(BufferError): |
| 133 |
memoryview(ja[0:3]) |
| 134 |
_jpype.fault("JPJavaFrame::ReleaseIntArrayElements") |
| 135 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 136 |
ja[0:3] = bytes([1, 2, 3]) |
| 137 |
_jpype.fault("JPJavaFrame::ReleaseIntArrayElements") |
| 138 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 139 |
jpype.JObject(ja[::2], jpype.JObject) |
| 140 |
_jpype.fault("JPJavaFrame::ReleaseIntArrayElements") |
| 141 |
|
| 142 |
def f(): |
| 143 |
|
| 144 |
memoryview(ja[0:3]) |
| 145 |
f() |
| 146 |
_jpype.fault("JPIntType::setArrayRange") |
| 147 |
with self.assertRaisesRegex(SystemError, "fault"): |
| 148 |
ja[1:3] = [0, 0] |
| 149 |
|
| 150 |
def testFromJIntWiden(self): |
| 151 |
self.assertEqual(JInt(JByte(123)), 123) |
| 152 |
self.assertEqual(JInt(JShort(12345)), 12345) |
| 153 |
self.assertEqual(JInt(JInt(12345678)), 12345678) |
| 154 |
self.assertEqual(JInt(JLong(12345678)), 12345678) |
| 155 |
|
| 156 |
def testFromJIntWiden_2(self): |
| 157 |
self.assertEqual(JInt(JDouble(12345678)), 12345678) |
| 158 |
|
| 159 |
def testFromNone(self): |
| 160 |
with self.assertRaises(TypeError): |
| 161 |
JInt(None) |
| 162 |
self.assertEqual(JInt._canConvertToJava(None), "none") |
| 163 |
|
| 164 |
def testUnBox(self): |
| 165 |
self.assertEqual(JInt(java.lang.Double(1.2345)), 1) |
| 166 |
|
| 167 |
def testFromFloat(self): |
| 168 |
self.assertEqual(JInt._canConvertToJava(1.2345), "explicit") |
| 169 |
|
| 170 |
@jpype.JImplements("java.util.function.IntSupplier") |
| 171 |
class q(object): |
| 172 |
@jpype.JOverride |
| 173 |
def getAsInt(self): |
| 174 |
return 4.5 |
| 175 |
self.assertEqual(JObject(q()).getAsInt(), 4) |
| 176 |
|
| 177 |
def testFromLong(self): |
| 178 |
self.assertEqual(JInt(12345), 12345) |
| 179 |
self.assertEqual(JInt._canConvertToJava(12345), "implicit") |
| 180 |
|
| 181 |
def testFromObject(self): |
| 182 |
with self.assertRaises(TypeError): |
| 183 |
JInt(object()) |
| 184 |
with self.assertRaises(TypeError): |
| 185 |
JInt(JObject()) |
| 186 |
with self.assertRaises(TypeError): |
| 187 |
JInt(JString("A")) |
| 188 |
self.assertEqual(JInt._canConvertToJava(object()), "none") |
| 189 |
ja = JArray(JInt)(5) |
| 190 |
with self.assertRaises(TypeError): |
| 191 |
ja[1] = object() |
| 192 |
jf = JClass("jpype.common.Fixture") |
| 193 |
with self.assertRaises(TypeError): |
| 194 |
jf.static_int_field = object() |
| 195 |
with self.assertRaises(TypeError): |
| 196 |
jf().int_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_int_field = None |
| 203 |
with self.assertRaises(TypeError): |
| 204 |
self.fixture.int_field = None |
| 205 |
|
| 206 |
def testThrow(self): |
| 207 |
|
| 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 |
|
| 217 |
self.fixture.int_field = q |
| 218 |
self.assertEqual(self.fixture.int_field, q) |
| 219 |
self.assertEqual(self.fixture.getInt(), q) |
| 220 |
|
| 221 |
self.cls.static_int_field = q |
| 222 |
self.assertEqual(self.fixture.static_int_field, q) |
| 223 |
self.assertEqual(self.fixture.getStaticInt(), q) |
| 224 |
self.assertEqual(self.cls.getStaticInt(), q) |
| 225 |
|
| 226 |
self.assertEqual(self.fixture.callInt(q), q) |
| 227 |
self.assertEqual(self.cls.callStaticInt(q), q) |
| 228 |
|
| 229 |
def checkTypeFail(self, q, exc=TypeError): |
| 230 |
with self.assertRaises(exc): |
| 231 |
self.fixture.int_field = q |
| 232 |
with self.assertRaises(exc): |
| 233 |
self.fixture.callInt(q) |
| 234 |
with self.assertRaises(exc): |
| 235 |
self.fixture.callStaticInt(q) |
| 236 |
|
| 237 |
def testCastFloat(self): |
| 238 |
self.fixture.int_field = JInt(6.0) |
| 239 |
self.assertEqual(self.fixture.int_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**31 - 1) |
| 249 |
self.checkType(-2**31) |
| 250 |
self.checkTypeFail(2**31, exc=OverflowError) |
| 251 |
self.checkTypeFail(-2**31 - 1, exc=OverflowError) |
| 252 |
|
| 253 |
def testExplicitRange(self): |
| 254 |
|
| 255 |
self.assertEqual(JInt(2**32), 0) |
| 256 |
self.assertEqual(JInt(-2**32), 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 testCheckJInt(self): |
| 278 |
self.checkType(JInt(-2**31 + 1)) |
| 279 |
self.checkType(JInt(2**31 - 1)) |
| 280 |
|
| 281 |
def testCheckJLong(self): |
| 282 |
self.checkTypeFail(JLong(-2**63 + 1)) |
| 283 |
self.checkTypeFail(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.checkTypeFail(np.uint32(2**32 - 1), exc=OverflowError) |
| 307 |
self.checkType(np.int32(-2**31)) |
| 308 |
self.checkType(np.int32(2**31 - 1)) |
| 309 |
|
| 310 |
@common.requireNumpy |
| 311 |
def testCheckNumpyInt64(self): |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
self.checkTypeFail(np.int64(-2**63), OverflowError) |
| 318 |
self.checkTypeFail(np.int64(2**63 - 1), OverflowError) |
| 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 |
|
| 330 |
ja = JArray(JInt)(a) |
| 331 |
self.assertElementsEqual(ja, expected) |
| 332 |
ja = JArray(JInt)(len(a)) |
| 333 |
ja[:] = a |
| 334 |
self.assertElementsEqual(ja, expected) |
| 335 |
return ja |
| 336 |
|
| 337 |
def checkArrayTypeFail(self, a): |
| 338 |
|
| 339 |
ja = JArray(JInt)(a) |
| 340 |
ja = JArray(JInt)(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 |
|
| 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.int32)) |
| 383 |
|
| 384 |
@common.requireNumpy |
| 385 |
def testArrayInitFromNPFloat32(self): |
| 386 |
a = np.random.random(100).astype(np.float32) |
| 387 |
self.checkArrayType(a, a.astype(np.int32)) |
| 388 |
|
| 389 |
@common.requireNumpy |
| 390 |
def testArrayInitFromNPFloat64(self): |
| 391 |
a = np.random.random(100).astype(np.float64) |
| 392 |
self.checkArrayType(a, a.astype(np.int32)) |
| 393 |
|
| 394 |
def testArraySetRange(self): |
| 395 |
ja = JArray(JInt)(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(JInt)(VALUES) |
| 409 |
with self.assertRaises(TypeError): |
| 410 |
jarr[1] = 'a' |
| 411 |
|
| 412 |
def testArraySliceLength(self): |
| 413 |
jarr = JArray(JInt)(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(JInt)(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(JInt, 1)(10) |
| 427 |
with self.assertRaises(TypeError): |
| 428 |
jarr[1:2] = [dict()] |
| 429 |
|
| 430 |
jarr[1:2] = [-1] |
| 431 |
|
| 432 |
def testArrayClone(self): |
| 433 |
array = JArray(JInt, 2)([[1, 2], [3, 4]]) |
| 434 |
carray = array.clone() |
| 435 |
|
| 436 |
self.assertFalse(array.equals(carray)) |
| 437 |
|
| 438 |
self.assertTrue(array[0].equals(carray[0])) |
| 439 |
|
| 440 |
def testArrayGetSlice(self): |
| 441 |
contents = VALUES |
| 442 |
array = JArray(JInt)(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(JInt)(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(JInt)(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(JInt)(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(JInt)(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(JInt)(contents) |
| 492 |
array2 = JArray(JInt)(contents) |
| 493 |
self.assertEqual(array, array) |
| 494 |
self.assertNotEqual(array, array2) |
| 495 |
|
| 496 |
def testArrayIter(self): |
| 497 |
contents = VALUES |
| 498 |
array = JArray(JInt)(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(JInt)(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(JInt)(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(JInt) |
| 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(JInt)(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(JInt)([1, 2, 3]) |
| 545 |
self.assertIsInstance(hash(ja), int) |
| 546 |
|
| 547 |
@common.requireNumpy |
| 548 |
def testArrayBufferDims(self): |
| 549 |
ja = JArray(JInt)(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(JInt)(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 |
|
| 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 |