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 */ 27 28 import java.lang.invoke.MethodHandles; 29 import java.nicl.*; 30 import java.nicl.types.*; 31 import java.nicl.metadata.*; 32 33 public class GlobalVariable { 34 @NativeHeader 35 static interface globvar { 36 @C(file="dummy", line=1, column=1, USR="c:@F@init") 37 @NativeType(layout="()V", ctype="dummy", size=1) 38 @CallingConvention(value=1) 39 public abstract void init(); 40 41 @C(file="dummy", line=1, column=1, USR="c:@global_boolean") 42 @NativeType(layout="B", ctype="dummy", size=1, name="global_boolean") 43 public abstract boolean global_boolean$get(); 44 public abstract void global_boolean$set(boolean arg); 45 public abstract Pointer<Boolean> global_boolean$ptr(); 46 47 @C(file="dummy", line=1, column=1, USR="c:@global_i8") 48 @NativeType(layout="c", ctype="dummy", size=1, name="global_i8") 49 public abstract byte global_i8$get(); 50 public abstract void global_i8$set(byte arg); 51 public abstract Pointer<Byte> global_i8$ptr(); 52 53 @C(file="dummy", line=1, column=1, USR="c:@global_i16") 54 @NativeType(layout="s", ctype="dummy", size=2, name="global_i16") 55 public abstract short global_i16$get(); 56 public abstract void global_i16$set(short arg); 57 public abstract Pointer<Short> global_i16$ptr(); 58 59 @C(file="dummy", line=1, column=1, USR="c:@global_i32") 60 @NativeType(layout="i", ctype="dummy", size=4, name="global_i32") 61 public abstract int global_i32$get(); 62 public abstract void global_i32$set(int arg); 63 public abstract Pointer<Integer> global_i32$ptr(); 64 65 @C(file="dummy", line=1, column=1, USR="c:@global_i64") 66 @NativeType(layout="l", ctype="dummy", size=8, name="global_i64") 67 public abstract long global_i64$get(); 68 public abstract void global_i64$set(long arg); 69 public abstract Pointer<Long> global_i64$ptr(); 70 71 @C(file="dummy", line=1, column=1, USR="c:@global_f32") 72 @NativeType(layout="f", ctype="dummy", size=4, name="global_f32") 73 public abstract float global_f32$get(); 74 public abstract void global_f32$set(float arg); 75 public abstract Pointer<Float> global_f32$ptr(); 76 77 @C(file="dummy", line=1, column=1, USR="c:@global_d64") 78 @NativeType(layout="d", ctype="dummy", size=8, name="global_d64") 79 public abstract double global_d64$get(); 80 public abstract void global_d64$set(double arg); 81 public abstract Pointer<Double> global_d64$ptr(); 82 83 @NativeType(layout="[i]", ctype="dummy", size=4, isRecordType=true) 84 @C(file="dummy", line=47, column=11, USR="C:@S@MyStruct") 85 static interface MyStruct extends Struct<MyStruct> { 86 @Offset(offset=0l) 87 @C(file="dummy", line=47, column=11, USR="c:@SA@MyStruct@FI@i") 88 @NativeType(layout="i", ctype="int", size=4l) 89 int i$get(); 90 void i$set(int i); 91 } 92 93 @C(file="dummy", line=1, column=1, USR="c:@global_struct") 94 @NativeType(layout="[i]", ctype="dummy", size=4, name="global_struct") 95 public abstract MyStruct global_struct$get(); 96 public abstract void global_struct$set(MyStruct arg); 97 public abstract Pointer<MyStruct> global_struct$ptr(); 98 } 99 100 private final globvar i; 101 { 102 i = Libraries.bind(globvar.class, Libraries.loadLibrary(MethodHandles.lookup(), "GlobalVariable")); 103 i.init(); 104 } 105 106 public void testboolean() { 107 // boolean 108 assertTrue(i.global_boolean$get()); 109 assertTrue(i.global_boolean$ptr().get()); 110 111 i.global_boolean$set(false); 112 113 assertFalse(i.global_boolean$get()); 114 assertFalse(i.global_boolean$ptr().get()); 115 } 116 117 118 public void testi8() { 119 // int8_t 120 assertEquals(42, i.global_i8$get()); 121 assertEquals(42, i.global_i8$ptr().get()); 122 123 i.global_i8$set((byte)47); 124 125 assertEquals(47, i.global_i8$get()); 126 assertEquals(47, i.global_i8$ptr().get()); 127 } 128 129 public void testi16() { 130 // int16_t 131 assertEquals(42, i.global_i16$get()); 132 assertEquals(42, i.global_i16$ptr().get()); 133 134 i.global_i16$set((short)47); 135 136 assertEquals(47, i.global_i16$get()); 137 assertEquals(47, i.global_i16$ptr().get()); 138 } 139 140 public void testi32() { 141 // int32_t 142 assertEquals(42, i.global_i32$get()); 143 assertEquals(42, i.global_i32$ptr().get()); 144 145 i.global_i32$set(47); 146 147 assertEquals(47, i.global_i32$get()); 148 assertEquals(47, i.global_i32$ptr().get()); 149 } 150 151 public void testi64() { 152 // int64_t 153 assertEquals(42, i.global_i64$get()); 154 assertEquals(42, i.global_i64$ptr().get()); 155 156 i.global_i64$set(47); 157 158 assertEquals(47, i.global_i64$get()); 159 assertEquals(47, i.global_i64$ptr().get()); 160 } 161 162 public void testf32() { 163 // float 164 assertEquals(42f, i.global_f32$get()); 165 assertEquals(42f, i.global_f32$ptr().get()); 166 167 i.global_f32$set(47f); 168 169 assertEquals(47f, i.global_f32$get()); 170 assertEquals(47f, i.global_f32$ptr().get()); 171 } 172 173 public void testd64() { 174 // double 175 assertEquals(42.0, i.global_d64$get()); 176 assertEquals(42.0, i.global_d64$ptr().get()); 177 178 i.global_d64$set(47.0); 179 180 assertEquals(47.0, i.global_d64$get()); 181 assertEquals(47.0, i.global_d64$ptr().get()); 182 } 183 184 public void teststruct() { 185 assertEquals(42, i.global_struct$get().i$get()); 186 assertEquals(42, i.global_struct$ptr().get().i$get()); 187 188 try (Scope scope = Scope.newNativeScope()) { 189 globvar.MyStruct s = scope.allocateStruct(globvar.MyStruct.class); 190 191 s.i$set(47); 192 193 i.global_struct$set(s); 194 } 195 196 assertEquals(47, i.global_struct$get().i$get()); 197 assertEquals(47, i.global_struct$ptr().get().i$get()); 198 } 199 200 201 static void assertEquals(long expected, long actual) { 202 if (expected != actual) { 203 throw new RuntimeException("expected: " + expected + " does not match actual: " + actual); 204 } 205 } 206 207 static void assertEquals(float expected, float actual) { 208 if (expected != actual) { 209 throw new RuntimeException("expected: " + expected + " does not match actual: " + actual); 210 } 211 } 212 213 static void assertEquals(double expected, double actual) { 214 if (expected != actual) { 215 throw new RuntimeException("expected: " + expected + " does not match actual: " + actual); 216 } 217 } 218 219 static void assertTrue(boolean actual) { 220 if (!actual) { 221 throw new RuntimeException("expected: true does not match actual: " + actual); 222 } 223 } 224 225 static void assertFalse(boolean actual) { 226 if (actual) { 227 throw new RuntimeException("expected: false does not match actual: " + actual); 228 } 229 } 230 231 public static void main(String[] args) { 232 GlobalVariable t = new GlobalVariable(); 233 234 t.testboolean(); 235 t.testi8(); 236 t.testi16(); 237 t.testi32(); 238 t.testi64(); 239 t.testf32(); 240 t.testd64(); 241 t.teststruct(); 242 } 243 }