< prev index next >

test/compiler/intrinsics/unsafe/HeapByteBufferTest.java

Print this page
rev 8304 : 8079459: JCK test api/java_nio/ByteBuffer/index.html#GetPutXXX start failing after JDK-8026049
Description: nextPutIndex used where nextGetIndex is correct.
Reviewed-by: alanb

*** 22,45 **** // questions. // // import com.oracle.java.testlibrary.Utils; ! import static java.lang.Math.abs; import java.nio.ByteBuffer; import java.nio.ByteOrder; import static java.nio.ByteOrder.BIG_ENDIAN; import static java.nio.ByteOrder.LITTLE_ENDIAN; - import java.util.Random; - import java.util.Arrays; /** * @test * @bug 8026049 * @library /testlibrary ! * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:-UseUnalignedAccesses HeapByteBufferTest ! * @run main/othervm HeapByteBufferTest * @summary Verify that byte buffers are correctly accessed. */ // A wrapper for a ByteBuffer which maintains a backing array and a // position. Whenever this wrapper is written the backing array and --- 22,47 ---- // questions. // // import com.oracle.java.testlibrary.Utils; ! import java.nio.BufferOverflowException; ! import java.nio.BufferUnderflowException; import java.nio.ByteBuffer; import java.nio.ByteOrder; + import java.util.Arrays; + import java.util.Random; + import static java.lang.Math.abs; import static java.nio.ByteOrder.BIG_ENDIAN; import static java.nio.ByteOrder.LITTLE_ENDIAN; /** * @test * @bug 8026049 * @library /testlibrary ! * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:-UseUnalignedAccesses -Dcom.oracle.java.testlibrary.random.seed=0 HeapByteBufferTest ! * @run main/othervm -Dcom.oracle.java.testlibrary.random.seed=0 HeapByteBufferTest * @summary Verify that byte buffers are correctly accessed. */ // A wrapper for a ByteBuffer which maintains a backing array and a // position. Whenever this wrapper is written the backing array and
*** 343,353 **** --- 345,488 ---- result = Float.intBitsToFloat(bits | 0x400000); } return result; } + enum PrimitiveType { + BYTE(1), CHAR(2), SHORT(2), INT(4), LONG(8), FLOAT(4), DOUBLE(8); + + public final int size; + PrimitiveType(int size) { + this.size = size; + } + } + + void getOne(ByteBuffer b, PrimitiveType t) { + switch (t) { + case BYTE: b.get(); break; + case CHAR: b.getChar(); break; + case SHORT: b.getShort(); break; + case INT: b.getInt(); break; + case LONG: b.getLong(); break; + case FLOAT: b.getFloat(); break; + case DOUBLE: b.getDouble(); break; + } + } + + void putOne(ByteBuffer b, PrimitiveType t) { + switch (t) { + case BYTE: b.put((byte)0); break; + case CHAR: b.putChar('0'); break; + case SHORT: b.putShort((short)0); break; + case INT: b.putInt(0); break; + case LONG: b.putLong(0); break; + case FLOAT: b.putFloat(0); break; + case DOUBLE: b.putDouble(0); break; + } + } + + void getOne(ByteBuffer b, PrimitiveType t, int index) { + switch (t) { + case BYTE: b.get(index); break; + case CHAR: b.getChar(index); break; + case SHORT: b.getShort(index); break; + case INT: b.getInt(index); break; + case LONG: b.getLong(index); break; + case FLOAT: b.getFloat(index); break; + case DOUBLE: b.getDouble(index); break; + } + } + + void putOne(ByteBuffer b, PrimitiveType t, int index) { + switch (t) { + case BYTE: b.put(index, (byte)0); break; + case CHAR: b.putChar(index, '0'); break; + case SHORT: b.putShort(index, (short)0); break; + case INT: b.putInt(index, 0); break; + case LONG: b.putLong(index, 0); break; + case FLOAT: b.putFloat(index, 0); break; + case DOUBLE: b.putDouble(index, 0); break; + } + } + + void checkBoundaryConditions() { + long errors = 0; + for (int i = 0; i < 100; i++) { + int bufSize = random.nextInt(16); + byte[] bytes = new byte[bufSize]; + ByteBuffer buf = ByteBuffer.wrap(bytes); + for (int j = 0; j < 100; j++) { + int offset = random.nextInt(32) - 8; + for (PrimitiveType t : PrimitiveType.values()) { + boolean threw = false; + try { + buf.position(offset); + getOne(buf, t); + } catch (BufferUnderflowException e) { + if (offset + t.size < bufSize) + throw new RuntimeException("" + e + ", type = " + t + ", offset = " + offset + ", bufSize = " + bufSize); + threw = true; + } catch (IllegalArgumentException e) { + if (offset >= 0 && offset + t.size < bufSize) + throw new RuntimeException("" + e + ", type = " + t + ", offset = " + offset + ", bufSize = " + bufSize); + threw = true; + } catch (Throwable th) { + throw new RuntimeException("" + th + ", type = " + t + ", offset = " + offset + ", bufSize = " + bufSize); + } + + try { + buf.position(offset); + putOne(buf, t); + } catch (BufferOverflowException e) { + if (offset + t.size < bufSize) + throw new RuntimeException("" + e + ", type = " + t + ", offset = " + offset + ", bufSize = " + bufSize); + threw = true; + } catch (IllegalArgumentException e) { + if (offset >= 0 && offset + t.size < bufSize) + throw new RuntimeException("" + e + ", type = " + t + ", offset = " + offset + ", bufSize = " + bufSize); + threw = true; + } catch (Throwable th) { + throw new RuntimeException("" + th + ", type = " + t + ", offset = " + offset + ", bufSize = " + bufSize); + } + + try { + putOne(buf, t, offset); + } catch (IndexOutOfBoundsException e) { + if (offset >= 0 && offset + t.size < bufSize) + throw new RuntimeException("" + e + ", type = " + t + ", offset = " + offset + ", bufSize = " + bufSize); + threw = true; + } catch (Throwable th) { + throw new RuntimeException("" + th + ", type = " + t + ", offset = " + offset + ", bufSize = " + bufSize); + } + + try { + getOne(buf, t, offset); + } catch (IndexOutOfBoundsException e) { + if (offset >= 0 && offset + t.size < bufSize) + throw new RuntimeException("" + e + ", type = " + t + ", offset = " + offset + ", bufSize = " + bufSize); + threw = true; + } catch (Throwable th) { + throw new RuntimeException("" + th + ", type = " + t + ", offset = " + offset + ", bufSize = " + bufSize); + } + + if (! threw) { + if (offset < 0 || offset + t.size > bufSize) { + throw new RuntimeException("should have thrown but did not, type = " + t + + ", offset = " + offset + ", bufSize = " + bufSize); + } + } + } + } + } + if (errors != 0) { + throw new RuntimeException(); + } + } + public void run() { + checkBoundaryConditions(); + for (int i = 0; i < data.capacity(); i += 8) { data.putLong(i, random.nextLong()); } for (int i = 0; i < iterations; i++) {
< prev index next >