< prev index next >

test/jdk/java/foreign/TestLayoutEquality.java

Print this page

 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 /*
 26  * @test
 27  * @modules jdk.incubator.foreign/jdk.internal.foreign
 28  *
 29  * @run testng TestLayoutEquality
 30  */
 31 
 32 import jdk.incubator.foreign.MemoryLayout;
 33 import jdk.incubator.foreign.ValueLayout;
 34 import jdk.internal.foreign.PlatformLayouts;
 35 import org.testng.annotations.DataProvider;
 36 import org.testng.annotations.Test;
 37 
 38 import java.lang.reflect.Field;
 39 import java.util.ArrayList;
 40 import java.util.List;
 41 









 42 import static org.testng.Assert.*;
 43 
 44 public class TestLayoutEquality {
 45 
 46     @Test(dataProvider = "layoutConstants")
 47     public void testReconstructedEquality(ValueLayout layout) {
 48         ValueLayout newLayout = MemoryLayout.valueLayout(layout.bitSize(), layout.order());


 49 
 50         // properties should be equal
 51         assertEquals(newLayout.bitSize(), layout.bitSize());
 52         assertEquals(newLayout.bitAlignment(), layout.bitAlignment());
 53         assertEquals(newLayout.name(), layout.name());
 54         assertEquals(newLayout.attributes().toArray().length, 0);
 55         assertEquals(layout.attributes().toArray().length, 1);
 56 
 57         // but equals should return false, because one is a ValueLayout with a CLinker kind
 58         assertNotEquals(newLayout, layout);
 59     }
 60 
 61     @DataProvider
 62     public static Object[][] layoutConstants() throws ReflectiveOperationException {
 63         List<ValueLayout> testValues = new ArrayList<>();
 64 
 65         addLayoutConstants(testValues, PlatformLayouts.SysV.class);
 66         addLayoutConstants(testValues, PlatformLayouts.Win64.class);
 67         addLayoutConstants(testValues, PlatformLayouts.AArch64.class);
 68 
 69         return testValues.stream().map(e -> new Object[]{ e }).toArray(Object[][]::new);
 70     }
 71 
 72     private static void addLayoutConstants(List<ValueLayout> testValues, Class<?> cls) throws ReflectiveOperationException {
 73         for (Field f : cls.getFields()) {
 74             if (f.getName().startsWith("C_"))
 75                 testValues.add((ValueLayout) f.get(null));
 76         }
 77     }
 78 























 79 }

 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 /*
 26  * @test
 27  * @modules jdk.incubator.foreign/jdk.internal.foreign
 28  *
 29  * @run testng TestLayoutEquality
 30  */
 31 
 32 import jdk.incubator.foreign.MemoryAddress;
 33 import jdk.incubator.foreign.ValueLayout;
 34 import jdk.internal.foreign.PlatformLayouts;
 35 import org.testng.annotations.DataProvider;
 36 import org.testng.annotations.Test;
 37 
 38 import java.lang.reflect.Field;
 39 import java.util.ArrayList;
 40 import java.util.List;
 41 
 42 import static jdk.incubator.foreign.ValueLayout.ADDRESS;
 43 import static jdk.incubator.foreign.ValueLayout.JAVA_BOOLEAN;
 44 import static jdk.incubator.foreign.ValueLayout.JAVA_BYTE;
 45 import static jdk.incubator.foreign.ValueLayout.JAVA_CHAR;
 46 import static jdk.incubator.foreign.ValueLayout.JAVA_DOUBLE;
 47 import static jdk.incubator.foreign.ValueLayout.JAVA_FLOAT;
 48 import static jdk.incubator.foreign.ValueLayout.JAVA_INT;
 49 import static jdk.incubator.foreign.ValueLayout.JAVA_LONG;
 50 import static jdk.incubator.foreign.ValueLayout.JAVA_SHORT;
 51 import static org.testng.Assert.*;
 52 
 53 public class TestLayoutEquality {
 54 
 55     @Test(dataProvider = "layoutConstants")
 56     public void testReconstructedEquality(ValueLayout layout) {
 57         ValueLayout newLayout = valueLayoutForCarrier(layout.carrier());
 58         newLayout = newLayout.withBitAlignment(layout.bitAlignment());
 59         newLayout = newLayout.withOrder(layout.order());
 60 
 61         // properties should be equal
 62         assertEquals(newLayout.bitSize(), layout.bitSize());
 63         assertEquals(newLayout.bitAlignment(), layout.bitAlignment());
 64         assertEquals(newLayout.name(), layout.name());


 65 
 66         // layouts should be equals
 67         assertEquals(newLayout, layout);
 68     }
 69 
 70     @DataProvider
 71     public static Object[][] layoutConstants() throws ReflectiveOperationException {
 72         List<ValueLayout> testValues = new ArrayList<>();
 73 
 74         addLayoutConstants(testValues, PlatformLayouts.SysV.class);
 75         addLayoutConstants(testValues, PlatformLayouts.Win64.class);
 76         addLayoutConstants(testValues, PlatformLayouts.AArch64.class);
 77 
 78         return testValues.stream().map(e -> new Object[]{ e }).toArray(Object[][]::new);
 79     }
 80 
 81     private static void addLayoutConstants(List<ValueLayout> testValues, Class<?> cls) throws ReflectiveOperationException {
 82         for (Field f : cls.getFields()) {
 83             if (f.getName().startsWith("C_"))
 84                 testValues.add((ValueLayout) f.get(null));
 85         }
 86     }
 87 
 88     static ValueLayout valueLayoutForCarrier(Class<?> carrier) {
 89         if (carrier == boolean.class) {
 90             return JAVA_BOOLEAN;
 91         } else if (carrier == char.class) {
 92             return JAVA_CHAR;
 93         } else if (carrier == byte.class) {
 94             return JAVA_BYTE;
 95         } else if (carrier == short.class) {
 96             return JAVA_SHORT;
 97         } else if (carrier == int.class) {
 98             return JAVA_INT;
 99         } else if (carrier == long.class) {
100             return JAVA_LONG;
101         } else if (carrier == float.class) {
102             return JAVA_FLOAT;
103         } else if (carrier == double.class) {
104             return JAVA_DOUBLE;
105         } else if (carrier == MemoryAddress.class) {
106             return ADDRESS;
107         } else {
108             throw new UnsupportedOperationException();
109         }
110     }
111 }
< prev index next >