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