1 /* 2 * Copyright (c) 2016, 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 */ 23 24 /** 25 * @test 26 * @requires vm.bits == "64" & (os.arch == "amd64" | os.arch == "x86_64") & !(os.family == "windows") 27 * @modules jdk.aot/jdk.tools.jaotc.utils 28 * @run junit/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI jdk.tools.jaotc.test.NativeOrderOutputStreamTest 29 */ 30 31 package jdk.tools.jaotc.test; 32 33 import jdk.tools.jaotc.utils.NativeOrderOutputStream; 34 import org.junit.Assert; 35 import org.junit.Before; 36 import org.junit.Test; 37 38 import java.nio.ByteBuffer; 39 import java.nio.ByteOrder; 40 41 public class NativeOrderOutputStreamTest { 42 43 private NativeOrderOutputStream target; 44 45 @Before 46 public void setup() { 47 target = new NativeOrderOutputStream(); 48 } 49 50 @Test 51 public void shouldAdd4BytesForInt() { 52 target.putInt(5); 53 Assert.assertEquals(4, target.position()); 54 } 55 56 @Test 57 public void shouldAdd8BytesForLong() { 58 target.putLong(8); 59 Assert.assertEquals(8, target.position()); 60 } 61 62 @Test 63 public void shouldHaveCorrectSizeBeforePatch() { 64 target.patchableInt(); 65 Assert.assertEquals(4, target.position()); 66 } 67 68 @Test 69 public void shouldHaveCorrectSizeAfterPatch() { 70 NativeOrderOutputStream.PatchableInt patchableInt = target.patchableInt(); 71 patchableInt.set(12); 72 Assert.assertEquals(4, target.position()); 73 } 74 75 @Test 76 public void shouldSetCorrectValueInPatch() { 77 NativeOrderOutputStream.PatchableInt patchableInt = target.patchableInt(); 78 patchableInt.set(42); 79 Assert.assertEquals(42, getInt(0)); 80 } 81 82 private int getInt(int pos) { 83 ByteBuffer buffer = ByteBuffer.wrap(target.array()); 84 buffer.order(ByteOrder.nativeOrder()); 85 return buffer.getInt(pos); 86 } 87 88 @Test 89 public void shouldPutArrayCorrectly() { 90 target.put(new byte[]{42, 5, 43, 44}); 91 Assert.assertEquals(4, target.position()); 92 Assert.assertEquals(42, target.array()[0]); 93 Assert.assertEquals(4, target.position()); 94 } 95 96 @Test 97 public void shouldOnlyPatchSlot() { 98 NativeOrderOutputStream.PatchableInt patchableInt = target.patchableInt(); 99 target.putInt(7); 100 patchableInt.set(39); 101 Assert.assertEquals(39, getInt(0)); 102 Assert.assertEquals(7, getInt(4)); 103 } 104 105 @Test 106 public void shouldBeAbleToPatchAnywhere() { 107 target.putInt(19); 108 NativeOrderOutputStream.PatchableInt patchableInt = target.patchableInt(); 109 patchableInt.set(242); 110 111 Assert.assertEquals(19, getInt(0)); 112 Assert.assertEquals(242, getInt(4)); 113 } 114 115 @Test 116 public void shouldHavePatchableAtRightOffset() { 117 target.putInt(27); 118 Assert.assertEquals(4, target.position()); 119 NativeOrderOutputStream.PatchableInt patchableInt = target.patchableInt(); 120 Assert.assertEquals(4, patchableInt.position()); 121 } 122 123 @Test 124 public void shouldAlign() { 125 target.putInt(9); 126 target.align(16); 127 target.put(new byte[]{3}); 128 target.align(8); 129 Assert.assertEquals(24, target.position()); 130 } 131 }