1 /*
   2  * Copyright (c) 2016, 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  * @modules java.base/jdk.internal.misc
  27  */
  28 
  29 import java.nicl.*;
  30 import java.nicl.metadata.*;
  31 import java.nicl.metadata.Array;
  32 import java.nicl.types.*;
  33 
  34 public class StructTest {
  35     public static final boolean DEBUG = Boolean.getBoolean("StructTest.DEBUG");
  36 
  37     public static final long TOTAL_SIZE = 16;
  38     public static final long A_OFFSET  = 0;
  39     public static final long A_LENGTH  = 4;
  40     public static final long M_OFFSET  = 256;
  41     public static final long MA_OFFSET = 512;
  42     public static final int MA_LENGTH = 2;
  43 
  44     public static long alignUp(long n, long alignment) {
  45         return (n + alignment - 1) & ~(alignment - 1);
  46     }
  47 
  48     @NativeLocation(file="dummy", line=47, column=11, USR="c:@S@MyStruct")
  49     @NativeStruct("[4i]")
  50     @NativeType(ctype="struct MyStruct")
  51     static interface MyStruct extends Struct<MyStruct> {
  52         @NativeLocation(file="dummy", line=47, column=11, USR="c:@SA@MyStruct@field1")
  53         @NativeType(layout="4i", ctype="off_t")
  54         @Array(elementType="int", elementSize=4l, length=4l)
  55         @Offset(offset=0l)
  56         int[] a$get();
  57         void a$set(int[] a);
  58     }
  59 
  60     public int buildInt(long baseValue) {
  61         int tmp = 0;
  62 
  63         for (int i = 0; i < 4; i++) {
  64             tmp |= baseValue++ << (i * 8);
  65         }
  66 
  67         return tmp;
  68     }
  69 
  70 
  71     public long buildLong(long baseValue) {
  72         long tmp = 0;
  73 
  74         for (int i = 0; i < 8; i++) {
  75             tmp |= baseValue++ << (i * 8);
  76         }
  77 
  78         return tmp;
  79     }
  80 
  81     public void testIntArray(MyStruct s, Pointer<Byte> p) {
  82         {
  83             long expected = A_OFFSET / 8;
  84 
  85             int[] ia = s.a$get();
  86             assertEquals(A_LENGTH, ia.length);
  87 
  88             for (int i = 0; i < ia.length; i++, expected += 4) {
  89                 if (DEBUG) {
  90                     System.err.println("ia[" + i + "] = 0x" + Integer.toHexString(ia[i]));
  91                 }
  92                 try {
  93                     assertEquals(buildInt(expected), ia[i]);
  94                 } catch (Exception e) {
  95                     throw new RuntimeException("Failed to verify ia[" + i + "]", e);
  96                 }
  97             }
  98         }
  99 
 100         {
 101             int counter = 0x80;
 102 
 103             int[] ia = new int[4];
 104             for (int i = 0; i < ia.length; i++, counter += 4) {
 105                 ia[i] = buildInt(counter);
 106             }
 107             s.a$set(ia);
 108         }
 109 
 110         {
 111             int expected = 0x80;
 112 
 113             int[] ia = s.a$get();
 114             assertEquals(A_LENGTH, ia.length);
 115 
 116             for (int i = 0; i < ia.length; i++, expected += 4) {
 117                 int val = ia[i];
 118                 if (DEBUG) {
 119                     System.err.println("ia[" + i + "] = 0x" + Integer.toHexString(val));
 120                 }
 121                 try {
 122                     assertEquals(buildInt(expected), val);
 123                 } catch (Exception e) {
 124                     throw new RuntimeException("Failed to verify ia[" + i + "]", e);
 125                 }
 126             }
 127         }
 128     }
 129 
 130     private static void assertEquals(int expected, int actual) {
 131         if (expected != actual) {
 132             throw new RuntimeException("actual: 0x" + Long.toHexString(actual) + " does not match expected: 0x" + Long.toHexString(expected));
 133         }
 134     }
 135 
 136     private static void assertEquals(long expected, long actual) {
 137         if (expected != actual) {
 138             throw new RuntimeException("actual: 0x" + Long.toHexString(actual) + " does not match expected: 0x" + Long.toHexString(expected));
 139         }
 140     }
 141 
 142     public void test() {
 143         try (Scope scope = Scope.newNativeScope()) {
 144             MyStruct s = scope.allocateStruct(MyStruct.class);
 145             long size = TOTAL_SIZE;
 146             Pointer<Byte> p = scope.allocate(NativeTypes.UINT8, size);
 147 
 148             for (int i = 0; i < size; i++) {
 149                 p.offset(i).set((byte)i);
 150             }
 151             Pointer.copy(p, s.ptr(), size);
 152 
 153             testIntArray(s, p);
 154         } catch (IllegalAccessException iae) {
 155             throw new RuntimeException("Unexpected exception " + iae);
 156         }
 157     }
 158 
 159     public static void main(String[] args) {
 160         StructTest t = new StructTest();
 161         t.test();
 162     }
 163 }