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.nio.file.Path;
 25 
 26 import jdk.incubator.foreign.CLinker;
 27 import jdk.incubator.foreign.GroupLayout;
 28 import jdk.incubator.foreign.MemoryLayout;
 29 import org.testng.annotations.Test;
 30 import static org.testng.Assert.assertNotNull;
 31 import static org.testng.Assert.assertTrue;
 32 
 33 /*
 34  * @test
 35  * @modules jdk.incubator.jextract
 36  * @library /test/lib
 37  * @build JextractToolRunner
 38  * @bug 8240811
 39  * @summary jextract generates non-compilable code for name collision between a struct and a global variable
 40  * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED Test8240811
 41  */
 42 public class Test8240811 extends JextractToolRunner {
 43     @Test
 44     public void testNameCollision() {
 45         Path nameCollisionOutput = getOutputFilePath("name_collision_gen");
 46         Path nameCollisionH = getInputFilePath("name_collision.h");
 47         run("-d", nameCollisionOutput.toString(), nameCollisionH.toString()).checkSuccess();
 48         try(Loader loader = classLoader(nameCollisionOutput)) {
 49             Class<?> cls = loader.loadClass("name_collision_h");
 50             assertNotNull(cls);
 51 
 52             // check foo layout
 53             Class<?> fooCls = loader.loadClass("foo");
 54             MemoryLayout fooLayout = findLayout(fooCls);
 55             assertNotNull(fooLayout);
 56             assertTrue(((GroupLayout)fooLayout).isStruct());
 57             checkField(fooLayout, "x",  C_INT);
 58             checkField(fooLayout, "y",  C_INT);
 59             checkField(fooLayout, "z",  C_INT);
 60 
 61             MemoryLayout fooVarLayout = findLayout(cls, "foo");
 62             assertNotNull(fooVarLayout);
 63 
 64             // check foo2 layout
 65             Class<?> foo2Cls = loader.loadClass("foo2");
 66             MemoryLayout foo2Layout = findLayout(foo2Cls);
 67             assertNotNull(foo2Layout);
 68             assertTrue(((GroupLayout)foo2Layout).isUnion());
 69             checkField(foo2Layout, "i", C_INT);
 70             checkField(foo2Layout, "l", C_LONG);
 71 
 72             MemoryLayout foo2VarLayout = findLayout(cls, "foo2");
 73             assertNotNull(foo2VarLayout);
 74 
 75             MemoryLayout barVarLayout = findLayout(cls, "bar");
 76             assertNotNull(barVarLayout);
 77 
 78             // check bar layout
 79             Class<?> barCls = loader.loadClass("bar");
 80             MemoryLayout barLayout = findLayout(barCls);
 81             assertNotNull(barLayout);
 82             assertTrue(((GroupLayout)barLayout).isStruct());
 83             checkField(barLayout, "f1",  C_FLOAT);
 84             checkField(barLayout, "f2",  C_FLOAT);
 85 
 86             MemoryLayout bar2VarLayout = findLayout(cls, "bar2");
 87             assertNotNull(bar2VarLayout);
 88 
 89             // check bar layout
 90             Class<?> bar2Cls = loader.loadClass("bar2");
 91             MemoryLayout bar2Layout = findLayout(bar2Cls);
 92             assertNotNull(bar2Layout);
 93             assertTrue(((GroupLayout)bar2Layout).isUnion());
 94             checkField(bar2Layout, "f", C_FLOAT);
 95             checkField(bar2Layout, "d", C_DOUBLE);
 96         } finally {
 97             deleteDir(nameCollisionOutput);
 98         }
 99     }
100 }