1 /*
   2  * Copyright (c) 2016, 2019, 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 java.nio.ByteBuffer;
  36 import java.nio.ByteOrder;
  37 
  38 import org.junit.Assert;
  39 import org.junit.Test;
  40 
  41 import jdk.tools.jaotc.utils.NativeOrderOutputStream;
  42 
  43 public class NativeOrderOutputStreamTest {
  44 
  45     @Test
  46     public void shouldAdd4BytesForInt() {
  47         NativeOrderOutputStream target = new NativeOrderOutputStream();
  48         target.putInt(5);
  49         Assert.assertEquals(4, target.position());
  50     }
  51 
  52     @Test
  53     public void shouldAdd8BytesForLong() {
  54         NativeOrderOutputStream target = new NativeOrderOutputStream();
  55         target.putLong(8);
  56         Assert.assertEquals(8, target.position());
  57     }
  58 
  59     @Test
  60     public void shouldHaveCorrectSizeBeforePatch() {
  61         NativeOrderOutputStream target = new NativeOrderOutputStream();
  62         target.patchableInt();
  63         Assert.assertEquals(4, target.position());
  64     }
  65 
  66     @Test
  67     public void shouldHaveCorrectSizeAfterPatch() {
  68         NativeOrderOutputStream target = new NativeOrderOutputStream();
  69         NativeOrderOutputStream.PatchableInt patchableInt = target.patchableInt();
  70         patchableInt.set(12);
  71         Assert.assertEquals(4, target.position());
  72     }
  73 
  74     @Test
  75     public void shouldSetCorrectValueInPatch() {
  76         NativeOrderOutputStream target = new NativeOrderOutputStream();
  77         NativeOrderOutputStream.PatchableInt patchableInt = target.patchableInt();
  78         patchableInt.set(42);
  79         Assert.assertEquals(42, getInt(target, 0));
  80     }
  81 
  82     private static int getInt(NativeOrderOutputStream target, 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         NativeOrderOutputStream target = new NativeOrderOutputStream();
  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 target = new NativeOrderOutputStream();
 100         NativeOrderOutputStream.PatchableInt patchableInt = target.patchableInt();
 101         target.putInt(7);
 102         patchableInt.set(39);
 103         Assert.assertEquals(39, getInt(target, 0));
 104         Assert.assertEquals(7, getInt(target, 4));
 105     }
 106 
 107     @Test
 108     public void shouldBeAbleToPatchAnywhere() {
 109         NativeOrderOutputStream target = new NativeOrderOutputStream();
 110         target.putInt(19);
 111         NativeOrderOutputStream.PatchableInt patchableInt = target.patchableInt();
 112         patchableInt.set(242);
 113 
 114         Assert.assertEquals(19, getInt(target, 0));
 115         Assert.assertEquals(242, getInt(target, 4));
 116     }
 117 
 118     @Test
 119     public void shouldHavePatchableAtRightOffset() {
 120         NativeOrderOutputStream target = new NativeOrderOutputStream();
 121         target.putInt(27);
 122         Assert.assertEquals(4, target.position());
 123         NativeOrderOutputStream.PatchableInt patchableInt = target.patchableInt();
 124         Assert.assertEquals(4, patchableInt.position());
 125     }
 126 
 127     @Test
 128     public void shouldAlign() {
 129         NativeOrderOutputStream target = new NativeOrderOutputStream();
 130         target.putInt(9);
 131         target.align(16);
 132         target.put(new byte[]{3});
 133         target.align(8);
 134         Assert.assertEquals(24, target.position());
 135     }
 136 }