1 /*
  2  * Copyright (c) 2020, 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 import jdk.incubator.foreign.CLinker;
 25 import jdk.incubator.foreign.MemorySegment;
 26 import org.testng.annotations.AfterTest;
 27 import org.testng.annotations.BeforeTest;
 28 import org.testng.annotations.DataProvider;
 29 import org.testng.annotations.Test;
 30 import java.nio.file.Path;
 31 import java.util.function.Consumer;
 32 import java.util.function.Predicate;
 33 import java.util.stream.Stream;
 34 import jdk.incubator.foreign.GroupLayout;
 35 import jdk.incubator.foreign.MemoryAddress;
 36 
 37 import static org.testng.Assert.assertEquals;
 38 import static org.testng.Assert.assertNotNull;
 39 import static org.testng.Assert.assertSame;
 40 import static org.testng.Assert.assertTrue;
 41 
 42 /*
 43  * @test
 44  * @modules jdk.incubator.jextract
 45  * @library /test/lib
 46  * @build ConstantsTest
 47  * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED ConstantsTest
 48  */
 49 public class ConstantsTest extends JextractToolRunner {
 50     private Class<?> constants;
 51     private Path dirPath;
 52     private Loader loader;
 53 
 54     @BeforeTest
 55     public void setup() {
 56         dirPath = getOutputFilePath("ConstantsTest_output");
 57         run( "-d", dirPath.toString(), getInputFilePath("constants.h").toString()).checkSuccess();
 58         loader = classLoader(dirPath);
 59         constants = loader.loadClass("constants_h");
 60     }
 61 
 62     @AfterTest
 63     public void cleanup() {
 64         constants = null;
 65         loader.close();
 66         deleteDir(dirPath);
 67     }
 68 
 69     @Test(dataProvider = "definedConstants")
 70     public void checkConstantsTypesAndValues(String name, Class<?> type, Consumer<Object> checker) throws ReflectiveOperationException {
 71         var f = findMethod(constants, name);
 72         assertNotNull(f);
 73         assertSame(f.getReturnType(), type);
 74         f.setAccessible(true);
 75         Object actual = f.invoke(null);
 76         checker.accept(actual);
 77     }
 78 
 79     @Test(dataProvider = "missingConstants")
 80     public void checkMissingConstants(String name) {
 81         assertTrue(findMethod(constants, name) == null);
 82     }
 83 
 84     @DataProvider
 85     public static Object[][] definedConstants() {
 86         return new Object[][] {
 87                 { "SUP", int.class, equalsTo(5) },
 88                 { "ZERO", int.class, equalsTo(0) },
 89                 { "ONE", int.class, equalsTo(1) },
 90                 { "TWO", int.class, equalsTo(2) },
 91                 { "THREE", int.class, equalsTo(3) },
 92                 { "FOUR", long.class, equalsTo(4L) },
 93                 { "FIVE", long.class, equalsTo(5L) },
 94                 { "SIX", int.class, equalsTo(6) },
 95                 { "FLOAT_VALUE", float.class, equalsTo(1.32f) },
 96                 { "DOUBLE_VALUE", double.class, (Consumer<Double>) (actual -> assertEquals(actual, 1.32, 0.1)) },
 97                 { "CHAR_VALUE", int.class, equalsTo(104) }, //integer char constants have type int
 98                 { "MULTICHAR_VALUE", int.class, equalsTo(26728) },  //integer char constants have type int
 99                 { "BOOL_VALUE", byte.class, equalsTo((byte)1) },
100                 { "SUB", int.class, equalsTo( 7 ) },
101                 // pointer type values
102                 { "STR", MemorySegment.class, equalsToJavaStr("Hello") },
103                 { "QUOTE", MemorySegment.class, equalsToJavaStr("QUOTE") },
104                 { "ZERO_PTR", MemoryAddress.class, equalsPtrContents(0) },
105                 { "F_PTR", MemoryAddress.class, equalsPtrContents(0xFFFFFFFFFFFFFFFFL) },
106         };
107     }
108 
109     static Consumer<Object> equalsTo(Object expected) {
110         return actual -> assertEquals(actual, expected);
111     }
112 
113     static Consumer<MemorySegment> equalsToJavaStr(String expected) {
114         return actual -> assertEquals(actual.getUtf8String(0), expected);
115     }
116 
117     static Consumer<MemoryAddress> equalsPtrContents(long expected) {
118         return actual -> assertEquals(actual.toRawLongValue(), expected);
119     }
120 
121     @DataProvider
122     public static Object[][] missingConstants() {
123         return new Object[][] {
124                 { "ID" },
125                 { "SUM" },
126                 { "BLOCK_BEGIN" },
127                 { "BLOCK_END" },
128                 { "INTEGER_MAX_VALUE" },
129                 { "CYCLIC_1" },
130                 { "CYCLIC_2" },
131                 // array
132                 { "ARRAY" }
133         };
134     }
135 }