/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * Run positive tests * * @throws RuntimeException if an error is found
*/ privatevoid testPositive() {
testSmallCopy(true);
}
/** * Run negative tests, testing corner cases and the various exceptions * * @throws RuntimeException if an error is found
*/ privatevoid testNegative() { long bufRaw = 0;
// Check various illegal element sizes for (int elemSize = 2; elemSize <= 8; elemSize <<= 1) { long[] illegalSizes = { -1, 1, elemSize - 1, elemSize + 1, elemSize * 2 - 1 }; for (long size : illegalSizes) { try { // Check that illegal elemSize throws an IAE
UNSAFE.copySwapMemory(null, buf, null, buf, size, elemSize); thrownew RuntimeException("copySwapMemory failed to throw IAE for size=" + size + " elemSize=" + elemSize);
} catch (IllegalArgumentException e) { // good
}
}
}
try { // Check that negative srcOffset throws an IAE
UNSAFE.copySwapMemory(arr, -1, arr, UNSAFE.arrayBaseOffset(arr.getClass()), 16, 2); thrownew RuntimeException("copySwapMemory failed to throw IAE for srcOffset=-1");
} catch (IllegalArgumentException e) { // good
}
try { // Check that negative dstOffset throws an IAE
UNSAFE.copySwapMemory(arr, UNSAFE.arrayBaseOffset(arr.getClass()), arr, -1, 16, 2); thrownew RuntimeException("copySwapMemory failed to throw IAE for destOffset=-1");
} catch (IllegalArgumentException e) { // good
}
long illegalElemSizes[] = { 0, 1, 3, 5, 6, 7, 9, 10, -1 }; for (long elemSize : illegalElemSizes) { try { // Check that elemSize 1 throws an IAE
UNSAFE.copySwapMemory(null, buf, null, buf, 16, elemSize); thrownew RuntimeException("copySwapMemory failed to throw NPE");
} catch (IllegalArgumentException e) { // good
}
}
try { // Check that a reference array destination throws IAE
UNSAFE.copySwapMemory(null, buf, new Object[16], UNSAFE.arrayBaseOffset(Object[].class), 16, 8); thrownew RuntimeException("copySwapMemory failed to throw NPE");
} catch (IllegalArgumentException e) { // good
}
// Check that invalid source & dest pointers throw IAEs (only relevant on 32-bit platforms) if (UNSAFE.addressSize() == 4) { long invalidPtr = (long)1 << 35; // Pick a random bit in upper 32 bits
try { // Check that an invalid (not 32-bit clean) source pointer throws IAE
UNSAFE.copySwapMemory(null, invalidPtr, null, buf, 16, 2); thrownew RuntimeException("copySwapMemory failed to throw IAE for srcOffset 0x" + Long.toHexString(invalidPtr));
} catch (IllegalArgumentException e) { // good
}
try { // Check that an invalid (not 32-bit clean) source pointer throws IAE
UNSAFE.copySwapMemory(null, buf, null, invalidPtr, 16, 2); thrownew RuntimeException("copySwapMemory failed to throw IAE for destOffset 0x" + Long.toHexString(invalidPtr));
} catch (IllegalArgumentException e) { // good
}
}
} finally { if (bufRaw != 0) {
UNSAFE.freeMemory(bufRaw);
}
}
}
/** * Run all tests * * @throws RuntimeException if an error is found
*/ privatevoid test() {
testPositive();
testNegative();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.