ByteBufferTest.java revision 13017:134219a5b0ec
1/* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23package org.graalvm.compiler.core.test; 24 25import java.nio.ByteBuffer; 26import java.nio.ByteOrder; 27import java.util.ArrayList; 28import java.util.Collection; 29import org.junit.Test; 30import org.junit.runner.RunWith; 31import org.junit.runners.Parameterized; 32import org.junit.runners.Parameterized.Parameter; 33import org.junit.runners.Parameterized.Parameters; 34 35@RunWith(Parameterized.class) 36public class ByteBufferTest extends GraalCompilerTest { 37 38 class Ret { 39 40 byte byteValue = 0; 41 short shortValue = 0; 42 int intValue = 0; 43 float floatValue = 0.0f; 44 double doubleValue = 0.0d; 45 46 @Override 47 public boolean equals(Object obj) { 48 if (!(obj instanceof Ret)) { 49 return false; 50 } 51 52 Ret other = (Ret) obj; 53 if (this.byteValue != other.byteValue) { 54 return false; 55 } 56 if (this.shortValue != other.shortValue) { 57 return false; 58 } 59 if (this.intValue != other.intValue) { 60 return false; 61 } 62 if (Float.floatToRawIntBits(this.floatValue) != Float.floatToRawIntBits(other.floatValue)) { 63 return false; 64 } 65 if (Double.doubleToRawLongBits(this.doubleValue) != Double.doubleToRawLongBits(other.doubleValue)) { 66 return false; 67 } 68 69 return true; 70 } 71 72 @Override 73 public int hashCode() { 74 return 0; 75 } 76 77 @Override 78 public String toString() { 79 return String.format("0x%02x, 0x%04x, 0x%08x, 0x%04x, 0x%08x", byteValue, shortValue, intValue, Float.floatToRawIntBits(floatValue), Double.doubleToRawLongBits(doubleValue)); 80 } 81 } 82 83 @Parameters(name = "{0}") 84 public static Collection<Object[]> data() { 85 ArrayList<Object[]> ret = new ArrayList<>(); 86 ret.add(new Object[]{ByteOrder.BIG_ENDIAN}); 87 ret.add(new Object[]{ByteOrder.LITTLE_ENDIAN}); 88 return ret; 89 } 90 91 @Parameter public ByteOrder byteOrder; 92 93 Ret alignedReadSnippet(byte[] arg) { 94 ByteBuffer buffer = ByteBuffer.wrap(arg).order(byteOrder); 95 96 Ret ret = new Ret(); 97 ret.byteValue = buffer.get(); 98 ret.byteValue += buffer.get(); 99 ret.shortValue = buffer.getShort(); 100 ret.intValue = buffer.getInt(); 101 ret.doubleValue = buffer.getDouble(); 102 ret.floatValue = buffer.getFloat(); 103 104 return ret; 105 } 106 107 @Test 108 public void testReadAligned() { 109 byte[] input = new byte[20]; 110 for (int i = 0; i < 20; i++) { 111 input[i] = (byte) (7 * (i + 42)); 112 } 113 test("alignedReadSnippet", input); 114 } 115 116 byte[] alignedWriteSnippet(byte a, byte b, short c, int d, double e, float f) { 117 byte[] ret = new byte[20]; 118 ByteBuffer buffer = ByteBuffer.wrap(ret).order(byteOrder); 119 120 buffer.put(a); 121 buffer.put(b); 122 buffer.putShort(c); 123 buffer.putInt(d); 124 buffer.putDouble(e); 125 buffer.putFloat(f); 126 127 return ret; 128 } 129 130 @Test 131 public void testWriteAligned() { 132 test("alignedWriteSnippet", (byte) 5, (byte) -3, (short) 17, 42, 84.72, 1.23f); 133 } 134 135 Ret unalignedReadSnippet(byte[] arg) { 136 ByteBuffer buffer = ByteBuffer.wrap(arg).order(byteOrder); 137 138 Ret ret = new Ret(); 139 ret.byteValue = buffer.get(); 140 ret.shortValue = buffer.getShort(); 141 ret.intValue = buffer.getInt(); 142 ret.doubleValue = buffer.getDouble(); 143 ret.floatValue = buffer.getFloat(); 144 145 return ret; 146 } 147 148 @Test 149 public void testReadUnaligned() { 150 byte[] input = new byte[19]; 151 for (int i = 0; i < 19; i++) { 152 input[i] = (byte) (7 * (i + 42)); 153 } 154 test("unalignedReadSnippet", input); 155 } 156 157 byte[] unalignedWriteSnippet(byte a, short b, int c, double d, float e) { 158 byte[] ret = new byte[20]; 159 ByteBuffer buffer = ByteBuffer.wrap(ret).order(byteOrder); 160 161 buffer.put(a); 162 buffer.putShort(b); 163 buffer.putInt(c); 164 buffer.putDouble(d); 165 buffer.putFloat(e); 166 167 return ret; 168 } 169 170 @Test 171 public void testWriteUnaligned() { 172 test("unalignedWriteSnippet", (byte) -3, (short) 17, 42, 84.72, 1.23f); 173 } 174} 175