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