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