< prev index next >

test/compiler/intrinsics/unsafe/HeapByteBufferTest.java

Print this page
rev 11557 : 8132919: use package in compiler tests
Reviewed-by: duke
   1 //
   2 // Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
   3 // Copyright (c) 2015, Red Hat Inc. All rights reserved.
   4 // DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5 //
   6 // This code is free software; you can redistribute it and/or modify it
   7 // under the terms of the GNU General Public License version 2 only, as
   8 // published by the Free Software Foundation.
   9 //
  10 // This code is distributed in the hope that it will be useful, but WITHOUT
  11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13 // version 2 for more details (a copy is included in the LICENSE file that
  14 // accompanied this code).
  15 //
  16 // You should have received a copy of the GNU General Public License version
  17 // 2 along with this work; if not, write to the Free Software Foundation,
  18 // Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19 //
  20 // Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21 // or visit www.oracle.com if you need additional information or have any
  22 // questions.
  23 //
  24 //
  25 

















  26 import java.nio.BufferOverflowException;
  27 import java.nio.BufferUnderflowException;
  28 import java.nio.ByteBuffer;
  29 import static java.nio.ByteOrder.BIG_ENDIAN;
  30 import static java.nio.ByteOrder.LITTLE_ENDIAN;
  31 import java.nio.ByteOrder;
  32 import java.util.Arrays;
  33 import java.util.Random;
  34 import jdk.test.lib.Utils;
  35 
  36 /**
  37  * @test
  38  * @bug 8026049
  39  * @modules java.base/jdk.internal.misc
  40  * @library /testlibrary
  41  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:-UseUnalignedAccesses -Djdk.test.lib.random.seed=0 HeapByteBufferTest
  42  * @run main/othervm -Djdk.test.lib.random.seed=0 HeapByteBufferTest
  43  * @summary Verify that byte buffers are correctly accessed.
  44  */
  45 
  46 // A wrapper for a ByteBuffer which maintains a backing array and a
  47 // position.  Whenever this wrapper is written the backing array and
  48 // the wrapped byte buffer are updated together, and whenever it is
  49 // read we check that the ByteBuffer and the backing array are identical.
  50 
  51 class MyByteBuffer {

  52     final ByteBuffer buf;
  53     final byte[] bytes;
  54     int pos;
  55     ByteOrder byteOrder = BIG_ENDIAN;
  56 
  57     MyByteBuffer(ByteBuffer buf, byte[] bytes) {
  58         this.buf = buf;
  59         this.bytes = Arrays.copyOf(bytes, bytes.length);
  60         pos = 0;
  61     }
  62 
  63     public final MyByteBuffer order(ByteOrder bo) {
  64         byteOrder = bo;
  65         buf.order(bo);
  66         return this;
  67     }
  68 
  69     static MyByteBuffer wrap(byte[] bytes) {
  70         return new MyByteBuffer(ByteBuffer.wrap(bytes), bytes);
  71     }


 224     void putShort(int i, short x) { buf.putShort(i, x); putShortX(i, x); }
 225     void putChar(int i, char x) { buf.putChar(i, x); putShortX(i, (short)x); }
 226     void putDouble(int i, double x) { buf.putDouble(i, x); putLongX(i, Double.doubleToRawLongBits(x)); }
 227     void putFloat(int i, float x) { buf.putFloat(i, x); putIntX(i, Float.floatToRawIntBits(x)); }
 228 
 229     long getLong() { ck(buf.getLong(buf.position()), getLongX(pos)); long x = buf.getLong(); pos += 8; return x; }
 230     int getInt() { ck(buf.getInt(buf.position()), getIntX(pos)); int x = buf.getInt(); pos += 4; return x; }
 231     short getShort() { ck(buf.getShort(buf.position()), getShortX(pos)); short x = buf.getShort(); pos += 2; return x; }
 232     char getChar() {  ck(buf.getChar(buf.position()), (char)getShortX(pos)); char x = buf.getChar(); pos += 2; return x; }
 233     double getDouble() { ck(buf.getDouble(buf.position()), getDoubleX(pos)); double x = buf.getDouble(); pos += 8; return x; }
 234     float getFloat() { ck(buf.getFloat(buf.position()), getFloatX(pos)); float x = buf.getFloat(); pos += 4; return x; }
 235 
 236     void putLong(long x) { putLongX(pos, x); pos += 8; buf.putLong(x); }
 237     void putInt(int x) { putIntX(pos, x); pos += 4; buf.putInt(x); }
 238     void putShort(short x) { putShortX(pos, x); pos += 2; buf.putShort(x); }
 239     void putChar(char x) { putShortX(pos, (short)x); pos += 2; buf.putChar(x); }
 240     void putDouble(double x) { putLongX(pos, Double.doubleToRawLongBits(x)); pos += 8; buf.putDouble(x); }
 241     void putFloat(float x) { putIntX(pos, Float.floatToRawIntBits(x)); pos += 4; buf.putFloat(x); }
 242 
 243     void rewind() { pos = 0; buf.rewind(); }
 244 }
 245 
 246 public class HeapByteBufferTest implements Runnable {
 247 
 248     Random random = Utils.getRandomInstance();
 249     MyByteBuffer data = MyByteBuffer.wrap(new byte[1024]);
 250 
 251     int randomOffset(Random r, MyByteBuffer buf, int size) {
 252         return r.nextInt(buf.capacity() - size);
 253     }
 254 
 255     long iterations;
 256 
 257     HeapByteBufferTest(long iterations) {
 258         this.iterations = iterations;
 259     }
 260 
 261     // The core of the test.  Walk over the buffer reading and writing
 262     // random data, XORing it as we go.  We can detect writes in the
 263     // wrong place, writes which are too long or too short, and reads
 264     // or writes of the wrong data,
 265     void step(Random r) {
 266         data.order((r.nextInt() & 1) != 0 ? BIG_ENDIAN : LITTLE_ENDIAN);
 267 


   1 /*
   2  * Copyright (c) 200, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2015, Red Hat Inc. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 /**
  27  * @test
  28  * @bug 8026049
  29  * @summary Verify that byte buffers are correctly accessed.
  30  * @modules java.base/jdk.internal.misc
  31  * @library /testlibrary
  32  *
  33  * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:-UseUnalignedAccesses -Djdk.test.lib.random.seed=0
  34  *      compiler.intrinsics.unsafe.HeapByteBufferTest
  35  * @run main/othervm -Djdk.test.lib.random.seed=0
  36  *      compiler.intrinsics.unsafe.HeapByteBufferTest
  37  */
  38 
  39 package compiler.intrinsics.unsafe;
  40 
  41 import jdk.test.lib.Utils;
  42 
  43 import java.nio.BufferOverflowException;
  44 import java.nio.BufferUnderflowException;
  45 import java.nio.ByteBuffer;


  46 import java.nio.ByteOrder;
  47 import java.util.Arrays;
  48 import java.util.Random;

  49 
  50 import static java.nio.ByteOrder.BIG_ENDIAN;
  51 import static java.nio.ByteOrder.LITTLE_ENDIAN;







  52 
  53 // A wrapper for a ByteBuffer which maintains a backing array and a
  54 // position.  Whenever this wrapper is written the backing array and
  55 // the wrapped byte buffer are updated together, and whenever it is
  56 // read we check that the ByteBuffer and the backing array are identical.
  57 
  58 public class HeapByteBufferTest implements Runnable {
  59     static class MyByteBuffer {
  60         final ByteBuffer buf;
  61         final byte[] bytes;
  62         int pos;
  63         ByteOrder byteOrder = BIG_ENDIAN;
  64 
  65         MyByteBuffer(ByteBuffer buf, byte[] bytes) {
  66             this.buf = buf;
  67             this.bytes = Arrays.copyOf(bytes, bytes.length);
  68             pos = 0;
  69         }
  70 
  71         public final MyByteBuffer order(ByteOrder bo) {
  72             byteOrder = bo;
  73             buf.order(bo);
  74             return this;
  75         }
  76 
  77         static MyByteBuffer wrap(byte[] bytes) {
  78             return new MyByteBuffer(ByteBuffer.wrap(bytes), bytes);
  79         }


 232         void putShort(int i, short x) { buf.putShort(i, x); putShortX(i, x); }
 233         void putChar(int i, char x) { buf.putChar(i, x); putShortX(i, (short)x); }
 234         void putDouble(int i, double x) { buf.putDouble(i, x); putLongX(i, Double.doubleToRawLongBits(x)); }
 235         void putFloat(int i, float x) { buf.putFloat(i, x); putIntX(i, Float.floatToRawIntBits(x)); }
 236 
 237         long getLong() { ck(buf.getLong(buf.position()), getLongX(pos)); long x = buf.getLong(); pos += 8; return x; }
 238         int getInt() { ck(buf.getInt(buf.position()), getIntX(pos)); int x = buf.getInt(); pos += 4; return x; }
 239         short getShort() { ck(buf.getShort(buf.position()), getShortX(pos)); short x = buf.getShort(); pos += 2; return x; }
 240         char getChar() {  ck(buf.getChar(buf.position()), (char)getShortX(pos)); char x = buf.getChar(); pos += 2; return x; }
 241         double getDouble() { ck(buf.getDouble(buf.position()), getDoubleX(pos)); double x = buf.getDouble(); pos += 8; return x; }
 242         float getFloat() { ck(buf.getFloat(buf.position()), getFloatX(pos)); float x = buf.getFloat(); pos += 4; return x; }
 243 
 244         void putLong(long x) { putLongX(pos, x); pos += 8; buf.putLong(x); }
 245         void putInt(int x) { putIntX(pos, x); pos += 4; buf.putInt(x); }
 246         void putShort(short x) { putShortX(pos, x); pos += 2; buf.putShort(x); }
 247         void putChar(char x) { putShortX(pos, (short)x); pos += 2; buf.putChar(x); }
 248         void putDouble(double x) { putLongX(pos, Double.doubleToRawLongBits(x)); pos += 8; buf.putDouble(x); }
 249         void putFloat(float x) { putIntX(pos, Float.floatToRawIntBits(x)); pos += 4; buf.putFloat(x); }
 250 
 251         void rewind() { pos = 0; buf.rewind(); }
 252     }
 253 


 254     Random random = Utils.getRandomInstance();
 255     MyByteBuffer data = MyByteBuffer.wrap(new byte[1024]);
 256 
 257     int randomOffset(Random r, MyByteBuffer buf, int size) {
 258         return r.nextInt(buf.capacity() - size);
 259     }
 260 
 261     long iterations;
 262 
 263     HeapByteBufferTest(long iterations) {
 264         this.iterations = iterations;
 265     }
 266 
 267     // The core of the test.  Walk over the buffer reading and writing
 268     // random data, XORing it as we go.  We can detect writes in the
 269     // wrong place, writes which are too long or too short, and reads
 270     // or writes of the wrong data,
 271     void step(Random r) {
 272         data.order((r.nextInt() & 1) != 0 ? BIG_ENDIAN : LITTLE_ENDIAN);
 273 


< prev index next >