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