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 java.lang.reflect.Field; 25 import java.lang.reflect.Method; 26 import java.nio.file.Path; 27 import org.testng.annotations.Test; 28 import static org.testng.Assert.assertEquals; 29 import static org.testng.Assert.assertNotNull; 30 import static org.testng.Assert.assertTrue; 31 32 /* 33 * @test 34 * @modules jdk.incubator.jextract 35 * @library /test/lib 36 * @build JextractToolRunner 37 * @bug 8240752 38 * @summary jextract generates non-compilable code for special floating point values 39 * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED Test8240752 40 */ 41 public class Test8240752 extends JextractToolRunner { 42 private float getFloatConstant(Class<?> cls, String name) { 43 Method method = findMethod(cls, name); 44 assertNotNull(method); 45 assertEquals(method.getReturnType(), float.class); 46 try { 47 return (float)method.invoke(null); 48 } catch (Exception exp) { 49 System.err.println(exp); 50 assertTrue(false, "should not reach here"); 51 } 52 return 0.0f; 53 } 54 55 private double getDoubleConstant(Class<?> cls, String name) { 56 Method method = findMethod(cls, name); 57 assertNotNull(method); 58 assertEquals(method.getReturnType(), double.class); 59 try { 60 return (double)method.invoke(null); 61 } catch (Exception exp) { 62 System.err.println(exp); 63 assertTrue(false, "should not reach here"); 64 } 65 return 0.0d; 66 } 67 68 @Test 69 public void testConstants() { 70 Path floatConstsOutput = getOutputFilePath("floatconstsgen"); 71 Path floatConstsH = getInputFilePath("float_constants.h"); 72 run("-d", floatConstsOutput.toString(), floatConstsH.toString()).checkSuccess(); 73 try(Loader loader = classLoader(floatConstsOutput)) { 74 Class<?> cls = loader.loadClass("float_constants_h"); 75 assertNotNull(cls); 76 77 double d = getDoubleConstant(cls, "NAN"); 78 assertTrue(Double.isNaN(d)); 79 d = getDoubleConstant(cls, "PINFINITY"); 80 assertTrue(Double.isInfinite(d) && d > 0); 81 d = getDoubleConstant(cls, "NINFINITY"); 82 assertTrue(Double.isInfinite(d) && d < 0); 83 84 float f = getFloatConstant(cls, "NANF"); 85 assertTrue(Float.isNaN(f)); 86 f = getFloatConstant(cls, "PINFINITYF"); 87 assertTrue(Float.isInfinite(f) && f > 0); 88 f = getFloatConstant(cls, "NINFINITYF"); 89 assertTrue(Float.isInfinite(f) && f < 0); 90 } finally { 91 deleteDir(floatConstsOutput); 92 } 93 } 94 }