MCPcopy Create free account
hub / github.com/java-native-access/jna / testBoundsCheckArray

Method testBoundsCheckArray

test/com/sun/jna/MemoryTest.java:290–338  ·  view source on GitHub ↗
(Class componentClass, int elementSize)

Source from the content-addressed store, hash-verified

288 }
289
290 private void testBoundsCheckArray(Class componentClass, int elementSize) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
291 Object javaArray = Array.newInstance(componentClass, 2);
292 Method readMethod = Memory.class.getMethod("read", new Class[]{long.class, javaArray.getClass(), int.class, int.class});
293 Method writeMethod = Memory.class.getMethod("write", new Class[]{long.class, javaArray.getClass(), int.class, int.class});
294
295 Memory mem = new Memory(elementSize * 10);
296
297 readMethod.invoke(mem, elementSize * 0, javaArray, 0, 2);
298 readMethod.invoke(mem, elementSize * 8, javaArray, 0, 2);
299 try {
300 readMethod.invoke(mem, elementSize * -1, javaArray, 0, 2);
301 fail("Negative offset was read");
302 } catch (InvocationTargetException ex) {
303 checkCauseInstance(ex, IndexOutOfBoundsException.class);
304 }
305 try {
306 readMethod.invoke(mem, elementSize * 9, javaArray, 0, 2);
307 fail("Half of the array contents layed outside the allocated area");
308 } catch (InvocationTargetException ex) {
309 checkCauseInstance(ex, IndexOutOfBoundsException.class);
310 }
311 try {
312 readMethod.invoke(mem, elementSize * 20, javaArray, 0, 2);
313 fail("Array contents layed completely outside the allocated area");
314 } catch (InvocationTargetException ex) {
315 checkCauseInstance(ex, IndexOutOfBoundsException.class);
316 }
317
318 writeMethod.invoke(mem, 0, javaArray, 0, 2);
319 writeMethod.invoke(mem, elementSize * 8, javaArray, 0, 2);
320 try {
321 writeMethod.invoke(mem, elementSize * -1, javaArray, 0, 2);
322 fail("Negative offset was read");
323 } catch (InvocationTargetException ex) {
324 checkCauseInstance(ex, IndexOutOfBoundsException.class);
325 }
326 try {
327 writeMethod.invoke(mem, elementSize * 9, javaArray, 0, 2);
328 fail("Half of the array contents layed outside the allocated area");
329 } catch (InvocationTargetException ex) {
330 checkCauseInstance(ex, IndexOutOfBoundsException.class);
331 }
332 try {
333 writeMethod.invoke(mem, elementSize * 20, javaArray, 0, 2);
334 fail("Array contents layed completely outside the allocated area");
335 } catch (InvocationTargetException ex) {
336 checkCauseInstance(ex, IndexOutOfBoundsException.class);
337 }
338 }
339
340 private void checkCauseInstance(Exception ex, Class<? extends Exception> expectedClazz) {
341 Assert.assertThat(ex.getCause(), instanceOf(expectedClazz));

Callers 1

testBoundsCheckingMethod · 0.95

Calls 4

checkCauseInstanceMethod · 0.95
invokeMethod · 0.65
newInstanceMethod · 0.45
getMethodMethod · 0.45

Tested by

no test coverage detected